2
0

max80.sv 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. // SDRAM bus
  40. output sr_clk,
  41. output sr_cke,
  42. output [1:0] sr_ba, // Bank address
  43. output [12:0] sr_a, // Address within bank
  44. inout [15:0] sr_dq, // Also known as D or IO
  45. output [1:0] sr_dqm, // DQML and DQMH
  46. output sr_cs_n,
  47. output sr_we_n,
  48. output sr_cas_n,
  49. output sr_ras_n,
  50. // SD card
  51. output sd_clk,
  52. output sd_cmd,
  53. inout [3:0] sd_dat,
  54. // USB serial (naming is FPGA as DCE)
  55. input tty_txd,
  56. output tty_rxd,
  57. input tty_rts,
  58. output tty_cts,
  59. input tty_dtr,
  60. // SPI flash memory (also configuration)
  61. output flash_cs_n,
  62. output flash_clk,
  63. output flash_mosi,
  64. input flash_miso,
  65. // SPI bus (connected to ESP32 so can be bidirectional)
  66. inout spi_clk,
  67. inout spi_miso,
  68. inout spi_mosi,
  69. inout spi_cs_esp_n, // ESP32 IO10
  70. // Other ESP32 connections
  71. inout esp_io0, // ESP32 IO00
  72. inout esp_int, // ESP32 IO09
  73. // I2C bus (RTC and external)
  74. inout i2c_scl,
  75. inout i2c_sda,
  76. input rtc_32khz,
  77. input rtc_int_n,
  78. // LED
  79. output [3:1] led,
  80. // GPIO pins
  81. inout [5:0] gpio,
  82. // HDMI
  83. output [2:0] hdmi_d,
  84. output hdmi_clk,
  85. inout hdmi_scl,
  86. inout hdmi_sda,
  87. inout hdmi_hpd
  88. );
  89. // Set if MOSFETs Q1-Q6 are installed rather than the corresponding
  90. // resistors.
  91. parameter [6:1] mosfet_installed = 6'b000_000;
  92. // PLL and reset
  93. parameter reset_pow2 = 12; // Assert internal reset for 4096 cycles
  94. reg [reset_pow2-1:0] rst_ctr;
  95. reg rst_n; // Internal reset
  96. wire pll_locked;
  97. wire clk; // System clock
  98. wire vid_clk;
  99. pll pll (
  100. .areset ( 1'b0 ),
  101. .inclk0 ( clock_48 ),
  102. .c0 ( sr_clk ), // SDRAM clock (96 MHz)
  103. .c1 ( clk ), // System clock (96 MHz)
  104. .c2 ( vid_clk ), // Video pixel clock
  105. .locked ( pll_locked ),
  106. .phasestep ( 1'b0 ),
  107. .phasecounterselect ( 3'b0 ),
  108. .phaseupdown ( 1'b1 ),
  109. .scanclk ( 1'b0 ),
  110. .phasedone ( )
  111. );
  112. always @(negedge pll_locked or posedge clk)
  113. if (~pll_locked)
  114. begin
  115. rst_ctr <= 1'b0;
  116. rst_n <= 1'b0;
  117. end
  118. else if (~rst_n)
  119. begin
  120. { rst_n, rst_ctr } <= rst_ctr + 1'b1;
  121. end
  122. // Unused device stubs - remove when used
  123. // HDMI - generate random data to give Quartus something to do
  124. reg [23:0] dummydata = 30'hc8_fb87;
  125. always @(posedge vid_clk)
  126. dummydata <= { dummydata[22:0], dummydata[23] };
  127. wire [7:0] hdmi_data[3];
  128. wire [9:0] hdmi_tmds[3];
  129. wire [29:0] hdmi_to_tx;
  130. assign hdmi_data[0] = dummydata[7:0];
  131. assign hdmi_data[1] = dummydata[15:8];
  132. assign hdmi_data[2] = dummydata[23:16];
  133. generate
  134. genvar i;
  135. for (i = 0; i < 3; i = i + 1)
  136. begin : hdmitmds
  137. tmdsenc enc (
  138. .rst_n ( rst_n ),
  139. .clk ( vid_clk ),
  140. .den ( 1'b1 ),
  141. .d ( hdmi_data[i] ),
  142. .c ( 2'b00 ),
  143. .q ( hdmi_tmds[i] )
  144. );
  145. end
  146. endgenerate
  147. assign hdmi_scl = 1'bz;
  148. assign hdmi_sck = 1'bz;
  149. assign hdmi_hpd = 1'bz;
  150. //
  151. // The ALTLVDS_TX megafunctions is MSB-first and in time-major order.
  152. // However, TMDS is LSB-first, and we have three TMDS words that
  153. // concatenate in word(channel)-major order.
  154. //
  155. transpose #(.words(3), .bits(10), .reverse_b(1)) hdmitranspose
  156. (
  157. .d ( { hdmi_tmds[2], hdmi_tmds[1], hdmi_tmds[0] } ),
  158. .q ( hdmi_to_tx )
  159. );
  160. hdmitx hdmitx (
  161. .pll_areset ( 1'b0 ),
  162. .tx_in ( hdmi_to_tx ),
  163. .tx_inclock ( vid_clk ),
  164. .tx_locked ( ),
  165. .tx_out ( hdmi_d ),
  166. .tx_outclock ( hdmi_clk )
  167. );
  168. // ABC bus
  169. // On ABC800, only one of XINPSTB# or XOUTPSTB# will be active;
  170. // on ABC80 they will either be 00 or ZZ; in the latter case pulled
  171. // low by external resistors.
  172. wire abc800 = abc_xinpstb_n | abc_xoutpstb_n;
  173. wire abc80 = ~abc800;
  174. // Memory read/write strobes
  175. wire abc_xmemrd = ~abc_xmemfl_n; // For consistency
  176. wire abc_xmemwr = abc800 ? ~abc_xmemw800_n : ~abc_xmemw80_n;
  177. // I/O read/write strobes
  178. wire abc_iord = (abc800 & ~abc_xinpstb_n) | ~(|abc_inp_n);
  179. wire abc_iowr = (abc800 & ~abc_xoutpstb_n) | ~(|abc_out_n);
  180. // Open drain signals with optional MOSFETs
  181. wire abc_wait;
  182. wire abc_resin;
  183. wire abc_int;
  184. wire abc_nmi;
  185. wire abc_xm;
  186. function reg opt_mosfet(input signal, input mosfet);
  187. if (mosfet)
  188. opt_mosfet = signal;
  189. else
  190. opt_mosfet = signal ? 1'b0 : 1'bz;
  191. endfunction // opt_mosfet
  192. assign abc_int80_x = opt_mosfet(abc_int & abc80, mosfet_installed[1]);
  193. assign abc_rdy_x = opt_mosfet(abc_wait, mosfet_installed[2]);
  194. assign abc_nmi_x = opt_mosfet(abc_nmi, mosfet_installed[3]);
  195. assign abc_resin_x = opt_mosfet(abc_resin, mosfet_installed[4]);
  196. assign abc_int800_x = opt_mosfet(abc_int & abc800, mosfet_installed[5]);
  197. assign abc_xm_x = opt_mosfet(abc_xm, mosfet_installed[6]);
  198. // LED blink counter
  199. reg [28:0] led_ctr;
  200. always @(posedge clk or negedge rst_n)
  201. if (~rst_n)
  202. led_ctr <= 29'b0;
  203. else
  204. led_ctr <= led_ctr + 1'b1;
  205. assign led = led_ctr[28:26];
  206. // SDRAM bus
  207. assign sr_cke = 1'b0;
  208. assign sr_ba = 2'b0;
  209. assign sr_a = 13'b0;
  210. assign sr_dq = 16'b0;
  211. assign sr_dqm = 2'b11;
  212. assign sr_cs_n = 1'b1;
  213. assign sr_we_n = 1'b1;
  214. assign sr_cas_n = 1'b1;
  215. assign sr_ras_n = 1'b1;
  216. // SD card
  217. assign sd_clk = 1'b1;
  218. assign sd_cmd = 1'b1;
  219. assign sd_dat = 4'hz;
  220. // USB serial
  221. assign tty_rxd = 1'b1;
  222. assign tty_cts = 1'b1;
  223. // SPI bus (free for ESP32)
  224. assign spi_clk = 1'bz;
  225. assign spi_miso = 1'bz;
  226. assign spi_mosi = 1'bz;
  227. assign spi_cs_esp_n = 1'bz;
  228. assign spi_cs_flash_n = 1'bz;
  229. // ESP32
  230. assign esp_io0 = 1'bz;
  231. assign esp_int = 1'bz;
  232. // I2C
  233. assign i2c_scl = 1'bz;
  234. assign i2c_sda = 1'bz;
  235. // GPIO
  236. assign gpio = 6'bzzzzzz;
  237. endmodule