`timescale 1ns / 1ps // Z-80 computer: CPU, 64 KB of block RAM preloaded with BASIC, console and // timer ports. Runs on its own clock; the console crosses to the terminal's // clock with toggle handshakes (one byte in flight each way). // // I/O ports (low address byte): // 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 // // Graphics are 320x240 in 16 colours (0 = transparent), shown over the text. // Writes below ROM_TOP are ignored so a crashed program cannot damage BASIC. module z80_system #( parameter MEM_FILE = "basic.mem", parameter CPU_HZ = 100_000_000, parameter ROM_TOP = 16'h8000 ) ( 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, // graphics layer read port, on the video clock input vclk, input [16:0] gfx_vaddr, output reg [3:0] gfx_vpix ); wire [15:0] ma, io_addr; wire we, io_rd, io_wr; wire [7:0] wd, io_wd; reg [7:0] din, io_din; z80_cpu cpu ( .clk(clk), .rst(rst), .ma(ma), .we(we), .wd(wd), .din(din), .io_addr(io_addr), .io_rd(io_rd), .io_wr(io_wr), .io_wd(io_wd), .io_din(io_din), .halted(halted), .ins_start(), .dbg_pc(), .dbg_af(), .dbg_bc(), .dbg_de(), .dbg_hl(), .dbg_ix(), .dbg_iy(), .dbg_sp() ); reg [7:0] ram [0:65535]; initial $readmemh(MEM_FILE, ram); always @(posedge clk) begin if (we && ma >= ROM_TOP) ram[ma] <= wd; din <= ram[ma]; end // ------------------------------------------------------------- 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 localparam GFX_PIXELS = 320 * 240; (* ram_style = "block" *) reg [3:0] gfx [0:GFX_PIXELS - 1]; integer gi; initial for (gi = 0; gi < GFX_PIXELS; gi = gi + 1) gfx[gi] = 4'd0; reg [8:0] gx; reg [7:0] gy; reg [3:0] gfx_q, fill_c; 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[7:0] == 8'h33 && gx < 9'd320 && gy < 8'd240; wire [16:0] g_addr = fill_busy ? fill_a : gxy; always @(posedge clk) begin if (fill_busy || plot) gfx[g_addr] <= fill_busy ? fill_c : io_wd[3:0]; gfx_q <= gfx[g_addr]; end always @(posedge vclk) gfx_vpix <= gfx[gfx_vaddr]; always @(posedge clk) if (rst) begin fill_busy <= 1'b0; gx <= 9'd0; gy <= 8'd0; end else begin if (io_wr) case (io_addr[7:0]) 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]; end default: ; endcase if (fill_busy) begin fill_a <= fill_a + 1'b1; if (fill_a == GFX_PIXELS - 1) fill_busy <= 1'b0; end end // millisecond timer localparam MS_DIV = CPU_HZ / 1000; reg [31:0] ms, ms_latch; reg [17:0] ms_div; always @(posedge clk) if (rst) begin ms <= 0; ms_div <= 0; end else if (ms_div == MS_DIV - 1) begin ms <= ms + 1'b1; ms_div <= 0; end else ms_div <= ms_div + 1'b1; always @* case (io_addr[7:0]) 8'h00: io_din = {6'd0, tx_ready, rx_avail}; 8'h01: io_din = rx_data; 8'h10: io_din = ms_latch[7:0]; 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}; default: io_din = 8'hFF; endcase always @(posedge clk) if (rst) begin rx_ack <= rx_req_s[1]; tx_req <= tx_ack_s[1]; seg_val <= 16'h2080; end else begin if (io_rd && io_addr[7:0] == 8'h01 && rx_avail) rx_ack <= ~rx_ack; if (io_rd && io_addr[7:0] == 8'h10) ms_latch <= ms; if (io_wr && io_addr[7:0] == 8'h01 && tx_ready) begin tx_data <= io_wd; tx_req <= ~tx_req; end if (io_wr && io_addr[7:0] == 8'h20) seg_val[7:0] <= io_wd; if (io_wr && io_addr[7:0] == 8'h21) seg_val[15:8] <= io_wd; end endmodule