Monitor: Z-80 assembler (ASM "file", A addr)

Two-pass table-driven assembler in the monitor: assembles BASIC-saved
source files from the file system, or one line at a time with A addr.
Labels, EQU/ORG/DB/DW/DS/END, expressions. Table generated from the
disassembler (tools/gen-asmtab.py); round-trip and sdasz80 tests, plus a
Z-80 build run in z80emu. Monitor RAM grows to 6000-7FFF for the symbol
table. Works around an SDCC 4.5 inline strcpy bug with s_copy().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-28 16:42:42 -08:00
1 parent 88b5942da5
commit fe1daf180d
16 files changed
+14320 -13257

No files matched your search

+20 -3
View File
@@ -292,14 +292,16 @@ Commands: Enter steps one instruction, `S n` steps *n*, `O` steps over a call,
`G`/`X` carries on, `B addr` sets the breakpoint, `D`/`M addr` move the windows,
`E addr bytes` writes memory, `R HL=1234` sets a register, and
`SAVE "NAME" start end`, `LOAD "NAME" [addr]` and `DIR` move memory to and
from files in flash (the same files as BASIC's `BSAVE`/`BLOAD`). The manual's
from files in flash (the same files as BASIC's `BSAVE`/`BLOAD`). `ASM "NAME"`
assembles Z-80 source (typed into BASIC as numbered lines and saved) into
memory, and `A addr` assembles a line at a time. The manual's
section 10 has the details.
How it works:
- **Entry:** the NMI pushes PC and jumps to 0066h. Both BASIC's ROM and the
monitor's have the same trampoline there (`basic/tramp.s`). It saves AF,
switches in the monitor's 32 KB bank (`monitor/`, ROM 0000-6FFF and RAM
7000-7FFF, replacing 0000-7FFF) and enters it.
switches in the monitor's 32 KB bank (`monitor/`, ROM 0000-5FFF and RAM
6000-7FFF, replacing 0000-7FFF) and enters it.
- **Reading memory:** the monitor reads the program's memory below 8000h
through a debug port (62h-64h).
- **Leaving:** through 0070h, which switches the bank out and does `POP AF`
@@ -309,6 +311,18 @@ How it works:
exactly one instruction runs.
- **Breakpoints:** two hardware comparators (the user's and a temporary one
for step-over) watch each instruction's address. Both work in ROM.
- **The assembler** (`monitor/asm.c`): two passes with labels,
expressions and `ORG`/`EQU`/`DB`/`DW`/`DS`/`END`. Its opcode table
(`monitor/asmtab.h`, 912 forms) is generated from the disassembler by
`tools/gen-asmtab.py`, so the two agree.
- `monitor/test-asm.py` assembles every disassembled form back: 913 come
back exactly, 468 duplicates give the documented encoding.
- `monitor/test-asm-z80.sh` runs the same lines through the Z-80 build in
the emulator and compares the output with the PC build.
- A test program matched sdasz80 byte for byte.
- SDCC 4.5 can lose a value held in A around an inline `strcpy`, which gave
the assembler a bad pointer. The monitor copies strings with its own
function instead.
- **The disassembler:** it covers every opcode. `monitor/test-disasm.py`
disassembles all 1,784 forms and assembles each back with `sdasz80`. The
664 documented forms come back byte for byte. The 455 undocumented
@@ -318,6 +332,9 @@ How it works:
```bash
monitor/build.sh # -> monitor/monitor.mem, loaded by the bitstream build
monitor/test-disasm.py # check the disassembler (needs gcc and sdasz80)
monitor/test-asm.py # check the assembler against the disassembler
monitor/test-asm-z80.sh # the same, for the Z-80 build (about 2 minutes)
tools/gen-asmtab.py # regenerate monitor/asmtab.h after changing the disassembler
```
### The Z-80