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

+41
View File
@@ -0,0 +1,41 @@
#!/usr/bin/env python3
"""Check the monitor's assembler against its disassembler: disassemble every
opcode form, assemble each line back (host build of monitor.c) and compare
the bytes. Undocumented duplicates come back as their documented encoding;
the three-operand DDCB register copies aren't supported.
monitor/test-asm.py
"""
import os
import subprocess
import sys
import tempfile
here = os.path.dirname(os.path.abspath(__file__))
exe = os.path.join(tempfile.mkdtemp(), "mon")
subprocess.check_call(["gcc", "-O1", "-w", "-o", exe, os.path.join(here, "monitor.c")])
forms = [l.split("\t") for l in subprocess.check_output([exe], text=True).splitlines()]
forms = [(h, t) for h, t in forms if t != "NOP*" and not t.startswith("DB ")]
out = subprocess.run([exe, "line"], input="\n".join(t for _, t in forms) + "\n",
capture_output=True, text=True).stdout.splitlines()
first = {}
for hexb, text in forms:
first.setdefault(text, hexb) # the documented encoding comes first
ok = alias = copies = bad = 0
for (hexb, text), got in zip(forms, out):
if got == hexb:
ok += 1
elif text.count(",") == 2: # RES 0,(IX+d),B and friends
copies += 1
elif got == first[text]: # a duplicate of an earlier form
alias += 1
elif hexb[:2] in ("DD", "FD") and text.split()[0] in ("JR", "DJNZ") and \
got == hexb[2:4] + "%02X" % (int(hexb[4:6], 16) + 1):
alias += 1 # a dead prefix made the jump a byte longer
else:
bad += 1
print("FAIL %-10s %-24s -> %s" % (hexb, text, got))
print("%d exact, %d duplicates assembled to the documented encoding, %d DDCB copies skipped, %d failures"
% (ok, alias, copies, bad))
sys.exit(1 if bad else 0)