#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Capture lockstep (rungs 4 and 4b): replay logic-analyser captures of real 65C02s. python -I scripts/run_capture.py # every capture, and Foot's python -I scripts/run_capture.py master reset # one capture (the smoke run) python -I scripts/run_capture.py beebr65c02 dormann_d65c11 --max-instructions 1000000 python -I scripts/run_capture.py foot # rung 4b only The method, and what counts as a mismatch, are tests/capture_replay.py's. A capture passes with no mismatch and no model difference. ``foot`` is George Foot's data-only capture, replayed on the two rows with bit instructions (W65C02S and R65C02). Prints one report per capture, and with ``--cycles`` the cycle counts seen per opcode; ``--json DIR`` keeps every count, per bus-cycle slot. Exit status 1 if any capture did not pass. Run it from the project's venv; PyPy for the long captures (about 25 million instructions each; the whole set takes about 90 seconds). At most ``--jobs`` processes (default 4). """ from __future__ import annotations import argparse import json import multiprocessing import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) import capture_replay from w65c02_python import R65C02, W65C02S FOOT_ROWS = {"w65c02s": W65C02S, "r65c02": R65C02} def run(task: tuple[str, str, int | None, bool, Path | None]) -> tuple[bool, str]: machine, capture, limit, show_cycles, json_dir = task if machine == "foot": report = capture_replay.replay_data_only(FOOT_ROWS[capture], limit) report.capture = f"dormann on {FOOT_ROWS[capture].__name__}" else: report = capture_replay.replay(machine, capture, max_instructions=limit) text = capture_replay.format_report(report) if show_cycles: for opcode in sorted(report.cycles_by_opcode): seen = " ".join( f"{n}(x{k})" for n, k in sorted(report.cycles_by_opcode[opcode].items()) ) text += f"\n {opcode:02X} {seen}" if json_dir is not None: # Per slot (opcode or sequence, cycle): reads, reads that agreed with the model, differed. slots = { f"{key[0] if isinstance(key[0], str) else format(key[0], '02X')}:{key[1]}": [ count, report.model_agreed.get(key, 0), report.model_differences.get(key, 0), ] for key, count in report.reads.items() } result = { "instructions": report.instructions, "cycles": report.cycles, "interrupts": dict(report.interrupts), "ended": report.ended, "mismatch": report.mismatch, "start": report.start, "writes_not_kept": report.writes_not_kept, "cycles_by_opcode": { f"{opcode:02X}": dict(sorted(seen.items())) for opcode, seen in sorted(report.cycles_by_opcode.items()) }, "slots": dict(sorted(slots.items())), } json_dir.mkdir(parents=True, exist_ok=True) (json_dir / f"{machine}_{capture}.json").write_text(json.dumps(result, indent=1)) return report.ok, text def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("machine", nargs="?", choices=[*sorted(capture_replay.MACHINES), "foot"]) parser.add_argument("capture", nargs="?") parser.add_argument("--max-instructions", type=int) parser.add_argument("--cycles", action="store_true", help="print cycle counts per opcode") parser.add_argument("--jobs", type=int, default=4) parser.add_argument( "--json", type=Path, metavar="DIR", help="write each capture's full result here" ) args = parser.parse_args() if not capture_replay.captures_present(): raise SystemExit(capture_replay.FETCH_HINT) captures = {**capture_replay.CAPTURES, "foot": tuple(FOOT_ROWS)} machines = [args.machine] if args.machine else sorted(captures) tasks = [ (machine, capture, args.max_instructions, args.cycles, args.json) for machine in machines for capture in ([args.capture] if args.capture else captures[machine]) ] passed = True with multiprocessing.Pool(min(args.jobs, len(tasks))) as pool: for ok, text in pool.imap(run, tasks): print(text, flush=True) passed = passed and ok return 0 if passed else 1 if __name__ == "__main__": sys.exit(main())