2
0

max80.sv 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. // Clocks
  110. wire sdram_clk;
  111. wire clk; // System clock
  112. wire vid_clk; // Video pixel clock
  113. wire vid_hdmiclk; // D:o in the HDMI clock domain
  114. pll pll (
  115. .areset ( 1'b0 ),
  116. .inclk0 ( clock_48 ),
  117. .c0 ( sdram_clk ), // SDRAM clock (168 MHz)
  118. .c1 ( clk ), // System clock (84 MHz)
  119. .c2 ( vid_clk ), // Video pixel clock (48 MHz)
  120. .locked ( pll_locked[0] ),
  121. .phasestep ( 1'b0 ),
  122. .phasecounterselect ( 3'b0 ),
  123. .phaseupdown ( 1'b1 ),
  124. .scanclk ( 1'b0 ),
  125. .phasedone ( )
  126. );
  127. wire all_plls_locked = &pll_locked;
  128. always @(negedge all_plls_locked or posedge clk)
  129. if (~&all_plls_locked)
  130. begin
  131. rst_ctr <= 1'b0;
  132. rst_n <= 1'b0;
  133. end
  134. else if (~rst_n)
  135. begin
  136. { rst_n, rst_ctr } <= rst_ctr + 1'b1;
  137. end
  138. // Unused device stubs - remove when used
  139. // Reset in the video clock domain
  140. reg vid_rst_n;
  141. always @(negedge all_plls_locked or posedge vid_clk)
  142. if (~all_plls_locked)
  143. vid_rst_n <= 1'b0;
  144. else
  145. vid_rst_n <= rst_n;
  146. // HDMI - generate random data to give Quartus something to do
  147. reg [23:0] dummydata = 30'hc8_fb87;
  148. always @(posedge vid_clk)
  149. dummydata <= { dummydata[22:0], dummydata[23] };
  150. wire [7:0] hdmi_data[3];
  151. wire [9:0] hdmi_tmds[3];
  152. wire [29:0] hdmi_to_tx;
  153. assign hdmi_data[0] = dummydata[7:0];
  154. assign hdmi_data[1] = dummydata[15:8];
  155. assign hdmi_data[2] = dummydata[23:16];
  156. generate
  157. genvar i;
  158. for (i = 0; i < 3; i = i + 1)
  159. begin : hdmitmds
  160. tmdsenc enc (
  161. .rst_n ( vid_rst_n ),
  162. .clk ( vid_clk ),
  163. .den ( 1'b1 ),
  164. .d ( hdmi_data[i] ),
  165. .c ( 2'b00 ),
  166. .q ( hdmi_tmds[i] )
  167. );
  168. end
  169. endgenerate
  170. assign hdmi_scl = 1'bz;
  171. assign hdmi_sck = 1'bz;
  172. assign hdmi_hpd = 1'bz;
  173. //
  174. // The ALTLVDS_TX megafunctions is MSB-first and in time-major order.
  175. // However, TMDS is LSB-first, and we have three TMDS words that
  176. // concatenate in word(channel)-major order.
  177. //
  178. transpose #(.words(3), .bits(10), .reverse_b(1),
  179. .reg_d(0), .reg_q(0)) hdmitranspose
  180. (
  181. .clk ( vid_clk ),
  182. .d ( { hdmi_tmds[2], hdmi_tmds[1], hdmi_tmds[0] } ),
  183. .q ( hdmi_to_tx )
  184. );
  185. hdmitx hdmitx (
  186. .pll_areset ( ~pll_locked[0] ),
  187. .tx_in ( hdmi_to_tx ),
  188. .tx_inclock ( vid_clk ),
  189. .tx_coreclock ( vid_hdmiclk ), // Pixel clock in HDMI domain
  190. .tx_locked ( pll_locked[1] ),
  191. .tx_out ( hdmi_d ),
  192. .tx_outclock ( hdmi_clk )
  193. );
  194. // ABC bus
  195. // On ABC800, only one of XINPSTB# or XOUTPSTB# will be active;
  196. // on ABC80 they will either be 00 or ZZ; in the latter case pulled
  197. // low by external resistors.
  198. wire abc800 = abc_xinpstb_n | abc_xoutpstb_n;
  199. wire abc80 = ~abc800;
  200. // Memory read/write strobes
  201. wire abc_xmemrd = ~abc_xmemfl_n; // For consistency
  202. wire abc_xmemwr = abc800 ? ~abc_xmemw800_n : ~abc_xmemw80_n;
  203. // I/O read/write strobes
  204. wire abc_iord = (abc800 & ~abc_xinpstb_n) | ~(|abc_inp_n);
  205. wire abc_iowr = (abc800 & ~abc_xoutpstb_n) | ~(|abc_out_n);
  206. reg [7:0] abc_do;
  207. reg [7:0] abc_di;
  208. assign abc_d_oe = abc_xmemrd;
  209. assign abc_d = abc_d_oe ? abc_do : 8'hzz;
  210. // Open drain signals with optional MOSFETs
  211. wire abc_wait;
  212. wire abc_resin;
  213. wire abc_int;
  214. wire abc_nmi;
  215. wire abc_xm;
  216. function reg opt_mosfet(input signal, input mosfet);
  217. if (mosfet)
  218. opt_mosfet = signal;
  219. else
  220. opt_mosfet = signal ? 1'b0 : 1'bz;
  221. endfunction // opt_mosfet
  222. assign abc_int80_x = opt_mosfet(abc_int & abc80, mosfet_installed[1]);
  223. assign abc_rdy_x = opt_mosfet(abc_wait, mosfet_installed[2]);
  224. assign abc_nmi_x = opt_mosfet(abc_nmi, mosfet_installed[3]);
  225. assign abc_resin_x = opt_mosfet(abc_resin, mosfet_installed[4]);
  226. assign abc_int800_x = opt_mosfet(abc_int & abc800, mosfet_installed[5]);
  227. assign abc_xm_x = opt_mosfet(abc_xm, mosfet_installed[6]);
  228. // ABC-bus extension header (exth_c and exth_h are input only)
  229. // The naming of pins is kind of nonsensical:
  230. //
  231. // +3V3 - 1 2 - +3V3
  232. // HA - 3 4 - HE
  233. // HB - 5 6 - HG
  234. // HC - 7 8 - HH
  235. // HD - 9 10 - HF
  236. // GND - 11 12 - GND
  237. //
  238. // This layout allows the header to be connected on either side
  239. // of the board. This logic assigns the following names to the pins;
  240. // if the ext_reversed is set to 1 then the left and right sides
  241. // are flipped.
  242. //
  243. // +3V3 - 1 2 - +3V3
  244. // exth[0] - 3 4 - exth[1]
  245. // exth[2] - 5 6 - exth[3]
  246. // exth[6] - 7 8 - exth[7]
  247. // exth[4] - 9 10 - exth[5]
  248. // GND - 11 12 - GND
  249. wire exth_reversed = 1'b0;
  250. wire [7:0] exth_d; // Input data
  251. wire [5:0] exth_q; // Output data
  252. wire [5:0] exth_oe; // Output enable
  253. assign exth_d[0] = exth_reversed ? exth_he : exth_ha;
  254. assign exth_d[1] = exth_reversed ? exth_ha : exth_he;
  255. assign exth_d[2] = exth_reversed ? exth_hg : exth_hb;
  256. assign exth_d[3] = exth_reversed ? exth_hb : exth_hg;
  257. assign exth_d[4] = exth_reversed ? exth_hf : exth_hd;
  258. assign exth_d[5] = exth_reversed ? exth_hd : exth_hf;
  259. assign exth_d[6] = exth_reversed ? exth_hh : exth_hc;
  260. assign exth_d[7] = exth_reversed ? exth_hc : exth_hh;
  261. wire [2:0] erx = { 2'b00, exth_reversed };
  262. assign exth_ha = exth_oe[3'd0 ^ erx] ? exth_q[3'd0 ^ erx] : 1'bz;
  263. assign exth_he = exth_oe[3'd1 ^ erx] ? exth_q[3'd1 ^ erx] : 1'bz;
  264. assign exth_hb = exth_oe[3'd2 ^ erx] ? exth_q[3'd2 ^ erx] : 1'bz;
  265. assign exth_hg = exth_oe[3'd3 ^ erx] ? exth_q[3'd3 ^ erx] : 1'bz;
  266. assign exth_hd = exth_oe[3'd4 ^ erx] ? exth_q[3'd4 ^ erx] : 1'bz;
  267. assign exth_hf = exth_oe[3'd5 ^ erx] ? exth_q[3'd5 ^ erx] : 1'bz;
  268. assign exth_q = 6'b0;
  269. assign exth_oe = 6'b0;
  270. // LED blink counter
  271. reg [28:0] led_ctr;
  272. always @(posedge clk or negedge rst_n)
  273. if (~rst_n)
  274. led_ctr <= 29'b0;
  275. else
  276. led_ctr <= led_ctr + 1'b1;
  277. //assign led = led_ctr[28:26];
  278. // SDRAM controller
  279. reg abc_rrq;
  280. reg abc_wrq;
  281. reg abc_xmemrd_q;
  282. reg abc_xmemwr_q;
  283. reg abc_xmem_done;
  284. reg [9:0] abc_mempg;
  285. wire abc_rack;
  286. wire abc_wack;
  287. wire [7:0] abc_sr_rd;
  288. always @(posedge sdram_clk or negedge rst_n)
  289. if (~rst_n)
  290. begin
  291. abc_rrq <= 1'b0;
  292. abc_wrq <= 1'b0;
  293. abc_xmemrd_q <= 1'b0;
  294. abc_xmemwr_q <= 1'b0;
  295. abc_xmem_done <= 1'b0;
  296. abc_mempg <= 0;
  297. end
  298. else
  299. begin
  300. abc_di <= abc_d;
  301. abc_xmemrd_q <= abc_xmemrd;
  302. abc_xmemwr_q <= abc_xmemwr;
  303. abc_xmem_done <= (abc_xmemrd_q & (abc_xmem_done | abc_rack))
  304. | (abc_xmemwr_q & (abc_xmem_done | abc_wack));
  305. abc_rrq <= abc_xmemrd_q & ~(abc_xmem_done | abc_rack);
  306. abc_wrq <= abc_xmemwr_q & ~(abc_xmem_done | abc_wack);
  307. if (abc_rack & abc_rvalid)
  308. abc_do <= abc_sr_rd;
  309. // HACK FOR TESTING ONLY
  310. if (abc_iowr)
  311. abc_mempg <= { abc_a[1:0], abc_di };
  312. end // else: !if(~rst_n)
  313. sdram sdram (
  314. .rst_n ( rst_n ),
  315. .clk ( sdram_clk ), // Input clock
  316. .sr_clk ( sr_clk ), // Output clock buffer
  317. .sr_cke ( sr_cke ),
  318. .sr_cs_n ( sr_cs_n ),
  319. .sr_ras_n ( sr_ras_n ),
  320. .sr_cas_n ( sr_cas_n ),
  321. .sr_we_n ( sr_we_n ),
  322. .sr_dqm ( sr_dqm ),
  323. .sr_ba ( sr_ba ),
  324. .sr_a ( sr_a ),
  325. .sr_dq ( sr_dq ),
  326. .a0 ( { abc_mempg, abc_a } ),
  327. .rd0 ( abc_sr_rd ),
  328. .rrq0 ( abc_rrq ),
  329. .rack0 ( abc_rack ),
  330. .rvalid0 ( abc_rvalid ),
  331. .wd0 ( abc_d ),
  332. .wrq0 ( abc_wrq ),
  333. .wack0 ( abc_wack ),
  334. .a1 ( 24'hxxxxxx ),
  335. .be1 ( 8'b0000_0000 ),
  336. .rd1 ( ),
  337. .rrq1 ( 1'b0 ),
  338. .rack1 ( ),
  339. .rvalid1 ( ),
  340. .wd1 ( 32'hxxxx_xxxx ),
  341. .wrq1 ( 1'b0 ),
  342. .wack1 ( )
  343. );
  344. // SD card
  345. assign sd_clk = 1'b1;
  346. assign sd_cmd = 1'b1;
  347. assign sd_dat = 4'hz;
  348. // USB serial
  349. assign tty_rxd = 1'b1;
  350. assign tty_cts = 1'b1;
  351. // SPI bus (free for ESP32)
  352. assign spi_clk = 1'bz;
  353. assign spi_miso = 1'bz;
  354. assign spi_mosi = 1'bz;
  355. assign spi_cs_esp_n = 1'bz;
  356. assign spi_cs_flash_n = 1'bz;
  357. // ESP32
  358. assign esp_io0 = 1'bz;
  359. assign esp_int = 1'bz;
  360. // I2C
  361. assign i2c_scl = 1'bz;
  362. assign i2c_sda = 1'bz;
  363. // GPIO
  364. assign gpio = 6'bzzzzzz;
  365. // Embedded RISC-V CPU
  366. parameter cpu_fast_mem_bits = 11; /* 2^[this] * 4 bytes */
  367. wire cpu_mem_read;
  368. wire cpu_mem_write;
  369. wire [31:0] cpu_mem_addr;
  370. wire [31:0] cpu_mem_wdata;
  371. wire [ 7:0] cpu_mem_be;
  372. reg [31:0] cpu_mem_rdata;
  373. wire cpu_mem_valid = cpu_mem_read | cpu_mem_write;
  374. wire cpu_mem_instr;
  375. wire cpu_la_read;
  376. wire cpu_la_write;
  377. wire [31:0] cpu_la_addr;
  378. picorv32 #(
  379. .ENABLE_COUNTERS ( 1 ),
  380. .ENABLE_COUNTERS64 ( 1 ),
  381. .ENABLE_REGS_16_31 ( 1 ),
  382. .ENABLE_REGS_DUALPORT ( 1 ),
  383. .LATCHED_MEM_RDATA ( 1 ),
  384. .BARREL_SHIFTER ( 1 ),
  385. .TWO_CYCLE_COMPARE ( 0 ),
  386. .TWO_CYCLE_ALU ( 0 ),
  387. .COMPRESSED_ISA ( 1 ),
  388. .CATCH_MISALIGN ( 1 ),
  389. .CATCH_ILLINSN ( 1 ),
  390. .UNALIGNED_DATA ( 1 ),
  391. .ENABLE_FAST_MUL ( 1 ),
  392. .ENABLE_DIV ( 1 ),
  393. .ENABLE_IRQ ( 1 ),
  394. .ENABLE_IRQ_QREGS ( 1 ),
  395. .ENABLE_IRQ_TIMER ( 1 ),
  396. .REGS_INIT_ZERO ( 1 ),
  397. .STACKADDR ( 3'h4 << cpu_fast_mem_bits )
  398. )
  399. cpu (
  400. .clk ( clk ),
  401. .resetn ( rst_n ),
  402. .trap ( ),
  403. .mem_instr ( cpu_mem_instr ),
  404. .mem_ready ( cpu_mem_ready ),
  405. .mem_read ( cpu_mem_read ),
  406. .mem_write ( cpu_mem_write ),
  407. .mem_addr ( cpu_mem_addr ),
  408. .mem_wdata ( cpu_mem_wdata ),
  409. .mem_be ( cpu_mem_be ),
  410. .mem_rdata ( cpu_mem_rdata ),
  411. .irq ( 0 ),
  412. .eoi ( )
  413. );
  414. // Address space addressed by CPU
  415. wire [3:0] cpu_aspace = 4'b0001 << cpu_mem_addr[31:30];
  416. // cpu_mem_ready is always true for fast memory
  417. assign cpu_mem_ready = cpu_mem_valid;
  418. //
  419. // Fast memory. This runs on the SDRAM clock, i.e. 2x the speed
  420. // of the CPU. The .bits parameter gives the number of dwords
  421. // as a power of 2, i.e. 11 = 2^11 * 4 = 8K.
  422. //
  423. wire [31:0] fast_mem_rdata;
  424. fast_mem #(.bits(cpu_fast_mem_bits), .mif("../fw/boot"))
  425. fast_mem(
  426. .rst_n ( rst_n ),
  427. .clk ( ~clk ),
  428. .read ( cpu_aspace[0] & cpu_mem_read ),
  429. .write ( cpu_aspace[0] & cpu_mem_write ),
  430. .be ( cpu_mem_be ),
  431. .addr ( cpu_mem_addr[12:2] ),
  432. .wdata ( cpu_mem_wdata ),
  433. .rdata ( fast_mem_rdata )
  434. );
  435. always @(posedge clk)
  436. cpu_mem_rdata <= cpu_aspace[0] ? fast_mem_rdata : 32'hxxxx_xxxx;
  437. // Avoid optimizing out the CPU
  438. reg [3:1] led_q;
  439. always @(negedge rst_n or posedge clk)
  440. if (~rst_n)
  441. led_q <= 3'b000;
  442. else
  443. if ( cpu_mem_write & cpu_aspace[3] )
  444. led_q <= cpu_mem_wdata[2:0];
  445. assign led = led_q;
  446. endmodule