# MAME 0.285 as a detector: the verified recipe for `snes` MAME 0.285 (`/usr/games/mame`, source pinned at tag `mame0285`, `f379cd06`, sparse in `reference/src/mame`) is a **detector** for this board, never a judge (validation.md). It is a good one for the **instruction streams** (its 5A22 passes gilyon's 649-test `cputest-full`, its S-SMP the 557-test `spctest`) and for the **order and values of register accesses**; a weak one for anything timed within a line (scanline PPU, no refresh pause, DMA cycles not charged, mul/div instant). Everything here was run on 2026-10-21; `scripts/mame_trace.py` encodes it. ## Drivers and the BIOS `mame -listdevices snes`: `maincpu` = Ricoh 5A22 @ 21.47 MHz, `soundcpu` = S-SMP @ 2.04 MHz (24.576 MHz / 12: MAME counts half SPC700 cycles), `s_dsp` @ 24.57 MHz, `ppu` @ 21.47 MHz, `screen` @ 10.73 MHz (raw at twice the dot clock), `snsslot`, `ctrl1`/`ctrl2` (`joypad`). `snespal` is the PAL machine. `mame -listroms snes`: **`spc700.rom`, 64 bytes, CRC `44bb3a40`, SHA-1 `97e352553e94242ae823547cd853eecda55c20f0`** (the IPL ROM, in device `s_smp`): bring your own, by `--rompath` (default: Aubrey's MAME non-merged directory, `snes.zip`, read in place) or `SNES_MAME_ROMPATH`. A loose `.sfc`/`.smc` loads with `-cart`; MAME logs its header guess (`[:snsslot] HiROM/LoROM id: LoROM (LoROM: 14, HiROM: 0, ExHiROM: 0)`, title, speed, type, size) to `error.log`. Coprocessor programs (`sns_dsp1*`, `sns_rom_sgb*`) are MAME device sets in `ROMs (bios-devices)`. ## The recipe ```sh python scripts/mame_trace.py snes tests/test_roms/gilyon/cputest/cputest-full.sfc \ --no-trace --seconds 30 --text Success --text Failed python scripts/mame_trace.py snes "$NO_INTRO_DIR/Nintendo - Super Nintendo Entertainment System/Super Mario World (USA).zip" \ --seconds 2 --spc ``` which runs, in a fresh `tests/mame_traces/-/` (the `.7z` is extracted there with `7z`): ```sh mame snes -cart .sfc -rompath -homepath -snapshot_directory snap \ -video none -sound none -nothrottle -noreadconfig -skip_gameinfo \ -debug -debugger none -log -seconds_to_run -autoboot_script /trace.lua ``` with: ```lua local dbg = manager.machine.debugger dbg.visible_cpu = manager.machine.devices[":maincpu"] dbg:command('trace cpu.trace,:maincpu,noloop,{logerror "C %X %X %X %X %X %X %X %X %X %d %d %d %d\\n",pc,a,x,y,p,s,d,db,e,totalcycles,beamy,beamx,frame}') dbg:command('trace spc.trace,:soundcpu,noloop') -- --spc only dbg:command('wpset 2100,100,r,1,{logerror "R %X %X %d\\n",wpaddr,wpdata,totalcycles; g}') dbg:command('wpset 2100,100,w,1,{logerror "W %X %X %d\\n",wpaddr,wpdata,totalcycles; g}') dbg:command('wpset 4200,200,r,1,{logerror "R %X %X %d\\n",wpaddr,wpdata,totalcycles; g}') dbg:command('wpset 4200,200,w,1,{logerror "W %X %X %d\\n",wpaddr,wpdata,totalcycles; g}') -- --text WORD: a one-shot read watchpoint on the string's LoROM addresses dbg:command('wpset CCD3,1,r,temp0==0,{logerror "T Success CCD3 %d\\n",totalcycles; temp0=1; g}') dbg:command("go") ``` ## What the output means ``` C 8000 0 0 0 34 1FF 0 0 1 0 225 0 0 SEI (Super Mario World reset) C 8001 0 0 0 34 1FF 0 0 1 14 225 7 0 STZ $4200 C 8004 0 0 0 34 1FF 0 0 1 44 225 22 0 W 4200 0 14 ``` - **`totalcycles` is master clocks**: `SEI` cost 14 (8 SlowROM opcode fetch + 6 internal), `STZ $4200` 30 (three 8-clock ROM fetches + a 6-clock register write). So instruction-level lockstep against the board can compare clocks directly. - **PC is 24-bit** (bank:PC); `E` is the emulation flag; `P` is the real P (in emulation mode bits 4-5 read as set). - **`beamx` is in half-dots** (0-681: the screen runs at twice the dot clock); `beamy` is the scanline (225 at power-on, i.e. MAME starts in VBlank). - R/W lines are stamped with the **instruction's** start, not the access's cycle. DMA's B-bus accesses **are** logged (the watchpoints are on the address space, not the CPU): Super Mario World's 544-byte OAM DMA appears as 544 `W 2104` lines under the `STA $420B` (measured 2026-11-23; this line said the opposite before). - `spc.trace` (with `--spc`) is MAME's plain trace: `PC: disassembly` per S-SMP instruction (`FFC0: MOV X,#$ef` first: the IPL ROM), no registers, no cycles. `cpu.trace` is the same for the 5A22. ## Traps found 1. **A trace action is evaluated with the visible CPU's symbols, whichever CPU is traced.** `trace spc.trace,:soundcpu,noloop,{logerror ... pc,a ...}` logs the **5A22's** PC and registers at each S-SMP instruction, even with `visible_cpu` switched to `:soundcpu` while the command is issued (measured: the "S" lines repeated the C lines' values). Hence the plain trace file for the S-SMP. 2. **An unknown symbol drops the whole trace command silently**: the S-SMP's symbols are `PC S P A X Y` (`spc700.cpp` l.1229-1234), not `psw`/`sp`; with `psw` there was no trace and no error. 3. **The IPL ROM is required** even for a ROM that never talks to the APU: with no `spc700.rom` on the rompath MAME prints `spc700.rom NOT FOUND (tried in s_smp snes)` / `Fatal error: Required files are missing, the machine cannot be run.` and exits (measured). 4. **Snapshots are 512 x 225** (or 512 x 450 interlaced): twice the dot width, and one line taller than the 224 visible lines; the **extra line is at the bottom** (cropping it there made gilyon's and krom's `CPUADC` frames identical; cropping the top left 4,314 pixels different). 5. **Colours**: MAME expands 5-bit 31 at full brightness to 247, not 248; compare at 5 bits (`round(c * 31 / 255)`). 6. The `--text` detector maps a ROM string to LoROM addresses in banks `$00` and `$80`; for HiROM test ROMs it needs the HiROM mapping (not needed yet). 7. `grep` on `error.log` needs `-a`: its first lines carry a UTF-8 BOM and MAME's header dump includes non-ASCII bytes. 8. `logerror` silently drops values past 14 (found on the Z80 boards); the C line uses exactly 14. ## Measured (2026-10-21) | Run | Emulated | Wall | Result | | --- | ---: | ---: | --- | | gilyon `cputest-full` (`--no-trace --text`) | 30 s | 4.1 s | "Success" at master clock 58,252,660 (649 tests) | | gilyon `cputest-basic` | 30 s | 4.2 s | "Success" at 40,384,270 | | gilyon `spctest` | 60 s | 7.3 s | "Success" at 45,030,274 (557 tests) | | Super Mario World (USA), traced, `--spc` | 2 s | 19.6-25.8 s | 1,785,297 5A22 + 526,292 S-SMP instructions; 290,092 R, 131,488 W; mean frame 357,368 clocks | | krom, 137 ROMs with pictures (`scripts/mame_screens.py`) | 5 s each | ~2 min, 12 jobs | 60 identical; CPUTest 27/30 at 60 s | ## What MAME does not model (so lockstep diverges there) - the DRAM refresh pause (40 clocks per line) and the short line (the mean frame is exactly 262 x 1364); - DMA and HDMA cycle costs (`snes_m.cpp` TODO); - mul/div latency and division by zero (remainder `$000C`); - per-dot PPU effects (the PPU draws per scanline); - mid-frame CGRAM writes: a write at line 100 dot 100 lands at CGADD (`$080`), where the documents (and Mesen2) put it at the PPU's current address (`probes/midframe`, 2026-10-22); - OAM read-back: every sprite's Y byte reads back one higher than written (`$F0` in, `$F1` out, the other 416 bytes exact); a scratch diagnostic that fills OAM in forced blank and reads all 544 bytes, 2026-10-22. A mid-frame OAM write changes one byte and leaves no `$A5` tag. It does model the PPU1/PPU2 open-bus split for the 11 cases of `probes/open_bus` (all as fullsnes predicts, 2026-10-22). Lockstep against MAME is therefore by **instruction and register-access order**, with clock differences named per cause, as megadrive-python's milestone 1 did; a clock difference that is not one of the causes above is a finding. ## The lockstep (milestone 1's last gate, 2026-11-23) `scripts/mame_lockstep.py` runs the board on the cartridge `mame_trace.py` traced, records the same fields before each instruction (PC, A, X, Y, P, S, D, DB, E, master clock) and every register access in bank `$00` (MAME's watchpoints cover only that bank), and aligns the streams: equal PCs advance together; on a difference it skips the fewest instructions on either side after which 32 PCs agree, and names the cause. Per-instruction clocks are compared with the board's refresh (40) and DMA/HDMA clocks taken off. ```sh python scripts/mame_trace.py snes "$NO_INTRO_DIR/Nintendo - Super Nintendo Entertainment System/Super Mario World (USA).zip" \ --seconds 3 --out tests/mame_traces/lockstep-Super_Mario_World PYPY_GC_NURSERY=1M nice .venv-pypy/bin/python scripts/mame_lockstep.py \ "$NO_INTRO_DIR/Nintendo - Super Nintendo Entertainment System/Super Mario World (USA).zip" \ tests/mame_traces/lockstep-Super_Mario_World ``` Donkey Kong Country needs `--seconds 10` and `--board-factor 4` (below). Results, first 2,000,000 MAME instructions, board commit in status.md: | | Super Mario World (USA), LoROM | Chrono Trigger (USA), HiROM | Donkey Kong Country (USA), HiROM | | --- | ---: | ---: | ---: | | MAME instructions compared | 2,000,000 | 2,000,000 | 1,853,731 (board ran out at 8,000,000) | | aligned (same PC) | 1,805,843 | 1,871,391 | 1,789,080 | | of which every register equal | 978,185 (54.2 %) | 1,498,966 (80.1 %) | 1,625,183 (90.8 %) | | skipped, MAME / board | 194,157 / 149,713 | 128,609 / 66,378 | 64,651 / 6,210,921 | | divergences | 15,539, all named | 12,239, 1 not named | 10,619, all named | | clocks equal per aligned instruction | 1,715,077 of 1,790,304 | 1,825,979 of 1,859,152 | 1,757,687 of 1,778,460 | | board time (PyPy) | 13 s | 18 s | 41 s | **Divergences** (where the PC streams part), by cause: - **S-SMP ports `$2140-3`** (the great majority in all three): the two S-SMPs run at different times relative to the S-CPU (the board's S-CPU is slower: refresh, DMA, the timing below), so handshake loops run different numbers of iterations. - **An interrupt taken at another instruction** (NMI in a wait loop; the return address differs), **`$4210/$4211`, `$4212` (auto-joypad busy, HBlank)** and the **H/V counters**: frame and line timing, which MAME does not model to the clock. - **WAI with /IRQ held** (DKC): DKC writes `$4200 = $B1` with VTIME = HTIME = 0, sets P.I and loops on `WAI; BRA`. The IRQ line stays asserted (nobody reads TIMEUP), so the board's WAI resumes at once every time, as WDC's datasheet describes WAI with I set (DOC); MAME's WAI resumes once and then sleeps to the NMI (306,894 clocks). The board runs about 15,800 instructions a frame there to MAME's 300, which is why it ran out of its 8M budget. A console probe would settle it (probe-pack.md, `wai_irq`). - one Chrono Trigger divergence the heuristics did not name; the "polled in the skipped stretch" labels name the first register read inside a skipped loop and are a weak guess. **Register differences at the same PC**, by cause: the S-SMP ports and counters as above; **S after reset** (the core leaves `$01FC`, three stack cycles below `$01FF`; MAME shows `$01FF`; power-on S is a HYPOTHESIS either way); **MAME traces an interrupt handler's first instruction before setting P.I** (its P shows I clear, the next line shows it set); **power-on memory**: Super Mario World's `LDA [$00]` at `$00AAE3` reads `$7E:B8FF`, `$5500` in MAME, `$0000` on the board (WRAM zeros, HYPOTHESIS), and Chrono Trigger's `LDA abs,X` at `$FFF9D8` with DB = `$30` reads SRAM, `$FFFF` in MAME, `$0000` on the board; and **game state that drifted** (a counter at `$FDE3AC` in Chrono Trigger one apart after earlier divergences). All checked by hand; the harness calls them "unattributed: a memory value". **Clock differences per instruction** (aligned, after refresh and DMA), all in MAME's instruction timing; the board's core (65816-python) agrees with WDC's table 5-7 and with SingleStepTests on every opcode (65816-python docs), so these are MAME's, OBSERVED: | Instruction | MAME | WDC / board | Seen | | --- | --- | --- | ---: | | XBA | 2 cycles | 3 | 105,502 | | LDA/CMP/ADC abs,X or abs,Y, 16-bit index, no page crossing | no extra cycle | +1 when X = 0 | 22,677 | | STA/STZ abs,X, page crossed | 2 extra cycles (6) | 1 (5) | 966 | | JMP (abs,X) | 5 cycles | 6 | 17 | | RTI | 6 clocks more than the board in 9 cases, equal in the rest | 7 cycles native | 9 (condition not found: open) | | interrupt entry | no clocks between the interrupted instruction's line and the handler's | 7-8 cycles | measured on SMW's NMI: MAME 22-24 clocks for `LDA dp` + entry, board 84-86 | | WAI | to the wake-up | to the wake-up | 1 (the DKC case) | No difference was found that these causes do not explain, so the lockstep gives the board no correction to make. It is a detector only (rule 5): every row above is OBSERVED (MAME 0.285).