max80.sv 24 KB

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