"""Produce a galemu board reference run: canonical JSON of events and snapshot. galemu is the oracle for the board, as z80-python is for the CPU. This driver configures galemu's own ``GalaxianBoard`` (unchanged) the way the Rust board is driven: optional vblank NMIs, optional memory-write events, and inputs applied before stated instruction counts. Usage: python oracle/galemu_run.py --rom-path rom/galaxian.zip --instructions 50000 \ [--vblank] [--no-memory-writes] [--input 20000:coin_1:1 --input 20060:coin_1:0 ...] python oracle/galemu_run.py --rom-path rom/galaxian.zip --frames 303 --no-memory-writes \ [--input 120:coin_1:1 ...] # inputs keyed by frame The output is ``galemu.trace.serialize_run`` exactly, one line. """ from __future__ import annotations import argparse from pathlib import Path from galemu.board import GalaxianBoard from galemu.trace import serialize_run def parse_input(text: str) -> tuple[int, str, bool]: at, control, pressed = text.split(":") return int(at), control, pressed == "1" def main() -> int: parser = argparse.ArgumentParser() parser.add_argument("--rom-path", type=Path, required=True) parser.add_argument("--instructions", type=int, help="stop after this many instruction boundaries") parser.add_argument("--frames", type=int, help="stop after this many vblank frames (implies --vblank); inputs are keyed by frame") parser.add_argument("--vblank", action="store_true", help="schedule the vblank NMI as the display does") parser.add_argument("--no-memory-writes", action="store_true", help="do not record RAM writes as events (the display's mode)") parser.add_argument("--input", action="append", default=[], metavar="AT:CONTROL:PRESSED", help="apply set_input(CONTROL, PRESSED) before instruction AT (0-based count of instructions executed)") parser.add_argument("--output", type=Path) args = parser.parse_args() if (args.instructions is None) == (args.frames is None): parser.error("give exactly one of --instructions and --frames") board = GalaxianBoard.from_rom_source( args.rom_path, enable_vblank=args.vblank or args.frames is not None ) board.trace_memory_writes = not args.no_memory_writes inputs = sorted(parse_input(text) for text in args.input) next_input = 0 if args.frames is not None: # The display's loop: inputs land before a frame, then the ROM runs # until the vblank scheduler advances exactly one frame. for frame in range(args.frames): while next_input < len(inputs) and inputs[next_input][0] <= frame: _, control, pressed = inputs[next_input] board.set_input(control, pressed) next_input += 1 target = board.vblank_count + 1 while board.vblank_count < target: board.step() else: while board.instruction_count < args.instructions: while next_input < len(inputs) and inputs[next_input][0] <= board.instruction_count: _, control, pressed = inputs[next_input] board.set_input(control, pressed) next_input += 1 board.step() evidence = serialize_run(board.events, board.snapshot()) if args.output is None: print(evidence) else: args.output.write_text(evidence + "\n", encoding="utf-8") return 0 if __name__ == "__main__": raise SystemExit(main())