#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Run MAME 0.285's Sega CD driver headless: snapshots of chosen frames, optional traces. MAME is a **detector** for this board (docs/validation.md, docs/mame-oracle.md): its Sega CD is the weakest of the three referees (it fails ten mcd-verificator rows and stops at CDC INIT, 2026-10-21), but it is the only one with a debugger that traces both 68000s and the Z80 by instruction. The run happens in a fresh directory (``--work``, default ``runs/mame``) so MAME's cfg, nvram (backup RAM) and snapshots never mix with another run's. The BIOS comes from ``--rompath`` (a MAME romset directory with ``segacd.zip`` etc.; bring your own) or, with ``--bios-is-verificator``, from KRIKzz's mcd-verificator copied into the work directory under the BIOS's file name; MAME reports the wrong checksum and runs it. python scripts/mame_cd.py DISC.chd --rompath ROMS --frames 1500 python scripts/mame_cd.py DISC.chd --bios-is-verificator --frames 600,3600 python scripts/mame_cd.py DISC.chd --rompath ROMS --frames 300 --trace # writes main.tr (maincpu), sub.tr (segacd_68k), z80.tr up to the last frame The sub CPU's tag is ``:segacd:segacd_68k`` (``mame segacd -listdevices``). """ from __future__ import annotations import argparse import shutil import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] VERIFICATOR = ROOT / "reference/src/mcd-verificator/mcd-verificator/mcd-verificator.bin" #: The file name of each driver's first BIOS (MAME's mdconsole.cpp), for #: --bios-is-verificator. BIOS_NAME = {"segacd": "mpr-15045b.bin", "megacd": "megacd_model1_bios_1_00_e.bin", "megacdj": "epr-14088e.bin"} # fmt: skip SNAP_LUA = """ local wanted = {{{frames}}} local last = {last} local n = 0 local set = {{}} for _, f in ipairs(wanted) do set[f] = true end emu.register_frame_done(function() n = n + 1 if set[n] then manager.machine.video:snapshot() end if n >= last then manager.machine:exit() end end) """ #: Prepended to the snapshot script with --trace: MAME's debugger runs headless #: (``-debug -debugger none``) and takes its commands from Lua, not ``-debugscript`` #: (which does nothing with ``-debugger none``; found 2026-10-21). One file per CPU: #: pc, sr and the cycle count for each 68000; pc and the cycle count for the Z80. TRACE_LUA = """ local dbg = manager.machine.debugger dbg:command('trace main.tr,:maincpu,noloop,{{tracelog "%X %X %d\\n",curpc,sr,totalcycles}}') dbg:command('trace sub.tr,:segacd:segacd_68k,noloop,{{tracelog "%X %X %d\\n",curpc,sr,totalcycles}}') dbg:command('trace z80.tr,:genesis_snd_z80,noloop,{{tracelog "%X %d\\n",curpc,totalcycles}}') dbg:command('go') """ def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("disc", type=Path) parser.add_argument("--driver", default="segacd", choices=sorted(BIOS_NAME)) parser.add_argument("--frames", required=True, help="comma-separated frame numbers") bios = parser.add_mutually_exclusive_group(required=True) bios.add_argument("--rompath", type=Path, help="a MAME romset directory (your own)") bios.add_argument("--bios-is-verificator", action="store_true") parser.add_argument("--trace", action="store_true", help="trace all three CPUs") parser.add_argument("--work", type=Path, default=ROOT / "runs" / "mame") parser.add_argument("--mame", default="mame") args = parser.parse_args() work = args.work.resolve() work.mkdir(parents=True, exist_ok=True) if args.bios_is_verificator: roms = work / "roms" (roms / args.driver).mkdir(parents=True, exist_ok=True) shutil.copyfile(VERIFICATOR, roms / args.driver / BIOS_NAME[args.driver]) rompath = roms else: rompath = args.rompath.resolve() frames = sorted({int(n) for n in args.frames.split(",")}) lua = SNAP_LUA.format(frames=", ".join(map(str, frames)), last=max(frames) + 1) (work / "snap.lua").write_text((TRACE_LUA.format() if args.trace else "") + lua) command = [args.mame, args.driver, "-rompath", str(rompath), "-cdrom", str(args.disc.resolve()), "-video", "none", "-sound", "none", "-nothrottle", "-skip_gameinfo", "-snapshot_directory", "snap", "-autoboot_script", "snap.lua"] # fmt: skip if args.trace: command += ["-debug", "-debugger", "none"] result = subprocess.run(command, cwd=work, check=False) for path in sorted((work / "snap" / args.driver).glob("*.png")): print(path) return result.returncode if __name__ == "__main__": sys.exit(main())