# SPDX-License-Identifier: MIT """Run a cart headless: ``python -m lynx_python CART --frames N --png out.png``. The cart is any image the front door takes (.lnx, .lyx, BLL; loose, .zip, .7z), read in place. The boot ROM is found by hash through ``$MAME_ROMPATH`` (lynx_python.bootrom); without one the cart is decrypted by the host. ``--png`` writes the last finished frame, turned for the cart's rotation; ``--wav`` the run's sound (Mikey.mix, 32 kHz). """ from __future__ import annotations import argparse import sys import time from pathlib import Path from lynx_python import bootrom from lynx_python.board import Lynx from lynx_python.cart import bll, eeprom, load from lynx_python.machine import MACHINES from lynx_python.mikey import SAMPLE_RATE from lynx_python.picture import save, write_png from lynx_python.sound import write_wav __all__ = ["boot", "main", "run_frames", "save", "write_png"] def boot(cart: Path, machine: str = "lynx2", description=None) -> Lynx: description = description or load(cart) rom = bootrom.find() if rom is None: # The host-decryption path (cart.decrypt) is checked against the ROM (M1) but # not yet wired to start a cart: the boot ROM's hand-over state is owed. raise SystemExit("no Lynx boot ROM found under $MAME_ROMPATH (lynx.zip)") payload, block = description.payload, description.bank0_block if description.boot == "ram": # A BLL program goes behind the BLL kit's cart loader (lynx_python.cart.bll). payload, block = bll.as_cart(payload) board = Lynx(payload, block, machine=MACHINES[machine], boot_rom=rom, bank1=description.bank1, block1=description.bank1_block) # fmt: skip # The cart's EEPROM, if its description has one, saved under the user's data # directory by payload SHA-1 (lynx_python.cart.eeprom), never beside the ROM. board.port.eeprom = eeprom.for_cart(description) board.reset() return board def run_frames(board: Lynx, frames: int) -> None: display = board.mikey.display target = display.frames + frames step = board.step while display.frames < target: step() def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("cart", type=Path) parser.add_argument("--frames", type=int, default=60) parser.add_argument("--machine", default="lynx2", choices=sorted(MACHINES)) parser.add_argument("--png", type=Path) parser.add_argument("--rotation", choices=("none", "left", "right"), help="override the cart's rotation field for --png") # fmt: skip parser.add_argument("--wav", type=Path, help="write the run's sound (32 kHz, 16-bit stereo)") parser.add_argument("--eeprom", help="override the cart's EEPROM: none, or TYPE[:ORG] " "(e.g. 93C46:16)") # fmt: skip args = parser.parse_args(argv) description = load(args.cart, overrides={"eeprom": args.eeprom} if args.eeprom else None) board = boot(args.cart, args.machine, description) if args.wav: board.mikey.start_audio(board.tick) started = time.process_time() run_frames(board, args.frames) spent = time.process_time() - started if args.wav: write_wav(args.wav, board.mikey.samples, SAMPLE_RATE) if args.png: # The picture as the player sees it: turned for the cart's rotation field. save(args.png, board.mikey.display.rgb(), args.rotation or description.rotation) seconds = board.tick / 16_000_000 print(f"{args.frames} frames, {seconds:.2f} emulated s in {spent:.1f} s CPU " f"({seconds / spent if spent else 0:.3f}x real time)") # fmt: skip return 0 if __name__ == "__main__": sys.exit(main())