max80.sv 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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. wire [31:0] sys_clk_ctr_next = sys_clk_ctr + 1'b1;
  182. reg [31:1] sys_clk_stb;
  183. reg [31:1] sdram_clk_stb; // == sys_clk_stb for the sdram clk
  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_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_stb <= 1'b0;
  215. rst_n <= 1'b0;
  216. hard_rst_n <= hard_rst_n & ~|reset_cmd_q[3:2];
  217. end
  218. else
  219. begin
  220. sys_clk_ctr <= sys_clk_ctr_next;
  221. sys_clk_stb <= ~sys_clk_ctr_next & sys_clk_ctr;
  222. rst_n <= rst_n | ~sys_clk_ctr[reset_pow2];
  223. hard_rst_n <= hard_rst_n | ~sys_clk_ctr[reset_pow2];
  224. end
  225. end
  226. // This code assumes sdram_clk == 2 x sys_clk
  227. always @(posedge sdram_clk)
  228. sdram_clk_stb <= sys_clk ? sys_clk_stb : 'b0;
  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 space, using a bit of lookahead
  262. // decoding for speed.
  263. //
  264. // Address space 0 = SRAM
  265. // 1 = SDRAM
  266. // 2 = I/O
  267. typedef enum {
  268. AS_SRAM = 0,
  269. AS_SDRAM = 1,
  270. AS_IO = 2
  271. } as_enum_t;
  272. localparam as_enum_t AS_MAX = AS_IO;
  273. function logic [AS_MAX:0] mem_as_decode(logic [31:0] addr);
  274. mem_as_decode[AS_SRAM] = addr[31:29] == 3'b000;
  275. mem_as_decode[AS_SDRAM] = !addr[31] && addr[30:29] != 2'b00;
  276. mem_as_decode[AS_IO] = addr[31];
  277. endfunction
  278. reg [AS_MAX:0] mem_as;
  279. always @(negedge rst_n or posedge sys_clk)
  280. if (~rst_n)
  281. mem_as <= 'b0;
  282. else if (cpu_mem_valid)
  283. mem_as <= mem_as_decode(cpu_mem_addr);
  284. else
  285. mem_as <= mem_as_decode(cpu_la_addr);
  286. wire [AS_MAX:0] cpu_mem_as = cpu_mem_valid ? mem_as : 'b0;
  287. // I/O device map from iodevs.conf
  288. wire iodev_mem_valid = cpu_mem_as[AS_IO];
  289. `include "iodevs.vh"
  290. //
  291. // SDRAM
  292. //
  293. localparam dram_port_count = 4;
  294. dram_bus sr_bus[1:dram_port_count] ( );
  295. // ABC interface
  296. wire [24:0] abc_sr_addr;
  297. wire [ 7:0] abc_sr_rd;
  298. wire abc_sr_valid;
  299. wire abc_sr_ready;
  300. wire [ 7:0] abc_sr_wd;
  301. wire abc_sr_wstrb;
  302. dram_port #(8)
  303. abc_dram_port (
  304. .bus ( sr_bus[1] ),
  305. .prio ( 2'd3 ),
  306. .addr ( abc_sr_addr ),
  307. .rd ( abc_sr_rd ),
  308. .valid ( abc_sr_valid ),
  309. .ready ( abc_sr_ready ),
  310. .wd ( abc_sr_wd ),
  311. .wstrb ( abc_sr_wstrb )
  312. );
  313. // CPU interface
  314. wire sdram_valid = cpu_mem_as[AS_SDRAM];
  315. wire [31:0] sdram_mem_rdata;
  316. wire sdram_ready;
  317. reg sdram_ready_q;
  318. reg sdram_mem_ready;
  319. //
  320. // Retard sdram_ready by one sys_clk (multicycle path for the data,
  321. // see max80.sdc)
  322. //
  323. // Note that if the CPU leaves valid asserted the CPU cycle after
  324. // receiving ready, it is the beginning of another request. The
  325. // sdram core expects valid to be strobed, so deassert valid
  326. // to the sdram core while asserting ready to the CPU.
  327. //
  328. always @(posedge sys_clk)
  329. sdram_mem_ready <= sdram_ready & sdram_valid;
  330. dram_port #(32)
  331. cpu_dram_port (
  332. .bus ( sr_bus[4] ),
  333. .prio ( 2'd1 ),
  334. .addr ( cpu_mem_addr[24:0] ),
  335. .rd ( sdram_mem_rdata ),
  336. .valid ( sdram_valid & ~sdram_mem_ready ),
  337. .ready ( sdram_ready ),
  338. .wd ( cpu_mem_wdata ),
  339. .wstrb ( cpu_mem_wstrb )
  340. );
  341. // Romcopy interface
  342. wire [15:0] sdram_rom_wd;
  343. wire [24:1] sdram_rom_waddr;
  344. wire [ 1:0] sdram_rom_wrq;
  345. wire sdram_rom_wacc;
  346. // Dirty page tracking
  347. wire [12:0] sdram_dirty_pg;
  348. wire sdram_dirty_stb;
  349. sdram #(.port1_count(dram_port_count))
  350. sdram (
  351. .rst_n ( rst_n ),
  352. .clk ( sdram_clk ), // Internal memory clock
  353. .init_tmr ( sys_clk_stb[14] ), // > 100 μs (tP) after reset
  354. .rfsh_tmr ( sys_clk_stb[8] ), // < 3.9 μs (tREFI/2)
  355. .sr_cs_n ( sr_cs_n ),
  356. .sr_ras_n ( sr_ras_n ),
  357. .sr_cas_n ( sr_cas_n ),
  358. .sr_we_n ( sr_we_n ),
  359. .sr_dqm ( sr_dqm ),
  360. .sr_ba ( sr_ba ),
  361. .sr_a ( sr_a ),
  362. .sr_dq ( sr_dq ),
  363. .port1 ( sr_bus ),
  364. .a2 ( sdram_rom_waddr ),
  365. .wd2 ( sdram_rom_wd ),
  366. .wrq2 ( sdram_rom_wrq ),
  367. .wacc2 ( sdram_rom_wacc ),
  368. .dirty_pg ( sdram_dirty_pg ),
  369. .dirty_stb ( sdram_dirty_stb )
  370. );
  371. // Dirty page memory
  372. dirty sdram_dirty (
  373. .rst_n ( rst_n ),
  374. .clk ( sdram_clk ),
  375. .dirty_pg ( sdram_dirty_pg ),
  376. .dirty_stb ( sdram_dirty_stb ),
  377. .cpu_addr ( cpu_mem_addr ),
  378. .cpu_wstrb ( cpu_mem_wstrb ),
  379. .cpu_wdata ( cpu_mem_wdata ),
  380. .cpu_rdata ( iodev_rdata_dirty )
  381. );
  382. //
  383. // ABC-bus interface
  384. //
  385. wire abc_clk_s; // abc_clk synchronous to sys_clk
  386. abcbus #(.mosfet_installed(x_mosfet), .sdram_base_addr(SDRAM_ADDR))
  387. abcbus (
  388. .rst_n ( rst_n ),
  389. .sys_clk ( sys_clk ),
  390. .sdram_clk ( sdram_clk ),
  391. .stb_1mhz ( sys_clk_stb[6] ),
  392. .stb_50us ( sdram_clk_stb[13] ),
  393. .abc_valid ( iodev_valid_abc ),
  394. .map_valid ( iodev_valid_abcmemmap ),
  395. .cpu_addr ( cpu_mem_addr ),
  396. .cpu_wdata ( cpu_mem_wdata ),
  397. .cpu_wstrb ( cpu_mem_wstrb ),
  398. .cpu_rdata ( iodev_rdata_abc ),
  399. .cpu_rdata_map ( iodev_rdata_abcmemmap ),
  400. .irq ( iodev_irq_abc ),
  401. .abc_clk ( abc_clk ),
  402. .abc_clk_s ( abc_clk_s ),
  403. .abc_a ( abc_a ),
  404. .abc_d ( abc_d ),
  405. .abc_d_oe ( abc_d_oe ),
  406. .abc_rst_n ( abc_rst_n ),
  407. .abc_cs_n ( abc_cs_n ),
  408. .abc_out_n ( abc_out_n ),
  409. .abc_inp_n ( abc_inp_n ),
  410. .abc_xmemfl_n ( abc_xmemfl_n ),
  411. .abc_xmemw800_n ( abc_xmemw800_n ),
  412. .abc_xmemw80_n ( abc_xmemw80_n ),
  413. .abc_xinpstb_n ( abc_xinpstb_n ),
  414. .abc_xoutpstb_n ( abc_xoutpstb_n ),
  415. .abc_rdy_x ( abc_rdy_x ),
  416. .abc_resin_x ( abc_resin_x ),
  417. .abc_int80_x ( abc_int80_x ),
  418. .abc_int800_x ( abc_int800_x ),
  419. .abc_nmi_x ( abc_nmi_x ),
  420. .abc_xm_x ( abc_xm_x ),
  421. .abc_host ( abc_host ),
  422. .exth_ha ( exth_ha ),
  423. .exth_hb ( exth_hb ),
  424. .exth_hc ( exth_hc ),
  425. .exth_hd ( exth_hd ),
  426. .exth_he ( exth_he ),
  427. .exth_hf ( exth_hf ),
  428. .exth_hg ( exth_hg ),
  429. .exth_hh ( exth_hh ),
  430. .sdram_addr ( abc_sr_addr ),
  431. .sdram_rd ( abc_sr_rd ),
  432. .sdram_valid ( abc_sr_valid ),
  433. .sdram_ready ( abc_sr_ready ),
  434. .sdram_wd ( abc_sr_wd ),
  435. .sdram_wstrb ( abc_sr_wstrb )
  436. );
  437. // Embedded RISC-V CPU
  438. // Edge-triggered IRQs. picorv32 latches interrupts
  439. // but doesn't edge detect for a slow signal, so do it
  440. // here instead and use level triggered signalling to the
  441. // CPU.
  442. reg [31:0] cpu_irq;
  443. reg [31:0] sys_irq_q;
  444. wire [31:0] cpu_eoi;
  445. always @(negedge rst_n or posedge sys_clk)
  446. if (~rst_n)
  447. begin
  448. sys_irq_q <= 32'b0;
  449. cpu_irq <= 32'b0;
  450. end
  451. else
  452. begin
  453. sys_irq_q <= sys_irq & irq_edge_mask;
  454. cpu_irq <= (sys_irq & ~sys_irq_q) |
  455. (cpu_irq & irq_edge_mask & ~cpu_eoi);
  456. end
  457. // CPU permanently hung?
  458. wire cpu_trap;
  459. // Request to halt the CPU on the next instruction boundary
  460. wire cpu_halt;
  461. picorv32 #(
  462. .COUNTER_CYCLE_WIDTH ( 64 ),
  463. .COUNTER_INSTR_WIDTH ( 0 ), // No use...
  464. .ENABLE_REGS_16_31 ( 1 ),
  465. .ENABLE_REGS_DUALPORT ( 1 ),
  466. .LATCHED_MEM_RDATA ( 0 ),
  467. .BARREL_SHIFTER ( 1 ),
  468. .TWO_CYCLE_COMPARE ( 0 ),
  469. .TWO_CYCLE_ALU ( 0 ),
  470. .COMPRESSED_ISA ( 1 ),
  471. .CATCH_MISALIGN ( 1 ),
  472. .CATCH_ILLINSN ( 1 ),
  473. .ENABLE_FAST_MUL ( 1 ),
  474. .ENABLE_DIV ( 1 ),
  475. .ENABLE_IRQ ( 1 ),
  476. .ENABLE_IRQ_QREGS ( 1 ),
  477. .ENABLE_IRQ_TIMER ( 1 ),
  478. .MASKED_IRQ ( irq_masked ),
  479. .LATCHED_IRQ ( 32'h0000_0007 ),
  480. .REGS_INIT_ZERO ( 1 ),
  481. .STACKADDR ( 1'b1 << SRAM_BITS ),
  482. .USER_CONTEXTS ( 7 )
  483. ) cpu (
  484. .clk ( sys_clk ),
  485. .resetn ( rst_n ),
  486. .halt ( cpu_halt ),
  487. .trap ( cpu_trap ),
  488. .progaddr_reset ( _PC_RESET ),
  489. .progaddr_irq ( _PC_IRQ ),
  490. .mem_instr ( cpu_mem_instr ),
  491. .mem_ready ( cpu_mem_ready ),
  492. .mem_valid ( cpu_mem_valid ),
  493. .mem_wstrb ( cpu_mem_wstrb ),
  494. .mem_addr ( cpu_mem_addr ),
  495. .mem_wdata ( cpu_mem_wdata ),
  496. .mem_rdata ( cpu_mem_rdata ),
  497. .mem_la_read ( cpu_la_read ),
  498. .mem_la_write ( cpu_la_write ),
  499. .mem_la_wdata ( cpu_la_wdata ),
  500. .mem_la_addr ( cpu_la_addr ),
  501. .mem_la_wstrb ( cpu_la_wstrb ),
  502. .irq ( cpu_irq ),
  503. .eoi ( cpu_eoi )
  504. );
  505. // Add a mandatory wait state to iodevs to reduce the size
  506. // of the CPU memory input MUX (it hurts timing on memory
  507. // accesses...)
  508. reg iodev_mem_ready;
  509. wire sram_mem_ready;
  510. assign cpu_mem_ready = (cpu_mem_as[AS_SRAM] & sram_mem_ready) |
  511. (cpu_mem_as[AS_SDRAM] & sdram_mem_ready) |
  512. (cpu_mem_as[AS_IO] & iodev_mem_ready);
  513. //
  514. // Fast memory. This runs on the SDRAM clock, i.e. 2x the speed
  515. // of the CPU. The .bits parameter gives the number of dwords
  516. // as a power of 2, i.e. 11 = 2^11 * 4 = 8K.
  517. //
  518. wire [31:0] fast_mem_rdata;
  519. wire [SRAM_BITS-1:2] vjtag_sram_addr;
  520. wire vjtag_sram_read;
  521. wire vjtag_sram_write;
  522. wire [31:0] vjtag_sram_rdata;
  523. wire [31:0] vjtag_sram_wdata;
  524. fast_mem #(.words_lg2(SRAM_BITS-2),
  525. .data_file("mif/sram.mif"))
  526. fast_mem(
  527. .rst_n ( rst_n ),
  528. .clk ( sys_clk ),
  529. .read0 ( 1'b1 ), // cpu_la_read & cpu_la_addr[31:30] == 2'b00
  530. .write0 ( cpu_la_write & cpu_la_addr[31:30] == 2'b00 ),
  531. .wstrb0 ( cpu_la_wstrb ),
  532. .addr0 ( cpu_la_addr[SRAM_BITS-1:2] ),
  533. .wdata0 ( cpu_la_wdata ),
  534. .rdata0 ( fast_mem_rdata ),
  535. .read1 ( 1'b1 ), // vjtag_sram_read
  536. .write1 ( vjtag_sram_write ),
  537. .wstrb1 ( 4'b1111 ),
  538. .addr1 ( vjtag_sram_addr ),
  539. .wdata1 ( vjtag_sram_wdata ),
  540. .rdata1 ( vjtag_sram_rdata )
  541. );
  542. assign sram_mem_ready = 1'b1; // Always ready
  543. // Register I/O data to reduce the size of the read data MUX
  544. reg [31:0] iodev_rdata_q;
  545. // Read data MUX
  546. always_comb
  547. case ( cpu_mem_as )
  548. 1'b1 << AS_SRAM: cpu_mem_rdata = fast_mem_rdata;
  549. 1'b1 << AS_SDRAM: cpu_mem_rdata = sdram_mem_rdata;
  550. 1'b1 << AS_IO: cpu_mem_rdata = iodev_rdata_q;
  551. default: cpu_mem_rdata = 32'hxxxx_xxxx;
  552. endcase
  553. // Miscellaneous system control/status registers
  554. wire [ 4:0] sysreg_subreg = cpu_mem_addr[6:2];
  555. wire [31:0] sysreg = iodev_valid_sys << sysreg_subreg;
  556. tri1 [31:0] sysreg_rdata[0:31];
  557. assign iodev_rdata_sys = sysreg_rdata[sysreg_subreg];
  558. //
  559. // Board identification
  560. //
  561. // Magic number: "MAX8"
  562. // Board revision: 1.0/2.0
  563. // Board rework flags:
  564. // [7:0] - reserved
  565. //
  566. wire rtc_32khz_rework = 1'b1;
  567. reg board_id_q;
  568. always @(posedge sys_clk)
  569. board_id_q <= board_id;
  570. wire [ 7:0] max80_fpga = fpga_ver;
  571. wire [ 7:0] max80_major = ~board_id_q ? 8'd2 : 8'd1;
  572. wire [ 7:0] max80_minor = 8'd0;
  573. wire [ 7:0] max80_fixes = 8'b0;
  574. assign sysreg_rdata[0] = SYS_MAGIC_MAX80;
  575. assign sysreg_rdata[1] = { max80_fpga, max80_major,
  576. max80_minor, max80_fixes };
  577. // System reset
  578. wire usb_rxd_break_rst; // Reset due to USB serial port BREAK
  579. wire tty_rxd_break_rst; // Reset due to TTY serial port BREAK
  580. wire vjtag_reset_cmd; // Reset due to virtual JTAG request
  581. // Reset control. Note that CPU reset command 0 is a noop.
  582. wire [3:0] cpu_reset_io_cmd =
  583. (sysreg[3] & cpu_mem_wstrb[0]) << cpu_mem_wdata[1:0];
  584. //
  585. // Soft system reset: FPGA not reloaded, PLLs not reset,
  586. // USB and console are not reset
  587. //
  588. // Triggered by:
  589. // - CPU reset command 1
  590. // - CPU entering TRAP state (irrecoverable error)
  591. // - BREAK received on console
  592. // - VJTAG request
  593. //
  594. assign cpu_reset_cmd[1] = cpu_reset_io_cmd[1] | cpu_trap;
  595. assign aux_reset_cmd[1] = usb_rxd_break_rst | tty_rxd_break_rst |
  596. vjtag_reset_cmd;
  597. //
  598. // Hard system reset: FPGA not reloaded, PLLs reset, all hw units reset
  599. //
  600. assign cpu_reset_cmd[2] = cpu_reset_io_cmd[2];
  601. assign aux_reset_cmd[2] = 1'b0;
  602. //
  603. // FPGA reload reset (not implemented yet)
  604. //
  605. assign cpu_reset_cmd[3] = cpu_reset_io_cmd[3];
  606. assign aux_reset_cmd[3] = 1'b0;
  607. // LED indication from the CPU
  608. reg [2:0] led_q;
  609. always @(negedge rst_n or posedge sys_clk)
  610. if (~rst_n)
  611. led_q <= 3'b000;
  612. else
  613. if ( sysreg[2] & cpu_mem_wstrb[0] )
  614. led_q <= cpu_mem_wdata[2:0];
  615. assign led = led_q;
  616. assign sysreg_rdata[2] = { 29'b0, led_q };
  617. // Random number generator
  618. wire rtc_clk_s;
  619. rng #(.nclocks(2), .width(32)) rng
  620. (
  621. .rst_n ( rst_n ),
  622. .sys_clk ( sys_clk ),
  623. .read_stb ( iodev_valid_random ),
  624. .latch_stb ( sys_clk_stb[16] ),
  625. .ready ( iodev_irq_random ),
  626. .q ( iodev_rdata_random ),
  627. .clocks ( { rtc_clk_s, abc_clk_s } ),
  628. .rngio ( rngio )
  629. );
  630. //
  631. // Serial ROM (also configuration ROM.) Fast hardwired data download
  632. // unit to SDRAM.
  633. //
  634. wire rom_done;
  635. reg rom_done_q;
  636. spirom ddu (
  637. .rst_n ( rst_n ),
  638. .rom_clk ( flash_clk ),
  639. .ram_clk ( sdram_clk ),
  640. .sys_clk ( sys_clk ),
  641. .spi_sck ( flash_sck ),
  642. .spi_io ( flash_io ),
  643. .spi_cs_n ( flash_cs_n ),
  644. .wd ( sdram_rom_wd ),
  645. .waddr ( sdram_rom_waddr ),
  646. .wrq ( sdram_rom_wrq ),
  647. .wacc ( sdram_rom_wacc ),
  648. .cpu_rdata ( iodev_rdata_romcopy ),
  649. .cpu_wdata ( cpu_mem_wdata ),
  650. .cpu_valid ( iodev_valid_romcopy ),
  651. .cpu_wstrb ( cpu_mem_wstrb ),
  652. .cpu_addr ( cpu_mem_addr[4:2] ),
  653. .irq ( iodev_irq_romcopy )
  654. );
  655. //
  656. // Serial port. Direct to the CP2102N for v1 boards
  657. // boards or to GPIO for v2 boards.
  658. //
  659. wire tty_data_out; // Output data
  660. wire tty_data_in; // Input data
  661. wire tty_cts_out; // Assert CTS# externally
  662. wire tty_rts_in; // RTS# received from outside
  663. wire tty_dtr_in; // DTR# received from outside
  664. assign tty_data_in = tty_txd;
  665. assign tty_rxd = tty_data_out;
  666. assign tty_rts_in = ~tty_rts;
  667. assign tty_dtr_in = ~tty_dtr;
  668. assign tty_cts = ~tty_cts_out;
  669. assign tty_cts_out = 1'b1; // Always assert CTS# for now
  670. // The physical tty now just snoops USB ACM channel 0; as such it does
  671. // not respond to any write requests nor issue any irqs
  672. wire serial_tx_full;
  673. wire serial_rx_break;
  674. serial #(.ENABLE_RX_DATA (1'b0),
  675. .ENABLE_RX_BREAK (1'b1),
  676. .ENABLE_TX_DATA (1'b1),
  677. .ENABLE_TX_BREAK (1'b0),
  678. .BAUDRATE_SETTABLE (1'b0),
  679. .BAUDRATE (921600),
  680. .TTY_CLK (84000000))
  681. (
  682. .rst_n ( hard_rst_n ),
  683. .clk ( sys_clk ),
  684. // Snoops USB TTY channel 0
  685. .tx_wstrb ( iodev_valid_tty &
  686. cpu_mem_addr[6:2] == 5'b00000 &
  687. cpu_mem_wstrb[0] ),
  688. .tx_data ( cpu_mem_wdata[7:0] ),
  689. .tx_flush ( 1'b0 ),
  690. .rx_flush ( 1'b0 ),
  691. .tty_rx ( tty_data_in ),
  692. .tty_tx ( tty_data_out ),
  693. .tx_full ( serial_tx_full ),
  694. .rx_break ( tty_rxd_break_rst )
  695. );
  696. // If DTR# is asserted, block on full serial Tx FIFO; this allows
  697. // us to not lose debugging messages.
  698. assign iodev_wait_n_tty = ~(serial_tx_full & tty_dtr_in);
  699. max80_usb #( .channels( TTY_CHANNELS ) ) usb (
  700. .hard_rst_n ( hard_rst_n ),
  701. .clock48 ( usb_clk ),
  702. .rst_n ( rst_n ),
  703. .sys_clk ( sys_clk ),
  704. .cpu_valid_usbdesc ( iodev_valid_usbdesc ),
  705. .cpu_valid_cdc ( iodev_valid_tty ),
  706. .cpu_addr ( cpu_mem_addr ),
  707. .cpu_rdata_usbdesc ( iodev_rdata_usbdesc ),
  708. .cpu_rdata_cdc ( iodev_rdata_tty ),
  709. .cpu_wdata ( cpu_mem_wdata ),
  710. .cpu_wstrb ( cpu_mem_wstrb ),
  711. .irq ( iodev_irq_tty ),
  712. .tty_rxd_break ( usb_rxd_break_rst ),
  713. .usb_dp ( usb_dp ),
  714. .usb_dn ( usb_dn ),
  715. .usb_pu ( usb_pu ),
  716. .usb_rx ( usb_rx ),
  717. .usb_rx_ok ( usb_rx_ok )
  718. );
  719. // SD card
  720. sdcard #(
  721. .with_irq_mask ( 8'b0000_0001 )
  722. )
  723. sdcard (
  724. .rst_n ( rst_n ),
  725. .clk ( sys_clk ),
  726. .sd_cs_n ( sd_cs_n ),
  727. .sd_di ( sd_di ),
  728. .sd_sclk ( sd_clk ),
  729. .sd_do ( sd_do ),
  730. .sd_cd_n ( sd_cd_n ),
  731. .sd_irq_n ( 1'b1 ),
  732. .wdata ( cpu_mem_wdata ),
  733. .rdata ( iodev_rdata_sdcard ),
  734. .valid ( iodev_valid_sdcard ),
  735. .wstrb ( cpu_mem_wstrb ),
  736. .addr ( cpu_mem_addr[6:2] ),
  737. .wait_n ( iodev_wait_n_sdcard ),
  738. .irq ( iodev_irq_sdcard )
  739. );
  740. //
  741. // System local clock (not an RTC per se, but settable from one);
  742. // also provides a periodic interrupt, currently set to 32 Hz.
  743. //
  744. // The RTC 32.768 kHz output is open drain, so use the negative
  745. // edge for clocking.
  746. //
  747. wire clk_32kHz = ~rtc_32khz; // Inverted
  748. wire [15:0] rtc_ctr;
  749. sysclock #(.PERIODIC_HZ_LG2 ( TIMER_SHIFT ))
  750. sysclock (
  751. .rst_n ( rst_n ),
  752. .sys_clk ( sys_clk ),
  753. .rtc_clk ( clk_32kHz ),
  754. .rtc_clk_s ( rtc_clk_s ),
  755. .wdata ( cpu_mem_wdata ),
  756. .rdata ( iodev_rdata_sysclock ),
  757. .valid ( iodev_valid_sysclock ),
  758. .wstrb ( cpu_mem_wstrb ),
  759. .addr ( cpu_mem_addr[2] ),
  760. .periodic ( iodev_irq_sysclock ),
  761. .rtc_ctr ( rtc_ctr )
  762. );
  763. // ESP32
  764. assign spi_cs_flash_n = 1'bz;
  765. esp esp (
  766. .rst_n ( rst_n ),
  767. .sys_clk ( sys_clk ),
  768. .sdram_clk ( sdram_clk ),
  769. .cpu_valid ( iodev_valid_esp ),
  770. .cpu_addr ( cpu_mem_addr[6:2] ),
  771. .cpu_wstrb ( cpu_mem_wstrb ),
  772. .cpu_wdata ( cpu_mem_wdata ),
  773. .cpu_rdata ( iodev_rdata_esp ),
  774. .irq ( iodev_irq_esp ),
  775. .esp_int ( esp_int ),
  776. .spi_clk ( spi_clk ),
  777. .spi_io ( spi_io ),
  778. .spi_cs_n ( spi_cs_esp_n ),
  779. .dram ( sr_bus[2].dstr ),
  780. .rtc_ctr ( rtc_ctr )
  781. );
  782. //
  783. // I2C bus (RTC and to connector)
  784. //
  785. i2c i2c (
  786. .rst_n ( rst_n ),
  787. .clk ( sys_clk ),
  788. .valid ( iodev_valid_i2c ),
  789. .addr ( cpu_mem_addr[3:2] ),
  790. .wdata ( cpu_mem_wdata ),
  791. .wstrb ( cpu_mem_wstrb ),
  792. .rdata ( iodev_rdata_i2c ),
  793. .irq ( iodev_irq_i2c ),
  794. .i2c_scl ( i2c_scl ),
  795. .i2c_sda ( i2c_sda )
  796. );
  797. // Virtual JTAG interface
  798. wire vjtag_cpu_halt;
  799. vjtag_max80 #(.sdram_base_addr(SDRAM_ADDR),
  800. .sdram_bits(SDRAM_BITS),
  801. .sram_bits(SRAM_BITS))
  802. vjtag (
  803. .rst_n ( rst_n ),
  804. .sys_clk ( sys_clk ),
  805. .reset_cmd ( vjtag_reset_cmd ),
  806. .sdram ( sr_bus[3].dstr ),
  807. .cpu_valid ( iodev_valid_vjtag ),
  808. .cpu_addr ( cpu_mem_addr[6:2] ),
  809. .cpu_wdata ( cpu_mem_wdata ),
  810. .cpu_wstrb ( cpu_mem_wstrb ),
  811. .cpu_rdata ( iodev_rdata_vjtag ),
  812. .cpu_irq ( iodev_irq_vjtag ),
  813. .cpu_halt ( vjtag_cpu_halt ),
  814. .sram_addr ( vjtag_sram_addr ),
  815. .sram_rdata ( vjtag_sram_rdata ),
  816. .sram_wdata ( vjtag_sram_wdata ),
  817. .sram_read ( vjtag_sram_read ),
  818. .sram_write ( vjtag_sram_write )
  819. );
  820. assign cpu_halt = vjtag_cpu_halt;
  821. //
  822. // Registering of I/O data and handling of iodev_mem_ready
  823. //
  824. always @(posedge sys_clk)
  825. iodev_rdata_q <= iodev_rdata;
  826. always @(negedge rst_n or posedge sys_clk)
  827. if (~rst_n)
  828. iodev_mem_ready <= 1'b0;
  829. else
  830. iodev_mem_ready <= iodev_wait_n & cpu_mem_valid;
  831. endmodule