# MAME as a detector: the recipe, what it can say, what it cannot MAME 0.285 (`/usr/games/mame`, source pinned at `f379cd06` in `reference/src/mame`) is a **tier-5 detector**: it finds differences, it does not decide them ([validation.md](validation.md)). It is the easiest way to get the instruction stream and register values of real cartridges on the real EXEC. ## Recipe (run on 2026-11-11 and 2026-11-12) ```sh python3 -I scripts/mame_trace.py intv "$NO_INTRO_DIR/Mattel - Intellivision/Astrosmash (USA, Europe).zip" --seconds 2 python3 -I scripts/mame_trace.py intv2 "" --seconds 2 python3 -I scripts/mame_trace.py intv --no-cart --seconds 1 ``` The script makes a fresh directory `tests/mame_traces/-/` (gitignored; it refuses an existing one: move the old one into `trash/`), extracts the cartridge there and runs ```sh mame intv -cart X.int -rompath "$MAME_ROMPATH" \ -homepath . -snapshot_directory snap -video none -sound none -nothrottle -noreadconfig \ -skip_gameinfo -log -seconds_to_run 2 -debug -debugger none -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 %04X %04X %04X %04X %04X %04X %04X %04X %02X %d %d %d\\n",r7,r0,r1,r2,r3,r4,r5,r6,curflags,totalcycles,frame,beamy}') dbg:command("go") ``` `error.log` (in the working directory, not under `-homepath`) then holds one `C` line per instruction, before it executes: R7 R0-R6 flags (hex), totalcycles, frame, beamy (decimal). `cpu.trace` holds the disassembly. The snapshot at exit lands in `snap//0000.png`. The script ends with a summary. ## Results | Run | Instructions | Wall | Largest gap between instructions | Interrupt entries (`$1004`) | Snapshot | | --- | --- | --- | --- | --- | --- | | `intv`, Astrosmash, 2 s | 228,767 | 5.4 s | 21 cycles | first at 14,960, then every ~14,934 | "Mattel Electronics presents ASTROSMASH!" title | | `intv2`, Astrosmash, 2 s | 228,564 | 5.1 s | 21 cycles | 118, 14,934.6 apart on average | the same title | First instruction `C 1000` with every register 0. On `intv2` the EXEC runs 81 distinct addresses in `$0400-$04FF` (its extension ROM), first reached at `$04B5` (cycle 15,472): the starting point for U9. ## Traps 1. **`genflags` is not a symbol and the trace action is dropped silently** (the disassembly still appears, so it looks as if it worked). The CP1610's state names are `R0`-`R7`, `GENPC`, `CURPC`, `CURFLAGS`; use `curflags` (S `$80`, Z `$40`, OV `$20`, C `$10`). `frame`, `beamy`, `beamx` and `totalcycles` work. There is no symbol for the interrupt enable. 2. The drivers need MAME's ROM sets: `intv` (exec.bin + `ro-3-9503-003.u21`), `intv2`, `intvsrs`, `intvoice`, `intvecs`, `intvkbd`; all present and good in the non-merged set. 3. `-seconds_to_run` takes an integer. 4. SDBD never appears alone: MAME merges it with the next instruction (one line, a cycle delta of 14), as the cp1610 core's notes record. 5. One MAME process at a time on this machine; a 2 s trace is about 5 s of wall time and 5 MB of logs. ## What MAME can decide, and what it cannot | Question | MAME? | Why | | --- | --- | --- | | Which instructions a cartridge runs, in what order, with which register values, given the inputs | **detector** for M1 lockstep | the CPU core is independent of jzIntv's (Frank Palazzolo's) | | Cycle counts of instructions | detector with known faults | the cp1610 core lists them (SETC 6, `SARC r,2` 6, jumps 12, HLT a no-op, ...) | | **Bus-steal timing, row-copy timing, the register windows** | **no** | `intv_m.cpp` calls `adjust_icount(-(12*ROW_BUSRQ+FRAME_BUSRQ))` from a timer callback between CPU slices, where it has no effect ([hw26] STIC_NOTES T2). Measured here: the largest gap between consecutive instructions is 21 cycles and interrupts are 14,934.6 apart: the CPU loses **nothing** to the STIC. Its STIC constants come from jzIntv's documents (`stic.h`: "based on Joe Zbiciak's documentation"). Its frame is 14,934.68 cycles, not 14,934 | | A read of a closed STIC register | no | returns the address offset; the console returns `$0000` | | Pictures (STIC rendering) | detector, frame-at-a-time | compare at 4-bit colour numbers; border colour latched per frame (D17) | | PSG register writes and the PSG's stream | detector | [hw26] matched MAME's register logs and streams exactly on eight games once MAME's three timing quirks were reproduced (`PSG_NOTES.md` 3-4) | | Intellivoice | weak detector | MAME's SP0256 is Zbiciak's code (one witness with jzIntv) and runs at **9,285 Hz**, where console recordings say 10 kHz ([addons.md](addons.md)) | | Cartridge maps | detector | `hash/intv.xml` software lists (CC0) and `intv.hsi`; see the census for where they disagree with FreeIntv | So for timing the board's judge is the console results in [validation.md](validation.md); MAME is used for the instruction stream and registers with the STIC's stealing switched off in the board (a `mame` timing mode, as [hw26]'s model has), so that the two can be lockstepped at all.