#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Rung 1, ADEN = 0: megadrive-rust's own lockstep cases through a passive 32X. The reference's milestone-1 gate (sega32x-python ``harness.py``): megadrive-python's own gate with a 32X inserted and ADEN never set changes nothing. Here, for every case of megadrive-rust's ``scripts/cases.tsv``, three traces of the Mega Drive board must be identical, byte for byte: - ``md-trace`` built from megadrive-rust ``0959147`` (branch ``sockets``, what its own gate proved against megadrive-python ``efd80fc``: 87 of 87), external/megadrive-rust-base; - ``md-trace`` built from the branch this board depends on (``sockets-32x``: the hooks this board needs, external/megadrive-rust), with nothing attached; - ``s32x-mdtrace``: the same case with a passive 32X holding the cartridge (not for the cases with a fake socket device or a save state, which have a device or a state of their own: those compare only the first two). python3 scripts/passive.py [--only NAME ...] [--jobs N] The case table's variables are megadrive-rust's (``$NI``, ``$AM`` under ``$NO_INTRO_DIR``; ``$TEST_ROMS``, default ``../megadrive-python/tests/test_roms``; ``$EXTRA_ROMS``, ``$TITAN_DEMOS``). Stdlib only. """ from __future__ import annotations import argparse import importlib.util import os import subprocess import sys from concurrent.futures import ThreadPoolExecutor from pathlib import Path HERE = Path(__file__).resolve().parent REPO = HERE.parent MD = REPO / "external/megadrive-rust" BASE = REPO / "external/megadrive-rust-base" os.environ.setdefault("TEST_ROMS", str(REPO.parent / "megadrive-python/tests/test_roms")) _spec = importlib.util.spec_from_file_location("md_lockstep", MD / "scripts/lockstep.py") md_lockstep = importlib.util.module_from_spec(_spec) # type: ignore[arg-type] _spec.loader.exec_module(md_lockstep) # type: ignore[union-attr] def run_case(case, out: Path, passive: Path): name, count, cart, args = case why = md_lockstep.missing(cart) if why: return name, "MISSING", why common = [cart, "--frames", str(count), *args] own = any(a in ("--fake", "--save-at", "--rewind-at") for a in args) runs = [("base", BASE / "target/release/md-trace"), ("branch", MD / "target/release/md-trace")] if not own: runs.append(("passive", passive)) paths = [] for label, binary in runs: path = out / f"{name}.{label}.trace" r = subprocess.run(["nice", str(binary), *common, "--out", str(path)], capture_output=True, text=True) if r.returncode: return name, "ERROR", f"{label}: {r.stderr.strip()[-300:]}" paths.append(path) first = paths[0].read_bytes() for (label, _), path in zip(runs[1:], paths[1:]): if path.read_bytes() != first: return name, "DIFFERENT", f"{label}: {md_lockstep.first_difference(paths[0], path)}" lines = first.count(b"\n") return name, "identical", f"{len(runs)} traces, {lines} lines" + ("" if not own else " (device or state of its own: no passive run)") def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--only", action="append", default=[]) parser.add_argument("--jobs", type=int, default=8) parser.add_argument("--out", type=Path, default=REPO / "external/passive") parser.add_argument("--passive", type=Path, default=REPO / "target/release/s32x-mdtrace") args = parser.parse_args() args.out.mkdir(parents=True, exist_ok=True) selected = [c for c in md_lockstep.cases() if not args.only or c[0] in args.only] failed = 0 passive = 0 with ThreadPoolExecutor(args.jobs) as pool: for name, verdict, note in pool.map(lambda c: run_case(c, args.out, args.passive), selected): failed += verdict != "identical" passive += verdict == "identical" and "3 traces" in note print(f"{name:24} {verdict:9} {note}", flush=True) print(f"{len(selected) - failed} of {len(selected)} identical ({passive} with a passive 32X)") return 1 if failed else 0 if __name__ == "__main__": sys.exit(main())