top.v 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 = 6;
  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. localparam integer BUTTON_COUNTER_WIDTH = 5;
  52. genvar i;
  53. // Signals
  54. // -------
  55. // Wishbone
  56. wire [WB_AW-1:0] wb_addr;
  57. wire [WB_DW-1:0] wb_rdata [0:WB_N-1];
  58. wire [WB_RW-1:0] wb_rdata_flat;
  59. wire [WB_DW-1:0] wb_wdata;
  60. wire [WB_MW-1:0] wb_wmsk;
  61. wire [WB_N -1:0] wb_cyc;
  62. wire wb_we;
  63. wire [WB_N -1:0] wb_ack;
  64. // WarmBoot
  65. reg boot_now;
  66. reg [1:0] boot_sel;
  67. // Clock / Reset logic
  68. wire clk_24m;
  69. wire clk_48m;
  70. wire rst;
  71. // 3 signal
  72. wire [SLOW_PWM_WIDTH-1:0] period1;
  73. wire [SLOW_PWM_WIDTH-1:0] delay1;
  74. wire [SLOW_PWM_WIDTH-1:0] period2;
  75. wire [SLOW_PWM_WIDTH-1:0] delay2;
  76. wire [FAST_PWM_WIDTH-1:0] period3;
  77. wire [FAST_PWM_WIDTH-1:0] duty3;
  78. wire [SLOW_PWM_WIDTH-1:0] delay3;
  79. wire [PULSE_COUNTER_WIDTH-1:0] npuls3;
  80. wire [1:0] odd_train_flag;
  81. wire ena_odd_out3;
  82. wire write_flash;
  83. // Buttons
  84. reg [BUTTON_COUNTER_WIDTH-1:0] inc_f1;
  85. reg [BUTTON_COUNTER_WIDTH-1:0] dec_f1;
  86. reg [BUTTON_COUNTER_WIDTH-1:0] inc_d1;
  87. reg [BUTTON_COUNTER_WIDTH-1:0] dec_d1;
  88. reg [BUTTON_COUNTER_WIDTH-1:0] inc_d2;
  89. reg [BUTTON_COUNTER_WIDTH-1:0] dec_d2;
  90. reg [BUTTON_COUNTER_WIDTH-1:0] inc_ph2;
  91. reg [BUTTON_COUNTER_WIDTH-1:0] dec_ph2;
  92. reg [BUTTON_COUNTER_WIDTH-1:0] inc_f3;
  93. reg [BUTTON_COUNTER_WIDTH-1:0] dec_f3;
  94. reg [BUTTON_COUNTER_WIDTH-1:0] inc_d3;
  95. reg [BUTTON_COUNTER_WIDTH-1:0] dec_d3;
  96. reg [BUTTON_COUNTER_WIDTH-1:0] inc_ph3;
  97. reg [BUTTON_COUNTER_WIDTH-1:0] dec_ph3;
  98. reg [BUTTON_COUNTER_WIDTH-1:0] inc_n3;
  99. reg [BUTTON_COUNTER_WIDTH-1:0] dec_n3;
  100. // Mailbox signal wires
  101. wire [16*16-1:0] mailbox_regs_flat; // Flattened register array (16 registers of 16 bits each)
  102. wire [16*16-1:0] mailbox_btns_flat;
  103. // SoC
  104. // ---
  105. soc_picorv32_base #(
  106. .WB_N (WB_N),
  107. .WB_DW (WB_DW),
  108. .WB_AW (WB_AW),
  109. .SPRAM_AW(SPRAM_AW)
  110. ) base_I (
  111. .wb_addr (wb_addr),
  112. .wb_rdata(wb_rdata_flat),
  113. .wb_wdata(wb_wdata),
  114. .wb_wmsk (wb_wmsk),
  115. .wb_we (wb_we),
  116. .wb_cyc (wb_cyc),
  117. .wb_ack (wb_ack),
  118. .clk (clk_24m),
  119. .rst (rst)
  120. );
  121. for (i=0; i<WB_N; i=i+1)
  122. assign wb_rdata_flat[i*WB_DW+:WB_DW] = wb_rdata[i];
  123. // UART [1]
  124. // ----
  125. uart_wb #(
  126. .DIV_WIDTH(12),
  127. .DW(WB_DW)
  128. ) uart_I (
  129. .uart_tx (uart_tx),
  130. .uart_rx (uart_rx),
  131. .wb_addr (wb_addr[1:0]),
  132. .wb_rdata (wb_rdata[1]),
  133. .wb_we (wb_we),
  134. .wb_wdata (wb_wdata),
  135. .wb_cyc (wb_cyc[1]),
  136. .wb_ack (wb_ack[1]),
  137. .clk (clk_24m),
  138. .rst (rst)
  139. );
  140. // SPI [2]
  141. // ---
  142. ice40_spi_wb #(
  143. `ifdef HAS_PSRAM
  144. .N_CS(2),
  145. `else
  146. .N_CS(1),
  147. `endif
  148. .WITH_IOB(1),
  149. .UNIT(0)
  150. ) spi_I (
  151. .pad_mosi (spi_mosi),
  152. .pad_miso (spi_miso),
  153. .pad_clk (spi_clk),
  154. `ifdef HAS_PSRAM
  155. .pad_csn ({spi_ram_cs_n, spi_flash_cs_n}),
  156. `else
  157. .pad_csn (spi_flash_cs_n),
  158. `endif
  159. .wb_addr (wb_addr[3:0]),
  160. .wb_rdata (wb_rdata[2]),
  161. .wb_wdata (wb_wdata),
  162. .wb_we (wb_we),
  163. .wb_cyc (wb_cyc[2]),
  164. .wb_ack (wb_ack[2]),
  165. .clk (clk_24m),
  166. .rst (rst)
  167. );
  168. // RGB LEDs [3]
  169. // --------
  170. ice40_rgb_wb #(
  171. .CURRENT_MODE("0b1"),
  172. .RGB0_CURRENT("0b000001"),
  173. .RGB1_CURRENT("0b000001"),
  174. .RGB2_CURRENT("0b000001")
  175. ) rgb_I (
  176. .pad_rgb (rgb),
  177. .wb_addr (wb_addr[4:0]),
  178. .wb_rdata (wb_rdata[3]),
  179. .wb_wdata (wb_wdata),
  180. .wb_we (wb_we),
  181. .wb_cyc (wb_cyc[3]),
  182. .wb_ack (wb_ack[3]),
  183. .clk (clk_24m),
  184. .rst (rst)
  185. );
  186. // WB Register Mailbox [4]
  187. // ----------
  188. mailbox_wb_sw2rtl #(
  189. .AW(4),
  190. .DW(WB_DW)
  191. ) mailbox_regs_I (
  192. .clk(clk_24m),
  193. .rst(rst),
  194. .wb_addr(wb_addr[3:0]),
  195. .wb_wdata(wb_wdata),
  196. .wb_rdata(wb_rdata[4]),
  197. .wb_we(wb_we),
  198. .wb_cyc(wb_cyc[4]),
  199. .wb_ack(wb_ack[4]),
  200. .registers_flat(mailbox_regs_flat)
  201. );
  202. // 3 Signal
  203. // --------
  204. assign period1 = mailbox_regs_flat[15:0]; // First 16 bits for period1
  205. assign delay1 = mailbox_regs_flat[31:16]; // Next 16 bits for delay1
  206. assign period2 = mailbox_regs_flat[47:32]; // Next 16 bits for period2
  207. assign delay2 = mailbox_regs_flat[63:48]; // Next 16 bits for delay2
  208. assign period3 = mailbox_regs_flat[79:64]; // Next 16 bits for period3
  209. assign duty3 = mailbox_regs_flat[95:80]; // Next 16 bits for duty3
  210. assign delay3 = mailbox_regs_flat[111:96]; // Next 16 bits for delay3
  211. assign npuls3 = mailbox_regs_flat[127:112]; // Next 16 bits for npuls3
  212. assign odd_train_flag = mailbox_regs_flat[143:128]; // Next 16 bits for odd_train_flag
  213. assign ena_odd_out3 = mailbox_regs_flat[159:144]; // Next 16 bits for ena_odd_out3
  214. assign write_flash = mailbox_regs_flat[175:160]; // Next 16 bits for write_flash (triggered from keyboard)
  215. three_signal #(
  216. .FAST_PWM_WIDTH(FAST_PWM_WIDTH),
  217. .PULSE_COUNTER_WIDTH(PULSE_COUNTER_WIDTH),
  218. .SLOW_PWM_WIDTH(SLOW_PWM_WIDTH)
  219. ) three_signal_I(
  220. .nrst(~rst),
  221. .clk(clk_48m), // TODO: fix later we have CDC problem here
  222. .period1(period1),
  223. .delay1(delay1),
  224. .period2(period2),
  225. .delay2(delay2),
  226. .period3(period3),
  227. .duty3(duty3),
  228. .delay3(delay3),
  229. .npuls3(npuls3),
  230. .odd_train_flag(odd_train_flag),
  231. .ena_odd_out3(ena_odd_out3),
  232. .Out1(out1),
  233. .Out2(out2),
  234. .Out3(out3)
  235. );
  236. // WB Button Mailbox [5]
  237. // ----------
  238. mailbox_wb_rtl2sw #(
  239. .AW(4),
  240. .DW(WB_DW)
  241. ) mailbox_btns_I (
  242. .clk(clk_24m),
  243. .rst(rst),
  244. .wb_addr(wb_addr[3:0]),
  245. .wb_rdata(wb_rdata[5]),
  246. .wb_cyc(wb_cyc[5]),
  247. .wb_ack(wb_ack[5]),
  248. .registers_flat_in(mailbox_btns_flat)
  249. );
  250. // Buttons
  251. assign mailbox_btns_flat[15:0] = inc_f1;
  252. assign mailbox_btns_flat[31:16] = dec_f1;
  253. assign mailbox_btns_flat[47:32] = inc_d1;
  254. assign mailbox_btns_flat[63:48] = dec_d1;
  255. assign mailbox_btns_flat[79:64] = inc_d2;
  256. assign mailbox_btns_flat[95:80] = dec_d2;
  257. assign mailbox_btns_flat[111:96] = inc_ph2;
  258. assign mailbox_btns_flat[127:112] = dec_ph2;
  259. assign mailbox_btns_flat[143:128] = inc_f3;
  260. assign mailbox_btns_flat[159:144] = dec_f3;
  261. assign mailbox_btns_flat[175:160] = inc_d3;
  262. assign mailbox_btns_flat[191:176] = dec_d3;
  263. assign mailbox_btns_flat[207:192] = inc_ph3;
  264. assign mailbox_btns_flat[223:208] = dec_ph3;
  265. assign mailbox_btns_flat[239:224] = inc_n3;
  266. assign mailbox_btns_flat[255:240] = dec_n3;
  267. button b1(
  268. .clk(clk_24m),
  269. .nrst(~rst),
  270. .butt(btn_1),
  271. .press_count(inc_f1)
  272. );
  273. // TODO: dummy led onoff when value has been written
  274. always @(posedge clk_24m or posedge rst)
  275. if (rst) begin
  276. led[0] = 1'b0;
  277. led[1] = 1'b0;
  278. led[2] = 1'b0;
  279. end else if (period1 == 1) begin
  280. led[0] = 1'b1;
  281. led[1] = 1'b0;
  282. led[2] = 1'b0;
  283. end else if (period1 == 2) begin
  284. led[0] = 1'b0;
  285. led[1] = 1'b1;
  286. led[2] = 1'b0;
  287. end else if (period1 == 4) begin
  288. led[0] = 1'b0;
  289. led[1] = 1'b0;
  290. led[2] = 1'b1;
  291. end else if (period1 != 0) begin
  292. led[0] = 1'b1;
  293. led[1] = 1'b1;
  294. led[2] = 1'b1;
  295. end else begin
  296. led[0] = 1'b0;
  297. led[1] = 1'b0;
  298. led[2] = 1'b0;
  299. end
  300. //always @(posedge clk_24m or posedge rst)
  301. // if (rst) begin
  302. // led[3] = 1'b0;
  303. // end else if (btn_2) begin
  304. // led[3] = 1'b1;
  305. // end else begin
  306. // led[3] = 1'b0;
  307. // end
  308. //// TODO: dummy driving from delay1
  309. always @(posedge clk_24m or posedge rst)
  310. if (rst) begin
  311. led[4] = 1'b0;
  312. end else if (delay1 != 0) begin
  313. led[4] = 1'b1;
  314. end else begin
  315. led[4] = 1'b0;
  316. end
  317. //always @(posedge clk_48m or posedge rst)
  318. // if (rst) begin
  319. // led[3] = 1'b0;
  320. // end else if (delay2 != 0) begin
  321. // led[3] = 1'b1;
  322. // end else begin
  323. // led[3] = 1'b0;
  324. // end
  325. // Warm Boot
  326. // ---------
  327. // Bus interface
  328. always @(posedge clk_24m or posedge rst)
  329. if (rst) begin
  330. boot_now <= 1'b0;
  331. boot_sel <= 2'b00;
  332. end else if (wb_cyc[0] & wb_we & (wb_addr[2:0] == 3'b000)) begin
  333. boot_now <= wb_wdata[2];
  334. boot_sel <= wb_wdata[1:0];
  335. end
  336. assign wb_rdata[0] = 0;
  337. assign wb_ack[0] = wb_cyc[0];
  338. // Helper
  339. dfu_helper #(
  340. .TIMER_WIDTH(24),
  341. .BTN_MODE(3),
  342. .DFU_MODE(0)
  343. ) dfu_helper_I (
  344. .boot_now(boot_now),
  345. .boot_sel(boot_sel),
  346. .btn_pad(btn_n),
  347. .btn_val(),
  348. .rst_req(),
  349. .clk(clk_24m),
  350. .rst(rst)
  351. );
  352. // Clock / Reset
  353. // -------------
  354. `ifdef SIM
  355. reg clk_48m_s = 1'b0;
  356. reg clk_24m_s = 1'b0;
  357. reg rst_s = 1'b1;
  358. always #5 clk_48m_s <= !clk_48m_s;
  359. always #20 clk_24m_s <= !clk_24m_s;
  360. initial begin
  361. #200 rst_s = 0;
  362. end
  363. assign clk_48m = clk_48m_s;
  364. assign clk_24m = clk_24m_s;
  365. assign rst = rst_s;
  366. `else
  367. sysmgr sys_mgr_I (
  368. .clk_in(clk_in),
  369. .rst_in(1'b0),
  370. .clk_48m(clk_48m),
  371. .clk_24m(clk_24m),
  372. .rst_out(rst)
  373. );
  374. `endif
  375. endmodule // top