# MAME as a detector: the recipe that was run MAME 0.285 (`/usr/games/mame`, source pinned at `f379cd06`, tag `mame0285`). Tier 5: it finds differences, it does not judge them. Everything below was run on 2026-11-11 and 2026-11-12 on this machine. ## The one way in: `vgmplay` Every game driver in MAME 0.285 (388 machines: CPS-2, ZN, CPS-1.5) is built with the **high-level** `qsound_hle` device. The low-level `qsound` device, a DSP16A running `dl-1425.bin`, is instantiated only by the `vgmplay` driver ("only enabled in vgmplay for performance reasons", MAME 0.196 release notes). Without rebuilding MAME, a DSP trace therefore needs a VGM file as stimulus. `scripts/mame_lle_trace.py` writes one from scratch (a synthetic sine as the sample ROM and a handful of register writes, no game data) or takes any QSound VGM with `--vgm`. ```sh python -I scripts/mame_lle_trace.py # tone: 3 s, trace 1.000-1.017 s python -I scripts/mame_lle_trace.py --name busy --busy --seconds 2 # a host write every ~35 samples python -I scripts/mame_lle_trace.py --name dasm --dasm --seconds 1 # MAME's disassembly only python -I scripts/cycle_audit.py tests/mame_traces/tone/dsp.trace # charge it by the manual ``` Output lands in `tests/mame_traces//` (git-ignored): `stimulus.vgm`, `trace.lua`, `dsp.trace`, `qsound.wav`, `devices.txt`, `summary.json`, `mame.stdout`, `error.log`. About 75 seconds of wall time for the 3-second tone run on the loaded machine (MAME reports about 4 % of real time with the debugger on). One MAME process at a time. The command line the script runs (in the run directory): ```sh nice -n 19 mame vgmplay -quik stimulus.vgm \ -rompath "/ROMs (bios-devices);/ROMs (non-merged)" \ -homepath . -cfg_directory cfg -video none -sound none -nothrottle \ -noreadconfig -skip_gameinfo -debug -debugger none -log \ -wavwrite qsound.wav -seconds_to_run 3 -autoboot_script trace.lua ``` The rompath comes from `MAME_ROMPATH` (the local MAME set's device-set folder and non-merged folder); the device set `qsound.zip` holds `dl-1425.bin`. `mame -verifyroms qsound` says the set is good; `-listroms qsound` lists the file as 24,576 bytes. ## The Lua A frame notifier, held in a global (Lua taps that are not held are collected), finds the DSP by its short name, writes the device list and the register symbols once, and at the chosen time issues a debugger `trace`: ```lua dbg.visible_cpu = manager.machine.devices[":qsound:dsp"] dbg:command('trace dsp.trace,:qsound:dsp,noloop,{tracelog "%04X: pt=%04X ... t=%d\n",pc,pt,...,totalcycles}') -- one frame later dbg:command('trace off,:qsound:dsp') ``` - Device tags: `:qsound` (`qsound`), `:qsound:dsp` (`dsp16a`). - Register symbols (MAME 0.285): `PC CURPC CURFLAGS PT PR PI I R0 R1 R2 R3 RB RE J K X Y P A0 A1 C0 C1 C2 AUC PSW YH A0H A1H YL A0L A1L`. In expressions they are lower case (`pc`, `a0`, ...). `totalcycles` counts **machine cycles** (30,000,000 per second at the 60 MHz CKI). - `noloop` keeps every iteration of cache loops in the trace. ## Reading the trace Each instruction is two lines: the `tracelog` line (registers and `totalcycles` **before** the instruction) and MAME's own line `0:ADDR: disassembly`. Three things to know: 1. **Take the address from the disassembly line**, not from `pc`: while the cache runs, `pc` holds the ROM address after the block. 2. An instruction displaced by an **interrupt appears twice**: once at the entry (its line is followed by `0:001:`, and the two cycles charged to it are the entry's) and again after `ireturn`, when it really runs. 3. A conditional branch's target instruction is not traced separately: the qualifier's line (`0:547: if ne goto 0x055c // will branch`) carries the pair's three cycles. One frame (1/60 s, ~400 samples, ~402,000 instructions) is 84 MB of text: keep windows short. ## What was measured with it - The per-instruction cycle charge equals the manual's rules on every one of 805,331 executions in two windows; the sample loop is 1,245 cycles on both its paths (timing.md, C1). - The steady-state loop executes 608 distinct addresses (busy run), from 24 T classes (instruction-set.md). - The full disassembly of 0x0000-0x0FFF (`dasm program.dasm,0,1000,1,:qsound:dsp`): 3,810 lines, byte-identical on two runs a day apart. It is the reference for the disassembler rung; note that MAME disassembles the data tables (0x0D53-0x0FCF) and the zero padding as instructions too. - The tone stimulus produces sound in the LLE (a steady tone on both channels of `qsound.wav` from 1.0 s). ## Messages to expect - `bad rip detected, correcting qsound clock` (stdout): VGM files carry 4 MHz for QSound by convention; MAME runs the device at 60 MHz. - `DSP16: unimplemented SRTA = 00FF` (error.log): MAME does not model `srta` or SADD (C4). - ALSA permission errors: harmless with `-sound none`. ## What MAME cannot be used for here - Directed per-instruction tests: the DSP's program is the fixed mask ROM, and no MAME driver loads a different DSP16A program. A test program can only run in MAME by rebuilding it (not done; outside Phase 0). - Hardware truth: see validation.md. - Game audio through the LLE: rebuilding MAME with the low-level device in `cps2.cpp` (a "3-character change", per Vas Crabb's commit `3f67473`) would allow it; recorded host writes played as VGM (`--vgm`, or jtdsp16's `ver/fw/*.qs` lists converted) are the way without a rebuild.