# MAME 0.285 as an emulator-derived 6809 oracle MAME's `mc6809e` core is the most exercised 6809 model in existence: every Williams, Konami, Namco and Data East 6809 board in MAME runs on it. It is an **emulator-derived** oracle (its instruction list is Nathan Woods's 2013 rewrite, with the 2023 undocumented-opcode additions transcribed from hoglet67's hardware work), so under the tier rule it is a **detector, never a judge**: a disagreement between this core and MAME is a lead, resolved by the datasheet, by a hardware-captured source, or by a second independent emulator, not by MAME's say-so. This page records how to get instruction traces out of it, verified on 2026-09-11/12 with the runs described. ## What is installed - MAME **0.285** at `/usr/games/mame` (`mame -version` prints `0.285 (unknown)`). - ROM sets under `$MAME_ROMPATH/`, used in place with `-rompath`. `robotron.zip`, `joust.zip`, `defender.zip`, `sinistar.zip`, `gyruss.zip` and `timeplt.zip` are all present. Nothing is copied, and no ROM or trace is committed to this repository (`.gitignore` excludes `*.trace`, `error.log` and `mame-home/`). - `robotron` and `joust` both run a **Motorola MC6809E at 1,000,000 Hz** as `:maincpu` (`mame -listxml robotron`: ``), sourced from `midway/williams.cpp`. ## Register names the core exposes From Lua, `manager.machine.devices[":maincpu"].state` has exactly these entries (printed by `scripts/mame_trace.lua`): ```text A B CC CURFLAGS CURPC D DP PC S U X Y ``` `D` is a view of A:B; `CURPC` and `CURFLAGS` are the debugger's generic "current instruction" aliases (`m_ppc` and a formatted CC). There is **no state entry for the E-flag separately, nor for the NMI/FIRQ/IRQ line levels or the `lds_encountered` latch**; those must be inferred from the trace (`cc` bit 7 for E; a jump to a vector address for an interrupt). In debugger expressions the same names are used in lower case: `pc`, `a`, `b`, `x`, `y`, `u`, `s`, `dp`, `cc`, plus the generic symbols `totalcycles`, `frame`, `beamx`, `beamy`. ## The working command line ```text /usr/games/mame robotron \ -rompath "$MAME_ROMPATH" \ -homepath "$PWD/mame-home" \ -video none -sound none -nothrottle -noreadconfig -skip_gameinfo \ -debug -debugger none -log \ -seconds_to_run 20 \ -autoboot_script "$PWD/scripts/mame_trace.lua" ``` `scripts/mame_trace.sh` wraps exactly that. Facts established by running it: - It runs headless. `-debug -debugger none` enables the debugger engine (needed for `trace`) without a UI. `-seconds_to_run N` exits cleanly after N emulated seconds; with `-nothrottle` and the trace active MAME reported 73-97 % of real time, so 20 emulated seconds cost about 27 s of wall time and produce a 5.49-million-line, 274 MB `error.log` (twelve columns) plus a 97 MB disassembly `.trace`. MAME also creates `cfg/`, `nvram/` and `snap/` in the work directory; the wrapper keeps all of it out of the repository. - **`-log` writes `error.log` into the current working directory, not into `-homepath`.** (The note from the earlier session that it lands under `-homepath` was wrong; `-homepath` is where the Lua script's relative paths and the `trace` file's relative path resolve *only if* MAME is started from there, so the wrapper `cd`s into a work directory first.) - The `trace FILE,,noloop,{...}` debugger command writes MAME's own disassembly listing to `FILE` (one line per instruction, `F433: LDS #$BF70`), **and** executes the `{logerror ...}` action once per instruction, whose output goes to `error.log`. Two files, two formats, from one command. `tracelog` on its own produces nothing usable here, as the earlier session found; `focus` and the Lua `device.debug:bpset` API were not retried (reported to break actions and to segfault respectively). - `logerror "%X ..."` prints the values **before** the instruction at `pc` executes, one line per instruction, hex without leading zeros, so `F000 0 0 0 0 0 0 0 50` is PC=$F000 with CC=$50 (I and F set, as reset leaves them) before the first instruction. - The first line of `error.log` is `Soft reset`, and PIA warnings (`[:pia_1] Warning! No CA1 read handler ...`) are interleaved: filter to lines matching the register-field pattern before comparing. - Interrupt acceptance produces **no line of its own**. The trace shows the interrupted instruction, then the first instruction of the handler at the vector address; the `totalcycles` column jumps by the entry cost plus the interrupted instruction's cost, and `s` drops by 12 (IRQ/NMI) or 3 (FIRQ). ## The Lua script `scripts/mame_trace.lua` (kept in the repository, MIT): ```lua local cpu = manager.machine.devices[":maincpu"] -- print the state names and the vector table, then trace local dbg = manager.machine.debugger dbg.visible_cpu = cpu dbg:command('trace robotron.trace,,noloop,{logerror "%X %X %X %X %X %X %X %X %X %d %d %d\n",pc,a,b,x,y,u,s,dp,cc,totalcycles,frame,beamy}') dbg:command("go") ``` plus the vector-table dump (`cpu.spaces["program"]:read_u16(0xFFF8)`) and the Advance-button handling described next. ## The Williams boot trap and the fix Both `robotron` and `joust` come up with an unwritten CMOS. After about 10.7 s of RAM test (measured: the loop was entered around 11.4 s into the run) the ROM spins on ```text 74EB: LDA #$39 / STA $CBFF (watchdog) / LDA $C80C / BITA #$02 / BEQ $74EB ``` reading PIA1 port A bit 1, the **Advance** button, with IRQs masked (`ORCC #$FF` at `$F431` on reset and I never cleared). A 15 s trace therefore contains **zero** interrupts. The script presses Advance for ten frames the first time it sees PC inside `$74EB..$74F7` (`cpu.state["PC"].value` from an `emu.register_frame_done` callback, `ioport.ports[":IN2"].fields["Advance"]:set_value(1)`), after which the game enters attract mode with interrupts enabled. Joust's equivalent loop is at `$3D69..$3D73`, same PIA bit; its IRQ vector is `$FF18`. An alternative that needs no scripting is to let one run write the NVRAM (`-nvram_directory`) and reuse it; that was not tried. ## The verified run Robotron, 20 emulated seconds, Advance pressed at frame 685 and released at 695 (reproduced twice on 2026-09-12, the second time through `scripts/mame_trace.sh robotron 20` exactly as committed: 5,486,883 instruction lines, 2,069 IRQ entries): ```text STATE NAMES: A B CC CURFLAGS CURPC D DP PC S U X Y VECTORS: FFF0=F000 FFF2=F000 FFF4=F000 FFF6=F000 FFF8=DC56 FFFA=F000 FFFC=F000 FFFE=F000 5,489,488 lines in error.log; 2,069 entries at the IRQ vector $DC56 ``` A sample around the first accepted IRQ (columns: pc a b x y u s dp cc totalcycles frame beamy): ```text D186 2C 0 A9E0 CD68 9810 BF70 98 D9 11398688 684 251 DC56 2C 0 A9E0 CD68 9810 BF64 98 90 11398710 684 251 DC59 2C 0 A9E0 CD68 9810 BF64 98 90 11398715 684 251 ``` Reading it: the 3-cycle instruction at `$D186` ran, then IRQ entry took 19 cycles (22 total), S dropped from `$BF70` to `$BF64` (12 bytes), and CC went from `$D9` to `$90`: E set, I set, F kept, the low nibble preserved. The first two handler instructions cost 5 cycles. The scanlines at which the 2,069 IRQs were accepted: ```text 517 at line 64 515 at line 128 515 at line 192 515 at line 0 2 at 65, 129, 1 (late acceptance after a long instruction) 1 at 251 (the first, mid-frame) ``` and 516 whole frames contained exactly 4. That is the VA11 model in `williams_m.cpp` (`va11_callback`: PIA1 CB1 = bit 5 of the scanline, toggling every 32 lines and not firing at line 256) with the PIA programmed for the **falling** edge, i.e. lines 64, 128, 192 and 0. See [timing](timing.md). The 2-second run used at first (before the Advance fix) is still a valid sample of straight-line code: 552,507 instructions of reset, RAM test and watchdog service, with no interrupts. ## Using it as an oracle The intended comparison is **not** a lockstep of the whole machine (that would need Williams's blitter, PIAs and video counters in Python) but: 1. **Boot-segment replay.** Take the ROM image from the zip in place, load it at the addresses `williams.cpp` maps (`$D000-$FFFF` ROM, `$0000-$BFFF` RAM, `$C000-$CFFF` I/O; the bank at `$C000` after `$D000` writes is not needed for the first two seconds of straight-line code), reset, and run the same number of instructions with I/O reads returning what MAME's trace implies. Compare `pc a b x y u s dp cc` per line. The first 552,507 lines before any I/O dependency are a deterministic instruction-semantics check on real arcade code. Where a line reads a PIA or the video counter the values diverge by design; the comparison tool must know the I/O map and stop, or feed the register value MAME saw. 2. **Cycle totals.** `totalcycles` deltas give MAME's per-instruction cost for every instruction in the trace, including indexed modes and the 19- and 10-cycle interrupt entries, which is a cheap cross-check of the timing table over millions of real instructions. 3. **Interrupt entry.** The lines at `$DC56` verify the stacked frame size, the E/I bits, and the scanline model, once the host implements the VA11 timer. The trace files stay outside the repository: they are about 14 MB per emulated second and are derived from a copyrighted ROM. ## Limits - MAME charges one cycle per bus access and models dummy reads at `$FFFF` (`dummy_vma`) and re-reads of the instruction stream (`dummy_read_opcode_arg`); its counts agree with the datasheet table where checked ([start-here](start-here.md)), but it is a model, not a measurement. - The trace has no bus-level detail: which addresses the dummy cycles touch is not in it. The `neetandev/m6809` corpus, generated from the same core, does record every bus cycle ([validation](validation.md)). - `SYNC`'s cycle count and the undefined indexed postbytes are MAME choices ([undocumented-behavior](undocumented-behavior.md)).