# MAME as an emulator-derived oracle: V30 traces from R-Type MAME 0.285's `nec` core, running inside the Irem M72 driver, produces a per-instruction register trace of R-Type's V30 with the real board around it: the µPD71059C interrupt controller, the scanline timer, the sound CPU. That trace is an **emulator-derived** oracle, tier 3: it detects divergences, and a divergence is decided by the corpora or the manuals, never by MAME. What follows was run on 2026-09-12 and the outputs quoted are from that run. ## Environment | | | | --- | --- | | MAME | `/usr/games/mame`, `-version` prints `0.285 (unknown)` (Debian package) | | ROM sets | `$MAME_ROMPATH/`, used in place with `-rompath`; nothing is copied, and ROMs and traces are gitignored | | Machine | `rtype` (R-Type (World), Irem 1987, `irem/m72.cpp`). `mame -listdevices rtype`: `maincpu NEC V30 @ 8.00 MHz`, `soundcpu Zilog Z80 @ 3.57 MHz`, `upd71059c Intel 8259 PIC`, `ymsnd YM2151 OPM @ 3.57 MHz`, screen at 8.00 MHz | | License of the code cited | BSD-3-Clause (`nec.cpp` copyright Bryan McPhail; `m72.cpp` copyright Nicola Salmoria and others) | ## Register names in MAME's V30 debugger From `nec.cpp` `device_state_interface` registrations (`state_add`, lines 527-556 at tag `mame0285`), usable in debugger expressions: | MAME name | Intel name | Notes | | --- | --- | --- | | `PC` | IP | the offset only | | `PS` `SS` `DS0` `DS1` | CS SS DS ES | | | `AW` `BW` `CW` `DW` | AX BX CX DX | byte halves `AL AH BL BH CL CH DL DH` also registered | | `SP` `BP` `IX` `IY` | SP BP SI DI | | | `PSW` | FLAGS | 16-bit; the `GENFLAGS` string shows `N` for MD (bit 15), then `O D I T S Z A P C` | | `CURPC` (`curpc` in expressions) | linear address of the instruction | `(PS << 4) + PC`, 20-bit; `GENPC` is the same for the *next* instruction | Verified: an expression using `pc` printed `0` for the reset instruction at FFFF:0000 while the disassembly line said `FFFF0`; `curpc` prints `FFFF0`. Use `curpc` for the linear address. MAME's disassembler uses NEC mnemonics. The ones that appear in the first lines of an R-Type trace: `br` (JMP), `dbnz` (LOOP), `bne` (JNZ), `di`/`ei` (CLI/STI), `reti` (IRET), `mov cw,...`, `out 40h,aw`, `in aw,4h`, `pop r` (POPA), `pop ds1` (POP ES). The full NEC-to-Intel table is appendix A of the NEC manual; [start-here.md](start-here.md) has the register mapping. ## The command line ```text mame rtype -rompath "" -homepath \ -cfg_directory /cfg -nvram_directory /nvram \ -video none -sound none -nothrottle -noreadconfig -skip_gameinfo \ -debug -debugger none -log -autoboot_script trace_rtype.lua ``` with, in `trace_rtype.lua` (the shipped copy is [`scripts/mame/trace_rtype.lua`](../scripts/mame/trace_rtype.lua) and [`scripts/mame/trace_rtype.sh`](../scripts/mame/trace_rtype.sh) wraps the whole thing): ```lua local dbg = manager.machine.debugger dbg.visible_cpu = manager.machine.devices[":maincpu"] dbg:command('trace trace_disasm.txt,,noloop,{logerror "%05X %04X:%04X AW=%04X BW=%04X CW=%04X DW=%04X SP=%04X BP=%04X IX=%04X IY=%04X DS0=%04X DS1=%04X SS=%04X PSW=%04X\n",curpc,ps,pc,aw,bw,cw,dw,sp,bp,ix,iy,ds0,ds1,ss,psw}') dbg:command("go") local frames = 0 emu.register_frame_done(function() frames = frames + 1 if frames >= 60 then manager.machine:exit() end end) ``` What each piece does, as verified: - `-debug -debugger none` enables the debugger engine with no UI, which is what makes `manager.machine.debugger` available to Lua; `-video none -sound none -nothrottle` run headless at full speed (MAME prints a warning that `-video none` "doesn't make much sense without -seconds_to_run"; the Lua exit makes it moot). `-noreadconfig` keeps the user's `mame.ini` out of it; `-log` turns on `error.log` under `-homepath`, which is where `logerror` output lands. - The `trace FILE,,noloop,{action}` debugger command writes one disassembled line per instruction to `FILE` (relative to the working directory) and evaluates the action **before** each instruction; the `logerror` action's text goes to `error.log`. So `error.log` line *n* is the register state *before* the instruction on `trace_disasm.txt` line *n*. `noloop` suppresses the "(loops for N instructions)" compression, so every iteration of a delay loop is one line. - `tracelog` (the debugger's own formatted-print command) produces **nothing** in this configuration: a run with `dbg:command('tracelog "%X %X\n",curpc,aw')` left `error.log` holding only its `Soft reset` line. Reported by an earlier session and confirmed here. - Also reported by an earlier session, **not re-verified here**: the `focus` debugger command breaks the trace action, and calling `device.debug:bpset` from Lua segfaults MAME 0.285. Use `trace` with an action, as above. - `emu.register_frame_done` counts frames; to trace a window in steady state issue the `trace` command at a later frame and `trace off` before exiting (the shipped script takes `TRACE_START_FRAME` and `TRACE_STOP_FRAME`). ## Sample output Boot, first frame (`error.log`, first lines; the `Soft reset` line is MAME's): ```text Soft reset FFFF0 FFFF:0000 AW=0000 BW=0000 CW=0000 DW=0000 SP=0000 BP=0000 IX=0000 IY=0000 DS0=0000 DS1=0000 SS=0000 PSW=F002 3F800 3F00:0800 AW=0000 BW=0000 CW=0000 DW=0000 SP=0000 BP=0000 IX=0000 IY=0000 DS0=0000 DS1=0000 SS=0000 PSW=F002 3F801 3F00:0801 AW=0000 BW=0000 CW=0000 DW=0000 SP=0000 BP=0000 IX=0000 IY=0000 DS0=0000 DS1=0000 SS=0000 PSW=F002 3F804 3F00:0804 AW=0000 BW=0000 CW=FFFF DW=0000 SP=0000 BP=0000 IX=0000 IY=0000 DS0=0000 DS1=0000 SS=0000 PSW=F002 3F804 3F00:0804 AW=0000 BW=0000 CW=FFFE DW=0000 SP=0000 BP=0000 IX=0000 IY=0000 DS0=0000 DS1=0000 SS=0000 PSW=F002 ``` and the matching `trace_disasm.txt`: ```text FFFF0: br 3F00h:800h 3F800: di 3F801: mov cw,0FFFFh 3F804: dbnz 3F804h 3F804: dbnz 3F804h ``` Reset state as MAME models it: PS = FFFF, PC = 0000, PSW = F002 (bits 12-15 and bit 1 set, MD = 1, everything else clear), all other registers 0. Later in the same frame the game programs the interrupt controller, and the values match the table in `m72.cpp` ("rtype irqbase 0x20: port 0x40 ← 17, port 0x42 ← 20 0F"): ```text 3F806: mov aw,17h 3F809: out 40h,aw 3F80B: mov aw,20h 3F80E: out 42h,aw 3F810: mov aw,0Fh 3F813: out 42h,aw ``` Steady state, frames 300-302 (`TRACE_START_FRAME=300 TRACE_STOP_FRAME=302`): 28,752 instructions in two frames, about 14,400 per 55 Hz frame at 8 MHz. The vertical-blank handler (vector 0x20) ends at `0040:0298`; its tail and the return, with the register line for each: ```text 0068F: mov byte ptr [2F18h],0h 00695 0040:0295 ... SP=3FF2 ... DS1=1000 SS=4000 PSW=F202 00694: pop r 00696 0040:0296 ... SP=3FF4 ... DS1=D000 ... PSW=F202 00695: pop ds1 00697 0040:0297 ... SP=3FF6 ... 00696: pop ds0 00698 0040:0298 ... SP=3FF6 ... 00697: ei 00698: reti 0EC95 0040:E895 ... SP=3FFC ... PSW=F216 ``` PSW = F202 inside the handler is IF set and every status flag clear; F216 after `reti` is the interrupted code's flags restored (PF, AF set). The register line printed *after* `reti` belongs to the next instruction at 0040:E895, which is what "state before each instruction" means. ## Cost With the 15-field format, one emulated second of R-Type is about 760,000 instructions, 88 MB of `error.log` and 16.5 MB of `trace_disasm.txt`; the run itself takes about 5 s wall for one emulated second at boot (the delay loop) and about 1 s wall for two frames in steady state. Trace windows, not whole sessions. Traces live outside the repository (`traces/` is ignored). ## Using the trace in lockstep The core-side host needs the `rtype` memory map and port map from [timing.md](timing.md) (ROM 0x00000-0x3FFFF from the ROM set, read in place; RAM at 0x40000-0x43FFF, 0xC0000-0xDBFFF; anything else returns 0xFF and swallows writes) and the same interrupt timing: IR0 at scanline 256 → vector 0x20 through the 8259 model, IR2 at the raster line the game sets → 0x22. Then step the core once per `error.log` line and compare the fourteen register fields; the first line that differs is the divergence, and the instruction on the same line of `trace_disasm.txt` names the suspect. A difference in an undefined flag after MUL/DIV/AAM is expected until the V30 milestone reproduces NEC's values, and even then MAME's opinion of an undefined flag is not evidence; the corpus is. A difference in the timing of an interrupt entry is expected whenever the core's clock totals and MAME's disagree (MAME's are "99% accurate", by its own header), and is not a core bug until the instruction totals themselves are shown wrong.