B32P_tb.v 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * Testbench
  3. * Simulates the B32P CPU
  4. */
  5. // Set timescale
  6. `timescale 1 ns/1 ns
  7. // Include modules
  8. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/CPU/CPU.v"
  9. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/CPU/ALU.v"
  10. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/CPU/ControlUnit.v"
  11. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/CPU/InstructionDecoder.v"
  12. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/CPU/Regbank.v"
  13. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/CPU/Stack.v"
  14. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/CPU/InstrMem.v"
  15. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/CPU/DataMem.v"
  16. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/CPU/Regr.v"
  17. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/CPU/IntController.v"
  18. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/CPU/Arbiter.v"
  19. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/Memory/VRAM.v"
  20. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/Memory/mt48lc16m16a2.v"
  21. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/Memory/w25q128jv.v"
  22. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/Memory/SDRAMcontroller.v"
  23. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/Memory/SPIreader.v"
  24. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/Memory/ROM.v"
  25. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/Memory/MemoryUnit.v"
  26. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/IO/Keyboard.v"
  27. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/IO/OStimer.v"
  28. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/IO/UARTtx.v"
  29. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/IO/UARTrx.v"
  30. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/IO/SimpleSPI.v"
  31. `include "/home/bart/Documents/FPGA/FPGC6/Verilog/modules/IO/LEDvisualizer.v"
  32. // Define testmodule
  33. module B32P_tb;
  34. //---------------CPU----------------
  35. // CPU I/O
  36. reg clk = 0;
  37. reg clk_SDRAM = 0;
  38. reg reset = 0;
  39. reg int1 = 1'b0;
  40. reg int2 = 1'b0;
  41. reg int3 = 1'b0;
  42. reg int4 = 1'b0;
  43. reg int5 = 1'b0;
  44. reg int6 = 1'b0;
  45. reg int7 = 1'b0;
  46. reg int8 = 1'b0;
  47. reg int9 = 1'b0;
  48. reg int10 = 1'b0;
  49. //Bus
  50. wire [26:0] bus_addr;
  51. wire [31:0] bus_data;
  52. wire bus_we;
  53. wire bus_start;
  54. wire [31:0] bus_q;
  55. wire bus_done;
  56. CPU cpu(
  57. .clk (clk),
  58. .reset (reset),
  59. .int1(int1),
  60. .int2(int2),
  61. .int3(int3),
  62. .int4(int4),
  63. .int5(int5),
  64. .int6(int6),
  65. .int7(int7),
  66. .int8(int8),
  67. .int9(int9),
  68. .int10(int10),
  69. // bus
  70. .bus_addr(bus_addr),
  71. .bus_data(bus_data),
  72. .bus_we(bus_we),
  73. .bus_start(bus_start),
  74. .bus_q(bus_q),
  75. .bus_done(bus_done)
  76. );
  77. //----------DEV-------------
  78. //---------------------------VRAM32---------------------------------
  79. //VRAM32 I/O
  80. wire vram32_gpu_clk;
  81. wire [13:0] vram32_gpu_addr;
  82. wire [31:0] vram32_gpu_d;
  83. wire vram32_gpu_we;
  84. wire [31:0] vram32_gpu_q;
  85. wire vram32_cpu_clk;
  86. wire [13:0] vram32_cpu_addr;
  87. wire [31:0] vram32_cpu_d;
  88. wire vram32_cpu_we;
  89. wire [31:0] vram32_cpu_q;
  90. //because FSX will not write to VRAM
  91. assign vram32_gpu_we = 1'b0;
  92. assign vram32_gpu_d = 32'd0;
  93. VRAM #(
  94. .WIDTH(32),
  95. .WORDS(1056),
  96. .LIST("/home/bart/Documents/FPGA/FPGC5/Verilog/memory/vram32.list")
  97. ) vram32(
  98. //CPU port
  99. .cpu_clk (clk),
  100. .cpu_d (vram32_cpu_d),
  101. .cpu_addr (vram32_cpu_addr),
  102. .cpu_we (vram32_cpu_we),
  103. .cpu_q (vram32_cpu_q),
  104. //GPU port
  105. .gpu_clk (clkMuxOut),
  106. .gpu_d (vram32_gpu_d),
  107. .gpu_addr (vram32_gpu_addr),
  108. .gpu_we (vram32_gpu_we),
  109. .gpu_q (vram32_gpu_q)
  110. );
  111. //---------------------------VRAM322--------------------------------
  112. //VRAM322 I/O
  113. wire vram322_gpu_clk;
  114. wire [13:0] vram322_gpu_addr;
  115. wire [31:0] vram322_gpu_d;
  116. wire vram322_gpu_we;
  117. wire [31:0] vram322_gpu_q;
  118. //because FSX will not write to VRAM
  119. assign vram322_gpu_we = 1'b0;
  120. assign vram322_gpu_d = 32'd0;
  121. VRAM #(
  122. .WIDTH(32),
  123. .WORDS(1056),
  124. .LIST("/home/bart/Documents/FPGA/FPGC5/Verilog/memory/vram32.list")
  125. ) vram322(
  126. //CPU port
  127. .cpu_clk (clk),
  128. .cpu_d (vram32_cpu_d),
  129. .cpu_addr (vram32_cpu_addr),
  130. .cpu_we (vram32_cpu_we),
  131. .cpu_q (),
  132. //GPU port
  133. .gpu_clk (clkMuxOut),
  134. .gpu_d (vram322_gpu_d),
  135. .gpu_addr (vram322_gpu_addr),
  136. .gpu_we (vram322_gpu_we),
  137. .gpu_q (vram322_gpu_q)
  138. );
  139. //--------------------------VRAM8--------------------------------
  140. //VRAM8 I/O
  141. wire vram8_gpu_clk;
  142. wire [13:0] vram8_gpu_addr;
  143. wire [7:0] vram8_gpu_d;
  144. wire vram8_gpu_we;
  145. wire [7:0] vram8_gpu_q;
  146. wire vram8_cpu_clk;
  147. wire [13:0] vram8_cpu_addr;
  148. wire [7:0] vram8_cpu_d;
  149. wire vram8_cpu_we;
  150. wire [7:0] vram8_cpu_q;
  151. //because FSX will not write to VRAM
  152. assign vram8_gpu_we = 1'b0;
  153. assign vram8_gpu_d = 8'd0;
  154. VRAM #(
  155. .WIDTH(8),
  156. .WORDS(8194),
  157. .LIST("/home/bart/Documents/FPGA/FPGC5/Verilog/memory/vram8.list")
  158. ) vram8(
  159. //CPU port
  160. .cpu_clk (clk),
  161. .cpu_d (vram8_cpu_d),
  162. .cpu_addr (vram8_cpu_addr),
  163. .cpu_we (vram8_cpu_we),
  164. .cpu_q (vram8_cpu_q),
  165. //GPU port
  166. .gpu_clk (clkMuxOut),
  167. .gpu_d (vram8_gpu_d),
  168. .gpu_addr (vram8_gpu_addr),
  169. .gpu_we (vram8_gpu_we),
  170. .gpu_q (vram8_gpu_q)
  171. );
  172. //--------------------------VRAMSPR--------------------------------
  173. //VRAMSPR I/O
  174. wire vramSPR_gpu_clk;
  175. wire [13:0] vramSPR_gpu_addr;
  176. wire [8:0] vramSPR_gpu_d;
  177. wire vramSPR_gpu_we;
  178. wire [8:0] vramSPR_gpu_q;
  179. wire vramSPR_cpu_clk;
  180. wire [13:0] vramSPR_cpu_addr;
  181. wire [8:0] vramSPR_cpu_d;
  182. wire vramSPR_cpu_we;
  183. wire [8:0] vramSPR_cpu_q;
  184. //because FSX will not write to VRAM
  185. assign vramSPR_gpu_we = 1'b0;
  186. assign vramSPR_gpu_d = 9'd0;
  187. VRAM #(
  188. .WIDTH(9),
  189. .WORDS(256),
  190. .LIST("/home/bart/Documents/FPGA/FPGC5/Verilog/memory/vramSPR.list")
  191. ) vramSPR(
  192. //CPU port
  193. .cpu_clk (clk),
  194. .cpu_d (vramSPR_cpu_d),
  195. .cpu_addr (vramSPR_cpu_addr),
  196. .cpu_we (vramSPR_cpu_we),
  197. .cpu_q (vramSPR_cpu_q),
  198. //GPU port
  199. .gpu_clk (clkMuxOut),
  200. .gpu_d (vramSPR_gpu_d),
  201. .gpu_addr (vramSPR_gpu_addr),
  202. .gpu_we (vramSPR_gpu_we),
  203. .gpu_q (vramSPR_gpu_q)
  204. );
  205. //-------------------ROM-------------------------
  206. //ROM I/O
  207. wire [8:0] rom_addr;
  208. wire [31:0] rom_q;
  209. ROM rom(
  210. .clk (clk),
  211. .reset (reset),
  212. .address (rom_addr),
  213. .q (rom_q)
  214. );
  215. //SPI0 Flash
  216. wire SPI0_clk;
  217. wire SPI0_cs;
  218. wire SPI0_data;
  219. wire SPI0_wp;
  220. wire SPI0_q;
  221. wire SPI0_hold;
  222. W25Q128JV spiFlash (
  223. .CLK (SPI0_clk),
  224. .DIO (SPI0_data),
  225. .CSn (SPI0_cs),
  226. .WPn (SPI0_wp),
  227. .HOLDn (SPI0_hold),
  228. .DO (SPI0_q)
  229. );
  230. //SDRAM
  231. wire SDRAM_CLK; // SDRAM clock
  232. wire [15 : 0] SDRAM_DQ; // SDRAM I/O
  233. wire [12 : 0] SDRAM_A; // SDRAM Address
  234. wire [1 : 0] SDRAM_BA; // Bank Address
  235. wire SDRAM_CKE; // Synchronous Clock Enable
  236. wire SDRAM_CSn; // CS#
  237. wire SDRAM_RASn; // RAS#
  238. wire SDRAM_CASn; // CAS#
  239. wire SDRAM_WEn; // WE#
  240. wire [1 : 0] SDRAM_DQM; // Mask
  241. //Run SDRAM at 100MHz
  242. assign SDRAM_CLK = clk_SDRAM;
  243. mt48lc16m16a2 sdram (
  244. .Dq (SDRAM_DQ),
  245. .Addr (SDRAM_A),
  246. .Ba (SDRAM_BA),
  247. .Clk (SDRAM_CLK),
  248. .Cke (SDRAM_CKE),
  249. .Cs_n (SDRAM_CSn),
  250. .Ras_n (SDRAM_RASn),
  251. .Cas_n (SDRAM_CASn),
  252. .We_n (SDRAM_WEn),
  253. .Dqm (SDRAM_DQM)
  254. );
  255. //HDMI
  256. wire [3:0] TMDS_p;
  257. wire [3:0] TMDS_n;
  258. //SPI1
  259. wire SPI1_clk;
  260. wire SPI1_cs;
  261. wire SPI1_mosi;
  262. wire SPI1_miso;
  263. wire SPI1_rst;
  264. reg SPI1_nint;
  265. //SPI2
  266. wire SPI2_clk;
  267. wire SPI2_cs;
  268. wire SPI2_mosi;
  269. wire SPI2_miso;
  270. wire SPI2_rst;
  271. reg SPI2_nint;
  272. //SPI3
  273. wire SPI3_clk;
  274. wire SPI3_cs;
  275. wire SPI3_mosi;
  276. wire SPI3_miso;
  277. wire SPI3_nrst;
  278. reg SPI3_int;
  279. //SPI4
  280. wire SPI4_clk;
  281. wire SPI4_cs;
  282. wire SPI4_mosi;
  283. wire SPI4_miso;
  284. reg SPI4_gp;
  285. //UART0
  286. reg UART0_in;
  287. wire UART0_out;
  288. reg UART0_dtr;
  289. //UART1
  290. //reg UART1_in;
  291. //wire UART1_out;
  292. //UART2
  293. reg UART2_in;
  294. wire UART2_out;
  295. //PS/2
  296. reg PS2_clk;
  297. reg PS2_data;
  298. //Led
  299. wire led;
  300. //GPIO
  301. wire [3:0] GPO;
  302. reg [3:0] GPI;
  303. //DIP Switch
  304. reg [3:0] DIPS;
  305. //----------------Memory Unit--------------------
  306. //Memory Unit I/O
  307. //Interrupt signals
  308. wire OST1_int, OST2_int, OST3_int;
  309. wire UART0_rx_int, UART2_rx_int;
  310. wire PS2_int;
  311. wire SPI0_QSPI;
  312. MemoryUnit mu(
  313. //clock
  314. .clk (clk),
  315. .clk_SDRAM (clk_SDRAM),
  316. .reset (reset),
  317. //CPU connection (Bus)
  318. .bus_addr (bus_addr),
  319. .bus_data (bus_data),
  320. .bus_we (bus_we),
  321. .bus_start (bus_start),
  322. .bus_q (bus_q),
  323. .bus_done (bus_done),
  324. /********
  325. * MEMORY
  326. ********/
  327. //SPI Flash / SPI0
  328. .SPIflash_data (SPI0_data),
  329. .SPIflash_q (SPI0_q),
  330. .SPIflash_wp (SPI0_wp),
  331. .SPIflash_hold (SPI0_hold),
  332. .SPIflash_cs (SPI0_cs),
  333. .SPIflash_clk (SPI0_clk),
  334. //SDRAM
  335. .SDRAM_CSn (SDRAM_CSn),
  336. .SDRAM_WEn (SDRAM_WEn),
  337. .SDRAM_CASn (SDRAM_CASn),
  338. .SDRAM_CKE (SDRAM_CKE),
  339. .SDRAM_RASn (SDRAM_RASn),
  340. .SDRAM_A (SDRAM_A),
  341. .SDRAM_BA (SDRAM_BA),
  342. .SDRAM_DQM (SDRAM_DQM),
  343. .SDRAM_DQ (SDRAM_DQ),
  344. //VRAM32 cpu port
  345. .VRAM32_cpu_d (vram32_cpu_d),
  346. .VRAM32_cpu_addr (vram32_cpu_addr),
  347. .VRAM32_cpu_we (vram32_cpu_we),
  348. .VRAM32_cpu_q (vram32_cpu_q),
  349. //VRAM8 cpu port
  350. .VRAM8_cpu_d (vram8_cpu_d),
  351. .VRAM8_cpu_addr (vram8_cpu_addr),
  352. .VRAM8_cpu_we (vram8_cpu_we),
  353. .VRAM8_cpu_q (vram8_cpu_q),
  354. //VRAMspr cpu port
  355. .VRAMspr_cpu_d (vramSPR_cpu_d),
  356. .VRAMspr_cpu_addr (vramSPR_cpu_addr),
  357. .VRAMspr_cpu_we (vramSPR_cpu_we),
  358. .VRAMspr_cpu_q (vramSPR_cpu_q),
  359. //ROM
  360. .ROM_addr (rom_addr),
  361. .ROM_q (rom_q),
  362. /********
  363. * I/O
  364. ********/
  365. //UART0 (Main USB)
  366. .UART0_in (UART0_in),
  367. .UART0_out (UART0_out),
  368. .UART0_rx_interrupt (UART0_rx_int),
  369. //UART1 (APU)
  370. /*.UART1_in (),
  371. .UART1_out (),
  372. .UART1_rx_interrupt (),
  373. */
  374. //UART2 (GP)
  375. .UART2_in (UART2_in),
  376. .UART2_out (UART2_out),
  377. .UART2_rx_interrupt (UART2_rx_int),
  378. //SPI0 (Flash)
  379. //declared under MEMORY
  380. .SPI0_QSPI (SPI0_QSPI),
  381. //SPI1 (USB0/CH376T)
  382. .SPI1_clk (SPI1_clk),
  383. .SPI1_cs (SPI1_cs),
  384. .SPI1_mosi (SPI1_mosi),
  385. .SPI1_miso (SPI1_miso),
  386. .SPI1_nint (SPI1_nint_stable),
  387. //SPI2 (USB1/CH376T)
  388. .SPI2_clk (SPI2_clk),
  389. .SPI2_cs (SPI2_cs),
  390. .SPI2_mosi (SPI2_mosi),
  391. .SPI2_miso (SPI2_miso),
  392. .SPI2_nint (SPI2_nint_stable),
  393. //SPI3 (W5500)
  394. .SPI3_clk (SPI3_clk),
  395. .SPI3_cs (SPI3_cs),
  396. .SPI3_mosi (SPI3_mosi),
  397. .SPI3_miso (SPI3_miso),
  398. .SPI3_int (SPI3_int_stable),
  399. //SPI4 (EXT/GP)
  400. .SPI4_clk (SPI4_clk),
  401. .SPI4_cs (SPI4_cs),
  402. .SPI4_mosi (SPI4_mosi),
  403. .SPI4_miso (SPI4_miso),
  404. .SPI4_GP (SPI4_gp_stable),
  405. //GPIO (Separated GPI and GPO until GPIO module is implemented)
  406. .GPI (GPI[3:0]),
  407. .GPO (GPO[3:0]),
  408. //OStimers
  409. .OST1_int (OST1_int),
  410. .OST2_int (OST2_int),
  411. .OST3_int (OST3_int),
  412. //SNESpad
  413. /*
  414. .SNES_clk (),
  415. .SNES_latch (),
  416. .SNES_data (),
  417. */
  418. //PS/2
  419. .PS2_clk (PS2_clk),
  420. .PS2_data (PS2_data),
  421. .PS2_int (PS2_int), //Scan code ready signal
  422. //Boot mode
  423. .boot_mode (boot_mode_stable)
  424. );
  425. initial
  426. begin
  427. // dump everything for GTKwave
  428. $dumpfile("/home/bart/Documents/FPGA/FPGC6/Verilog/output/wave.vcd");
  429. $dumpvars;
  430. reset = 0;
  431. //repeat(5120) #10 clk = ~clk; // 50MHz
  432. repeat(4)
  433. begin
  434. #5 clk_SDRAM = ~clk_SDRAM; clk = ~clk; //50MHz
  435. #5 clk_SDRAM = ~clk_SDRAM; //100MHz
  436. end
  437. reset = 1;
  438. repeat(4)
  439. begin
  440. #5 clk_SDRAM = ~clk_SDRAM; clk = ~clk; //50MHz
  441. #5 clk_SDRAM = ~clk_SDRAM; //100MHz
  442. end
  443. reset = 0;
  444. repeat(22)
  445. begin
  446. #5 clk_SDRAM = ~clk_SDRAM; clk = ~clk; //50MHz
  447. #5 clk_SDRAM = ~clk_SDRAM; //100MHz
  448. end
  449. int1 = 1'b1;
  450. int2 = 1'b1;
  451. int3 = 1'b1;
  452. int4 = 1'b1;
  453. int5 = 1'b1;
  454. int6 = 1'b1;
  455. int7 = 1'b1;
  456. int8 = 1'b1;
  457. int9 = 1'b1;
  458. int10 = 1'b1;
  459. repeat(500)
  460. begin
  461. #5 clk_SDRAM = ~clk_SDRAM; clk = ~clk; //50MHz
  462. #5 clk_SDRAM = ~clk_SDRAM; //100MHz
  463. end
  464. int1 = 1'b0;
  465. int2 = 1'b0;
  466. int3 = 1'b0;
  467. int4 = 1'b0;
  468. int5 = 1'b0;
  469. int6 = 1'b0;
  470. int7 = 1'b0;
  471. int8 = 1'b0;
  472. int9 = 1'b0;
  473. int10 = 1'b0;
  474. repeat(100)
  475. begin
  476. #5 clk_SDRAM = ~clk_SDRAM; clk = ~clk; //50MHz
  477. #5 clk_SDRAM = ~clk_SDRAM; //100MHz
  478. end
  479. int1 = 1'b1;
  480. int2 = 1'b1;
  481. int3 = 1'b1;
  482. int4 = 1'b1;
  483. int5 = 1'b1;
  484. int6 = 1'b1;
  485. int7 = 1'b1;
  486. int8 = 1'b1;
  487. int9 = 1'b1;
  488. int10 = 1'b1;
  489. repeat(500)
  490. begin
  491. #5 clk_SDRAM = ~clk_SDRAM; clk = ~clk; //50MHz
  492. #5 clk_SDRAM = ~clk_SDRAM; //100MHz
  493. end
  494. /*
  495. repeat(4096)
  496. begin
  497. #5 clk_SDRAM = ~clk_SDRAM; clk = ~clk; //50MHz
  498. #5 clk_SDRAM = ~clk_SDRAM; //100MHz
  499. end
  500. */
  501. #1 $finish;
  502. end
  503. endmodule