# MAME 0.285 as an emulator-derived detector for the uPD7725/uPD96050 MAME 0.285 (`mame` on the PATH; source tag `mame0285`, `f379cd06`, sparse in `reference/src/mame` or snes-python's tree) runs the SNES DSPs with `necdsp_device` (`src/devices/cpu/upd7725/upd7725.cpp`, byuu's core converted by R. Belmont) and the real programs. It is a **detector**: one lineage with ares (and bsnes), with its own differences (validation.md). Everything here was run on 2026-11-05; `scripts/mame_dsp_trace.py` encodes it. ## The programs: MAME's device sets, read in place The `snes` driver's cartridge slot loads a DSP cartridge into a "legacy" slot device that takes its program from a device ROM set: `sns_dsp1leg` (`dsp1.bin`), `sns_dsp1bleg` (`dsp1b.bin`), `sns_dsp1leg_hi` (the same `dsp1.bin`), `sns_dsp2leg`, `sns_dsp3leg`, `sns_dsp4leg` (10,240 bytes each), `sns_seta10leg`, `sns_seta11leg` (69,632 bytes). They are found on `MAME_ROMPATH` (one or more folders separated by `;`, as MAME's `-rompath`); the driver also needs `snes.zip` (the S-SMP IPL) there. The folders go on `-rompath`, read in place. The images are the PROM's byte layout (start-here.md "Program and data ROM images"). Measured slot names (a loose `.sfc` from No-Intro or the play set): `lorom_dsp1leg` (Pilotwings), `hirom_dsp1leg` (Super Mario Kart), `lorom_dsp2leg` (Dungeon Master), `lorom_dsp3leg` (SD Gundam GX), `lorom_dsp4leg` (Top Gear 3000), `lorom_st10leg` (Exhaust Heat II), `lorom_st11leg` (Hayazashi Nidan Morita Shougi). The DSP device is `:snsslot::dsp`, short name `upd7725` or `upd96050`. ## The recipe ```sh python -I scripts/mame_dsp_trace.py "path/to/Pilotwings (USA).7z" \ --start-frame 1800 --frames 30 # trace: ~2 min wall python -I scripts/mame_dsp_trace.py CART --host-only --frames 3600 # find the busy frames python -I scripts/mame_dsp_trace.py CART --dasm # program.dasm ``` which runs, in `tests/mame_traces//` (gitignored; the cartridge is extracted there): ```sh nice -n 19 mame snes -cart CART.sfc -rompath "$MAME_ROMPATH" -homepath RUN \ -video none -sound none -nothrottle -noreadconfig -skip_gameinfo \ -debug -debugger none -oslog -seconds_to_run N -autoboot_script RUN/trace.lua ``` with a Lua script that finds the DSP device, makes it the visible CPU, sets read and write watchpoints on the host's DSP window **from power-on**, and at the start frame issues ``` trace /dev/null,:snsslot:lorom_dsp1leg:dsp,noloop,{logerror "D %X %X %X %X %X %X %X %X %X %X %X %d\n",pc,a,b,k,l,tr,trb,dp,rp,sp,dr,totalcycles} wpset 208000:maincpu,200000,r,(wpaddr & 8000) != 0,{logerror "R %X %X %d\n",wpaddr,wpdata,totalcycles; g} ``` (and the `w` twin, and the other windows by slot), then exits at the end frame. MAME's log goes to stderr (`-oslog`); the script reads it as a stream, collapses each run of `D` lines identical but for the cycle into `L n`, and writes `dsp.log.gz` and `summary.json`. ## The output ``` D 4 0 C0 0 0 0 0 0 0 0 80 4 PC A B K L TR TRB DP RP SP DR cycle L 160 the line above, 160 more times W 3F8000 80 164 host write of $80 to $3F:8000 at DSP cycle 164 D 4 0 C0 0 0 0 0 0 0 0 80 165 D 5 0 C0 0 0 0 0 0 0 0 80 166 JRQM fell through: the write was seen at 165 ``` - **One `D` line per instruction, before it executes**; MAME bills one cycle per instruction, so the cycle is the instruction count since power-on. - **The host stamp is the DSP's cycle**, because a watchpoint action is evaluated with the visible CPU's symbols (snes-python mame-oracle.md trap 1, used on purpose here). **An access stamped `N` lands after instruction `N` and is first seen by instruction `N + 1`**: measured on all 49,315 accesses in 30 frames of Pilotwings (every stamp equals the preceding `D` line's cycle, and every following `D` line is `N + 1`). - **No SR, no flags**: MAME's upd7725 registers PC RP DP SP K L M N A B TR TRB DR SI SO IDB for the debugger, not SR or FLAGA/FLAGB (`device_start`; the `UPD7725_FLAGA` enum is only used by `state_string_export`). M and N follow from K and L. Flag errors show as branch or result divergences. - A host read's value is the byte the DSP gave the S-CPU: the replay checks it against the core's `read_dr`/`read_sr`. ## Traps found 1. **`-seconds_to_run` is an integer.** `0.998` ran without end (killed after 10 minutes); the script exits from a frame notifier and passes an integer backstop. 2. **A FIFO in place of `error.log` receives nothing**: MAME ran 10 frames with a reader waiting on it and the reader saw end-of-file with no line; `-oslog` to a pipe works. 3. **An unknown symbol in an action silently kills the action** (snes-python trap 2 again): `wptype` is not a symbol, and watchpoints with it were listed by `wplist` but never logged. Use separate `r` and `w` watchpoints. 4. **`noloop` does not collapse a one-instruction loop** (`JRQM $`): MAME wrote 8.12 M lines (283 MB) for one emulated second, 99.995 % of them PC 4. The script collapses them (1.46 M instructions in 10 frames became a few hundred lines; 30 frames of the busy demo, 6.6 MB gzipped). 5. **`addr:tag` space syntax** works for watchpoints on another CPU while the DSP is visible: `wpset 208000:maincpu,...`. 6. **SIGTERM does not stop a `-debug` MAME**; SIGKILL does. 7. **F1 ROC (USA) has no DSP** (chipset byte `$02`): the ST010 game is *Exhaust Heat II* (Japan) / *F1 ROC II*. Its accesses are almost all to the data RAM in bank `$68`, not to DR/SR. 8. The trace is deterministic: the same window traced with watchpoints from power-on or from the start frame executed the same 3,993,445 instructions. 9. Cost: about 150,000 traced instructions per wall second (a traced emulated second is ~53 s of one core at MAME's 8 MHz); `--host-only` runs at about full speed (60 s of Pilotwings in 49 s). 10. **RAM accesses of the ST010/ST011 are logged twice**: the DR/SR watch range (banks 60-6F, offsets below 4000h) overlaps the RAM range (68-6F). Harmless for the replay (a repeated read, the same byte written twice). 11. **MAME 0.285 does not connect the ST011's DR/SR reads in banks 60-6F**: `snes20_lo_r` has `SNES_ST010 /*|| SNES_ST011*/ // why does this break moritash?`, so Morita Shougi's `LDA $600001` reads open bus (60h, the bank byte) and the DSP never sees the read; writes and the banks 80+ mirror are connected. The replay skips those reads (`mame_unmapped_reads`) instead of applying them. With C4 (MAME reverses SO, so the command dispatch's JMPSO goes to 3800h) the game cannot work in MAME; the trace is still an instruction-level detector of the path MAME takes. 12. **MAME never initialises `regs.stack[]`** (`device_start` zeroes SP, not the array): the ST011 run's RT on an empty stack popped 3Eh from it. The replay adopts MAME's value at that D line and records it (`mame_power_on_stack`). 13. **Buttons**: `--press FRAME:"P1 Start"[:HOLD]` sets an input field from the frame notifier (`field:set_value(1)`, `clear_value()` HOLD frames later); `--list-inputs` prints the field names (`:ctrl1:joypad:JOYPAD`, "P1 Start", "P1 A", ...); `--snapshots N` saves the screen every N frames (works with `-video none`) to see where a game is. Top Gear 3000 needs **no** presses: its attract demo drives the DSP-4 from about frame 5,800 (98 s), and pressing Start in the intro leads into the menus instead. Dungeon Master (DSP-2, from frame 1,250) and Morita Shougi (ST011, frame 647) need Start at frames 300, 420, ... 3000 and A 60 frames after each. 14. MAME's PC is 16 bits wide like DP and RP (C9): a JMPSO to an SO above 3FFFh shows as such in the trace; the replay compares within the chip's width. ## What MAME does (read from `upd7725.cpp` and `bus/snes/upd.cpp`; emulator-derived) - One instruction per `execute_run` iteration, `m_icount--`, then `M = (K * L) >> 15`, `N = (K * L) << 1` (l.341-345). - Carry by comparison (`r < q`, `r > q`, l.433/437): C1. - `SIL` and `@SOL` bit-reversed with `bitswap<16>` (l.373, l.561): C4. - `stack[16]`, `sp &= 0xf` (l.478, l.538-539): C5. - `device_start` zeroes the registers; `device_reset` sets PC, SR, flags, SI/SO ACK only (l.142-181): C6. - INT: rising edge with EI = 1 sets EI = 0, then executes `000000h` (NOP) and `A80400h` (`LCALL 100h`) as two instruction cycles (l.275-331): C7. The SNES glue never drives the line. - `DP` and `RP` are `uint16_t`, masked only on access (`dp & 0x07ff` for RAM on both chips; the data ROM through the address map): C9. - Clocks 8 MHz (DSP-n), 10 MHz (ST010), 15 MHz (ST011) (`upd.cpp`): C10. - The internal data bus is a register (`regs.idb`) that `exec_ld` also sets; it is visible to the debugger only. - Host side: `chip_read`/`chip_write` select SR by address bit 14 (LoROM), 12 (HiROM) or 0 (Seta); a host write to SR is logged and ignored; the Seta RAM is `dataRAM[(offset & 0xffff) >> 1 & 0x7ff]` with byte lanes. ## The replay (rung 4), as built `src/upd7725_python/replay.py` (`python -m upd7725_python.replay RUN_DIR`) follows the design below with two cores in lockstep: a **MAME-switched** core (C1 comparison carry, C4 reversed SO/SI, C5 16-deep stack, C11 stored DRS, C16 emulators' DPL, power-on zeros without reset: C6) that must equal every `D` line (C9 width-only differences counted) and every host read, and the **default** core, compared with it after every instruction; a difference is named by re-running that instruction from the saved state with one switch flipped (C1, C4, C16), or as C5/U10 for the stack, then the default core is resynchronised. Results: validation.md "Rung 4 results". ## The replay (rung 4), as designed 1. Construct the core with MAME's power-on state (all registers 0, RP = 0, flags 0, RAM 0) and the program image MAME used. 2. Read `dsp.log.gz` in order. Before the first `D` line, run the core one instruction at a time, applying each `R`/`W` line after the instruction whose index equals its stamp (`read_dr`/`read_sr` for `R`, comparing the returned byte; `write_dr` or a RAM byte write for `W`). 3. From the first `D` line on, compare PC A B K L TR TRB DP RP SP DR before every instruction (`L n` repeats the previous line `n` times at consecutive cycles). 4. A divergence stops the run and is classified by C-entry (C1 at an ADC/SBB with P + C = `10000h`; C4 at an `@SOL`; C5 at a deep CALL; C6 at RP; C9 at DP/RP widths); anything else is a finding against the core or a new C-entry. 5. Reaching frame 1,800 replays ~240 M instructions (MAME's 8 MHz x 30 s), minutes on PyPy for a pure-Python core. Pick windows early in a game where possible.