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:
1 parent
88b5942da5
commit
fe1daf180d
16 files changed
+14320
-13257
No files matched your search
Executable
+78
@@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Generate monitor/asmtab.h, the monitor assembler's opcode table, from the
|
||||
monitor's own disassembler (host build, symbolic mode): every opcode form,
|
||||
its mnemonic and operand pattern. The first encoding of each pattern is kept,
|
||||
so duplicates (undocumented aliases, DD/FD prefixes with no effect) give the
|
||||
documented form. Three-operand DDCB register copies are left out.
|
||||
|
||||
tools/gen-asmtab.py (needs gcc)
|
||||
"""
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
exe = os.path.join(tempfile.mkdtemp(), "dis")
|
||||
subprocess.check_call(["gcc", "-O1", "-w", "-o", exe, os.path.join(root, "monitor", "monitor.c")])
|
||||
lines = subprocess.check_output([exe, "sym"], text=True).splitlines()
|
||||
|
||||
PFX = {"": 0, "CB": 1, "ED": 2, "DD": 3, "FD": 4, "DDCB": 5, "FDCB": 6}
|
||||
mnems, kinds, seen, tab = [], [""], set(), []
|
||||
|
||||
|
||||
def kind(o):
|
||||
"""operand text -> kind id; small literal numbers -> 0x80 | value"""
|
||||
lit = o[:-1] if o.endswith("H") and o[:-1].isalnum() else None
|
||||
if o.isdigit():
|
||||
return 0x80 | int(o)
|
||||
if lit is not None and all(c in "0123456789ABCDEF" for c in lit):
|
||||
return 0x80 | int(lit, 16)
|
||||
if o not in kinds:
|
||||
kinds.append(o)
|
||||
return kinds.index(o)
|
||||
|
||||
|
||||
for ln in lines:
|
||||
hexb, text = ln.split("\t")
|
||||
if text.startswith("DB ") or text == "NOP*":
|
||||
continue
|
||||
b = [hexb[i:i + 2] for i in range(0, len(hexb), 2)]
|
||||
if b[0] in ("DD", "FD") and b[1] == "CB":
|
||||
pfx, op = b[0] + "CB", b[3]
|
||||
elif b[0] in ("CB", "ED", "DD", "FD"):
|
||||
pfx, op = b[0], b[1]
|
||||
else:
|
||||
pfx, op = "", b[0]
|
||||
mn, _, rest = text.partition(" ")
|
||||
ops = rest.split(",") if rest else []
|
||||
if len(ops) > 2:
|
||||
continue
|
||||
if mn not in mnems:
|
||||
mnems.append(mn)
|
||||
k = [kind(o) for o in ops] + [0, 0]
|
||||
key = (mn, k[0], k[1])
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
tab.append((mnems.index(mn), k[0], k[1], PFX[pfx], int(op, 16)))
|
||||
|
||||
out = ["/* Generated by tools/gen-asmtab.py from the disassembler: do not edit. */",
|
||||
"/* {mnemonic, operand 1, operand 2, prefix, opcode}; operand 0x80|v is the",
|
||||
" literal number v; prefix 0 none, 1 CB, 2 ED, 3 DD, 4 FD, 5 DDCB, 6 FDCB */",
|
||||
"#define ASM_MN %d" % len(mnems),
|
||||
"static const char *const asm_mn[] = {",
|
||||
" " + ", ".join('"%s"' % m for m in mnems), "};",
|
||||
"#define ASM_KN %d" % len(kinds),
|
||||
"static const char *const asm_kn[] = {",
|
||||
" " + ", ".join('"%s"' % k for k in kinds), "};"]
|
||||
for name in ("n", "nn", "e", "(n)", "(nn)", "(IX+d)", "(IY+d)"):
|
||||
sym = {"n": "N", "nn": "NN", "e": "E", "(n)": "MN", "(nn)": "MNN", "(IX+d)": "IXD", "(IY+d)": "IYD"}[name]
|
||||
out.append("#define K_%s %d" % (sym, kinds.index(name)))
|
||||
out.append("#define ASM_N %d" % len(tab))
|
||||
out.append("static const u8 asm_tab[][5] = {")
|
||||
for i in range(0, len(tab), 6):
|
||||
out.append(" " + " ".join("{%d,%d,%d,%d,0x%02X}," % t for t in tab[i:i + 6]))
|
||||
out.append("};")
|
||||
open(os.path.join(root, "monitor", "asmtab.h"), "w").write("\n".join(out) + "\n")
|
||||
print("%d forms, %d mnemonics, %d operand kinds, %d bytes of table" %
|
||||
(len(tab), len(mnems), len(kinds), len(tab) * 5))
|
||||
Reference in new issue
Block a user