2
0

max80.sv 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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 on the ABC-bus.
  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. // Host/device control
  35. output abc_master, // 1 = host, 0 = device
  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_sck,
  74. inout [1:0] flash_io,
  75. // SPI bus (connected to ESP32 so can be bidirectional)
  76. inout spi_clk,
  77. inout spi_miso,
  78. inout spi_mosi,
  79. inout spi_cs_esp_n, // ESP32 IO10
  80. inout spi_cs_flash_n, // ESP32 IO01
  81. // Other ESP32 connections
  82. inout esp_io0, // ESP32 IO00
  83. inout esp_int, // ESP32 IO09
  84. // I2C bus (RTC and external)
  85. inout i2c_scl,
  86. inout i2c_sda,
  87. input rtc_32khz,
  88. input rtc_int_n,
  89. // LED (2 = D23/G, 1 = D22/R, 0 = D17/B)
  90. output [2:0] led,
  91. // GPIO pins
  92. inout [5:0] gpio,
  93. // HDMI
  94. output [2:0] hdmi_d,
  95. output hdmi_clk,
  96. inout hdmi_scl,
  97. inout hdmi_sda,
  98. inout hdmi_hpd
  99. );
  100. // Set if MOSFETs Q1-Q6 are installed rather than the corresponding
  101. // resistors.
  102. parameter [6:1] mosfet_installed = 6'b000_000;
  103. // PLL and reset
  104. parameter reset_pow2 = 12; // Assert internal reset for 4096 cycles after PLL lock
  105. reg [reset_pow2-1:0] rst_ctr = 1'b0;
  106. reg rst_n = 1'b0; // Internal reset
  107. wire [1:0] pll_locked;
  108. // Clocks
  109. wire sdram_clk; // SDRAM clock
  110. wire sdram_out_clk; // SDRAM clock, phase shifted
  111. wire sys_clk; // System clock
  112. wire vid_clk; // Video pixel clock
  113. wire vid_hdmiclk; // D:o in the HDMI clock domain
  114. wire flash_clk; // Serial flash ROM clock
  115. reg reset_cmd_q = 1'b0;
  116. wire reset_cmd;
  117. pll pll (
  118. .areset ( reset_cmd_q ),
  119. .inclk0 ( clock_48 ),
  120. .c0 ( sdram_out_clk ), // SDRAM external clock (168 MHz)
  121. .c1 ( sys_clk ), // System clock (84 MHz)
  122. .c2 ( vid_clk ), // Video pixel clock (48 MHz)
  123. .c3 ( flash_clk ), // Serial flash ROM clock (134 MHz)
  124. .c4 ( sdram_clk ), // SDRAM internal clock (168 MHz)
  125. .locked ( pll_locked[0] ),
  126. .phasestep ( 1'b0 ),
  127. .phasecounterselect ( 3'b0 ),
  128. .phaseupdown ( 1'b1 ),
  129. .scanclk ( 1'b0 ),
  130. .phasedone ( )
  131. );
  132. wire all_plls_locked = &pll_locked;
  133. always @(negedge all_plls_locked or posedge sys_clk)
  134. if (~&all_plls_locked)
  135. begin
  136. rst_ctr <= 1'b0;
  137. rst_n <= 1'b0;
  138. reset_cmd_q <= 1'b0;
  139. end
  140. else
  141. begin
  142. reset_cmd_q <= rst_n & (reset_cmd_q | reset_cmd);
  143. if (~rst_n)
  144. { rst_n, rst_ctr } <= rst_ctr + 1'b1;
  145. end
  146. // Unused device stubs - remove when used
  147. // Reset in the video clock domain
  148. reg vid_rst_n;
  149. always @(negedge all_plls_locked or posedge vid_clk)
  150. if (~all_plls_locked)
  151. vid_rst_n <= 1'b0;
  152. else
  153. vid_rst_n <= rst_n;
  154. // HDMI - generate random data to give Quartus something to do
  155. reg [23:0] dummydata = 30'hc8_fb87;
  156. always @(posedge vid_clk)
  157. dummydata <= { dummydata[22:0], dummydata[23] };
  158. wire [7:0] hdmi_data[3];
  159. wire [9:0] hdmi_tmds[3];
  160. wire [29:0] hdmi_to_tx;
  161. assign hdmi_data[0] = dummydata[7:0];
  162. assign hdmi_data[1] = dummydata[15:8];
  163. assign hdmi_data[2] = dummydata[23:16];
  164. generate
  165. genvar i;
  166. for (i = 0; i < 3; i = i + 1)
  167. begin : hdmitmds
  168. tmdsenc enc (
  169. .rst_n ( vid_rst_n ),
  170. .clk ( vid_clk ),
  171. .den ( 1'b1 ),
  172. .d ( hdmi_data[i] ),
  173. .c ( 2'b00 ),
  174. .q ( hdmi_tmds[i] )
  175. );
  176. end
  177. endgenerate
  178. assign hdmi_scl = 1'bz;
  179. assign hdmi_sda = 1'bz;
  180. assign hdmi_hpd = 1'bz;
  181. //
  182. // The ALTLVDS_TX megafunctions is MSB-first and in time-major order.
  183. // However, TMDS is LSB-first, and we have three TMDS words that
  184. // concatenate in word(channel)-major order.
  185. //
  186. transpose #(.words(3), .bits(10), .reverse_b(1),
  187. .reg_d(0), .reg_q(0)) hdmitranspose
  188. (
  189. .clk ( vid_clk ),
  190. .d ( { hdmi_tmds[2], hdmi_tmds[1], hdmi_tmds[0] } ),
  191. .q ( hdmi_to_tx )
  192. );
  193. hdmitx hdmitx (
  194. .pll_areset ( ~pll_locked[0] ),
  195. .tx_in ( hdmi_to_tx ),
  196. .tx_inclock ( vid_clk ),
  197. .tx_coreclock ( vid_hdmiclk ), // Pixel clock in HDMI domain
  198. .tx_locked ( pll_locked[1] ),
  199. .tx_out ( hdmi_d ),
  200. .tx_outclock ( hdmi_clk )
  201. );
  202. // ABC bus
  203. assign abc_master = 1'b0; // Only device mode supported
  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. // SDRAM controller
  280. reg abc_rrq;
  281. reg abc_wrq;
  282. reg abc_xmemrd_q;
  283. reg abc_xmemwr_q;
  284. reg abc_xmem_done;
  285. reg [9:0] abc_mempg;
  286. wire abc_rack;
  287. wire abc_wack;
  288. wire abc_rready;
  289. wire [7:0] abc_sr_rd;
  290. always @(posedge sdram_clk or negedge rst_n)
  291. if (~rst_n)
  292. begin
  293. abc_rrq <= 1'b0;
  294. abc_wrq <= 1'b0;
  295. abc_xmemrd_q <= 1'b0;
  296. abc_xmemwr_q <= 1'b0;
  297. abc_xmem_done <= 1'b0;
  298. abc_mempg <= 0;
  299. end
  300. else
  301. begin
  302. abc_di <= abc_d;
  303. abc_xmemrd_q <= abc_xmemrd;
  304. abc_xmemwr_q <= abc_xmemwr;
  305. abc_xmem_done <= (abc_xmemrd_q & (abc_xmem_done | abc_rack))
  306. | (abc_xmemwr_q & (abc_xmem_done | abc_wack));
  307. abc_rrq <= abc_xmemrd_q & ~(abc_xmem_done | abc_rack);
  308. abc_wrq <= abc_xmemwr_q & ~(abc_xmem_done | abc_wack);
  309. if (abc_rack & abc_rready)
  310. abc_do <= abc_sr_rd;
  311. // HACK FOR TESTING ONLY
  312. if (abc_iowr)
  313. abc_mempg <= { abc_a[1:0], abc_di };
  314. end // else: !if(~rst_n)
  315. //
  316. // Internal CPU bus
  317. //
  318. wire cpu_mem_valid;
  319. wire cpu_mem_instr;
  320. wire [ 3:0] cpu_mem_wstrb;
  321. wire [31:0] cpu_mem_addr;
  322. wire [31:0] cpu_mem_wdata;
  323. reg [31:0] cpu_mem_rdata;
  324. wire cpu_mem_ready;
  325. wire cpu_la_read;
  326. wire cpu_la_write;
  327. wire [31:0] cpu_la_addr;
  328. wire [31:0] cpu_la_wdata;
  329. wire [ 3:0] cpu_la_wstrb;
  330. // cpu_mem_valid by address quadrant
  331. wire [ 3:0] cpu_mem_quad = cpu_mem_valid << cpu_mem_addr[31:30];
  332. // Decode for small devices; use address space within range of
  333. // negative offsets from the zero register [-1K,0)
  334. //
  335. // Device map:
  336. // 0 - LED
  337. // 1 - Reset
  338. // 2 - SPI->SDRAM downloader
  339. // 3 - Serial port
  340. // 4 - SD card
  341. // 5 - system local clock (not RTC)
  342. //
  343. // A device has IRQ (devno)+16 if it needs an interrupt.
  344. //
  345. wire [15:0] iodev = cpu_mem_quad[3] << cpu_mem_addr[10:7];
  346. tri0 [15:0] iodev_irq; // tri0: if nothing is driving, value is 0
  347. //
  348. // SDRAM
  349. //
  350. wire [31:0] sdram_rd;
  351. wire sdram_rack;
  352. wire sdram_rready;
  353. wire sdram_wack;
  354. reg sdram_acked;
  355. wire [15:0] sdram_rom_wd;
  356. wire [24:1] sdram_rom_waddr;
  357. wire [ 1:0] sdram_rom_wrq;
  358. wire sdram_rom_wacc;
  359. always @(posedge sdram_clk)
  360. sdram_acked <= cpu_mem_quad[1] & (sdram_acked | sdram_rack | sdram_wack);
  361. wire sdram_req = cpu_mem_quad[1] & ~sdram_acked;
  362. sdram sdram (
  363. .rst_n ( rst_n ),
  364. .clk ( sdram_clk ), // Internal clock
  365. .out_clk ( sdram_out_clk ), // External clock (phase shifted)
  366. .sr_clk ( sr_clk ), // Output clock buffer
  367. .sr_cke ( sr_cke ),
  368. .sr_cs_n ( sr_cs_n ),
  369. .sr_ras_n ( sr_ras_n ),
  370. .sr_cas_n ( sr_cas_n ),
  371. .sr_we_n ( sr_we_n ),
  372. .sr_dqm ( sr_dqm ),
  373. .sr_ba ( sr_ba ),
  374. .sr_a ( sr_a ),
  375. .sr_dq ( sr_dq ),
  376. .a0 ( { abc_mempg, abc_a } ),
  377. .rd0 ( abc_sr_rd ),
  378. .rrq0 ( abc_rrq ),
  379. .rack0 ( abc_rack ),
  380. .rready0 ( abc_rready ),
  381. .wd0 ( abc_d ),
  382. .wrq0 ( abc_wrq ),
  383. .wack0 ( abc_wack ),
  384. .a1 ( cpu_mem_addr[24:2] ),
  385. .rd1 ( sdram_rd ),
  386. .rrq1 ( sdram_req & ~|cpu_mem_wstrb ),
  387. .rack1 ( sdram_rack ),
  388. .rready1 ( sdram_rready ),
  389. .wd1 ( cpu_mem_wdata ),
  390. .wstrb1 ( {4{sdram_req}} & cpu_mem_wstrb ),
  391. .wack1 ( sdram_wack ),
  392. .a2 ( sdram_rom_waddr ),
  393. .wd2 ( sdram_rom_wd ),
  394. .wrq2 ( sdram_rom_wrq ),
  395. .wacc2 ( sdram_rom_wacc )
  396. );
  397. // SPI bus (free for ESP32)
  398. assign spi_clk = 1'bz;
  399. assign spi_miso = 1'bz;
  400. assign spi_mosi = 1'bz;
  401. assign spi_cs_esp_n = 1'bz;
  402. assign spi_cs_flash_n = 1'bz;
  403. // ESP32
  404. assign esp_io0 = 1'bz;
  405. assign esp_int = 1'bz;
  406. // I2C
  407. assign i2c_scl = 1'bz;
  408. assign i2c_sda = 1'bz;
  409. // GPIO
  410. assign gpio = 6'bzzzzzz;
  411. // Embedded RISC-V CPU
  412. parameter cpu_fast_mem_bits = 13; /* 2^[this] * 4 bytes */
  413. // Edge-triggered system IRQs not necessarily associated
  414. // with a specific I/O device. picorv32 latches interrupts
  415. // but doesn't edge detect for a slow signal, so do it
  416. // here instead and use level triggered signalling to the
  417. // CPU.
  418. wire [31:0] cpu_eoi;
  419. reg [31:0] cpu_eoi_q;
  420. tri0 [15:3] sys_irq;
  421. reg [15:3] sys_irq_q;
  422. reg [15:3] sys_irq_pending;
  423. always @(negedge rst_n or posedge sys_clk)
  424. if (~rst_n)
  425. begin
  426. sys_irq_q <= 1'b0;
  427. cpu_eoi_q <= 1'b0;
  428. sys_irq_pending <= 13'b0;
  429. end
  430. else
  431. begin
  432. sys_irq_q <= sys_irq;
  433. cpu_eoi_q <= cpu_eoi;
  434. sys_irq_pending <= (sys_irq & ~sys_irq_q)
  435. | (sys_irq_pending & ~(cpu_eoi[15:3] & ~cpu_eoi_q[15:3]));
  436. end
  437. picorv32 #(
  438. .ENABLE_COUNTERS ( 1 ),
  439. .ENABLE_COUNTERS64 ( 1 ),
  440. .ENABLE_REGS_16_31 ( 1 ),
  441. .ENABLE_REGS_DUALPORT ( 1 ),
  442. .LATCHED_MEM_RDATA ( 1 ),
  443. .BARREL_SHIFTER ( 1 ),
  444. .TWO_CYCLE_COMPARE ( 0 ),
  445. .TWO_CYCLE_ALU ( 0 ),
  446. .COMPRESSED_ISA ( 1 ),
  447. .CATCH_MISALIGN ( 1 ),
  448. .CATCH_ILLINSN ( 1 ),
  449. .ENABLE_FAST_MUL ( 1 ),
  450. .ENABLE_DIV ( 1 ),
  451. .ENABLE_IRQ ( 1 ),
  452. .ENABLE_IRQ_QREGS ( 1 ),
  453. .ENABLE_IRQ_TIMER ( 1 ),
  454. .LATCHED_IRQ ( 32'h0000_0007 ),
  455. .REGS_INIT_ZERO ( 1 ),
  456. .STACKADDR ( 32'h4 << cpu_fast_mem_bits )
  457. )
  458. cpu (
  459. .clk ( sys_clk ),
  460. .resetn ( rst_n ),
  461. .trap ( ),
  462. .progaddr_reset ( 32'h0000_0000 ),
  463. .progaddr_irq ( 32'h0000_0020 ),
  464. .mem_instr ( cpu_mem_instr ),
  465. .mem_ready ( cpu_mem_ready ),
  466. .mem_valid ( cpu_mem_valid ),
  467. .mem_wstrb ( cpu_mem_wstrb ),
  468. .mem_addr ( cpu_mem_addr ),
  469. .mem_wdata ( cpu_mem_wdata ),
  470. .mem_rdata ( cpu_mem_rdata ),
  471. .mem_la_read ( cpu_la_read ),
  472. .mem_la_write ( cpu_la_write ),
  473. .mem_la_wdata ( cpu_la_wdata ),
  474. .mem_la_addr ( cpu_la_addr ),
  475. .mem_la_wstrb ( cpu_la_wstrb ),
  476. .irq ( { iodev_irq, sys_irq_pending, 3'b000 } ),
  477. .eoi ( cpu_eoi )
  478. );
  479. // cpu_mem_ready is always true for fast memory; for SDRAM we have to
  480. // wait either for a write ack or a low-high transition on the
  481. // read ready signal.
  482. reg sdram_rready_q;
  483. reg sdram_mem_ready;
  484. reg [31:0] sdram_rdata;
  485. always @(posedge sys_clk)
  486. begin
  487. sdram_rready_q <= sdram_rready;
  488. if (cpu_mem_quad[1])
  489. sdram_mem_ready <= sdram_mem_ready | sdram_wack |
  490. (sdram_rready & ~sdram_rready_q);
  491. else
  492. sdram_mem_ready <= 1'b0;
  493. sdram_rdata <= sdram_rd;
  494. end
  495. // Add a mandatory wait state to iodevs to reduce the size
  496. // of the CPU memory input MUX (it hurts timing on memory
  497. // accesses...)
  498. tri1 [15:0] iodev_wait_n;
  499. reg iodev_mem_ready;
  500. always @(*)
  501. case ( cpu_mem_quad )
  502. 4'b0000: cpu_mem_ready = 1'b0;
  503. 4'b0001: cpu_mem_ready = 1'b1;
  504. 4'b0010: cpu_mem_ready = sdram_mem_ready;
  505. 4'b0100: cpu_mem_ready = 1'b1;
  506. 4'b1000: cpu_mem_ready = iodev_mem_ready;
  507. default: cpu_mem_ready = 1'bx;
  508. endcase // case ( mem_quad )
  509. //
  510. // Fast memory. This runs on the SDRAM clock, i.e. 2x the speed
  511. // of the CPU. The .bits parameter gives the number of dwords
  512. // as a power of 2, i.e. 11 = 2^11 * 4 = 8K.
  513. //
  514. wire [31:0] fast_mem_rdata;
  515. fast_mem // #(.bits(cpu_fast_mem_bits), .mif("../fw/boot"))
  516. fast_mem(
  517. .rst_n ( rst_n ),
  518. .clk ( sys_clk ),
  519. .read ( cpu_la_read & cpu_la_addr[31:30] == 2'b00 ),
  520. .write ( cpu_la_write & cpu_la_addr[31:30] == 2'b00 ),
  521. .wstrb ( cpu_la_wstrb ),
  522. .addr ( cpu_la_addr[14:2] ),
  523. .wdata ( cpu_la_wdata ),
  524. .rdata ( fast_mem_rdata )
  525. );
  526. // Input data MUX
  527. wire [31:0] iodev_rdata;
  528. always @(*)
  529. case ( cpu_mem_quad )
  530. 4'b0001: cpu_mem_rdata = fast_mem_rdata;
  531. 4'b0010: cpu_mem_rdata = sdram_rdata;
  532. 4'b1000: cpu_mem_rdata = iodev_rdata;
  533. default: cpu_mem_rdata = 32'hxxxx_xxxx;
  534. endcase
  535. // Hard system reset under program control
  536. assign reset_cmd = rst_n & iodev[1] & cpu_mem_wstrb[0] & cpu_mem_wdata[0];
  537. // LED indication from the CPU
  538. reg [2:0] led_q;
  539. always @(negedge rst_n or posedge sys_clk)
  540. if (~rst_n)
  541. led_q <= 3'b000;
  542. else
  543. if ( iodev[0] & cpu_mem_wstrb[0] )
  544. led_q <= cpu_mem_wdata[2:0];
  545. assign led = led_q;
  546. //
  547. // Serial ROM (also configuration ROM.) Fast hardwired data download
  548. // unit to SDRAM.
  549. //
  550. wire rom_done;
  551. reg rom_done_q;
  552. spirom ddu (
  553. .rst_n ( rst_n ),
  554. .rom_clk ( flash_clk ),
  555. .ram_clk ( sdram_clk ),
  556. .spi_sck ( flash_sck ),
  557. .spi_io ( flash_io ),
  558. .spi_cs_n ( flash_cs_n ),
  559. .wd ( sdram_rom_wd ),
  560. .waddr ( sdram_rom_waddr ),
  561. .wrq ( sdram_rom_wrq ),
  562. .wacc ( sdram_rom_wacc ),
  563. .done ( rom_done )
  564. );
  565. always @(posedge sys_clk)
  566. rom_done_q <= rom_done;
  567. //
  568. // Serial port. Direct to the CP2102N for reworked
  569. // boards or to GPIO for non-reworked boards, depending on
  570. // whether DTR# is asserted on either.
  571. //
  572. // The GPIO numbering matches the order of pins for FT[2]232H.
  573. // gpio[0] - TxD
  574. // gpio[1] - RxD
  575. // gpio[2] - RTS#
  576. // gpio[3] - CTS#
  577. // gpio[4] - DTR#
  578. //
  579. wire tty_data_out; // Output data
  580. wire tty_data_in; // Input data
  581. wire tty_cts_out; // Assert CTS# externally
  582. wire tty_rts_in; // RTS# received from outside
  583. wire [31:0] tty_rdata;
  584. assign tty_cts_out = 1'b0; // Assert CTS#
  585. tty tty (
  586. .rst_n ( rst_n ),
  587. .clk ( sys_clk ),
  588. .valid ( iodev[3] ),
  589. .wstrb ( cpu_mem_wstrb ),
  590. .wdata ( cpu_mem_wdata ),
  591. .rdata ( tty_rdata ),
  592. .addr ( cpu_mem_addr[3:2] ),
  593. .irq ( iodev_irq[3] ),
  594. .tty_txd ( tty_data_out ) // DTE -> DCE
  595. );
  596. reg [1:0] tty_dtr_q;
  597. always @(posedge sys_clk)
  598. begin
  599. tty_dtr_q[0] <= tty_dtr;
  600. tty_dtr_q[1] <= gpio[4];
  601. end
  602. //
  603. // Route data to the two output ports
  604. //
  605. // tty_rxd because pins are DCE named
  606. assign tty_data_in = (tty_txd | tty_dtr_q[0]) &
  607. (gpio[0] | tty_dtr_q[1]);
  608. assign tty_rxd = tty_dtr_q[0] ? 1'bz : tty_data_out;
  609. assign gpio[1] = tty_dtr_q[1] ? 1'bz : tty_data_out;
  610. assign tty_rts_in = (tty_rts | tty_dtr_q[0]) &
  611. (gpio[2] | tty_dtr_q[1]);
  612. assign tty_cts = tty_dtr_q[0] ? 1'bz : tty_cts_out;
  613. assign gpio[3] = tty_dtr_q[1] ? 1'bz : tty_cts_out;
  614. // SD card
  615. wire [31:0] sdcard_rdata;
  616. sdcard sdcard (
  617. .rst_n ( rst_n ),
  618. .clk ( sys_clk ),
  619. .sd_cs_n ( sd_dat[3] ),
  620. .sd_di ( sd_cmd ),
  621. .sd_sclk ( sd_clk ),
  622. .sd_do ( sd_dat[0] ),
  623. .sd_cd_n ( 1'b0 ),
  624. .wdata ( cpu_mem_wdata ),
  625. .rdata ( sdcard_rdata ),
  626. .valid ( iodev[4] ),
  627. .wstrb ( cpu_mem_wstrb ),
  628. .addr ( cpu_mem_addr[6:2] ),
  629. .wait_n ( iodev_wait_n[4] )
  630. );
  631. assign sd_dat[2:1] = 2'bzz;
  632. // System local clock (not an RTC, but settable from one)
  633. // Also provides a periodic interrupt (set to 32 Hz)
  634. wire [31:0] sysclock_rdata;
  635. // XXX: the RTC 32 kHz signal is missing a pull-up,
  636. // so it will require board rework. For now, use an
  637. // divider down from the 84 MHz system clock. The
  638. // error is about 200 ppm; a proper NCO could do better.
  639. reg [10:0] ctr_64khz;
  640. reg ctr_32khz;
  641. always @(posedge sys_clk)
  642. begin
  643. if (~|ctr_64khz)
  644. begin
  645. ctr_32khz <= ~ctr_32khz;
  646. ctr_64khz <= 11'd1280;
  647. end
  648. else
  649. ctr_64khz <= ctr_64khz - 1'b1;
  650. end
  651. sysclock #(.PERIODIC_HZ_LG2 ( 5 ))
  652. sysclock (
  653. .rst_n ( rst_n ),
  654. .sys_clk ( sys_clk ),
  655. .rtc_clk ( ctr_32khz ),
  656. .wdata ( cpu_mem_wdata ),
  657. .rdata ( sysclock_rdata ),
  658. .valid ( iodev[5] ),
  659. .wstrb ( cpu_mem_wstrb ),
  660. .addr ( cpu_mem_addr[2] ),
  661. .periodic ( sys_irq[3] )
  662. );
  663. //
  664. // I/O device input data (registered to reduce MUX overhead for
  665. // the critical memory data paths.)
  666. // abo
  667. //
  668. always @(posedge sys_clk)
  669. case ( cpu_mem_addr[10:7] )
  670. 4'd0: iodev_rdata <= { 29'b0, led_q };
  671. 4'd2: iodev_rdata <= { 31'b0, rom_done_q };
  672. 4'd3: iodev_rdata <= tty_rdata;
  673. 4'd4: iodev_rdata <= sdcard_rdata;
  674. 4'd5: iodev_rdata <= sysclock_rdata;
  675. default: iodev_rdata <= 32'h0;
  676. endcase
  677. always @(negedge rst_n or posedge sys_clk)
  678. if (~rst_n)
  679. iodev_mem_ready <= 1'b0;
  680. else
  681. iodev_mem_ready <= &iodev_wait_n & cpu_mem_valid;
  682. endmodule