#!/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)