# MAME as a closed-box detector: the recipe as run MAME 0.285 (`mame -version`: `0.285 (unknown)`, the Debian package) is run as a binary; its source is never fetched ([sources.md](sources.md)). One process at a time, short runs, `nice` and `taskset -c 24-31`. Romsets come from `MAME_ROMPATH` (non-merged), read in place. Outputs go to `reference/mame/` (git-ignored). ## Commands run on 2026-11-26, with their counts | Command | Runs | Result | | --- | --- | --- | | `mame -listsource`, `mame -listxml <143 sets>` (inside `scripts/census.py`) | 1 + 1 | 143 sets in m72/m90/m92/m107/m62.cpp | | `mame -listdevices rtype`, `gunforce` | 2 | device lists quoted in boards.md | | address-map dumps (`scripts/mame_maps.lua`; `mame_trace.py --maps`) | 10 (`rtype`, `nspirit`, `bchopper`, `rtype2`, `hharry`, `cosmccop`, `dynablst`, `gunforce`, `inthunt`, `airass`) | memory-map.md | | `scripts/reset_bytes.py` (reset-vector bytes, `mame_reset_bytes.lua`) | 118 sets (every set with a NEC CPU), 1 emulated second each, plus 3 for the test | 164 NEC CPUs: protection.md | | traces (`mame_trace.py --start 900 --frames 3`, and frames 0-4 once) | 12: `rtype` main 4 (boot frames 1-4 once; frames 901-903 once without `noloop`, twice with), `rtype` sound 2, `nspirit` MCU 2, `gunforce` main 2 and sound 2 (each once without `noloop`, once with) | `rtype` main and sound, `nspirit` MCU, `gunforce` main and sound: domains-and-speed.md | | snapshots (`mame_trace.py --snap 20`) | 2 (`rtype`, `gunforce`) | R-Type in attract-mode play at about frame 1,100; Gunforce's title screen | | Lua probes (logerror, state names, palettes) | 3 | `manager.machine:logerror` works; palette sizes 512 (`rtype`) and 8192 (`gunforce`) | ## The trace recipe ```sh MAME_ROMPATH=... python -I scripts/mame_trace.py rtype --start 900 --frames 3 MAME_ROMPATH=... python -I scripts/mame_trace.py rtype --start 900 --frames 3 --cpu sound --z80 MAME_ROMPATH=... python -I scripts/mame_trace.py nspirit --start 900 --frames 3 --cpu mcu MAME_ROMPATH=... python -I scripts/mame_trace.py gunforce --start 900 --frames 3 --cpu sound ``` The script writes an autoboot Lua script that turns the debugger's `trace` on at frame `--start` with a `logerror` action per instruction, and marks every frame with `manager.machine:logerror("FRAME n")`, so `run.log` holds the instruction lines and the frame markers in order; `summary.txt` gives instructions, clocks and clocks per instruction per frame. Command line (as built by the script): ```text mame -rompath $MAME_ROMPATH -homepath -cfg_directory /cfg -nvram_directory /nvram -snapshot_directory /snap -video none -sound none -nothrottle -noreadconfig -skip_gameinfo -log -verbose -seconds_to_run N -autoboot_script /run.lua -debug -debugger none ``` ### Traps found - **Pass `noloop`.** Without it MAME's trace detects loops and collapses a repeated loop into one line, and the `logerror` action runs only for the lines it writes: on `rtype` frames 901-903 the counts came out at 1,405-2,346 instructions per frame with 104 apparent "long" instructions, against 12,492-14,566 with `noloop` and no instruction longer than 100 clocks (checked 2026-11-26). i8086-python's recipe passes `noloop`, rightly. - State names: the NEC cores (V30, V33, V35) use `PS PC AW BW CW DW SP BP IX IY DS0 DS1 SS PSW` and `CURPC` (Lua `device.state` keys, printed by a probe); `curpc` is the 20-bit linear address. Z80: `pc af bc de hl sp`. i8751: `pc acc psw sp`. - No CPU halted in the traced frames: every one spins in an idle loop, so `totalcycles` between frame markers equals the clock divided by the frame rate (a check on the clock figures in boards.md). - Map dumps: entries come back in hash order, and MAME's handlers are anonymous (`delegate`); meaning comes from documents ([memory-map.md](memory-map.md)). - The translated V35s: MAME's trace shows decoded instructions; raw memory reads (`space:read_u8`) return the encrypted bytes, which is what `reset_bytes.py` relies on. ## A verified sample | Set, CPU | Instructions per frame (901-903) | Clocks per frame | Clocks per instruction | | --- | --- | --- | --- | | `rtype` V30 | 12,645 / 14,566 / 12,492 | 145,379-145,395 | 11.0 | | `rtype` Z80 | 8,130 / 8,064 / 8,131 | 65,050-65,060 | 8.0 | | `nspirit` i8751 | 8,064 / 8,065 / 8,064 | 12,115-12,117 (machine cycles) | 1.5 | | `gunforce` V33 | 28,246 / 28,245 / 28,072 | 149,957-149,964 | 5.3 | | `gunforce` V35 | 15,725 / 15,730 / 15,728 | 119,276-119,291 | 7.6 | Frames 1-4 of `rtype` (boot, with `noloop`): 11,185-11,186 instructions, 145,392-145,405 clocks each.