#!/usr/bin/env python3 """Count what the DL-1425 program uses, by the manual's encoding fields (appendix A). Reads ``dl-1425.bin`` in place from MAME's ``qsound`` device set, checks its SHA-1, and walks the words linearly (a ``R = N`` instruction takes two). It is a census of encodings, not a disassembler and not emulator code: its job is to say which instruction classes the real program can test and which need directed tests. Data tables decode as nonsense, so the walk stops at ``--end`` (default 0x0D53, where the filter tables start: docs/qsound.md). python -I scripts/program_census.py """ from __future__ import annotations import os import argparse import collections import hashlib import struct import sys import zipfile from pathlib import Path def _find_set(name: str = "qsound.zip") -> Path: """The device set, searched for in the MAME_ROMPATH folders (';'-separated).""" for folder in os.environ.get("MAME_ROMPATH", "").split(";"): if folder and (Path(folder) / name).exists(): return Path(folder) / name sys.exit(f"{name} not found: set MAME_ROMPATH to the folder(s) holding it") SET = None # found at run time by _find_set() SHA1 = "555f50fe5cdf127619da7d854c03f4a244a0c501" T_NAMES = { 0x00: "goto JA", 0x01: "goto JA", 0x02: "short imm j,k,rb,re", 0x03: "short imm r0-r3", 0x04: "F1; Y = a1[l]", 0x05: "F1; Z : aT[l]", 0x06: "F1; Y", 0x07: "F1; aT[l] = Y", 0x08: "aT = R", 0x09: "R = a0", 0x0A: "R = N", 0x0B: "R = a1", 0x0C: "Y = R", 0x0D: "Z : R", 0x0E: "do / redo", 0x0F: "R = Y", 0x10: "call JA", 0x11: "call JA", 0x12: "ifc CON F2", 0x13: "if CON F2", 0x14: "F1; Y = y[l]", 0x15: "F1; Z : y[l]", 0x16: "F1; x = Y", 0x17: "F1; y[l] = Y", 0x18: "branch indirect", 0x19: "F1; y = a0; x = X", 0x1A: "cond. qualifier / icall", 0x1B: "F1; y = a1; x = X", 0x1C: "F1; Y = a0[l]", 0x1D: "F1; Z : y; x = X", 0x1E: "reserved", 0x1F: "F1; y = Y; x = X", } # fmt: skip HAS_F1 = {0x04, 0x05, 0x06, 0x07, 0x14, 0x15, 0x16, 0x17, 0x19, 0x1B, 0x1C, 0x1D, 0x1F} HAS_R = {0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0F} R_NAMES = { 0: "r0", 1: "r1", 2: "r2", 3: "r3", 4: "j", 5: "k", 6: "rb", 7: "re", 8: "pt", 9: "pr", 10: "pi", 11: "i", 16: "x", 17: "y", 18: "yl", 19: "auc", 20: "psw", 21: "c0", 22: "c1", 23: "c2", 24: "sioc", 25: "srta", 26: "sdx", 27: "tdms", 28: "pioc", 29: "pdx0", 30: "pdx1", } # fmt: skip def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--end", type=lambda text: int(text, 0), default=0x0D53) args = parser.parse_args() data = zipfile.ZipFile(SET or _find_set()).read("dl-1425.bin") if hashlib.sha1(data).hexdigest() != SHA1: print("dl-1425.bin does not match its SHA-1") return 1 words = struct.unpack(f"<{len(data) // 2}H", data) types, f1, f2, con, regs, modes, loops = (collections.Counter() for _ in range(7)) address = 0 while address < args.end: word = words[address] kind = word >> 11 types[kind] += 1 if kind in HAS_F1: f1[(word >> 5) & 15] += 1 modes[("Z" if kind in (0x05, 0x15, 0x1D) else "Y", word & 3)] += 1 if kind in (0x12, 0x13): f2[(word >> 5) & 15] += 1 con[word & 31] += 1 if kind == 0x1A: con[word & 31] += 1 if kind in HAS_R: regs[R_NAMES.get((word >> 4) & 63, f"reserved {(word >> 4) & 63:02X}")] += 1 if kind == 0x0E: loops[("redo" if not (word >> 7) & 15 else f"do NI={(word >> 7) & 15}", word & 127)] += 1 address += 2 if kind == 0x0A else 1 print(f"words 0x0000-0x{args.end - 1:04X}; nonzero words in the whole image: " f"{sum(1 for word in words if word)} of {len(words)}") # fmt: skip print("\nT field (instruction type):") for kind in range(32): print(f" {kind:05b} {T_NAMES[kind]:26} {types[kind]}") for title, counter in (("F1", f1), ("F2", f2), ("CON", con)): print(f"\n{title}: " + ", ".join(f"{key:X}:{count}" for key, count in sorted(counter.items()))) print("\nR: " + ", ".join(f"{key}:{count}" for key, count in sorted(regs.items()))) print("\nY/Z postmodify (field, low two bits): " + ", ".join(f"{key[0]}{key[1]}:{count}" for key, count in sorted(modes.items()))) # fmt: skip print("\ncache: " + ", ".join(f"{key[0]} K={key[1]}:{count}" for key, count in sorted(loops.items()))) return 0 if __name__ == "__main__": sys.exit(main())