top.v 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * top.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2021 Sylvain Munaut <tnt@246tNt.com>
  7. * SPDX-License-Identifier: CERN-OHL-P-2.0
  8. */
  9. `default_nettype none
  10. module top (
  11. // SPI
  12. inout wire [3:0] spi_io,
  13. inout wire spi_sck,
  14. inout wire [1:0] spi_cs_n,
  15. // Video output
  16. output wire [3:0] hdmi_r,
  17. output wire [3:0] hdmi_g,
  18. output wire [3:0] hdmi_b,
  19. output wire hdmi_hsync,
  20. output wire hdmi_vsync,
  21. output wire hdmi_de,
  22. output wire hdmi_clk,
  23. // Debug UART
  24. input wire uart_rx,
  25. output wire uart_tx,
  26. // Button
  27. input wire btn,
  28. // LED
  29. output wire [2:0] rgb,
  30. // Clock
  31. input wire clk_in
  32. );
  33. localparam integer WB_N = 4;
  34. localparam integer WB_DW = 32;
  35. localparam integer WB_AW = 22;
  36. localparam integer WB_RW = WB_DW * WB_N;
  37. localparam integer WB_MW = WB_DW / 8;
  38. genvar i;
  39. // Signals
  40. // -------
  41. // Vex Misc
  42. wire [31:0] vex_externalResetVector;
  43. wire vex_timerInterrupt;
  44. wire vex_softwareInterrupt;
  45. wire [31:0] vex_externalInterruptArray;
  46. // Vex busses
  47. wire i_axi_ar_valid;
  48. wire i_axi_ar_ready;
  49. wire [31:0] i_axi_ar_payload_addr;
  50. wire [ 7:0] i_axi_ar_payload_len;
  51. wire [ 1:0] i_axi_ar_payload_burst;
  52. wire [ 3:0] i_axi_ar_payload_cache;
  53. wire [ 2:0] i_axi_ar_payload_prot;
  54. wire i_axi_r_valid;
  55. wire i_axi_r_ready;
  56. wire [31:0] i_axi_r_payload_data;
  57. wire [ 1:0] i_axi_r_payload_resp;
  58. wire i_axi_r_payload_last;
  59. wire d_wb_cyc;
  60. wire d_wb_stb;
  61. wire d_wb_ack;
  62. wire d_wb_we;
  63. wire [29:0] d_wb_adr;
  64. wire [31:0] d_wb_dat_miso;
  65. wire [31:0] d_wb_dat_mosi;
  66. wire [ 3:0] d_wb_sel;
  67. wire d_wb_err;
  68. wire [ 1:0] d_wb_bte;
  69. wire [ 2:0] d_wb_cti;
  70. // RAM
  71. wire [27:0] ram_addr;
  72. wire [31:0] ram_rdata;
  73. wire [31:0] ram_wdata;
  74. wire [ 3:0] ram_wmsk;
  75. wire ram_we;
  76. // Cache Request / Response interface
  77. wire [27:0] cache_req_addr_pre;
  78. wire cache_req_valid;
  79. wire cache_req_write;
  80. wire [31:0] cache_req_wdata;
  81. wire [ 3:0] cache_req_wmsk;
  82. wire cache_resp_ack;
  83. wire cache_resp_nak;
  84. wire [31:0] cache_resp_rdata;
  85. // Memory interface
  86. wire [23:0] mi_addr;
  87. wire [ 6:0] mi_len;
  88. wire mi_rw;
  89. wire mi_linear;
  90. wire mi_valid;
  91. wire mi_ready;
  92. wire [31:0] mi_wdata;
  93. wire [ 3:0] mi_wmsk;
  94. wire mi_wack;
  95. wire mi_wlast;
  96. wire [31:0] mi_rdata;
  97. wire mi_rstb;
  98. wire mi_rlast;
  99. // QSPI PHY signals
  100. wire [15:0] phy_io_i;
  101. wire [15:0] phy_io_o;
  102. wire [ 3:0] phy_io_oe;
  103. wire [ 3:0] phy_clk_o;
  104. wire [ 1:0] phy_cs_o;
  105. // Wishbone
  106. wire [WB_AW-1:0] wb_addr;
  107. wire [WB_DW-1:0] wb_rdata [0:WB_N-1];
  108. wire [WB_RW-1:0] wb_rdata_flat;
  109. wire [WB_DW-1:0] wb_wdata;
  110. wire [WB_MW-1:0] wb_wmsk;
  111. wire wb_we;
  112. wire [WB_N -1:0] wb_cyc;
  113. wire [WB_N -1:0] wb_ack;
  114. // Clock / Reset logic
  115. wire clk_1x;
  116. wire clk_4x;
  117. wire sync_4x;
  118. wire rst;
  119. // SoC
  120. // ---
  121. // CPU
  122. VexRiscv cpu_I (
  123. .externalResetVector (vex_externalResetVector),
  124. .timerInterrupt (vex_timerInterrupt),
  125. .softwareInterrupt (vex_softwareInterrupt),
  126. .externalInterruptArray (vex_externalInterruptArray),
  127. .iBusAXI_ar_valid (i_axi_ar_valid),
  128. .iBusAXI_ar_ready (i_axi_ar_ready),
  129. .iBusAXI_ar_payload_addr (i_axi_ar_payload_addr),
  130. .iBusAXI_ar_payload_len (i_axi_ar_payload_len),
  131. .iBusAXI_ar_payload_burst (i_axi_ar_payload_burst),
  132. .iBusAXI_ar_payload_cache (i_axi_ar_payload_cache),
  133. .iBusAXI_ar_payload_prot (i_axi_ar_payload_prot),
  134. .iBusAXI_r_valid (i_axi_r_valid),
  135. .iBusAXI_r_ready (i_axi_r_ready),
  136. .iBusAXI_r_payload_data (i_axi_r_payload_data),
  137. .iBusAXI_r_payload_resp (i_axi_r_payload_resp),
  138. .iBusAXI_r_payload_last (i_axi_r_payload_last),
  139. .dBusWishbone_CYC (d_wb_cyc),
  140. .dBusWishbone_STB (d_wb_stb),
  141. .dBusWishbone_ACK (d_wb_ack),
  142. .dBusWishbone_WE (d_wb_we),
  143. .dBusWishbone_ADR (d_wb_adr),
  144. .dBusWishbone_DAT_MISO (d_wb_dat_miso),
  145. .dBusWishbone_DAT_MOSI (d_wb_dat_mosi),
  146. .dBusWishbone_SEL (d_wb_sel),
  147. .dBusWishbone_ERR (d_wb_err),
  148. .dBusWishbone_BTE (d_wb_bte),
  149. .dBusWishbone_CTI (d_wb_cti),
  150. .clk (clk_1x),
  151. .reset (rst)
  152. );
  153. // CPU interrupt wiring
  154. assign vex_externalResetVector = 32'h00000000;
  155. assign vex_timerInterrupt = 1'b0;
  156. assign vex_softwareInterrupt = 1'b0;
  157. assign vex_externalInterruptArray = 32'h00000000;
  158. // Cache bus interface / bridge
  159. mc_bus_vex #(
  160. .WB_N(WB_N)
  161. ) cache_bus_I (
  162. .i_axi_ar_valid (i_axi_ar_valid),
  163. .i_axi_ar_ready (i_axi_ar_ready),
  164. .i_axi_ar_payload_addr (i_axi_ar_payload_addr),
  165. .i_axi_ar_payload_len (i_axi_ar_payload_len),
  166. .i_axi_ar_payload_burst (i_axi_ar_payload_burst),
  167. .i_axi_ar_payload_cache (i_axi_ar_payload_cache),
  168. .i_axi_ar_payload_prot (i_axi_ar_payload_prot),
  169. .i_axi_r_valid (i_axi_r_valid),
  170. .i_axi_r_ready (i_axi_r_ready),
  171. .i_axi_r_payload_data (i_axi_r_payload_data),
  172. .i_axi_r_payload_resp (i_axi_r_payload_resp),
  173. .i_axi_r_payload_last (i_axi_r_payload_last),
  174. .d_wb_cyc (d_wb_cyc),
  175. .d_wb_stb (d_wb_stb),
  176. .d_wb_ack (d_wb_ack),
  177. .d_wb_we (d_wb_we),
  178. .d_wb_adr (d_wb_adr),
  179. .d_wb_dat_miso (d_wb_dat_miso),
  180. .d_wb_dat_mosi (d_wb_dat_mosi),
  181. .d_wb_sel (d_wb_sel),
  182. .d_wb_err (d_wb_err),
  183. .d_wb_bte (d_wb_bte),
  184. .d_wb_cti (d_wb_cti),
  185. .wb_addr (wb_addr),
  186. .wb_wdata (wb_wdata),
  187. .wb_wmsk (wb_wmsk),
  188. .wb_rdata (wb_rdata_flat),
  189. .wb_cyc (wb_cyc),
  190. .wb_we (wb_we),
  191. .wb_ack (wb_ack),
  192. .ram_addr (ram_addr),
  193. .ram_wdata (ram_wdata),
  194. .ram_wmsk (ram_wmsk),
  195. .ram_rdata (ram_rdata),
  196. .ram_we (ram_we),
  197. .req_addr_pre (cache_req_addr_pre),
  198. .req_valid (cache_req_valid),
  199. .req_write (cache_req_write),
  200. .req_wdata (cache_req_wdata),
  201. .req_wmsk (cache_req_wmsk),
  202. .resp_ack (cache_resp_ack),
  203. .resp_nak (cache_resp_nak),
  204. .resp_rdata (cache_resp_rdata),
  205. .clk (clk_1x),
  206. .rst (rst)
  207. );
  208. for (i=0; i<WB_N; i=i+1)
  209. assign wb_rdata_flat[i*WB_DW+:WB_DW] = wb_rdata[i];
  210. // Boot memory
  211. soc_bram #(
  212. .AW(8),
  213. .INIT_FILE("boot.hex")
  214. ) bram_I (
  215. .addr (ram_addr[7:0]),
  216. .rdata (ram_rdata),
  217. .wdata (ram_wdata),
  218. .wmsk (ram_wmsk),
  219. .we (ram_we),
  220. .clk (clk_1x)
  221. );
  222. // Cache
  223. mc_core #(
  224. .N_WAYS(4),
  225. .ADDR_WIDTH(24),
  226. .CACHE_LINE(32),
  227. .CACHE_SIZE(64)
  228. ) cache_I (
  229. .req_addr_pre (cache_req_addr_pre[23:0]),
  230. .req_valid (cache_req_valid),
  231. .req_write (cache_req_write),
  232. .req_wdata (cache_req_wdata),
  233. .req_wmsk (cache_req_wmsk),
  234. .resp_ack (cache_resp_ack),
  235. .resp_nak (cache_resp_nak),
  236. .resp_rdata (cache_resp_rdata),
  237. .mi_addr (mi_addr),
  238. .mi_len (mi_len),
  239. .mi_rw (mi_rw),
  240. .mi_valid (mi_valid),
  241. .mi_ready (mi_ready),
  242. .mi_wdata (mi_wdata),
  243. .mi_wack (mi_wack),
  244. .mi_wlast (mi_wlast),
  245. .mi_rdata (mi_rdata),
  246. .mi_rstb (mi_rstb),
  247. .mi_rlast (mi_rlast),
  248. .clk (clk_1x),
  249. .rst (rst)
  250. );
  251. // QSPI
  252. // ----
  253. // Simulation
  254. `ifdef SIM
  255. mem_sim #(
  256. .INIT_FILE("firmware.hex"),
  257. .AW(20)
  258. ) qpi_sim (
  259. .mi_addr ({mi_addr[22], mi_addr[18:0]}),
  260. .mi_len (mi_len),
  261. .mi_rw (mi_rw),
  262. .mi_valid (mi_valid),
  263. .mi_ready (mi_ready),
  264. .mi_wdata (mi_wdata),
  265. .mi_wack (mi_wack),
  266. .mi_wlast (mi_wlast),
  267. .mi_rdata (mi_rdata),
  268. .mi_rstb (mi_rstb),
  269. .mi_rlast (mi_rlast),
  270. .clk (clk_1x),
  271. .rst (rst)
  272. );
  273. assign wb_ack[0] = wb_cyc[0];
  274. `else
  275. // Controller
  276. qpi_memctrl #(
  277. .CMD_READ (16'hEB0B),
  278. .CMD_WRITE (16'h0202),
  279. .DUMMY_CLK (6),
  280. .PAUSE_CLK (8),
  281. .FIFO_DEPTH (1),
  282. .N_CS (2),
  283. .PHY_SPEED (4),
  284. .PHY_WIDTH (1),
  285. .PHY_DELAY (4)
  286. ) memctrl_I (
  287. .phy_io_i (phy_io_i),
  288. .phy_io_o (phy_io_o),
  289. .phy_io_oe (phy_io_oe),
  290. .phy_clk_o (phy_clk_o),
  291. .phy_cs_o (phy_cs_o),
  292. .mi_addr_cs (mi_addr[23:22]),
  293. .mi_addr ({mi_addr[21:0], 2'b00 }), /* 32 bits aligned */
  294. .mi_len (mi_len),
  295. .mi_rw (mi_rw),
  296. .mi_valid (mi_valid),
  297. .mi_ready (mi_ready),
  298. .mi_wdata ({mi_wdata[7:0], mi_wdata[15:8], mi_wdata[23:16], mi_wdata[31:24]}),
  299. .mi_wack (mi_wack),
  300. .mi_wlast (mi_wlast),
  301. .mi_rdata ({mi_rdata[7:0], mi_rdata[15:8], mi_rdata[23:16], mi_rdata[31:24]}),
  302. .mi_rstb (mi_rstb),
  303. .mi_rlast (mi_rlast),
  304. .wb_wdata (wb_wdata),
  305. .wb_rdata (wb_rdata[0]),
  306. .wb_addr (wb_addr[4:0]),
  307. .wb_we (wb_we),
  308. .wb_cyc (wb_cyc[0]),
  309. .wb_ack (wb_ack[0]),
  310. .clk (clk_1x),
  311. .rst (rst)
  312. );
  313. // PHY
  314. qpi_phy_ice40_4x #(
  315. .N_CS(2),
  316. .WITH_CLK(1)
  317. ) phy_I (
  318. .pad_io (spi_io),
  319. .pad_clk (spi_sck),
  320. .pad_cs_n (spi_cs_n),
  321. .phy_io_i (phy_io_i),
  322. .phy_io_o (phy_io_o),
  323. .phy_io_oe (phy_io_oe),
  324. .phy_clk_o (phy_clk_o),
  325. .phy_cs_o (phy_cs_o),
  326. .clk_1x (clk_1x),
  327. .clk_4x (clk_4x),
  328. .clk_sync (sync_4x)
  329. );
  330. `endif
  331. // Video [1]
  332. // -----
  333. vid_top vid_I (
  334. .hdmi_r (hdmi_r),
  335. .hdmi_g (hdmi_g),
  336. .hdmi_b (hdmi_b),
  337. .hdmi_hsync (hdmi_hsync),
  338. .hdmi_vsync (hdmi_vsync),
  339. .hdmi_de (hdmi_de),
  340. .hdmi_clk (hdmi_clk),
  341. .wb_addr (wb_addr[15:0]),
  342. .wb_rdata (wb_rdata[1]),
  343. .wb_wdata (wb_wdata),
  344. .wb_wmsk (wb_wmsk),
  345. .wb_we (wb_we),
  346. .wb_cyc (wb_cyc[1]),
  347. .wb_ack (wb_ack[1]),
  348. .clk (clk_1x),
  349. .rst (rst)
  350. );
  351. // UART [2]
  352. // ----
  353. uart_wb #(
  354. .DIV_WIDTH(12),
  355. .DW(WB_DW)
  356. ) uart_I (
  357. .uart_tx (uart_tx),
  358. .uart_rx (uart_rx),
  359. .wb_addr (wb_addr[1:0]),
  360. .wb_rdata (wb_rdata[2]),
  361. .wb_we (wb_we),
  362. .wb_wdata (wb_wdata),
  363. .wb_cyc (wb_cyc[2]),
  364. .wb_ack (wb_ack[2]),
  365. .clk (clk_1x),
  366. .rst (rst)
  367. );
  368. // LEDs [3]
  369. // ----
  370. ice40_rgb_wb #(
  371. .CURRENT_MODE("0b1"),
  372. .RGB0_CURRENT("0b000001"),
  373. .RGB1_CURRENT("0b000001"),
  374. .RGB2_CURRENT("0b000001")
  375. ) rgb_I (
  376. .pad_rgb (rgb),
  377. .wb_addr (wb_addr[4:0]),
  378. .wb_rdata (wb_rdata[3]),
  379. .wb_wdata (wb_wdata),
  380. .wb_we (wb_we),
  381. .wb_cyc (wb_cyc[3]),
  382. .wb_ack (wb_ack[3]),
  383. .clk (clk_1x),
  384. .rst (rst)
  385. );
  386. // Clock / Reset
  387. // -------------
  388. `ifdef SIM
  389. reg rst_s = 1'b1;
  390. reg clk_4x_s = 1'b0;
  391. reg clk_1x_s = 1'b0;
  392. reg [1:0] clk_sync_cnt = 2'b00;
  393. always #5 clk_4x_s <= !clk_4x_s;
  394. always #20 clk_1x_s <= !clk_1x_s;
  395. initial
  396. #200 rst_s = 0;
  397. always @(posedge clk_4x_s)
  398. if (rst)
  399. clk_sync_cnt <= 2'b00;
  400. else
  401. clk_sync_cnt <= clk_sync_cnt + 1;
  402. assign clk_4x = clk_4x_s;
  403. assign clk_1x = clk_1x_s;
  404. assign sync_4x = (clk_sync_cnt == 2'b10);
  405. assign rst = rst_s;
  406. `else
  407. sysmgr sys_mgr_I (
  408. .clk_in (clk_in),
  409. .clk_1x (clk_1x),
  410. .clk_4x (clk_4x),
  411. .sync_4x (sync_4x),
  412. .rst (rst)
  413. );
  414. `endif
  415. endmodule // top