#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Milestone 3: bank-select writes of one title per scheme, against MAME 0.285's trace. MAME's ``error.log`` (``scripts/mame_trace.py``: writes to $4000-$400F, $8000-$BFFF and $FF80-$FF8F are watched) gives the order and values of the cartridge's bank-select writes; the board runs the same dump from the same BIOS and records its writes to the same ranges. The first ``--count`` of each are compared (address, value), in order. Times differ (the two machines' clocks differ, docs/mame-oracle.md), order and values must not. python scripts/bank_writes.py tests/mame_traces/a7800-commandou/error.log \\ ".../Commando (USA).zip" --count 200 """ from __future__ import annotations import argparse import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) from a7800_python.system import System # noqa: E402 RANGES = [(0x4000, 0x400F), (0x8000, 0xBFFF), (0xFF80, 0xFF8F)] def watched(address: int) -> bool: return any(lo <= address <= hi for lo, hi in RANGES) def mame_writes(log: Path, count: int) -> list[tuple[int, int]]: out = [] in_cart = False with log.open("rb") as handle: for raw in handle: parts = raw.split() if len(parts) == 10 and parts[0] == b"26BE": in_cart = True if in_cart and len(parts) == 4 and parts[0] == b"W": address = int(parts[1], 16) if watched(address): out.append((address, int(parts[2], 16))) if len(out) >= count: break return out def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("log", type=Path) parser.add_argument("cart", type=Path) parser.add_argument("--count", type=int, default=200) parser.add_argument("--frames", type=int, default=400) parser.add_argument("--range", help="compare only writes in START-END (hex), e.g. 4000-4FFF " "for Rescue on Fractalus's mirror RAM (trace with --watch-writes)") # fmt: skip parser.add_argument("--press", metavar="A-B", help="hold both P1 buttons from MARIA frame A to B (as mame_trace.py)") args = parser.parse_args() if args.range: lo, hi = (int(x, 16) for x in args.range.split("-")) RANGES[:] = [(lo, hi)] theirs = mame_writes(args.log, args.count) system = System.from_rom(args.cart) board = system.board ours: list[tuple[int, int]] = [] def watch(kind, address, value, clock): if kind == "W" and board.inptctrl & 0x04 and watched(address) and len(ours) < args.count: ours.append((address, value)) board.bus_observers.append(watch) start = board.maria.frame target = start + args.frames press = tuple(int(x) for x in args.press.split("-")) if args.press else None held = False while len(ours) < len(theirs) and board.maria.frame < target: if press: want = press[0] <= board.maria.frame - start < press[1] if want != held: system.controllers.set(0, left_button=want, right_button=want) held = want system.step() n = min(len(ours), len(theirs)) first = next((i for i in range(n) if ours[i] != theirs[i]), None) print(f"{args.cart.name}: {system.description.summary()}") print(f"MAME {len(theirs)} bank-region writes, board {len(ours)}; " + ("identical in order and value" if first is None and len(ours) == len(theirs) else f"first difference at {first}: MAME {theirs[first] if first is not None else '-'}" f" board {ours[first] if first is not None else '-'}")) # fmt: skip print("first writes:", " ".join(f"{a:04X}={v:02X}" for a, v in theirs[:8])) return 0 if first is None and len(ours) == len(theirs) else 1 if __name__ == "__main__": sys.exit(main())