#!/usr/bin/env python3 """The lockstep gate: run each case on both boards and compare the traces. Each case of ``scripts/cases.tsv`` runs ``md-trace`` (this board) and ``scripts/md_trace.py`` on the reference's interpreter, into ``external/traces/``, several at a time, and the two traces are compared line for line. A case passes when they are identical; otherwise the first differing line is reported with its frame. python3 scripts/lockstep.py [--only NAME ...] [--jobs N] [--frames N] [--rust PATH] [--reference PATH] ``--reference PATH`` puts another board's ``md-trace`` on the reference's side: in megadrive-rust-fast, upstream megadrive-rust's, which is itself identical to megadrive-python on every case (``scripts/ladder.sh``). The case list's variables come from the environment. ``$NI`` and ``$AM`` default to the No-Intro Mega Drive set and its aftermarket half under ``$NO_INTRO_DIR``; ``$TEST_ROMS`` to the test ROMs of a megadrive-python checkout beside this one (its ``scripts/fetch_test_roms`` fills them). ``$EXTRA_ROMS`` (homebrew outside No-Intro) and ``$TITAN_DEMOS`` (Titan's demo releases) have no default. A case whose cartridge cannot be found is reported MISSING with the variable to set, and counts as a failure: the gate never passes on fewer cases than it lists. No machine path lives here. Stdlib only. """ from __future__ import annotations import argparse import os import shlex import subprocess import sys import time from concurrent.futures import ThreadPoolExecutor from pathlib import Path HERE = Path(__file__).resolve().parent REPO = HERE.parent _NO_INTRO = os.environ.get("NO_INTRO_DIR") DEFAULTS = { "TEST_ROMS": str(REPO.parent / "megadrive-python" / "tests" / "test_roms"), # Our own cartridges (scripts/make_examples.py), committed with the repository. "EXAMPLES": str(REPO / "tests" / "examples"), "EXTRA_ROMS": None, "TITAN_DEMOS": None, "NI": f"{_NO_INTRO}/Sega - Mega Drive - Genesis" if _NO_INTRO else None, "AM": f"{_NO_INTRO}/Sega - Mega Drive - Genesis (Aftermarket)" if _NO_INTRO else None, } #: What to set when a variable has no value. NEEDS = {"NI": "NO_INTRO_DIR", "AM": "NO_INTRO_DIR"} def expand(text: str) -> str: """Substitute the variables; one with no value stays as ``$NAME`` (reported MISSING).""" for name, default in DEFAULTS.items(): value = os.environ.get(name, default) if value is not None: text = text.replace(f"${name}", value) return text def missing(cart: str) -> str | None: """Why CART cannot be run, or None.""" for name in DEFAULTS: if f"${name}" in cart: return f"set {NEEDS.get(name, name)}" if not Path(cart).exists(): return f"not found: {cart}" return None def cases() -> list[tuple[str, int, str, list[str]]]: out = [] for line in (HERE / "cases.tsv").read_text().splitlines(): if not line.strip() or line.startswith("#"): continue fields = line.split("\t") name, frames, cart = fields[0], int(fields[1]), expand(fields[2]) args: list[str] = [] if len(fields) > 3: # Arguments are space-separated; a value with spaces (a path) runs to the # next option. for token in shlex.split(fields[3].replace("$", "\\$"), posix=True): if args and not token.startswith("--") and not args[-1].startswith("--"): args[-1] += " " + expand(token) else: args.append(expand(token)) out.append((name, frames, cart, args)) return out def first_difference(a: Path, b: Path) -> str: frame = "-" with a.open() as left, b.open() as right: for number, (x, y) in enumerate(zip(left, right), 1): if x.startswith("F "): frame = x.split()[1] if x != y: return f"line {number} (frame {frame}): {x.split()[0]} line differs" return "one trace is longer" def run_case(case, python: Path, rust: Path, out: Path, frames: int | None, reference: Path | None = None): name, count, cart, args = case count = frames or count why = missing(cart) if why: return name, count, "MISSING", why, 0.0, 0.0 rs, py = out / f"{name}.rs.trace", out / f"{name}.py.trace" common = [cart, "--frames", str(count), *args] started = time.perf_counter() r = subprocess.run(["nice", str(rust), *common, "--out", str(rs)], capture_output=True, text=True) rust_s = time.perf_counter() - started started = time.perf_counter() side = [str(reference)] if reference else [str(python), str(HERE / "md_trace.py")] p = subprocess.run( ["nice", *side, *common, "--out", str(py)], capture_output=True, text=True, ) # fmt: skip python_s = time.perf_counter() - started if r.returncode or p.returncode: return name, count, "ERROR", (r.stderr + p.stderr).strip()[-300:], rust_s, python_s if rs.read_bytes() == py.read_bytes(): lines = sum(1 for _ in rs.open()) lock = next((l for l in rs.open() if l.startswith("L ")), "") note = f"{lines} lines" + (f"; both stop: {lock.strip()}" if lock else "") return name, count, "identical", note, rust_s, python_s return name, count, "DIFFERENT", first_difference(rs, py), rust_s, python_s 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=14) parser.add_argument("--frames", type=int, help="override every case's frame count") parser.add_argument("--rust", type=Path, default=REPO / "target/release/md-trace") parser.add_argument( "--python", type=Path, default=REPO / "external/megadrive-python/.venv/bin/python" ) parser.add_argument("--reference", type=Path, help="an md-trace binary for the reference's side") # fmt: skip parser.add_argument("--out", type=Path, default=REPO / "external/traces") args = parser.parse_args() args.out.mkdir(parents=True, exist_ok=True) selected = [c for c in cases() if not args.only or c[0] in args.only] failed = 0 with ThreadPoolExecutor(args.jobs) as pool: jobs = [pool.submit(run_case, c, args.python, args.rust, args.out, args.frames, args.reference) for c in selected] # fmt: skip for job in jobs: name, count, verdict, note, rust_s, python_s = job.result() failed += verdict != "identical" side = "reference" if args.reference else "python" print(f"{name:18} {count:6} frames {verdict:9} rust {rust_s:6.1f} s " f"{side} {python_s:7.1f} s {note}", flush=True) # fmt: skip print(f"{len(selected) - failed} of {len(selected)} identical") return 1 if failed else 0 if __name__ == "__main__": sys.exit(main())