#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Rung 1, part one: every row's opcode table against the reference's ``opcodes_for``. check_tables.py TRACE_BINARY Runs ``TRACE_BINARY --tables`` (this crate's 256 cells per row: handler, mode, mnemonic, form, base cycles, length) and compares each cell with the reference's ``w65c02_python._dispatch.opcodes_for(row)`` and ``FORM_BYTES``. Run it with the reference's interpreter. """ from __future__ import annotations import json import subprocess import sys from w65c02_python._dispatch import FORM_BYTES, opcodes_for from w65c02_python.harness import ROWS def camel(name: str | None) -> str: return "Implied" if name is None else "".join(part.capitalize() for part in name.split("_")) def main() -> int: ours = json.loads(subprocess.run([sys.argv[1], "--tables"], capture_output=True, text=True, check=True).stdout) wrong = 0 for row in ROWS.values(): table = opcodes_for(row) cells = ours[row.__name__] for opcode in range(256): handler, mode, mnemonic, form, cycles = table[opcode] want = [camel(handler), camel(mode), mnemonic, camel(form), cycles, FORM_BYTES[form]] if cells[opcode] != want: wrong += 1 print(f"{row.__name__} ${opcode:02X}: ours {cells[opcode]}, reference {want}") print(f"{row.__name__}: 256 opcodes compared, {sum(1 for o in range(256) if cells[o][0] != 'NopOneCycle')} not one-cycle NOPs") print("tables are identical" if not wrong else f"{wrong} cells differ") return 1 if wrong else 0 if __name__ == "__main__": sys.exit(main())