/Active/ProgramROMRAM.cs

# · C# · 256 lines · 165 code · 30 blank · 61 comment · 1 complexity · 8db8ebcf8fc42b1b74f0e281c18468e7 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace RapidHDL
  5. {
  6. public class ProgramROMRAM : Component
  7. {
  8. public NodeVector O_IR;
  9. public NodeVector O_CONST;
  10. public NodeVector I_PC;
  11. public NodeVector I_WRITE_DATA_CONST;
  12. public NodeVector I_WRITE_DATA_IR;
  13. public NodeVector I_ADDRESS;
  14. public NodeVector I_WRITE;
  15. ProgramTable oROMProgram;
  16. ProgramTable oRAMProgram;
  17. //NodeVector nvReset;
  18. ClockComponent oClockComponet;
  19. int iPCWidth = 0;
  20. int iRAMStartAddress = 0;
  21. int iRAMDepth = 0;
  22. int iConstWidth=0;
  23. string sFileText="";
  24. Dictionary<int, int> dicAddressToLine = new Dictionary<int, int>();
  25. List<int> lBreakpoints = new List<int>();
  26. FPGABlock oFPGARAM;
  27. public ProgramROMRAM(Component poParentComponent, string psName, ProgramTable poROMProgramTable, ProgramTable poRAMProgramTable, int piRAMStartAddress, int piRAMDepth, int piConstWidth, ClockComponent poClock)
  28. : base (poParentComponent,psName)
  29. {
  30. oROMProgram = poROMProgramTable;
  31. O_IR = this.CreateNodeVector("IR_O", oROMProgram.IRWidth);
  32. O_CONST = this.CreateNodeVector("CONST_O", piConstWidth);
  33. iPCWidth = Conversion.MinBitWidth(piRAMStartAddress + piRAMDepth - 1);
  34. I_PC = this.CreateNodeVector("PC_I",iPCWidth);
  35. I_WRITE_DATA_CONST = this.CreateNodeVector("WRITE_DATA_CONST_I",piConstWidth);
  36. I_WRITE_DATA_IR = this.CreateNodeVector("WRITE_DATA_IR_I",oROMProgram.IRWidth);
  37. I_ADDRESS = this.CreateNodeVector("ADDRESS_I", iPCWidth);
  38. iRAMStartAddress = piRAMStartAddress;
  39. iRAMDepth = piRAMDepth;
  40. iConstWidth = piConstWidth;
  41. I_WRITE = this.CreateNodeVector("WRITE_I", 1);
  42. oClockComponet = poClock;
  43. oRAMProgram = poRAMProgramTable;
  44. }
  45. NodeVector nvSelect;
  46. NodeVector nvRAMAddress;
  47. NodeVector nvRAMIR;
  48. NodeVector nvRAMConst;
  49. public override void GenerateStructure()
  50. {
  51. //SignalSource oWrite = RapidHardware.SystemInterface.AddSignalSource(Name + "_WRITE_I",I_WRITE,0);
  52. //SignalSource oWriteDataConst = RapidHardware.SystemInterface.AddSignalSource(Name + "_WRITE_DATA_CONST_I",I_WRITE_DATA_CONST,0);
  53. //SignalSource oWriteDataIR = RapidHardware.SystemInterface.AddSignalSource(Name + "_WRITE_DATA_IR_I",I_WRITE_DATA_IR,0);
  54. //SignalSource oAddress = RapidHardware.SystemInterface.AddSignalSource(Name + "_ADDRESS_I",I_ADDRESS,0);
  55. ProgramROM oProgramROM = new ProgramROM(this, "program_rom", oROMProgram);
  56. RAM oRAM = new RAM(this, "program_ram", oClockComponet, oROMProgram.IRWidth + iConstWidth, iRAMDepth,true);
  57. Mux oIRMux = new Mux(this, "ir_mux", oROMProgram.IRWidth, 2);
  58. Mux oConstMux = new Mux(this, "const_mux", iConstWidth, 2);
  59. CompareGreaterEqual oCompare = new CompareGreaterEqual(this, "ram_sel", iPCWidth, iPCWidth);
  60. oRAM.I_READ.Join(1);
  61. oCompare.InputNodesA.Join(I_PC);
  62. oCompare.InputNodesB.Join(iRAMStartAddress);
  63. oIRMux.SelectNodes.Join(oCompare.Output);
  64. nvSelect = oCompare.Output;
  65. oConstMux.SelectNodes.Join(oCompare.Output);
  66. oIRMux.InputNodes[0].Join(oProgramROM.O_IR);
  67. NodeVector nvROMConst = this.CreateNodeVector("rom_const",iConstWidth);
  68. if (nvROMConst.Width <= oProgramROM.O_CONST.Width)
  69. {
  70. nvROMConst.Join(oProgramROM.O_CONST);
  71. }
  72. else
  73. {
  74. nvROMConst[0,oProgramROM.O_CONST.Width -1].Join(oProgramROM.O_CONST);
  75. nvROMConst[oProgramROM.O_CONST.Width,nvROMConst.Width -1].Join(0);
  76. }
  77. oConstMux.InputNodes[0].Join(nvROMConst);
  78. oIRMux.InputNodes[1].Join(oRAM.O_DATA[iConstWidth, iConstWidth + oROMProgram.IRWidth - 1]);
  79. oConstMux.InputNodes[1].Join(oRAM.O_DATA[0, iConstWidth - 1]);
  80. oProgramROM.I_PC.Join(I_PC);
  81. oRAM.I_WRITE.Join(I_WRITE);
  82. oRAM.I_DATA[0,iConstWidth-1].Join(I_WRITE_DATA_CONST);
  83. oRAM.I_DATA[iConstWidth , iConstWidth + oROMProgram.IRWidth - 1].Join(I_WRITE_DATA_IR);
  84. Mux oAddressMux = new Mux(this,"addr_mux",iPCWidth,2);
  85. oAddressMux.SelectNodes.Join(I_WRITE);
  86. oAddressMux.InputNodes[0].Join(I_PC);
  87. oAddressMux.InputNodes[1].Join(I_ADDRESS);
  88. ALUComponent oALU = new ALUComponent(this, "sub_offset", ALUType.Subtract, iPCWidth);
  89. oALU.I_A.Join(oAddressMux.OutputNodes);
  90. oALU.I_B.Join(iRAMStartAddress);
  91. oRAM.I_ADDRESS.Join(oALU.O_A);
  92. oRAM.I_ENABLE.Join(1);
  93. nvRAMAddress = oALU.O_A;
  94. nvRAMIR = oIRMux.InputNodes[1];
  95. nvRAMConst = oConstMux.InputNodes[1];
  96. O_IR.Join(oIRMux.OutputNodes);
  97. O_CONST.Join(oConstMux.OutputNodes);
  98. UpdateProgramTrace(oROMProgram);
  99. UpdateProgramTrace(oRAMProgram);
  100. RegisterRAM(oRAMProgram);
  101. }
  102. public void AddSignalViews()
  103. {
  104. ParentComponent.RapidHardware.SystemInterface.AddSignalView(Name + "_RAM_SEL", nvSelect);
  105. ParentComponent.RapidHardware.SystemInterface.AddSignalView(Name + "_RAM_ADDR", nvRAMAddress);
  106. ParentComponent.RapidHardware.SystemInterface.AddSignalView(Name + "_RAM_IR", nvRAMIR);
  107. ParentComponent.RapidHardware.SystemInterface.AddSignalView(Name + "_RAM_CONST", nvRAMConst);
  108. }
  109. public void SignalDisplay(HardwareScript poScript, int piClockTime)
  110. {
  111. poScript.SignalView(piClockTime, Name + "_RAM_SEL");
  112. poScript.SignalView(piClockTime, Name + "_RAM_ADDR",SignalFormat.Unsigned);
  113. poScript.SignalView(piClockTime, Name + "_RAM_IR", SignalFormat.Unsigned);
  114. poScript.SignalView(piClockTime, Name + "_RAM_CONST", SignalFormat.Unsigned);
  115. }
  116. private void UpdateProgramTrace(ProgramTable oProgram)
  117. {
  118. int iLines = Utility.Count(sFileText,"\n");
  119. foreach (int iAddress in oProgram.AddressToLine.Keys)
  120. {
  121. int iLine = oProgram.AddressToLine[iAddress] + iLines;
  122. dicAddressToLine.Add(iAddress, iLine);
  123. }
  124. sFileText += oProgram.FileText;
  125. foreach (int iBreakpoint in oProgram.Breakpoints)
  126. {
  127. lBreakpoints.Add(iBreakpoint);
  128. }
  129. }
  130. private void RegisterRAM(ProgramTable poRAMProgram)
  131. {
  132. oFPGARAM = new FPGABlock();
  133. foreach (int iAddress in poRAMProgram.ProgramRAMIR.Keys)
  134. {
  135. int iIR = poRAMProgram.ProgramRAMIR[iAddress];
  136. int iConst = poRAMProgram.ProgramRAMConstant[iAddress];
  137. oFPGARAM.LoadA(iIR);
  138. oFPGARAM.LoadB(iConst);
  139. oFPGARAM.WriteProgRAM(iAddress);
  140. }
  141. }
  142. public bool WriteRAMToFPGA()
  143. {
  144. ParentComponent.RapidHardware.FPGAInterface.Reset();
  145. ParentComponent.RapidHardware.FPGAInterface.StartClock();
  146. IOInterface.PostFPGA(oFPGARAM);
  147. //return true;
  148. ParentComponent.RapidHardware.FPGAInterface.StopClock();
  149. FPGABlock oStartBlock = new FPGABlock();
  150. oStartBlock.AddEntry(0, 61, 0, true);
  151. IOInterface.PostFPGA(oStartBlock);
  152. return true;
  153. }
  154. public void LoadRAMProgram()
  155. {
  156. WriteRAMToFPGA();
  157. /*RapidHardware.FPGAInterface.StopClock();
  158. SignalSource oWrite = RapidHardware.SystemInterface.GetSignalSource(Name + "_WRITE_I");
  159. SignalSource oWriteDataConst = RapidHardware.SystemInterface.GetSignalSource(Name + "_WRITE_DATA_CONST_I");
  160. SignalSource oWriteDataIR = RapidHardware.SystemInterface.GetSignalSource(Name + "_WRITE_DATA_IR_I");
  161. SignalSource oAddress = RapidHardware.SystemInterface.GetSignalSource(Name + "_ADDRESS_I");
  162. SignalView oConstTest = RapidHardware.SystemInterface.GetSignalView(Name + "_RAM_CONST");
  163. SignalView oIRTest = RapidHardware.SystemInterface.GetSignalView(Name + "_RAM_IR");
  164. foreach (int iAddress in oRAMProgram.ProgramRAMConstant.Keys)
  165. {
  166. int iIR = oRAMProgram.ProgramRAMIR[iAddress];
  167. int iConst = oRAMProgram.ProgramRAMConstant[iAddress];
  168. RapidHardware.FPGAInterface.Go(100);
  169. System.Threading.Thread.Sleep(100);
  170. oWriteDataConst.ValueAsInt = iConst;
  171. oWriteDataIR.ValueAsInt = iIR;
  172. oAddress.ValueAsInt = iAddress;
  173. RapidHardware.FPGAInterface.Go(100);
  174. System.Threading.Thread.Sleep(100);
  175. oWrite.ValueAsInt = 1;
  176. RapidHardware.FPGAInterface.Go(100);
  177. System.Threading.Thread.Sleep(100);
  178. /*if (oConstTest.ValueAsInt != iConst)
  179. return;
  180. if (oIRTest.ValueAsInt != iIR)
  181. return;
  182. oWrite.ValueAsInt = 0;
  183. RapidHardware.FPGAInterface.Go(100);
  184. System.Threading.Thread.Sleep(100);
  185. }
  186. foreach (int iAddress in oRAMProgram.ProgramRAMConstant.Keys)
  187. {
  188. int iIR = oRAMProgram.ProgramRAMIR[iAddress];
  189. int iConst = oRAMProgram.ProgramRAMConstant[iAddress];
  190. RapidHardware.FPGAInterface.Go(100);
  191. System.Threading.Thread.Sleep(100);
  192. oWriteDataConst.ValueAsInt = 0;
  193. oWriteDataIR.ValueAsInt = 0;
  194. oAddress.ValueAsInt = iAddress;
  195. RapidHardware.FPGAInterface.Go(100);
  196. System.Threading.Thread.Sleep(100);
  197. if (oConstTest.ValueAsInt != iConst)
  198. return;
  199. if (oIRTest.ValueAsInt != iIR)
  200. return;
  201. }
  202. RapidHardware.FPGAInterface.Reset();
  203. */
  204. }
  205. public Dictionary<int, int> AddressToLine
  206. {
  207. get { return dicAddressToLine; }
  208. }
  209. public string FileText
  210. {
  211. get { return sFileText; }
  212. }
  213. public List<int> Breakpoints
  214. {
  215. get { return lBreakpoints; }
  216. }
  217. }
  218. }