# MAME 0.285 as a detector MAME is an emulator (tier 5): its traces find differences, they do not decide them. The binary run is the machine's installed `mame` (`0.285 (unknown)`; `scripts/mame_trace.py` takes `--mame`, else `$MAME`, else `mame` on PATH); its source is pinned at `mamedev/mame` @ `f379cd06` (tag `mame0285`), `src/devices/cpu/m6502/` (BSD-3-Clause), read for facts only. ## Which device is which row MAME's CMOS 6502s (`dw65c02.lst`, `dr65c02.lst`, `dw65c02s.lst` are the dispatch tables; `ow65c02.lst`, `ow65c02s.lst` the per-cycle lists): | MAME device | What it is in MAME | This core's row | Drivers in 0.285 | A driver that needs no media | | --- | --- | --- | --- | --- | | "WDC W65C02" (`w65c02`) | the base: no bit instructions, no `WAI`/`STP` | `G65SC02` | 230 | `apple2ee` (Apple //e enhanced) | | "GTE G65SC02", "G65SC12" (`g65sc02.cpp`) | the base device under other names; its source says in a comment that the parts' differences are not emulated | `G65SC02` | 37, 20 | `bbcm` (BBC Master); `lynx` needs a cart | | "Rockwell R65C02" (`r65c02`) | base + bit instructions **+ `WAI`/`STP`** (C5) | `R65C02` | 277 | `cexpert` (Fidelity Chess Expert) | | "WDC W65C02S" (`w65c02s`) | `r65c02` + different dummy-read addresses (C9) | `W65C02S` | 5: `arbv2`, `cmmb103`, `cmmb162`, `bbc_tube_rc6502`, `a2superdrive` | `cmmb103` and `arbv2` (replayed in the build) | `reference/mame_cmos_cpus.tsv` (made by filtering `mame -listxml`) lists every driver per device. Run as a closed box, `mame -listxml lynx` names one CPU, the "GTE G65SC02" device, clocked at 4,000,000 Hz (checked 2026-11-13); MAME therefore runs the Lynx's cell without the bit instructions, which agrees with VDS. It uses the same device for the Lynx II (C12). ## The recipe python -I scripts/mame_trace.py lynx --cart "" --seconds 1 python -I scripts/mame_trace.py apple2ee python -I scripts/mame_trace.py cexpert python -I scripts/mame_trace.py bbcm which runs, in a fresh `tests/mame_traces//` (gitignored): mame [-cart FILE] -rompath "" -homepath -video none -sound none -nothrottle -noreadconfig -skip_gameinfo -debug -debugger none -log -seconds_to_run N -autoboot_script trace.lua with `trace.lua` issuing, from Lua, `trace cpu.trace,,noloop,{logerror "%X %X %X %X %X %X %d\n",pc,a,x,y,sp,p,totalcycles}` and `go`. `error.log` then has one line per instruction, before it executes: `PC A X Y SP P totalcycles` (hex, the last decimal). Run on 2026-11-11, one emulated second each: | Driver | Instruction lines | First lines | | --- | --- | --- | | `lynx` + "T-Tris (World) (Pirate)" | 1,121,825 | `FF80 0 80 0 1FD 36 8`, `FF83 1 80 0 1FD 34 12` | | `apple2ee` | 410,528 | `FA62 0 80 0 1FD 36 8` | | `cexpert` | 1,678,793 | `E6A3 0 80 0 1FD 36 8` | | `bbcm` | 560,847 | `E364 0 80 0 1FD 36 8` (`$E364` is also the first address of the real Master's capture) | ## The lockstep (rung 5) python -I scripts/mame_trace.py lynx --bus --seconds 1 --cart "" python -I scripts/mame_trace.py cexpert --bus --seconds 1 python -I scripts/mame_trace.py bbcm --bus --seconds 2 python -I scripts/mame_trace.py apple2ee --bus --seconds 3 python -I scripts/mame_trace.py cmmb103 --bus --seconds 1 python -I scripts/mame_trace.py arbv2 --bus --seconds 1 python -I scripts/run_mame_replay.py `--bus` adds two Lua memory taps on the CPU's program space, so that between the instruction lines `error.log` holds every access MAME's CPU made: `[luaengine] R address data` and `W address data`. A trace is then about 170 to 270 MB and takes one to three minutes to record. `tests/mame_replay.py` replays it with no model of the machine: the core's reads are answered from MAME's reads of the same address in the same instruction, its writes are compared with MAME's, its registers and cycle count with the next line. Run on 2026-11-12 (PyPy, about two minutes for all four): | Driver | Row | Instructions | Interrupt entries | Agree in registers, writes and cycles | Differ | | --- | --- | --- | --- | --- | --- | | `lynx` + T-Tris | `G65SC02` | 1,120,163 | 1,658 IRQ | 1,121,813 | 8 | | `bbcm` | `G65SC02` | 1,112,640 | 323 IRQ | 1,112,938 | 25 | | `apple2ee` | `G65SC02` | 1,177,619 | 0 | 1,177,619 | 0 | | `cexpert` | `R65C02` | 1,683,330 | 489 IRQ | 1,683,787 | 32 | | `cmmb103` (`--seconds 1`) | `W65C02S` | 5,334,258 | 1 IRQ | 5,334,259 | 0 | | `arbv2` (`--seconds 1`) | `W65C02S` | 5,360,310 | 1,305 IRQ | 5,361,615 | 0 | The first four are the drivers rung 5 names; the last two are MAME's "WDC W65C02S" device (two machines that need no media; their traces are 684 MB and 772 MB), added so that the `W65C02S` row has a detector too. `cmmb103` executes 383,172 bit instructions; neither executes `WAI` or `STP`. Over 15.8 million instructions and 3,776 interrupt entries no register, flag or written byte differs. The 65 differences are two kinds of cycle count: | Difference | Count | Which side a higher tier supports | | --- | --- | --- | | `ROR abs,X` (`$7E`, `bbcm`) and `ROL abs,X` (`$3E`, `cexpert`) within a page: core 6 cycles, MAME 7 | 25 + 32 | **The core.** DS Table 7-1 "Read/Modify/Write instructions absolute indexed in same page: six cycles"; GDS Tables IV and V; CAP: `1E 3E 5E 7E` took 6 in every execution on a real G65SC12 and a real R65C02 (C19) | | `STZ $FD91` on the Lynx: core 4 cycles, MAME 1,400 to 3,840 | 8 | **Neither: it is the board's.** `$FD91` is `CPUSLEEP`; MAME holds the CPU with `INPUT_LINE_HALT` while Suzy draws and the CPU's cycle counter keeps running. The instruction is four cycles; the hold is `lynx-python`'s to time | What the lockstep did **not** meet, so says nothing about: reset (the traces start after it; MAME reads the stack on every CMOS device and counts 8, C4, U1, U2), `$CB`/`$DB` on the `r65c02` device (C5: `cexpert` never executes them), `BRK` with an interrupt pending (C6), `WAI` and `STP` (neither W65C02S trace executes them). Dummy-read addresses differ between MAME and the core in several sequences (C9) and the replay is blind to that by design: a core read at an address MAME did not touch in that instruction is answered from the last byte seen there, or with `$FF` when MAME had never touched the address at all (0 such reads in the first four traces; 10,556 and 812 in the two W65C02S traces, whose device has MAME's other dummy pattern). MAME takes its interrupts at instruction boundaries, and its `CLI`, `SEI` and `PLP` delay I as the core's do: none of the 3,776 entries began at a boundary where the core's poll refused it. ## Traps - **`lynx` will not start without a cartridge** ("Images must be mounted for the following devices: cartridge"). Pass `--cart`; MAME opens a `.zip` holding one `.lnx` directly, so No-Intro files are read in place. It also warns "lynx.bin ROM NEEDS REDUMP" (the boot ROM set in `lynx.zip`); it runs. - **The debugger command must come from Lua** (`-autoboot_script`), not `-debugscript`, under `-debugger none`; and `noloop` is needed or MAME folds repeated loops into one line (the family's recipe, from `huc6280-python/scripts/mame_trace.py`). - `sp` prints as `1FD`: MAME keeps the page bit in the register. Mask to 8 bits. - `P` prints with bits 4 and 5 set (`36`, `34`): compare without B. - X is `$80` at power-on in MAME; a real chip's registers are undefined. A replay harness takes the first line's registers as the initial state. - **`totalcycles` is 8 at the first instruction**: MAME's reset costs 8 on its counter (C4: real parts take 7), and its reset **reads** the stack on every CMOS device (`reset_c`), against the capture of a real G65SC12 (writes). - `totalcycles` counts CPU cycles at a flat 4 MHz on the Lynx: MAME has no page mode, no register waits and holds the CPU with `INPUT_LINE_HALT` for `CPUSLEEP`, so cycle deltas across a sprite draw are not the CPU's. - MAME's cycle lists differ from SST in dummy-read addresses and from the data sheets in `BRK` with an NMI pending (C6); its `$5C` reads `PC` seven times after the opcode (8 cycles, C1 agrees on the count); its `$DC`/`$FC` are shaped like `abs,X` (C10). A lockstep at instruction level sees none of this except through cycle deltas. - `r65c02` drivers that execute `$CB`/`$DB` will wait or stop where this core's `R65C02` row runs a NOP (C5). - An interrupt appears in the trace as a jump with no instruction line of its own. **The line before it is an instruction that did not run**: MAME writes the line when it has fetched the opcode, then enters the interrupt in its place. The replay recognises an entry by S three lower with the line's own PC pushed. - **MAME fetches an opcode at the end of the instruction before**, so with `--bus` the read of an instruction's opcode is logged just before its line, not after it. `tests/mame_replay.py` moves it. - The Lua taps must stay in globals or they are garbage-collected; each `logerror` from Lua is prefixed `[luaengine]` and followed by a blank line. - ROM sets (a merged MAME set) are read in place from the directory the `MAME_ROMPATH` environment variable names (or `--rompath`), passed to MAME as `-rompath`; the script stops when neither is given. Nothing is copied. Traces are never committed.