top.sv 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * This is top level entity file.
  3. * It includes all cpu external modules like UART
  4. * and SDRAM controller.
  5. */
  6. `include "const.sv"
  7. `ifdef OISC
  8. `include "oisc/cpu.sv"
  9. `endif
  10. `ifdef RISC
  11. `include "risc/cpu.sv"
  12. `endif
  13. module top(
  14. input CLK50, // Clock 50MHz
  15. // Board connections
  16. input [3:0] SWITCH, // 4 Dip switches
  17. input [1:0] KEY, // 2 Keys
  18. output [7:0] LED, // 8 LEDs
  19. // UART
  20. input RX, // UART Receive
  21. output TX, // UART Transmit
  22. // SDRAM
  23. inout [15:0] DRAM_DQ, // Data
  24. output [12:0] DRAM_ADDR, // Address
  25. output [1:0] DRAM_DQM, // Byte Data Mask
  26. output DRAM_CLK, // Clock
  27. output DRAM_CKE, // Clock Enable
  28. output DRAM_WE_N, // Write Enable
  29. output DRAM_CAS_N, // Column Address Strobe
  30. output DRAM_RAS_N, // Row Address Strobe
  31. output DRAM_CS_N, // Chip Select
  32. output [1:0] DRAM_BA // Bank Address
  33. );
  34. `ifdef SYNTHESIS
  35. initial $display("Assuming this is synthesis");
  36. // Adding external reset source
  37. wire debug_rst;
  38. sys_ss#("RST") sys_ss_rst(debug_rst);
  39. assign rst = ~KEY[0] | debug_rst;
  40. `else
  41. initial $display("Assuming this is simulation");
  42. assign rst = ~KEY[0];
  43. `endif
  44. /* Clocks */
  45. wire mclk; // Master clock 1MHz (for cpu)
  46. wire fclk; // Fast clock 100MHz (for sdram)
  47. wire aclk; // Auxiliary clock 32,768kHz (for timers)
  48. wire mclk0;
  49. `ifdef DEBUG
  50. wire mclk_debug, clkd;
  51. sys_ss#("CLKD", 1) sys_clkd(clkd);
  52. sys_ss#("MCLK", 1) sys_mclk(mclk_debug);
  53. assign mclk = clkd ? mclk_debug : mclk0;
  54. `else
  55. assign mclk = mclk0;
  56. `endif
  57. pll_clk#(.CLK1_MUL(`MCLK_PLL_MUL),.CLK1_DIV(`MCLK_PLL_DIV))
  58. pll_clk0 (
  59. .inclk0(CLK50),
  60. .areset(0),
  61. .c0(fclk),
  62. .c1(mclk0),
  63. .c2(aclk)
  64. );
  65. //clk_dive#(28'd50) clk_div_mclk(CLK50, mclk);
  66. //assign mclk = ~KEY[1];
  67. //assign mclk = CLK50;
  68. wire [23:0] ram_addr;
  69. wire [15:0] ram_wr_data;
  70. wire [15:0] ram_rd_data;
  71. wire ram_wr_en;
  72. wire ram_rd_en;
  73. wire ram_busy;
  74. wire ram_rd_ready;
  75. wire ram_rd_ack;
  76. `ifdef OISC
  77. ram#({`RAMDIR, "oisc8.data"}) ram_block0(ram_addr[$clog2(`RAM_SIZE)-1:0], mclk, ram_wr_data, ram_wr_en, ram_rd_en, ram_rd_data);
  78. `endif
  79. `ifdef RISC
  80. ram#({`RAMDIR, "risc8.data"}) ram_block0(ram_addr[$clog2(`RAM_SIZE)-1:0], mclk, ram_wr_data, ram_wr_en, ram_rd_en, ram_rd_data);
  81. `endif
  82. `ifdef DEBUG
  83. reg[23:0] ram_addr_rd_pr, ram_addr_wr_pr;
  84. reg[15:0] ram_data_rd_pr, ram_data_wr_pr;
  85. reg ram_rd_pr0;
  86. always_ff@(posedge mclk) begin
  87. ram_rd_pr0 <= ram_rd_en;
  88. if(ram_wr_en) begin
  89. ram_addr_wr_pr <= ram_addr;
  90. ram_data_wr_pr <= ram_wr_data;
  91. end
  92. if(ram_rd_en) ram_addr_rd_pr <= ram_addr;
  93. if(ram_rd_pr0) ram_data_rd_pr <= ram_rd_data;
  94. end
  95. sys_sp#("ramw",40) sys_ramw({ram_addr_wr_pr,ram_data_wr_pr});
  96. sys_sp#("ramr",40) sys_ramr({ram_addr_rd_pr,ram_data_rd_pr});
  97. `endif
  98. //sdram_block sdram0(
  99. // .mclk(mclk),
  100. // .fclk(fclk),
  101. //// .rst_n(~rst),
  102. // .ram_addr(racm_addr),
  103. // .ram_wr_data(ram_wr_data),
  104. // .ram_rd_data(ram_rd_data),
  105. // .ram_wr_en(ram_wr_en),
  106. // .ram_rd_en(ram_rd_en),
  107. // .ram_busy(ram_busy),
  108. // .ram_rd_ready(ram_rd_ready),
  109. // .ram_rd_ack(ram_rd_ack),
  110. // .DRAM_DQ(DRAM_DQ),
  111. // .DRAM_ADDR(DRAM_ADDR),
  112. // .DRAM_DQM(DRAM_DQM),
  113. // .DRAM_CLK(DRAM_CLK),
  114. // .DRAM_CKE(DRAM_CKE),
  115. // .DRAM_WE_N(DRAM_WE_N),
  116. // .DRAM_CAS_N(DRAM_CAS_N),
  117. // .DRAM_RAS_N(DRAM_RAS_N),
  118. // .DRAM_CS_N(DRAM_CS_N),
  119. // .DRAM_BA(DRAM_BA)
  120. //);
  121. //Communication block
  122. wire [7:0] com0_addr, com0_wr, com0_rd;
  123. wire com0_interrupt;
  124. com_block com0 (
  125. `ifdef DEBUG
  126. .clk(mclk1),
  127. `else
  128. .clk(mclk),
  129. `endif
  130. .rst(rst),
  131. .addr(com0_addr),
  132. .in_data(com0_wr),
  133. .out_data(com0_rd),
  134. .interrupt(com0_interrupt),
  135. .leds(LED),
  136. .switches(SWITCH),
  137. .uart0_rx(RX),
  138. .uart0_tx(TX),
  139. .key1(KEY[1])
  140. );
  141. // Processor
  142. processor_port port0 (
  143. .clk(mclk),
  144. .rst(rst),
  145. .ram_addr(ram_addr),
  146. .ram_wr_data(ram_wr_data),
  147. .ram_rd_data(ram_rd_data),
  148. .ram_wr_en(ram_wr_en),
  149. .ram_rd_en(ram_rd_en),
  150. .ram_busy(ram_busy),
  151. .ram_rd_ready(ram_rd_ready),
  152. .ram_rd_ack(ram_rd_ack),
  153. .com_addr(com0_addr),
  154. .com_wr(com0_wr),
  155. .com_rd(com0_rd),
  156. .com_interrupt(com0_interrupt)
  157. );
  158. `ifdef OISC
  159. oisc8_cpu cpu_block0(port0);
  160. `endif
  161. `ifdef RISC
  162. risc8_cpu cpu_block0(port0);
  163. `endif
  164. endmodule
  165. module clk_dive(clock_in,clock_out);
  166. input clock_in; // input clock on FPGA
  167. output clock_out; // output clock after dividing the input clock by divisor
  168. reg[27:0] counter=28'd0;
  169. parameter DIVISOR = 28'd2;
  170. always @(posedge clock_in)
  171. begin
  172. counter <= counter + 28'd1;
  173. if(counter>=(DIVISOR-1))
  174. counter <= 28'd0;
  175. end
  176. assign clock_out = (counter<DIVISOR/2)?1'b0:1'b1;
  177. endmodule
  178. `timescale 1ns/1ns
  179. module top_tb;
  180. logic CLK50; // Clock 50MHz
  181. logic [3:0] SWITCH; // 4 Dip switches
  182. logic [1:0] KEY; // 2 Keys
  183. wire [7:0] LED; // 8 LEDs
  184. logic RX; // UART Receive
  185. logic TX; // UART Transmit
  186. wire [15:0] DRAM_DQ; // Data
  187. logic [12:0] DRAM_ADDR; // Address
  188. logic [1:0] DRAM_DQM; // Byte Data Mask
  189. logic DRAM_CLK; // Clock
  190. logic DRAM_CKE; // Clock Enable
  191. logic DRAM_WE_N; // Write Enable
  192. logic DRAM_CAS_N; // Column Address Strobe
  193. logic DRAM_RAS_N; // Row Address Strobe
  194. logic DRAM_CS_N; // Chip Select
  195. logic [1:0] DRAM_BA; // Bank Address
  196. top top0(
  197. CLK50,
  198. SWITCH,
  199. KEY,
  200. LED,
  201. RX,
  202. TX,
  203. DRAM_DQ,
  204. DRAM_ADDR,
  205. DRAM_DQM,
  206. DRAM_CLK,
  207. DRAM_CKE,
  208. DRAM_WE_N,
  209. DRAM_CAS_N,
  210. DRAM_RAS_N,
  211. DRAM_CS_N,
  212. DRAM_BA
  213. );
  214. integer cycles = 0;
  215. initial forever begin
  216. #10ns CLK50 = ~CLK50;
  217. cycles = cycles + 1;
  218. end
  219. initial begin
  220. CLK50 = 0;
  221. KEY[0] = 0;
  222. KEY[1] = 1;
  223. SWITCH = 4'b0110;
  224. RX = 0;
  225. #1100ns;
  226. KEY[0] = 1;
  227. #400ns;
  228. end
  229. integer f;
  230. initial begin
  231. f = $fopen("oisc8_mod_u16_2.log","w");
  232. forever begin
  233. `ifdef OISC
  234. if(top0.cpu_block0.pc0.pcr==16'h0009) break;
  235. #1us;
  236. $fwrite(f,"%H %b %H %H\n",
  237. top0.cpu_block0.pc0.pcr,
  238. top0.cpu_block0.bus0.imm,
  239. top0.cpu_block0.bus0.instr_dst,
  240. top0.cpu_block0.bus0.instr_src
  241. );
  242. `endif
  243. `ifdef RISC
  244. if(top0.cpu_block0.rom_block0.ff_addr==16'h000b) break;
  245. #1us;
  246. //$fwrite(f,"%H %b %H %H\n",
  247. // top0.cpu_block0.pc0.pcp,
  248. // top0.cpu_block0.bus0.imm,
  249. // top0.cpu_block0.bus0.instr_dst,
  250. // top0.cpu_block0.bus0.instr_src
  251. //);
  252. `endif
  253. end
  254. $fclose(f);
  255. $finish;
  256. end
  257. endmodule