"""Replay a MAME port-access log (scripts/mame_io.py) against the board. python scripts/replay_io.py tests/mame_traces/io-Donkey_Kong CART.zip [--default] [--png-at F] The inputs are read from the run's ``inputs.txt`` and applied at the CPU clock where MAME set them (frame k's inputs at clock 59,736 k: scripts/smoke_pictures.py). Every access is compared in order: kind, port, value and the start clock of the instruction that made it. Prints the counts and the first differences. The board runs with MAME's choice on every switch unless ``--default``. """ from __future__ import annotations import argparse import sys import time from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from smoke_pictures import Inputs, parse_input from colecovision_python import NTSC, PAL, BoardOptions, ColecoVision, mame_options from colecovision_python.images import load_image from colecovision_python.mame_log import PortAccess, read_log def replay( run: Path, cart: str | None, options, max_differences: int = 5, until: int | None = None, machine=NTSC ): schedule = [parse_input(t) for t in (run / "inputs.txt").read_text().split()] console = ColecoVision(load_image(cart) if cart else None, options=options, io_log=[], machine=machine) board = console.board inputs = Inputs(console.controllers, schedule) board.input_hook = inputs.update pending: list[tuple] = [] compared = differences = 0 report = [] sound: list[int] = [] for event in read_log(run / "error.log"): if not isinstance(event, PortAccess): continue if until is not None and event.cycles > until: break while not pending: board.step() inputs.update(board.clock) pending.extend(board.io_log) board.io_log.clear() kind, port, value, start, _ = pending.pop(0) compared += 1 if (kind, port, value, start) != (event.kind, event.port, event.value, event.cycles): differences += 1 report.append( f"access {compared}: board {kind} {port:02X} {value:02X} @{start}, " f"MAME {event.kind} {event.port:02X} {event.value:02X} @{event.cycles}" ) if differences >= max_differences: break elif kind == "IW" and port >= 0xE0: sound.append(start) board.sound_matched = sound # instruction-start clocks of the sound writes that matched return compared, differences, report, board def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("run", type=Path) parser.add_argument("cart", nargs="?") parser.add_argument("--default", action="store_true", help="the board's defaults, not MAME's switches") parser.add_argument("--pal", action="store_true", help="the PAL row (a colecop log, no inputs)") parser.add_argument("--max-differences", type=int, default=5) args = parser.parse_args() began = time.perf_counter() options = BoardOptions() if args.default else mame_options() compared, differences, report, board = replay( args.run, args.cart, options, args.max_differences, machine=PAL if args.pal else NTSC ) print( f"{compared:,} accesses compared, {differences} differences, board clock {board.clock:,}, " f"{time.perf_counter() - began:.1f} s, {sys.implementation.name}" ) sound = board.sound_matched if sound: print( f" sound writes matched: {len(sound):,}, clocks {sound[0]:,} to {sound[-1]:,} " f"({(sound[-1] - sound[0]) / 3_579_545:.1f} s)" ) for line in report: print(" " + line) return 1 if differences else 0 if __name__ == "__main__": sys.exit(main())