"""Dump the DAC byte stream a ROM plays, from inside megadrive-python. tools/player_timing.py measures the intervals between DAC writes and is therefore blind to the fault that matters most: a sample played twice or skipped. The pacing can be immaculate while the data has shifted, which is what "+8 samples of drift with no lost laps" means. So this records the values as well, and tools/oracle_audio.py scores them with the same lag analysis tools/blastem_audio.py uses on a BlastEm VGM - one analysis, two capture sources. Runs in the megadrive-python venv (which has no numpy), so it only writes files: /python tools/dump_dac.py ROM FRAMES out/dac -> out/dac.u8 one byte per DAC write, in order -> out/dac.mclk one little-endian u64 per write, the master clock it happened at """ from __future__ import annotations import struct import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] for sub in ("megadrive-python", "m68000-python", "z80-python"): sys.path.insert(0, str(ROOT.parent / sub / "src")) from megadrive_python.__main__ import load_image # noqa: E402 from megadrive_python.cartridge import Cartridge # noqa: E402 from megadrive_python.machine import MACHINES # noqa: E402 from megadrive_python.system import System # noqa: E402 def main(rom: Path, frames: int, out: Path, machine: str = "megadrive2") -> int: system = System(MACHINES[machine], Cartridge.from_image(load_image(rom))) fm = system.board.fm fm.enabled = True values = bytearray() times: list[int] = [] addr = [None] real = fm.write def write(port: int, value: int) -> None: p = port & 3 if p == 0: addr[0] = value elif p == 1 and addr[0] == 0x2A: values.append(value) times.append(fm.mclk) real(port, value) fm.write = write # type: ignore[method-assign] for i in range(frames): system.run_frame() fm.take_samples() system.board.psg.take_samples() if (i + 1) % 200 == 0: print(f" {i + 1}/{frames} frames, {len(values):,} DAC writes", flush=True) out.parent.mkdir(parents=True, exist_ok=True) out.with_suffix(".u8").write_bytes(bytes(values)) out.with_suffix(".mclk").write_bytes(b"".join(struct.pack(" {out.with_suffix('.u8')}") return 0 if __name__ == "__main__": raise SystemExit(main(Path(sys.argv[1]), int(sys.argv[2]), Path(sys.argv[3]), sys.argv[4] if len(sys.argv) > 4 else "megadrive2")) # fmt: skip