# MAME as an oracle: the headless tap, as executed MAME 0.285 (`/usr/games/mame`) runs a real game's sound program and logs every access its CPU makes to the YM2151. The log is a **stimulus**: the writes, with their times, replayed through the referee (and later the core). MAME's own YM2151 (ymfm) answers the reads in the log, so the reads are a **detector** (tier 5), never a judge. ## The recipe ```text sh scripts/mame_tap.sh [rompath] python -I scripts/replay_tap.py reference/mame-run//ym2151-tap.txt --seconds ``` `scripts/mame_tap.sh` runs ```text mame -rompath "" -video none -sound none -nothrottle -noreadconfig -skip_gameinfo -cfg_directory /cfg -nvram_directory /nvram -seconds_to_run -autoboot_script scripts/mame_ym2151_tap.lua ``` in `reference/mame-run//` (git-ignored). The Lua script installs a read tap and a write tap on the sound CPU's program space over the chip's two addresses and writes one line per access: ```text ``` Environment: `YM_TAP_CPU` (default `:audiocpu`), `YM_TAP_BASE` (hex, default `f000`), `YM_TAP_CHIP` (default `:2151`), `YM_TAP_CLOCK` (default 3579545), `YM_TAP_PRESS` (optional: `"2.0:Coin 1,3.5:1 Player Start"` holds each named input field for 0.2 emulated seconds from that time). `scripts/replay_tap.py` turns the writes into referee events (two input clocks to a cycle; MAME's time 0 taken as the referee's cycle 0 after its own reset), runs `opm_ref`, and prints how many samples are not zero, the peak, and for every status read whether the referee's status at that cycle equals MAME's byte. ## Traps met - MAME 0.285's Lua has **no `device.clock`** (it is nil), so the chip's clock is passed in `YM_TAP_CLOCK`; `scripts/mame_census.py` lists each machine's. - Taps, the frame notifier and the stop notifier must be **globals**, or MAME's Lua collects them and the log silently stops. - An attract mode often keys no notes, and pressing coin and start does not always help (below). Pick a machine whose attract mode plays music. - MAME prints an ALSA sequencer permission error with `-sound none`; harmless. ## Runs (2026-11-11 and 2026-11-12, one MAME process at a time) | Machine | Emulated | Presses | Accesses | Output through the referee | Status reads equal | | --- | --- | --- | --- | --- | --- | | `ghouls` (CPS-1) | 8 s | none | 20,072 W, 16,009 R | silent | 7,988 of 7,988 (first 4 s) | | `ghouls` | 12 s | coin 2.0 s, start 3.5 s | 30,036 W, 23,980 R | silent | 23,969 of 23,969 | | `ghouls` | 24 s | coin 8.0 s, start 10.0 s | 60,080 W, 47,988 R | silent | 47,964 of 47,964 | | `ffight` (CPS-1) | 30 s | coin 10.0 s, start 12.0 s | 61,592 W, 64,487 R | silent (not replayed: no notes in the log) | | | **`ddragon`** (Technos) | 25 s | coin 15.0 s, start 17.0 s | 53,620 W, 55,330 R | **not silent**: first 15 s, 565,547 of 838,955 samples non-zero, peak +17,088 / -12,192 | 33,817 of 35,030 | Set SHA-1s are in [users.md](users.md). `ddragon` used `YM_TAP_CPU=:soundcpu YM_TAP_BASE=2800 YM_TAP_CHIP=:fmsnd` (`technos/ddragon.cpp:576`, `:972`). Each run took a few seconds of wall time (MAME reported 646% and 1,149% of real speed on this loaded machine). The `ghouls` log on disk is the 24 s run's; the replay outputs are kept in `reference/replay-ghouls-coin.txt` and `reference/replay-ddragon.txt`. **The CPS-1 logs.** The sound program re-arms the timers about 500 times a second (0x10, 0x11, 0x12, 0x14) and writes operator registers only at boot and when a sound command stops everything: 177 non-timer register writes in each of seconds 0, 1 and 12 of the `ffight` run, none otherwise. Neither the coin nor the start produced a key-on in the logs. Why (a DIP setting, the sound command path, something in the Lua presses) was not found; cps1-python, which owns the board, can export its own stream later. For this chip, those logs exercise timers, flags and /IRQ only, and on those MAME and the referee agree on every read. **The `ddragon` log** has music from the attract mode (non-timer register writes in every second from 4 to 15; 438 address writes to 0x08 in the whole log). Replayed for 15 s it is the first stream here that proves the path from a game's writes to sound on the referee. The status reads that differ: | MAME read | Referee | Count | Why | | --- | --- | --- | --- | | 0x00 | 0x80 | 1,200 | BUSY: the program polls BUSY 32-33 cycles after a data write; MAME clears it at 64 input clocks from the write; the referee shows it for 32 cycles from the cycle after the write ([timers-and-bus.md](timers-and-bus.md), C2) | | 0x01 | 0x00, 0x80, 0x81 | 12 | timer A flag at its edge | | 0x81 | 0x80 | 1 | the same | So the detector found one real difference (MAME's BUSY window is 4 input clocks early and 4 short) and a handful of edge cases whose size is within the alignment uncertainty: MAME's write times are instruction-granular, and the referee's cycle phase against MAME's clock is a guess (cycle 0 = MAME's time 0). ## What the log is worth - **Writes** (tier 5 timing): good stimulus. The write order and approximate spacing are the game's; the exact cycle is MAME's CPU model's. - **Reads**: MAME's opinion; a detector for BUSY and timer differences. - **No audio comparison** is made against MAME: ymfm is a detector, and the reference is the referee (a closed box). Rung 2 replays such logs through both the referee and the core and counts differing pins. Logs are derived from ROMs and stay in `reference/` or `validation/streams/` (both git-ignored).