# SPDX-License-Identifier: MIT """Run MAME 0.285's X68000 driver headless, as a closed-box detector (docs/mame-oracle.md). python -I scripts/mame_trace.py --frames 60 # trace the 68000, log I/O writes python -I scripts/mame_trace.py --frames 60 --bios ipl13 # ... with another IPL ROM python -I scripts/mame_trace.py --snap 8 # no trace: run, save a PNG No floppy is mounted: the IPL ROM runs to its own "insert a disk" screen, which is all the board's first milestones need. The romsets come from MAME_ROMPATH (x68000.zip holds the IPL ROMs and the CGROM). Output goes to reference/mame/-/ (git-ignored): run.log, with one line per instruction ("S pc sr a7 totalcycles") and one line per watched I/O access ("W address size data totalcycles", R for reads), and snap/. MAME is run, never read: its x68k files carry BSD-3-Clause `license:` lines, but this Phase 0's brief names the driver a closed box (docs/sources.md). """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] # The I/O blocks of the X68000's map (docs/memory-map.md), as start, length in bytes. # Each is watched for writes, and the ones a polling loop would flood are not watched for # reads. WATCHES = [ ("CRTC", "e80000", "800", "w"), ("VCTL", "e82000", "800", "w"), # palette and video controller (VSOP/VICON) ("DMAC", "e84000", "100", "w"), ("AREA", "e86000", "2", "w"), ("MFP", "e88000", "40", "rw"), ("RTC", "e8a000", "20", "w"), ("PRN", "e8c000", "4", "w"), ("SYS", "e8e000", "10", "rw"), ("OPM", "e90000", "4", "w"), ("ADPCM", "e92000", "4", "w"), ("FDC", "e94000", "8", "rw"), ("SASI", "e96000", "8", "rw"), ("SCC", "e98000", "8", "rw"), ("PPI", "e9a000", "8", "w"), ("IOC", "e9c000", "4", "rw"), ("SPR", "eb0000", "8000", "w"), ] def lua(frames: int, trace: bool, watch_reads: bool) -> str: lines = [] if trace: lines += [ "local dbg = manager.machine.debugger", 'dbg.visible_cpu = manager.machine.devices[":maincpu"]', "dbg:command('trace S.trace,:maincpu,noloop," '{logerror "S %X %X %X %d\\\\n",pc,sr,a7,totalcycles}\')', ] for tag, start, length, rw in WATCHES: kinds = rw if watch_reads else rw.replace("r", "") for kind in kinds: letter = kind.upper() lines.append( f"dbg:command('wpset {start},{length},{kind},1," f'{{logerror "{letter} {tag} %X %X %X %d\\\\n",wpaddr,wpsize,wpdata,totalcycles; g}}\')' ) lines.append('dbg:command("go")') lines += [ "count = 0", "notifier = emu.add_machine_frame_notifier(function()", " count = count + 1", f" if count == {frames} then", " manager.machine.video:snapshot()", " manager.machine:exit()", " end", "end)", ] return "\n".join(lines) + "\n" def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--driver", default="x68000", help="x68000, x68ksupr, x68kxvi, x68030") parser.add_argument("--bios", default="", help="ipl10, ipl11, ipl12, ipl13, cz600ce") parser.add_argument("--frames", type=int, default=0, help="trace this many frames") parser.add_argument("--snap", type=float, default=0, help="untraced run of N seconds") parser.add_argument("--reads", action="store_true", help="also log reads of MFP/FDC/SCC/...") parser.add_argument("--rompath", default=os.environ.get("MAME_ROMPATH")) parser.add_argument("--mame", default="mame") args = parser.parse_args() if not args.rompath: print("set MAME_ROMPATH (it must hold x68000.zip)", file=sys.stderr) return 2 trace = args.frames > 0 frames = args.frames if trace else max(1, round(args.snap * 55)) what = "trace" if trace else "snap" name = f"{args.driver}-{args.bios or 'default'}-{what}" out = ROOT / "reference" / "mame" / name out.mkdir(parents=True, exist_ok=True) script = out / "run.lua" script.write_text(lua(frames, trace, args.reads), encoding="utf-8") command = [ args.mame, args.driver, "-rompath", args.rompath, "-homepath", str(out), "-snapshot_directory", str(out / "snap"), "-nvram_directory", str(out / "nvram"), "-cfg_directory", str(out / "cfg"), "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-log", "-seconds_to_run", str(int(frames / 50) + 5), "-autoboot_script", str(script), ] # fmt: skip if args.bios: command += ["-bios", args.bios] if trace: command += ["-debug", "-debugger", "none"] result = subprocess.run(["nice", "taskset", "-c", "24-31", *command], cwd=out, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) # fmt: skip (out / "stdout.txt").write_text(result.stdout, encoding="utf-8") log = out / "error.log" if log.exists(): log.replace(out / "run.log") (out / "S.trace").unlink(missing_ok=True) # the disassembly; run.log has the state print(f"exit {result.returncode}; output in {out.relative_to(ROOT)}") return result.returncode if __name__ == "__main__": sys.exit(main())