#!/usr/bin/env python # SPDX-License-Identifier: MIT """Replay a MAME tap log (scripts/mame_tap.sh) through the Nuked-OPM referee. python -I scripts/replay_tap.py reference/mame-run/ghouls/ym2151-tap.txt [--seconds 3] It turns the log's writes into referee events (two input clocks to an internal cycle, MAME's time 0 taken as the referee's cycle 0), runs the referee, and prints what came out: how many samples are not silent, the peak, and, for every status read in the log, whether the referee's status at that cycle is the byte MAME's own YM2151 returned. That last line is a detector at work: MAME's answer is an emulator's (tier 5), the referee's a die-derived model's (closed box). Phase 0 groundwork: it shows the path from a real game's writes to the referee's pins works. A later rung feeds the same events to the core. """ from __future__ import annotations import argparse import array import collections import struct import os import subprocess import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parent.parent #: The referee: nuked-harness's opm-events, a GPL tool outside this repository, at its #: documented path under EMU_TOOLS_GPL (docs/provenance.md, "History"). TOOL = "tools/nuked/build/opm-events" REFEREE = Path(os.environ["EMU_TOOLS_GPL"]) / TOOL if os.environ.get("EMU_TOOLS_GPL") else None CYCLES_PER_SAMPLE = 32 CLOCKS_PER_CYCLE = 2 def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("log", type=Path) parser.add_argument("--seconds", type=float, default=3.0) parser.add_argument("--chip", default="ym2151", choices=("ym2151", "ym2164")) args = parser.parse_args(argv) if REFEREE is None or not REFEREE.exists(): print(f"the referee is missing: set EMU_TOOLS_GPL so that $EMU_TOOLS_GPL/{TOOL} exists " "(sh validation/build_referee.sh checks it)", file=sys.stderr) return 3 clock = 3_579_545 events, reads = [], [] for line in args.log.read_text(encoding="utf-8").splitlines(): if line.startswith("#"): fields = line.split() if "clock" in fields: clock = int(fields[fields.index("clock") + 1]) continue when, kind, a0, value = line.split() cycle = int(when) // CLOCKS_PER_CYCLE if kind == "W": events.append((cycle, int(a0), int(value, 16))) elif int(a0) == 1: reads.append((cycle, int(value, 16))) cycles = int(args.seconds * clock) // CLOCKS_PER_CYCLE cycles -= cycles % CYCLES_PER_SAMPLE events = [event for event in events if event[0] < cycles] reads = [read for read in reads if read[0] < cycles] with tempfile.TemporaryDirectory(dir=ROOT / "reference") as scratch: source = Path(scratch) / "events.bin" target = Path(scratch) / "out.bin" source.write_bytes(b"".join(struct.pack("