2
0

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