# MAME 0.285 as a detector: the recipe that ran MAME is tier 5 ([validation.md](validation.md)): it finds differences, it does not decide them. For this board it is also the only register-level description of the custom chips, so it is the detector every milestone from M2 to M10 uses. This page is the recipe as it was run here, with its output and its traps. ## The command `scripts/mame_trace.py SET --seconds S --trace-seconds T` writes a Lua script into `tests/runs/SET/` (git-ignored) and runs, in that directory: ```sh mame SET \ -rompath "$MAME_ROMPATH;$MAME_CHDS" \ -video none -sound none -nothrottle -noreadconfig -skip_gameinfo \ -debug -debugger none -log \ -nvram_directory tests/runs/SET/nvram -cfg_directory tests/runs/SET/cfg \ -snapshot_directory tests/runs/SET/snap -snapname final \ -autoboot_script tests/runs/SET/trace.lua -seconds_to_run S+30 ``` with `MAME_ROMPATH` the non-merged sets and `MAME_CHDS` the merged CHDs. The Lua script: 1. writes `symbols.txt`: the main CPU's tag and short name, its address spaces, every debugger state symbol with its reset value, and the device list; 2. sets `dbg.visible_cpu` and issues `trace trace.log,,noloop,{tracelog "%04X %04X R0=%08X R15=%08X PR=%08X SR=%08X ",ow@pc,pw@pc,r0,r15,pr,sr}`: one line per instruction with the word at PC in the opcode space (`ow@`), the same word in the program space (`pw@`), four registers (values before the instruction), then MAME's disassembly; 3. from a frame notifier: switches the trace off after `T` emulated seconds, samples PC, SR and the DMA status word `040C000C` once per emulated second into `state.txt`, and at `S` seconds takes a snapshot and exits. ## What it produced `[ran]` | Run | Date | Wall time | Result | | --- | --- | --- | --- | | `redearthnr1 --seconds 4 --trace-seconds 0.02` | 2026-11-11 | 12.6 s at load 54 | game code (`060018xx`-`06001970`) from the first sample at 1.0 s; snapshot at 4 s **black** | | `redearthnr1 --seconds 8 --trace-seconds 0.001` | 2026-11-12 | 14.1 s at load ~80 (MAME reports 331%) | PC in `060018D0`-`06001970` at every sample; snapshot at 8 s: the game's "WARNING ... south-east Asian countries only" screen | | `redearthnr1 --seconds 1.5 --trace-seconds 0.001` | 2026-11-12 | ~10 s | snapshot at 1.5 s black | | `sfiii --seconds 8 --trace-seconds 0.001` | 2026-11-11 | | PC in the BIOS (`00000F82`-`00003D4A`) throughout; snapshot at 8 s: the BIOS's "A New CD-ROM has been inserted. Do you wish to rewrite the game? ... 15~20 min. to load" screen over the New Generation bitmap | The status page of 2026-11-11 said Red Earth "ended on RED EARTH / 961023 / ASIA (NCD)"; no snapshot of this phase shows that screen (1.5 s and 4 s are black), so it is not claimed. What is shown is that a NO CD set leaves the BIOS for the program SIMM within one emulated second and reaches the game's own region warning by eight. Symbols (`symbols.txt`): CPU tag `:maincpu`, short name `sh2_7604`; address spaces `program` and `decrypted_opcodes`; register symbols `PC`, `CURPC`, `SP`, `R0`-`R15`, `SR`, `PR`, `GBR`, `VBR`, `MACH`, `MACL`, `EA`, `CURFLAGS`. The Lua `cpu.clock` is `nil`. The first traced instruction is `00000400: MOV.L @($0004,PC),R13` with R15 = `02080000`: the decrypted reset vectors ([encryption.md](encryption.md)). The first traced frame holds 280,424 instructions for `redearthnr1` (the BIOS clearing RAM in a tight loop at `00000E7C`) and 495,758 for `sfiii` (the first notifier frame starts at reset, so it is not a whole display frame). MAME charges most instructions one clock and has no cache, so its counts are instruction counts, not board time ([validation.md](validation.md), "the board"). `error.log` lists every unmapped access; the ones that are not cache purges are table "Writes nobody maps" in [memory-map.md](memory-map.md). The CRTC values the BIOS writes to the SS registers appear there too (`SS regs write 00 data 2A` ...). ## Traps 1. **An unknown symbol silently drops the whole trace action.** Using `opcode` in the `tracelog` format gave a trace with no prefix at all and no error. Check every symbol against `symbols.txt`. 2. **The trace length is whole frames.** It is switched off from a frame notifier, so `--trace-seconds 0.001` still traces a whole frame: 280,000-500,000 lines, about 24 MB. 3. **MAME cannot show the raw bus for a live-key game.** It decrypts the BIOS region in place at start-up and decrypts data reads of the program SIMMs in its handlers (its opcode space is a separately decrypted copy). Both `ow@` and `pw@` therefore show plaintext. The raw (encrypted) word comes only from the set's files and the mask. For 2nd Impact (`m_altEncryption`), `pw@` on the SIMMs is raw and `ow@` is decrypted. 4. **The SH-2 DMAC re-encrypts.** MAME's DMA read hook XORs the mask back onto reads from the BIOS and the program SIMMs, so a DMAC copy sees what the board's bus would carry. A lockstep comparison of DMAC copies must compare raw bytes. 5. **A NO CD set copies its SIMMs into `nvram/`** (44 MB for Red Earth: `simm1.0` ... and the 128-byte `eeprom`). The directory is git-ignored; later runs reuse it. A CD set's `nvram/` holds what the BIOS has flashed so far. 6. **The frame notifier must be held in a global** (`cps3_sub = emu.add_machine_frame_notifier(...)`) or it is collected and the run never stops. 7. **`-video none` still writes the snapshot**, through the software snapshot path. 8. **Cache purges look like unmapped writes.** The BIOS's associative purges (`40000000`, `40000010`, ... = `00000410`) are logged as "unmapped program memory write": MAME has no cache, so nothing in its log or trace says anything about cache state. 9. The ALSA "seq" permission error at start is harmless. 10. Lua `device.debug:bpset` segfaults this build; the debugger command `bpset` works (family note). ## Not run here - Any CD set with `wd33c9x` logging switched on (the SCSI command list the BIOS issues). - Flashing a blank board from a CD to the end (15-30 emulated minutes). - Save states, frame-by-frame picture dumps, a sound stream dump. - FinalBurn Neo (a port of MAME's early driver; it adds a speed hack and a periodic IRQ10). ## How the build uses it - **Lockstep windows** (M0, M2): run the board and MAME from reset over the same set, compare PC and registers at every instruction for a window of a few hundred thousand instructions, and name every difference. Expect differences from the moment the board charges real bus costs (M11): from then on compare at VBlank boundaries (register state at IRQ12 entry), not cycle by cycle. - **Memory dumps** at a chosen frame (`save` debugger command on `program` ranges): work RAM, sprite RAM, colour RAM, SS RAM, character RAM (through MAME's `char_ram` share). - **Pictures** (M5-M9): MAME's render buffer holds 17-bit colour indices before the palette lookup; compare at that level, never at RGB.