#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Milestone 5: run a title from the BIOS under a scripted controller and keep frames. A script is a list of ``FRAME:ACTION`` items, each applied when MARIA starts that frame (counted from power-on, as frames_mame.py counts): - ``gun:LINE,CLOCK`` aim the light gun (``gun:off`` points it away); ``pull`` / ``release`` the trigger (controllers.LightGun, port 0); - ``b1`` / ``b2`` / ``b12`` / ``none``: the ProLine's buttons on port 0 (left, right, both, released); ``up``/``down``/``left``/``right`` a direction (with the buttons released); ``reset`` / ``select`` hold that console switch, ``switches-off`` releases both. python scripts/controller_run.py ".../Barnyard Blaster (USA).zip" --frames 900 \\ --script 700:gun:150,260 700:pull 704:release --png-at 600,702,720 --out tests/detector_runs/gun Prints the light gun's sensor reads that saw light, and writes each ``--png-at`` frame as ``/f.png`` and a digest list of all frames from ``--digest-from`` on (for comparing two scripted runs). """ from __future__ import annotations import argparse import hashlib import sys import time from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) from a7800_python.controllers import LightGun # noqa: E402 from a7800_python.system import System # noqa: E402 from a7800_python.television import Television, frame_png # noqa: E402 def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("cart", type=Path) parser.add_argument("--frames", type=int, default=900) parser.add_argument("--script", nargs="*", default=[]) parser.add_argument("--png-at", default="") parser.add_argument("--digest-from", type=int, default=0) parser.add_argument("--out", type=Path, required=True) parser.add_argument("--no-gun", action="store_true", help="plug no light gun (ProLine only)") args = parser.parse_args() args.out.mkdir(parents=True, exist_ok=True) system = System.from_rom(args.cart) board = system.board gun = None if args.no_gun else LightGun(board, 0) tv = Television(board.maria, keep=1) script: dict[int, list[str]] = {} for item in args.script: frame, action = item.split(":", 1) script.setdefault(int(frame), []).append(action) pngs = {int(x) for x in args.png_at.split(",") if x} digests = [] aim, pulled = None, False began = time.monotonic() seen = 0 while tv.count < args.frames: for action in script.pop(tv.count + 1, []): if action.startswith("gun:"): spec = action[4:] aim = None if spec == "off" else tuple(int(v) for v in spec.split(",")) elif action in ("pull", "release"): pulled = action == "pull" elif action in ("reset", "select", "switches-off"): # SWCHB D0 / D1, active low bit = {"reset": 1, "select": 2, "switches-off": 0}[action] board.riot.input_b = (board.riot.input_b | 0x03) & ~bit elif action in ("b1", "b2", "b12", "none"): system.controllers.set(0, left_button=action in ("b1", "b12"), right_button=action in ("b2", "b12")) # fmt: skip else: system.controllers.set(0, **{action: True}) if gun is not None: gun.set(aim=aim, trigger=pulled) while tv.count == seen: system.step() seen = tv.count frame = tv.frames[-1] if seen >= args.digest_from: digests.append(hashlib.sha1(b"".join(frame)).hexdigest()[:12]) if seen in pngs: (args.out / f"f{seen}.png").write_bytes(frame_png(frame)) wall = time.monotonic() - began (args.out / "digests.txt").write_text("\n".join(digests) + "\n", encoding="utf-8") print(f"{args.cart.name}: {system.description.summary()}; {args.frames} frames in {wall:.0f} s") if gun is not None: print(f"light gun: {gun.hits} sensor reads saw light") return 0 if __name__ == "__main__": sys.exit(main())