2
0

max80.sv 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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 slave on the ABC-bus.
  6. //
  7. // Sharing JTAG pins (via JTAGEN)
  8. `undef SHARED_JTAG
  9. module max80
  10. #(
  11. // Pull-up installed on RTC 32 kHz line
  12. parameter rtc_32khz_rework = 1'b0
  13. ) (
  14. // Clock oscillator
  15. input clock_48, // 48 MHz
  16. // ABC-bus
  17. input abc_clk, // ABC-bus 3 MHz clock
  18. input [15:0] abc_a, // ABC address bus
  19. inout [7:0] abc_d, // ABC data bus
  20. output abc_d_oe, // Data bus output enable
  21. input abc_rst_n, // ABC bus reset strobe
  22. input abc_cs_n, // ABC card select strobe
  23. input [4:0] abc_out_n, // OUT, C1-C4 strobe
  24. input [1:0] abc_inp_n, // INP, STATUS strobe
  25. input abc_xmemfl_n, // Memory read strobe
  26. input abc_xmemw800_n, // Memory write strobe (ABC800)
  27. input abc_xmemw80_n, // Memory write strobe (ABC80)
  28. input abc_xinpstb_n, // I/O read strobe (ABC800)
  29. input abc_xoutpstb_n, // I/O write strobe (ABC80)
  30. // The following are inverted versus the bus IF
  31. // the corresponding MOSFETs are installed
  32. output abc_rdy_x, // RDY = WAIT#
  33. output abc_resin_x, // System reset request
  34. output abc_int80_x, // System INT request (ABC80)
  35. output abc_int800_x, // System INT request (ABC800)
  36. output abc_nmi_x, // System NMI request (ABC800)
  37. output abc_xm_x, // System memory override (ABC800)
  38. // Host/device control
  39. output abc_master, // 1 = host, 0 = device
  40. output abc_a_oe,
  41. // Bus isolation
  42. output abc_d_ce_n,
  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 sr_cke,
  57. output [1:0] sr_ba, // Bank address
  58. output [12:0] sr_a, // Address within bank
  59. inout [15:0] sr_dq, // Also known as D or IO
  60. output [1:0] sr_dqm, // DQML and DQMH
  61. output sr_cs_n,
  62. output sr_we_n,
  63. output sr_cas_n,
  64. output sr_ras_n,
  65. // SD card
  66. output sd_clk,
  67. output sd_cmd,
  68. inout [3:0] sd_dat,
  69. // USB serial (naming is FPGA as DCE)
  70. input tty_txd,
  71. output tty_rxd,
  72. input tty_rts,
  73. output tty_cts,
  74. input tty_dtr,
  75. // SPI flash memory (also configuration)
  76. output flash_cs_n,
  77. output flash_sck,
  78. inout [1:0] flash_io,
  79. // SPI bus (connected to ESP32 so can be bidirectional)
  80. inout spi_clk,
  81. inout spi_miso,
  82. inout spi_mosi,
  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. // LED (2 = D23/G, 1 = D22/R, 0 = D17/B)
  94. output [2:0] led,
  95. // GPIO pins
  96. inout [5:0] gpio,
  97. // HDMI
  98. output [2:0] hdmi_d,
  99. output hdmi_clk,
  100. inout hdmi_scl,
  101. inout hdmi_sda,
  102. inout hdmi_hpd
  103. );
  104. // PLL and reset
  105. parameter reset_pow2 = 12; // Assert internal reset for 4096 cycles after PLL lock
  106. reg rst_n = 1'b0; // Internal reset
  107. wire [1:0] pll_locked;
  108. // Clocks
  109. wire sdram_clk; // SDRAM clock
  110. wire sdram_out_clk; // SDRAM clock, phase shifted
  111. wire sys_clk; // System clock
  112. wire vid_clk; // Video pixel clock
  113. wire vid_hdmiclk; // D:o in the HDMI clock domain
  114. wire flash_clk; // Serial flash ROM clock
  115. reg reset_cmd_q = 1'b0;
  116. wire reset_cmd;
  117. pll pll (
  118. .areset ( reset_cmd_q ),
  119. .inclk0 ( clock_48 ),
  120. .c0 ( sdram_out_clk ), // SDRAM external clock (168 MHz)
  121. .c1 ( sys_clk ), // System clock (84 MHz)
  122. .c2 ( vid_clk ), // Video pixel clock (48 MHz)
  123. .c3 ( flash_clk ), // Serial flash ROM clock (134 MHz)
  124. .c4 ( sdram_clk ), // SDRAM internal clock (168 MHz)
  125. .locked ( pll_locked[0] ),
  126. .phasestep ( 1'b0 ),
  127. .phasecounterselect ( 3'b0 ),
  128. .phaseupdown ( 1'b1 ),
  129. .scanclk ( 1'b0 ),
  130. .phasedone ( )
  131. );
  132. wire all_plls_locked = &pll_locked;
  133. // sys_clk pulse generation of various powers of two
  134. // Also used to generate rst_n
  135. reg [23:1] sys_clk_ctr;
  136. reg [23:1] sys_clk_ctr_q;
  137. reg [23:1] sys_clk_stb;
  138. always @(negedge all_plls_locked or posedge sys_clk)
  139. if (~&all_plls_locked)
  140. begin
  141. rst_n <= 1'b0;
  142. reset_cmd_q <= 1'b0;
  143. sys_clk_ctr <= 1'b0;
  144. sys_clk_ctr_q <= 1'b0;
  145. sys_clk_stb <= 1'b0;
  146. end
  147. else
  148. begin
  149. sys_clk_ctr <= sys_clk_ctr + 1'b1;
  150. sys_clk_ctr_q <= sys_clk_ctr;
  151. sys_clk_stb <= ~sys_clk_ctr & sys_clk_ctr_q;
  152. reset_cmd_q <= rst_n & (reset_cmd_q | reset_cmd);
  153. rst_n <= rst_n | sys_clk_stb[reset_pow2];
  154. end
  155. // Unused device stubs - remove when used
  156. // Reset in the video clock domain
  157. reg vid_rst_n;
  158. always @(negedge all_plls_locked or posedge vid_clk)
  159. if (~all_plls_locked)
  160. vid_rst_n <= 1'b0;
  161. else
  162. vid_rst_n <= rst_n;
  163. // HDMI - generate random data to give Quartus something to do
  164. reg [23:0] dummydata = 30'hc8_fb87;
  165. always @(posedge vid_clk)
  166. dummydata <= { dummydata[22:0], dummydata[23] };
  167. wire [7:0] hdmi_data[3];
  168. wire [9:0] hdmi_tmds[3];
  169. wire [29:0] hdmi_to_tx;
  170. assign hdmi_data[0] = dummydata[7:0];
  171. assign hdmi_data[1] = dummydata[15:8];
  172. assign hdmi_data[2] = dummydata[23:16];
  173. generate
  174. genvar i;
  175. for (i = 0; i < 3; i = i + 1)
  176. begin : hdmitmds
  177. tmdsenc enc (
  178. .rst_n ( vid_rst_n ),
  179. .clk ( vid_clk ),
  180. .den ( 1'b1 ),
  181. .d ( hdmi_data[i] ),
  182. .c ( 2'b00 ),
  183. .q ( hdmi_tmds[i] )
  184. );
  185. end
  186. endgenerate
  187. assign hdmi_scl = 1'bz;
  188. assign hdmi_sda = 1'bz;
  189. assign hdmi_hpd = 1'bz;
  190. //
  191. // The ALTLVDS_TX megafunctions is MSB-first and in time-major order.
  192. // However, TMDS is LSB-first, and we have three TMDS words that
  193. // concatenate in word(channel)-major order.
  194. //
  195. transpose #(.words(3), .bits(10), .reverse_b(1),
  196. .reg_d(0), .reg_q(0)) hdmitranspose
  197. (
  198. .clk ( vid_clk ),
  199. .d ( { hdmi_tmds[2], hdmi_tmds[1], hdmi_tmds[0] } ),
  200. .q ( hdmi_to_tx )
  201. );
  202. hdmitx hdmitx (
  203. .pll_areset ( ~pll_locked[0] ),
  204. .tx_in ( hdmi_to_tx ),
  205. .tx_inclock ( vid_clk ),
  206. .tx_coreclock ( vid_hdmiclk ), // Pixel clock in HDMI domain
  207. .tx_locked ( pll_locked[1] ),
  208. .tx_out ( hdmi_d ),
  209. .tx_outclock ( hdmi_clk )
  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. wire 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. // ABC interface
  235. wire [24:0] abc_sr_addr;
  236. wire [ 7:0] abc_sr_rd;
  237. wire abc_sr_rrq;
  238. wire abc_sr_rack;
  239. wire abc_sr_ready;
  240. wire abc_sr_wd;
  241. wire abc_sr_wrq;
  242. wire abc_sr_wack;
  243. // CPU interface
  244. wire [31:0] sdram_rd;
  245. wire sdram_rack;
  246. wire sdram_rready;
  247. wire sdram_wack;
  248. reg sdram_acked;
  249. wire sdram_valid = cpu_mem_quad[1];
  250. wire sdram_req = sdram_valid & ~sdram_acked;
  251. always @(posedge sdram_clk)
  252. sdram_acked <= sdram_valid & (sdram_acked | sdram_rack | sdram_wack);
  253. // Romcopy interface
  254. wire [15:0] sdram_rom_wd;
  255. wire [24:1] sdram_rom_waddr;
  256. wire [ 1:0] sdram_rom_wrq;
  257. wire sdram_rom_wacc;
  258. sdram sdram (
  259. .rst_n ( rst_n ),
  260. .clk ( sdram_clk ), // Internal clock
  261. .out_clk ( sdram_out_clk ), // External clock (phase shifted)
  262. .sr_clk ( sr_clk ), // Output clock buffer
  263. .sr_cke ( sr_cke ),
  264. .sr_cs_n ( sr_cs_n ),
  265. .sr_ras_n ( sr_ras_n ),
  266. .sr_cas_n ( sr_cas_n ),
  267. .sr_we_n ( sr_we_n ),
  268. .sr_dqm ( sr_dqm ),
  269. .sr_ba ( sr_ba ),
  270. .sr_a ( sr_a ),
  271. .sr_dq ( sr_dq ),
  272. .a0 ( abc_sr_addr ),
  273. .rd0 ( abc_sr_rd ),
  274. .rrq0 ( abc_sr_rrq ),
  275. .rack0 ( abc_sr_rack ),
  276. .rready0 ( abc_sr_rready ),
  277. .wd0 ( abc_sr_wd ),
  278. .wrq0 ( abc_sr_wrq ),
  279. .wack0 ( abc_sr_wack ),
  280. .a1 ( cpu_mem_addr[24:2] ),
  281. .rd1 ( sdram_rd ),
  282. .rrq1 ( sdram_req & ~|cpu_mem_wstrb ),
  283. .rack1 ( sdram_rack ),
  284. .rready1 ( sdram_rready ),
  285. .wd1 ( cpu_mem_wdata ),
  286. .wstrb1 ( {4{sdram_req}} & cpu_mem_wstrb ),
  287. .wack1 ( sdram_wack ),
  288. .a2 ( sdram_rom_waddr ),
  289. .wd2 ( sdram_rom_wd ),
  290. .wrq2 ( sdram_rom_wrq ),
  291. .wacc2 ( sdram_rom_wacc )
  292. );
  293. //
  294. // ABC-bus interface
  295. //
  296. abcbus abcbus (
  297. .rst_n ( rst_n ),
  298. .sys_clk ( sys_clk ),
  299. .sdram_clk ( sdram_clk ),
  300. .stb_1mhz ( sys_clk_stb[6] ),
  301. .abc_valid ( iodev_valid_abc ),
  302. .map_valid ( iodev_valid_abcmemmap ),
  303. .cpu_addr ( cpu_mem_addr ),
  304. .cpu_wdata ( cpu_mem_wdata ),
  305. .cpu_wstrb ( cpu_mem_wstrb ),
  306. .cpu_rdata ( iodev_rdata_abc ),
  307. .cpu_rdata_map ( iodev_rdata_abcmemmap ),
  308. .irq ( iodev_irq_abc ),
  309. .abc_clk ( abc_clk ),
  310. .abc_a ( abc_a ),
  311. .abc_d ( abc_d ),
  312. .abc_d_oe ( abc_d_oe ),
  313. .abc_rst_n ( abc_rst_n ),
  314. .abc_cs_n ( abc_cs_n ),
  315. .abc_out_n ( abc_out_n ),
  316. .abc_inp_n ( abc_inp_n ),
  317. .abc_xmemfl_n ( abc_xmemfl_n ),
  318. .abc_xmemw800_n ( abc_xmemw800_n ),
  319. .abc_xmemw80_n ( abc_xmemw80_n ),
  320. .abc_xinpstb_n ( abc_xinpstb_n ),
  321. .abc_xoutpstb_n ( abc_xoutpstb_n ),
  322. .abc_rdy_x ( abc_rdy_x ),
  323. .abc_resin_x ( abc_resin_x ),
  324. .abc_int80_x ( abc_int80_x ),
  325. .abc_int800_x ( abc_int800_x ),
  326. .abc_nmi_x ( abc_nmi_x ),
  327. .abc_xm_x ( abc_xm_x ),
  328. .abc_master ( abc_master ),
  329. .abc_a_oe ( abc_a_oe ),
  330. .abc_d_ce_n ( abc_d_ce_n ),
  331. .exth_ha ( exth_ha ),
  332. .exth_hb ( exth_hb ),
  333. .exth_hc ( exth_hc ),
  334. .exth_hd ( exth_hd ),
  335. .exth_he ( exth_he ),
  336. .exth_hf ( exth_hf ),
  337. .exth_hg ( exth_hg ),
  338. .exth_hh ( exth_hh ),
  339. .sdram_addr ( abc_sr_addr ),
  340. .sdram_rd ( abc_sr_rd ),
  341. .sdram_rrq ( abc_sr_rrq ),
  342. .sdram_rack ( abc_sr_rack ),
  343. .sdram_rready ( abc_sr_rready ),
  344. .sdram_wd ( abc_sr_wd ),
  345. .sdram_wrq ( abc_sr_wrq ),
  346. .sdram_wack ( abc_sr_wack )
  347. );
  348. // GPIO
  349. assign gpio = 6'bzzzzzz;
  350. // Embedded RISC-V CPU
  351. parameter cpu_fast_mem_bits = 13; /* 2^[this] * 4 bytes */
  352. // Edge-triggered IRQs. picorv32 latches interrupts
  353. // but doesn't edge detect for a slow signal, so do it
  354. // here instead and use level triggered signalling to the
  355. // CPU.
  356. wire [31:0] cpu_eoi;
  357. reg [31:0] cpu_eoi_q;
  358. // sys_irq defined in iodevs.vh
  359. reg [31:0] sys_irq_q;
  360. reg [31:0] cpu_irq;
  361. // CPU permanently hung?
  362. wire cpu_trap;
  363. always @(negedge rst_n or posedge sys_clk)
  364. if (~rst_n)
  365. begin
  366. sys_irq_q <= 32'b0;
  367. cpu_eoi_q <= 32'b0;
  368. cpu_irq <= 32'b0;
  369. end
  370. else
  371. begin
  372. sys_irq_q <= sys_irq & irq_edge_mask;
  373. cpu_eoi_q <= cpu_eoi & irq_edge_mask;
  374. cpu_irq <= (sys_irq & ~sys_irq_q)
  375. | (cpu_irq & irq_edge_mask & ~(cpu_eoi & ~cpu_eoi_q));
  376. end
  377. picorv32 #(
  378. .ENABLE_COUNTERS ( 1 ),
  379. .ENABLE_COUNTERS64 ( 1 ),
  380. .ENABLE_REGS_16_31 ( 1 ),
  381. .ENABLE_REGS_DUALPORT ( 1 ),
  382. .LATCHED_MEM_RDATA ( 1 ),
  383. .BARREL_SHIFTER ( 1 ),
  384. .TWO_CYCLE_COMPARE ( 0 ),
  385. .TWO_CYCLE_ALU ( 0 ),
  386. .COMPRESSED_ISA ( 1 ),
  387. .CATCH_MISALIGN ( 1 ),
  388. .CATCH_ILLINSN ( 1 ),
  389. .ENABLE_FAST_MUL ( 1 ),
  390. .ENABLE_DIV ( 1 ),
  391. .ENABLE_IRQ ( 1 ),
  392. .ENABLE_IRQ_QREGS ( 1 ),
  393. .ENABLE_IRQ_TIMER ( 1 ),
  394. .MASKED_IRQ ( irq_masked ),
  395. .LATCHED_IRQ ( 32'h0000_0007 ),
  396. .REGS_INIT_ZERO ( 1 ),
  397. .STACKADDR ( 32'h4 << cpu_fast_mem_bits )
  398. )
  399. cpu (
  400. .clk ( sys_clk ),
  401. .resetn ( rst_n ),
  402. .trap ( cpu_trap ),
  403. .progaddr_reset ( 32'h0000_0000 ),
  404. .progaddr_irq ( 32'h0000_0040 ),
  405. .mem_instr ( cpu_mem_instr ),
  406. .mem_ready ( cpu_mem_ready ),
  407. .mem_valid ( cpu_mem_valid ),
  408. .mem_wstrb ( cpu_mem_wstrb ),
  409. .mem_addr ( cpu_mem_addr ),
  410. .mem_wdata ( cpu_mem_wdata ),
  411. .mem_rdata ( cpu_mem_rdata ),
  412. .mem_la_read ( cpu_la_read ),
  413. .mem_la_write ( cpu_la_write ),
  414. .mem_la_wdata ( cpu_la_wdata ),
  415. .mem_la_addr ( cpu_la_addr ),
  416. .mem_la_wstrb ( cpu_la_wstrb ),
  417. .irq ( cpu_irq ),
  418. .eoi ( cpu_eoi )
  419. );
  420. // cpu_mem_ready is always true for fast memory; for SDRAM we have to
  421. // wait either for a write ack or a low-high transition on the
  422. // read ready signal.
  423. reg sdram_rready_q;
  424. reg sdram_mem_ready;
  425. reg [31:0] sdram_rdata;
  426. always @(posedge sys_clk)
  427. begin
  428. sdram_rready_q <= sdram_rready;
  429. if (cpu_mem_quad[1])
  430. sdram_mem_ready <= sdram_mem_ready | sdram_wack |
  431. (sdram_rready & ~sdram_rready_q);
  432. else
  433. sdram_mem_ready <= 1'b0;
  434. sdram_rdata <= sdram_rd;
  435. end
  436. // Add a mandatory wait state to iodevs to reduce the size
  437. // of the CPU memory input MUX (it hurts timing on memory
  438. // accesses...)
  439. reg iodev_mem_ready;
  440. always @(*)
  441. case ( cpu_mem_quad )
  442. 4'b0000: cpu_mem_ready = 1'b0;
  443. 4'b0001: cpu_mem_ready = 1'b1;
  444. 4'b0010: cpu_mem_ready = sdram_mem_ready;
  445. 4'b0100: cpu_mem_ready = 1'b1;
  446. 4'b1000: cpu_mem_ready = iodev_mem_ready;
  447. default: cpu_mem_ready = 1'bx;
  448. endcase // case ( mem_quad )
  449. //
  450. // Fast memory. This runs on the SDRAM clock, i.e. 2x the speed
  451. // of the CPU. The .bits parameter gives the number of dwords
  452. // as a power of 2, i.e. 11 = 2^11 * 4 = 8K.
  453. //
  454. wire [31:0] fast_mem_rdata;
  455. fast_mem // #(.bits(cpu_fast_mem_bits), .mif("../fw/boot"))
  456. fast_mem(
  457. .rst_n ( rst_n ),
  458. .clk ( sys_clk ),
  459. .read ( cpu_la_read & cpu_la_addr[31:30] == 2'b00 ),
  460. .write ( cpu_la_write & cpu_la_addr[31:30] == 2'b00 ),
  461. .wstrb ( cpu_la_wstrb ),
  462. .addr ( cpu_la_addr[14:2] ),
  463. .wdata ( cpu_la_wdata ),
  464. .rdata ( fast_mem_rdata )
  465. );
  466. // Register I/O data to reduce the size of the read data MUX
  467. reg [31:0] iodev_rdata_q;
  468. // Read data MUX
  469. always @(*)
  470. case ( cpu_mem_quad )
  471. 4'b0001: cpu_mem_rdata = fast_mem_rdata;
  472. 4'b0010: cpu_mem_rdata = sdram_rdata;
  473. 4'b1000: cpu_mem_rdata = iodev_rdata_q;
  474. default: cpu_mem_rdata = 32'hxxxx_xxxx;
  475. endcase
  476. // Miscellaneous system control/status registers
  477. wire [ 4:0] sysreg_subreg = cpu_mem_addr[6:2];
  478. wire [31:0] sysreg = iodev_valid_sys << sysreg_subreg;
  479. tri1 [31:0] sysreg_rdata[0:31];
  480. assign iodev_rdata_sys = sysreg_rdata[sysreg_subreg];
  481. assign sysreg_rdata[0] = 32'h5058414d;
  482. assign sysreg_rdata[1] = { 31'b0, rtc_32khz_rework };
  483. // Hard system reset under program control
  484. assign reset_cmd =
  485. (sysreg[3] & cpu_mem_wstrb[0] & cpu_mem_wdata[0])
  486. | cpu_trap; // CPU hung
  487. // LED indication from the CPU
  488. reg [2:0] led_q;
  489. always @(negedge rst_n or posedge sys_clk)
  490. if (~rst_n)
  491. led_q <= 3'b000;
  492. else
  493. if ( sysreg[2] & cpu_mem_wstrb[0] )
  494. led_q <= cpu_mem_wdata[2:0];
  495. assign led = led_q;
  496. assign sysreg_rdata[2] = { 29'b0, led_q };
  497. //
  498. // Serial ROM (also configuration ROM.) Fast hardwired data download
  499. // unit to SDRAM.
  500. //
  501. wire rom_done;
  502. reg rom_done_q;
  503. spirom ddu (
  504. .rst_n ( rst_n ),
  505. .rom_clk ( flash_clk ),
  506. .ram_clk ( sdram_clk ),
  507. .spi_sck ( flash_sck ),
  508. .spi_io ( flash_io ),
  509. .spi_cs_n ( flash_cs_n ),
  510. .wd ( sdram_rom_wd ),
  511. .waddr ( sdram_rom_waddr ),
  512. .wrq ( sdram_rom_wrq ),
  513. .wacc ( sdram_rom_wacc ),
  514. .done ( rom_done )
  515. );
  516. always @(posedge sys_clk)
  517. rom_done_q <= rom_done;
  518. assign sysreg_rdata[4] = { 31'b0, rom_done_q };
  519. //
  520. // Serial port. Direct to the CP2102N for reworked
  521. // boards or to GPIO for non-reworked boards, depending on
  522. // whether DTR# is asserted on either.
  523. //
  524. // The GPIO numbering matches the order of pins for FT[2]232H.
  525. // gpio[0] - TxD
  526. // gpio[1] - RxD
  527. // gpio[2] - RTS#
  528. // gpio[3] - CTS#
  529. // gpio[4] - DTR#
  530. //
  531. wire tty_data_out; // Output data
  532. wire tty_data_in; // Input data
  533. wire tty_cts_out; // Assert CTS# externally
  534. wire tty_rts_in; // RTS# received from outside
  535. assign tty_cts_out = 1'b0; // Assert CTS#
  536. tty console (
  537. .rst_n ( rst_n ),
  538. .clk ( sys_clk ),
  539. .valid ( iodev_valid_console ),
  540. .wstrb ( cpu_mem_wstrb ),
  541. .wdata ( cpu_mem_wdata ),
  542. .rdata ( iodev_rdata_console ),
  543. .addr ( cpu_mem_addr[3:2] ),
  544. .irq ( iodev_irq_console ),
  545. .tty_txd ( tty_data_out ) // DTE -> DCE
  546. );
  547. reg [1:0] tty_dtr_q;
  548. always @(posedge sys_clk)
  549. begin
  550. tty_dtr_q[0] <= tty_dtr;
  551. tty_dtr_q[1] <= gpio[4];
  552. end
  553. //
  554. // Route data to the two output ports
  555. //
  556. // tty_rxd because pins are DCE named
  557. assign tty_data_in = (tty_txd | tty_dtr_q[0]) &
  558. (gpio[0] | tty_dtr_q[1]);
  559. assign tty_rxd = tty_dtr_q[0] ? 1'bz : tty_data_out;
  560. assign gpio[1] = tty_dtr_q[1] ? 1'bz : tty_data_out;
  561. assign tty_rts_in = (tty_rts | tty_dtr_q[0]) &
  562. (gpio[2] | tty_dtr_q[1]);
  563. assign tty_cts = tty_dtr_q[0] ? 1'bz : tty_cts_out;
  564. assign gpio[3] = tty_dtr_q[1] ? 1'bz : tty_cts_out;
  565. // SD card
  566. sdcard #(
  567. .with_irq_mask ( 8'b0000_0001 )
  568. )
  569. sdcard (
  570. .rst_n ( rst_n ),
  571. .clk ( sys_clk ),
  572. .sd_cs_n ( sd_dat[3] ),
  573. .sd_di ( sd_cmd ),
  574. .sd_sclk ( sd_clk ),
  575. .sd_do ( sd_dat[0] ),
  576. .sd_cd_n ( 1'b0 ),
  577. .sd_irq_n ( 1'b1 ),
  578. .wdata ( cpu_mem_wdata ),
  579. .rdata ( iodev_rdata_sdcard ),
  580. .valid ( iodev_valid_sdcard ),
  581. .wstrb ( cpu_mem_wstrb ),
  582. .addr ( cpu_mem_addr[6:2] ),
  583. .wait_n ( iodev_wait_n_sdcard ),
  584. .irq ( iodev_irq_sdcard )
  585. );
  586. assign sd_dat[2:1] = 2'bzz;
  587. // System local clock (not an RTC, but settable from one)
  588. // Also provides a periodic interrupt (set to 32 Hz)
  589. //
  590. // XXX: the RTC 32 kHz signal is missing a pull-up,
  591. // so unless the board has been reworked, use a
  592. // divider down from the 84 MHz system clock. The
  593. // error is about 200 ppm; a proper NCO could do better.
  594. reg ctr_32khz;
  595. reg [10:0] ctr_64khz;
  596. always @(posedge sys_clk)
  597. begin
  598. if (~|ctr_64khz)
  599. begin
  600. ctr_32khz <= ~ctr_32khz;
  601. ctr_64khz <= 11'd1280;
  602. end
  603. else
  604. ctr_64khz <= ctr_64khz - 1'b1;
  605. end
  606. // 32kHz clock synchronized with sys_clk
  607. wire clk_32kHz = rtc_32khz_rework ? rtc_32khz : ctr_32khz;
  608. sysclock #(.PERIODIC_HZ_LG2 ( 5 ))
  609. sysclock (
  610. .rst_n ( rst_n ),
  611. .sys_clk ( sys_clk ),
  612. .rtc_clk ( clk_32kHz ),
  613. .wdata ( cpu_mem_wdata ),
  614. .rdata ( iodev_rdata_sysclock ),
  615. .valid ( iodev_valid_sysclock ),
  616. .wstrb ( cpu_mem_wstrb ),
  617. .addr ( cpu_mem_addr[2] ),
  618. .periodic ( iodev_irq_sysclock )
  619. );
  620. // SPI bus to ESP32; using the sdcard IP as a SPI master for now at
  621. // least...
  622. `ifdef REALLY_ESP32
  623. // ESP32
  624. assign spi_cs_flash_n = 1'bz;
  625. assign esp_io0 = 1'b1; // If pulled down on reset, ESP32 will enter
  626. // firmware download mode
  627. sdcard #(
  628. .with_irq_mask ( 8'b0000_0101 ),
  629. .with_crc7 ( 1'b0 ),
  630. .with_crc16 ( 1'b0 )
  631. )
  632. esp (
  633. .rst_n ( rst_n ),
  634. .clk ( sys_clk ),
  635. .sd_cs_n ( spi_cs_esp_n ),
  636. .sd_di ( spi_mosi ),
  637. .sd_sclk ( spi_clk ),
  638. .sd_do ( spi_miso ),
  639. .sd_cd_n ( 1'b0 ),
  640. .sd_irq_n ( esp_int ),
  641. .wdata ( cpu_mem_wdata ),
  642. .rdata ( iodev_rdata_esp ),
  643. .valid ( iodev_valid_esp ),
  644. .wstrb ( cpu_mem_wstrb ),
  645. .addr ( cpu_mem_addr[6:2] ),
  646. .wait_n ( iodev_wait_n_esp ),
  647. .irq ( iodev_irq_esp )
  648. );
  649. `else // !`ifdef REALLY_ESP32
  650. reg [5:-13] esp_ctr; // 32768 * 2^-13 = 4 Hz
  651. always @(posedge clk_32kHz)
  652. esp_ctr <= esp_ctr + 1'b1;
  653. assign spi_clk = esp_ctr[0];
  654. assign spi_mosi = esp_ctr[1];
  655. assign spi_miso = esp_ctr[2];
  656. assign spi_cs_flash_n = esp_ctr[3]; // IO01
  657. assign spi_cs_esp_n = esp_ctr[4]; // IO10
  658. assign spi_int = esp_ctr[5]; // IO09
  659. assign esp_io0 = 1'b1;
  660. `endif
  661. //
  662. // I2C bus (RTC and to connector)
  663. //
  664. i2c i2c (
  665. .rst_n ( rst_n ),
  666. .clk ( sys_clk ),
  667. .valid ( iodev_valid_i2c ),
  668. .addr ( cpu_mem_addr[3:2] ),
  669. .wdata ( cpu_mem_wdata ),
  670. .wstrb ( cpu_mem_wstrb ),
  671. .rdata ( iodev_rdata_i2c ),
  672. .irq ( iodev_irq_i2c ),
  673. .i2c_scl ( i2c_scl ),
  674. .i2c_sda ( i2c_sda )
  675. );
  676. //
  677. // Registering of I/O data and handling of iodev_mem_ready
  678. //
  679. always @(posedge sys_clk)
  680. iodev_rdata_q <= iodev_rdata;
  681. always @(negedge rst_n or posedge sys_clk)
  682. if (~rst_n)
  683. iodev_mem_ready <= 1'b0;
  684. else
  685. iodev_mem_ready <= iodev_wait_n & cpu_mem_valid;
  686. endmodule