max80.sv 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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.
  411. reg [31:0] cpu_irq;
  412. reg [31:0] sys_irq_q;
  413. wire [31:0] cpu_eoi;
  414. always @(negedge rst_n or posedge sys_clk)
  415. if (~rst_n)
  416. begin
  417. sys_irq_q <= 32'b0;
  418. cpu_irq <= 32'b0;
  419. end
  420. else
  421. begin
  422. sys_irq_q <= sys_irq & irq_edge_mask;
  423. cpu_irq <= (sys_irq & ~sys_irq_q) |
  424. (cpu_irq & irq_edge_mask & ~cpu_eoi);
  425. end
  426. // CPU permanently hung?
  427. wire cpu_trap;
  428. // Request to halt the CPU on the next instruction boundary
  429. wire cpu_halt;
  430. picorv32 #(
  431. .COUNTER_CYCLE_WIDTH ( 64 ),
  432. .COUNTER_INSTR_WIDTH ( 0 ), // No use...
  433. .ENABLE_REGS_16_31 ( 1 ),
  434. .ENABLE_REGS_DUALPORT ( 1 ),
  435. .LATCHED_MEM_RDATA ( 0 ),
  436. .BARREL_SHIFTER ( 1 ),
  437. .TWO_CYCLE_COMPARE ( 0 ),
  438. .TWO_CYCLE_ALU ( 0 ),
  439. .COMPRESSED_ISA ( 1 ),
  440. .CATCH_MISALIGN ( 1 ),
  441. .CATCH_ILLINSN ( 1 ),
  442. .ENABLE_FAST_MUL ( 1 ),
  443. .ENABLE_DIV ( 1 ),
  444. .ENABLE_IRQ ( 1 ),
  445. .ENABLE_IRQ_QREGS ( 1 ),
  446. .ENABLE_IRQ_TIMER ( 1 ),
  447. .MASKED_IRQ ( irq_masked ),
  448. .LATCHED_IRQ ( 32'h0000_0007 ),
  449. .REGS_INIT_ZERO ( 1 ),
  450. .STACKADDR ( 1'b1 << SRAM_BITS ),
  451. .USER_CONTEXTS ( 7 )
  452. ) cpu (
  453. .clk ( sys_clk ),
  454. .resetn ( rst_n ),
  455. .halt ( cpu_halt ),
  456. .trap ( cpu_trap ),
  457. .progaddr_reset ( _PC_RESET ),
  458. .progaddr_irq ( _PC_IRQ ),
  459. .mem_instr ( cpu_mem_instr ),
  460. .mem_ready ( cpu_mem_ready ),
  461. .mem_valid ( cpu_mem_valid ),
  462. .mem_wstrb ( cpu_mem_wstrb ),
  463. .mem_addr ( cpu_mem_addr ),
  464. .mem_wdata ( cpu_mem_wdata ),
  465. .mem_rdata ( cpu_mem_rdata ),
  466. .mem_la_read ( cpu_la_read ),
  467. .mem_la_write ( cpu_la_write ),
  468. .mem_la_wdata ( cpu_la_wdata ),
  469. .mem_la_addr ( cpu_la_addr ),
  470. .mem_la_wstrb ( cpu_la_wstrb ),
  471. .irq ( cpu_irq ),
  472. .eoi ( cpu_eoi )
  473. );
  474. // Add a mandatory wait state to iodevs to reduce the size
  475. // of the CPU memory input MUX (it hurts timing on memory
  476. // accesses...)
  477. reg iodev_mem_ready;
  478. assign cpu_mem_ready =
  479. (cpu_mem_quad[0] & 1'b1) |
  480. (cpu_mem_quad[1] & sdram_mem_ready) |
  481. (cpu_mem_quad[2] & 1'b1) |
  482. (cpu_mem_quad[3] & iodev_mem_ready);
  483. //
  484. // Fast memory. This runs on the SDRAM clock, i.e. 2x the speed
  485. // of the CPU. The .bits parameter gives the number of dwords
  486. // as a power of 2, i.e. 11 = 2^11 * 4 = 8K.
  487. //
  488. wire [31:0] fast_mem_rdata;
  489. wire [SRAM_BITS-1:2] vjtag_sram_addr;
  490. wire vjtag_sram_read;
  491. wire vjtag_sram_write;
  492. wire [31:0] vjtag_sram_rdata;
  493. wire [31:0] vjtag_sram_wdata;
  494. fast_mem #(.words_lg2(SRAM_BITS-2),
  495. .data_file("mif/sram.mif"))
  496. fast_mem(
  497. .rst_n ( rst_n ),
  498. .clk ( sys_clk ),
  499. .read0 ( 1'b1 ), // cpu_la_read & cpu_la_addr[31:30] == 2'b00
  500. .write0 ( cpu_la_write & cpu_la_addr[31:30] == 2'b00 ),
  501. .wstrb0 ( cpu_la_wstrb ),
  502. .addr0 ( cpu_la_addr[SRAM_BITS-1:2] ),
  503. .wdata0 ( cpu_la_wdata ),
  504. .rdata0 ( fast_mem_rdata ),
  505. .read1 ( 1'b1 ), // vjtag_sram_read
  506. .write1 ( vjtag_sram_write ),
  507. .wstrb1 ( 4'b1111 ),
  508. .addr1 ( vjtag_sram_addr ),
  509. .wdata1 ( vjtag_sram_wdata ),
  510. .rdata1 ( vjtag_sram_rdata )
  511. );
  512. // Register I/O data to reduce the size of the read data MUX
  513. reg [31:0] iodev_rdata_q;
  514. // Read data MUX
  515. always_comb
  516. case ( cpu_mem_quad )
  517. 4'b0001: cpu_mem_rdata = fast_mem_rdata;
  518. 4'b0010: cpu_mem_rdata = sdram_mem_rdata;
  519. 4'b1000: cpu_mem_rdata = iodev_rdata_q;
  520. default: cpu_mem_rdata = 32'hxxxx_xxxx;
  521. endcase
  522. // Miscellaneous system control/status registers
  523. wire [ 4:0] sysreg_subreg = cpu_mem_addr[6:2];
  524. wire [31:0] sysreg = iodev_valid_sys << sysreg_subreg;
  525. tri1 [31:0] sysreg_rdata[0:31];
  526. assign iodev_rdata_sys = sysreg_rdata[sysreg_subreg];
  527. //
  528. // Board identification
  529. //
  530. // Magic number: "MAX8"
  531. // Board revision: 1.0/2.0
  532. // Board rework flags:
  533. // [7:0] - reserved
  534. //
  535. wire rtc_32khz_rework = 1'b1;
  536. reg board_id_q;
  537. always @(posedge sys_clk)
  538. board_id_q <= board_id;
  539. wire [ 7:0] max80_fpga = fpga_ver;
  540. wire [ 7:0] max80_major = ~board_id_q ? 8'd2 : 8'd1;
  541. wire [ 7:0] max80_minor = 8'd0;
  542. wire [ 7:0] max80_fixes = 8'b0;
  543. assign sysreg_rdata[0] = SYS_MAGIC_MAX80;
  544. assign sysreg_rdata[1] = { max80_fpga, max80_major,
  545. max80_minor, max80_fixes };
  546. // System reset
  547. wire usb_rxd_break_rst; // Reset due to USB serial port BREAK
  548. wire tty_rxd_break_rst; // Reset due to TTY serial port BREAK
  549. wire vjtag_reset_cmd; // Reset due to virtual JTAG request
  550. // Reset control. Note that CPU reset command 0 is a noop.
  551. wire [3:0] cpu_reset_io_cmd =
  552. (sysreg[3] & cpu_mem_wstrb[0]) << cpu_mem_wdata[1:0];
  553. //
  554. // Soft system reset: FPGA not reloaded, PLLs not reset,
  555. // USB and console are not reset
  556. //
  557. // Triggered by:
  558. // - CPU reset command 1
  559. // - CPU entering TRAP state (irrecoverable error)
  560. // - BREAK received on console
  561. // - VJTAG request
  562. //
  563. assign cpu_reset_cmd[1] = cpu_reset_io_cmd[1] | cpu_trap;
  564. assign aux_reset_cmd[1] = usb_rxd_break_rst | tty_rxd_break_rst |
  565. vjtag_reset_cmd;
  566. //
  567. // Hard system reset: FPGA not reloaded, PLLs reset, all hw units reset
  568. //
  569. assign cpu_reset_cmd[2] = cpu_reset_io_cmd[2];
  570. assign aux_reset_cmd[2] = 1'b0;
  571. //
  572. // FPGA reload reset (not implemented yet)
  573. //
  574. assign cpu_reset_cmd[3] = cpu_reset_io_cmd[3];
  575. assign aux_reset_cmd[3] = 1'b0;
  576. // LED indication from the CPU
  577. reg [2:0] led_q;
  578. always @(negedge rst_n or posedge sys_clk)
  579. if (~rst_n)
  580. led_q <= 3'b000;
  581. else
  582. if ( sysreg[2] & cpu_mem_wstrb[0] )
  583. led_q <= cpu_mem_wdata[2:0];
  584. assign led = led_q;
  585. assign sysreg_rdata[2] = { 29'b0, led_q };
  586. // Random number generator
  587. wire rtc_clk_s;
  588. rng #(.nclocks(2), .width(32)) rng
  589. (
  590. .rst_n ( rst_n ),
  591. .sys_clk ( sys_clk ),
  592. .read_stb ( iodev_valid_random ),
  593. .latch_stb ( sys_clk_stb[16] ),
  594. .ready ( iodev_irq_random ),
  595. .q ( iodev_rdata_random ),
  596. .clocks ( { rtc_clk_s, abc_clk_s } ),
  597. .rngio ( rngio )
  598. );
  599. //
  600. // Serial ROM (also configuration ROM.) Fast hardwired data download
  601. // unit to SDRAM.
  602. //
  603. wire rom_done;
  604. reg rom_done_q;
  605. spirom ddu (
  606. .rst_n ( rst_n ),
  607. .rom_clk ( flash_clk ),
  608. .ram_clk ( sdram_clk ),
  609. .sys_clk ( sys_clk ),
  610. .spi_sck ( flash_sck ),
  611. .spi_io ( flash_io ),
  612. .spi_cs_n ( flash_cs_n ),
  613. .wd ( sdram_rom_wd ),
  614. .waddr ( sdram_rom_waddr ),
  615. .wrq ( sdram_rom_wrq ),
  616. .wacc ( sdram_rom_wacc ),
  617. .cpu_rdata ( iodev_rdata_romcopy ),
  618. .cpu_wdata ( cpu_mem_wdata ),
  619. .cpu_valid ( iodev_valid_romcopy ),
  620. .cpu_wstrb ( cpu_mem_wstrb ),
  621. .cpu_addr ( cpu_mem_addr[4:2] ),
  622. .irq ( iodev_irq_romcopy )
  623. );
  624. //
  625. // Serial port. Direct to the CP2102N for v1 boards
  626. // boards or to GPIO for v2 boards.
  627. //
  628. wire tty_data_out; // Output data
  629. wire tty_data_in; // Input data
  630. wire tty_cts_out; // Assert CTS# externally
  631. wire tty_rts_in; // RTS# received from outside
  632. wire tty_dtr_in; // DTR# received from outside
  633. assign tty_data_in = tty_txd;
  634. assign tty_rxd = tty_data_out;
  635. assign tty_rts_in = ~tty_rts;
  636. assign tty_dtr_in = ~tty_dtr;
  637. assign tty_cts = ~tty_cts_out;
  638. assign tty_cts_out = 1'b1; // Always assert CTS# for now
  639. // The physical tty now just snoops USB ACM channel 0; as such it does
  640. // not respond to any write requests nor issue any irqs
  641. wire serial_tx_full;
  642. wire serial_rx_break;
  643. serial #(.ENABLE_RX_DATA (1'b0),
  644. .ENABLE_RX_BREAK (1'b1),
  645. .ENABLE_TX_DATA (1'b1),
  646. .ENABLE_TX_BREAK (1'b0),
  647. .BAUDRATE_SETTABLE (1'b0),
  648. .BAUDRATE (921600),
  649. .TTY_CLK (84000000))
  650. (
  651. .rst_n ( hard_rst_n ),
  652. .clk ( sys_clk ),
  653. // Snoops USB TTY channel 0
  654. .tx_wstrb ( iodev_valid_tty &
  655. cpu_mem_addr[6:2] == 5'b00000 &
  656. cpu_mem_wstrb[0] ),
  657. .tx_data ( cpu_mem_wdata[7:0] ),
  658. .tx_flush ( 1'b0 ),
  659. .rx_flush ( 1'b0 ),
  660. .tty_rx ( tty_data_in ),
  661. .tty_tx ( tty_data_out ),
  662. .tx_full ( serial_tx_full ),
  663. .rx_break ( tty_rxd_break_rst )
  664. );
  665. // If DTR# is asserted, block on full serial Tx FIFO; this allows
  666. // us to not lose debugging messages.
  667. assign iodev_wait_n_tty = ~(serial_tx_full & tty_dtr_in);
  668. max80_usb #( .channels( TTY_CHANNELS ) ) usb (
  669. .hard_rst_n ( hard_rst_n ),
  670. .clock48 ( usb_clk ),
  671. .rst_n ( rst_n ),
  672. .sys_clk ( sys_clk ),
  673. .cpu_valid_usbdesc ( iodev_valid_usbdesc ),
  674. .cpu_valid_cdc ( iodev_valid_tty ),
  675. .cpu_addr ( cpu_mem_addr ),
  676. .cpu_rdata_usbdesc ( iodev_rdata_usbdesc ),
  677. .cpu_rdata_cdc ( iodev_rdata_tty ),
  678. .cpu_wdata ( cpu_mem_wdata ),
  679. .cpu_wstrb ( cpu_mem_wstrb ),
  680. .irq ( iodev_irq_tty ),
  681. .tty_rxd_break ( usb_rxd_break_rst ),
  682. .usb_dp ( usb_dp ),
  683. .usb_dn ( usb_dn ),
  684. .usb_pu ( usb_pu ),
  685. .usb_rx ( usb_rx ),
  686. .usb_rx_ok ( usb_rx_ok )
  687. );
  688. // SD card
  689. sdcard #(
  690. .with_irq_mask ( 8'b0000_0001 )
  691. )
  692. sdcard (
  693. .rst_n ( rst_n ),
  694. .clk ( sys_clk ),
  695. .sd_cs_n ( sd_cs_n ),
  696. .sd_di ( sd_di ),
  697. .sd_sclk ( sd_clk ),
  698. .sd_do ( sd_do ),
  699. .sd_cd_n ( sd_cd_n ),
  700. .sd_irq_n ( 1'b1 ),
  701. .wdata ( cpu_mem_wdata ),
  702. .rdata ( iodev_rdata_sdcard ),
  703. .valid ( iodev_valid_sdcard ),
  704. .wstrb ( cpu_mem_wstrb ),
  705. .addr ( cpu_mem_addr[6:2] ),
  706. .wait_n ( iodev_wait_n_sdcard ),
  707. .irq ( iodev_irq_sdcard )
  708. );
  709. //
  710. // System local clock (not an RTC per se, but settable from one);
  711. // also provides a periodic interrupt, currently set to 32 Hz.
  712. //
  713. // The RTC 32.768 kHz output is open drain, so use the negative
  714. // edge for clocking.
  715. //
  716. wire clk_32kHz = ~rtc_32khz; // Inverted
  717. wire [15:0] rtc_ctr;
  718. sysclock #(.PERIODIC_HZ_LG2 ( TIMER_SHIFT ))
  719. sysclock (
  720. .rst_n ( rst_n ),
  721. .sys_clk ( sys_clk ),
  722. .rtc_clk ( clk_32kHz ),
  723. .rtc_clk_s ( rtc_clk_s ),
  724. .wdata ( cpu_mem_wdata ),
  725. .rdata ( iodev_rdata_sysclock ),
  726. .valid ( iodev_valid_sysclock ),
  727. .wstrb ( cpu_mem_wstrb ),
  728. .addr ( cpu_mem_addr[2] ),
  729. .periodic ( iodev_irq_sysclock ),
  730. .rtc_ctr ( rtc_ctr )
  731. );
  732. // ESP32
  733. assign spi_cs_flash_n = 1'bz;
  734. esp esp (
  735. .rst_n ( rst_n ),
  736. .sys_clk ( sys_clk ),
  737. .sdram_clk ( sdram_clk ),
  738. .cpu_valid ( iodev_valid_esp ),
  739. .cpu_addr ( cpu_mem_addr[6:2] ),
  740. .cpu_wstrb ( cpu_mem_wstrb ),
  741. .cpu_wdata ( cpu_mem_wdata ),
  742. .cpu_rdata ( iodev_rdata_esp ),
  743. .irq ( iodev_irq_esp ),
  744. .esp_int ( esp_int ),
  745. .spi_clk ( spi_clk ),
  746. .spi_io ( spi_io ),
  747. .spi_cs_n ( spi_cs_esp_n ),
  748. .dram ( sr_bus[2].dstr ),
  749. .rtc_ctr ( rtc_ctr )
  750. );
  751. //
  752. // I2C bus (RTC and to connector)
  753. //
  754. i2c i2c (
  755. .rst_n ( rst_n ),
  756. .clk ( sys_clk ),
  757. .valid ( iodev_valid_i2c ),
  758. .addr ( cpu_mem_addr[3:2] ),
  759. .wdata ( cpu_mem_wdata ),
  760. .wstrb ( cpu_mem_wstrb ),
  761. .rdata ( iodev_rdata_i2c ),
  762. .irq ( iodev_irq_i2c ),
  763. .i2c_scl ( i2c_scl ),
  764. .i2c_sda ( i2c_sda )
  765. );
  766. // Virtual JTAG interface
  767. wire vjtag_cpu_halt;
  768. vjtag_max80 #(.sdram_base_addr(SDRAM_ADDR),
  769. .sdram_bits(SDRAM_BITS),
  770. .sram_bits(SRAM_BITS))
  771. vjtag (
  772. .rst_n ( rst_n ),
  773. .sys_clk ( sys_clk ),
  774. .reset_cmd ( vjtag_reset_cmd ),
  775. .sdram ( sr_bus[3].dstr ),
  776. .cpu_valid ( iodev_valid_vjtag ),
  777. .cpu_addr ( cpu_mem_addr[6:2] ),
  778. .cpu_wdata ( cpu_mem_wdata ),
  779. .cpu_wstrb ( cpu_mem_wstrb ),
  780. .cpu_rdata ( iodev_rdata_vjtag ),
  781. .cpu_irq ( iodev_irq_vjtag ),
  782. .cpu_halt ( vjtag_cpu_halt ),
  783. .sram_addr ( vjtag_sram_addr ),
  784. .sram_rdata ( vjtag_sram_rdata ),
  785. .sram_wdata ( vjtag_sram_wdata ),
  786. .sram_read ( vjtag_sram_read ),
  787. .sram_write ( vjtag_sram_write )
  788. );
  789. assign cpu_halt = vjtag_cpu_halt;
  790. //
  791. // Registering of I/O data and handling of iodev_mem_ready
  792. //
  793. always @(posedge sys_clk)
  794. iodev_rdata_q <= iodev_rdata;
  795. always @(negedge rst_n or posedge sys_clk)
  796. if (~rst_n)
  797. iodev_mem_ready <= 1'b0;
  798. else
  799. iodev_mem_ready <= iodev_wait_n & cpu_mem_valid;
  800. endmodule