# MAME as an emulator-derived oracle: TMS34010 traces from Mortal Kombat MAME 0.285's `tms34010` core, running inside the Midway T-unit driver, produces a per-instruction register trace of Mortal Kombat's TMS34010 with the real board around it: the video timing generator, the display interrupt, the 6809 sound board, the palette and DMA hardware. That trace is an **emulator-derived** oracle, tier 3: it detects divergences, and a divergence is decided by the *TMS34010 User's Guide*, never by MAME. For this processor that tier gap matters more than usual, because there is no tier-1 oracle at all ([validation.md](validation.md)). Everything below was run on 2026-09-12 and every output quoted is from those runs. ## Environment | | | | --- | --- | | MAME | `/usr/games/mame`, `-version` prints `0.285 (unknown)` (Debian package) | | ROM sets | `$MAME_ROMPATH/`, used in place with `-rompath`; nothing is copied, and ROMs and traces are gitignored | | Machine | `mk` — Mortal Kombat (rev 5.0 T-Unit 03/19/93), Midway 1992, `midway/midtunit.cpp`. `mame -listdevices mk`: `maincpu Texas Instruments TMS34010 @ 50.00 MHz`, `adpcm:cpu Motorola MC6809E @ 2.00 MHz`. Screen: 400x254, refresh **54.706840 Hz**, pixel clock 8 MHz, htotal 506, vtotal 289 (`mame -listxml mk`). | | Licence of the code cited | BSD-3-Clause (`src/devices/cpu/tms34010/`, `src/mame/midway/midtunit.cpp`) | Other TMS34010 machines whose ROM sets are present here and which were confirmed to start: `smashtv`, `narc`, `totcarn` (all Y-unit), `mk2`, `nbajam` (T-unit), and `yunittst`, the **Y-Unit Test ROM v1.40** — a pure Midway diagnostic program and the most interesting of them ([validation.md](validation.md)). Set `MACHINE=` to trace any of them. ## The critical fact about addresses: they are bit addresses Every address MAME's TMS34010 debugger prints — `PC`, `CURPC`, the disassembly's address column, the operands of `MOVE ...,@1F00000h` — is a **bit** address. Verified three ways on `mk`: 1. MAME's program space for the device reports `data_width = 16`, `shift = 3`, i.e. the address unit is one bit and a 16-bit word occupies 16 consecutive addresses. Read through the Lua `cpu.spaces["program"]` interface, `read_u16(0xFFC94F80)` returns `0x0360`, which is `DINT`, the first instruction executed. 2. Instruction addresses advance in multiples of 0x10. `DINT` at `FFC94F80` is followed by `FFC94F90` (one 16-bit word later); `MOVI 10FFFF0h,SP` at `FFC94FB0` is followed by `FFC94FE0`, 0x30 = three words, which is exactly the long `MOVI` encoding (opcode plus a 32-bit immediate). 3. The `:maincpu` ROM region is 0x100000 **bytes** and occupies the top of the address space, so it begins at bit address `0x100000000 - 0x100000*8 = 0xFF800000`. The reset PC `0xFFC94F80` then maps to region byte offset `(0xFFC94F80 - 0xFF800000) / 8 = 0x929F0`, where the ROM holds `60 03` = `0x0360` = `DINT`. It matches. So: ```text byte address = bit address >> 3 16-bit word index = bit address >> 4 bit address = byte address << 3 ``` **A correction to a note carried in from an earlier session**: the shift is **3 relative to byte addresses**, not 4. Four is the shift relative to *16-bit word* addresses, which is how a byte-oriented emulator author is most likely to be thinking about a ROM image. Get this wrong by one and every address is off by a factor of two. The reset vector confirms the same thing from the other end: `read_u16(0xFFFFFFE0) = 0x4F80` and the region's last four bytes are `80 4F C9 FF`, a little-endian 32-bit `0xFFC94F80` at bit address `0xFFFFFFE0`. Vectors live in the top 32 words of the address space and each is a 32-bit bit-address ([start-here.md](start-here.md#8-interrupts)). ## What MAME exposes The `tms34010` device registers exactly these debugger state names, read by iterating `manager.machine.devices[":maincpu"].state` from Lua: ```text PC GENPC CURPC SP ST CURFLAGS A0 A1 ... A14 B0 B1 ... B14 ``` `PC` and `CURPC` are equal for this device (unlike the segmented 8086, where they differ). `ST` is the 32-bit status register. There is **no state entry for the I/O register file** at 0xC0000000 and no entry for the implicit graphics state; read those out of the program space, or from the host device, if a lockstep needs them. The disassembler uses TI mnemonics: `DINT`, `SETF`, `MOVI`, `MOVE`, `MMTM`/`MMFM` (multiple move to/from memory, the register-list push/pop), `CALLA`/`CALLR`/`RETS`/`RETI`, `JRNE`/`JREQ`/`JRLT`/`JRC` (the PC-relative conditional jumps), `DSJS` (decrement and skip/jump short), `PIXT`, `MOVX`. ## The command line ```text mame mk -rompath "" -homepath \ -cfg_directory /cfg -nvram_directory /nvram \ -video none -sound none -nothrottle -noreadconfig -skip_gameinfo \ -debug -debugger none -log -autoboot_script trace_mk.lua ``` with, in `trace_mk.lua` (the shipped copy is [`scripts/mame/trace_mk.lua`](../scripts/mame/trace_mk.lua); [`scripts/mame/trace_mk.sh`](../scripts/mame/trace_mk.sh) wraps the whole thing): ```lua local dbg = manager.machine.debugger dbg.visible_cpu = manager.machine.devices[":maincpu"] dbg:command('trace trace_disasm.txt,,noloop,{logerror "%08X ST=%08X SP=%08X A0=%08X ...\n",curpc,st,sp,a0,...}') dbg:command("go") ``` Usage: ```text scripts/mame/trace_mk.sh [start-frame] [stop-frame] TRACE_REGS=short scripts/mame/trace_mk.sh 900 902 MACHINE=smashtv scripts/mame/trace_mk.sh 0 2 ``` ## Gotchas, each verified or corrected here - **`error.log` lands in the process's current working directory, not beside `-homepath`.** Confirmed by running with `-homepath ../home` from a different directory: `error.log` appeared in the working directory and `../home/` contained only `cfg/` and `nvram/`. `trace_mk.sh` therefore `cd`s into the output directory before starting MAME. - **`tracelog` is silent.** A run whose only debugger command was `tracelog "%X %X\n",curpc,st` left `error.log` holding two lines: MAME's own `Soft reset` and an unmapped-memory warning. Use `trace FILE,,noloop,{logerror ...}`. - **The Lua `device.debug:bpset` API segfaults MAME 0.285.** Reconfirmed on this CPU: `cpu.debug:bpset(0xFFC94F80)` from an autoboot script exits with signal 11 and a core dump. Set breakpoints with `dbg:command("bp ")` if you need them at all. - **`focus` did *not* break the trace action here — correcting an earlier note.** `dbg:command("focus :maincpu")` before the `trace` command produced a complete 152,161-line trace. It is still the wrong thing to do for a lockstep: `focus` suspends every other CPU, so the 6809 sound board stops answering and the board no longer behaves like the board. Do not use it; the reason is behavioural, not a tooling bug. - `-debug -debugger none` enables the debugger engine with no UI, which is what makes `manager.machine.debugger` available to Lua. `-video none -sound none -nothrottle` run headless at full speed; MAME warns that `-video none` "doesn't make much sense without -seconds_to_run", which the Lua frame-counted exit makes moot. `-noreadconfig` keeps the user's `mame.ini` out of it. `-log` is what opens `error.log`. - The `trace` action is evaluated **before** each instruction, so `error.log` line *n* is the state *entering* the instruction on `trace_disasm.txt` line *n*. `noloop` stops MAME compressing repeated lines, so every iteration of a `DSJS` delay loop is one line. - With a fresh `-nvram_directory` the game fails its CMOS check and stops on a "press any button" screen, so a late `start-frame` lands in a polling loop rather than in game code. Run once to populate NVRAM, then trace. ## Sample output Reset and the first instructions (`error.log` with `TRACE_REGS=short`; the `Soft reset` line is MAME's): ```text Soft reset FFC94F80 ST=00000010 SP=00000000 A0=00000000 A1=00000000 B0=00000000 B1=00000000 FFC94F90 ST=00000010 SP=00000000 A0=00000000 A1=00000000 B0=00000000 B1=00000000 FFC94FA0 ST=00000030 SP=00000000 A0=00000000 A1=00000000 B0=00000000 B1=00000000 FFC94FB0 ST=00000830 SP=00000000 A0=00000000 A1=00000000 B0=00000000 B1=00000000 FFC94FE0 ST=00000830 SP=010FFFF0 A0=00000000 A1=00000000 B0=00000000 B1=00000000 FFC95010 ST=00000830 SP=010FFFF0 A0=0000FFFC A1=00000000 B0=00000000 B1=00000000 ``` and the matching `trace_disasm.txt`: ```text FFC94F80: DINT FFC94F90: SETF 10h,1,0 FFC94FA0: SETF 20h,1,1 FFC94FB0: MOVI 10FFFF0h,SP FFC94FE0: MOVI FFFCh,A0 FFC95010: MOVE A0,@1F00000h,0 ``` Six lines that teach most of the model, and they are worth reading slowly: - **Reset state as MAME models it**: `ST = 0x00000010`, every register and SP zero, PC loaded from the 32-bit vector at bit address 0xFFFFFFE0. `ST = 0x10` means FS0 = 16: field 0 is sixteen bits wide, zero-extended. - `SETF 10h,1,0` sets field **0** to width 0x10 = 16 bits with FE0 = 1 (sign-extend). The next line shows `ST = 0x30`: FS0 = 0x10 in bits 0-4, FE0 = 1 in bit 5. - `SETF 20h,1,1` sets field **1** to width 0x20 = 32 bits, sign-extended. The next line shows `ST = 0x830`: bit 11 is FE1, and **FS1 reads back as 0, not 32** — a 5-bit field cannot hold 32, so *a field size of 32 is encoded as zero*. This is the single most easily-missed rule in the field machinery and it is visible here in the second and third instructions the machine ever executes. - `MOVI 10FFFF0h,SP` loads the stack pointer with a **bit** address: 0x010FFFF0 >> 3 = byte 0x21FFFE. `MOVE A0,@1F00000h,0` writes field 0 (16 bits) to bit address 0x01F00000 = byte 0x3E0000. Steady state, attract mode, frames 900-902 (`TRACE_START_FRAME=900 TRACE_STOP_FRAME=902`): 41,985 instructions in two frames. The opcode mix over that window, from `awk '{print $2}' trace_disasm.txt | sort | uniq -c`: ```text 13749 MOVI 6359 MOVE 6143 RETS 4630 JRNE 4607 CALLR 1565 JREQ 1541 MMFM 1537 CALLA 1535 MMTM 28 CMPI 24 SRL 24 ADD 22 MOVX 22 ADDI 14 CLR 14 BTST 10 INC 6 SETF 5 ORI 4 EINT 4 DINT 4 JR 2 PIXT 2 RETI ``` Two things to take from that histogram. First, real TMS34010 code is overwhelmingly **ordinary**: loads, calls, returns, conditional jumps and the `MMTM`/`MMFM` register-list push/pop. The graphics instructions barely appear in a trace because one `PIXBLT` does the work of thousands of `MOVE`s. A core that gets the boring instructions wrong will never reach the interesting ones. Second, exactly **two `RETI`s in two frames** — one display interrupt per frame, which is the entire interrupt model a Midway host needs ([timing.md](timing.md)). An I/O-register access and a shift-register transfer, from the same window: ```text FFC98D10: MOVE @C0000080h,A0,0 ; read DPYCTL FFC98D40: ORI 800h,A0 ; set bit 11 FFC98D70: MOVE A0,@C0000080h,0 ; write it back FFC98DA0: CLR A1 FFC98DB0: PIXT *A1,A2 ; a pixel read at bit address 0 FFC98DC0: ANDI FFFFF7FFh,A0 ; clear bit 11 FFC98DF0: MOVE A0,@C0000080h,0 ; write it back ``` `0xC0000080` is the ninth I/O register, DPYCTL. The sequence sets one DPYCTL bit, performs a single memory read, and clears it again — the documented way to make an ordinary read into a **VRAM shift-register transfer cycle** rather than a data read. It is a good example of why the video-timing side of the chip cannot simply be ignored by a core: a normal instruction changes meaning because of an I/O register bit. It is also a good example of the tier rule in action, because what that bit is called and what MAME does when it is set are questions for the User's Guide and for `tms34010.cpp`, not for a trace. ## Cost | Run | Instructions | `error.log` | `trace_disasm.txt` | Wall | | --- | --- | --- | --- | --- | | Boot, 2 frames, 33 register fields | 152,161 | 59 MB | 4.2 MB | 2.4 s | | Boot, 2 frames, `TRACE_REGS=short` (7 fields) | 152,161 | 12 MB | 4.2 MB | 2.2 s | | Attract, frames 900-902, 33 fields | 41,983 | 17 MB | 1.2 MB | 1.8 s | About 76,000 instructions per 54.7 Hz frame at boot, so roughly **4.2 million instructions per emulated second**, or 1.6 GB of full-width `error.log` per emulated second. Trace windows, never whole sessions. Traces live outside the repository; `traces/`, `*.log` and `trace_disasm.txt` are gitignored. ## Using the trace in lockstep Build a host with the `midtunit` memory map, step the core once per `error.log` line, and compare the 33 register fields. The first line that differs is the divergence; the instruction on the same line of `trace_disasm.txt` names the suspect. Three rules that follow from the tier: 1. **A divergence is not a core bug until the User's Guide says so.** MAME's TMS34010 core carries acknowledged guesses and unimplemented corners ([undocumented-behavior.md](undocumented-behavior.md)); any divergence in one of those areas is evidence about MAME, not about the core. 2. **Do not compare cycle counts.** MAME's TMS34010 timing is not the User's Guide's table and does not try to be ([timing.md](timing.md)). 3. **Interrupt entry will diverge before anything else does.** The display interrupt fires at a scanline the host programs; unless the host model is scanline-accurate the trace and the core will take it at different instructions. Trace windows that contain no `RETI` avoid the question; a window that does contain one is a test of the host, not of the core.