# MAME as an emulator-derived oracle: tracing Donkey Kong's 8035 MAME 0.285's `mcs48` device, running inside the Donkey Kong driver, produces a per-instruction register trace of the sound MCU with the real board around it: the Z80 writing the tune latch, the 6H latch driving T0 and T1, the DAC on P1, the banked sample ROM on `MOVX`. That trace is an **emulator-derived** oracle, tier 3: it detects divergences, and a divergence is decided by the manual -- never by MAME. [undocumented-behavior.md](undocumented-behavior.md) §6 records two places where MAME is known to be wrong and agreement would be the bug. Everything below was run on 2026-09-12 and every output quoted is from those runs. ## Environment | | | | --- | --- | | MAME | `/usr/games/mame`, `-version` prints `0.285 (unknown)` | | ROM sets | `$MAME_ROMPATH/`, used in place with `-rompath`. Nothing is copied; ROMs and traces are gitignored | | Machine | `dkong` (Donkey Kong (US set 1), Nintendo 1981, `nintendo/dkong.cpp`) | | Devices | `mame -listdevices dkong`: `maincpu Zilog Z80 @ 3.07 MHz`, `dma8257 Intel 8257 DMA Controller @ 3.07 MHz`, **`soundcpu Fujitsu MB8884 @ 6.00 MHz`**, `discrete`, three `latch8`s, `watchdog` | | Licence of the code cited | BSD-3-Clause (`mcs48.cpp`: Dan Boris, Mirko Buffoni, Aaron Giles, Couriersud; `dkong.cpp`/`dkong_a.cpp`: Nicola Salmoria and others) | ## Finding the tag, and confirming the part The brief that commissioned this repository guessed the tag was "probably `:soundcpu` or similar". It is `:soundcpu`, and the way to find that out without guessing is one Lua loop: ```lua for tag, device in pairs(manager.machine.devices) do print(tag, device.shortname) end ``` which prints, for `dkong`: ```text : dkong :maincpu z80 :dma8257 i8257 :screen screen :gfxdecode gfxdecode :palette palette :ls175.3d latch8 :ls259.6h latch8 :virtual_p2 latch8 :soundcpu mb8884 :mono speaker :discrete discrete :watchdog watchdog ``` `mb8884` is the short name of MAME's `MB8884` device, which `mcs48.cpp` line 292 constructs as `mcs48_cpu_device(..., 0 /* rom_size */, 64 /* ram_size */, I8048_FEATURE, s_mcs48_opcodes)` -- **a romless, 64-byte-RAM MCS-48, i.e. an 8035**, second-sourced by Fujitsu. The clock is `#define I8035_CLOCK (XTAL(6'000'000))` (`nintendo/dkong.h` line 68), applied at `nintendo/dkong_a.cpp` line 1341. So the canonical claim -- "Donkey Kong's sound CPU is an 8035 at 6 MHz" -- is confirmed from the source, not from memory. The same loop is how to find any other target. A sweep of `mame -listxml` (314 MB, dumped once) for every `` with an MCS-48 family name produced the board table in the [README](../README.md); it is also how the briefing's claim about Popeye was falsified (`popeye` has a Z80, an AY-3-8910A and a netlist -- no MCS-48). ## The registers MAME exposes for this core `mcs48.cpp` `device_start` (lines 1177-1202) registers these, and nothing else. The list was also read back at runtime by iterating `device.state`: | Symbol | Meaning | | --- | --- | | `pc` | 12-bit program counter, masked `0xfff` | | `curpc` | `STATE_GENPCBASE` -- the address of the instruction about to execute | | `genpc` | the same as `pc`, hidden | | `a` | accumulator | | `sp` | **PSW bits 2-0**, the stack pointer (registered `.noshow()` but usable in expressions) | | `curflags` | `STATE_GENFLAGS`, bound to `m_psw` -- **the whole PSW byte** | | `tc` | the timer/counter register | | `tpre` | the 5-bit prescaler | | `p1`, `p2` | the port latches | | `r0` … `r7` | the current bank's working registers | | `ea` | the EA pin | | `sts`, `dbbi`, `dbbo` | UPI-41/42 parts only | | `p0` | 8021/8022 only | **There is no `psw` symbol.** The trace action suggested in the brief, `{logerror "%X %X %X\n",curpc,a,psw}`, was tried here and produced **nothing**: 52,289 instructions were traced into the disassembly file and `error.log` held zero register lines, with no error message anywhere. An unknown symbol silently kills the whole action. Use `curflags` for the PSW byte -- it is `m_psw` itself, so it carries CY, AC, F0, BS and the stack pointer, with bit 3 always clear (see [undocumented-behavior.md](undocumented-behavior.md) §2). MAME's disassembler uses Intel mnemonics in lower case with `$` for hex: `strt t`, `orl p2,#$80`, `jtf $6A0`, `movp a,@a`, `sel rb1`, `retr`. The page-relative branch targets are printed as absolute addresses. ## The command line ```text mame dkong -rompath "" \ -homepath . -cfg_directory ./cfg -nvram_directory ./nvram \ -video none -sound none -nothrottle -noreadconfig -skip_gameinfo \ -debug -debugger none -log -autoboot_script trace_dkong.lua ``` The shipped, runnable form is [`scripts/mame/trace_dkong.sh`](../scripts/mame/trace_dkong.sh) wrapping [`trace_dkong.lua`](../scripts/mame/trace_dkong.lua); it takes `TRACE_CPU`, `TRACE_FILE`, `TRACE_START_FRAME`, `TRACE_STOP_FRAME`, `TRACE_IDLE`, `MACHINE`, `MAME` and `ROMPATH` from the environment and writes into a gitignored `traces/` directory. Inside the Lua script, the part that matters: ```lua local debugger = manager.machine.debugger debugger.visible_cpu = manager.machine.devices[":soundcpu"] debugger:command('trace dkong_soundcpu.txt,,noloop,' .. '{logerror "%03X A=%02X PSW=%02X SP=%X P1=%02X P2=%02X TC=%02X TPRE=%02X EA=%X ' .. 'R0=%02X R1=%02X R2=%02X R3=%02X R4=%02X R5=%02X R6=%02X R7=%02X\n",' .. 'curpc,a,curflags,sp,p1,p2,tc,tpre,ea,r0,r1,r2,r3,r4,r5,r6,r7}') debugger:command("go") ``` What each piece does, as verified here: - `-debug -debugger none` turns on the debugger engine with no UI, which is what makes `manager.machine.debugger` reachable from Lua. `-video none -sound none -nothrottle` run headless at full speed; MAME warns that `-video none` "doesn't make much sense without -seconds_to_run", which the Lua frame counter makes moot. `-noreadconfig` keeps the user's `mame.ini` out of it. `-log` enables `error.log`, where `logerror` output lands. - `trace FILE,,noloop,{action}` writes one disassembled line per instruction to `FILE` and evaluates `{action}` **before** each instruction. So `error.log` line *n* is the state before the instruction on `FILE` line *n*. `noloop` suppresses MAME's "(loops for N instructions)" compression, which matters enormously here: the DK sound loop would otherwise collapse to nothing. - `emu.register_frame_done` counts frames; issue `trace` at a later frame to capture a window in steady state, and `trace off` before exiting. ### Gotchas, confirmed and corrected Four were handed down from earlier sessions in this family of repositories. All four were re-tested here against MAME 0.285. | Claim | Verdict | | --- | --- | | `error.log` lands in the **current working directory**, not beside `-homepath` | **Confirmed.** A run with `-homepath ./home` put `cfg/` under `./home` and `error.log` in the cwd. Both the trace file and `error.log` are cwd-relative; `trace_dkong.sh` therefore `cd`s into the output directory first. | | `tracelog` is silent | **Corrected.** `tracelog` is not silent -- it writes into the **trace file**, interleaved line-by-line with the disassembly, and nothing into `error.log`. A run with `{tracelog "%03X %02X\n",curpc,a}` produced a 31,376-line trace file alternating `000 00` / `0:000: strt t`, and an `error.log` containing only MAME's own messages. Interleaving is sometimes what you want (the two halves can never drift apart); `logerror` into a separate file is used here because it keeps the register stream machine-readable. | | the Lua `device.debug:bpset` API segfaults | **Confirmed.** `manager.machine.devices[":soundcpu"].debug:bpset(0x003)` killed MAME 0.285 with SIGSEGV (exit 139, core dumped) before it printed anything. The `bpset` **debugger command** -- `debugger:command("bpset 3")` -- runs fine. Use commands, not the `device.debug` object. | | `focus` breaks the trace action | **Not re-verified here.** Not needed: `debugger.visible_cpu = ` selects the traced CPU, and `trace` takes the action. Left as received. | One more, new: **naming a register that does not exist silently disables the entire action** (see `psw` above). If `error.log` has no register lines, suspect a symbol before suspecting the flags. ## Provoking the MCU A sound MCU idles until the main CPU commands it, and this one is no exception. Traced from reset with no input, Donkey Kong's 8035 visits **76 distinct program addresses** in 60 frames -- the attract-mode tone loop. The fix is to play the game from Lua. `trace_dkong.lua` inserts a coin at frame 240, presses start at frame 300, and from frame 380 holds right and taps jump on a cycle, using the field names MAME reports for this machine (`:IN2`/`Coin 1`, `:IN2`/`1 Player Start`, `:IN0`/`P1 Right`, `:IN0`/`P1 Button 1`, enumerable with `for tag, port in pairs(manager.machine.ioport.ports) do for name in pairs(port.fields) do ... end end`). With that, a boot-to-gameplay run of 460 frames covers **196 distinct addresses** and 2,406,109 instructions, and reaches the sample-playback code: | Mnemonic | Count in the 460-frame run | | --- | --- | | `mov` | 799,432 | | `rr` | 353,100 | | `add` / `addc` | 266,178 / 176,530 | | `sel` | 178,022 | | `movp` | 177,141 | | `orl` | 176,531 | | `jmp` | 88,723 | | `outl` (the DAC) | 88,265 | | `jtf` | 88,265 | | `call` / `ret` / `retr` | 1,416 / 835 / 578 | | `movx` (the banked sample ROM) | 598 | | `movp3` | 81 | | `jb5`, `jb6`, `jb7`, `jnc`, `jz`, `jnz`, `jf0`, `jf1`, `jnt0`, `jnt1`, `djnz`, `swap`, `rl`, `rrc`, `xch`, `in`, `anl`, `clr`, `cpl`, `inc`, `strt`, `en` | 1 to 855 each | **What it never reaches: an interrupt.** Zero entries to location 3 and zero to location 7 in 2.4 million instructions. The reason is in the driver: the sound MCU's `INT` is driven from the Z80 write at `0x7D80` (`dkong.cpp` line 806, `dkong_audio_irq_w`), and `dkong.cpp`'s own memory-map comment (line 207) labels `0x7d80` "digital sound trigger - **dead**". The program enables interrupts once at boot (`0:052: en i`) and then polls `JTF` forever. `RETR` appears 578 times, used as an ordinary return for its PSW restore, not as an interrupt exit. A second attempt on Konami's Gyruss (`TRACE_CPU=:audio2 MACHINE=gyruss`, an 8039 at 8 MHz) traced 2,346,665 instructions of which 1,173,327 were `jmp` and 1,173,323 were `nop`: its 8039 idles in a two-instruction loop waiting for the Z80, and the coin/start injection above does not apply to that machine's port layout. **Conclusion for the validation plan: no MCS-48 interrupt sequence was observed on real arcade software in any run made here.** The interrupt model therefore rests on the manual and on targeted tests, not on a trace. That is recorded in [validation.md](validation.md) as a gap, not papered over. ## Sample output Reset and the first instructions (`TRACE_START_FRAME=0 TRACE_STOP_FRAME=4 TRACE_IDLE=1`). Disassembly on the left, the register line that precedes it on the right, abridged to the fields that move: ```text 0:000: strt t 000 A=00 PSW=00 SP=0 P1=FF P2=FF TC=00 TPRE=00 EA=1 R0=00 ... 0:001: jmp $052 001 A=00 PSW=00 SP=0 P1=FF P2=FF TC=00 TPRE=00 EA=1 R0=00 ... 0:052: en i 052 A=00 PSW=00 SP=0 P1=FF P2=FF TC=00 TPRE=02 EA=1 R0=00 ... 0:053: clr f0 053 ... TPRE=03 0:054: clr f1 054 ... TPRE=04 0:055: mov r0,#$20 055 ... TPRE=05 R0=00 0:057: mov @r0,#$00 057 ... TPRE=07 R0=20 0:059: sel rb1 059 A=00 PSW=00 ... TPRE=09 R0=20 0:05A: clr a 05A A=00 PSW=10 ... TPRE=0A R0=00 0:05B: mov r6,a 05B A=00 PSW=10 ... TPRE=0B 0:05C: mov r7,a 05C ... 0:05D: sel rb0 05D A=00 PSW=10 ... TPRE=0D R0=00 0:05E: orl p2,#$80 05E A=00 PSW=00 ... TPRE=0E R0=20 0:060: call $073 060 ... TPRE=10 0:073: retr ``` Four things are visible in those fifteen lines and each is a unit test waiting to be written: - **Reset state**: PC = 0, A = 0, PSW = 0 (so BS = 0 and SP = 0), P1 = P2 = 0xFF (input mode), TC = 0, EA = 1 (romless part, external ROM). - **`STRT T` resets the prescaler** and TPRE then advances by exactly the cycle count of each instruction: `jmp` (2 cycles) takes it 00 → 02, the one-cycle `clr f0` 02 → 03, the two-cycle `mov r0,#$20` 05 → 07. - **`SEL RB1` sets PSW bit 4** (`PSW=00` → `PSW=10`) and the `R0` column changes from 0x20 to 0x00 in the same step -- the same eight symbols now name RAM 24-31. `SEL RB0` puts it back. - **`CALL $073` where 073 is `RETR`** is the program clearing the interrupt-in-progress flip-flop at boot, a well-known MCS-48 idiom: push, pop, and come back with the interrupt logic armed. Gameplay, in the DAC output loop (frames 400-460): ```text 0:699: sel rb1 699 A=54 PSW=43 SP=3 P1=99 TC=98 TPRE=1E R0=CA R1=31 ... R7=04 0:69A: add a,r0 69A A=54 PSW=53 SP=3 P1=99 TC=98 TPRE=1F R0=48 R1=00 ... R7=02 0:69B: outl p1,a 69B A=9C PSW=13 SP=3 P1=99 TC=99 TPRE=00 R0=48 ... 0:69C: jtf $6A0 69C A=9C PSW=13 SP=3 P1=9C TC=99 TPRE=02 R0=48 ... 0:69E: jmp $681 69E A=9C PSW=13 SP=3 P1=9C TC=99 TPRE=04 R0=48 ... 0:681: mov a,r4 681 A=9C PSW=13 SP=3 P1=9C TC=99 TPRE=06 R4=30 R6=CE 0:682: add a,r6 682 A=30 PSW=13 SP=3 ... 0:683: mov r4,a 683 A=FE PSW=13 SP=3 ... ``` Read it as an arithmetic check: before `add a,r0` the state is A = 0x54, R0 (bank 1) = 0x48; 0x54 + 0x48 = 0x9C with no carry out of bit 7 and no carry out of bit 3, and PSW goes 0x53 → 0x13 -- AC cleared, CY still clear, BS still set, SP still 3. Then `outl p1,a` writes 0x9C and the next line's `P1` is 0x9C: that is the DAC sample leaving the chip, and it is exactly the observable a core must reproduce. And TPRE 1F → 00 with TC 98 → 99 is the prescaler rolling over and ticking the timer, whose overflow the `jtf` two instructions later is waiting for. ## Cost | Run | Frames | Instructions | Trace file | `error.log` | Wall time | | --- | --- | --- | --- | --- | --- | | gameplay window | 400-460 | 314,303 | 336 KB | 7.6 MB | 3.8 s | | boot to gameplay | 0-460 | 2,406,109 | 2.6 MB | 49 MB total | ~25 s | About 5,238 instructions per frame in gameplay, which at 6,667 machine cycles per 60 Hz frame means an average of 1.27 machine cycles per instruction -- consistent with a loop dominated by one-cycle register arithmetic. Trace windows, not whole sessions. `traces/` is gitignored. ## Using the trace in lockstep The core-side host needs Donkey Kong's sound board, which is small enough to state completely (`dkong_a.cpp` lines 1341-1350, `dkong.cpp` line 210): | | | | --- | --- | | program memory | the `soundcpu` ROM region, 0x000-0xFFF, read-only; `EA` high | | `MOVX` space | the same region's 0x800-0xFFF half, selected by P2.6, banked 256 bytes at a time by P2.2-P2.0 | | P1 out | the DAC | | P2 out | bit 7 external decay, bit 6 ROM select, bits 2-0 bank | | P2 in | bit 5 = "Mario is jumping", active low | | BUS in | the tune number the Z80 last wrote | | T0 in | which jump sound | | T1 in | "the gorilla is falling", active low | | INT | the Z80's write to 0x7D80, held as a level (unused by this program) | Then step the core once per `error.log` line and compare the seventeen fields. The first line that differs is the divergence and the instruction on the same line of the trace file names the suspect. Rules for reading a divergence: - A difference in **TC or TPRE around `STRT T` or `MOV A,T`** is expected and is probably MAME's: see [undocumented-behavior.md](undocumented-behavior.md) §6, where two measurements on a real 8048 say MAME is a cycle out. - A difference in **PSW bit 3** cannot happen: `curflags` is `m_psw`, in which bit 3 is always 0. - A difference in **R0-R7 immediately after `SEL RB0`/`SEL RB1`/`MOV PSW,A`/ `RETR`** means the core copied registers instead of rebasing a pointer. - A difference in **PC after a conditional jump near a page boundary** means the core masked the PC before fetching the operand instead of after. - Everything else is decided by the manual, by a unit test written from it, or by the T48 suite ([validation.md](validation.md)) -- not by MAME.