top.v 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * top.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2019-2020 Sylvain Munaut <tnt@246tNt.com>
  7. * SPDX-License-Identifier: CERN-OHL-P-2.0
  8. */
  9. `default_nettype none
  10. `include "boards.vh"
  11. module top (
  12. // SPI
  13. inout wire spi_mosi,
  14. inout wire spi_miso,
  15. inout wire spi_clk,
  16. output wire spi_flash_cs_n,
  17. `ifdef HAS_PSRAM
  18. output wire spi_ram_cs_n,
  19. `endif
  20. // USB
  21. inout wire usb_dp,
  22. inout wire usb_dn,
  23. output wire usb_pu,
  24. // Debug UART
  25. input wire uart_rx,
  26. output wire uart_tx,
  27. // Button
  28. input wire btn,
  29. // LED
  30. output wire [2:0] rgb,
  31. // Audio output
  32. output wire [1:0] audio,
  33. input wire midi_rx,
  34. output wire midi_tx,
  35. // Clock
  36. input wire clk_in
  37. );
  38. localparam integer SPRAM_AW = 14; /* 14 => 64k, 15 => 128k */
  39. localparam integer WB_N = 8;
  40. localparam integer WB_DW = 32;
  41. localparam integer WB_AW = 16;
  42. localparam integer WB_RW = WB_DW * WB_N;
  43. localparam integer WB_MW = WB_DW / 8;
  44. genvar i;
  45. // Signals
  46. // -------
  47. // Wishbone
  48. wire [WB_AW-1:0] wb_addr;
  49. wire [WB_DW-1:0] wb_rdata [0:WB_N-1];
  50. wire [WB_RW-1:0] wb_rdata_flat;
  51. wire [WB_DW-1:0] wb_wdata;
  52. wire [WB_MW-1:0] wb_wmsk;
  53. wire [WB_N -1:0] wb_cyc;
  54. wire wb_we;
  55. wire [WB_N -1:0] wb_ack;
  56. // USB
  57. wire usb_sof;
  58. // WarmBoot
  59. reg boot_now;
  60. reg [1:0] boot_sel;
  61. // Clock / Reset logic
  62. wire clk_24m;
  63. wire clk_48m;
  64. wire rst;
  65. // SoC
  66. // ---
  67. soc_picorv32_base #(
  68. .WB_N (WB_N),
  69. .WB_DW (WB_DW),
  70. .WB_AW (WB_AW),
  71. .SPRAM_AW(SPRAM_AW)
  72. ) base_I (
  73. .wb_addr (wb_addr),
  74. .wb_rdata(wb_rdata_flat),
  75. .wb_wdata(wb_wdata),
  76. .wb_wmsk (wb_wmsk),
  77. .wb_we (wb_we),
  78. .wb_cyc (wb_cyc),
  79. .wb_ack (wb_ack),
  80. .clk (clk_24m),
  81. .rst (rst)
  82. );
  83. for (i=0; i<WB_N; i=i+1)
  84. assign wb_rdata_flat[i*WB_DW+:WB_DW] = wb_rdata[i];
  85. // UART [1]
  86. // ----
  87. uart_wb #(
  88. .DIV_WIDTH(12),
  89. .DW(WB_DW)
  90. ) uart_I (
  91. .uart_tx (uart_tx),
  92. .uart_rx (uart_rx),
  93. .wb_addr (wb_addr[1:0]),
  94. .wb_rdata (wb_rdata[1]),
  95. .wb_we (wb_we),
  96. .wb_wdata (wb_wdata),
  97. .wb_cyc (wb_cyc[1]),
  98. .wb_ack (wb_ack[1]),
  99. .clk (clk_24m),
  100. .rst (rst)
  101. );
  102. // SPI [2]
  103. // ---
  104. ice40_spi_wb #(
  105. `ifdef HAS_PSRAM
  106. .N_CS(2),
  107. `else
  108. .N_CS(1),
  109. `endif
  110. .WITH_IOB(1),
  111. .UNIT(0)
  112. ) spi_I (
  113. .pad_mosi (spi_mosi),
  114. .pad_miso (spi_miso),
  115. .pad_clk (spi_clk),
  116. `ifdef HAS_PSRAM
  117. .pad_csn ({spi_ram_cs_n, spi_flash_cs_n}),
  118. `else
  119. .pad_csn (spi_flash_cs_n),
  120. `endif
  121. .wb_addr (wb_addr[3:0]),
  122. .wb_rdata (wb_rdata[2]),
  123. .wb_wdata (wb_wdata),
  124. .wb_we (wb_we),
  125. .wb_cyc (wb_cyc[2]),
  126. .wb_ack (wb_ack[2]),
  127. .clk (clk_24m),
  128. .rst (rst)
  129. );
  130. // RGB LEDs [3]
  131. // --------
  132. ice40_rgb_wb #(
  133. .CURRENT_MODE("0b1"),
  134. .RGB0_CURRENT("0b000001"),
  135. .RGB1_CURRENT("0b000001"),
  136. .RGB2_CURRENT("0b000001")
  137. ) rgb_I (
  138. .pad_rgb (rgb),
  139. .wb_addr (wb_addr[4:0]),
  140. .wb_rdata (wb_rdata[3]),
  141. .wb_wdata (wb_wdata),
  142. .wb_we (wb_we),
  143. .wb_cyc (wb_cyc[3]),
  144. .wb_ack (wb_ack[3]),
  145. .clk (clk_24m),
  146. .rst (rst)
  147. );
  148. // USB [4 & 5]
  149. // ---
  150. soc_usb #(
  151. .DW(WB_DW)
  152. ) usb_I (
  153. .usb_dp (usb_dp),
  154. .usb_dn (usb_dn),
  155. .usb_pu (usb_pu),
  156. .wb_addr (wb_addr[11:0]),
  157. .wb_rdata (wb_rdata[4]),
  158. .wb_wdata (wb_wdata),
  159. .wb_we (wb_we),
  160. .wb_cyc (wb_cyc[5:4]),
  161. .wb_ack (wb_ack[5:4]),
  162. .usb_sof (usb_sof),
  163. .clk_sys (clk_24m),
  164. .clk_48m (clk_48m),
  165. .rst (rst)
  166. );
  167. assign wb_rdata[5] = 0;
  168. // Audio PCM [6]
  169. // ---------
  170. audio_pcm pcm_I (
  171. .audio (audio),
  172. .wb_addr (wb_addr[1:0]),
  173. .wb_rdata (wb_rdata[6]),
  174. .wb_wdata (wb_wdata),
  175. .wb_we (wb_we),
  176. .wb_cyc (wb_cyc[6]),
  177. .wb_ack (wb_ack[6]),
  178. .usb_sof (usb_sof),
  179. .clk (clk_24m),
  180. .rst (rst)
  181. );
  182. // Midi [7]
  183. //---------
  184. uart_wb #(
  185. .DIV_WIDTH(12),
  186. .DW(WB_DW)
  187. ) midi_I (
  188. .uart_tx (midi_tx),
  189. .uart_rx (midi_rx),
  190. .wb_addr (wb_addr[1:0]),
  191. .wb_rdata (wb_rdata[7]),
  192. .wb_we (wb_we),
  193. .wb_wdata (wb_wdata),
  194. .wb_cyc (wb_cyc[7]),
  195. .wb_ack (wb_ack[7]),
  196. .clk (clk_24m),
  197. .rst (rst)
  198. );
  199. // Warm Boot
  200. // ---------
  201. // Bus interface
  202. always @(posedge clk_24m or posedge rst)
  203. if (rst) begin
  204. boot_now <= 1'b0;
  205. boot_sel <= 2'b00;
  206. end else if (wb_cyc[0] & wb_we & (wb_addr[2:0] == 3'b000)) begin
  207. boot_now <= wb_wdata[2];
  208. boot_sel <= wb_wdata[1:0];
  209. end
  210. assign wb_rdata[0] = 0;
  211. assign wb_ack[0] = wb_cyc[0];
  212. // Helper
  213. dfu_helper #(
  214. .TIMER_WIDTH(24),
  215. .BTN_MODE(3),
  216. .DFU_MODE(0)
  217. ) dfu_helper_I (
  218. .boot_now(boot_now),
  219. .boot_sel(boot_sel),
  220. .btn_pad(btn),
  221. .btn_val(),
  222. .rst_req(),
  223. .clk(clk_24m),
  224. .rst(rst)
  225. );
  226. // Clock / Reset
  227. // -------------
  228. `ifdef SIM
  229. reg clk_48m_s = 1'b0;
  230. reg clk_24m_s = 1'b0;
  231. reg rst_s = 1'b1;
  232. always #10.42 clk_48m_s <= !clk_48m_s;
  233. always #20.84 clk_24m_s <= !clk_24m_s;
  234. initial begin
  235. #200 rst_s = 0;
  236. end
  237. assign clk_48m = clk_48m_s;
  238. assign clk_24m = clk_24m_s;
  239. assign rst = rst_s;
  240. `else
  241. sysmgr sys_mgr_I (
  242. .clk_in(clk_in),
  243. .rst_in(1'b0),
  244. .clk_48m(clk_48m),
  245. .clk_24m(clk_24m),
  246. .rst_out(rst)
  247. );
  248. `endif
  249. endmodule // top