# MAME 0.285 as a detector: the VDP port-log recipe that was run MAME (`/usr/games/mame`, 0.285; source of the TMS99x8 device and the two drivers pinned sparse at tag `mame0285`, `f379cd06`, in `reference/src/mame`) is a **detector** for this core, never a judge ([validation.md](validation.md)). Its `tms9928a.cpp` draws a whole line at `HORZ_DISPLAY_START`, serves every port access at once, has no access windows, no status race and no 4K address mapping (its own to-do list). What it is good for: **register streams**. A log of every port access a real game makes, stamped with the CPU clock and the beam position, replays into the core with no CPU at all, and MAME's last frame and final VRAM are a first picture to compare. Everything here was run on 2026-11-12 on the loaded machine, one MAME at a time. ## The recipe: `scripts/mame_vdp_log.py` ```sh python -I scripts/mame_vdp_log.py coleco "/colecovision/Zaxxon (USA, Europe).7z" \ --seconds 20 --press "STD_KEYPAD1:1 (pad 1)@14" --out tests/mame_traces/coleco-Zaxxon-play python -I scripts/mame_vdp_log.py sg1000 "/sg1000/Flicky (Japan, New Zealand) (Ja).7z" \ --seconds 20 --out tests/mame_traces/sg1000-Flicky ``` It extracts the cartridge into a new run directory under the git-ignored `tests/mame_traces/` (it refuses an existing one) and runs ```sh mame -cart -rompath "" -homepath -snapshot_directory snap \ -video none -sound none -nothrottle -noreadconfig -skip_gameinfo -log -seconds_to_run \ -debug -debugger none -autoboot_script /vdp.lua ``` with a Lua script that (1) selects the Z80 (`:maincpu` on `coleco`, **`:z80` on `sg1000`**), (2) sets two I/O watchpoints over the VDP's port block (`$A0-$BF` on the ColecoVision, `$80-$BF` on the SG-1000): ``` wpiset A0,20,w,1,{logerror "W %X %X %d %d %d\n",wpaddr&0xff,wpdata,totalcycles,beamy,beamx; g} wpiset A0,20,r,1,{logerror "R %X %X %d %d %d\n",wpaddr&0xff,wpdata,totalcycles,beamy,beamx; g} ``` (3) dumps the chip's 16K VRAM space to `vram.bin` from a machine-stop notifier, and (4) optionally holds an input field for 0.25 s at a given emulated time (`--press`, through `emu.register_periodic` and `ioport.ports[...].fields[...]:set_value`), to get a game past its menu. `-seconds_to_run` saves the last frame as `snap//0000.png`. `error.log` holds one line per access: `W|R port data totalcycles beamy beamx`. Port bit 0 is MODE (even = data, odd = control/status). `totalcycles` is the Z80 clock count at the start of the instruction that made the access; MAME's ColecoVision Z80 runs with one extra clock per M1 cycle, the SG-1000's without. `beamy`/`beamx` are MAME's screen position: line 0 is the first line after vertical sync, the active picture is lines 40-231 (`VERT_DISPLAY_START_NTSC` = 13 + 27) and pixels 37-292 (`HORZ_DISPLAY_START` = 2 + 14 + 8 + 13), in pixel clocks. ## What it produced | Run | Emulated | Wall | Data writes | Control writes | Data reads | Status reads (F set) | Last frame | | --- | ---: | ---: | ---: | ---: | ---: | ---: | --- | | `coleco-Zaxxon` | 5 s | 1.3 s | 17,648 | 136 | 0 | 1 (1) | the BIOS title: "COLECOVISION / PRESENTS SEGA'S ZAXXON / (C) 1982 COLECO" | | `coleco-Zaxxon-15s` | 15 s | 1.7 s | 35,090 | 228 | 0 | 3 (3) | the skill menu ("TO SELECT GAME OPTION, PRESS BUTTON ON KEYPAD") | | **`coleco-Zaxxon-play`**, keypad 1 at 14 s | 20 s | 3.9 s | 75,492 | 18,166 | 128 | 132 (131) | **gameplay**: score, fuel gauge, altimeter, the player's ship and the fortress wall: sprites over Graphics II | | **`sg1000-Flicky`** | 20 s | 8.1 s | 280,235 | 370,098 | 8,355 | 1,108 (1,108) | **the attract demo**: platforms, Flicky, the cats and chicks as sprites | Log sizes 225 KB to 9.4 MB; VRAM dumps 16,384 bytes each. The four directories are git-ignored; rerun the commands to regenerate them. MAME was deterministic across two runs here: the 15-second Zaxxon log's first 17,786 lines equal the 5-second log byte for byte. Read off the logs: - **The BIOS's first VDP acts on the ColecoVision**: `W BF 00`, `W BF 40` (address `$0000` for writing) at clocks 1,793 and 1,818, then zeros to `$BE` every 47 clocks: 16K cleared. - **Status reads land outside the active picture** almost always (Zaxxon: 127 of 132 in MAME lines 232-261 or 0-39; Flicky: 1,105 of 1,108), nearly all with F set (`$80` plus 5S/C/number bits): the games read status in their interrupt handler as TI advises. The eight reads inside the active area are where the status race could bite on hardware. Flicky reads `$C4` and `$FC` most often, i.e. **5S is set with fifth-sprite numbers 4 and 28**: its attract mode puts five sprites on a line every frame, so the 5S rules are exercised by a real stream. - **Flicky writes VRAM during active display at 21-clock spacing** (93 accesses in 20 s, e.g. line 58): faster than TI's worst-case 8 us, a ready-made case for the access-window rung ([timing.md](timing.md)). Zaxxon never comes closer than 36 clocks in active display. - *Zaxxon* reads VRAM 128 times in gameplay (`R BE`, after setting a read address): the read-ahead path is in a real stream. ## The census `python -I scripts/mame_census.py` (15 s of CPU): 217 machines instantiate a TMS99x8 ([consumers.md](consumers.md)). ## Traps (found here) 1. **A wrong CPU tag segfaults MAME at the first `wpiset`**: `sg1000.cpp` names its Z80 `:z80`, not `:maincpu` (sms-python's oracle notes give the same tag table; there a wrong tag only gave an empty trace). The script has the table. 2. **`emu.add_machine_periodic_notifier` does not exist in 0.285**; `emu.register_periodic` does. Notifier handles must be held in globals. 3. **Watchpoints worked here without an instruction trace** on `coleco` and `sg1000`. sms-python found that `sms1` and `smsj` log nothing without a trace running: if a later driver's log comes back empty, add a `trace` to `/dev/null` first. 4. `error.log` lands in the working directory (the run directory here), starts with a byte order mark and `Soft reset`, and is appended to if reused: the script refuses to reuse a directory. Failed runs were moved to the git-ignored `trash/`, not deleted. 5. `-seconds_to_run` takes integers. The watchpoint's `totalcycles` is the instruction's start, not the bus cycle of the access: an `OUT` writes some clocks later (colecovision-python's MAME notes measured the ColecoVision's `OUT ($FF),A` at 40 clocks). 6. The `sg1000` driver needs no BIOS; `coleco` needs `coleco.zip` from the MAME sets. ## What MAME cannot tell us Access windows and lost writes; the status race; the dot at which F, 5S and C set (it sets F while drawing line 193, about 1.25 lines after TI's moment: C10); off-screen sprite collisions (it tests visible columns only: C7); the 4K address mapping; anything inside a line.