top.v 11 KB

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