max80.sv 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 pll_locked;
  109. wire clk; // System clock
  110. wire vid_clk;
  111. pll pll (
  112. .areset ( 1'b0 ),
  113. .inclk0 ( clock_48 ),
  114. .c0 ( sr_clk ), // SDRAM clock (96 MHz)
  115. .c1 ( clk ), // System clock (96 MHz)
  116. .c2 ( vid_clk ), // Video pixel clock
  117. .locked ( pll_locked ),
  118. .phasestep ( 1'b0 ),
  119. .phasecounterselect ( 3'b0 ),
  120. .phaseupdown ( 1'b1 ),
  121. .scanclk ( 1'b0 ),
  122. .phasedone ( )
  123. );
  124. always @(negedge pll_locked or posedge clk)
  125. if (~pll_locked)
  126. begin
  127. rst_ctr <= 1'b0;
  128. rst_n <= 1'b0;
  129. end
  130. else if (~rst_n)
  131. begin
  132. { rst_n, rst_ctr } <= rst_ctr + 1'b1;
  133. end
  134. // Unused device stubs - remove when used
  135. // HDMI - generate random data to give Quartus something to do
  136. reg [23:0] dummydata = 30'hc8_fb87;
  137. always @(posedge vid_clk)
  138. dummydata <= { dummydata[22:0], dummydata[23] };
  139. wire [7:0] hdmi_data[3];
  140. wire [9:0] hdmi_tmds[3];
  141. wire [29:0] hdmi_to_tx;
  142. assign hdmi_data[0] = dummydata[7:0];
  143. assign hdmi_data[1] = dummydata[15:8];
  144. assign hdmi_data[2] = dummydata[23:16];
  145. generate
  146. genvar i;
  147. for (i = 0; i < 3; i = i + 1)
  148. begin : hdmitmds
  149. tmdsenc enc (
  150. .rst_n ( rst_n ),
  151. .clk ( vid_clk ),
  152. .den ( 1'b1 ),
  153. .d ( hdmi_data[i] ),
  154. .c ( 2'b00 ),
  155. .q ( hdmi_tmds[i] )
  156. );
  157. end
  158. endgenerate
  159. assign hdmi_scl = 1'bz;
  160. assign hdmi_sck = 1'bz;
  161. assign hdmi_hpd = 1'bz;
  162. //
  163. // The ALTLVDS_TX megafunctions is MSB-first and in time-major order.
  164. // However, TMDS is LSB-first, and we have three TMDS words that
  165. // concatenate in word(channel)-major order.
  166. //
  167. transpose #(.words(3), .bits(10), .reverse_b(1)) hdmitranspose
  168. (
  169. .d ( { hdmi_tmds[2], hdmi_tmds[1], hdmi_tmds[0] } ),
  170. .q ( hdmi_to_tx )
  171. );
  172. hdmitx hdmitx (
  173. .pll_areset ( 1'b0 ),
  174. .tx_in ( hdmi_to_tx ),
  175. .tx_inclock ( vid_clk ),
  176. .tx_locked ( ),
  177. .tx_out ( hdmi_d ),
  178. .tx_outclock ( hdmi_clk )
  179. );
  180. // ABC bus
  181. // On ABC800, only one of XINPSTB# or XOUTPSTB# will be active;
  182. // on ABC80 they will either be 00 or ZZ; in the latter case pulled
  183. // low by external resistors.
  184. wire abc800 = abc_xinpstb_n | abc_xoutpstb_n;
  185. wire abc80 = ~abc800;
  186. // Memory read/write strobes
  187. wire abc_xmemrd = ~abc_xmemfl_n; // For consistency
  188. wire abc_xmemwr = abc800 ? ~abc_xmemw800_n : ~abc_xmemw80_n;
  189. // I/O read/write strobes
  190. wire abc_iord = (abc800 & ~abc_xinpstb_n) | ~(|abc_inp_n);
  191. wire abc_iowr = (abc800 & ~abc_xoutpstb_n) | ~(|abc_out_n);
  192. // Open drain signals with optional MOSFETs
  193. wire abc_wait;
  194. wire abc_resin;
  195. wire abc_int;
  196. wire abc_nmi;
  197. wire abc_xm;
  198. function reg opt_mosfet(input signal, input mosfet);
  199. if (mosfet)
  200. opt_mosfet = signal;
  201. else
  202. opt_mosfet = signal ? 1'b0 : 1'bz;
  203. endfunction // opt_mosfet
  204. assign abc_int80_x = opt_mosfet(abc_int & abc80, mosfet_installed[1]);
  205. assign abc_rdy_x = opt_mosfet(abc_wait, mosfet_installed[2]);
  206. assign abc_nmi_x = opt_mosfet(abc_nmi, mosfet_installed[3]);
  207. assign abc_resin_x = opt_mosfet(abc_resin, mosfet_installed[4]);
  208. assign abc_int800_x = opt_mosfet(abc_int & abc800, mosfet_installed[5]);
  209. assign abc_xm_x = opt_mosfet(abc_xm, mosfet_installed[6]);
  210. // ABC-bus extension header (exth_c and exth_h are input only)
  211. // The naming of pins is kind of nonsensical:
  212. //
  213. // +3V3 - 1 2 - +3V3
  214. // HA - 3 4 - HE
  215. // HB - 5 6 - HG
  216. // HC - 7 8 - HH
  217. // HD - 9 10 - HF
  218. // GND - 11 12 - GND
  219. //
  220. // This layout allows the header to be connected on either side
  221. // of the board. This logic assigns the following names to the pins;
  222. // if the ext_reversed is set to 1 then the left and right sides
  223. // are flipped.
  224. //
  225. // +3V3 - 1 2 - +3V3
  226. // exth[0] - 3 4 - exth[1]
  227. // exth[2] - 5 6 - exth[3]
  228. // exth[6] - 7 8 - exth[7]
  229. // exth[4] - 9 10 - exth[5]
  230. // GND - 11 12 - GND
  231. wire exth_reversed = 1'b0;
  232. wire [7:0] exth_d; // Input data
  233. wire [5:0] exth_q; // Output data
  234. wire [5:0] exth_oe; // Output enable
  235. assign exth_d[0] = exth_reversed ? exth_he : exth_ha;
  236. assign exth_d[1] = exth_reversed ? exth_ha : exth_he;
  237. assign exth_d[2] = exth_reversed ? exth_hg : exth_hb;
  238. assign exth_d[3] = exth_reversed ? exth_hb : exth_hg;
  239. assign exth_d[4] = exth_reversed ? exth_hf : exth_hd;
  240. assign exth_d[5] = exth_reversed ? exth_hd : exth_hf;
  241. assign exth_d[6] = exth_reversed ? exth_hh : exth_hc;
  242. assign exth_d[7] = exth_reversed ? exth_hc : exth_hh;
  243. wire [2:0] erx = { 2'b00, exth_reversed };
  244. assign exth_ha = exth_oe[3'd0 ^ erx] ? exth_q[3'd0 ^ erx] : 1'bz;
  245. assign exth_he = exth_oe[3'd1 ^ erx] ? exth_q[3'd1 ^ erx] : 1'bz;
  246. assign exth_hb = exth_oe[3'd2 ^ erx] ? exth_q[3'd2 ^ erx] : 1'bz;
  247. assign exth_hg = exth_oe[3'd3 ^ erx] ? exth_q[3'd3 ^ erx] : 1'bz;
  248. assign exth_hd = exth_oe[3'd4 ^ erx] ? exth_q[3'd4 ^ erx] : 1'bz;
  249. assign exth_hf = exth_oe[3'd5 ^ erx] ? exth_q[3'd5 ^ erx] : 1'bz;
  250. assign exth_q = 6'b0;
  251. assign exth_oe = 6'b0;
  252. // LED blink counter
  253. reg [28:0] led_ctr;
  254. always @(posedge clk or negedge rst_n)
  255. if (~rst_n)
  256. led_ctr <= 29'b0;
  257. else
  258. led_ctr <= led_ctr + 1'b1;
  259. assign led = led_ctr[28:26];
  260. // SDRAM bus
  261. assign sr_cke = 1'b0;
  262. assign sr_ba = 2'b0;
  263. assign sr_a = 13'b0;
  264. assign sr_dq = 16'b0;
  265. assign sr_dqm = 2'b11;
  266. assign sr_cs_n = 1'b1;
  267. assign sr_we_n = 1'b1;
  268. assign sr_cas_n = 1'b1;
  269. assign sr_ras_n = 1'b1;
  270. // SD card
  271. assign sd_clk = 1'b1;
  272. assign sd_cmd = 1'b1;
  273. assign sd_dat = 4'hz;
  274. // USB serial
  275. assign tty_rxd = 1'b1;
  276. assign tty_cts = 1'b1;
  277. // SPI bus (free for ESP32)
  278. assign spi_clk = 1'bz;
  279. assign spi_miso = 1'bz;
  280. assign spi_mosi = 1'bz;
  281. assign spi_cs_esp_n = 1'bz;
  282. assign spi_cs_flash_n = 1'bz;
  283. // ESP32
  284. assign esp_io0 = 1'bz;
  285. assign esp_int = 1'bz;
  286. // I2C
  287. assign i2c_scl = 1'bz;
  288. assign i2c_sda = 1'bz;
  289. // GPIO
  290. assign gpio = 6'bzzzzzz;
  291. endmodule