# SPDX-License-Identifier: MIT """Run a cartridge headless: ``python -m a2600_python ROM --frames N --png out.png --wav out.wav``. The picture is the last complete frame (:mod:`.television`, in the machine's palette, :mod:`.palettes`); the sound is every sample of the run (:mod:`.audio`), written at the tick rate, two a line, as 16-bit mono. """ from __future__ import annotations import argparse import struct import sys import wave from pathlib import Path from . import MACHINES, System from .palettes import PALETTES from .television import Television, frame_png def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(prog="a2600_python", description=__doc__.split("\n\n")[0]) parser.add_argument("rom", type=Path) parser.add_argument("--frames", type=int, default=60) parser.add_argument("--machine", default="ntsc", choices=("ntsc", "pal", "secam")) parser.add_argument("--scheme", help="bank-switching scheme (default: identified)") parser.add_argument("--png", type=Path, help="write the last frame here") parser.add_argument( "--palette", choices=sorted(PALETTES), help="the PNG's palette (default: the machine's)" ) parser.add_argument("--wav", type=Path, help="write the sound here") args = parser.parse_args(argv) options = {"scheme": args.scheme} if args.scheme else {} system = System.from_rom(args.rom, MACHINES[f"Atari VCS ({args.machine.upper()})"], **options) television = Television(system.board.tia) audio = system.board.tia.audio samples: list[float] = [] for _ in range(args.frames): system.run_frames(1) samples.extend(audio.take_samples()) if args.png: if television.frame is None: print("no complete frame", file=sys.stderr) return 1 palette = PALETTES[args.palette or system.machine.palette] args.png.write_bytes(frame_png(television.frame, palette=palette)) if args.wav: timing = system.machine.timing rate = round(2 * timing.colour_clock_hz / 228) # two ticks a line with wave.open(str(args.wav), "wb") as out: out.setnchannels(1) out.setsampwidth(2) out.setframerate(rate) out.writeframes(b"".join(struct.pack("