2
0

max80.sv 26 KB

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