max80.sv 27 KB

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