#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Run the self-checking programs on every row and print where each ended: rungs 2 and 3. python -I scripts/run_programs.py # every program on every row python -I scripts/run_programs.py --row g65sc02 --program extended Dormann's 65C02 extended-opcodes test (the image built for the row), his NMOS functional test, and Clark's decimal test (``cputype = 1``), each run in ``w65c02_python.harness`` from $0400 until it jumps to itself. Prints the trap address, the instructions and cycles it took, and whether that is the program's success. The images, the addresses and the reading of the result are tests/programs.py's; pytest runs the same programs (tests/test_dormann.py, tests/test_decimal.py), this shows the numbers. Exit status 1 if any program did not succeed. """ from __future__ import annotations import argparse import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) import programs from w65c02_python.harness import ROWS, Harness def run(row: type, image: bytes) -> tuple[Harness, int]: """Run ``image`` from $0400 to its trap, counting instructions.""" harness = Harness(row) harness.load(image) harness.start(0x0400) cpu = harness.cpu step = cpu.step instructions = 0 while True: before = cpu.pc step() instructions += 1 if cpu.pc == before or cpu.halted or instructions > 200_000_000: return harness, instructions def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--row", choices=sorted(ROWS), action="append") parser.add_argument( "--program", choices=("extended", "functional", "decimal"), action="append", help="default: all three (extended and functional are rung 2, decimal rung 3)", ) # fmt: skip args = parser.parse_args() wanted = args.program or ("extended", "functional", "decimal") failed = False for name in args.row or sorted(ROWS): row = ROWS[name] built_as, success = programs.EXTENDED[row] runs = ( ( f"65C02 extended ({built_as})", programs.extended_image(built_as).read_bytes(), lambda harness, success=success: harness.cpu.pc == success, ), ( "NMOS functional", programs.FUNCTIONAL.read_bytes(), lambda harness: harness.cpu.pc == programs.FUNCTIONAL_SUCCESS, ), ( "Clark decimal", programs.decimal_image(), lambda harness: harness.memory[programs.DECIMAL_ERROR] == 0, ), ) for title, image, succeeded in runs: if not any(word in title.lower() for word in wanted): continue harness, instructions = run(row, image) ok = succeeded(harness) failed = failed or not ok print( f"{row.__name__:<9} {title:<25} ended at ${harness.cpu.pc:04X} after " f"{instructions:,} instructions, {harness.cycles:,} cycles: " f"{'success' if ok else 'FAILED'}", flush=True, ) return 1 if failed else 0 if __name__ == "__main__": sys.exit(main())