`timescale 1ns / 1ps // RISC-V computer: rv32_cpu, 80 KB of block RAM preloaded with BASIC, the // monitor's 32 KB, and the Z-80 computer's I/O ports as memory-mapped // registers. Runs on its own clock; the console crosses to the terminal's // clock with toggle handshakes (one byte in flight each way). // // Memory map: // 0000_0000-0001_3FFF RAM, 80 KB (BASIC). Writes below ROM_TOP are ignored // so a crashed program cannot damage BASIC's code. // 0002_0000-0002_7FFF the monitor's ROM (writes ignored). The NMI enters at // 0002_0000. // 0002_8000-0002_9FFF the monitor's RAM, for data only (it is distributed // RAM, answering a cycle later than block RAM; code // can't run from it). // FFFF_FE00-FFFF_FEFF 32-bit registers (below) // FFFF_FF00-FFFF_FFFF I/O port N at FFFF_FF00 + N, a byte each, the same // ports as the Z-80 machine. Being within 2 KB of the top // of memory, a port is one instruction from x0: // lbu a0, -256+N(zero) / sb a0, -256+N(zero). // // I/O ports (FFFF_FF00 + N): // 00 in bit 0 = key waiting, bit 1 = console can take a byte // 01 in key (clears bit 0) out byte to the terminal // 10 in millisecond counter bits 7:0; reading it latches bits 31:8 for 11-13 // 20 out 7-segment display, right two digits 21 out left two digits // 30 out graphics X bits 7:0 31 out X bit 8 32 out Y // 33 out plot colour at (X,Y), then X+1 in colour at (X,Y) // 34 out fill the whole graphics screen in bit 0 = fill busy // (also turns the sprites off and the scroll back to 0, unless // bit 7 is set) // 35 out scroll: buffer X shown at the left, bits 7:0 // 36 out bit 0 = scroll X bit 8; bit 1 = hide the graphics in a border (4 // columns each side, 2 rows top and bottom) to draw in unseen // 37 out scroll: buffer Y shown at the top (0-239) // 40 SPI flash control, 41 SPI flash data (see flash_spi.v) // 60 out bit 1 = stop again (NMI) after one instruction of the program // in why the last NMI came: 1 Pause key, 2 port 61, 3 step, // 4 breakpoint 0, 5 breakpoint 1 // 61 out enter the monitor (NMI) // 67 out bit 0 = breakpoint 0 on, bit 1 = breakpoint 1 on // 70-78, 80-87 sprites (see sprites.v) // 88 in keys held: bit 0 up (W, cursor or keypad 8), 1 down (S, 2), 2 left // (A, 4), 3 right (D, 6), 4 fire (Space or Ctrl), 5 Enter, 6 Shift, // 7 Esc // 89 out pick a scan code (set 2; add 128 for an E0 code) // in 1 if that key is held, else 0 // 90-9D sound (see sound.v) // The Z-80's debug-read ports (62-64) are gone: the monitor runs in the same // address space as the program. Its breakpoint addresses are 32 bits, below. // // 32-bit registers (FFFF_FE00 + N): // 00 in milliseconds since reset // 04 in microseconds since reset (wraps every 71 minutes) // 08 timer compare: the timer interrupt (mip.MTIP) is pending while the // microsecond counter is at or past it (by less than 2^31) // 10 breakpoint 0 address 14 breakpoint 1 address // // The monitor: an NMI (the Pause key, port 61, a breakpoint or a single step) // saves the program's pc in mnepc and jumps to the monitor ROM, which leaves // with mnret. NMIs wait while the program counter is in the monitor's ROM. // Breakpoints compare the address of each instruction as it starts, so the // instruction at a breakpoint has not run. module rv32_system #( parameter MEM_FILE = "basic.mem", // RAM 0000-FFFF parameter MEM_HI_FILE = "basic-hi.mem", // RAM 10000-13FFF parameter MON_FILE = "monitor.mem", parameter CPU_HZ = 90_000_000, parameter ROM_TOP = 32'h0000_A000 ) ( input clk, // CPU clock input rst, // synchronous to clk // keyboard side: a byte is offered when rx_req != rx_ack input rx_req, input [7:0] rx_data, output reg rx_ack, // screen side: a byte is offered when tx_req != tx_ack output reg tx_req, output reg [7:0] tx_data, input tx_ack, output reg [15:0] seg_val, output halted, input brk_key, // pulse: the Pause key, enter the monitor // graphics layer read port, on the video clock input vclk, input [16:0] gfx_vaddr, output reg [3:0] gfx_vpix, // (combinational from registers) output reg [8:0] scroll_x, output reg [7:0] scroll_y, output reg g_border, // sprites, on the video clock (see sprites.v) input sp_go, input [7:0] sp_line, input sp_buf, input [8:0] sp_x, input sp_on, output [3:0] sp_pix, input sp_ftick, // keys held down (one bit per scan code, from the keyboard's clock) input [255:0] keys, // configuration flash (SPI) output fl_cs_n, output fl_sck, output fl_mosi, input fl_miso, // sound, for a PmodAMP2 output audio ); localparam [31:0] MON_BASE = 32'h0002_0000; assign halted = 1'b0; // ------------------------------------------------------------------ CPU wire i_en, d_req, d_we, dbg_decode, retire, nmi_taken; wire [31:0] i_addr, d_addr, d_wdata, dbg_pc; wire [3:0] d_wstrb; reg [31:0] i_rdata, d_rdata; reg d_ack; wire nmi, timer_irq; reg step_arm; rv32_cpu #(.RESET_PC(32'h0), .NMI_VEC(MON_BASE)) cpu ( .clk(clk), .rst(rst), .i_en(i_en), .i_addr(i_addr), .i_rdata(i_rdata), .d_req(d_req), .d_we(d_we), .d_wstrb(d_wstrb), .d_addr(d_addr), .d_wdata(d_wdata), .d_rdata(d_rdata), .d_ack(d_ack), .irq_soft(1'b0), .irq_timer(timer_irq), .irq_ext(1'b0), .irq_local(16'd0), .nmi(nmi), .nmi_step(step_arm), .nmi_taken(nmi_taken), .dbg_pc(dbg_pc), .dbg_decode(dbg_decode), .retire(retire) ); // ------------------------------------------------------------- memories // Each block RAM: instruction fetch on port A; loads and stores on port B, // where a store is written the cycle after the request (its address // replacing the core's for that cycle). // RAM is two arrays (64 KB and 16 KB) so that it takes 20 block RAMs, not 32. wire d_main = d_addr[31:17] == 15'd0 && (!d_addr[16] || d_addr[15:14] == 2'd0); wire d_mon = d_addr[31:15] == MON_BASE[31:15]; wire d_mram = d_addr[31:13] == 19'h14; // 0002_8000-0002_9FFF wire d_io = d_addr[31:12] == 20'hFFFFF; integer b; (* ram_style = "block" *) reg [31:0] ram0 [0:16383]; // 0000_0000-0000_FFFF (* ram_style = "block" *) reg [31:0] ram1 [0:4095]; // 0001_0000-0001_3FFF initial $readmemh(MEM_FILE, ram0); initial $readmemh(MEM_HI_FILE, ram1); reg [31:0] ram0_iq, ram1_iq, ram0_dq, ram1_dq, w_d; reg [13:0] w_a; reg [3:0] w_s; reg w_r0, w_r1, d_hi; wire [13:0] r0_b = w_r0 ? w_a : d_addr[15:2]; wire [11:0] r1_b = w_r1 ? w_a[11:0] : d_addr[13:2]; always @(posedge clk) if (i_en) ram0_iq <= ram0[i_addr[15:2]]; always @(posedge clk) if (i_en) ram1_iq <= ram1[i_addr[13:2]]; always @(posedge clk) begin if (w_r0) for (b = 0; b < 4; b = b + 1) if (w_s[b]) ram0[r0_b][8*b +: 8] <= w_d[8*b +: 8]; ram0_dq <= ram0[r0_b]; end always @(posedge clk) begin if (w_r1) for (b = 0; b < 4; b = b + 1) if (w_s[b]) ram1[r1_b][8*b +: 8] <= w_d[8*b +: 8]; ram1_dq <= ram1[r1_b]; end (* ram_style = "block" *) reg [31:0] mrom [0:8191]; initial $readmemh(MON_FILE, mrom); reg [31:0] mon_iq, mon_dq; always @(posedge clk) if (i_en) mon_iq <= mrom[i_addr[14:2]]; always @(posedge clk) mon_dq <= mrom[d_addr[14:2]]; // the monitor's RAM: an access is registered, then the distributed RAM is // read (or written) in the next cycle (* ram_style = "distributed" *) reg [31:0] mram [0:2047]; reg [10:0] mr_a; reg mr_go, mr_we; reg [31:0] mr_q; always @(posedge clk) begin if (mr_go && mr_we) for (b = 0; b < 4; b = b + 1) if (w_s[b]) mram[mr_a][8*b +: 8] <= w_d[8*b +: 8]; mr_q <= mram[mr_a]; end // instruction fetch: anything outside the two memories reads as 0 (illegal) reg [1:0] i_sel; always @(posedge clk) if (i_en) i_sel <= i_addr[31:16] == 16'h0000 ? 2'd0 : i_addr[31:14] == 18'h4 ? 2'd1 : i_addr[31:15] == MON_BASE[31:15] ? 2'd2 : 2'd3; always @* case (i_sel) 2'd0: i_rdata = ram0_iq; 2'd1: i_rdata = ram1_iq; 2'd2: i_rdata = mon_iq; default: i_rdata = 32'd0; endcase // ----------------------------------------------------------- data bus reg [2:0] d_sel; // 0 nothing, 1 RAM, 2 monitor ROM, 3 I/O, 4 monitor RAM reg io_go, io_we; reg [11:0] io_a; reg [31:0] io_ww; // the store data as it came reg [7:0] io_wd; // the byte for a port reg [31:0] io_q; always @(posedge clk) begin w_r0 <= d_req && d_we && d_main && !d_addr[16] && d_addr >= ROM_TOP; w_r1 <= d_req && d_we && d_main && d_addr[16] && d_addr >= ROM_TOP; mr_go <= d_req && d_mram; mr_we <= d_we; mr_a <= d_addr[12:2]; w_a <= d_addr[15:2]; w_d <= d_wdata; w_s <= d_wstrb; io_go <= d_req && d_io; io_we <= d_we; io_a <= d_addr[11:0]; io_ww <= d_wdata; io_wd <= d_wdata >> {d_addr[1:0], 3'b000}; if (d_req) begin d_sel <= d_main ? 3'd1 : d_mon ? 3'd2 : d_io ? 3'd3 : d_mram ? 3'd4 : 3'd0; d_hi <= d_addr[16]; end // block RAM answers the next cycle, I/O and the monitor's RAM the one after d_ack <= (d_req && !d_io && !d_mram) || io_go || mr_go; if (rst) begin io_go <= 1'b0; d_ack <= 1'b0; w_r0 <= 1'b0; w_r1 <= 1'b0; mr_go <= 1'b0; end end always @* case (d_sel) 3'd1: d_rdata = d_hi ? ram1_dq : ram0_dq; 3'd2: d_rdata = mon_dq; 3'd3: d_rdata = io_q; 3'd4: d_rdata = mr_q; default: d_rdata = 32'd0; endcase // the port strobes, a cycle after the request wire io_bytes = io_a[11:8] == 4'hF; wire io_wr = io_go && io_we && io_bytes; wire io_rd = io_go && !io_we && io_bytes; wire [7:0] io_addr = io_a[7:0]; wire wr_word = io_go && io_we && io_a[11:8] == 4'hE; reg [7:0] io_din; reg [31:0] word_q; // ------------------------------------------------------------- console (* ASYNC_REG = "TRUE" *) reg [1:0] rx_req_s, tx_ack_s; always @(posedge clk) begin rx_req_s <= {rx_req_s[0], rx_req}; tx_ack_s <= {tx_ack_s[0], tx_ack}; end wire rx_avail = rx_req_s[1] != rx_ack; wire tx_ready = tx_ack_s[1] == tx_req; // ------------------------------------------------------------ graphics // 76800 pixels: a 64K array and an 11264 one, so they take about 9.5 block // RAMs (one array would be built as 96K deep, 12 of them). localparam GFX_PIXELS = 320 * 240; (* ram_style = "block" *) reg [3:0] gfx_lo [0:65535]; (* ram_style = "block" *) reg [3:0] gfx_hi [0:GFX_PIXELS - 65536 - 1]; integer gi; initial begin for (gi = 0; gi < 65536; gi = gi + 1) gfx_lo[gi] = 4'd0; for (gi = 0; gi < GFX_PIXELS - 65536; gi = gi + 1) gfx_hi[gi] = 4'd0; end reg [8:0] gx; reg [7:0] gy; reg [3:0] gfx_q_lo, gfx_q_hi, fill_c; reg gfx_q_sel; reg [16:0] fill_a; reg fill_busy; wire [16:0] gxy = {gy, 8'd0} + {2'd0, gy, 6'd0} + {8'd0, gx}; wire plot = io_wr && io_addr == 8'h33 && gx < 9'd320 && gy < 8'd240; wire [16:0] g_addr = fill_busy ? fill_a : gxy; wire g_we = fill_busy || plot; wire [3:0] g_wd = fill_busy ? fill_c : io_wd[3:0]; wire [3:0] gfx_q = gfx_q_sel ? gfx_q_hi : gfx_q_lo; always @(posedge clk) begin if (g_we && !g_addr[16]) gfx_lo[g_addr[15:0]] <= g_wd; gfx_q_lo <= gfx_lo[g_addr[15:0]]; end always @(posedge clk) begin if (g_we && g_addr[16]) gfx_hi[g_addr[13:0]] <= g_wd; gfx_q_hi <= gfx_hi[g_addr[13:0]]; gfx_q_sel <= g_addr[16]; end reg [3:0] gv_lo, gv_hi; reg gv_sel; always @(posedge vclk) begin gv_lo <= gfx_lo[gfx_vaddr[15:0]]; gv_hi <= gfx_hi[gfx_vaddr[13:0]]; gv_sel <= gfx_vaddr[16]; end always @* gfx_vpix = gv_sel ? gv_hi : gv_lo; // whole-array view for testbenches function [3:0] gfx(input [16:0] n); gfx = n[16] ? gfx_hi[n[13:0]] : gfx_lo[n[15:0]]; endfunction always @(posedge clk) if (rst) begin fill_busy <= 1'b0; gx <= 9'd0; gy <= 8'd0; scroll_x <= 9'd0; scroll_y <= 8'd0; g_border <= 1'b0; end else begin if (io_wr) case (io_addr) 8'h30: gx[7:0] <= io_wd; 8'h31: gx[8] <= io_wd[0]; 8'h32: gy <= io_wd; 8'h33: gx <= gx + 1'b1; 8'h34: begin fill_busy <= 1'b1; fill_a <= 17'd0; fill_c <= io_wd[3:0]; if (!io_wd[7]) begin scroll_x <= 9'd0; scroll_y <= 8'd0; g_border <= 1'b0; end end 8'h35: scroll_x[7:0] <= io_wd; 8'h36: begin scroll_x[8] <= io_wd[0]; g_border <= io_wd[1]; end 8'h37: scroll_y <= io_wd >= 8'd240 ? io_wd - 8'd240 : io_wd; default: ; endcase if (fill_busy) begin fill_a <= fill_a + 1'b1; if (fill_a == GFX_PIXELS - 1) fill_busy <= 1'b0; end end // ------------------------------------------------------------- sprites wire [7:0] spr_q; sprites spr ( .clk(clk), .rst(rst), .io_wr(io_wr), .io_port(io_addr), .io_wd(io_wd), .io_q(spr_q), .vclk(vclk), .go(sp_go), .line(sp_line), .rd_buf(sp_buf), .rd_x(sp_x), .rd_on(sp_on), .pix(sp_pix), .gfx_pix(gfx_vpix), .ftick(sp_ftick) ); // ---------------------------------------------------------------- sound wire [7:0] snd_q; sound #(.CPU_HZ(CPU_HZ)) snd ( .clk(clk), .rst(rst), .hush(io_wr && io_addr == 8'h34 && !io_wd[7]), .io_wr(io_wr), .io_port(io_addr), .io_wd(io_wd), .io_q(snd_q), .audio(audio) ); // ---------------------------------------------------------- held keys // The keyboard's clock writes these; a bit that changes while it is read // just reads as before or after. reg [7:0] key_sel = 8'd0; always @(posedge clk) if (io_wr && io_addr == 8'h89) key_sel <= io_wd; wire [7:0] joy = { keys[8'h76], // Esc keys[8'h12] | keys[8'h59], // Shift keys[8'h5A] | keys[8'hDA], // Enter keys[8'h29] | keys[8'h14] | keys[8'h94], // Space, Ctrl keys[8'h23] | keys[8'hF4] | keys[8'h74], // D, right, keypad 6 keys[8'h1C] | keys[8'hEB] | keys[8'h6B], // A, left, keypad 4 keys[8'h1B] | keys[8'hF2] | keys[8'h72], // S, down, keypad 2 keys[8'h1D] | keys[8'hF5] | keys[8'h75] // W, up, keypad 8 }; // --------------------------------------------------------------- flash wire [7:0] fl_ctl_q, fl_dat_q; flash_spi flash ( .clk(clk), .rst(rst), .wr_ctl(io_wr && io_addr == 8'h40), .wr_dat(io_wr && io_addr == 8'h41), .wdata(io_wd), .ctl_q(fl_ctl_q), .dat_q(fl_dat_q), .cs_n(fl_cs_n), .sck(fl_sck), .mosi(fl_mosi), .miso(fl_miso) ); // ------------------------------------------------- milli/microseconds localparam MS_DIV = CPU_HZ / 1000; localparam US_DIV = CPU_HZ / 1000000; reg [31:0] ms, ms_latch, us, tcmp; reg [17:0] ms_div; reg [7:0] us_div; always @(posedge clk) if (rst) begin ms <= 0; ms_div <= 0; us <= 0; us_div <= 0; tcmp <= 32'h8000_0000; end else begin if (ms_div == MS_DIV - 1) begin ms <= ms + 1'b1; ms_div <= 0; end else ms_div <= ms_div + 1'b1; if (us_div == US_DIV - 1) begin us <= us + 1'b1; us_div <= 0; end else us_div <= us_div + 1'b1; if (wr_word && io_a[7:0] == 8'h08) tcmp <= io_ww; end wire [31:0] t_left = us - tcmp; reg timer_r; always @(posedge clk) timer_r <= !t_left[31]; assign timer_irq = timer_r; // ------------------------------------------------------------- monitor // An NMI request waits (in nmi_r) until the core takes it; breakpoint hits // go straight to the core as their instruction starts. reg [31:0] bp0, bp1; reg [1:0] bp_on, hit_d; reg [2:0] why, why_r; reg nmi_r, in_mon; wire hit0 = bp_on[0] && dbg_decode && dbg_pc == bp0; wire hit1 = bp_on[1] && dbg_decode && dbg_pc == bp1; assign nmi = nmi_r || hit0 || hit1; always @(posedge clk) begin in_mon <= dbg_pc[31:15] == MON_BASE[31:15]; hit_d <= {hit1, hit0}; if (rst) begin nmi_r <= 1'b0; step_arm <= 1'b0; bp_on <= 2'b00; why <= 3'd0; bp0 <= 32'd0; bp1 <= 32'd0; end else begin if (io_wr) case (io_addr) 8'h60: step_arm <= io_wd[1]; 8'h67: bp_on <= io_wd[1:0]; default: ; endcase if (wr_word && io_a[7:0] == 8'h10) bp0 <= io_ww; if (wr_word && io_a[7:0] == 8'h14) bp1 <= io_ww; if (!nmi_r && !in_mon) begin if (brk_key) begin nmi_r <= 1'b1; why_r <= 3'd1; end else if (io_wr && io_addr == 8'h61) begin nmi_r <= 1'b1; why_r <= 3'd2; end end if (nmi_taken) begin why <= nmi_r ? why_r : hit_d[0] ? 3'd4 : hit_d[1] ? 3'd5 : 3'd3; nmi_r <= 1'b0; step_arm <= 1'b0; end end end // -------------------------------------------------------- port reads always @* case (io_addr) 8'h00: io_din = {6'd0, tx_ready, rx_avail}; 8'h01: io_din = rx_data; 8'h10: io_din = ms[7:0]; // sampled on the same edge that fills ms_latch 8'h11: io_din = ms_latch[15:8]; 8'h12: io_din = ms_latch[23:16]; 8'h13: io_din = ms_latch[31:24]; 8'h33: io_din = {4'd0, gfx_q}; 8'h34: io_din = {7'd0, fill_busy}; 8'h40: io_din = fl_ctl_q; 8'h41: io_din = fl_dat_q; 8'h60: io_din = {5'd0, why}; 8'h78, 8'h80, 8'h81, 8'h82, 8'h83, 8'h84, 8'h85, 8'h86, 8'h87: io_din = spr_q; 8'h88: io_din = joy; 8'h89: io_din = {7'd0, keys[key_sel]}; 8'h98, 8'h99, 8'h9A: io_din = snd_q; default: io_din = 8'hFF; endcase always @* case (io_a[7:0]) 8'h00: word_q = ms; 8'h04: word_q = us; 8'h08: word_q = tcmp; 8'h10: word_q = bp0; 8'h14: word_q = bp1; default: word_q = 32'd0; endcase always @(posedge clk) begin if (io_go) io_q <= io_bytes ? {24'd0, io_din} << {io_a[1:0], 3'b000} : word_q; if (rst) begin rx_ack <= rx_req_s[1]; tx_req <= tx_ack_s[1]; seg_val <= 16'h0032; ms_latch <= 32'd0; end else begin if (io_rd && io_addr == 8'h01 && rx_avail) rx_ack <= ~rx_ack; if (io_rd && io_addr == 8'h10) ms_latch <= ms; if (io_wr && io_addr == 8'h01 && tx_ready) begin tx_data <= io_wd; tx_req <= ~tx_req; end if (io_wr && io_addr == 8'h20) seg_val[7:0] <= io_wd; if (io_wr && io_addr == 8'h21) seg_val[15:8] <= io_wd; end end endmodule