max80.sv 21 KB

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