# MAME 0.285 as a detector MAME is an emulator: it finds differences, it does not decide who is right ([validation.md](validation.md)). This is the recipe that was run on 2026-11-11 with `/usr/games/mame` ("0.285 (unknown)"), and the traps found. ## The driver `src/mame/atari/a7800.cpp`, `maria.cpp`, `src/devices/bus/a7800/` (pinned sparse at tag `mame0285`, `reference/src/mame`). | Machine | Set | BIOS files (SHA-1) | | --- | --- | --- | | `a7800` (NTSC) | `a7800.zip` | `7800.u7` 4 KiB `d9d134bb6b36907c615a594cc7688f7bfcef5b43` (default); `c300558-001a.u7` 16 KiB `14584b1eafe9721804782d4b1ac3a4a7313e455f` (bios `a7800pr`) | | `a7800p` (PAL) | `a7800p.zip` | `c300558-001b.u7` 16 KiB `5a140136a16d1d83e4ff32a19409ca376a8df874` | Both sets are found below `$MAME_ROMPATH` (MAME's non-merged sets) and are read from there with `-rompath`. The same NTSC 4 KiB and PAL images are in the No-Intro folder as `[BIOS] Atari 7800 (USA).zip` and `(Europe).zip` (same SHA-1; `scripts/count_carts.py` prints all hashes, SHA-256 included). MAME's machine: M6502 at 1,789,772 Hz; screen 454 x 263 at 7,159,090 Hz pixel clock (59.957873 Hz), PAL 454 x 313; TIA sound only (`tiasound`, 31,399 Hz); MOS6532; cartridge devices with POKEY at $4000 or $450, the XBoard and XM (POKEY + YM2151), the High Score Cartridge. The software list is `/usr/share/games/mame/hash/a7800.xml`, 182 entries, **licence CC0-1.0** (its own header), keyed by CRC-32 and SHA-1 per ROM chip. ## The recipe ```sh python scripts/mame_trace.py a7800 \ "$NO_INTRO_DIR/Atari - Atari 7800 (BIN)/Asteroids (USA).zip" ``` which runs, in `tests/mame_traces/a7800-asteroid/` (ignored): ```sh mame a7800 asteroid -hashpath /usr/share/games/mame/hash \ -rompath ";/softlist" -homepath -snapshot_directory snap \ -video none -sound none -nothrottle -noreadconfig -skip_gameinfo \ -debug -debugger none -log -seconds_to_run 2 -autoboot_script /trace.lua ``` with `/softlist/a7800/asteroid.zip` a symbolic link to the No-Intro zip. The Lua script sets a `trace` with a `logerror` action per instruction and watchpoints on $00-$3F, $280-$29F, $400-$47F, and writes to $4000-$400F and $8000-$BFFF. Output format: the script's docstring. Results of the two runs made: | Run | Wall time | Instructions | Slot device | CPU cycles per frame | First instruction after WSYNC, beamx | | --- | --- | --- | --- | --- | --- | | `a7800` Asteroids (USA), 2 s | 19.3 s | 783,066 | `a78_rom` | 29,848-29,853, centred on 29,850-29,852 | 0-4 | | `a7800` Ballblazer (USA), 1 s | 9.1 s | 403,606 | `a78_pokey` | 29,848-29,853 | 1-4 | The first traced instruction is the BIOS's: `F884 0 80 0 36 1FD 8 32 251 0` (PC, A, X, Y, P, SP, total cycles 8, beamx 32, beamy 251, frame 0). The first writes are INPTCTRL ($01 <- $02 at cycle 14) and CTRL ($3C <- $7F at 29). The frame length agrees with the measured 29,850.5 ([HF]) on average; MAME counts whole instructions, hence the spread. ## Traps 1. **Loose headerless files are refused.** `mame a7800 -cart game.bin` (or a zip of it) fails with "Not a valid A7800 image": the loose-file path wants an `.a78`. A software-list load takes headerless data, and MAME finds a ROM inside a zip by CRC when the member's name differs, so the script links the No-Intro zip under the software's name instead of copying anything. Consequence: only dumps whose CRC is in the list can be run this way: **97 of the 124 licensed** zips (`mame_trace.py a7800 --list `; the census's 107 counts multi-chip entries by SHA-1 as well, which a one-member zip cannot satisfy). For the rest a headered temporary copy is needed (cartridge.md "A converter"; Open decisions). 2. **`-noreadconfig` drops the hash path.** Without `-hashpath` the software name is "not found" with an empty suggestion list. 3. **No slow cycles.** MAME runs every CPU cycle at 1.79 MHz; a console stretches TIA and RIOT cycles to 6 MARIA clocks (C11). An instruction lockstep must compare registers and instruction order, and treat the cycle column as MAME's own clock: the board's MARIA-clock count will run ahead of `4 x totalcycles` by 2 for every TIA or RIOT access. 4. **DMA is charged per line in one lump.** MAME decides the whole line's DMA seven CPU cycles into the line and suspends the CPU for `(maria_clocks + 3) / 4` cycles (`maria.cpp`, `spin_until_time`), so the rounding to the CPU grid is not the console's, and the suspension always starts at an instruction boundary (MAME's scheduler), where a console halts mid-instruction (C1). The per-line total is comparable; the cycle at which an instruction resumes is not. 5. **MAME's DMA constants differ from the measured ones**: last line of a zone +6 (measured +8, C2), DLI +17 (measured 0, C4), no once-per-frame 8, holes charged like A7800's older rule (C3). The comparison in milestone 2 has to subtract these, or be made on programs without DLIs first. 6. **When DMA overruns, MAME stops the CPU until the next line** (the same mechanism as WSYNC). Whether a console does exactly that is the "DMA aborted on the leading edge of Border" rule of [1702B] p. 18; MAME's limit is 426 clocks. 7. **P reads with bits 4 and 5 set** (mask with $CF), **SP carries the page** ($1FD), as on the family's other MAME traces. A `logerror` action drops lines with more than 14 values; the trace line has 10. 8. **beamx/beamy are MAME's screen position** (vblank from line 251 to 27 of its own numbering), usable as the line and clock of an instruction only within trap 4's limits. 9. **The RIOT timer** is MAME's `mos6532` clocked at the CPU rate; it does not count during the lump suspension (C12). 10. **`error.log` grows by about 15 MB per emulated second**; `cpu.trace` is a by-product of the `trace` command (the instruction disassembly) and can be deleted. 11. **A lockstep across the BIOS** needs the board to start exactly as MAME does (registers above; RAM zeroed or not is to be read off MAME's first RAM reads). The BIOS's signature check runs about 400,000 instructions before the cartridge starts on NTSC. 12. **Scripted input in a trace** (session 7): `mame_trace.py --script FRAME:ACTION ...` applies frames_mame.py's items at MAME's frame_done count. The board cannot be made to press at the same instruction by frame counting (the two frame counters differ by about two frames), so `lockstep_mame.py --force-switches` gives the board MAME's logged SWCHB values read by read; without it the lockstep stops at the first `LDA SWCHB` of the release. 13. **MAME's Lua screen has no `vpos()`**: a tap callback that calls it fails silently and logs nothing. Compute the line from `manager.machine.time` (454 x 263 clocks of 7,159,090 Hz a frame; MAME's line = that count + 251, mod 263, as `dli_compare.py` does). ## Writes-only: sound-register writes with times, without the debugger A 60-second instruction trace is gigabytes (trap 10) and takes about ten minutes of wall time a minute. For milestone 5 only the sound-register writes are needed, with a time stamp. The cheapest recipe found (2026-11-12, session 4) uses **no debugger at all**: Lua memory taps. ```sh python scripts/mame_trace.py a7800 ".../Ballblazer (USA).zip" --writes-only --seconds 65 [--wavwrite] ``` runs MAME without `-debug` and with an autoboot script that calls `manager.machine.devices[":maincpu"].spaces["program"]:install_write_tap(lo, hi, name, fn)` on TIA $15-$1A and $01 (and the 7800-mode mirrors at $100, $200, $300) and on POKEY at $4000-$400F, $0440-$045F and $0800-$080F. Each callback writes `S addr value seconds` to `writes.log`, the time being `manager.machine.time` (an attotime, printed to 18 decimals): inside a CPU's timeslice MAME's scheduler returns the executing CPU's local time, so it is the cycle of the write (the first BIOS write, INPTCTRL $02, lands at 8.38 µs = cycle 15, against cycle 14 in the instruction trace's `totalcycles` before that instruction's write cycle). The table returned by each `install_write_tap` must be kept referenced (a global), or the tap is removed when Lua collects it. | Run | Emulated | Wall time | Writes logged | Log size | | --- | --- | --- | --- | --- | | Ballblazer (USA), `a78_pokey` | 65 s | 3.4 s | 27,362 (27,172 POKEY, 10 TIA audio, 180 $01) | 0.86 MB | | Commando (USA), `a78_sg_pokey` | 65 s | 3.1 s | 18,659 (12,130 POKEY, 6,455 TIA audio, 74 $01) | 0.6 MB | `--wavwrite` adds `-wavwrite mame.wav`: MAME's mixed output, recorded even with `-sound none` (48 kHz, 16-bit stereo, both channels equal). That is the detector for the audio comparison (`scripts/audio_compare.py`). `--nmi-log` (with `--writes-only`, session 5) adds read taps on $FFFA and $FFFE (the NMI and IRQ vector fetches: one per handler entry) and a write tap on MARIA's DPPH, DPPL and CTRL, to `nmi.log`, and dumps the display list list from memory whenever it changed. MAME 0.285's Lua screen object has no `vpos`/`hpos`; the line is computed from the time (time 0 is its line 251, clock 0). `scripts/dli_compare.py` sets it against the board's DLIs frame by frame. Unsigned probes run on NTSC with the prototype BIOS (`mame a7800 -bios a7800pr`), which, like the PAL BIOS, does not check the signature; A7800 5.2 takes the same option and an old-API Lua (`manager:machine()`), `scripts/dli_probe.py --a7800`. ## What MAME can and cannot judge It can show: instruction order through the BIOS and into a cartridge, the INPTCTRL sequence, bank-select writes and the slot device MAME's list assigns, per-line DMA totals within trap 5, frames (snapshots) for picture comparison. It cannot judge: the slow cycle, halting mid-instruction, the DLI's cycle, RIOT timing under DMA, anything about 2600 mode that `a2600-python`'s die traces already judge better. ## A7800 5.2 The fork of MAME's driver by Mike Saarna and Robert Tuccitto, with DMA timing tuned against consoles (its README: "accurate DMA hole penalties", "Graphical register updates made mid-scanline are now displayed mid-scanline", machines `a7800dev`/`a7800pdev` that display DMA usage per line). The prebuilt Linux binary is pinned (`reference/build-a7800/a7800-linux-v5.2.tgz`, SHA-256 in `scripts/pinned_files.json`) and was tried once: - it starts headless only with `SDL_VIDEODRIVER=offscreen -video soft -window` (`-video none` fails with "OpenGL not supported", and there is no Xvfb here); - it loads `.a78` files and zips of them, and refuses headerless `.bin` ("Unspecified error") although it lists the extension; - the public 7800 Utility Cart (`.a78`) ran for 2 emulated seconds; - **a debugger trace was not obtained**: `-debug -debugger none -debugscript` with MAME's old `trace` syntax produced no file. Not resolved in Phase 0. It remains the most hardware-informed detector for DMA timing, and the build session should finish its recipe (try `-debugger` text commands through `-debugscript` with `trace ,0` alone, or `a7800dev`'s on-screen DMA bars through snapshots) before milestone 2's comparison.