# MAME 0.285 as an emulator-derived detector for the 65C816 MAME's `snes` driver runs real SNES code on its `5a22` device (the `g65816` core plus the 5A22's registers, `src/devices/cpu/g65816/`) with a whole board around it. It is **emulator-derived**: use it to find where a core first disagrees on ordinary code, then settle the disagreement with a higher tier ([validation.md](validation.md)). Its cycle accounting is per instruction, not per bus cycle, and it has no DRAM refresh, so it is never a referee for timing below the instruction. Everything on this page was run on 2026-10-21 on this machine, MAME 0.285 at `/usr/games/mame`. `scripts/mame_trace.py` is the recipe as a script. ## The S-SMP boot ROM problem, solved without a Nintendo dump `mame -listroms snes` requires one file, `spc700.rom` (64 bytes, CRC 44bb3a40), the sound CPU's boot ROM ("IPL"). None is installed here, none is fetched or committed, and the family does not use Nintendo's. MAME refuses to start without it but **runs with a wrong-checksum substitute** after `spc700.rom WRONG CHECKSUMS ... WARNING: the machine might not run correctly`. - **Any 64 bytes** (e.g. zeros) are enough for ROMs that never talk to the APU: krom's and gilyon's CPU tests pass that way. - **Commercial games** wait at start-up for the IPL's handshake ($AA, $BB on ports $2140/$2141) and then upload their sound driver through its protocol. `scripts/mame_trace.py` therefore writes `IPL`, a 54-byte SPC700 program written for this repository from the documented protocol (fullsnes "SNES APU Main CPU Communication Port"), not from Nintendo's code: set SP, post $AA/$BB, wait for $CC, then serve blocks (ports 2-3 destination, port 1 zero = jump, non-zero = transfer; each byte arrives in port 1 with its index in port 0, which is echoed; a port 0 value ahead of the expected index starts the next block). Unlike Nintendo's it does not clear page zero. The annotated listing is in the script. - Verified: **Super Mario World (USA)** boots to its title screen and attract demo at 10 emulated seconds with the substitute (snapshot inspected), so the driver upload and the handshake both work. `scripts/build_referees.py ipl` writes the same file to `reference/build/mame-roms/snes/` for running MAME by hand. The sibling `spc700-python` may later want a free IPL of its own; this one is a test fixture for MAME, not a claim about the real IPL's timing. ## The recipe Write `trace.lua` in a fresh working directory: ```lua local dbg = manager.machine.debugger dbg.visible_cpu = manager.machine.devices[":maincpu"] dbg:command('trace cpu.trace,,noloop,{logerror "%X %X %X %X %X %X %X %X %X %X %d\n",pc,pb,a,x,y,s,d,db,p,e,totalcycles}') dbg:command("go") ``` and run, from that directory, with the substitute at `roms/snes/spc700.rom` and the cartridge copied in as `cart.sfc`: ```text mame snes -rompath roms -cart cart.sfc -homepath "$PWD" \ -video none -sound none -nothrottle -noreadconfig -skip_gameinfo \ -debug -debugger none -log -seconds_to_run 5 -autoboot_script "$PWD/trace.lua" ``` `error.log` lands in the working directory. Its first line starts with a UTF-8 byte-order mark and the cartridge slot's header report (`[:snsslot] ...` lines); then one line per instruction, before it executes. Verified runs: | ROM | Emulated | Wall | Lines | Notes | | --- | --- | --- | --- | --- | | krom `CPUADC.sfc` (zero IPL) | 3 s | 13 s | 2,495,096 | ends in its PASS screen | | Super Mario World (USA) (substitute IPL), from its `.7z` | 5 s | 52 s | **4,176,707** instructions, 168 MB | reaches its main loop ($00:806B); totalcycles at the end 106,852,998 = 4.97 s of 21.477 MHz | ## Facts about the trace - Device `:maincpu`, shortname `5a22`, "Ricoh 5A22". State symbols (Lua `cpu.state`): `PC S P A X Y PB DB D E NMI IRQ GENPC CURPC CURFLAGS fastROM`. `pc` in a debugger expression is **PB:PC, 24 bits** (it looks 16-bit only while PB is 0, as at reset; corrected 2026-10-23); the bank is also `pb`. There are no M/X symbols apart from P's bits, and no WAI/STP state. - `totalcycles` counts **master clocks**: MAME's 5A22 charges 6 per CPU cycle (`m_divider = 6`) plus `bus_5A22_cycle_burst()` per access: +2 for slow regions (8 total), +6 for $4000-$41FF (12), 0 for fast ROM with `fastROM` set. Super Mario World's first instructions: `SEI` at $00:8000 advances it by 14 (8 fetch + 6 IO); `STZ $2100` by 30 (three 8-clock ROM reads + a 6-clock I/O write). This matches fullsnes's table; MAME applies it per access but counts IO cycles from per-instruction constants (`CLK_*` in `g65816cm.h`). - **No DRAM refresh**: hardware loses 40 master clocks per scanline (SNESdev *Timing*); MAME does not. Instruction deltas that span the refresh point differ from hardware by 40. - Other devices: `:soundcpu` (`s_smp`), `:s_dsp`, `:ppu` (`snes_ppu`), `:snsslot` with the cartridge (`:snsslot:lorom` for LoROM), joypads. - The family's trace traps hold: keep `logerror` to at most 14 values (11 used); `-debugger none` is required; Lua notifier subscriptions must be held in globals. - For a result without the debugger: `scripts/mame_trace.py ROM --no-trace` takes a PNG snapshot at the last frame and exits (60.10 frames/s NTSC). ## How MAME 0.285 scores | ROM | MAME 0.285 | Tier of the ROM | | --- | --- | --- | | krom CPU tests, all 23 | PASS on every row of each final screen | hardware-verified | | gilyon cputest-full | Success, 1,610 tests | self-checking (hw run unstated) | | gilyon cputest-basic | Success, 1,107 tests | same | MAME therefore agrees with the CPU-semantics ROMs everywhere they look, including the emulation-mode `(d,x)` quirk (U3/C4) that SingleStepTests lacks. Its per-cycle bus kinds (C1-C3) are not observable from a trace. ## Use: rung 4, the replay (run 2026-10-23) `scripts/mame_trace.py GAME --io [--io-banks B9,..]` adds read watchpoints whose actions log `R address value` after the trace line of the instruction that made the read: banks $00 and $80 at $2000-$7FFF (I/O, expansion), plus cartridge RAM (LoROM $70-$7D:$0000-$7FFF; HiROM $20-$3F:$6000-$7FFF, one conditional watchpoint each). `tests/mame_replay.py` runs the core in the minimal harness from the same reset, answers the core's reads of those regions from the log in order (a DMA's B-bus reads too: MAME logs them as $00:21xx), and compares registers and clocks after every instruction. `scripts/mame_replay.py DIR` prints the report; `tests/test_mame_replay.py` runs every traced directory under `-m slow`. Facts found doing it: - **Watchpoints**: one per bank for all 128 I/O mirrors makes MAME 0.285 make no progress at all (no trace line in 5 minutes). Four cost about 2.3 times the plain trace's wall time and change nothing MAME does (instruction counts identical with and without). A read through an unwatched mirror bank is answered by the harness and the core then takes MAME's registers; the replay counts those by address, and `--io-banks` re-traces with the banks named (Donkey Kong Country uses $B9 and $B0, A Link to the Past $09, Super Metroid $82). - **Interrupts**: MAME's trace has no line for an entry; the line after the interrupted instruction is the handler's first, with the entry's bytes already pushed. The replay recognises it by the vector target and S and steps the core's entry there. - **MAME's NMI entry does not set I** (`g65816i_interrupt_nmi` lacks `g65816i_set_flag_i`; its IRQ entry has it; DS section 7.5 sets I for both). Most games' handlers start with SEI, so it rarely shows. - **MAME takes an IRQ right after CLI, PLP or REP clears I**, and an NMI right after an entry; the core, like ares (ISC; the poll before the flag changes), runs one more instruction first. 30 cases in the seven games. Interrupt timing is rung 6's to settle on the console. - **MVN/MVP**: `REGISTER_A--` without a mask leaves A = $FFFFFFFF in the trace until the next instruction rebuilds it. - **Power-on WRAM is $55** in every byte (`snes_init_ram`, "0x55 isn't exactly right but it's close"). Games read it (Super Mario World does). - **Cycle counts** (`CLK()` constants per instruction) differ from the datasheet in a few fixed ways, every one found by the replay and then read in `g65816op.ipp`: XBA with M = 1 is 2 cycles (DS 3); RTI is one more than DS in both modes; JMP (a,x) is 5 (DS 6); indexed modes add a cycle only for a page carry, so a,x/a,y/(d),y reads with 16-bit index registers and no carry are one short, a,x/a,y stores and read-modify-writes with a carry are one long, and (d),y stores without a carry are one short. `mame_extra_cycles` encodes these; nothing else differed in 55.6 million instructions. MAME has no DRAM refresh, and charged HDMA nowhere the replay could see. - `trace cpu.trace` also writes MAME's disassembly of every instruction to `cpu.trace`, a referee for a disassembler. The first divergence is still a lead, not a verdict: look it up in [undocumented-behavior.md](undocumented-behavior.md), then ask a higher tier.