top.v 4.2 KB

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