# Timing: what `step()` will return, and what it will not claim ## The claim a first core can make `step()` will return an **instruction cycle count in TMS34010 machine states**, from the *TMS34010 User's Guide*'s instruction tables, for the non-graphics instruction set. It will not claim: - cycle-exact memory timing (wait states, page hits, unaligned-field penalties), - DRAM refresh cycles, - the video timing generator's cycle stealing, - meaningful timing for the graphics instructions, - anything at all for the TMS34020. Each exclusion has a reason, and the reasons are below rather than in a footnote, because on this processor the exclusions are larger than on any other core in this set. ## The clock, and what a "cycle" is The TMS34010 divides its input clock internally. MAME divides by **8** (`tms34010.h:970-971`); the TMS34020 divides by **4** (`:1010-1011`). So a Midway T-unit's 50 MHz part executes 6.25 million machine states per second in MAME's accounting. Measured against that, on `mk` (Mortal Kombat, T-unit, 50 MHz, screen refresh 54.706840 Hz) a two-frame trace from reset holds **152,161 instructions**, i.e. about 76,000 instructions per frame or **4.16 million instructions per second** — roughly 1.5 machine states per instruction averaged over a window dominated by a `DSJS` delay loop. That number is here as a scale, not as a timing claim: it is MAME's opinion divided by MAME's divider. ## The User's Guide's tables, and why they are hard TI publishes per-instruction timings in the *TMS34010 User's Guide*'s instruction-set section, one entry per instruction, in machine states. Four things make them hard to turn into a table lookup: 1. **They are memory-cycle dependent.** Most entries are quoted as a base plus the cost of the memory cycles the instruction performs, and a local-memory cycle's cost depends on the board: wait states, page-mode hits, and whether the access is aligned. An instruction that reads a 32-bit field spanning three words costs more than the same instruction reading an aligned one. The instruction table alone does not determine the time. 2. **DRAM refresh steals cycles.** The chip is also a DRAM controller. The refresh rate is programmable and refresh cycles are taken from the instruction stream. MAME does not model this at all: `REFCNT` is synthesized on read as `(total_cycles() / 16) & 0xfffc` (`tms34010.cpp:1570-1571`) and no cycles are stolen. 3. **The video timing generator steals cycles.** Screen-refresh cycles and VRAM shift-register transfers contend with the CPU for the memory bus. MAME models neither: `read_pixel_shiftreg`/`write_pixel_shiftreg` call straight through to the driver's callback and charge nothing (`tms34010.cpp:409-417`, `:552-558`). 4. **The graphics-instruction timing model in the manual is, by MAME's account, unreliable.** The block comment at `34010gfx.hxx:147-165` says the manual's model has "enough typos and misleading statements to make the reliability of the timing info questionable", and MAME therefore uses a deliberately simplified approximation: ```text timing = setup + (srcwords * 2 + dstwords * gfxop) * rows ``` That is an emulator author's admission that the documentation is not usable as written. It is the strongest argument in this repository for treating graphics-instruction timing as out of scope and saying so plainly. ## What MAME models, and what it does not MAME's cycle accounting is `m_icount -= x` per instruction (`34010ops.hxx:25`), with a second macro `COUNT_UNKNOWN_CYCLES(x)` defined as *exactly the same thing* (`:26`) whose only purpose is to mark counts the authors invented. Its call sites are a short and honest list of what is guessed: - the illegal-opcode trap, 16 cycles (`34010ops.hxx:99`); - the generic 2-, 4-, 4-, 7- and 4-cycle macros covering the shift, rotate and field-move families (`:217`, `:237`, `:256`, `:287`, `:309`); - `LINE`, 2 cycles per pixel plotted (`34010gfx.hxx:58`). Beyond those, `CVXYL` is charged a flat 3 cycles with the comment "TODO: Handle cycles properly, currently we assume power of two" (`34010ops.hxx:183`), and the TMS34020's `CLIP` is charged a flat 3 with "manual does not specify cycles, only states that this is complex instruction" (`:2123`, `:2159`). MAME does not model at all: DRAM refresh, shift-register transfer cost, wait states, unaligned-field penalties, or the bus contention between the CPU and the display. **Therefore a MAME trace is not a timing oracle for this CPU, and [mame-oracle.md](mame-oracle.md) says not to compare cycle counts.** For the Z80 and 8086 cores in this set that rule was a caution; here it is structural. ## The interruptible blit One timing behaviour is not optional, because it is architecturally visible. A `PIXBLT` can take thousands of cycles, and hardware can accept an interrupt in the middle of one and resume it afterwards: ST bit 25, the **P** flag, marks a blit in progress, and the interrupt handler must preserve enough state for the resume. MAME does not reproduce the hardware's behaviour here. It executes the whole transfer atomically on first entry, guarded by the P flag, then "eats" the computed cycle cost afterwards; if the cost exceeds the remaining timeslice it backs the PC up by 0x10 so the instruction re-enters with P still set, and no work is repeated (`34010gfx.hxx:1276-1299`). The consequences are observable: memory reaches its final state *before* any of the charged cycles elapse, and the B-file registers SADDR and DADDR are updated only at completion (`:1287-1298`). `LINE` does the same thing one pixel at a time (`:59`). A first core should do what MAME does — execute the blit atomically, set and clear P correctly, charge an approximate cost — and **state that it does**, because a program that samples DADDR from an interrupt handler during a blit will see something different on hardware. ## What an arcade host actually needs Almost none of the above. A Midway T-unit host needs three things, and they were verified here. **The CPU clock.** `midtunit.cpp:605`: `constexpr XTAL CPU_CLOCK = 50_MHz_XTAL`. The pixel clock is `8_MHz_XTAL/2` = 4 MHz with two pixels per clock (`:606`, `:614`). The screen, from `mame -listxml mk`: 400×254 visible, htotal 506, vtotal 289, **refresh 54.706840 Hz**. The other Midway families: Wolf-unit 50 MHz with an 8 MHz pixel clock (`midwunit.cpp:625-634`); Y-unit 40, 48 or 50 MHz depending on the game (`midyunit.cpp:1238-1240`, `:1357`, `:1383`, `:1393`); X-unit a TMS34020 at 40 MHz (`midxunit.cpp:650-660`). **One display interrupt per frame.** The display interrupt is *internal to the TMS34010*: the scanline update compares VCOUNT with DPYINT and raises DI when they match (`tms34010.cpp:1021-1026`). The host does not wire it; the host only has to call the core once per scanline with the current VCOUNT, or once per frame at the programmed line. Verified empirically: a two-frame trace of Mortal Kombat in attract mode contains **exactly two `RETI`s** ([mame-oracle.md](mame-oracle.md#sample-output)). **One external interrupt line, and it is not the video.** The only `set_inputline` in the whole T-unit driver is `m_video->dma_irq_cb().set_inputline(m_maincpu, 0)` (`midtunit.cpp:609`) — the *custom DMA blitter chip* signalling completion on INT1. That is worth stating twice, because it is the most surprising thing about these boards: > **Midway's Y-, T- and Wolf-unit boards do not use the TMS34010's own > graphics instructions for sprites.** They have a separate DMA blitter > device (`midtunit_video_device`, `midyunit`'s `dma_draw`) with its own > register file, its own IRQ, and its own timer. The TMS34010 writes its > registers and waits. Measured, not inferred: over a two-frame attract-mode window Mortal Kombat executes **two** graphics instructions (both `PIXT`, both part of a shift-register transfer) out of 41,983 instructions. Exterminator, Lethal Justice, Ultimate Tennis and Cool Pool execute **zero** graphics instructions in a comparable window. Only `xtheball` (Rare, *X the Ball*) and `tickee` used them at all in this survey — 2,739 and 73 executions respectively in two frames ([validation.md](validation.md#which-games-actually-exercise-the-graphics-instructions)). So the timing a host needs is: a CPU clock, a per-scanline or per-frame VCOUNT tick so DPYINT can fire, and an INT1 line the board's blitter pulls. Everything else in this document is about a core's claims, not about making a game run. ## The TMS34020 is a different clock table Different divider (4 rather than 8), different bus width (32 rather than 16, so long immediates and field accesses cost differently), different instructions. MAME implements 39 of its 53 new opcodes as do-nothing `logerror` stubs ([start-here.md](start-here.md#10-the-tms34020-in-one-section)), so it has no timing for them either. A TMS34020 timing claim would rest on the *TMS34020 User's Guide* alone and is out of scope.