#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Extract anomie's SPC700 opcode table as JSON (tests/anomie_opcodes.json). Reads the pinned ``reference/docs/anomie-spc700.txt`` ($Revision 1162$, 2009-07-19; see scripts/pinned_files.json) and writes one entry per opcode: {"opcode": 0x99, "mnemonic": "ADC", "operands": "(X), (Y)", "bytes": 1, "cycles": [5], "flags": "NV..H.ZC", "operation": "(X) = (X)+(Y)+C"} ``cycles`` holds one value, or two for conditional instructions (not taken, taken), or is empty for SLEEP/STOP ("?" in the table). All 256 opcodes must appear exactly once; the script fails otherwise. Run it with ``python -I``: it reads fetched, untrusted text. python -I scripts/anomie_opcodes.py """ from __future__ import annotations import json import re import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SOURCE = ROOT / "reference" / "docs" / "anomie-spc700.txt" TARGET = ROOT / "tests" / "anomie_opcodes.json" # " ADC (X), (Y) 99 1 5 (X) = (X)+(Y)+C NV..H.ZC" ROW = re.compile( r"^ (?P[A-Z0-9]+)\s+(?P.*?)\s+(?P[0-9A-F]{2})\s+" r"(?P[123])\s+(?P\d+(?:/\d+)?|\?)\s+(?P.*?)\s+" r"(?P[NV.01][V.01][P.01][B.01][H.01][I.01][Z.01][C.01])\s*$" ) def parse(text: str) -> list[dict]: table = text.split("Mnemonic Code Bytes Cyc", 1)[1] entries = {} for line in table.splitlines(): match = ROW.match(line) if not match: continue opcode = int(match["opcode"], 16) if opcode in entries: raise SystemExit(f"opcode {opcode:02X} appears twice") cycles = [] if match["cycles"] == "?" else [int(c) for c in match["cycles"].split("/")] entries[opcode] = { "opcode": opcode, "mnemonic": match["mnemonic"], "operands": match["operands"].strip(), "bytes": int(match["bytes"]), "cycles": cycles, "flags": match["flags"], "operation": match["operation"].strip(), } missing = sorted(set(range(256)) - set(entries)) if missing: raise SystemExit("missing opcodes: " + " ".join(f"{m:02X}" for m in missing)) return [entries[op] for op in range(256)] def main() -> int: entries = parse(SOURCE.read_text(encoding="latin-1")) TARGET.parent.mkdir(exist_ok=True) TARGET.write_text(json.dumps(entries, indent=1) + "\n", encoding="utf-8") print(f"{len(entries)} opcodes -> {TARGET.relative_to(ROOT)}") return 0 if __name__ == "__main__": sys.exit(main())