top.v 4.6 KB

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