max80.sv 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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 target on the ABC-bus.
  6. //
  7. // Sharing JTAG pins (via JTAGEN)
  8. `undef SHARED_JTAG
  9. module max80
  10. #(parameter logic [6:1] x_mosfet,
  11. parameter logic [7:0] fpga_ver)
  12. (
  13. // Clock oscillator
  14. input master_clk, // 336 MHz from PLL2
  15. input slow_clk, // ~12 MHz clock from PLL2
  16. input master_pll_locked, // PLL2 is locked, master_clk is good
  17. output reset_plls, // Reset all PLLs including PLL2
  18. input board_id, // This better match the firmware
  19. // ABC-bus
  20. inout abc_clk, // ABC-bus 3 MHz clock
  21. inout [15:0] abc_a, // ABC address bus
  22. inout [7:0] abc_d, // ABC data bus
  23. output abc_d_oe, // Data bus output enable
  24. inout abc_rst_n, // ABC bus reset strobe
  25. inout abc_cs_n, // ABC card select strobe
  26. inout [4:0] abc_out_n, // OUT, C1-C4 strobe
  27. inout [1:0] abc_inp_n, // INP, STATUS strobe
  28. inout abc_xmemfl_n, // Memory read strobe
  29. inout abc_xmemw800_n, // Memory write strobe (ABC800)
  30. inout abc_xmemw80_n, // Memory write strobe (ABC80)
  31. inout abc_xinpstb_n, // I/O read strobe (ABC800)
  32. inout abc_xoutpstb_n, // I/O write strobe (ABC80)
  33. // The following are inverted versus the bus IF
  34. // the corresponding MOSFETs are installed
  35. inout abc_rdy_x, // RDY = WAIT#
  36. inout abc_resin_x, // System reset request
  37. inout abc_int80_x, // System INT request (ABC80)
  38. inout abc_int800_x, // System INT request (ABC800)
  39. inout abc_nmi_x, // System NMI request (ABC800)
  40. inout abc_xm_x, // System memory override (ABC800)
  41. // Host/device control
  42. output abc_host, // 1 = host, 0 = target
  43. // ABC-bus extension header
  44. // (Note: cannot use an array here because HC and HH are
  45. // input only.)
  46. inout exth_ha,
  47. inout exth_hb,
  48. input exth_hc,
  49. inout exth_hd,
  50. inout exth_he,
  51. inout exth_hf,
  52. inout exth_hg,
  53. input exth_hh,
  54. // SDRAM bus
  55. output sr_clk,
  56. output [1:0] sr_ba, // Bank address
  57. output [12:0] sr_a, // Address within bank
  58. inout [15:0] sr_dq, // Also known as D or IO
  59. output [1:0] sr_dqm, // DQML and DQMH
  60. output sr_cs_n,
  61. output sr_we_n,
  62. output sr_cas_n,
  63. output sr_ras_n,
  64. // SD card
  65. input sd_cd_n,
  66. output sd_cs_n,
  67. output sd_clk,
  68. output sd_di,
  69. input sd_do,
  70. // Serial console (naming is FPGA as DCE)
  71. input tty_txd,
  72. output tty_rxd,
  73. input tty_rts,
  74. output tty_cts,
  75. input tty_dtr,
  76. // SPI flash memory (also configuration)
  77. output flash_cs_n,
  78. output flash_sck,
  79. inout [1:0] flash_io,
  80. // SPI bus (connected to ESP32 so can be bidirectional)
  81. inout spi_clk, // ESP32 IO12
  82. inout [1:0] spi_io, // ESP32 IO13,IO11
  83. inout spi_cs_esp_n, // ESP32 IO10
  84. inout spi_cs_flash_n, // ESP32 IO01
  85. // Other ESP32 connections
  86. inout esp_io0, // ESP32 IO00
  87. inout esp_int, // ESP32 IO09
  88. // I2C bus (RTC and external)
  89. inout i2c_scl,
  90. inout i2c_sda,
  91. input rtc_32khz,
  92. input rtc_int_n,
  93. // LEDs
  94. output [2:0] led,
  95. // USB
  96. inout usb_dp,
  97. inout usb_dn,
  98. output usb_pu,
  99. input usb_rx,
  100. input usb_rx_ok,
  101. // HDMI
  102. output [2:0] hdmi_d,
  103. output hdmi_clk,
  104. inout hdmi_scl,
  105. inout hdmi_sda,
  106. inout hdmi_hpd,
  107. // Unconnected pins with pullups, used for randomness
  108. inout [2:0] rngio,
  109. // Various clocks available to the top level as well as internally
  110. output sdram_clk, // 168 MHz SDRAM clock
  111. output sys_clk, // 84 MHz System clock
  112. output flash_clk, // 134 MHz Serial flash ROM clock
  113. output usb_clk, // 48 MHz USB clock
  114. output vid_clk, // 56 MHz Video pixel clock
  115. output vid_hdmiclk // 280 MHz HDMI serializer clock = vid_clk x 5
  116. );
  117. // -----------------------------------------------------------------------
  118. // PLLs and reset
  119. // -----------------------------------------------------------------------
  120. reg rst_n = 1'b0; // Internal system reset
  121. reg hard_rst_n = 1'b0; // Strict POR reset only
  122. wire reconfig_rst; // Reconfigure FPGA
  123. assign reset_plls = 1'b0;
  124. tri1 [4:1] pll_locked;
  125. assign pll_locked[2] = master_pll_locked;
  126. fpgarst fpgarst (
  127. .rst_n ( master_pll_locked ),
  128. .clk ( slow_clk ),
  129. .reconfig ( reconfig_rst )
  130. );
  131. //
  132. // Clocks.
  133. //
  134. // All clocks are derived from a common oscillator connected to an
  135. // input clock pin, which is a dedicated clock pin feeding into
  136. // hardware PLL2 and PLL4. The SDRAM clock output is a dedicated
  137. // clock out pin from PLL3.
  138. //
  139. // The input frequency is not consistent across board revisions,
  140. // so PLL2 is configured to produce a common master clock (336 MHz)
  141. // in the appropriate top level file.
  142. //
  143. // The following sets of clocks are closely tied and expected to
  144. // be synchronous, and therefore should come from the same PLL each;
  145. // furthermore, the design strictly assumes the ratios specified.
  146. //
  147. // sdram_clk, sys_clk - 2:1 ratio
  148. // vid_hdmiclk, vid_clk - 5:1 ratio
  149. //
  150. pll3 pll3 (
  151. .areset ( ~pll_locked[2] ),
  152. .locked ( pll_locked[3] ),
  153. .inclk0 ( master_clk ),
  154. .c0 ( sr_clk ), // Output to clock pin (phase shift)
  155. .c1 ( sdram_clk ), // Internal logic/buffer data clock
  156. .c2 ( sys_clk ),
  157. .c3 ( flash_clk ),
  158. .c4 ( usb_clk )
  159. );
  160. pll4 pll4 (
  161. .areset ( ~pll_locked[2] ),
  162. .locked ( pll_locked[4] ),
  163. .inclk0 ( master_clk ),
  164. .c0 ( vid_hdmiclk ),
  165. .c1 ( vid_clk )
  166. );
  167. wire all_plls_locked = &pll_locked;
  168. //
  169. // sys_clk pulse generation of various powers of two; allows us to
  170. // reuse the same counter for a lot of things that require periodic
  171. // timing events without strong requirements on the specific timing.
  172. // The first strobe is asserted 2^n cycles after rst_n goes high.
  173. //
  174. // The same counter is used to hold rst_n and hard_rst_n low for
  175. // 2^reset_pow2 cycles.
  176. //
  177. // XXX: reuse this counter for the CPU cycle counter.
  178. //
  179. localparam reset_pow2 = 12;
  180. reg [31:0] sys_clk_ctr;
  181. reg [31:0] sys_clk_ctr_q;
  182. reg [31:1] sys_clk_stb;
  183. // 3 types of reset: system, hard, and reconfig
  184. wire [3:1] cpu_reset_cmd; // CPU-originated reset command
  185. reg [3:1] cpu_reset_cmd_q[0:1];
  186. wire [3:1] aux_reset_cmd; // Other reset sources
  187. reg [3:1] reset_cmd_q = 3'b0;
  188. assign reconfig_rst = reset_cmd_q[3];
  189. always @(negedge all_plls_locked or posedge sys_clk)
  190. if (~all_plls_locked)
  191. begin
  192. hard_rst_n <= 1'b0;
  193. rst_n <= 1'b0;
  194. reset_cmd_q[2:1] <= 3'b0;
  195. cpu_reset_cmd_q[0] <= 3'b0;
  196. cpu_reset_cmd_q[1] <= 3'b0;
  197. sys_clk_ctr <= (-'sb1) << reset_pow2;
  198. sys_clk_ctr_q <= 'b0;
  199. sys_clk_stb <= 'b0;
  200. end
  201. else
  202. begin
  203. cpu_reset_cmd_q[0] <= cpu_reset_cmd;
  204. cpu_reset_cmd_q[1] <= cpu_reset_cmd_q[0];
  205. reset_cmd_q <= (cpu_reset_cmd_q[0] & ~cpu_reset_cmd_q[1]) |
  206. aux_reset_cmd;
  207. // Reconfig reset is sticky until FPGA reloaded...
  208. reset_cmd_q[3] <= reset_cmd_q[3] | cpu_reset_cmd_q[0][3] |
  209. aux_reset_cmd[3];
  210. if (|reset_cmd_q)
  211. begin
  212. // Soft or hard reset
  213. sys_clk_ctr <= (-'sb1) << reset_pow2;
  214. sys_clk_ctr_q <= 1'b0;
  215. sys_clk_stb <= 1'b0;
  216. rst_n <= 1'b0;
  217. hard_rst_n <= hard_rst_n & ~|reset_cmd_q[3:2];
  218. end
  219. else
  220. begin
  221. sys_clk_ctr <= sys_clk_ctr + 1'b1;
  222. sys_clk_ctr_q <= ~rst_n ? 'b0 : sys_clk_ctr;
  223. sys_clk_stb <= ~sys_clk_ctr & sys_clk_ctr_q;
  224. rst_n <= rst_n | ~sys_clk_ctr[reset_pow2];
  225. hard_rst_n <= hard_rst_n | ~sys_clk_ctr[reset_pow2];
  226. end
  227. end
  228. // Reset in the video clock domain
  229. reg vid_rst_n;
  230. always @(negedge all_plls_locked or posedge vid_clk)
  231. if (~all_plls_locked)
  232. vid_rst_n <= 1'b0;
  233. else
  234. vid_rst_n <= rst_n;
  235. // HDMI video interface
  236. video video (
  237. .rst_n ( vid_rst_n ),
  238. .vid_clk ( vid_clk ),
  239. .vid_hdmiclk ( vid_hdmiclk ),
  240. .hdmi_d ( hdmi_d ),
  241. .hdmi_clk ( hdmi_clk ),
  242. .hdmi_scl ( hdmi_scl ),
  243. .hdmi_hpd ( hdmi_hpd )
  244. );
  245. //
  246. // Internal CPU bus
  247. //
  248. wire cpu_mem_valid;
  249. wire cpu_mem_ready;
  250. wire cpu_mem_instr;
  251. wire [ 3:0] cpu_mem_wstrb;
  252. wire [31:0] cpu_mem_addr;
  253. wire [31:0] cpu_mem_wdata;
  254. reg [31:0] cpu_mem_rdata;
  255. wire cpu_la_read;
  256. wire cpu_la_write;
  257. wire [31:0] cpu_la_addr;
  258. wire [31:0] cpu_la_wdata;
  259. wire [ 3:0] cpu_la_wstrb;
  260. // cpu_mem_valid by address quadrant, using a bit of lookahead
  261. // decoding for speed.
  262. reg [3:0] mem_quad;
  263. always @(negedge rst_n or posedge sys_clk)
  264. if (~rst_n)
  265. mem_quad <= 4'b0;
  266. else if (cpu_mem_valid)
  267. mem_quad <= 1'b1 << cpu_mem_addr[31:30];
  268. else
  269. mem_quad <= 1'b1 << cpu_la_addr[31:30];
  270. wire [3:0] cpu_mem_quad = cpu_mem_valid ? mem_quad : 4'b0;
  271. // I/O device map from iodevs.conf
  272. wire iodev_mem_valid = cpu_mem_quad[3];
  273. `include "iodevs.vh"
  274. //
  275. // SDRAM
  276. //
  277. localparam dram_port_count = 4;
  278. dram_bus sr_bus[1:dram_port_count] ( );
  279. // ABC interface
  280. wire [24:0] abc_sr_addr;
  281. wire [ 7:0] abc_sr_rd;
  282. wire abc_sr_valid;
  283. wire abc_sr_ready;
  284. wire [ 7:0] abc_sr_wd;
  285. wire abc_sr_wstrb;
  286. dram_port #(8)
  287. abc_dram_port (
  288. .bus ( sr_bus[1] ),
  289. .prio ( 2'd3 ),
  290. .addr ( abc_sr_addr ),
  291. .rd ( abc_sr_rd ),
  292. .valid ( abc_sr_valid ),
  293. .ready ( abc_sr_ready ),
  294. .wd ( abc_sr_wd ),
  295. .wstrb ( abc_sr_wstrb )
  296. );
  297. // CPU interface
  298. wire sdram_valid = cpu_mem_quad[1];
  299. wire [31:0] sdram_mem_rdata;
  300. wire sdram_ready;
  301. reg sdram_ready_q;
  302. reg sdram_mem_ready;
  303. //
  304. // Retard sdram_ready by one sys_clk (multicycle path for the data,
  305. // see max80.sdc)
  306. //
  307. // Note that if the CPU leaves valid asserted the CPU cycle after
  308. // receiving ready, it is the beginning of another request. The
  309. // sdram core expects valid to be strobed, so deassert valid
  310. // to the sdram core while asserting ready to the CPU.
  311. //
  312. always @(posedge sys_clk)
  313. begin
  314. sdram_mem_ready <= sdram_ready & sdram_valid;
  315. end
  316. dram_port #(32)
  317. cpu_dram_port (
  318. .bus ( sr_bus[4] ),
  319. .prio ( 2'd1 ),
  320. .addr ( cpu_mem_addr[24:0] ),
  321. .rd ( sdram_mem_rdata ),
  322. .valid ( sdram_valid & ~sdram_mem_ready ),
  323. .ready ( sdram_ready ),
  324. .wd ( cpu_mem_wdata ),
  325. .wstrb ( cpu_mem_wstrb )
  326. );
  327. // Romcopy interface
  328. wire [15:0] sdram_rom_wd;
  329. wire [24:1] sdram_rom_waddr;
  330. wire [ 1:0] sdram_rom_wrq;
  331. wire sdram_rom_wacc;
  332. sdram #(.port1_count(dram_port_count))
  333. sdram (
  334. .rst_n ( rst_n ),
  335. .clk ( sdram_clk ), // Internal memory clock
  336. .init_tmr ( sys_clk_stb[14] ), // > 100 μs (tP) after reset
  337. .rfsh_tmr ( sys_clk_stb[8] ), // < 3.9 μs (tREFI/2)
  338. .sr_cs_n ( sr_cs_n ),
  339. .sr_ras_n ( sr_ras_n ),
  340. .sr_cas_n ( sr_cas_n ),
  341. .sr_we_n ( sr_we_n ),
  342. .sr_dqm ( sr_dqm ),
  343. .sr_ba ( sr_ba ),
  344. .sr_a ( sr_a ),
  345. .sr_dq ( sr_dq ),
  346. .port1 ( sr_bus ),
  347. .a2 ( sdram_rom_waddr ),
  348. .wd2 ( sdram_rom_wd ),
  349. .wrq2 ( sdram_rom_wrq ),
  350. .wacc2 ( sdram_rom_wacc )
  351. );
  352. //
  353. // ABC-bus interface
  354. //
  355. wire abc_clk_s; // abc_clk synchronous to sys_clk
  356. abcbus #(.mosfet_installed(x_mosfet))
  357. abcbus (
  358. .rst_n ( rst_n ),
  359. .sys_clk ( sys_clk ),
  360. .sdram_clk ( sdram_clk ),
  361. .stb_1mhz ( sys_clk_stb[6] ),
  362. .abc_valid ( iodev_valid_abc ),
  363. .map_valid ( iodev_valid_abcmemmap ),
  364. .cpu_addr ( cpu_mem_addr ),
  365. .cpu_wdata ( cpu_mem_wdata ),
  366. .cpu_wstrb ( cpu_mem_wstrb ),
  367. .cpu_rdata ( iodev_rdata_abc ),
  368. .cpu_rdata_map ( iodev_rdata_abcmemmap ),
  369. .irq ( iodev_irq_abc ),
  370. .abc_clk ( abc_clk ),
  371. .abc_clk_s ( abc_clk_s ),
  372. .abc_a ( abc_a ),
  373. .abc_d ( abc_d ),
  374. .abc_d_oe ( abc_d_oe ),
  375. .abc_rst_n ( abc_rst_n ),
  376. .abc_cs_n ( abc_cs_n ),
  377. .abc_out_n ( abc_out_n ),
  378. .abc_inp_n ( abc_inp_n ),
  379. .abc_xmemfl_n ( abc_xmemfl_n ),
  380. .abc_xmemw800_n ( abc_xmemw800_n ),
  381. .abc_xmemw80_n ( abc_xmemw80_n ),
  382. .abc_xinpstb_n ( abc_xinpstb_n ),
  383. .abc_xoutpstb_n ( abc_xoutpstb_n ),
  384. .abc_rdy_x ( abc_rdy_x ),
  385. .abc_resin_x ( abc_resin_x ),
  386. .abc_int80_x ( abc_int80_x ),
  387. .abc_int800_x ( abc_int800_x ),
  388. .abc_nmi_x ( abc_nmi_x ),
  389. .abc_xm_x ( abc_xm_x ),
  390. .abc_host ( abc_host ),
  391. .exth_ha ( exth_ha ),
  392. .exth_hb ( exth_hb ),
  393. .exth_hc ( exth_hc ),
  394. .exth_hd ( exth_hd ),
  395. .exth_he ( exth_he ),
  396. .exth_hf ( exth_hf ),
  397. .exth_hg ( exth_hg ),
  398. .exth_hh ( exth_hh ),
  399. .sdram_addr ( abc_sr_addr ),
  400. .sdram_rd ( abc_sr_rd ),
  401. .sdram_valid ( abc_sr_valid ),
  402. .sdram_ready ( abc_sr_ready ),
  403. .sdram_wd ( abc_sr_wd ),
  404. .sdram_wstrb ( abc_sr_wstrb )
  405. );
  406. // Embedded RISC-V CPU
  407. // Edge-triggered IRQs. picorv32 latches interrupts
  408. // but doesn't edge detect for a slow signal, so do it
  409. // here instead and use level triggered signalling to the
  410. // CPU. This also allows using an explicit EOI instead of
  411. // using EOI-on-INTACK.
  412. //
  413. // sys_irq defined in iodevs.vh
  414. reg [31:0] sys_irq_q;
  415. reg [31:0] cpu_irq;
  416. wire [31:0] cpu_eoi = {32{sysreg[4]}}
  417. & {{8{cpu_mem_wstrb[3]}}, {8{cpu_mem_wstrb[2]}},
  418. {8{cpu_mem_wstrb[1]}}, {8{cpu_mem_wstrb[0]}}}
  419. & cpu_mem_wdata;
  420. // Reading the register shows the current set of pending interrupts.
  421. assign sysreg_rdata[4] = cpu_irq;
  422. // CPU permanently hung?
  423. wire cpu_trap;
  424. // Request to halt the CPU on the next instruction boundary
  425. wire cpu_halt;
  426. always @(negedge rst_n or posedge sys_clk)
  427. if (~rst_n)
  428. begin
  429. sys_irq_q <= 32'b0;
  430. cpu_irq <= 32'b0;
  431. end
  432. else
  433. begin
  434. sys_irq_q <= sys_irq & irq_edge_mask;
  435. cpu_irq <= (sys_irq & ~sys_irq_q) |
  436. (cpu_irq & irq_edge_mask & ~cpu_eoi);
  437. end
  438. picorv32 #(
  439. .ENABLE_COUNTERS ( 1 ),
  440. .ENABLE_COUNTERS64 ( 1 ),
  441. .ENABLE_REGS_16_31 ( 1 ),
  442. .ENABLE_REGS_DUALPORT ( 1 ),
  443. .LATCHED_MEM_RDATA ( 0 ),
  444. .BARREL_SHIFTER ( 1 ),
  445. .TWO_CYCLE_COMPARE ( 0 ),
  446. .TWO_CYCLE_ALU ( 0 ),
  447. .COMPRESSED_ISA ( 1 ),
  448. .CATCH_MISALIGN ( 1 ),
  449. .CATCH_ILLINSN ( 1 ),
  450. .ENABLE_FAST_MUL ( 1 ),
  451. .ENABLE_DIV ( 1 ),
  452. .ENABLE_IRQ ( 1 ),
  453. .ENABLE_IRQ_QREGS ( 1 ),
  454. .ENABLE_IRQ_TIMER ( 1 ),
  455. .MASKED_IRQ ( irq_masked ),
  456. .LATCHED_IRQ ( 32'h0000_0007 ),
  457. .REGS_INIT_ZERO ( 1 ),
  458. .STACKADDR ( 1'b1 << SRAM_BITS ),
  459. .USER_CONTEXTS ( 7 )
  460. ) cpu (
  461. .clk ( sys_clk ),
  462. .resetn ( rst_n ),
  463. .halt ( cpu_halt ),
  464. .trap ( cpu_trap ),
  465. .progaddr_reset ( _PC_RESET ),
  466. .progaddr_irq ( _PC_IRQ ),
  467. .mem_instr ( cpu_mem_instr ),
  468. .mem_ready ( cpu_mem_ready ),
  469. .mem_valid ( cpu_mem_valid ),
  470. .mem_wstrb ( cpu_mem_wstrb ),
  471. .mem_addr ( cpu_mem_addr ),
  472. .mem_wdata ( cpu_mem_wdata ),
  473. .mem_rdata ( cpu_mem_rdata ),
  474. .mem_la_read ( cpu_la_read ),
  475. .mem_la_write ( cpu_la_write ),
  476. .mem_la_wdata ( cpu_la_wdata ),
  477. .mem_la_addr ( cpu_la_addr ),
  478. .mem_la_wstrb ( cpu_la_wstrb ),
  479. .irq ( cpu_irq ),
  480. .eoi ( )
  481. );
  482. // Add a mandatory wait state to iodevs to reduce the size
  483. // of the CPU memory input MUX (it hurts timing on memory
  484. // accesses...)
  485. reg iodev_mem_ready;
  486. assign cpu_mem_ready =
  487. (cpu_mem_quad[0] & 1'b1) |
  488. (cpu_mem_quad[1] & sdram_mem_ready) |
  489. (cpu_mem_quad[2] & 1'b1) |
  490. (cpu_mem_quad[3] & iodev_mem_ready);
  491. //
  492. // Fast memory. This runs on the SDRAM clock, i.e. 2x the speed
  493. // of the CPU. The .bits parameter gives the number of dwords
  494. // as a power of 2, i.e. 11 = 2^11 * 4 = 8K.
  495. //
  496. wire [31:0] fast_mem_rdata;
  497. wire [SRAM_BITS-1:2] vjtag_sram_addr;
  498. wire vjtag_sram_read;
  499. wire vjtag_sram_write;
  500. wire [31:0] vjtag_sram_rdata;
  501. wire [31:0] vjtag_sram_wdata;
  502. fast_mem #(.words_lg2(SRAM_BITS-2),
  503. .data_file("mif/sram.mif"))
  504. fast_mem(
  505. .rst_n ( rst_n ),
  506. .clk ( sys_clk ),
  507. .read0 ( 1'b1 ), // cpu_la_read & cpu_la_addr[31:30] == 2'b00
  508. .write0 ( cpu_la_write & cpu_la_addr[31:30] == 2'b00 ),
  509. .wstrb0 ( cpu_la_wstrb ),
  510. .addr0 ( cpu_la_addr[SRAM_BITS-1:2] ),
  511. .wdata0 ( cpu_la_wdata ),
  512. .rdata0 ( fast_mem_rdata ),
  513. .read1 ( 1'b1 ), // vjtag_sram_read
  514. .write1 ( vjtag_sram_write ),
  515. .wstrb1 ( 4'b1111 ),
  516. .addr1 ( vjtag_sram_addr ),
  517. .wdata1 ( vjtag_sram_wdata ),
  518. .rdata1 ( vjtag_sram_rdata )
  519. );
  520. // Register I/O data to reduce the size of the read data MUX
  521. reg [31:0] iodev_rdata_q;
  522. // Read data MUX
  523. always_comb
  524. case ( cpu_mem_quad )
  525. 4'b0001: cpu_mem_rdata = fast_mem_rdata;
  526. 4'b0010: cpu_mem_rdata = sdram_mem_rdata;
  527. 4'b1000: cpu_mem_rdata = iodev_rdata_q;
  528. default: cpu_mem_rdata = 32'hxxxx_xxxx;
  529. endcase
  530. // Miscellaneous system control/status registers
  531. wire [ 4:0] sysreg_subreg = cpu_mem_addr[6:2];
  532. wire [31:0] sysreg = iodev_valid_sys << sysreg_subreg;
  533. tri1 [31:0] sysreg_rdata[0:31];
  534. assign iodev_rdata_sys = sysreg_rdata[sysreg_subreg];
  535. //
  536. // Board identification
  537. //
  538. // Magic number: "MAX8"
  539. // Board revision: 1.0/2.0
  540. // Board rework flags:
  541. // [7:0] - reserved
  542. //
  543. wire rtc_32khz_rework = 1'b1;
  544. reg board_id_q;
  545. always @(posedge sys_clk)
  546. board_id_q <= board_id;
  547. wire [ 7:0] max80_fpga = fpga_ver;
  548. wire [ 7:0] max80_major = ~board_id_q ? 8'd2 : 8'd1;
  549. wire [ 7:0] max80_minor = 8'd0;
  550. wire [ 7:0] max80_fixes = 8'b0;
  551. assign sysreg_rdata[0] = SYS_MAGIC_MAX80;
  552. assign sysreg_rdata[1] = { max80_fpga, max80_major,
  553. max80_minor, max80_fixes };
  554. // System reset
  555. wire usb_rxd_break_rst; // Reset due to USB serial port BREAK
  556. wire tty_rxd_break_rst; // Reset due to TTY serial port BREAK
  557. wire vjtag_reset_cmd; // Reset due to virtual JTAG request
  558. // Reset control. Note that CPU reset command 0 is a noop.
  559. wire [3:0] cpu_reset_io_cmd =
  560. (sysreg[3] & cpu_mem_wstrb[0]) << cpu_mem_wdata[1:0];
  561. //
  562. // Soft system reset: FPGA not reloaded, PLLs not reset,
  563. // USB and console are not reset
  564. //
  565. // Triggered by:
  566. // - CPU reset command 1
  567. // - CPU entering TRAP state (irrecoverable error)
  568. // - BREAK received on console
  569. // - VJTAG request
  570. //
  571. assign cpu_reset_cmd[1] = cpu_reset_io_cmd[1] | cpu_trap;
  572. assign aux_reset_cmd[1] = usb_rxd_break_rst | tty_rxd_break_rst |
  573. vjtag_reset_cmd;
  574. //
  575. // Hard system reset: FPGA not reloaded, PLLs reset, all hw units reset
  576. //
  577. assign cpu_reset_cmd[2] = cpu_reset_io_cmd[2];
  578. assign aux_reset_cmd[2] = 1'b0;
  579. //
  580. // FPGA reload reset (not implemented yet)
  581. //
  582. assign cpu_reset_cmd[3] = cpu_reset_io_cmd[3];
  583. assign aux_reset_cmd[3] = 1'b0;
  584. // LED indication from the CPU
  585. reg [2:0] led_q;
  586. always @(negedge rst_n or posedge sys_clk)
  587. if (~rst_n)
  588. led_q <= 3'b000;
  589. else
  590. if ( sysreg[2] & cpu_mem_wstrb[0] )
  591. led_q <= cpu_mem_wdata[2:0];
  592. assign led = led_q;
  593. assign sysreg_rdata[2] = { 29'b0, led_q };
  594. // Random number generator
  595. wire rtc_clk_s;
  596. rng #(.nclocks(2), .width(32)) rng
  597. (
  598. .rst_n ( rst_n ),
  599. .sys_clk ( sys_clk ),
  600. .read_stb ( iodev_valid_random ),
  601. .latch_stb ( sys_clk_stb[16] ),
  602. .ready ( iodev_irq_random ),
  603. .q ( iodev_rdata_random ),
  604. .clocks ( { rtc_clk_s, abc_clk_s } ),
  605. .rngio ( rngio )
  606. );
  607. //
  608. // Serial ROM (also configuration ROM.) Fast hardwired data download
  609. // unit to SDRAM.
  610. //
  611. wire rom_done;
  612. reg rom_done_q;
  613. spirom ddu (
  614. .rst_n ( rst_n ),
  615. .rom_clk ( flash_clk ),
  616. .ram_clk ( sdram_clk ),
  617. .sys_clk ( sys_clk ),
  618. .spi_sck ( flash_sck ),
  619. .spi_io ( flash_io ),
  620. .spi_cs_n ( flash_cs_n ),
  621. .wd ( sdram_rom_wd ),
  622. .waddr ( sdram_rom_waddr ),
  623. .wrq ( sdram_rom_wrq ),
  624. .wacc ( sdram_rom_wacc ),
  625. .cpu_rdata ( iodev_rdata_romcopy ),
  626. .cpu_wdata ( cpu_mem_wdata ),
  627. .cpu_valid ( iodev_valid_romcopy ),
  628. .cpu_wstrb ( cpu_mem_wstrb ),
  629. .cpu_addr ( cpu_mem_addr[4:2] ),
  630. .irq ( iodev_irq_romcopy )
  631. );
  632. //
  633. // Serial port. Direct to the CP2102N for v1 boards
  634. // boards or to GPIO for v2 boards.
  635. //
  636. wire tty_data_out; // Output data
  637. wire tty_data_in; // Input data
  638. wire tty_cts_out; // Assert CTS# externally
  639. wire tty_rts_in; // RTS# received from outside
  640. wire tty_dtr_in; // DTR# received from outside
  641. assign tty_data_in = tty_txd;
  642. assign tty_rxd = tty_data_out;
  643. assign tty_rts_in = ~tty_rts;
  644. assign tty_dtr_in = ~tty_dtr;
  645. assign tty_cts = ~tty_cts_out;
  646. assign tty_cts_out = 1'b1; // Always assert CTS# for now
  647. // The physical tty now just snoops USB ACM channel 0; as such it does
  648. // not respond to any write requests nor issue any irqs
  649. wire serial_tx_full;
  650. wire serial_rx_break;
  651. serial #(.ENABLE_RX_DATA (1'b0),
  652. .ENABLE_RX_BREAK (1'b1),
  653. .ENABLE_TX_DATA (1'b1),
  654. .ENABLE_TX_BREAK (1'b0),
  655. .BAUDRATE_SETTABLE (1'b0),
  656. .BAUDRATE (921600),
  657. .TTY_CLK (84000000))
  658. (
  659. .rst_n ( hard_rst_n ),
  660. .clk ( sys_clk ),
  661. // Snoops USB TTY channel 0
  662. .tx_wstrb ( iodev_valid_tty &
  663. cpu_mem_addr[6:2] == 5'b00000 &
  664. cpu_mem_wstrb[0] ),
  665. .tx_data ( cpu_mem_wdata[7:0] ),
  666. .tx_flush ( 1'b0 ),
  667. .rx_flush ( 1'b0 ),
  668. .tty_rx ( tty_data_in ),
  669. .tty_tx ( tty_data_out ),
  670. .tx_full ( serial_tx_full ),
  671. .rx_break ( tty_rxd_break_rst )
  672. );
  673. // If DTR# is asserted, block on full serial Tx FIFO; this allows
  674. // us to not lose debugging messages.
  675. assign iodev_wait_n_tty = ~(serial_tx_full & tty_dtr_in);
  676. max80_usb #( .channels( TTY_CHANNELS ) ) usb (
  677. .hard_rst_n ( hard_rst_n ),
  678. .clock48 ( usb_clk ),
  679. .rst_n ( rst_n ),
  680. .sys_clk ( sys_clk ),
  681. .cpu_valid_usbdesc ( iodev_valid_usbdesc ),
  682. .cpu_valid_cdc ( iodev_valid_tty ),
  683. .cpu_addr ( cpu_mem_addr ),
  684. .cpu_rdata_usbdesc ( iodev_rdata_usbdesc ),
  685. .cpu_rdata_cdc ( iodev_rdata_tty ),
  686. .cpu_wdata ( cpu_mem_wdata ),
  687. .cpu_wstrb ( cpu_mem_wstrb ),
  688. .irq ( iodev_irq_tty ),
  689. .tty_rxd_break ( usb_rxd_break_rst ),
  690. .usb_dp ( usb_dp ),
  691. .usb_dn ( usb_dn ),
  692. .usb_pu ( usb_pu ),
  693. .usb_rx ( usb_rx ),
  694. .usb_rx_ok ( usb_rx_ok )
  695. );
  696. // SD card
  697. sdcard #(
  698. .with_irq_mask ( 8'b0000_0001 )
  699. )
  700. sdcard (
  701. .rst_n ( rst_n ),
  702. .clk ( sys_clk ),
  703. .sd_cs_n ( sd_cs_n ),
  704. .sd_di ( sd_di ),
  705. .sd_sclk ( sd_clk ),
  706. .sd_do ( sd_do ),
  707. .sd_cd_n ( sd_cd_n ),
  708. .sd_irq_n ( 1'b1 ),
  709. .wdata ( cpu_mem_wdata ),
  710. .rdata ( iodev_rdata_sdcard ),
  711. .valid ( iodev_valid_sdcard ),
  712. .wstrb ( cpu_mem_wstrb ),
  713. .addr ( cpu_mem_addr[6:2] ),
  714. .wait_n ( iodev_wait_n_sdcard ),
  715. .irq ( iodev_irq_sdcard )
  716. );
  717. //
  718. // System local clock (not an RTC per se, but settable from one);
  719. // also provides a periodic interrupt, currently set to 32 Hz.
  720. //
  721. // The RTC 32.768 kHz output is open drain, so use the negative
  722. // edge for clocking.
  723. //
  724. wire clk_32kHz = ~rtc_32khz; // Inverted
  725. wire [15:0] rtc_ctr;
  726. sysclock #(.PERIODIC_HZ_LG2 ( TIMER_SHIFT ))
  727. sysclock (
  728. .rst_n ( rst_n ),
  729. .sys_clk ( sys_clk ),
  730. .rtc_clk ( clk_32kHz ),
  731. .rtc_clk_s ( rtc_clk_s ),
  732. .wdata ( cpu_mem_wdata ),
  733. .rdata ( iodev_rdata_sysclock ),
  734. .valid ( iodev_valid_sysclock ),
  735. .wstrb ( cpu_mem_wstrb ),
  736. .addr ( cpu_mem_addr[2] ),
  737. .periodic ( iodev_irq_sysclock ),
  738. .rtc_ctr ( rtc_ctr )
  739. );
  740. // ESP32
  741. assign spi_cs_flash_n = 1'bz;
  742. esp esp (
  743. .rst_n ( rst_n ),
  744. .sys_clk ( sys_clk ),
  745. .sdram_clk ( sdram_clk ),
  746. .cpu_valid ( iodev_valid_esp ),
  747. .cpu_addr ( cpu_mem_addr[6:2] ),
  748. .cpu_wstrb ( cpu_mem_wstrb ),
  749. .cpu_wdata ( cpu_mem_wdata ),
  750. .cpu_rdata ( iodev_rdata_esp ),
  751. .irq ( iodev_irq_esp ),
  752. .esp_int ( esp_int ),
  753. .spi_clk ( spi_clk ),
  754. .spi_io ( spi_io ),
  755. .spi_cs_n ( spi_cs_esp_n ),
  756. .dram ( sr_bus[2].dstr ),
  757. .rtc_ctr ( rtc_ctr )
  758. );
  759. //
  760. // I2C bus (RTC and to connector)
  761. //
  762. i2c i2c (
  763. .rst_n ( rst_n ),
  764. .clk ( sys_clk ),
  765. .valid ( iodev_valid_i2c ),
  766. .addr ( cpu_mem_addr[3:2] ),
  767. .wdata ( cpu_mem_wdata ),
  768. .wstrb ( cpu_mem_wstrb ),
  769. .rdata ( iodev_rdata_i2c ),
  770. .irq ( iodev_irq_i2c ),
  771. .i2c_scl ( i2c_scl ),
  772. .i2c_sda ( i2c_sda )
  773. );
  774. // Virtual JTAG interface
  775. wire vjtag_cpu_halt;
  776. vjtag_max80 #(.sdram_base_addr(SDRAM_ADDR),
  777. .sdram_bits(SDRAM_BITS),
  778. .sram_bits(SRAM_BITS))
  779. vjtag (
  780. .rst_n ( rst_n ),
  781. .sys_clk ( sys_clk ),
  782. .reset_cmd ( vjtag_reset_cmd ),
  783. .sdram ( sr_bus[3].dstr ),
  784. .cpu_valid ( iodev_valid_vjtag ),
  785. .cpu_addr ( cpu_mem_addr[6:2] ),
  786. .cpu_wdata ( cpu_mem_wdata ),
  787. .cpu_wstrb ( cpu_mem_wstrb ),
  788. .cpu_rdata ( iodev_rdata_vjtag ),
  789. .cpu_irq ( iodev_irq_vjtag ),
  790. .cpu_halt ( vjtag_cpu_halt ),
  791. .sram_addr ( vjtag_sram_addr ),
  792. .sram_rdata ( vjtag_sram_rdata ),
  793. .sram_wdata ( vjtag_sram_wdata ),
  794. .sram_read ( vjtag_sram_read ),
  795. .sram_write ( vjtag_sram_write )
  796. );
  797. assign cpu_halt = vjtag_cpu_halt;
  798. //
  799. // Registering of I/O data and handling of iodev_mem_ready
  800. //
  801. always @(posedge sys_clk)
  802. iodev_rdata_q <= iodev_rdata;
  803. always @(negedge rst_n or posedge sys_clk)
  804. if (~rst_n)
  805. iodev_mem_ready <= 1'b0;
  806. else
  807. iodev_mem_ready <= iodev_wait_n & cpu_mem_valid;
  808. endmodule