top.v 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * top.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2019 Sylvain Munaut <tnt@246tNt.com>
  7. * All rights reserved.
  8. *
  9. * BSD 3-clause, see LICENSE.bsd
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * * Neither the name of the <organization> nor the
  19. * names of its contributors may be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  26. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. `default_nettype none
  34. //`define STREAM
  35. `define PATTERN
  36. //`define VIDEO
  37. module top (
  38. // RGB panel PMOD
  39. `ifdef BOARD_ICEBREAKER_SINGLE
  40. output wire hub75_addr_inc,
  41. output wire hub75_addr_rst,
  42. output wire [2:0] hub75_data,
  43. output wire hub75_clk,
  44. output wire hub75_le,
  45. output wire hub75_blank,
  46. `elsif BOARD_ICEBREAKER_SINGLE2X
  47. output wire [1:0] hub75_addr_inc,
  48. output wire [1:0] hub75_addr_rst,
  49. output wire [5:0] hub75_data,
  50. output wire [1:0] hub75_clk,
  51. output wire [1:0] hub75_le,
  52. output wire [1:0] hub75_blank,
  53. `else
  54. output wire [4:0] hub75_addr,
  55. output wire [5:0] hub75_data,
  56. output wire hub75_clk,
  57. output wire hub75_le,
  58. output wire hub75_blank,
  59. `endif
  60. // SPI Flash interface
  61. `ifdef VIDEO
  62. output wire flash_mosi,
  63. input wire flash_miso,
  64. output wire flash_cs_n,
  65. output wire flash_clk,
  66. `endif
  67. // SPI Slave interface
  68. `ifdef STREAM
  69. input wire slave_mosi,
  70. output wire slave_miso,
  71. input wire slave_cs_n,
  72. input wire slave_clk,
  73. `endif
  74. // PMOD2 buttons
  75. input wire [2:0] pmod_btn,
  76. // Clock
  77. input wire clk_12m
  78. );
  79. // Params
  80. `ifdef BOARD_ICEBREAKER_SINGLE2X
  81. localparam integer N_BANKS = 4;
  82. `else
  83. localparam integer N_BANKS = 2;
  84. `endif
  85. localparam integer N_ROWS = 32;
  86. localparam integer N_COLS = 64;
  87. localparam integer N_CHANS = 3;
  88. localparam integer N_PLANES = 10;
  89. localparam integer BITDEPTH = 16;
  90. localparam integer LOG_N_BANKS = $clog2(N_BANKS);
  91. localparam integer LOG_N_ROWS = $clog2(N_ROWS);
  92. localparam integer LOG_N_COLS = $clog2(N_COLS);
  93. // Signals
  94. // -------
  95. // Clock / Reset logic
  96. `ifdef NO_PLL
  97. reg [7:0] rst_cnt = 8'h00;
  98. wire rst_i;
  99. `endif
  100. wire clk;
  101. wire clk_2x;
  102. wire rst;
  103. // Frame buffer write port
  104. wire [LOG_N_BANKS-1:0] fbw_bank_addr;
  105. wire [LOG_N_ROWS-1:0] fbw_row_addr;
  106. wire fbw_row_store;
  107. wire fbw_row_rdy;
  108. wire fbw_row_swap;
  109. wire [BITDEPTH-1:0] fbw_data;
  110. wire [LOG_N_COLS-1:0] fbw_col_addr;
  111. wire fbw_wren;
  112. wire frame_swap;
  113. wire frame_rdy;
  114. // Control
  115. reg ctrl_run;
  116. // Hub75 driver
  117. // ------------
  118. hub75_top #(
  119. .N_BANKS(N_BANKS),
  120. .N_ROWS(N_ROWS),
  121. .N_COLS(N_COLS),
  122. .N_CHANS(N_CHANS),
  123. .N_PLANES(N_PLANES),
  124. .BITDEPTH(BITDEPTH),
  125. .PANEL_INIT("FM6126"),
  126. `ifdef BOARD_ICEBREAKER_SINGLE
  127. .PHY_DDR(2), // DDR data with early edge
  128. .PHY_AIR(3), // Enabled and invert INC
  129. .SCAN_MODE("LINEAR")
  130. `elsif BOARD_ICEBREAKER_SINGLE2X
  131. .PHY_N(2),
  132. .PHY_DDR(2), // DDR data with early edge
  133. .PHY_AIR(3), // Enabled and invert INC
  134. .SCAN_MODE("LINEAR")
  135. `else
  136. .SCAN_MODE("ZIGZAG")
  137. `endif
  138. ) hub75_I (
  139. `ifdef BOARD_ICEBREAKER_SINGLE
  140. .hub75_addr_inc(hub75_addr_inc),
  141. .hub75_addr_rst(hub75_addr_rst),
  142. `elsif BOARD_ICEBREAKER_SINGLE2X
  143. .hub75_addr_inc(hub75_addr_inc),
  144. .hub75_addr_rst(hub75_addr_rst),
  145. `else
  146. .hub75_addr(hub75_addr),
  147. `endif
  148. .hub75_data(hub75_data),
  149. .hub75_clk(hub75_clk),
  150. .hub75_le(hub75_le),
  151. .hub75_blank(hub75_blank),
  152. .fbw_bank_addr(fbw_bank_addr),
  153. .fbw_row_addr(fbw_row_addr),
  154. .fbw_row_store(fbw_row_store),
  155. .fbw_row_rdy(fbw_row_rdy),
  156. .fbw_row_swap(fbw_row_swap),
  157. .fbw_data(fbw_data),
  158. .fbw_col_addr(fbw_col_addr),
  159. .fbw_wren(fbw_wren),
  160. .frame_swap(frame_swap),
  161. .frame_rdy(frame_rdy),
  162. .ctrl_run(ctrl_run),
  163. .cfg_pre_latch_len(8'h80),
  164. .cfg_latch_len(8'h80),
  165. .cfg_post_latch_len(8'h80),
  166. .cfg_bcm_bit_len(8'h06),
  167. .clk(clk),
  168. .clk_2x(clk_2x),
  169. .rst(rst)
  170. );
  171. // Only start the scan when we have our first frame
  172. always @(posedge clk or posedge rst)
  173. if (rst)
  174. ctrl_run <= 1'b0;
  175. else
  176. ctrl_run <= ctrl_run | frame_swap;
  177. // Host Streaming
  178. // --------------
  179. `ifdef STREAM
  180. vstream #(
  181. .N_ROWS(N_BANKS * N_ROWS),
  182. .N_COLS(N_COLS),
  183. .BITDEPTH(BITDEPTH)
  184. ) stream_I (
  185. .spi_mosi(slave_mosi),
  186. .spi_miso(slave_miso),
  187. .spi_cs_n(slave_cs_n),
  188. .spi_clk(slave_clk),
  189. .fbw_row_addr({fbw_bank_addr, fbw_row_addr}),
  190. .fbw_row_store(fbw_row_store),
  191. .fbw_row_rdy(fbw_row_rdy),
  192. .fbw_row_swap(fbw_row_swap),
  193. .fbw_data(fbw_data),
  194. .fbw_col_addr(fbw_col_addr),
  195. .fbw_wren(fbw_wren),
  196. .frame_swap(frame_swap),
  197. .frame_rdy(frame_rdy),
  198. .clk(clk),
  199. .rst(rst)
  200. );
  201. `endif
  202. // Pattern generator
  203. // -----------------
  204. `ifdef PATTERN
  205. pgen #(
  206. .N_ROWS(N_BANKS * N_ROWS),
  207. .N_COLS(N_COLS),
  208. .BITDEPTH(BITDEPTH)
  209. ) pgen_I (
  210. .fbw_row_addr({fbw_bank_addr, fbw_row_addr}),
  211. .fbw_row_store(fbw_row_store),
  212. .fbw_row_rdy(fbw_row_rdy),
  213. .fbw_row_swap(fbw_row_swap),
  214. .fbw_data(fbw_data),
  215. .fbw_col_addr(fbw_col_addr),
  216. .fbw_wren(fbw_wren),
  217. .frame_swap(frame_swap),
  218. .frame_rdy(frame_rdy),
  219. .clk(clk),
  220. .rst(rst)
  221. );
  222. `endif
  223. // Video generator (from SPI flash)
  224. // ---------------
  225. `ifdef VIDEO
  226. // Signals
  227. // SPI reader interface
  228. wire [23:0] sr_addr;
  229. wire [15:0] sr_len;
  230. wire sr_go;
  231. wire sr_rdy;
  232. wire [7:0] sr_data;
  233. wire sr_valid;
  234. // UI
  235. wire btn_up;
  236. wire btn_mode;
  237. wire btn_down;
  238. // Main video generator / controller
  239. vgen #(
  240. .ADDR_BASE(24'h040000),
  241. .N_FRAMES(30),
  242. .N_ROWS(N_BANKS * N_ROWS),
  243. .N_COLS(N_COLS),
  244. .BITDEPTH(BITDEPTH)
  245. ) vgen_I (
  246. .sr_addr(sr_addr),
  247. .sr_len(sr_len),
  248. .sr_go(sr_go),
  249. .sr_rdy(sr_rdy),
  250. .sr_data(sr_data),
  251. .sr_valid(sr_valid),
  252. .fbw_row_addr({fbw_bank_addr, fbw_row_addr}),
  253. .fbw_row_store(fbw_row_store),
  254. .fbw_row_rdy(fbw_row_rdy),
  255. .fbw_row_swap(fbw_row_swap),
  256. .fbw_data(fbw_data),
  257. .fbw_col_addr(fbw_col_addr),
  258. .fbw_wren(fbw_wren),
  259. .frame_swap(frame_swap),
  260. .frame_rdy(frame_rdy),
  261. .ui_up(btn_up),
  262. .ui_mode(btn_mode),
  263. .ui_down(btn_down),
  264. .clk(clk),
  265. .rst(rst)
  266. );
  267. // SPI reader to fetch frames from flash
  268. spi_flash_reader spi_reader_I (
  269. .spi_mosi(flash_mosi),
  270. .spi_miso(flash_miso),
  271. .spi_cs_n(flash_cs_n),
  272. .spi_clk(flash_clk),
  273. .addr(sr_addr),
  274. .len(sr_len),
  275. .go(sr_go),
  276. .rdy(sr_rdy),
  277. .data(sr_data),
  278. .valid(sr_valid),
  279. .clk(clk),
  280. .rst(rst)
  281. );
  282. // UI
  283. glitch_filter #( .L(8) ) gf_down_I (
  284. .in(pmod_btn[0]),
  285. .rise(btn_down),
  286. .clk(clk),
  287. .rst(rst)
  288. );
  289. glitch_filter #( .L(8) ) gf_mode_I (
  290. .in(pmod_btn[1]),
  291. .rise(btn_mode),
  292. .clk(clk),
  293. .rst(rst)
  294. );
  295. glitch_filter #( .L(8) ) gf_up_I (
  296. .in(pmod_btn[2]),
  297. .rise(btn_up),
  298. .clk(clk),
  299. .rst(rst)
  300. );
  301. `endif
  302. // Clock / Reset
  303. // -------------
  304. `ifdef NO_PLL
  305. always @(posedge clk)
  306. if (~rst_cnt[7])
  307. rst_cnt <= rst_cnt + 1;
  308. wire rst_i = ~rst_cnt[7];
  309. SB_GB clk_gbuf_I (
  310. .USER_SIGNAL_TO_GLOBAL_BUFFER(clk_12m),
  311. .GLOBAL_BUFFER_OUTPUT(clk)
  312. );
  313. SB_GB rst_gbuf_I (
  314. .USER_SIGNAL_TO_GLOBAL_BUFFER(rst_i),
  315. .GLOBAL_BUFFER_OUTPUT(rst)
  316. );
  317. `else
  318. sysmgr sys_mgr_I (
  319. .clk_in(clk_12m),
  320. .rst_in(1'b0),
  321. .clk_out(clk),
  322. .clk_2x_out(clk_2x),
  323. .rst_out(rst)
  324. );
  325. `endif
  326. endmodule // top