2
0

max80.sv 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 ),
  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. // SDRAM clock buffer - use a DDIO buffer for best performance
  138. // For EP4CE15 only could use a secondary PLL here, but it
  139. // isn't clear it buys us a whole lot.
  140. ddio_out sr_clk_out (
  141. .aclr ( 1'b0 ),
  142. .datain_l ( 1'b0 ),
  143. .datain_h ( 1'b1 ),
  144. .outclock ( sdram_clk ),
  145. .dataout ( sr_clk )
  146. );
  147. // Unused device stubs - remove when used
  148. // Reset in the video clock domain
  149. reg vid_rst_n;
  150. always @(negedge all_plls_locked or posedge vid_clk)
  151. if (~all_plls_locked)
  152. vid_rst_n <= 1'b0;
  153. else
  154. vid_rst_n <= rst_n;
  155. // HDMI - generate random data to give Quartus something to do
  156. reg [23:0] dummydata = 30'hc8_fb87;
  157. always @(posedge vid_clk)
  158. dummydata <= { dummydata[22:0], dummydata[23] };
  159. wire [7:0] hdmi_data[3];
  160. wire [9:0] hdmi_tmds[3];
  161. wire [29:0] hdmi_to_tx;
  162. assign hdmi_data[0] = dummydata[7:0];
  163. assign hdmi_data[1] = dummydata[15:8];
  164. assign hdmi_data[2] = dummydata[23:16];
  165. generate
  166. genvar i;
  167. for (i = 0; i < 3; i = i + 1)
  168. begin : hdmitmds
  169. tmdsenc enc (
  170. .rst_n ( vid_rst_n ),
  171. .clk ( vid_clk ),
  172. .den ( 1'b1 ),
  173. .d ( hdmi_data[i] ),
  174. .c ( 2'b00 ),
  175. .q ( hdmi_tmds[i] )
  176. );
  177. end
  178. endgenerate
  179. assign hdmi_scl = 1'bz;
  180. assign hdmi_sck = 1'bz;
  181. assign hdmi_hpd = 1'bz;
  182. //
  183. // The ALTLVDS_TX megafunctions is MSB-first and in time-major order.
  184. // However, TMDS is LSB-first, and we have three TMDS words that
  185. // concatenate in word(channel)-major order.
  186. //
  187. transpose #(.words(3), .bits(10), .reverse_b(1),
  188. .reg_d(0), .reg_q(0)) hdmitranspose
  189. (
  190. .clk ( vid_clk ),
  191. .d ( { hdmi_tmds[2], hdmi_tmds[1], hdmi_tmds[0] } ),
  192. .q ( hdmi_to_tx )
  193. );
  194. hdmitx hdmitx (
  195. .pll_areset ( ~pll_locked[0] ),
  196. .tx_in ( hdmi_to_tx ),
  197. .tx_inclock ( vid_clk ),
  198. .tx_coreclock ( vid_hdmiclk ), // Pixel clock in HDMI domain
  199. .tx_locked ( pll_locked[1] ),
  200. .tx_out ( hdmi_d ),
  201. .tx_outclock ( hdmi_clk )
  202. );
  203. // ABC bus
  204. // On ABC800, only one of XINPSTB# or XOUTPSTB# will be active;
  205. // on ABC80 they will either be 00 or ZZ; in the latter case pulled
  206. // low by external resistors.
  207. wire abc800 = abc_xinpstb_n | abc_xoutpstb_n;
  208. wire abc80 = ~abc800;
  209. // Memory read/write strobes
  210. wire abc_xmemrd = ~abc_xmemfl_n; // For consistency
  211. wire abc_xmemwr = abc800 ? ~abc_xmemw800_n : ~abc_xmemw80_n;
  212. // I/O read/write strobes
  213. wire abc_iord = (abc800 & ~abc_xinpstb_n) | ~(|abc_inp_n);
  214. wire abc_iowr = (abc800 & ~abc_xoutpstb_n) | ~(|abc_out_n);
  215. reg [7:0] abc_do;
  216. reg [7:0] abc_di;
  217. assign abc_d_oe = abc_xmemrd;
  218. assign abc_d = abc_d_oe ? abc_do : 8'hzz;
  219. // Open drain signals with optional MOSFETs
  220. wire abc_wait;
  221. wire abc_resin;
  222. wire abc_int;
  223. wire abc_nmi;
  224. wire abc_xm;
  225. function reg opt_mosfet(input signal, input mosfet);
  226. if (mosfet)
  227. opt_mosfet = signal;
  228. else
  229. opt_mosfet = signal ? 1'b0 : 1'bz;
  230. endfunction // opt_mosfet
  231. assign abc_int80_x = opt_mosfet(abc_int & abc80, mosfet_installed[1]);
  232. assign abc_rdy_x = opt_mosfet(abc_wait, mosfet_installed[2]);
  233. assign abc_nmi_x = opt_mosfet(abc_nmi, mosfet_installed[3]);
  234. assign abc_resin_x = opt_mosfet(abc_resin, mosfet_installed[4]);
  235. assign abc_int800_x = opt_mosfet(abc_int & abc800, mosfet_installed[5]);
  236. assign abc_xm_x = opt_mosfet(abc_xm, mosfet_installed[6]);
  237. // ABC-bus extension header (exth_c and exth_h are input only)
  238. // The naming of pins is kind of nonsensical:
  239. //
  240. // +3V3 - 1 2 - +3V3
  241. // HA - 3 4 - HE
  242. // HB - 5 6 - HG
  243. // HC - 7 8 - HH
  244. // HD - 9 10 - HF
  245. // GND - 11 12 - GND
  246. //
  247. // This layout allows the header to be connected on either side
  248. // of the board. This logic assigns the following names to the pins;
  249. // if the ext_reversed is set to 1 then the left and right sides
  250. // are flipped.
  251. //
  252. // +3V3 - 1 2 - +3V3
  253. // exth[0] - 3 4 - exth[1]
  254. // exth[2] - 5 6 - exth[3]
  255. // exth[6] - 7 8 - exth[7]
  256. // exth[4] - 9 10 - exth[5]
  257. // GND - 11 12 - GND
  258. wire exth_reversed = 1'b0;
  259. wire [7:0] exth_d; // Input data
  260. wire [5:0] exth_q; // Output data
  261. wire [5:0] exth_oe; // Output enable
  262. assign exth_d[0] = exth_reversed ? exth_he : exth_ha;
  263. assign exth_d[1] = exth_reversed ? exth_ha : exth_he;
  264. assign exth_d[2] = exth_reversed ? exth_hg : exth_hb;
  265. assign exth_d[3] = exth_reversed ? exth_hb : exth_hg;
  266. assign exth_d[4] = exth_reversed ? exth_hf : exth_hd;
  267. assign exth_d[5] = exth_reversed ? exth_hd : exth_hf;
  268. assign exth_d[6] = exth_reversed ? exth_hh : exth_hc;
  269. assign exth_d[7] = exth_reversed ? exth_hc : exth_hh;
  270. wire [2:0] erx = { 2'b00, exth_reversed };
  271. assign exth_ha = exth_oe[3'd0 ^ erx] ? exth_q[3'd0 ^ erx] : 1'bz;
  272. assign exth_he = exth_oe[3'd1 ^ erx] ? exth_q[3'd1 ^ erx] : 1'bz;
  273. assign exth_hb = exth_oe[3'd2 ^ erx] ? exth_q[3'd2 ^ erx] : 1'bz;
  274. assign exth_hg = exth_oe[3'd3 ^ erx] ? exth_q[3'd3 ^ erx] : 1'bz;
  275. assign exth_hd = exth_oe[3'd4 ^ erx] ? exth_q[3'd4 ^ erx] : 1'bz;
  276. assign exth_hf = exth_oe[3'd5 ^ erx] ? exth_q[3'd5 ^ erx] : 1'bz;
  277. assign exth_q = 6'b0;
  278. assign exth_oe = 6'b0;
  279. // LED blink counter
  280. reg [28:0] led_ctr;
  281. always @(posedge clk or negedge rst_n)
  282. if (~rst_n)
  283. led_ctr <= 29'b0;
  284. else
  285. led_ctr <= led_ctr + 1'b1;
  286. assign led = led_ctr[28:26];
  287. // SDRAM controller
  288. reg abc_rrq;
  289. reg abc_wrq;
  290. reg abc_xmemrd_q;
  291. reg abc_xmemwr_q;
  292. reg abc_xmem_done;
  293. wire abc_rack;
  294. wire abc_wack;
  295. wire [7:0] abc_sr_rd;
  296. always @(posedge sdram_clk or negedge rst_n)
  297. if (~rst_n)
  298. begin
  299. abc_rrq <= 1'b0;
  300. abc_wrq <= 1'b0;
  301. abc_xmemrd_q <= 1'b0;
  302. abc_xmemwr_q <= 1'b0;
  303. abc_xmem_done <= 1'b0;
  304. end
  305. else
  306. begin
  307. abc_di <= abc_d;
  308. abc_xmemrd_q <= abc_xmemrd;
  309. abc_xmemwr_q <= abc_xmemwr;
  310. abc_xmem_done <= (abc_xmemrd_q & (abc_xmem_done | abc_rack))
  311. | (abc_xmemwr_q & (abc_xmem_done | abc_wack));
  312. abc_rrq <= abc_xmemrd_q & ~(abc_xmem_done | abc_rack);
  313. abc_wrq <= abc_xmemwr_q & ~(abc_xmem_done | abc_wack);
  314. if (abc_rack)
  315. abc_do <= abc_sr_rd;
  316. end // else: !if(~rst_n)
  317. sdram sdram (
  318. .rst_n ( rst_n ),
  319. .sr_clk ( sdram_clk ),
  320. .sr_cke ( sr_cke ),
  321. .sr_cs_n ( sr_cs_n ),
  322. .sr_ras_n ( sr_ras_n ),
  323. .sr_cas_n ( sr_cas_n ),
  324. .sr_we_n ( sr_we_n ),
  325. .sr_dqm ( sr_dqm ),
  326. .sr_ba ( sr_ba ),
  327. .sr_a ( sr_a ),
  328. .sr_dq ( sr_dq ),
  329. .a0 ( { 9'b0, abc_a } ),
  330. .rd0 ( abc_sr_rd ),
  331. .rrq0 ( abc_rrq ),
  332. .rack0 ( abc_rack ),
  333. .wd0 ( abc_d ),
  334. .wrq0 ( abc_wrq ),
  335. .wack0 ( abc_wack ),
  336. .a1 ( 24'hxxxxxx ),
  337. .rd1 ( ),
  338. .rrq1 ( 1'b0 ),
  339. .rack1 ( ),
  340. .wd1 ( 16'hxxxx ),
  341. .wbe1 ( 2'b00 ),
  342. .wrq1 ( 1'b0 ),
  343. .wack1 ( )
  344. );
  345. // SD card
  346. assign sd_clk = 1'b1;
  347. assign sd_cmd = 1'b1;
  348. assign sd_dat = 4'hz;
  349. // USB serial
  350. assign tty_rxd = 1'b1;
  351. assign tty_cts = 1'b1;
  352. // SPI bus (free for ESP32)
  353. assign spi_clk = 1'bz;
  354. assign spi_miso = 1'bz;
  355. assign spi_mosi = 1'bz;
  356. assign spi_cs_esp_n = 1'bz;
  357. assign spi_cs_flash_n = 1'bz;
  358. // ESP32
  359. assign esp_io0 = 1'bz;
  360. assign esp_int = 1'bz;
  361. // I2C
  362. assign i2c_scl = 1'bz;
  363. assign i2c_sda = 1'bz;
  364. // GPIO
  365. assign gpio = 6'bzzzzzz;
  366. endmodule