top.v 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. reg 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. .write_flash(write_flash),
  201. .registers_flat(mailbox_regs_flat)
  202. );
  203. // 3 Signal
  204. // --------
  205. assign period1 = mailbox_regs_flat[15:0]; // First 16 bits for period1
  206. assign delay1 = mailbox_regs_flat[31:16]; // Next 16 bits for delay1
  207. assign period2 = mailbox_regs_flat[47:32]; // Next 16 bits for period2
  208. assign delay2 = mailbox_regs_flat[63:48]; // Next 16 bits for delay2
  209. assign period3 = mailbox_regs_flat[79:64]; // Next 16 bits for period3
  210. assign duty3 = mailbox_regs_flat[95:80]; // Next 16 bits for duty3
  211. assign delay3 = mailbox_regs_flat[111:96]; // Next 16 bits for delay3
  212. assign npuls3 = mailbox_regs_flat[127:112]; // Next 16 bits for npuls3
  213. assign odd_train_flag = mailbox_regs_flat[143:128]; // Next 16 bits for odd_train_flag
  214. assign ena_odd_out3 = mailbox_regs_flat[159:144]; // Next 16 bits for ena_odd_out3
  215. //assign write_flash = mailbox_regs_flat[175:160]; // Next 16 bits for write_flash (triggered from keyboard)
  216. three_signal #(
  217. .FAST_PWM_WIDTH(FAST_PWM_WIDTH),
  218. .PULSE_COUNTER_WIDTH(PULSE_COUNTER_WIDTH),
  219. .SLOW_PWM_WIDTH(SLOW_PWM_WIDTH)
  220. ) three_signal_I(
  221. .nrst(~rst),
  222. .clk(clk_48m), // TODO: fix later we have CDC problem here
  223. .period1(period1),
  224. .delay1(delay1),
  225. .period2(period2),
  226. .delay2(delay2),
  227. .period3(period3),
  228. .duty3(duty3),
  229. .delay3(delay3),
  230. .npuls3(npuls3),
  231. .odd_train_flag(odd_train_flag),
  232. .ena_odd_out3(ena_odd_out3),
  233. .Out1(out1),
  234. .Out2(out2),
  235. .Out3(out3)
  236. );
  237. // WB Button Mailbox [5]
  238. // ----------
  239. mailbox_wb_rtl2sw #(
  240. .AW(4),
  241. .DW(WB_DW)
  242. ) mailbox_btns_I (
  243. .clk(clk_24m),
  244. .rst(rst),
  245. .wb_addr(wb_addr[3:0]),
  246. .wb_wdata(wb_wdata),
  247. .wb_rdata(wb_rdata[5]),
  248. .wb_we(wb_we),
  249. .wb_cyc(wb_cyc[5]),
  250. .wb_ack(wb_ack[5]),
  251. .registers_flat_in(mailbox_btns_flat)
  252. );
  253. // Buttons
  254. assign mailbox_btns_flat[15:0] = inc_f1;
  255. assign mailbox_btns_flat[31:16] = dec_f1;
  256. assign mailbox_btns_flat[47:32] = inc_d1;
  257. assign mailbox_btns_flat[63:48] = dec_d1;
  258. assign mailbox_btns_flat[79:64] = inc_d2;
  259. assign mailbox_btns_flat[95:80] = dec_d2;
  260. assign mailbox_btns_flat[111:96] = inc_ph2;
  261. assign mailbox_btns_flat[127:112] = dec_ph2;
  262. assign mailbox_btns_flat[143:128] = inc_f3;
  263. assign mailbox_btns_flat[159:144] = dec_f3;
  264. assign mailbox_btns_flat[175:160] = inc_d3;
  265. assign mailbox_btns_flat[191:176] = dec_d3;
  266. assign mailbox_btns_flat[207:192] = inc_ph3;
  267. assign mailbox_btns_flat[223:208] = dec_ph3;
  268. assign mailbox_btns_flat[239:224] = inc_n3;
  269. assign mailbox_btns_flat[255:240] = dec_n3;
  270. button b1(
  271. .clk(clk_24m),
  272. .nrst(~rst),
  273. .butt(btn_1),
  274. .press_count(inc_f1)
  275. );
  276. // TODO: dummy led onoff when value has been written
  277. always @(posedge clk_24m or posedge rst)
  278. if (rst) begin
  279. led[0] = 1'b0;
  280. led[1] = 1'b0;
  281. led[2] = 1'b0;
  282. end else if (period1 == 1) begin
  283. led[0] = 1'b1;
  284. led[1] = 1'b0;
  285. led[2] = 1'b0;
  286. end else if (period1 == 2) begin
  287. led[0] = 1'b0;
  288. led[1] = 1'b1;
  289. led[2] = 1'b0;
  290. end else if (period1 == 4) begin
  291. led[0] = 1'b0;
  292. led[1] = 1'b0;
  293. led[2] = 1'b1;
  294. end else if (period1 != 0) begin
  295. led[0] = 1'b1;
  296. led[1] = 1'b1;
  297. led[2] = 1'b1;
  298. end else begin
  299. led[0] = 1'b0;
  300. led[1] = 1'b0;
  301. led[2] = 1'b0;
  302. end
  303. //always @(posedge clk_24m or posedge rst)
  304. // if (rst) begin
  305. // led[3] = 1'b0;
  306. // end else if (btn_2) begin
  307. // led[3] = 1'b1;
  308. // end else begin
  309. // led[3] = 1'b0;
  310. // end
  311. //// TODO: dummy driving from delay1
  312. always @(posedge clk_24m or posedge rst)
  313. if (rst) begin
  314. led[4] = 1'b0;
  315. end else if (delay1 != 0) begin
  316. led[4] = 1'b1;
  317. end else begin
  318. led[4] = 1'b0;
  319. end
  320. //always @(posedge clk_48m or posedge rst)
  321. // if (rst) begin
  322. // led[3] = 1'b0;
  323. // end else if (delay2 != 0) begin
  324. // led[3] = 1'b1;
  325. // end else begin
  326. // led[3] = 1'b0;
  327. // end
  328. // Warm Boot
  329. // ---------
  330. // Bus interface
  331. always @(posedge clk_24m or posedge rst)
  332. if (rst) begin
  333. boot_now <= 1'b0;
  334. boot_sel <= 2'b00;
  335. end else if (wb_cyc[0] & wb_we & (wb_addr[2:0] == 3'b000)) begin
  336. boot_now <= wb_wdata[2];
  337. boot_sel <= wb_wdata[1:0];
  338. end
  339. assign wb_rdata[0] = 0;
  340. assign wb_ack[0] = wb_cyc[0];
  341. // Helper
  342. dfu_helper #(
  343. .TIMER_WIDTH(24),
  344. .BTN_MODE(3),
  345. .DFU_MODE(0)
  346. ) dfu_helper_I (
  347. .boot_now(boot_now),
  348. .boot_sel(boot_sel),
  349. .btn_pad(btn_n),
  350. .btn_val(),
  351. .rst_req(),
  352. .clk(clk_24m),
  353. .rst(rst)
  354. );
  355. // Clock / Reset
  356. // -------------
  357. `ifdef SIM
  358. reg clk_48m_s = 1'b0;
  359. reg clk_24m_s = 1'b0;
  360. reg rst_s = 1'b1;
  361. always #5 clk_48m_s <= !clk_48m_s;
  362. always #20 clk_24m_s <= !clk_24m_s;
  363. initial begin
  364. #200 rst_s = 0;
  365. end
  366. assign clk_48m = clk_48m_s;
  367. assign clk_24m = clk_24m_s;
  368. assign rst = rst_s;
  369. `else
  370. sysmgr sys_mgr_I (
  371. .clk_in(clk_in),
  372. .rst_in(1'b0),
  373. .clk_48m(clk_48m),
  374. .clk_24m(clk_24m),
  375. .rst_out(rst)
  376. );
  377. `endif
  378. endmodule // top