max80.sv 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. // Clock oscillator
  11. input clock_48, // 48 MHz
  12. input board_id,
  13. // ABC-bus
  14. input abc_clk, // ABC-bus 3 MHz clock
  15. input [15:0] abc_a, // ABC address bus
  16. inout [7:0] abc_d, // ABC data bus
  17. output abc_d_oe, // Data bus output enable
  18. input abc_rst_n, // ABC bus reset strobe
  19. input abc_cs_n, // ABC card select strobe
  20. input [4:0] abc_out_n, // OUT, C1-C4 strobe
  21. input [1:0] abc_inp_n, // INP, STATUS strobe
  22. input abc_xmemfl_n, // Memory read strobe
  23. input abc_xmemw800_n, // Memory write strobe (ABC800)
  24. input abc_xmemw80_n, // Memory write strobe (ABC80)
  25. input abc_xinpstb_n, // I/O read strobe (ABC800)
  26. input abc_xoutpstb_n, // I/O write strobe (ABC80)
  27. // The following are inverted versus the bus IF
  28. // the corresponding MOSFETs are installed
  29. output abc_rdy_x, // RDY = WAIT#
  30. output abc_resin_x, // System reset request
  31. output abc_int80_x, // System INT request (ABC80)
  32. output abc_int800_x, // System INT request (ABC800)
  33. output abc_nmi_x, // System NMI request (ABC800)
  34. output abc_xm_x, // System memory override (ABC800)
  35. // Host/device control
  36. output abc_host, // 1 = host, 0 = target
  37. output abc_a_oe,
  38. // Bus isolation
  39. output abc_d_ce_n,
  40. // ABC-bus extension header
  41. // (Note: cannot use an array here because HC and HH are
  42. // input only.)
  43. inout exth_ha,
  44. inout exth_hb,
  45. input exth_hc,
  46. inout exth_hd,
  47. inout exth_he,
  48. inout exth_hf,
  49. inout exth_hg,
  50. input exth_hh,
  51. // SDRAM bus
  52. output sr_clk,
  53. output sr_cke,
  54. output [1:0] sr_ba, // Bank address
  55. output [12:0] sr_a, // Address within bank
  56. inout [15:0] sr_dq, // Also known as D or IO
  57. output [1:0] sr_dqm, // DQML and DQMH
  58. output sr_cs_n,
  59. output sr_we_n,
  60. output sr_cas_n,
  61. output sr_ras_n,
  62. // SD card
  63. output sd_clk,
  64. output sd_cmd,
  65. inout [3:0] sd_dat,
  66. // USB serial (naming is FPGA as DCE)
  67. input tty_txd,
  68. output tty_rxd,
  69. input tty_rts,
  70. output tty_cts,
  71. input tty_dtr,
  72. // SPI flash memory (also configuration)
  73. output flash_cs_n,
  74. output flash_sck,
  75. inout [1:0] flash_io,
  76. // SPI bus (connected to ESP32 so can be bidirectional)
  77. inout spi_clk,
  78. inout spi_miso,
  79. inout spi_mosi,
  80. inout spi_cs_esp_n, // ESP32 IO10
  81. inout spi_cs_flash_n, // ESP32 IO01
  82. // Other ESP32 connections
  83. inout esp_io0, // ESP32 IO00
  84. inout esp_int, // ESP32 IO09
  85. // I2C bus (RTC and external)
  86. inout i2c_scl,
  87. inout i2c_sda,
  88. input rtc_32khz,
  89. input rtc_int_n,
  90. // LED (2 = D23/G, 1 = D22/R, 0 = D17/B)
  91. output [2:0] led,
  92. // GPIO pins
  93. inout [5:0] gpio,
  94. // HDMI
  95. output [2:0] hdmi_d,
  96. output hdmi_clk,
  97. inout hdmi_scl,
  98. inout hdmi_sda,
  99. inout hdmi_hpd,
  100. // Unconnected pins with pullups, used for randomness
  101. inout [2:0] rngio
  102. );
  103. // -----------------------------------------------------------------------
  104. // PLLs and reset
  105. // -----------------------------------------------------------------------
  106. // Assert internal reset for 4096 cycles after PLL lock
  107. parameter reset_pow2 = 12;
  108. reg rst_n = 1'b0; // Internal system reset
  109. reg hard_rst_n = 1'b0; // Strict POR reset only
  110. tri1 [4:1] pll_locked;
  111. //
  112. // Clocks.
  113. //
  114. // All clocks are derived from a common 48 MHz oscillator
  115. // connected to clock_48, which is a dedicated clock pin
  116. // feeding into hardware PLL2 and PLL4. The SDRAM clock output
  117. // is a dedicated clock out pin from PLL3.
  118. //
  119. // The following sets of clocks are closely tied and expected to
  120. // be synchronous, and therefore should come from the same PLL each;
  121. // furthermore, the design strictly assumes the ratios specified.
  122. //
  123. // sdram_clk, sys_clk - 2:1 ratio
  124. // vid_hdmiclk, vid_clk - 5:1 ratio
  125. //
  126. wire master_clk; // 336 MHz internal master clock
  127. pll2 pll2 (
  128. .areset ( 1'b0 ),
  129. .locked ( pll_locked[2] ),
  130. .inclk0 ( clock_48 ),
  131. .c0 ( master_clk )
  132. );
  133. wire sdram_clk; // 168 MHz SDRAM clock
  134. wire sys_clk; // 84 MHz System clock
  135. wire flash_clk; // 134 MHz Serial flash ROM clock
  136. wire usb_clk; // 48 MHz USB clock
  137. pll3 pll3 (
  138. .areset ( ~pll_locked[2] ),
  139. .locked ( pll_locked[3] ),
  140. .inclk0 ( master_clk ),
  141. .c0 ( sr_clk ), // Output to clock pin (phase shift)
  142. .c1 ( sdram_clk ), // Internal logic/buffer data clock
  143. .c2 ( sys_clk ),
  144. .c3 ( flash_clk ),
  145. .c4 ( usb_clk )
  146. );
  147. wire vid_clk; // 56 MHz Video pixel clock
  148. wire vid_hdmiclk; // 280 MHz HDMI serializer clock = vid_clk x 5
  149. pll4 pll4 (
  150. .areset ( ~pll_locked[2] ),
  151. .locked ( pll_locked[4] ),
  152. .inclk0 ( master_clk ),
  153. .c0 ( vid_hdmiclk ),
  154. .c1 ( vid_clk )
  155. );
  156. wire all_plls_locked = &pll_locked;
  157. // sys_clk pulse generation of various powers of two
  158. // Also used to generate rst_n
  159. reg [23:1] sys_clk_ctr;
  160. reg [23:1] sys_clk_ctr_q;
  161. reg [23:1] sys_clk_stb;
  162. reg [1:0] reset_cmd_q;
  163. wire reset_cmd;
  164. always @(negedge all_plls_locked or posedge sys_clk)
  165. if (~all_plls_locked)
  166. begin
  167. hard_rst_n <= 1'b0;
  168. rst_n <= 1'b0;
  169. reset_cmd_q <= 2'b0;
  170. sys_clk_ctr <= 1'b0;
  171. sys_clk_ctr_q <= 1'b0;
  172. sys_clk_stb <= 1'b0;
  173. end
  174. else
  175. begin
  176. reset_cmd_q <= { reset_cmd_q[0], reset_cmd };
  177. if (reset_cmd_q == 2'b01)
  178. begin
  179. sys_clk_ctr <= 1'b0;
  180. sys_clk_ctr_q <= 1'b0;
  181. sys_clk_stb <= 1'b0;
  182. rst_n <= 1'b0;
  183. end
  184. else
  185. begin
  186. sys_clk_ctr <= sys_clk_ctr + 1'b1;
  187. sys_clk_ctr_q <= sys_clk_ctr;
  188. sys_clk_stb <= ~sys_clk_ctr & sys_clk_ctr_q;
  189. rst_n <= rst_n | sys_clk_stb[reset_pow2];
  190. hard_rst_n <= hard_rst_n | sys_clk_stb[reset_pow2];
  191. end
  192. end
  193. // Unused device stubs - remove when used
  194. // Reset in the video clock domain
  195. reg vid_rst_n;
  196. always @(negedge all_plls_locked or posedge vid_clk)
  197. if (~all_plls_locked)
  198. vid_rst_n <= 1'b0;
  199. else
  200. vid_rst_n <= rst_n;
  201. // HDMI video interface
  202. video video (
  203. .rst_n ( vid_rst_n ),
  204. .vid_clk ( vid_clk ),
  205. .vid_hdmiclk ( vid_hdmiclk ),
  206. .hdmi_d ( hdmi_d ),
  207. .hdmi_clk ( hdmi_clk ),
  208. .hdmi_scl ( hdmi_scl ),
  209. .hdmi_hpd ( hdmi_hpd )
  210. );
  211. //
  212. // Internal CPU bus
  213. //
  214. wire cpu_mem_valid;
  215. wire cpu_mem_instr;
  216. wire [ 3:0] cpu_mem_wstrb;
  217. wire [31:0] cpu_mem_addr;
  218. wire [31:0] cpu_mem_wdata;
  219. reg [31:0] cpu_mem_rdata;
  220. reg cpu_mem_ready;
  221. wire cpu_la_read;
  222. wire cpu_la_write;
  223. wire [31:0] cpu_la_addr;
  224. wire [31:0] cpu_la_wdata;
  225. wire [ 3:0] cpu_la_wstrb;
  226. // cpu_mem_valid by address quadrant
  227. wire [ 3:0] cpu_mem_quad = cpu_mem_valid << cpu_mem_addr[31:30];
  228. // I/O device map from iodevs.conf
  229. wire iodev_mem_valid = cpu_mem_quad[3];
  230. `include "iodevs.vh"
  231. //
  232. // SDRAM
  233. //
  234. localparam dram_port_count = 2;
  235. dram_bus sr_bus[1:dram_port_count] ( );
  236. // ABC interface
  237. wire [24:0] abc_sr_addr;
  238. wire [ 7:0] abc_sr_rd;
  239. wire abc_sr_valid;
  240. wire abc_sr_ready;
  241. wire [ 7:0] abc_sr_wd;
  242. wire abc_sr_wstrb;
  243. dram_port #(8)
  244. abc_dram_port (
  245. .bus ( sr_bus[1] ),
  246. .prio ( 2'd3 ),
  247. .addr ( abc_sr_addr ),
  248. .rd ( abc_sr_rd ),
  249. .valid ( abc_sr_valid ),
  250. .ready ( abc_sr_ready ),
  251. .wd ( abc_sr_wd ),
  252. .wstrb ( abc_sr_wstrb )
  253. );
  254. // CPU interface
  255. wire [31:0] sdram_mem_rdata;
  256. wire sdram_ready;
  257. reg sdram_mem_ready;
  258. // Retard sdram_ready by one sys_clk (multicycle path for the data,
  259. // see max80.sdc)
  260. always @(posedge sys_clk)
  261. sdram_mem_ready <= sdram_ready;
  262. dram_port #(32)
  263. cpu_dram_port (
  264. .bus ( sr_bus[2] ),
  265. .prio ( 2'd1 ),
  266. .addr ( cpu_mem_addr[24:0] ),
  267. .rd ( sdram_mem_rdata ),
  268. .valid ( cpu_mem_quad[1] ),
  269. .ready ( sdram_ready ),
  270. .wd ( cpu_mem_wdata ),
  271. .wstrb ( cpu_mem_wstrb )
  272. );
  273. // Romcopy interface
  274. wire [15:0] sdram_rom_wd;
  275. wire [24:1] sdram_rom_waddr;
  276. wire [ 1:0] sdram_rom_wrq;
  277. wire sdram_rom_wacc;
  278. sdram #(.port1_count(dram_port_count))
  279. sdram (
  280. .rst_n ( rst_n ),
  281. .clk ( sdram_clk ), // Internal clock
  282. .sr_cke ( sr_cke ),
  283. .sr_cs_n ( sr_cs_n ),
  284. .sr_ras_n ( sr_ras_n ),
  285. .sr_cas_n ( sr_cas_n ),
  286. .sr_we_n ( sr_we_n ),
  287. .sr_dqm ( sr_dqm ),
  288. .sr_ba ( sr_ba ),
  289. .sr_a ( sr_a ),
  290. .sr_dq ( sr_dq ),
  291. .port1 ( sr_bus ),
  292. .a2 ( sdram_rom_waddr ),
  293. .wd2 ( sdram_rom_wd ),
  294. .wrq2 ( sdram_rom_wrq ),
  295. .wacc2 ( sdram_rom_wacc )
  296. );
  297. //
  298. // ABC-bus interface
  299. //
  300. wire abc_clk_s; // abc_clk synchronous to sys_clk
  301. abcbus abcbus (
  302. .rst_n ( rst_n ),
  303. .sys_clk ( sys_clk ),
  304. .sdram_clk ( sdram_clk ),
  305. .stb_1mhz ( sys_clk_stb[6] ),
  306. .abc_valid ( iodev_valid_abc ),
  307. .map_valid ( iodev_valid_abcmemmap ),
  308. .cpu_addr ( cpu_mem_addr ),
  309. .cpu_wdata ( cpu_mem_wdata ),
  310. .cpu_wstrb ( cpu_mem_wstrb ),
  311. .cpu_rdata ( iodev_rdata_abc ),
  312. .cpu_rdata_map ( iodev_rdata_abcmemmap ),
  313. .irq ( iodev_irq_abc ),
  314. .abc_clk ( abc_clk ),
  315. .abc_clk_s ( abc_clk_s ),
  316. .abc_a ( abc_a ),
  317. .abc_d ( abc_d ),
  318. .abc_d_oe ( abc_d_oe ),
  319. .abc_rst_n ( abc_rst_n ),
  320. .abc_cs_n ( abc_cs_n ),
  321. .abc_out_n ( abc_out_n ),
  322. .abc_inp_n ( abc_inp_n ),
  323. .abc_xmemfl_n ( abc_xmemfl_n ),
  324. .abc_xmemw800_n ( abc_xmemw800_n ),
  325. .abc_xmemw80_n ( abc_xmemw80_n ),
  326. .abc_xinpstb_n ( abc_xinpstb_n ),
  327. .abc_xoutpstb_n ( abc_xoutpstb_n ),
  328. .abc_rdy_x ( abc_rdy_x ),
  329. .abc_resin_x ( abc_resin_x ),
  330. .abc_int80_x ( abc_int80_x ),
  331. .abc_int800_x ( abc_int800_x ),
  332. .abc_nmi_x ( abc_nmi_x ),
  333. .abc_xm_x ( abc_xm_x ),
  334. .abc_host ( abc_host ),
  335. .abc_a_oe ( abc_a_oe ),
  336. .abc_d_ce_n ( abc_d_ce_n ),
  337. .exth_ha ( exth_ha ),
  338. .exth_hb ( exth_hb ),
  339. .exth_hc ( exth_hc ),
  340. .exth_hd ( exth_hd ),
  341. .exth_he ( exth_he ),
  342. .exth_hf ( exth_hf ),
  343. .exth_hg ( exth_hg ),
  344. .exth_hh ( exth_hh ),
  345. .sdram_addr ( abc_sr_addr ),
  346. .sdram_rd ( abc_sr_rd ),
  347. .sdram_valid ( abc_sr_valid ),
  348. .sdram_ready ( abc_sr_ready ),
  349. .sdram_wd ( abc_sr_wd ),
  350. .sdram_wstrb ( abc_sr_wstrb )
  351. );
  352. // GPIO
  353. assign gpio = 6'bzzzzzz;
  354. // Embedded RISC-V CPU
  355. parameter cpu_fast_mem_bits = SRAM_BITS-2; /* 2^[this] * 4 bytes */
  356. // Edge-triggered IRQs. picorv32 latches interrupts
  357. // but doesn't edge detect for a slow signal, so do it
  358. // here instead and use level triggered signalling to the
  359. // CPU.
  360. wire [31:0] cpu_eoi;
  361. reg [31:0] cpu_eoi_q;
  362. // sys_irq defined in iodevs.vh
  363. reg [31:0] sys_irq_q;
  364. reg [31:0] cpu_irq;
  365. // CPU permanently hung?
  366. wire cpu_trap;
  367. always @(negedge rst_n or posedge sys_clk)
  368. if (~rst_n)
  369. begin
  370. sys_irq_q <= 32'b0;
  371. cpu_eoi_q <= 32'b0;
  372. cpu_irq <= 32'b0;
  373. end
  374. else
  375. begin
  376. sys_irq_q <= sys_irq & irq_edge_mask;
  377. cpu_eoi_q <= cpu_eoi & irq_edge_mask;
  378. cpu_irq <= (sys_irq & ~sys_irq_q)
  379. | (cpu_irq & irq_edge_mask & ~(cpu_eoi & ~cpu_eoi_q));
  380. end
  381. picorv32 #(
  382. .ENABLE_COUNTERS ( 1 ),
  383. .ENABLE_COUNTERS64 ( 1 ),
  384. .ENABLE_REGS_16_31 ( 1 ),
  385. .ENABLE_REGS_DUALPORT ( 1 ),
  386. .LATCHED_MEM_RDATA ( 1 ),
  387. .BARREL_SHIFTER ( 1 ),
  388. .TWO_CYCLE_COMPARE ( 0 ),
  389. .TWO_CYCLE_ALU ( 0 ),
  390. .COMPRESSED_ISA ( 1 ),
  391. .CATCH_MISALIGN ( 1 ),
  392. .CATCH_ILLINSN ( 1 ),
  393. .ENABLE_FAST_MUL ( 1 ),
  394. .ENABLE_DIV ( 1 ),
  395. .ENABLE_IRQ ( 1 ),
  396. .ENABLE_IRQ_QREGS ( 1 ),
  397. .ENABLE_IRQ_TIMER ( 1 ),
  398. .MASKED_IRQ ( irq_masked ),
  399. .LATCHED_IRQ ( 32'h0000_0007 ),
  400. .REGS_INIT_ZERO ( 1 ),
  401. .STACKADDR ( 32'h4 << cpu_fast_mem_bits )
  402. )
  403. cpu (
  404. .clk ( sys_clk ),
  405. .resetn ( rst_n ),
  406. .trap ( cpu_trap ),
  407. .progaddr_reset ( _PC_RESET ),
  408. .progaddr_irq ( _PC_IRQ ),
  409. .mem_instr ( cpu_mem_instr ),
  410. .mem_ready ( cpu_mem_ready ),
  411. .mem_valid ( cpu_mem_valid ),
  412. .mem_wstrb ( cpu_mem_wstrb ),
  413. .mem_addr ( cpu_mem_addr ),
  414. .mem_wdata ( cpu_mem_wdata ),
  415. .mem_rdata ( cpu_mem_rdata ),
  416. .mem_la_read ( cpu_la_read ),
  417. .mem_la_write ( cpu_la_write ),
  418. .mem_la_wdata ( cpu_la_wdata ),
  419. .mem_la_addr ( cpu_la_addr ),
  420. .mem_la_wstrb ( cpu_la_wstrb ),
  421. .irq ( cpu_irq ),
  422. .eoi ( cpu_eoi )
  423. );
  424. // Add a mandatory wait state to iodevs to reduce the size
  425. // of the CPU memory input MUX (it hurts timing on memory
  426. // accesses...)
  427. reg iodev_mem_ready;
  428. always @(*)
  429. case ( cpu_mem_quad )
  430. 4'b0000: cpu_mem_ready = 1'b0;
  431. 4'b0001: cpu_mem_ready = 1'b1;
  432. 4'b0010: cpu_mem_ready = sdram_mem_ready;
  433. 4'b0100: cpu_mem_ready = 1'b1;
  434. 4'b1000: cpu_mem_ready = iodev_mem_ready;
  435. default: cpu_mem_ready = 1'bx;
  436. endcase // case ( mem_quad )
  437. //
  438. // Fast memory. This runs on the SDRAM clock, i.e. 2x the speed
  439. // of the CPU. The .bits parameter gives the number of dwords
  440. // as a power of 2, i.e. 11 = 2^11 * 4 = 8K.
  441. //
  442. wire [31:0] fast_mem_rdata;
  443. fast_mem // #(.bits(cpu_fast_mem_bits), .mif("../rv32/boot"))
  444. fast_mem(
  445. .rst_n ( rst_n ),
  446. .clk ( sys_clk ),
  447. .read ( cpu_la_read & cpu_la_addr[31:30] == 2'b00 ),
  448. .write ( cpu_la_write & cpu_la_addr[31:30] == 2'b00 ),
  449. .wstrb ( cpu_la_wstrb ),
  450. .addr ( cpu_la_addr[14:2] ),
  451. .wdata ( cpu_la_wdata ),
  452. .rdata ( fast_mem_rdata )
  453. );
  454. // Register I/O data to reduce the size of the read data MUX
  455. reg [31:0] iodev_rdata_q;
  456. // Read data MUX
  457. always_comb
  458. case ( cpu_mem_quad )
  459. 4'b0001: cpu_mem_rdata = fast_mem_rdata;
  460. 4'b0010: cpu_mem_rdata = sdram_mem_rdata;
  461. 4'b1000: cpu_mem_rdata = iodev_rdata_q;
  462. default: cpu_mem_rdata = 32'hxxxx_xxxx;
  463. endcase
  464. // Miscellaneous system control/status registers
  465. wire [ 4:0] sysreg_subreg = cpu_mem_addr[6:2];
  466. wire [31:0] sysreg = iodev_valid_sys << sysreg_subreg;
  467. tri1 [31:0] sysreg_rdata[0:31];
  468. assign iodev_rdata_sys = sysreg_rdata[sysreg_subreg];
  469. //
  470. // Board identification
  471. //
  472. // Magic number: "MAX8"
  473. // Board revision: 1.0
  474. // Board rework flags:
  475. // [0] - RTC 32 kHz pullup and serial port RxD/TxD swap
  476. // [15:1] - reserved
  477. //
  478. wire rtc_32khz_rework = 1'b1;
  479. reg board_id_q;
  480. always @(posedge sys_clk)
  481. board_id_q <= board_id;
  482. wire [ 7:0] max80_major = ~board_id_q ? 8'd2 : 8'd1;
  483. wire [ 7:0] max80_minor = 8'd0;
  484. wire [15:0] max80_fixes = { 14'b0, rtc_32khz_rework }; // Workarounds
  485. assign sysreg_rdata[0] = SYS_MAGIC_MAX80;
  486. assign sysreg_rdata[1] = { max80_major, max80_minor, max80_fixes };
  487. // System reset
  488. wire usb_rxd_break;
  489. reg usb_rxd_break_q;
  490. reg usb_rxd_break_rst;
  491. always @(negedge rst_n or posedge sys_clk)
  492. if (~rst_n)
  493. begin
  494. usb_rxd_break_q <= 1'b1;
  495. usb_rxd_break_rst <= 1'b0;
  496. end
  497. else
  498. begin
  499. usb_rxd_break_q <= usb_rxd_break;
  500. usb_rxd_break_rst <= usb_rxd_break & ~usb_rxd_break_q;
  501. end
  502. assign reset_cmd =
  503. // Explicit reset command
  504. (sysreg[3] & cpu_mem_wstrb[0] & cpu_mem_wdata[0]) |
  505. // CPU hung
  506. cpu_trap |
  507. // BREAK received on USB tty
  508. usb_rxd_break_rst;
  509. // LED indication from the CPU
  510. reg [2:0] led_q;
  511. always @(negedge rst_n or posedge sys_clk)
  512. if (~rst_n)
  513. led_q <= 3'b000;
  514. else
  515. if ( sysreg[2] & cpu_mem_wstrb[0] )
  516. led_q <= cpu_mem_wdata[2:0];
  517. assign led = led_q;
  518. assign sysreg_rdata[2] = { 29'b0, led_q };
  519. // Random number generator
  520. wire rtc_clk_s;
  521. rng #(.nclocks(2), .width(32)) rng
  522. (
  523. .sys_clk ( sys_clk ),
  524. .q ( sysreg_rdata[4] ),
  525. .clocks ( { rtc_clk_s, abc_clk_s } ),
  526. .rngio ( rngio )
  527. );
  528. //
  529. // Serial ROM (also configuration ROM.) Fast hardwired data download
  530. // unit to SDRAM.
  531. //
  532. wire rom_done;
  533. reg rom_done_q;
  534. spirom ddu (
  535. .rst_n ( rst_n ),
  536. .rom_clk ( flash_clk ),
  537. .ram_clk ( sdram_clk ),
  538. .sys_clk ( sys_clk ),
  539. .spi_sck ( flash_sck ),
  540. .spi_io ( flash_io ),
  541. .spi_cs_n ( flash_cs_n ),
  542. .wd ( sdram_rom_wd ),
  543. .waddr ( sdram_rom_waddr ),
  544. .wrq ( sdram_rom_wrq ),
  545. .wacc ( sdram_rom_wacc ),
  546. .cpu_rdata ( iodev_rdata_romcopy ),
  547. .cpu_wdata ( cpu_mem_wdata ),
  548. .cpu_valid ( iodev_valid_romcopy ),
  549. .cpu_wstrb ( cpu_mem_wstrb ),
  550. .cpu_addr ( cpu_mem_addr[4:2] ),
  551. .irq ( iodev_irq_romcopy )
  552. );
  553. //
  554. // Serial port. Direct to the CP2102N for reworked
  555. // boards or to GPIO for non-reworked boards, depending on
  556. // whether DTR# is asserted on either.
  557. //
  558. // The GPIO numbering matches the order of pins for FT[2]232H.
  559. // gpio[0] - TxD
  560. // gpio[1] - RxD
  561. // gpio[2] - RTS#
  562. // gpio[3] - CTS#
  563. // gpio[4] - DTR#
  564. //
  565. wire tty_data_out; // Output data
  566. wire tty_data_in; // Input data
  567. wire tty_cts_out; // Assert CTS# externally
  568. wire tty_rts_in; // RTS# received from outside
  569. assign tty_cts_out = 1'b0; // Assert CTS#
  570. tty console (
  571. .rst_n ( hard_rst_n ),
  572. .clk ( sys_clk ),
  573. .valid ( iodev_valid_console ),
  574. .wstrb ( cpu_mem_wstrb ),
  575. .wdata ( cpu_mem_wdata ),
  576. .rdata ( iodev_rdata_console ),
  577. .addr ( cpu_mem_addr[3:2] ),
  578. .irq ( iodev_irq_console ),
  579. .tty_txd ( tty_data_out ) // DTE -> DCE
  580. );
  581. max80_usb usb (
  582. .rst_n ( hard_rst_n ),
  583. .clock48 ( usb_clk ),
  584. .tty_rxd ( ),
  585. .tty_rxd_break ( usb_rxd_break ),
  586. .tty_txd ( tty_data_out ),
  587. .usb_dp ( gpio[3] ),
  588. .usb_dn ( gpio[5] ),
  589. .usb_pu ( gpio[1] )
  590. );
  591. assign tty_data_in = tty_txd;
  592. assign tty_rxd = tty_data_out;
  593. assign tty_rts_in = tty_rts;
  594. assign tty_cts = tty_cts_out;
  595. // SD card
  596. sdcard #(
  597. .with_irq_mask ( 8'b0000_0001 )
  598. )
  599. sdcard (
  600. .rst_n ( rst_n ),
  601. .clk ( sys_clk ),
  602. .sd_cs_n ( sd_dat[3] ),
  603. .sd_di ( sd_cmd ),
  604. .sd_sclk ( sd_clk ),
  605. .sd_do ( sd_dat[0] ),
  606. .sd_cd_n ( 1'b0 ),
  607. .sd_irq_n ( 1'b1 ),
  608. .wdata ( cpu_mem_wdata ),
  609. .rdata ( iodev_rdata_sdcard ),
  610. .valid ( iodev_valid_sdcard ),
  611. .wstrb ( cpu_mem_wstrb ),
  612. .addr ( cpu_mem_addr[6:2] ),
  613. .wait_n ( iodev_wait_n_sdcard ),
  614. .irq ( iodev_irq_sdcard )
  615. );
  616. assign sd_dat[2:1] = 2'bzz;
  617. //
  618. // System local clock (not an RTC per se, but settable from one);
  619. // also provides a periodic interrupt, currently set to 32 Hz.
  620. //
  621. // The RTC 32.768 kHz output is open drain, so use the negative
  622. // edge for clocking.
  623. //
  624. wire clk_32kHz = ~rtc_32khz; // Inverted
  625. sysclock #(.PERIODIC_HZ_LG2 ( TIMER_SHIFT ))
  626. sysclock (
  627. .rst_n ( rst_n ),
  628. .sys_clk ( sys_clk ),
  629. .rtc_clk ( clk_32kHz ),
  630. .rtc_clk_s ( rtc_clk_s ),
  631. .wdata ( cpu_mem_wdata ),
  632. .rdata ( iodev_rdata_sysclock ),
  633. .valid ( iodev_valid_sysclock ),
  634. .wstrb ( cpu_mem_wstrb ),
  635. .addr ( cpu_mem_addr[2] ),
  636. .periodic ( iodev_irq_sysclock )
  637. );
  638. // SPI bus to ESP32; using the sdcard IP as a SPI master for now at
  639. // least...
  640. `ifdef REALLY_ESP32
  641. // ESP32
  642. assign spi_cs_flash_n = 1'bz;
  643. assign esp_io0 = 1'b1; // If pulled down on reset, ESP32 will enter
  644. // firmware download mode
  645. sdcard #(
  646. .with_irq_mask ( 8'b0000_0101 ),
  647. .with_crc7 ( 1'b0 ),
  648. .with_crc16 ( 1'b0 )
  649. )
  650. esp (
  651. .rst_n ( rst_n ),
  652. .clk ( sys_clk ),
  653. .sd_cs_n ( spi_cs_esp_n ),
  654. .sd_di ( spi_mosi ),
  655. .sd_sclk ( spi_clk ),
  656. .sd_do ( spi_miso ),
  657. .sd_cd_n ( 1'b0 ),
  658. .sd_irq_n ( esp_int ),
  659. .wdata ( cpu_mem_wdata ),
  660. .rdata ( iodev_rdata_esp ),
  661. .valid ( iodev_valid_esp ),
  662. .wstrb ( cpu_mem_wstrb ),
  663. .addr ( cpu_mem_addr[6:2] ),
  664. .wait_n ( iodev_wait_n_esp ),
  665. .irq ( iodev_irq_esp )
  666. );
  667. `else // !`ifdef REALLY_ESP32
  668. reg [5:-13] esp_ctr; // 32768 * 2^-13 = 4 Hz
  669. always @(posedge clk_32kHz)
  670. esp_ctr <= esp_ctr + 1'b1;
  671. assign spi_clk = esp_ctr[0];
  672. assign spi_mosi = esp_ctr[1];
  673. assign spi_miso = esp_ctr[2];
  674. assign spi_cs_flash_n = esp_ctr[3]; // IO01
  675. assign spi_cs_esp_n = esp_ctr[4]; // IO10
  676. assign spi_int = esp_ctr[5]; // IO09
  677. assign esp_io0 = 1'b1;
  678. `endif
  679. //
  680. // I2C bus (RTC and to connector)
  681. //
  682. i2c i2c (
  683. .rst_n ( rst_n ),
  684. .clk ( sys_clk ),
  685. .valid ( iodev_valid_i2c ),
  686. .addr ( cpu_mem_addr[3:2] ),
  687. .wdata ( cpu_mem_wdata ),
  688. .wstrb ( cpu_mem_wstrb ),
  689. .rdata ( iodev_rdata_i2c ),
  690. .irq ( iodev_irq_i2c ),
  691. .i2c_scl ( i2c_scl ),
  692. .i2c_sda ( i2c_sda )
  693. );
  694. //
  695. // Registering of I/O data and handling of iodev_mem_ready
  696. //
  697. always @(posedge sys_clk)
  698. iodev_rdata_q <= iodev_rdata;
  699. always @(negedge rst_n or posedge sys_clk)
  700. if (~rst_n)
  701. iodev_mem_ready <= 1'b0;
  702. else
  703. iodev_mem_ready <= iodev_wait_n & cpu_mem_valid;
  704. endmodule