# MAME as a detector: the verified recipe MAME 0.285 (`/usr/games/mame`) is a detector for this board, not a judge (see [validation.md](validation.md)). It is still the most useful one: it runs every cartridge in the romsets, and its debugger can log the Z80's full state before every instruction plus every I/O access, with exact cycle counts. A lockstep comparison against that log finds the first instruction where the board and MAME disagree. Then a hardware-tier source decides who is right. Everything on this page was run on 2026-09-18. `scripts/mame_trace.py` encodes it. ## The command ```sh python scripts/mame_trace.py sms path/to/cart.sms --seconds 12 python scripts/mame_trace.py sg1000 "$NO_INTRO_DIR/Sega - SG-1000 - SC-3000/Flicky (Japan) (Rev 1).zip" ``` which runs, in a fresh directory under `tests/mame_traces/`: ```sh mame -cart \ -rompath "$MAME_ROMPATH" \ -homepath -video none -sound none -nothrottle -noreadconfig \ -skip_gameinfo -debug -debugger none -log -seconds_to_run \ -autoboot_script /trace.lua ``` with this Lua (the CPU tag varies; see below): ```lua local dbg = manager.machine.debugger dbg.visible_cpu = manager.machine.devices[":maincpu"] dbg:command('trace z80.trace,,noloop,{logerror "%X %X %X %X %X %X %X %X %X %X %X %d %d %d\\n",pc,af,bc,de,hl,ix,iy,sp,af2,i,r,totalcycles,beamy,beamx}') dbg:command('wpiset 0,100,w,1,{logerror "IW %X %X %d\\n",wpaddr,wpdata,totalcycles; g}') dbg:command('wpiset 0,100,r,1,{logerror "IR %X %X %d\\n",wpaddr,wpdata,totalcycles; g}') dbg:command("go") local port = manager.machine.ioport.ports[":PERSISTENCE"] if port then port.fields["LCD Persistence Hack"].user_value = 0 end ``` The last two lines turn off the Game Gear's "LCD Persistence Hack", which blends each frame with the last: with it on, the exit snapshot's moving objects are half-bright and a line taller (GG Aleste II's stars), and every frame check against it fails. For a listed image whose board MAME would guess wrong, `-cart` takes the software-list name instead (trap 8). `error.log` lands in the working directory, not under `-homepath` (as for the other cores). ## What it produced | Run | Driver | Emulated time | Instructions | I/O events | | --- | --- | ---: | ---: | ---: | | Flicky (Japan) (Rev 1) | `sg1000` | 2 s | 707,843 | 56,231 | | SMS VDP Test v1.31 (`VDPTEST.sms`) | `sms` | 4 s | 2,192,661 | 22,832 | | SMS VDP Test v1.31 | `sms` | 12 s | 3,556,264 | 98,144 (17,387 reads) | | SN76489 TestRom (GG) | `gamegear` | 3 s | (trace on) | 7,976 | | YM2413 TestRom | `smsj` | 3 s | (trace on) | 18,579, incl. 49 writes each to $F0/$F1 | A sample from the 12-second VDP test run, the H-counter latch test: ``` 2C7B FF40 1D 20F3 0 FFFF FFFF DFFA 300 0 26 19537714 6 249 IW 3F 5 19537685 IW 3F FF 19537703 IR 7F 62 19537714 IR 7E DB 19537738 ``` The program toggles TH through port $3F and reads the latched H counter ($62) and the V counter ($DB). ## The traps, all found the hard way 1. **The CPU tag differs by driver.** `sg1000.cpp` names the Z80 `:z80`; `sms.cpp` (sms, sms1, smsj, smspal, gamegear) names it `:maincpu`. A wrong tag produces an empty trace and no error. `scripts/mame_trace.py` has the table. 2. **A `logerror` action stops logging above 14 values**, silently: with 16 values nothing is logged while the trace file still fills. So `BC'`, `DE'` and `HL'` are left out. `AF'` is kept, and a shadow-register divergence shows up one `EXX` later. There is no `ir` symbol: use `i` and `r`. 3. **I/O watchpoints need an active instruction trace.** With only `wpiset`, `sms1` and `smsj` logged nothing, while `sms` and `gamegear` logged normally. Watchpoints fire reliably on all five drivers once a `trace` is also running. (Cause not proven: the BIOS's $3E writes remap the I/O space, which may drop the watchpoints' handlers.) Re-arming the watchpoints from a Lua frame notifier (`wpiclear` + `wpiset`) *hangs* MAME: don't. 4. **Lua memory taps are the wrong tool here.** `install_write_tap` on the I/O space recorded nothing before the cartridge started and never saw a read. On top of that, `screen:vpos()` is not exposed to Lua in 0.285, and `device.clock` is nil. The debugger's `beamy`/`beamx`/`totalcycles` symbols are the reliable source. 5. **MAME stamps each I/O event with the instruction's start cycle**, not the bus cycle: in the sample, `IR 7F 62 19537714` has the same `totalcycles` as the `IN` instruction's trace line. For sub-instruction timing, MAME's log says *which* instruction, not *when* within it ([timing.md](timing.md)). 6. **MAME loads the first member of a zip**, which for test-program archives is often not the ROM (SMSVDPTest's zip starts with a compressed charset; MAME ran that 2 KB file as the cartridge). `scripts/mame_trace.py` extracts the `.sms`/`.sg`/`.gg` member itself. 7. **MAME's interrupt acceptance wanders by about ten cycles between frames.** In Flicky, the latest point at which MAME's frame interrupt can have been raised, measured against the frame, moves within an 11-cycle window from frame to frame, so no fixed interrupt time reproduces every entry. MAME's Z80 appears to see the VDP's INT line at scheduler boundaries rather than at the cycle (not proven). `scripts/lockstep_mame.py` therefore takes each interrupt where MAME took it, and reports how far that was from our INT rising. 8. **Given a file, MAME guesses the cartridge board**, and can guess wrong (`sega8_slot.cpp` `get_cart_type` counts `LD (nnnn),A` targets in the first 32 KB). Ernie Els Golf writes only $4000 there, so MAME ran it as a plain Sega-mapper board and showed a black screen; Galactic Revenge v1.0 has one `LD ($0002),A` and became a Zemina board. A listed image loaded by its software-list name gets the listed board, so `mame_media.py` offers every listed image that way (a scratch rompath with `/.zip`, plus `-hashpath`). An unlisted image MAME puts on another board is reported by the sweep as `mame-board`, not as a difference. 9. **`-seconds_to_run` snapshots at the first vblank after the time**, from the screen's last completed frame (`video.cpp`): the frame whose active display ends after the time. The sweep compares our frame completed after the same cycle. 10. **The power-on phase is one number per driver**, not one per log: MAME starts every run in the same state. Solving it per log from a handful of counter reads misfired on games that read few of them, so the lockstep uses `POWER_ON_PHASE` (SG-1000 139,460; SMS 142,245; GG 114,877 mclocks). The GG value was fitted to V counter reads over 20 Game Gear traces: 114,876 to 114,878 minimise the differing reads in every one of them (Greendog: 277 differing reads at 114,864, 1 at 114,877). 11. **Typing has no type-ahead.** `manager.machine.natkeyboard:post(text)` from an autoboot script types on the SC-3000's keyboard, but what is posted before the program reads keys is lost, and a long post overruns it. Post one line at a time from a frame notifier, with a few seconds between lines, starting after the prompt is up (BASIC Level III: frame 480; Disk BASIC: frame 1,200). 12. **A disk is slow to start, and MAME writes to the image it is given.** Disk BASIC on `sf7000` is ready at about 15 s, so a 10 s snapshot shows the IPL's screen. `-flop` on a copy: a `SAVE` changes the file, which is also how the sectors MAME wrote can be compared with ours. ## Lockstep with the CPU `scripts/lockstep_mame.py LOG --driver D` runs the whole system (z80-python on the board, through the export BIOS for `sms`) against MAME's instruction trace: registers and cycle counts before every instruction, and every port read against MAME's recorded read. For the SG-1000 the VDP phase comes from the trace's interrupt entries (the tightest upper bound on when INT rose), because the machine has no counters to read. `--patch-reads` continues past a read that differs, using MAME's value, and reports it. `--frame PNG` renders throughout, runs on to `--seconds` as the sweep does, and compares that frame with the exit snapshot MAME kept next to the log: with the reads patched, a frame that still differs is a rendering difference. ## Replaying MAME's I/O into the devices (no CPU) `scripts/replay_mame_io.py LOG --driver D [--seconds N]` feeds every port write from the log into a `Board` at MAME's cycle stamps and checks every port read MAME recorded. With `--seconds` it also compares our last complete frame with MAME's exit snapshot at the best alignment. The phase (where MAME's cycle 0 falls in our frame) is solved from V/H counter reads; the SG-1000 has neither, so its phase is solved from status bit 7 and then refined by short replays. Palettes differ, so frames are compared as colours, not RGB: MAME 0.285 maps the SMS's 2-bit levels to $00/$59/$AE/$FF (we use $00/$55/$AA/$FF), and its TMS9918A palette is not the wiki's. SMS pixels are compared as 2-bit levels, GG pixels as 4-bit levels, TMS pixels as the nearest of the 15 TMS colours. Where MAME's model was adopted for bits no hardware source fixes, the code says so: the Sega status low bits after a sprite overflow (`315_5124.cpp` `sprite_count_overflow`), and the TMS9918A's fifth-sprite bookkeeping (`tms9928a.cpp`: updated per displayed line while 5S is clear, 5S set only while the frame flag is clear, untouched while blanked, 31 in text mode). ## Driver facts - **`sms`** (export SMS2, BIOS `mpr-12808` in `sms.zip`) boots through its BIOS. The VDP test's first I/O came at cycle 3,408,512 (about 57 frames). With a cartridge that lacks the "TMR SEGA" header, it runs the built-in Alex Kidd instead: observed with Sega's Color & Switch Test. - **`smsj`** (Japanese SMS, BIOS `mpr-11124` + YM2413) does not check the header and runs the Color & Switch Test. - **`sms1`** has six BIOS options (`mpr-10052`, `mpr-11459a`, `mpr-11458`, `missiled`, `v1.0`, `m404prot`); `-bios` selects one. - **`sg1000`** needs no BIOS. For a `.sg` in a zip MAME prints `verify_cart failed` and a garbage header dump: harmless, the cartridge runs. - **The SG-1000 M2 Check Program** fails with `-cart` ("Unknown slot option 'xterminator'": MAME's loader misdetects the 8 KB image). Load it through the software list instead: put the image at `/sg1000/m2cp/sg-1000 m2 check program.bin`, add `` to `-rompath`, and run `mame sg1000 m2cp -hashpath /usr/share/games/mame/hash`. `-noreadconfig` drops the default hash path, so `-hashpath` is required. - Snapshots work with `-video none`: `manager.machine.video:snapshot()` from a frame notifier, with `-snapshot_directory`. ## What MAME does not model (so lockstep will diverge there) - The SG-1000 PSG's READY wait states (`sg1000.cpp` TODO list). - Sub-instruction I/O timing, *as logged* (point 5 above). Internally MAME's Z80 and VDP do schedule accesses within an instruction. - "Accurate video timing" on the SG-1000 is also a listed TODO in `sg1000.cpp`.