Files
Basys3Terminal/rtl/z80_system.v
T
brad ArantandClaude Opus 5.5 743071cff3 The Iron Keep: a top-down escape game, held-key ports, monitor HEX loader
games/keep: an original game in C for the Z-80 using the sprites - twelve
rooms, guards that patrol, see along rows and columns and shoot, keys and
locked doors, gold, ammunition and a crown to steal. gen.py holds the art
and castle map and checks reachability; test-keep.py runs it in z80emu on
a model of the board's ports, takes screenshots and has a bot play it
through. keep.bas loads KEEPGAME at 8600h and CALLs it.

Ports 88h/89h read keys held down (ps2_keystate), the monitor writes
Intel HEX records typed at its prompt, and tools/send-hex.py sends a
whole file and saves it as a memory file. Testbench covers both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 09:21:02 -08:00

304 lines
12 KiB
Verilog

`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
// (also turns the sprites off unless bit 7 is set)
// 40 SPI flash control, 41 SPI flash data (see flash_spi.v)
// 60 out bit 0 = monitor bank in; bit 1 (with bit 0 = 0) = stop again after
// one instruction of the program
// in why the monitor was entered: 1 Pause key, 2 port 61, 3 step,
// 4 breakpoint 0, 5 breakpoint 1
// 61 out enter the monitor (NMI)
// 62 out debug address low 63 out debug address high
// 64 in main-memory byte at the debug address, then address + 1
// 65 out breakpoint 0 low 66 high 68 out breakpoint 1 low 69 high
// 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
//
// The monitor: an NMI (Pause key, port 61, a breakpoint or the step counter)
// pushes the program's PC and jumps to 0066h, where both BASIC and the monitor
// ROM hold the same few bytes: they switch the monitor bank in, which replaces
// 0000-7FFF with the monitor's ROM (0000-5FFF) and RAM (6000-7FFF), and leave
// through 0070h, which switches it out again and returns. No NMI is taken while
// the bank is in, or between the request and the bank coming in.
//
// 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 MON_FILE = "monitor.mem",
parameter MON_RAM = 16'h6000,
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,
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,
// 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
);
wire [15:0] ma, io_addr, pc_dbg;
wire we, io_rd, io_wr, ins_start;
wire nmi, nmi_now;
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), .nmi(nmi), .nmi_now(nmi_now), .ins_start(ins_start), .dbg_pc(pc_dbg),
.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);
reg [7:0] ram_q, mon_q, dbg_q;
reg [15:0] dbg_a;
reg mon, sel_mon;
always @(posedge clk) begin
if (we && ma >= ROM_TOP && !(mon && !ma[15])) ram[ma] <= wd;
ram_q <= ram[ma];
dbg_q <= ram[dbg_a];
end
// the monitor bank, 0000-7FFF while mon is set
(* ram_style = "block" *) reg [7:0] mrom [0:32767];
initial $readmemh(MON_FILE, mrom);
always @(posedge clk) begin
if (we && mon && !ma[15] && ma >= MON_RAM) mrom[ma[14:0]] <= wd;
mon_q <= mrom[ma[14:0]];
sel_mon <= mon && !ma[15];
end
always @* din = sel_mon ? mon_q : ram_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
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
// ------------------------------------------------------------- sprites
wire [7:0] spr_q;
sprites spr (
.clk(clk), .rst(rst),
.io_wr(io_wr), .io_port(io_addr[7:0]), .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)
);
// ---------------------------------------------------------- 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[7:0] == 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[7:0] == 8'h40), .wr_dat(io_wr && io_addr[7:0] == 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)
);
// 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;
// ------------------------------------------------------------- monitor
reg [15:0] bp0, bp1; // breakpoint addresses
reg [1:0] bp_on;
reg [2:0] why;
reg nmi_busy, step_arm, nmi_r, io_rd_d;
reg [1:0] step_n;
wire can_nmi = !mon && !nmi_busy;
wire hit0 = bp_on[0] && pc_dbg == bp0;
wire hit1 = bp_on[1] && pc_dbg == bp1;
assign nmi_now = can_nmi && ins_start && (hit0 || hit1);
assign nmi = nmi_r;
always @(posedge clk) begin
nmi_r <= 1'b0;
io_rd_d <= io_rd && io_addr[7:0] == 8'h64;
if (io_rd_d) dbg_a <= dbg_a + 1'b1;
if (rst) begin
mon <= 1'b0; nmi_busy <= 1'b0; step_arm <= 1'b0; bp_on <= 2'b00; why <= 3'd0;
end else begin
if (io_wr) case (io_addr[7:0])
8'h60: begin
mon <= io_wd[0];
if (io_wd[0]) nmi_busy <= 1'b0;
step_arm <= !io_wd[0] && io_wd[1];
step_n <= ins_start ? 2'd1 : 2'd0; // POP AF may start on this clock
end
8'h62: dbg_a[7:0] <= io_wd;
8'h63: dbg_a[15:8] <= io_wd;
8'h65: bp0[7:0] <= io_wd;
8'h66: bp0[15:8] <= io_wd;
8'h68: bp1[7:0] <= io_wd;
8'h69: bp1[15:8] <= io_wd;
8'h67: bp_on <= io_wd[1:0];
default: ;
endcase
// step: the exit path runs POP AF and RETN, then one instruction
// of the program; ask for the NMI as that one starts
if (step_arm && ins_start) begin
step_n <= step_n + 1'b1;
if (step_n == 2'd2) begin
step_arm <= 1'b0;
nmi_r <= 1'b1; nmi_busy <= 1'b1; why <= 3'd3;
end
end
if (can_nmi && !nmi_r) begin
if (nmi_now) begin
nmi_busy <= 1'b1; why <= hit0 ? 3'd4 : 3'd5;
end else if (brk_key) begin
nmi_r <= 1'b1; nmi_busy <= 1'b1; why <= 3'd1;
end else if (io_wr && io_addr[7:0] == 8'h61) begin
nmi_r <= 1'b1; nmi_busy <= 1'b1; why <= 3'd2;
end
end
end
end
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[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'h64: io_din = dbg_q;
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]};
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;
ms_latch <= 32'd0;
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