#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """The reference's rungs 2 and 3, printed as ``target/release/roms`` prints them. reference_roms.py CPUTEST_DIR KROM_DIR Runs 65816-python's own harness exactly as its ``tests/test_cputest.py`` and ``tests/test_krom.py`` do (``TextLog`` and the verdict helpers are imported from those tests) and prints one line per ROM in the crate's format, so the two outputs can be compared line for line. Run it with the reference's interpreter; ``W65C816_PYTHON`` names the checkout. """ from __future__ import annotations import os import sys from pathlib import Path sys.path.insert(0, str(Path(os.environ["W65C816_PYTHON"]) / "tests")) from test_cputest import LAST_TEST, MAX_CYCLES, failed, verdict # noqa: E402 from test_krom import GROUPS, TextLog, expected # noqa: E402 from test_krom import MAX_CYCLES as KROM_MAX_CYCLES # noqa: E402 from w65c816.harness import Harness # noqa: E402 def main() -> int: cputest, krom = Path(sys.argv[1]), Path(sys.argv[2]) ok = True for name, last in LAST_TEST.items(): harness = Harness((cputest / name).read_bytes()) result = harness.run(MAX_CYCLES, failed) word, number = verdict(harness) number_text = "none" if number is None else f"{number:04X}" passed = result.reason == "spin" and (word, number) == ("Success", last) print(f"{name}: {result.reason} after {result.cycles} cycles at {result.pc:06X}; " f"printed '{word}', test {number_text}: {'pass' if passed else 'FAIL'}") # fmt: skip ok &= passed for group in GROUPS: folder = krom / group passes, fails = expected((folder / f"CPU{group}.asm").read_text(encoding="utf-8")) harness = TextLog((folder / f"CPU{group}.sfc").read_bytes()) result = harness.run(KROM_MAX_CYCLES, TextLog.failing) if result.reason == "stopped" and group == "MSC": harness.cpu.request_reset() result = harness.run(KROM_MAX_CYCLES, TextLog.failing) failed_at = len(set(harness.printed(b"FAIL")) & fails) printed = harness.printed(b"PASS") rows = sum(passes.values()) matched = sum(1 for at, n in passes.items() if printed[at] == n) passed = not failed_at and result.reason == "spin" and matched == len(passes) and rows > 0 print(f"CPU{group}: {result.reason} after {result.cycles} cycles at {result.pc:06X}; " f"{rows} PASS rows expected, {matched} of {len(passes)} destinations as expected, " f"FAIL at {failed_at}: {'pass' if passed else 'FAIL'}") # fmt: skip ok &= passed return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())