"""The board's picture of chosen frames, as ``fNNNNN.png``: the other half of ``scripts/blastem_frames.py``, for ``scripts/compare_frames.py``. A frame is the VDP's count since power-on (the window's title bar shows the same number). The FM generator is off unless ``--fm``: nothing a game can observe changes (docs/worklog.md, milestone 4) and it is about three times faster. For a run to set against the referee's (``reference/nuked-od2``, docs/worklog.md Session 13), three more things: - ``--reset-mclk T`` retains the legacy instruction-boundary reset, which can overshoot T when a frame crosses it. Add ``--reset-exact`` to schedule T, rounded to the CPU clock, using a preliminary run to find the preceding instruction. For the standard Nuked-MD Overdrive 2 capture, use T = 26,601,719: its physical reset pulse at 26,841,996 minus the measured VDP offset of 240,277 MCLK. The die's button press at 25 x 1,070,460 is not the board's reset-pulse time. - ``--audio DIR`` records the sound chips' own output: ``fm.bin``, the YM2612's samples (one per 24 internal cycles, the sum of the 24 multiplexed DAC values) as int32 left, right; ``psg.bin``, the PSG's digital state per sample (four attenuations, one byte each). Needs ``--fm`` for the FM half. - ``--snapshots DIR --snapshot-every N`` pickles the whole system every N frames, so a later look at one scene starts from the snapshot before it. python scripts/board_frames.py ROM OUT --range 4100:5200:25 [--machine megadrive] python scripts/board_frames.py ROM OUT --range 1:20400:1 --fm \\ --reset-mclk 26601719 --reset-exact \\ --audio OUT --snapshots OUT --snapshot-every 1000 """ from __future__ import annotations import argparse import pickle import struct import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) from megadrive_python.__main__ import load_image from megadrive_python.cartridge import Cartridge from megadrive_python.machine import MACHINES from megadrive_python.png import write_png from megadrive_python.system import System def reset_boundary(probe: System, at: int) -> int: """Consume a preliminary run to find the instruction containing the reset time.""" if at < probe.mclk: raise ValueError("exact reset precedes the initial CPU clock") boundary = probe.mclk while probe.mclk <= at: boundary = probe.mclk probe.step() return boundary def run_reset_frame(system: System, at: int, boundary: int) -> bool: """Run one frame, returning whether the scheduled reset is still pending.""" pending = True frame = system.vdp.frame while system.vdp.frame == frame: if pending: if system.mclk > boundary: raise RuntimeError("capture run passed the probed reset boundary") if system.mclk == boundary: system.press_reset(at=at) pending = False system.step() system.board.sound_sync(system.mclk) return pending def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("rom", type=Path) parser.add_argument("out", type=Path) parser.add_argument("--machine", default="megadrive", choices=sorted(MACHINES)) parser.add_argument("--frames", default="", help="comma-separated frame numbers") parser.add_argument("--range", default="", help="first:last:step, inclusive") parser.add_argument("--fm", action="store_true", help="run the FM generator too") parser.add_argument("--reset-mclk", type=int, default=-1, help="press reset here") parser.add_argument("--reset-exact", action="store_true", help="schedule --reset-mclk precisely, with a preliminary CPU run") parser.add_argument("--audio", type=Path, help="record fm.bin and psg.bin here") parser.add_argument("--snapshots", type=Path, help="pickle the system here") parser.add_argument("--snapshot-every", type=int, default=1000) parser.add_argument("--tmss-rom", type=Path, help="a TMSS machine locks up at its first VDP access without one") # fmt: skip args = parser.parse_args() if args.reset_exact and args.reset_mclk < 0: parser.error("--reset-exact requires --reset-mclk") wanted = {int(n) for n in args.frames.split(",") if n} if args.range: first, last, step = (int(n) for n in args.range.split(":")) wanted |= set(range(first, last + 1, step)) if not wanted: parser.error("no frames asked for") args.out.mkdir(parents=True, exist_ok=True) image = load_image(args.rom) tmss_rom = load_image(args.tmss_rom) if args.tmss_rom else None system = System(MACHINES[args.machine], Cartridge.from_image(image), tmss_rom=tmss_rom) system.board.fm.enabled = args.fm boundary = None if args.reset_exact: try: boundary = reset_boundary(system, args.reset_mclk) except ValueError as error: parser.error(str(error)) system = System(MACHINES[args.machine], Cartridge.from_image(image), tmss_rom=tmss_rom) system.board.fm.enabled = args.fm vdp = system.vdp fm_file = psg_file = None if args.audio: args.audio.mkdir(parents=True, exist_ok=True) fm_file = (args.audio / "fm.bin").open("wb") psg_file = (args.audio / "psg.bin").open("wb") if args.snapshots: args.snapshots.mkdir(parents=True, exist_ok=True) reset_pending = args.reset_mclk >= 0 while vdp.frame < max(wanted): if reset_pending and boundary is not None: reset_pending = run_reset_frame(system, args.reset_mclk, boundary) else: if reset_pending and system.mclk + 1_000_000 >= args.reset_mclk: # Preserve the original capture mode for existing comparisons. while system.mclk < args.reset_mclk: system.step() system.press_reset() reset_pending = False system.run_frame() fm = system.board.fm.take_samples() if psg_file: psg_file.write(b"".join(bytes(a) for a in system.board.psg.take_attenuations())) fm_file.write(b"".join(struct.pack("