#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Trace an ARMv6 CPU in MAME 0.285 (a closed-box detector, tier 5). MAME has no 3DS machine and no ARM11 MPCore device. The nearest ARMv6 cores it has are "ARM1176JZF-S" (in its ARM7-family core; machines ``iphone2g`` and ``kov3hd``) and "ARM11" (machine ``zeebo``), all found with ``mame -listdevices`` on 2026-11-26. This script runs ``iphone2g``'s boot ROM (Apple's firmware, found through MAME_ROMPATH, never copied, never read) for a few frames with the debugger's instruction trace on, so the build session has an ARMv6 instruction stream to diff its decoder and its ARMv6 semantics against. MAME's verdicts are an emulator's, not the hardware's. MAME_ROMPATH=... python -I scripts/mame_trace.py --state MAME_ROMPATH=... python -I scripts/mame_trace.py --frames 1 --regs CPSR,R0,R13 \\ --out tests/mame_traces/iphone2g-boot.trace All output lands in tests/mame_traces/ (git-ignored), which is the run's cwd. """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] RUNS = ROOT / "tests" / "mame_traces" LUA = """ dbg = manager.machine.debugger cpu = manager.machine.devices['{tag}'] if {state} then local names = {{}} for k, _ in pairs(cpu.state) do names[#names + 1] = k end table.sort(names) print('STATE {tag} ' .. table.concat(names, ' ')) manager.machine:exit() else dbg.visible_cpu = cpu dbg:command('trace {out},,noloop{action}') dbg:command('go') frames = 0 sub = emu.add_machine_frame_notifier(function() frames = frames + 1 if frames == {stop} then dbg:command('trace off') print(string.format('TIME %.6f s', manager.machine.time:as_double())) manager.machine:exit() end end) end """ def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--machine", default="iphone2g") parser.add_argument("--tag", default=":maincpu") parser.add_argument("--frames", type=int, default=1) parser.add_argument("--regs", default="", help="comma-separated MAME state symbols") parser.add_argument("--state", action="store_true", help="print state symbols, trace nothing") parser.add_argument("--out", type=Path, default=RUNS / "boot.trace") parser.add_argument("--mame", default="mame") args = parser.parse_args() rompath = os.environ.get("MAME_ROMPATH") if not rompath: print("set MAME_ROMPATH (MAME's ';'-separated rompath)", file=sys.stderr) return 2 RUNS.mkdir(parents=True, exist_ok=True) regs = [r for r in args.regs.split(",") if r] action = "" if regs: fmt = " ".join(f"{r}=%08X" for r in regs) + " " action = ',{tracelog "' + fmt + '",' + ",".join(regs) + "}" out = args.out.resolve() script = RUNS / "trace.lua" script.write_text(LUA.format(tag=args.tag, state="true" if args.state else "false", out=out.as_posix(), action=action, stop=args.frames), encoding="utf-8") command = [ "nice", args.mame, args.machine, "-rompath", rompath, "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-debug", "-debugger", "none", "-autoboot_script", str(script), "-seconds_to_run", str(args.frames // 60 + 5), ] result = subprocess.run(command, cwd=RUNS, capture_output=True, text=True, check=False) for line in result.stdout.splitlines(): if line.startswith(("STATE", "TIME")): print(line) if args.state: return 0 if not out.exists(): print(result.stdout[-2000:], result.stderr[-2000:], sep="\n", file=sys.stderr) return 1 with out.open("rb") as handle: count = sum(1 for _ in handle) print(f"{args.machine}{args.tag}: {count} instructions traced to {out.relative_to(ROOT)}") return 0 if __name__ == "__main__": sys.exit(main())