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>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-29 09:21:02 -08:00
1 parent 13373244e8
commit 743071cff3
19 files changed
+3544 -1306

No files matched your search

+7 -1
View File
@@ -117,6 +117,12 @@ module basys3_terminal #(
.ascii(key), .ascii_valid(key_valid), .caps(caps), .cad(cad)
);
// keys held down, for the Z-80's game ports (nothing is held outside Z-80 mode)
wire [255:0] keys_down;
ps2_keystate kbd_state (
.clk(clk), .rst(rst | ~z80_mode), .code(scan), .code_valid(scan_valid), .down(keys_down)
);
// Ctrl-Alt-Del in Z-80 mode restarts the Z-80, like BTND (held 1 us)
reg [6:0] cad_t = 7'd0;
always @(posedge clk) cad_t <= cad && z80_mode ? 7'd100 : cad_t - (cad_t != 0);
@@ -338,7 +344,7 @@ module basys3_terminal #(
.seg_val(z80_seg), .halted(), .brk_key(brk_s[2] ^ brk_s[1]),
.vclk(pix_clk), .gfx_vaddr(gfx_addr), .gfx_vpix(gfx_pix),
.sp_go(sp_go), .sp_line(sp_line), .sp_buf(sp_buf), .sp_x(sp_x), .sp_on(sp_on),
.sp_pix(sp_pix), .sp_ftick(pix_frame),
.sp_pix(sp_pix), .sp_ftick(pix_frame), .keys(keys_down),
.fl_cs_n(QspiCSn), .fl_sck(fl_sck), .fl_mosi(QspiDB[0]), .fl_miso(QspiDB[1])
);
+29
View File
@@ -237,3 +237,32 @@ module ps2_keymap (
end
end
endmodule
// Which keys are held down, for games: one bit per set-2 scan code, 128 plain
// codes ({0, code}) and 128 after the E0 prefix ({1, code}). The Pause key's
// E1 sequence is skipped.
module ps2_keystate (
input clk,
input rst,
input [7:0] code,
input code_valid,
output reg [255:0] down
);
reg ext, brk, pause;
reg [2:0] skip;
always @(posedge clk)
if (rst) begin
down <= 256'd0;
ext <= 1'b0; brk <= 1'b0; skip <= 3'd0;
end else if (code_valid) begin
if (skip != 3'd0) skip <= skip - 1'b1; // rest of E1 14 77 E1 F0 14 F0 77
else if (code == 8'hE1) skip <= 3'd7;
else if (code == 8'hE0) ext <= 1'b1;
else if (code == 8'hF0) brk <= 1'b1;
else begin
if (!code[7]) down[{ext, code[6:0]}] <= !brk;
ext <= 1'b0;
brk <= 1'b0;
end
end
endmodule
+25
View File
@@ -24,6 +24,11 @@
// 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
@@ -66,6 +71,8 @@ module z80_system #(
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,
@@ -164,6 +171,22 @@ module z80_system #(
.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 (
@@ -256,6 +279,8 @@ module z80_system #(
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