#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """The reference's rungs 2 and 3, printed as ``target/release/programs`` prints them. reference_programs.py SPCTEST_DIR CPUTEST_DIR Runs spc700-python's own ``run_spctest`` and ``run_cpu_test`` exactly as its ``tests/test_spctest.py`` and ``tests/test_cpu_tests.py`` do, and prints one line per run in the crate's format. ``SPC700_PYTHON`` names the checkout. """ from __future__ import annotations import os import sys from pathlib import Path sys.path.insert(0, str(Path(os.environ["SPC700_PYTHON"]) / "src")) from spc700_python.harness import run_cpu_test, run_spctest # noqa: E402 TESTS = 1368 SUBTESTS = {"ADC": 26, "AND": 28, "DEC": 14, "EOR": 26, "INC": 14, "ORA": 28, "SBC": 26} def main() -> int: spctest, cputest = Path(sys.argv[1]), Path(sys.argv[2]) programs = [(spctest / f"spc_tests{n}.bin").read_bytes() for n in range(3)] report = run_spctest(programs) ok = report.failure is None and report.programs == 3 and report.tests == list(range(TESTS)) last = f"{report.tests[-1]:04X}" if report.tests else "none" failure = "none" if report.failure is None else str(report.failure) print(f"spctest: {report.programs} programs, {len(report.tests)} tests (last {last}), " f"{report.cycles} cycles, {report.instructions} instructions, failure {failure}: " f"{'pass' if ok else 'FAIL'}") # fmt: skip for name, subtests in SUBTESTS.items(): for form in ("raw", "snapshot"): path = cputest / name / f"SPC700{name}.spc" if form == "raw" else cputest / f"SPC700{name}.spc" result = run_cpu_test(path.read_bytes()) passed = (result.reason == "self-branch" and result.failures == () and result.passes == tuple(range(1, subtests + 1))) # fmt: skip codes = " ".join(f"{c:02X}" for c in result.codes) print(f"SPC700{name} {form}: {result.reason} after {result.cycles} cycles, " f"{result.instructions} instructions; codes {codes}: {'pass' if passed else 'FAIL'}") # fmt: skip ok &= passed return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())