top.v 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 TODO: remove later
  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. // Buttons (2 for now to test up and down)
  28. input wire btn_n,
  29. input wire btn_1,
  30. input wire btn_2,
  31. // LEDs to blink to show that a value change has been registered?
  32. output wire[4:0] led,
  33. // GPIOs for out signal
  34. output wire out1,
  35. output wire out2,
  36. output wire out3,
  37. // LED TODO: remove later
  38. output wire [2:0] rgb,
  39. // Clock
  40. input wire clk_in
  41. );
  42. localparam integer SPRAM_AW = 14; /* 14 => 64k, 15 => 128k */
  43. localparam integer WB_N = 5; // TODO: Reduce
  44. localparam integer WB_DW = 32;
  45. localparam integer WB_AW = 16;
  46. localparam integer WB_RW = WB_DW * WB_N;
  47. localparam integer WB_MW = WB_DW / 8;
  48. localparam integer FAST_PWM_WIDTH = 8;
  49. localparam integer PULSE_COUNTER_WIDTH = 8;
  50. localparam integer SLOW_PWM_WIDTH = 14;
  51. genvar i;
  52. // Signals
  53. // -------
  54. // Wishbone
  55. wire [WB_AW-1:0] wb_addr;
  56. wire [WB_DW-1:0] wb_rdata [0:WB_N-1];
  57. wire [WB_RW-1:0] wb_rdata_flat;
  58. wire [WB_DW-1:0] wb_wdata;
  59. wire [WB_MW-1:0] wb_wmsk;
  60. wire [WB_N -1:0] wb_cyc;
  61. wire wb_we;
  62. wire [WB_N -1:0] wb_ack;
  63. // WarmBoot
  64. reg boot_now;
  65. reg [1:0] boot_sel;
  66. // Clock / Reset logic
  67. wire clk_24m;
  68. wire clk_48m;
  69. wire rst;
  70. // 3 signal
  71. wire [SLOW_PWM_WIDTH-1:0] period1;
  72. wire [SLOW_PWM_WIDTH-1:0] delay1;
  73. wire [SLOW_PWM_WIDTH-1:0] period2;
  74. wire [SLOW_PWM_WIDTH-1:0] delay2;
  75. wire [FAST_PWM_WIDTH-1:0] period3;
  76. wire [FAST_PWM_WIDTH-1:0] duty3;
  77. wire [SLOW_PWM_WIDTH-1:0] delay3;
  78. wire [PULSE_COUNTER_WIDTH-1:0] npuls3;
  79. wire [1:0] odd_train_flag;
  80. wire ena_odd_out3;
  81. // Mailbox signal wires
  82. wire [16*16-1:0] mailbox_regs_flat; // Flattened register array (16 registers of 16 bits each)
  83. // SoC
  84. // ---
  85. soc_picorv32_base #(
  86. .WB_N (WB_N),
  87. .WB_DW (WB_DW),
  88. .WB_AW (WB_AW),
  89. .SPRAM_AW(SPRAM_AW)
  90. ) base_I (
  91. .wb_addr (wb_addr),
  92. .wb_rdata(wb_rdata_flat),
  93. .wb_wdata(wb_wdata),
  94. .wb_wmsk (wb_wmsk),
  95. .wb_we (wb_we),
  96. .wb_cyc (wb_cyc),
  97. .wb_ack (wb_ack),
  98. .clk (clk_24m),
  99. .rst (rst)
  100. );
  101. for (i=0; i<WB_N; i=i+1)
  102. assign wb_rdata_flat[i*WB_DW+:WB_DW] = wb_rdata[i];
  103. // UART [1]
  104. // ----
  105. uart_wb #(
  106. .DIV_WIDTH(12),
  107. .DW(WB_DW)
  108. ) uart_I (
  109. .uart_tx (uart_tx),
  110. .uart_rx (uart_rx),
  111. .wb_addr (wb_addr[1:0]),
  112. .wb_rdata (wb_rdata[1]),
  113. .wb_we (wb_we),
  114. .wb_wdata (wb_wdata),
  115. .wb_cyc (wb_cyc[1]),
  116. .wb_ack (wb_ack[1]),
  117. .clk (clk_24m),
  118. .rst (rst)
  119. );
  120. // SPI [2]
  121. // ---
  122. ice40_spi_wb #(
  123. `ifdef HAS_PSRAM
  124. .N_CS(2),
  125. `else
  126. .N_CS(1),
  127. `endif
  128. .WITH_IOB(1),
  129. .UNIT(0)
  130. ) spi_I (
  131. .pad_mosi (spi_mosi),
  132. .pad_miso (spi_miso),
  133. .pad_clk (spi_clk),
  134. `ifdef HAS_PSRAM
  135. .pad_csn ({spi_ram_cs_n, spi_flash_cs_n}),
  136. `else
  137. .pad_csn (spi_flash_cs_n),
  138. `endif
  139. .wb_addr (wb_addr[3:0]),
  140. .wb_rdata (wb_rdata[2]),
  141. .wb_wdata (wb_wdata),
  142. .wb_we (wb_we),
  143. .wb_cyc (wb_cyc[2]),
  144. .wb_ack (wb_ack[2]),
  145. .clk (clk_24m),
  146. .rst (rst)
  147. );
  148. // RGB LEDs [3]
  149. // --------
  150. ice40_rgb_wb #(
  151. .CURRENT_MODE("0b1"),
  152. .RGB0_CURRENT("0b000001"),
  153. .RGB1_CURRENT("0b000001"),
  154. .RGB2_CURRENT("0b000001")
  155. ) rgb_I (
  156. .pad_rgb (rgb),
  157. .wb_addr (wb_addr[4:0]),
  158. .wb_rdata (wb_rdata[3]),
  159. .wb_wdata (wb_wdata),
  160. .wb_we (wb_we),
  161. .wb_cyc (wb_cyc[3]),
  162. .wb_ack (wb_ack[3]),
  163. .clk (clk_24m),
  164. .rst (rst)
  165. );
  166. // WB Mailbox [4]
  167. // ----------
  168. mailbox_wb #(
  169. .AW(4),
  170. .DW(WB_DW)
  171. ) mailbox_I (
  172. .clk(clk_24m),
  173. .rst(rst),
  174. .wb_addr(wb_addr[3:0]),
  175. .wb_wdata(wb_wdata),
  176. .wb_rdata(wb_rdata[4]),
  177. .wb_we(wb_we),
  178. .wb_cyc(wb_cyc[4]),
  179. .wb_ack(wb_ack[4]),
  180. .registers_flat(mailbox_regs_flat)
  181. );
  182. // 3 Signal
  183. // --------
  184. assign period1 = mailbox_regs_flat[15:0]; // First 16 bits for period1
  185. assign delay1 = mailbox_regs_flat[31:16]; // Next 16 bits for delay1
  186. assign period2 = mailbox_regs_flat[47:32]; // Next 16 bits for period2
  187. assign delay2 = mailbox_regs_flat[63:48]; // Next 16 bits for delay2
  188. assign period3 = mailbox_regs_flat[79:64]; // Next 16 bits for period3
  189. assign duty3 = mailbox_regs_flat[95:80]; // Next 16 bits for duty3
  190. assign delay3 = mailbox_regs_flat[111:96]; // Next 16 bits for delay3
  191. assign npuls3 = mailbox_regs_flat[127:112]; // Next 16 bits for npuls3
  192. assign odd_train_flag = mailbox_regs_flat[143:128]; // Next 16 bits for odd_train_flag
  193. assign ena_odd_out3 = mailbox_regs_flat[159:144]; // Next 16 bits for ena_odd_out3
  194. three_signal #(
  195. .FAST_PWM_WIDTH(FAST_PWM_WIDTH),
  196. .PULSE_COUNTER_WIDTH(PULSE_COUNTER_WIDTH),
  197. .SLOW_PWM_WIDTH(SLOW_PWM_WIDTH)
  198. ) three_signal_I(
  199. .nrst(~rst),
  200. .clk(clk_48m), // TODO: fix later we have CDC problem here
  201. .period1(period1),
  202. .delay1(delay1),
  203. .period2(period2),
  204. .delay2(delay2),
  205. .period3(period3),
  206. .duty3(duty3),
  207. .delay3(delay3),
  208. .npuls3(npuls3),
  209. .odd_train_flag(odd_train_flag),
  210. .ena_odd_out3(ena_odd_out3),
  211. .Out1(out1),
  212. .Out2(out2),
  213. .Out3(out3)
  214. );
  215. reg [31:0] pcount; //dummy variable + usage to make sure button will not be optimized out
  216. button b1(
  217. .clk(clk_24m),
  218. .nrst(~rst),
  219. .butt(btn_1),
  220. .press_count(pcount)
  221. );
  222. // TODO: dummy led onoff when value has been written
  223. always @(posedge clk_24m or posedge rst)
  224. if (rst) begin
  225. led[0] = 1'b0;
  226. led[1] = 1'b0;
  227. led[2] = 1'b0 & pcount[25];
  228. end else if (period1 == 1) begin
  229. led[0] = 1'b1;
  230. led[1] = 1'b0;
  231. led[2] = 1'b0;
  232. end else if (period1 == 2) begin
  233. led[0] = 1'b0;
  234. led[1] = 1'b1;
  235. led[2] = 1'b0;
  236. end else if (period1 == 4) begin
  237. led[0] = 1'b0;
  238. led[1] = 1'b0;
  239. led[2] = 1'b1;
  240. end else if (period1 != 0) begin
  241. led[0] = 1'b1;
  242. led[1] = 1'b1;
  243. led[2] = 1'b1;
  244. end else begin
  245. led[0] = 1'b0;
  246. led[1] = 1'b0;
  247. led[2] = 1'b0;
  248. end
  249. always @(posedge clk_24m or posedge rst)
  250. if (rst) begin
  251. led[3] = 1'b0;
  252. end else if (btn_2) begin
  253. led[3] = 1'b1;
  254. end else begin
  255. led[3] = 1'b0;
  256. end
  257. //// TODO: dummy driving from delay1
  258. always @(posedge clk_24m or posedge rst)
  259. if (rst) begin
  260. led[4] = 1'b0;
  261. end else if (delay1 != 0) begin
  262. led[4] = 1'b1;
  263. end else begin
  264. led[4] = 1'b0;
  265. end
  266. //always @(posedge clk_48m or posedge rst)
  267. // if (rst) begin
  268. // led[3] = 1'b0;
  269. // end else if (delay2 != 0) begin
  270. // led[3] = 1'b1;
  271. // end else begin
  272. // led[3] = 1'b0;
  273. // end
  274. // Warm Boot
  275. // ---------
  276. // Bus interface
  277. always @(posedge clk_24m or posedge rst)
  278. if (rst) begin
  279. boot_now <= 1'b0;
  280. boot_sel <= 2'b00;
  281. end else if (wb_cyc[0] & wb_we & (wb_addr[2:0] == 3'b000)) begin
  282. boot_now <= wb_wdata[2];
  283. boot_sel <= wb_wdata[1:0];
  284. end
  285. assign wb_rdata[0] = 0;
  286. assign wb_ack[0] = wb_cyc[0];
  287. // Helper
  288. dfu_helper #(
  289. .TIMER_WIDTH(24),
  290. .BTN_MODE(3),
  291. .DFU_MODE(0)
  292. ) dfu_helper_I (
  293. .boot_now(boot_now),
  294. .boot_sel(boot_sel),
  295. .btn_pad(btn_n),
  296. .btn_val(),
  297. .rst_req(),
  298. .clk(clk_24m),
  299. .rst(rst)
  300. );
  301. // Clock / Reset
  302. // -------------
  303. `ifdef SIM
  304. reg clk_48m_s = 1'b0;
  305. reg clk_24m_s = 1'b0;
  306. reg rst_s = 1'b1;
  307. always #5 clk_48m_s <= !clk_48m_s;
  308. always #20 clk_24m_s <= !clk_24m_s;
  309. initial begin
  310. #200 rst_s = 0;
  311. end
  312. assign clk_48m = clk_48m_s;
  313. assign clk_24m = clk_24m_s;
  314. assign rst = rst_s;
  315. `else
  316. sysmgr sys_mgr_I (
  317. .clk_in(clk_in),
  318. .rst_in(1'b0),
  319. .clk_48m(clk_48m),
  320. .clk_24m(clk_24m),
  321. .rst_out(rst)
  322. );
  323. `endif
  324. endmodule // top