# MAME as the MB88xx oracle: tracing a Namco MCU headlessly MAME 0.285 will emit one line per executed instruction of a Namco custom's MB88xx, with its page, program counter, accumulator, Y register, all six flags and a running cycle count, from a headless run with no display and no sound. That is a per-instruction lockstep oracle for a core, and it is **emulator-derived**: tier 3, a detector. A divergence between a core and this trace is a bug in one of them, and which one is decided by the Fujitsu datasheet, not by MAME. See [validation.md](validation.md). The recipe was first proven in the `../emu-galaga` work (its `docs/build-record.md`, stage 4: "Its output was captured by making the MCU the debugger's visible CPU and logging registers per instruction"). It was **re-run from scratch for this document on 2026-09-12**, and every number below is from those runs on this machine. ## Environment | | | | --- | --- | | MAME | `/usr/games/mame`, `-version` prints `0.285 (unknown)` | | ROM set | `galaga.zip` (non-merged, includes `51xx.bin` and `54xx.bin`) in `$MAME_ROMPATH/`, used **in place** with `-rompath`. No ROM byte is copied anywhere. | | Display | none needed. `-video none -sound none` is enough; no `DISPLAY`, no `xvfb-run`, no `SDL_VIDEODRIVER`. | | Device tags | `:54xx:mcu` (MB8844) and `:51xx:mcu` (MB8843). The Namco device wraps the CPU, so the CPU tag is nested. | | Licence of the code cited | BSD-3-Clause (`mb88xx.cpp` copyright Ernesto Corvi; `namco5*.cpp` copyright Aaron Giles, Mike Harris) | ## The command line ```sh cd "$OUTDIR" # <- error.log and the trace file land HERE, see below TRACE_MCU=:54xx:mcu TRACE_FRAMES=2500 \ timeout -s KILL 900 /usr/games/mame galaga \ -rompath "$MAME_ROMPATH" \ -video none -sound none -nothrottle -noreadconfig -skip_gameinfo \ -debug -debugger none -log \ -homepath "$OUTDIR" -cfg_directory "$OUTDIR/cfg" \ -nvram_directory "$OUTDIR/nvram" -snapshot_directory "$OUTDIR/snap" \ -autoboot_script "$OUTDIR/trace_mcu.lua" ``` The runnable versions are [`scripts/mame/trace_mcu.sh`](../scripts/mame/trace_mcu.sh) and [`scripts/mame/trace_mcu.lua`](../scripts/mame/trace_mcu.lua). What each piece does: - `-debug -debugger none` turns the debugger **engine** on with no UI, which is what makes `manager.machine.debugger` exist in Lua. Without `-debug` the Lua `debugger` object is nil; with a real debugger the run stops for input. - `-log` creates `error.log`, which is where the `logerror` action writes. - `-noreadconfig` keeps any `mame.ini` out of it; `-skip_gameinfo` skips the warning screen; `-nothrottle` runs flat out. - `timeout -s KILL` because MAME under `-video none` ignores SIGTERM. - MAME prints `Warning: -video none doesn't make much sense without -seconds_to_run`. The Lua script exits the machine itself, so `-seconds_to_run` is not used (with it, MAME also writes an unwanted snapshot at exit). The `ALSA ... /dev/snd/seq` line on stderr is noise. ## The Lua script ```lua local frames = tonumber(os.getenv("TRACE_FRAMES") or "800") local tag = os.getenv("TRACE_MCU") or ":54xx:mcu" local dbg = manager.machine.debugger local mcu = manager.machine.devices[tag] dbg.visible_cpu = mcu dbg:command('trace FILE,,noloop,{logerror "%X %X %X %X %d %d\n",pa,pc,a,y,curflags,totalcycles}') dbg:command("go") sub = emu.add_machine_frame_notifier(function () if manager.machine.screens[":screen"]:frame_number() >= frames then dbg:command("traceflush") manager.machine:exit() end end) ``` Line by line: - `dbg.visible_cpu = mcu` is the whole trick. The debugger's `trace` command traces the *visible* CPU, and MAME's visible CPU defaults to `maincpu`. Point it at the MCU and `trace` follows the MCU. - `trace FILE,,noloop,{action}` — filename, empty CPU field (meaning the visible one), `noloop` to suppress MAME's "(loops for N instructions)" collapsing, and a braced action run at every instruction. - The action's `logerror` writes to `error.log`. This is the useful output: six fixed fields per line and nothing else. - `sub` is a **global** on purpose. A frame notifier held in a local is garbage-collected and stops firing silently (recorded in emu-galaga's `docs/mame-oracle.md`; the same rule applies here). - `traceflush` before `exit()` or the last few thousand lines are lost. ## What comes out `error.log` gets three boilerplate lines from MAME's own startup and then one line per instruction: ```text [:namco] Namco: freq fractional bits = 16: internal freq = 192000, output freq = 192000 Soft reset [:06xx] (no context): 06XX ':06xx' write in read mode 10 0 0 0 0 1 0 0 6 0 0 1 1 0 7 0 0 3 2 0 8 0 0 3 3 0 9 0 0 3 4 0 A 0 0 3 5 0 B 0 1 1 6 0 A 0 1 1 7 ``` Fields, in order: **PA, PC, A, Y** (hexadecimal, no prefix), **curflags** (decimal), **totalcycles** (decimal). Filter with `awk 'NF==6'` to drop the boilerplate. Reading those first lines: the MCU starts at PA 0, PC 0 with flags 1 (ST set, everything else clear — exactly MAME's reset state), and the instruction there costs one cycle and lands at PC 6. Then a short loop between PC 0x0A and 0x0B with Y incrementing: `54xx.bin` clearing its 64 nibbles of RAM. ### `curflags` bit assignment From `mb88xx.cpp`'s `state_export`: | Bit | Value | Flag | | --- | --- | --- | | 0 | 1 | ST | | 1 | 2 | ZF | | 2 | **4** | **CF** | | 3 | 8 | VF | | 4 | 16 | SF | | 5 | 32 | IF (the /IRQ pin level) | **Bit 2 is the carry, and on the 54xx the carry is how the two O-port channels are told apart** — `OUTO` writes O3-O0 when CF = 0 and O7-O4 when CF = 1 (see [start-here.md](start-here.md)). A trace with the flags column is therefore a complete record of which channel every output went to, without decoding the instruction stream at all. ## Gotchas, all of them load-bearing - **The full program counter is `(pa << 6) | pc`.** Neither column alone is an address. `PA 2, PC 1F` is `0x09F`. - **`error.log` and the trace file land in the current working directory**, *not* under `-homepath`. Verified directly: a run with `cwd` and `-homepath` pointed at different directories put `error.log` and `FILE` in `cwd` and left `-homepath` holding only `cfg/`. `cd` first. - **`tracelog` produces nothing.** Re-verified here: substituting `dbg:command('tracelog "%X ...",pa,pc,...)` for the `trace ... logerror` form ran to completion at 5,702 % speed and left an `error.log` of 159 bytes — the three boilerplate lines and no trace at all. Use `trace FILE,,noloop,{logerror ...}`. - **`focus` breaks the actions.** Focusing the debugger on the MCU instead of setting `visible_cpu` stops the trace actions firing (recorded in the emu-galaga work; not re-tested here). - **The Lua `device.debug:bpset` API segfaults** MAME 0.285 (same source; not re-tested). Do not set breakpoints from Lua — drive everything through `dbg:command(...)` strings. - The trace action's format string is authoritative: extra values after the last conversion specifier are **silently dropped**. An action written `"%X %X %X %X %d %d\n",pa,pc,a,y,curflags,totalcycles,frame` runs without complaint and never prints `frame`. Count your specifiers. - The `FILE` argument to `trace` gets MAME's **disassembly** of every instruction, which is a listing of a copyrighted mask ROM. It is useful while debugging and must not be committed; `.gitignore` covers `FILE`, `error.log` and `*.log`. ## Verified runs, 2026-09-12 | Run | Device | Frames | Wall | MAME speed | Trace lines | Final `totalcycles` | | --- | --- | ---: | ---: | ---: | ---: | ---: | | A | `:54xx:mcu` | 800 | 12 s | 778 % | **406,856** | 428,179 | | B | `:54xx:mcu` | 2,500 | 40 s | 137 % | **7,328,972** | 7,608,979 | | C | `:51xx:mcu` | 2,500 | 40 s | 140 % | **6,115,866** | 7,608,979 | Run B's `error.log` is 7.3 million lines and 137 MB; the `FILE` disassembly beside it is another 87 MB. Run C's is 116 MB. Budget disk, not time. ### The numbers check out against the hardware - **The instruction rate.** Both MCUs reach 7,608,979 cycles in 2,500 frames. Galaga holds the 51xx and 54xx in reset through its power-on self-test and releases them at frame 699 (emu-galaga measured that independently). 1,801 running frames × 4,224 cycles per frame = 7,607,424 — within 0.02 % of the trace, the remainder being where in frame 699 the LS259 latch is written. The 256 kHz instruction rate of [timing.md](timing.md) is confirmed end to end. - **Cycles per instruction.** Run B: 7,608,979 / 7,328,972 = 1.038. Run A: 1.052. Consistent with a table where 237 of 256 opcodes cost 1 cycle. - **The 3-cycle interrupt entry.** In run B the 54xx's last instruction before its first interrupt is at cycle 60,746 and the first instruction of the handler, at `PA 0, PC 2`, is at cycle 60,750. One cycle for the instruction plus **three** for the entry. MAME's `burn_cycles(3); // ?` is what a core must reproduce to stay in lockstep — and what neither of us can prove. - **The vector.** Every interrupt entry in both traces lands at `PA 0, PC 2`, i.e. address `0x002`, the external-interrupt vector. ### What the two programs actually do **54xx, run B (2,500 frames):** exactly **4** external interrupts, no timer interrupt, no serial interrupt. They fall at cycles 60,750 / 61,603 / 62,456 / 62,627 — gaps of 853, 853 and 171 cycles. Galaga sends the 54xx a 12-byte boot burst under 06xx control `0xA8`, which is divider 2⁵, 1.5 kHz, **170.67 MCU cycles per byte**. So the four interrupts are four *commands* at 5, 5, 1 and 1 bytes: `3x` + 4 parameters, `4x` + 4 parameters, `1x`, `2x` — exactly the burst emu-galaga recorded (`30 40 00 02 df 40 30 30 03 df 10 20`). **The MCU takes one interrupt per command and polls the /IRQ pin with `TSTI` for the parameter bytes that follow**, which is why 12 bytes produce 4 interrupts. That is a fact about `54xx.bin` that falls straight out of the trace and that a core must reproduce. The trace visits 360 distinct (PA, PC) pairs across 8 of the 16 pages. `OUTO` at address `0x09F` (PA 2, PC 0x1F) executes **76,295** times, and `curflags & 4` — the carry — is set on **every single one**: all output goes to channel 1, O7-O4, confirming emu-galaga's finding that channels 0 and 2 never move in Galaga. The accumulator values at that instruction are 0, 1, 2, 3, 4, 6, 8, B, D and F, which is the envelope ladder that project measured (13, 15, 11, 8, 6, 4, 3, 2, 1) plus silence. **51xx, run C (2,500 frames):** **7,140** external interrupts — about four per frame, the game's 3-byte input poll plus its command writes — and **zero** timer and serial interrupt entries. The 51xx's /TC pin is wired to vblank, so it is counting vblanks, but its program polls VF with `TSTV` rather than vectoring on it. Its interrupt handler at `0x002` falls through to a two-byte instruction at `0x004` and another at `0x006`, so the three vector slots are contiguous code, not three separate handlers. ## Tracing the 51xx instead Change one environment variable: ```sh TRACE_MCU=:51xx:mcu ./scripts/mame/trace_mcu.sh 2500 out-51xx ``` Everything else is identical. The other three customs are reachable the same way on the games that have them: `:50xx:mcu` (Xevious), `:50xx_1:mcu` / `:50xx_2:mcu` and `:52xx:mcu` (Bosconian), `:53xx:mcu` (Dig Dug, Pole Position). `mame -listdevices ` prints the tags. ## Using a trace in lockstep The comparison is per instruction, six fields, no tolerance: 1. Run MAME with the script; keep `error.log`, discard `FILE`. 2. Run the core on the same program with a host that delivers the same /IRQ, /TC and /RESET edges at the same cycle counts. Getting the edges right is the hard part and is why gate 2 in [validation.md](validation.md) starts at reset and stops after the boot burst: up to the first interrupt, the MCU is a closed system and the host does not matter at all. 3. Emit the same six fields **before** executing each instruction — MAME's `debugger_instruction_hook` fires at the fetch, so the flags on a line are the flags *going into* that instruction, and `totalcycles` is the count *before* it runs. 4. Compare with `cmp`. Stop at the first difference and decide it from the datasheet. Keep traces out of the repository. A 2,500-frame run is 116-137 MB of `error.log`; regenerating it takes 40 seconds.