max80.sv 24 KB

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