"""Run a ROM on the board from the command line. python -m gba_python ROM.gba [--bios PATH] [--frames N] [--skip-bios] [--trace out.jsonl] [--keys FRAME:MASK]... [--save-state FILE] [--load-state FILE] [--png out.png] [--wav out.wav] python -m gba_python ROM.gba --debug [-c COMMAND]... [--batch] python -m gba_python play ROM.gba # a window: gba_python.frontend It prints the BIOS used (retail, or the labelled free one), r12 and PC at the end, the console text of a text-mode screen (libgba's console), and every line sent through mGBA's debug port. A ROM that idles on a branch to itself stops the run early. ``--png`` writes the last frame, ``--wav`` the sound of the whole run (16-bit stereo at 32,768 Hz). ``--debug`` opens the command debugger (``gba_python.console``; ``help`` lists the commands) on the console as it stands after ``--load-state``, or at power-on: commands given with ``-c`` run first, then the prompt reads stdin unless ``--batch`` is given. """ from __future__ import annotations import argparse import sys from pathlib import Path from gba_python import bios as bios_module from gba_python import state from gba_python.board import GBA from gba_python.testrom import at_branch_to_self, read_rom, screen_png, screen_text def main(argv: list[str] | None = None) -> int: argv = sys.argv[1:] if argv is None else argv if argv[:1] == ["play"]: from gba_python.frontend import main as play return play(argv[1:]) parser = argparse.ArgumentParser(prog="gba_python", description=__doc__.split("\n\n")[0]) parser.add_argument("rom", type=Path) parser.add_argument("--bios", help="a BIOS file or No-Intro .zip (default: retail, found)") parser.add_argument("--frames", type=int, default=600) parser.add_argument("--skip-bios", action="store_true", help="start at 8000000 (development)") parser.add_argument("--keys", action="append", default=[], help="FRAME:MASK, held from FRAME") parser.add_argument("--trace", type=Path, help="write every bus access as JSON Lines") parser.add_argument("--save-state", type=Path) parser.add_argument("--load-state", type=Path) parser.add_argument("--png", type=Path, help="write the last frame as a PNG") parser.add_argument("--wav", type=Path, help="write the run's sound as a WAV file") parser.add_argument( "--no-stop", action="store_true", help="run every frame, even at a branch to itself" ) parser.add_argument("--debug", action="store_true", help="open the command debugger") parser.add_argument("-c", "--command", action="append", default=[], help="debugger command") parser.add_argument("--batch", action="store_true", help="exit after the -c commands") args = parser.parse_args(argv) if args.load_state: gba = state.load(args.load_state.read_bytes()) else: bios = bios_module.find(args.bios) gba = GBA(bios, read_rom(args.rom), skip_bios=args.skip_bios) print(f"BIOS: {gba.bios.label} ({gba.bios.source})") if args.debug: from gba_python import console return console.run(gba, args.command, args.batch) keys = {int(f): int(m, 0) for f, m in (k.split(":") for k in args.keys)} trace_file = args.trace.open("w") if args.trace else None if trace_file: from gba_python import trace trace.attach(gba, trace_file) if args.wav: gba.apu.start_capture() frame = 0 for frame in range(args.frames): if frame in keys: gba.keypad.set_pressed(keys[frame]) gba.run_frame() if not args.no_stop and at_branch_to_self(gba): break if trace_file: trace_file.close() cpu = gba.cpu print( f"frames {frame + 1}, cycles {gba.scheduler.now:,}; r12 {cpu.r[12]:#x} pc {cpu.r[15]:08x}" ) text = [row for row in screen_text(gba) if row] if text: print("screen:\n " + "\n ".join(text)) for line in gba.debug_log: print(f"debug: {line}") if args.png: args.png.write_bytes(screen_png(gba)) if args.wav: import wave from gba_python.apu import SAMPLE_RATE with wave.open(str(args.wav), "wb") as out: out.setnchannels(2) out.setsampwidth(2) out.setframerate(SAMPLE_RATE) samples = gba.apu.take_samples() if sys.byteorder == "big": samples.byteswap() out.writeframes(samples.tobytes()) if args.save_state: args.save_state.write_bytes(state.save(gba)) return 0 if __name__ == "__main__": sys.exit(main())