max80.sv 26 KB

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