2
0

max80.sv 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. //
  2. // Top level module for the FPGA on the MAX80 board by
  3. // Per Mårtensson and H. Peter Anvin
  4. //
  5. // This is for MAX80 as slave.
  6. //
  7. // Sharing JTAG pins (via JTAGEN)
  8. `undef SHARED_JTAG
  9. module max80 (
  10. // Clock oscillator
  11. input clock_48, // 48 MHz
  12. // ABC-bus
  13. input abc_clk, // ABC-bus 3 MHz clock
  14. input [15:0] abc_a, // ABC address bus
  15. inout [7:0] abc_d, // ABC data bus
  16. output abc_d_oe, // Data bus output enable
  17. input abc_rst_n, // ABC bus reset strobe
  18. input abc_cs_n, // ABC card select strobe
  19. input [4:0] abc_out_n, // OUT, C1-C4 strobe
  20. input [1:0] abc_inp_n, // INP, STATUS strobe
  21. input abc_xmemfl_n, // Memory read strobe
  22. input abc_xmemw800_n, // Memory write strobe (ABC800)
  23. input abc_xmemw80_n, // Memory write strobe (ABC80)
  24. input abc_xinpstb_n, // I/O read strobe (ABC800)
  25. input abc_xoutpstb_n, // I/O write strobe (ABC80)
  26. // The following are inverted versus the bus IF
  27. // the corresponding MOSFETs are installed
  28. output abc_rdy_x, // RDY = WAIT#
  29. output abc_resin_x, // System reset request
  30. output abc_int80_x, // System INT request (ABC80)
  31. output abc_int800_x, // System INT request (ABC800)
  32. output abc_nmi_x, // System NMI request (ABC800)
  33. output abc_xm_x, // System memory override (ABC800)
  34. // Master/slave control
  35. output abc_master, // 1 = master, 0 = slave
  36. output abc_a_oe,
  37. // Bus isolation
  38. output abc_d_ce_n,
  39. // ABC-bus extension header
  40. // (Note: cannot use an array here because HC and HH are
  41. // input only.)
  42. inout exth_ha,
  43. inout exth_hb,
  44. input exth_hc,
  45. inout exth_hd,
  46. inout exth_he,
  47. inout exth_hf,
  48. inout exth_hg,
  49. input exth_hh,
  50. // SDRAM bus
  51. output sr_clk,
  52. output sr_cke,
  53. output [1:0] sr_ba, // Bank address
  54. output [12:0] sr_a, // Address within bank
  55. inout [15:0] sr_dq, // Also known as D or IO
  56. output [1:0] sr_dqm, // DQML and DQMH
  57. output sr_cs_n,
  58. output sr_we_n,
  59. output sr_cas_n,
  60. output sr_ras_n,
  61. // SD card
  62. output sd_clk,
  63. output sd_cmd,
  64. inout [3:0] sd_dat,
  65. // USB serial (naming is FPGA as DCE)
  66. input tty_txd,
  67. output tty_rxd,
  68. input tty_rts,
  69. output tty_cts,
  70. input tty_dtr,
  71. // SPI flash memory (also configuration)
  72. output flash_cs_n,
  73. output flash_clk,
  74. output flash_mosi,
  75. input flash_miso,
  76. // SPI bus (connected to ESP32 so can be bidirectional)
  77. inout spi_clk,
  78. inout spi_miso,
  79. inout spi_mosi,
  80. inout spi_cs_esp_n, // ESP32 IO10
  81. inout spi_cs_flash_n, // ESP32 IO01
  82. // Other ESP32 connections
  83. inout esp_io0, // ESP32 IO00
  84. inout esp_int, // ESP32 IO09
  85. // I2C bus (RTC and external)
  86. inout i2c_scl,
  87. inout i2c_sda,
  88. input rtc_32khz,
  89. input rtc_int_n,
  90. // LED
  91. output [3:1] led,
  92. // GPIO pins
  93. inout [5:0] gpio,
  94. // HDMI
  95. output [2:0] hdmi_d,
  96. output hdmi_clk,
  97. inout hdmi_scl,
  98. inout hdmi_sda,
  99. inout hdmi_hpd
  100. );
  101. // Set if MOSFETs Q1-Q6 are installed rather than the corresponding
  102. // resistors.
  103. parameter [6:1] mosfet_installed = 6'b000_000;
  104. // PLL and reset
  105. parameter reset_pow2 = 12; // Assert internal reset for 4096 cycles after PLL lock
  106. reg [reset_pow2-1:0] rst_ctr = 1'b0;
  107. reg rst_n = 1'b0; // Internal reset
  108. wire [1:0] pll_locked;
  109. wire sdram_clk;
  110. wire clk; // System clock
  111. wire vid_clk; // Video pixel clock
  112. wire vid_hdmiclk; // D:o in the HDMI clock domain
  113. pll pll (
  114. .areset ( 1'b0 ),
  115. .inclk0 ( clock_48 ),
  116. .c0 ( sdram_clk ), // SDRAM clock (168 MHz)
  117. .c1 ( clk ), // System clock (168 MHz)
  118. .c2 ( vid_clk ), // Video pixel clock (48 MHz)
  119. .locked ( pll_locked[0] ),
  120. .phasestep ( 1'b0 ),
  121. .phasecounterselect ( 3'b0 ),
  122. .phaseupdown ( 1'b1 ),
  123. .scanclk ( 1'b0 ),
  124. .phasedone ( )
  125. );
  126. wire all_plls_locked = &pll_locked;
  127. always @(negedge all_plls_locked or posedge clk)
  128. if (~&all_plls_locked)
  129. begin
  130. rst_ctr <= 1'b0;
  131. rst_n <= 1'b0;
  132. end
  133. else if (~rst_n)
  134. begin
  135. { rst_n, rst_ctr } <= rst_ctr + 1'b1;
  136. end
  137. // Unused device stubs - remove when used
  138. // Reset in the video clock domain
  139. reg vid_rst_n;
  140. always @(negedge all_plls_locked or posedge vid_clk)
  141. if (~all_plls_locked)
  142. vid_rst_n <= 1'b0;
  143. else
  144. vid_rst_n <= rst_n;
  145. // HDMI - generate random data to give Quartus something to do
  146. reg [23:0] dummydata = 30'hc8_fb87;
  147. always @(posedge vid_clk)
  148. dummydata <= { dummydata[22:0], dummydata[23] };
  149. wire [7:0] hdmi_data[3];
  150. wire [9:0] hdmi_tmds[3];
  151. wire [29:0] hdmi_to_tx;
  152. assign hdmi_data[0] = dummydata[7:0];
  153. assign hdmi_data[1] = dummydata[15:8];
  154. assign hdmi_data[2] = dummydata[23:16];
  155. generate
  156. genvar i;
  157. for (i = 0; i < 3; i = i + 1)
  158. begin : hdmitmds
  159. tmdsenc enc (
  160. .rst_n ( vid_rst_n ),
  161. .clk ( vid_clk ),
  162. .den ( 1'b1 ),
  163. .d ( hdmi_data[i] ),
  164. .c ( 2'b00 ),
  165. .q ( hdmi_tmds[i] )
  166. );
  167. end
  168. endgenerate
  169. assign hdmi_scl = 1'bz;
  170. assign hdmi_sck = 1'bz;
  171. assign hdmi_hpd = 1'bz;
  172. //
  173. // The ALTLVDS_TX megafunctions is MSB-first and in time-major order.
  174. // However, TMDS is LSB-first, and we have three TMDS words that
  175. // concatenate in word(channel)-major order.
  176. //
  177. transpose #(.words(3), .bits(10), .reverse_b(1),
  178. .reg_d(0), .reg_q(0)) hdmitranspose
  179. (
  180. .clk ( vid_clk ),
  181. .d ( { hdmi_tmds[2], hdmi_tmds[1], hdmi_tmds[0] } ),
  182. .q ( hdmi_to_tx )
  183. );
  184. hdmitx hdmitx (
  185. .pll_areset ( ~pll_locked[0] ),
  186. .tx_in ( hdmi_to_tx ),
  187. .tx_inclock ( vid_clk ),
  188. .tx_coreclock ( vid_hdmiclk ), // Pixel clock in HDMI domain
  189. .tx_locked ( pll_locked[1] ),
  190. .tx_out ( hdmi_d ),
  191. .tx_outclock ( hdmi_clk )
  192. );
  193. // ABC bus
  194. // On ABC800, only one of XINPSTB# or XOUTPSTB# will be active;
  195. // on ABC80 they will either be 00 or ZZ; in the latter case pulled
  196. // low by external resistors.
  197. wire abc800 = abc_xinpstb_n | abc_xoutpstb_n;
  198. wire abc80 = ~abc800;
  199. // Memory read/write strobes
  200. wire abc_xmemrd = ~abc_xmemfl_n; // For consistency
  201. wire abc_xmemwr = abc800 ? ~abc_xmemw800_n : ~abc_xmemw80_n;
  202. // I/O read/write strobes
  203. wire abc_iord = (abc800 & ~abc_xinpstb_n) | ~(|abc_inp_n);
  204. wire abc_iowr = (abc800 & ~abc_xoutpstb_n) | ~(|abc_out_n);
  205. reg [7:0] abc_do;
  206. reg [7:0] abc_di;
  207. assign abc_d_oe = abc_xmemrd;
  208. assign abc_d = abc_d_oe ? abc_do : 8'hzz;
  209. // Open drain signals with optional MOSFETs
  210. wire abc_wait;
  211. wire abc_resin;
  212. wire abc_int;
  213. wire abc_nmi;
  214. wire abc_xm;
  215. function reg opt_mosfet(input signal, input mosfet);
  216. if (mosfet)
  217. opt_mosfet = signal;
  218. else
  219. opt_mosfet = signal ? 1'b0 : 1'bz;
  220. endfunction // opt_mosfet
  221. assign abc_int80_x = opt_mosfet(abc_int & abc80, mosfet_installed[1]);
  222. assign abc_rdy_x = opt_mosfet(abc_wait, mosfet_installed[2]);
  223. assign abc_nmi_x = opt_mosfet(abc_nmi, mosfet_installed[3]);
  224. assign abc_resin_x = opt_mosfet(abc_resin, mosfet_installed[4]);
  225. assign abc_int800_x = opt_mosfet(abc_int & abc800, mosfet_installed[5]);
  226. assign abc_xm_x = opt_mosfet(abc_xm, mosfet_installed[6]);
  227. // ABC-bus extension header (exth_c and exth_h are input only)
  228. // The naming of pins is kind of nonsensical:
  229. //
  230. // +3V3 - 1 2 - +3V3
  231. // HA - 3 4 - HE
  232. // HB - 5 6 - HG
  233. // HC - 7 8 - HH
  234. // HD - 9 10 - HF
  235. // GND - 11 12 - GND
  236. //
  237. // This layout allows the header to be connected on either side
  238. // of the board. This logic assigns the following names to the pins;
  239. // if the ext_reversed is set to 1 then the left and right sides
  240. // are flipped.
  241. //
  242. // +3V3 - 1 2 - +3V3
  243. // exth[0] - 3 4 - exth[1]
  244. // exth[2] - 5 6 - exth[3]
  245. // exth[6] - 7 8 - exth[7]
  246. // exth[4] - 9 10 - exth[5]
  247. // GND - 11 12 - GND
  248. wire exth_reversed = 1'b0;
  249. wire [7:0] exth_d; // Input data
  250. wire [5:0] exth_q; // Output data
  251. wire [5:0] exth_oe; // Output enable
  252. assign exth_d[0] = exth_reversed ? exth_he : exth_ha;
  253. assign exth_d[1] = exth_reversed ? exth_ha : exth_he;
  254. assign exth_d[2] = exth_reversed ? exth_hg : exth_hb;
  255. assign exth_d[3] = exth_reversed ? exth_hb : exth_hg;
  256. assign exth_d[4] = exth_reversed ? exth_hf : exth_hd;
  257. assign exth_d[5] = exth_reversed ? exth_hd : exth_hf;
  258. assign exth_d[6] = exth_reversed ? exth_hh : exth_hc;
  259. assign exth_d[7] = exth_reversed ? exth_hc : exth_hh;
  260. wire [2:0] erx = { 2'b00, exth_reversed };
  261. assign exth_ha = exth_oe[3'd0 ^ erx] ? exth_q[3'd0 ^ erx] : 1'bz;
  262. assign exth_he = exth_oe[3'd1 ^ erx] ? exth_q[3'd1 ^ erx] : 1'bz;
  263. assign exth_hb = exth_oe[3'd2 ^ erx] ? exth_q[3'd2 ^ erx] : 1'bz;
  264. assign exth_hg = exth_oe[3'd3 ^ erx] ? exth_q[3'd3 ^ erx] : 1'bz;
  265. assign exth_hd = exth_oe[3'd4 ^ erx] ? exth_q[3'd4 ^ erx] : 1'bz;
  266. assign exth_hf = exth_oe[3'd5 ^ erx] ? exth_q[3'd5 ^ erx] : 1'bz;
  267. assign exth_q = 6'b0;
  268. assign exth_oe = 6'b0;
  269. // LED blink counter
  270. reg [28:0] led_ctr;
  271. always @(posedge clk or negedge rst_n)
  272. if (~rst_n)
  273. led_ctr <= 29'b0;
  274. else
  275. led_ctr <= led_ctr + 1'b1;
  276. assign led = led_ctr[28:26];
  277. // SDRAM controller
  278. reg abc_rrq;
  279. reg abc_wrq;
  280. reg abc_xmemrd_q;
  281. reg abc_xmemwr_q;
  282. reg abc_xmem_done;
  283. wire abc_rack;
  284. wire abc_wack;
  285. wire [7:0] abc_sr_rd;
  286. always @(posedge sdram_clk or negedge rst_n)
  287. if (~rst_n)
  288. begin
  289. abc_rrq <= 1'b0;
  290. abc_wrq <= 1'b0;
  291. abc_xmemrd_q <= 1'b0;
  292. abc_xmemwr_q <= 1'b0;
  293. abc_xmem_done <= 1'b0;
  294. end
  295. else
  296. begin
  297. abc_di <= abc_d;
  298. abc_xmemrd_q <= abc_xmemrd;
  299. abc_xmemwr_q <= abc_xmemwr;
  300. abc_xmem_done <= (abc_xmemrd_q & (abc_xmem_done | abc_rack))
  301. | (abc_xmemwr_q & (abc_xmem_done | abc_wack));
  302. abc_rrq <= abc_xmemrd_q & ~(abc_xmem_done | abc_rack);
  303. abc_wrq <= abc_xmemwr_q & ~(abc_xmem_done | abc_wack);
  304. if (abc_rack)
  305. abc_do <= abc_sr_rd;
  306. end // else: !if(~rst_n)
  307. sdram sdram (
  308. .rst_n ( rst_n ),
  309. .clk ( sdram_clk ), // Input clock
  310. .sr_clk ( sr_clk ), // Output clock buffer
  311. .sr_cke ( sr_cke ),
  312. .sr_cs_n ( sr_cs_n ),
  313. .sr_ras_n ( sr_ras_n ),
  314. .sr_cas_n ( sr_cas_n ),
  315. .sr_we_n ( sr_we_n ),
  316. .sr_dqm ( sr_dqm ),
  317. .sr_ba ( sr_ba ),
  318. .sr_a ( sr_a ),
  319. .sr_dq ( sr_dq ),
  320. .a0 ( { 9'b0, abc_a } ),
  321. .rd0 ( abc_sr_rd ),
  322. .rrq0 ( abc_rrq ),
  323. .rack0 ( abc_rack ),
  324. .wd0 ( abc_d ),
  325. .wrq0 ( abc_wrq ),
  326. .wack0 ( abc_wack ),
  327. .a1 ( 24'hxxxxxx ),
  328. .be1 ( 8'b0000_0000 ),
  329. .rd1 ( ),
  330. .rrq1 ( 1'b0 ),
  331. .rack1 ( ),
  332. .wd1 ( 32'hxxxx_xxxx ),
  333. .wrq1 ( 1'b0 ),
  334. .wack1 ( )
  335. );
  336. // SD card
  337. assign sd_clk = 1'b1;
  338. assign sd_cmd = 1'b1;
  339. assign sd_dat = 4'hz;
  340. // USB serial
  341. assign tty_rxd = 1'b1;
  342. assign tty_cts = 1'b1;
  343. // SPI bus (free for ESP32)
  344. assign spi_clk = 1'bz;
  345. assign spi_miso = 1'bz;
  346. assign spi_mosi = 1'bz;
  347. assign spi_cs_esp_n = 1'bz;
  348. assign spi_cs_flash_n = 1'bz;
  349. // ESP32
  350. assign esp_io0 = 1'bz;
  351. assign esp_int = 1'bz;
  352. // I2C
  353. assign i2c_scl = 1'bz;
  354. assign i2c_sda = 1'bz;
  355. // GPIO
  356. assign gpio = 6'bzzzzzz;
  357. endmodule