# SPDX-License-Identifier: MIT """Run a 32X cartridge and keep its last picture and its sound. python -m sega32x_python CART --bios-dir DIR --frames N [--png out.png] [--wav out.wav] [--machine genesis] [--rate 44100] [--save-state FILE --save-at K] [--load-state FILE [--any-board]] ``--frames N`` runs to frame N (from a loaded state, on from the frame it was saved at). ``--png`` is the picture that leaves the console after the last frame (the composite, on the 32X's grid); ``--wav`` the sound of the frames run in this process (16-bit stereo at ``--rate``; the FM runs only when it is asked for). ``--save-state FILE --save-at K`` saves the whole machine after frame K; ``--load-state`` starts from such a file, made from the same cartridge and boot ROMs (``savestate.py``). A thin wrapper of :class:`~sega32x_python.runner.Session`; ``scripts/run_32x.py`` has the rest (a PNG every frame, button presses, reset, the PWM log). """ from __future__ import annotations import argparse import sys import time from pathlib import Path from megadrive_python.board import Lockup from megadrive_python.png import write_png from megadrive_python.wav import write_wav from .runner import Session from .savestate import StateError def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(prog="python -m sega32x_python", description=__doc__.split("\n\n")[0]) # fmt: skip parser.add_argument("cartridge", type=Path) parser.add_argument("--bios-dir", type=Path, required=True) parser.add_argument("--frames", type=int, required=True, help="run to this frame") parser.add_argument("--machine", default="genesis") parser.add_argument("--png", type=Path, help="the last frame's picture") parser.add_argument("--wav", type=Path, help="the sound (16-bit stereo)") parser.add_argument("--rate", type=int, default=44_100, help="sample rate of --wav") parser.add_argument("--save-state", type=Path, help="save the machine here (--save-at)") parser.add_argument("--save-at", type=int, help="the frame after which to save") parser.add_argument("--load-state", type=Path, help="start from this saved machine") parser.add_argument("--any-board", action="store_true", help="load a state saved by another board revision") # fmt: skip args = parser.parse_args(argv) if (args.save_state is None) != (args.save_at is None): parser.error("--save-state and --save-at go together") try: session = Session.start(args.cartridge, args.bios_dir, machine=args.machine, load_state=args.load_state, any_board=args.any_board) # fmt: skip except StateError as refused: parser.error(str(refused)) session.sound(fm=args.wav is not None, rate=args.rate if args.wav else None) audio: list[tuple[int, int]] = [] began = time.time() first = session.frame + 1 status = 0 while session.frame < args.frames: try: session.run_frame() except Lockup as stop: print(f"frame {session.frame + 1}: Lockup: {stop}", file=sys.stderr, flush=True) status = 2 break if args.wav: audio.extend(session.system.take_audio()) if session.frame == args.save_at: session.save(args.save_state) elapsed = time.time() - began run = session.frame - first + 1 print(f"frames {first}-{session.frame}: {elapsed:.1f} s ({run / max(elapsed, 1e-9):.2f} " f"frames/s), SH-2 instructions {session.device.sh2.instructions}", # type: ignore[union-attr] file=sys.stderr, flush=True) # fmt: skip if args.png: write_png(args.png, session.picture()) if args.wav: write_wav(args.wav, audio, args.rate) return status if __name__ == "__main__": sys.exit(main())