# MAME 0.285 as a value detector for the Super FX > **Sources (wave 2, 2026-11-19).** byuu's model is cited to **ares** (ISC, > `ares/component/processor/gsu` and `ares/sfc/coprocessor/superfx` at `9408cb4`; notice > in [NOTICE](../NOTICE)), its readable form; bsnes is a closed box and is not cited. > MAME is cited from its BSD-3 files (`bus/snes/sfx.cpp`, `sfx_dasm.cpp`) and from runs > (OBSERVED, MAME 0.285: traces, krom's rows, the rung-4 replays); its GSU file > `cpu/superfx/superfx.cpp` is GPL and a closed box. A **†** marks a statement about > Mesen2, snes9x, the sd2snes GSU or MAME's `superfx.cpp` that was **read from source under > the old rule (before 2026-11-19)**: those are closed boxes from now on, and such a > statement is never the basis of a default. Results of running Mesen2 or MAME are > closed-box observations and carry no mark. MAME 0.285 (`/usr/games/mame`; source tag `mame0285`, `f379cd06`; its BSD-3 files in `../snes-python/reference/src/mame`) runs the GSU as `superfx1` / `superfx2` (`src/devices/cpu/superfx/superfx.cpp`, GPL-2.0+, licence header "copyright-holders:byuu": an older form of byuu's core, a closed box) behind the `lorom_gsu1` / `lorom_gsu2` cartridge devices (`bus/snes/sfx.cpp`). It is a **detector for register values and the order of the S-CPU's accesses**, one lineage with ares, and **useless for time** (C17). Everything here was run on 2026-11-06; `scripts/mame_gsu_trace.py` encodes it, snes-python's `docs/mame-oracle.md` (in `../snes-python`) is the parent recipe (IPL ROM, snapshots, colour traps). ## Loading - Any `.sfc` with a GSU chipset byte (`$13-$15`, `$1A`) and map `$20` loads as `lorom_gsu1` when it is at most 1 MiB, else `lorom_gsu2` (`snes_slot.cpp` l.578-586); the software list, when the dump is in it, names the slot directly (`snes.xml`: 22 `lorom_gsu1`, 14 `lorom_gsu2` entries). The device tag is `:snsslot:lorom_gsu1:superfx`, short name `superfx1` (or `...gsu2...`, `superfx2`). The driver needs `snes.zip` (the S-SMP IPL) on `-rompath`, read in place from a folder on `MAME_ROMPATH` (';'-separated, as MAME's `-rompath`). - The script extracts a `.zip`/`.7z` into its run directory and passes the `.sfc` to `-cart`; nothing else is written outside it. ## The recipe ```sh python -I scripts/mame_gsu_trace.py "$NO_INTRO_DIR/Nintendo - Super Nintendo Entertainment System/Star Fox (USA) (Rev 2).zip" \ --start-frame 600 --frames 3 --ram # trace: 15 s wall, 225,133 instructions python -I scripts/mame_gsu_trace.py CART --host-only --frames 1800 # settings and starts: 26-35 s python -I scripts/mame_gsu_trace.py KROM.sfc --krom --frames 900 # krom PASS/FAIL: 9-15 s python -I scripts/mame_gsu_trace.py CART --dasm 018A60:40 # MAME's disassembly ``` which runs, in `tests/mame_traces//` (gitignored): ```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 GSU device, makes it the visible CPU, sets watchpoints on the S-CPU's `$3000-$34FF` in banks `$00` and `$80` (all of `$00-3F/$80-BF` with `--all-banks`), its Game Pak RAM writes with `--ram`, its `$2116-$2118` writes with `--krom`, and at the start frame issues ``` trace /dev/null,:snsslot:lorom_gsu1:superfx,noloop,{logerror "G %X %X %X %X %X %X %X %X %X %X %X %X %X %d\n",pc,r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,totalcycles; logerror "H %X %X %X %X %X %X %X %X %X %X %X %X %d\n",r12,r13,r14,r15,sfr,pbr,rombr,rambr,cbr,colr,por,ramaddr,totalcycles} wpset 003000:maincpu,500,r,1,{logerror "R %X %X %d\n",wpaddr,wpdata,totalcycles; g} ``` (and the `w` twin), then exits at the end frame. The log is read from stderr (`-oslog`) as a stream and written as `gsu.log.gz` with runs of identical G/H pairs collapsed to `L n`. ## The output ``` W 301E B9 0 the S-CPU writes R15 = $BCB9 (krom ADD) W 301F BC 0 ... high byte: GO G BCB8 0 0 0 0 0 0 0 0 0 0 0 0 545276 PC R0-R11 cycles: the pipeline's NOP (pc = R15 - 1) H 0 0 0 BCB9 20 0 0 0 0 0 0 0 545276 R12-R15 SFR PBR ROMBR RAMBR CBR COLR POR RAMADDR cycles G BCB9 0 0 0 0 0 0 0 0 0 0 0 0 545277 IWT R1,#$7FFF ... G BCC1 8001 0 0 ... 545281 STOP (R1 = $7FFF + $8001 = 0) H 0 0 0 BCC2 26 ... SFR = $26: GO, CY, Z R 3030 6 545281 the S-CPU sees GO = 0, CY, Z R 301E C3 545281 R15 read back = $BCC3 = STOP + 2 (C1) ``` - **`pc` is `(PBR << 16 | R15) - 1`** (`state_export`), the address of the opcode in the pipeline; **the first instruction after every start is the pipeline byte** (`01h`, NOP, after STOP or power-on), shown at the start address - 1, not the ROM byte there (`trace_coverage.py` counts it apart). - **The log is in MAME's execution order**: the S-CPU's accesses fall between the GSU instructions where MAME's scheduler put them. **`totalcycles` is the GSU's**, and MAME 0.285 adds **one per instruction** (`execute_run` `m_icount -= 1`, whatever the opcode's fetches and waits cost), and does not advance it while GO = 0 between timeslices. Measured on 3 traced frames of Star Fox: every one of 6,016 S-CPU accesses after the first G line is stamped with the last G line's cycle; the next G line is 1 later 5,424 times and jumps (a restart) 12 times. So a replay applies host lines **in log order**, and the stamps are a check, not a clock. ## Traps found 1. **`-debug` crashes MAME 0.285 (SIGSEGV, exit 139) on a 32 KiB GSU cartridge** (krom's `GSUADD.sfc`), with or without a script, within a second; the same ROM without `-debug` runs, and Star Fox (1 MiB, GSU-1) and Yoshi's Island (2 MiB, GSU-2) run with it. **Mirroring the ROM to 1 MiB fixes it** (what the LoROM board's decode does anyway); the script does that for any ROM under 512 KiB, in the run directory. 2. **`logerror` drops arguments past 14** (snes-python trap 8): the 16 registers plus state go in two lines, G and H, from one trace action. 3. **MAME's GSU time is not the chip's** (C17): one cycle per instruction; the device is clocked at the slot's 21.48 MHz (`DERIVED_CLOCK(1,1)`), so a running GSU executes about one instruction per master clock, three to six times too fast; cache hits are charged a memory access inside the buffer countdown (`superfx_op_read`); it never waits on RON/RAN (its `op_read` does not test SCMR); an S-CPU write of R14 does not start a ROM read (U9). Values are unaffected; any count of cycles or instructions per frame is MAME's. 4. **MAME's disassembler is stateless**: `dasm` prints each prefix on its own line and the base opcode's name (`3E` then `AE 0D` prints `ALT2`, `IBT R14,0x0d`, which executes as `SMS (1Ah),R14`), so rung 0 compares forms after folding the prefixes. 5. **Watchpoints cover banks `$00` and `$80` by default**: a game that reaches `$3000` through another bank is missed unless `--all-banks` (slower: every ROM access in banks `$01-3F` meets the tap). Star Fox and Yoshi's Island showed their starts, settings and SFR polling in `$00`. 6. Host lines before the trace's start frame are logged too (845,688 for 600 frames of Star Fox: SFR polling), so the replay can bring the core from power-on to the window on the S-CPU's writes alone. Without `--ram` the S-CPU's Game Pak RAM writes are missing and a replay of a game that uploads data (all of them) diverges at the first load. 7. **A trace that opens mid-run cannot be replayed**: the GSU's own RAM writes before the window are not in the log, and `--start-frame` lands wherever the GSU happens to be (Star Fox frame 600: mid-run). `--arm-on-start` (2026-11-07) waits for the first `$301F` write at or after the start frame (a Lua write tap on `00:301F` and `80:301F`), dumps the GSU's `70:0000-71:FFFF` to `RUN/ram_at_start.bin` at that moment, then starts the trace and runs `--frames` from there; it gives up 600 frames after the start frame. **Pick a start frame the game uses the GSU at**: Stunt Race FX has no start between frames 900 and 1500 of its attract mode, Yoshi's Island (USA Rev 1) none between 300 and 900. 8. **Stunt Race FX restarts the GSU through another bank**: with the default `00`/`80` watchpoints a restart was missing from the log and the replay diverged at it (the core still stopped, MAME at the new R15); with `--all-banks` the same window replays with no finding. Use `--all-banks` for replays (about twice the wall time). 9. **MAME's Game Pak RAM is 1 MiB, unmasked** (`sfx.cpp` `sfx_ram[offset & 0xfffff]`, "TODO: mask sfx_ram based on the actual RAM"), so nothing mirrors inside `70-71`; the replay gives the core 128 KiB to match. ## What MAME does (emulator) The items below were read from `superfx.cpp` (GPL: under the old rule, before 2026-11-19; marked †) and `bus/snes/sfx.cpp` (BSD-3, citable). Every value claim the rung-4 replays exercise (C1, C6, C7, C9, C11, U9, U16, C18, C19) is also a closed-box observation: the MAME-switched core matched MAME 0.285 instruction by instruction. - † `device_reset`: registers 0, VCR 4 for both devices, pipeline NOP (U23, U26). - † STOP: SFR.IRQ and the line only when CFGR bit 7 is clear (C11); pipeline NOP; R15 incremented after, so R15 = STOP + 2 (C1). - † FMULT/LMULT: Sreg times R6 as signed 16-bit numbers, LMULT writes R4 then Dreg (so Dreg = R4 gets the high word; FMULT R4 likewise: C9); its internal clocks for them count more in high-speed mode than in standard, another reason never to read time from MAME. - † RAM buffer one byte, ROM buffer `m_memory_access_speed` = 5/6 (C13); MS0 cleared when CLSR is set (`superfx_update_speed`). - † S-CPU side (`sfx.cpp` for the vectors and RAM, `snes.cpp` for the window, both BSD-3): registers written a byte at a time (C6), all accessible while running (C7); GO 1 -> 0 clears CBR and the cache; `$3031` read clears IRQ; PBR write flushes the cache; the fixed ROM table while `access_rom()` is false **only in banks `40-5F`** (U16); RAM reads `FFh` and writes dropped while the GSU owns RAM (U16); the register window is `$3000-$32FF` only (`snes.cpp` `snessfx_lo_r`), `$3300-$34FF` going to the S-CPU's I/O decode (U12). ## The replay (rung 4), as built (2026-11-07) `python -m gsu_python.replay RUN --model MODEL` (`src/gsu_python/replay.py`): the trace must come from `--ram --all-banks --arm-on-start`. Two cores run in lockstep from the start that armed the trace, seeded from the first G/H pair, `ram_at_start.bin` and the last S-CPU writes of CFGR, SCBR, CLSR, SCMR and BRAMR before it: the **MAME-switched** core (C6 byte writes, C7 open, C9 high word, U9 no prefetch, no RON/RAN wait, C18's CBR-relative window, C19's 4-bit test, VCR 4, no mirrors) must match every G/H pair and every host read (SFR without bit 6); the **default** core is compared with it after every instruction, and a difference is re-run from the saved state with one switch at a time to name its entry. Six games, 1,701,962 instructions: no finding, no default-core divergence ([claims.md](claims.md)). Winter Gold (Europe) needs `--driver snespal`: on the NTSC driver it stays on its region screen and never starts the GSU. ## The replay (rung 4), as designed 1. Construct the core with MAME's power-on state (all registers 0, pipeline NOP, VCR 4, cache and RAM 0) over the cartridge's ROM bytes (read in place) and a zeroed RAM of the board's size. 2. Read `gsu.log.gz` in order. Apply every `W` line as the S-CPU's write: a register or cache write through the core's host interface, a RAM write (`--ram`) straight into the RAM array; check every `R` line's value against the core's host read **when the GSU is stopped** (MAME lets reads of a running GSU through: C7). 3. Before every G/H pair, compare R0-R15, SFR with bit 6 (R) masked (MAME's ROM buffer timing is its own), PBR, ROMBR, RAMBR, CBR, COLR, POR and RAMADDR; `L n` repeats the pair. Step the core one instruction per pair. 4. A divergence stops the run and is classified by C/U entry (C1 at a restart, C9 at FMULT into R4, U9 after an S-CPU R14 write followed by a GETxx with no GSU R14 write); anything else is a finding against the core. 5. Costs: MAME traces about 15,000 GSU instructions per wall second with the host watchpoints (225,133 in 15 s); keep windows to a few frames.