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

+74 -1
View File
@@ -548,7 +548,7 @@ for full speed.
| Addresses | Contents |
|---|---|
| 0000–7FFF | BASIC itself (writes here are ignored) |
| 8000–EFFF | BASIC's variables, your program and arrays, file buffers |
| 8000–EFFF | BASIC's variables, your program and arrays, file buffers (your program starts at 8482h) |
| F000–F3F7 | Free for your machine code (1016 bytes); BASIC never uses it |
| F3F8–F3FF | The clock set by `DATE` |
| F400–FFFF | The Z-80 stack |
@@ -575,6 +575,8 @@ kept free for it.
| 97 | — | enter the monitor (what `MONITOR` does) |
| 98–105 | memory read and breakpoint registers (used by the monitor) | |
| 112–120, 128–135 | sprites (section 7) | sprites (section 7) |
| 136 | keys held down, for games (below) | — |
| 137 | 1 if the key picked with `OUT 137` is held down, else 0 | pick a key by its scan code |
```
10 FOR I = 0 TO 255
@@ -590,6 +592,39 @@ A key check that doesn't wait (the key is used up):
100 IF INP(0) AND 1 THEN K = INP(1): PRINT "YOU PRESSED "; CHR$(K)
```
### Keys held down
Typed characters can't tell a game that a key is being *held*: they arrive
once, then repeat only after a pause. Ports 136 and 137 read the keyboard
directly instead, for as long as a key is down.
`INP(136)` adds up these values for the keys held now:
| Value | Keys |
|---|---|
| 1 | up: W, cursor up or keypad 8 |
| 2 | down: S, cursor down or keypad 2 |
| 4 | left: A, cursor left or keypad 4 |
| 8 | right: D, cursor right or keypad 6 |
| 16 | fire: Space or either Ctrl |
| 32 | Enter |
| 64 | either Shift |
| 128 | Esc |
```
10 J = INP(136)
20 IF J AND 4 THEN X = X - 1
30 IF J AND 8 THEN X = X + 1
40 IF J AND 16 THEN PRINT "BANG"
```
For any other key, `OUT 137` its PS/2 scan code (set 2; add 128 for keys whose
code starts with E0), then `INP(137)` is 1 while it is held: `OUT 137, 77:
IF INP(137) THEN …` checks P. The keys still type characters as well, so a
game should throw those away now and then with `IF INP(0) AND 1 THEN K =
INP(1)` in a loop. Held keys only work with the board's own keyboard, not with
characters sent from the PC.
### Speed
The Z-80 runs at 90 MHz and needs about 2.7 clocks per instruction, roughly 50
@@ -669,6 +704,7 @@ Type a command on the bottom line and press Enter. Numbers are in hex.
| Up / Down | Scroll the disassembly by one instruction |
| Page Up / Page Down | Scroll the memory window |
| `?` | A short reminder of the commands |
| `:` *record* | An Intel HEX record, written into memory (`tools/send-hex.py` sends whole files this way; see section 12) |
- **Stepping** runs exactly one instruction (a whole `LDIR`, or a `HALT` and
what wakes it). The hardware counts instructions, so it works on code in ROM
@@ -957,6 +993,18 @@ tools/get-bas.py --baud 9600 --cmd 'DIR' --cmd 'LOAD "SPIRO"' --cmd 'LIST'
seconds, 30 by default). Program lines typed this way get no `READY`, so they
take the full timeout each; use `send-bas.py` for those.
**Sending machine code compiled on the PC:**
```
tools/send-hex.py prog.ihx --save PROG --baud 3000000
```
This enters the monitor, sends the Intel HEX file (as SDCC and most
assemblers write it) a record at a time, saves the bytes as the memory file
`PROG`, and goes back to BASIC. `BLOAD "PROG"` then loads it where it was
compiled for. It must be at 8000h or above. That's how The Iron Keep
(section 14) gets onto the board.
When you're done, flip SW14 back down, and bring the getty back with
`sudo systemctl unmask --runtime serial-getty@ttyUSB1` (a reboot does it too).
Don't do that with SW14 up.
@@ -1038,6 +1086,31 @@ An error stops the program and reports the line, for example
bars, circles, a burst of lines, sine and cosine curves, random rectangles, a
bouncing ball and a Mandelbrot set, then prints how long it all took.
### The Iron Keep
A game in machine code. You are a prisoner in a castle of twelve rooms:
steal the king's crown from the throne room and escape down the stairs.
Guards patrol, and shoot when they can see you along a row or column of the
castle; some come after you. Keys open locked doors, chests hold gold, crates
hold ammunition. You have three lives.
```
RUN "KEEP"
```
- **Moving:** the cursor keys or W A S D. **Shooting:** Space or Ctrl, in the
direction you face.
- **P** pauses; **Esc** gives up the game. Esc on the title or the final
screen goes back to BASIC.
- **Score:** 100 a chest, 50 a guard, 1,000 for the crown, and 500 plus 200 a
remaining life for getting out.
`KEEP` is a four-line BASIC program that loads the game, the memory file
`KEEPGAME`, at 8600h, just past itself, and `CALL`s it. The game overwrites
the rest of BASIC's program area while it runs, so save your own program
first. It is written in C (`games/keep/` on the PC, with its build and
install steps).
## 15. Limits at a glance
| Item | Limit |