# Timing: what `step()` will return, and what it will not claim ## The claim a first core can make `step()` returns the **documented instruction clock total** for the instruction it ran: the "Clocks" column of Intel's table 2-21 plus the effective-address time of table 2-20, with the taken/not-taken split for conditional transfers and the per-repetition figure for repeated strings. That is the same shape of claim z80-python makes with T-states. It is *not* a claim of cycle-exact bus behaviour, and this page says why not and what the arcade hosts can do with an instruction-total core anyway. ## Intel's tables, restated Source: *The 8086 Family User's Manual* (Oct 1979), table 2-20 "Effective Address Calculation Time" (p. 2-51) and table 2-21 "Instruction Set Reference Data" (p. 2-51 to 2-67); interrupt costs from table 4-6 (p. 4-18). Values below were read from the PDF's text layer. The tables apply to the 8086 and 8088 alike with two footnotes: **on the 8086 add 4 clocks for each 16-bit word transfer at an odd address; on the 8088 add 4 clocks for every 16-bit word transfer** (table 2-21 footnote, every page). Intel also says a sequence typically runs within 5-10 % of the sum of the individual figures and that exact times must be measured on hardware (p. 2-51); the hardware-captured corpora are that measurement. ### Effective address time (table 2-20) | EA components | Clocks | | --- | ---: | | Displacement only | 6 | | Base or index only (BX, BP, SI, DI) | 5 | | Displacement + base or index | 9 | | Base + index: BP+DI, BX+SI | 7 | | Base + index: BP+SI, BX+DI | 8 | | Displacement + base + index: BP+DI+disp, BX+SI+disp | 11 | | Displacement + base + index: BP+SI+disp, BX+DI+disp | 12 | | Segment override prefix | +2 | ### Instruction clocks (table 2-21, selected) `EA` means add the figure above. Forms are (register, register) etc. as in the table; "mem" is the r/m memory form. | Instruction | Clocks | | --- | --- | | MOV r,r 2; r,mem 8+EA; mem,r 9+EA; r,imm 4; mem,imm 10+EA; AL/AX,[addr] 10; [addr],AL/AX 10; sreg,r 2; sreg,mem 8+EA; r,sreg 2; mem,sreg 9+EA | | | PUSH r 11; PUSH sreg 10; PUSH mem 16+EA; POP r 8; POP sreg 8; POP mem 17+EA; PUSHF 10; POPF 8 | | | XCHG AX,r 3; XCHG r,r 4; XCHG mem,r 17+EA; XLAT 11; LEA 2+EA; LDS/LES 16+EA; LAHF 4; SAHF 4 | | | IN AL/AX,imm8 10; IN AL/AX,DX 8; OUT imm8 10; OUT DX 8 | | | ADD ADC SUB SBB AND OR XOR: r,r 3; r,mem 9+EA; mem,r 16+EA; r,imm 4; mem,imm 17+EA; AL/AX,imm 4 | | | CMP: r,r 3; r,mem 9+EA; mem,r 9+EA; r,imm 4; mem,imm 10+EA; AL/AX,imm 4. TEST: r,r 3; r,mem 9+EA; r,imm 5; mem,imm 11+EA; AL/AX,imm 4 | | | INC/DEC r16 2; r8 3; mem 15+EA. NEG/NOT r 3; mem 16+EA | | | AAA 4; AAS 4; DAA 4; DAS 4; AAM 83; AAD 60; CBW 2; CWD 5 | | | MUL r8 70-77; r16 118-133; mem8 (76-83)+EA; mem16 (124-139)+EA | | | IMUL r8 80-98; r16 128-154; mem8 (86-104)+EA; mem16 (134-160)+EA | | | DIV r8 80-90; r16 144-162; mem8 (86-96)+EA; mem16 (150-168)+EA | | | IDIV r8 101-112; r16 165-184; mem8 (107-118)+EA; mem16 (171-190)+EA | | | Shifts and rotates (SHL SHR SAR ROL ROR RCL RCR): r,1 2; mem,1 15+EA; r,CL 8+4/bit; mem,CL 20+EA+4/bit | | | JMP short/near/far (direct) 15; JMP r16 11; JMP mem16 18+EA; JMP mem32 24+EA | | | Jcc 16 taken / 4 not; JCXZ 18/6; LOOP 17/5; LOOPE 18/6; LOOPNE 19/5 | | | CALL near 19; CALL far 28; CALL r16 16; CALL mem16 21+EA; CALL mem32 37+EA | | | RET near 8; RET near imm 12; RET far 18; RET far imm 17 (1979 table; later Intel editions print 16/20/26/25, so treat this row as documentation to be settled by the corpus) | | | INT n 51; INT 3 52; INTO 53 taken / 4 not; IRET 24 (table 2-21; table 4-6 agrees on INT/INTO); INTR acknowledge 61; NMI 50; single step 50 | | | MOVS 18, REP 9+17/rep; CMPS 22, 9+22/rep; SCAS 15, 9+15/rep; LODS 12, 9+13/rep; STOS 11, 9+10/rep; REP prefix itself 2 | | | CLC STC CMC CLD STD CLI STI 2; HLT 2; NOP 3; WAIT 3+5n; LOCK 2; segment override 2; ESC r 2, ESC mem 8+EA | | What those numbers mean: they are the execution unit's clocks assuming the instruction bytes are already in the prefetch queue and no wait states (NEC states the same assumption for its section 12 figures, p. 12-1). ## Why cycle exactness is out of scope for a first core The 8086 is two units. The **execution unit** runs microcode against the registers; the **bus interface unit** runs bus cycles (four clocks each, T1-T4, plus wait states) to fetch instruction bytes into a queue ahead of need and to move operands (Intel p. 2-5; NEC p. 3-1, 3-4). The two overlap. The clock an instruction actually takes therefore depends on: - whether its bytes were already in the queue (the queue is 6 bytes on the 8086/V30, 4 on the 8088/V20, refilled a word at a time on the 16-bit parts, a byte at a time on the 8-bit ones: NEC p. 3-4); - whether the previous instruction emptied the queue (every jump, call, return and interrupt flushes it: NEC p. 3-4); - whether a fetch was in flight when the execution unit asked for the bus for an operand; - alignment: a word operand at an odd address is two bus cycles on the 8086 (the +4 footnote), and every word is two bus cycles on the 8088; - wait states inserted by the memory system. The SingleStepTests corpora record all of it: each case's `cycles` array is one entry per clock with the bus status, T-state, queue operation and the byte read from the queue, and the README explains that an instruction's cycle count "begins from the cycle in which the CPU's queue status lines indicate that an instruction First Byte has been fetched" and ends when the *next* instruction's first byte is read from the queue, so an instruction that ends with an empty queue is longer than its table figure. Half of the 8088 and V20 cases start with a full queue and half with an empty one precisely to expose this. Modelling that is a BIU emulator: a queue, a fetch scheduler, bus-cycle state, and per-instruction microcode timing for when the EU requests the bus. That is a legitimate later milestone (the corpus makes it testable), but it is not what an instruction core is for, and a core that returns table clocks is what z80-python and 6502-python return too. The trade is stated: **`len(cycles)` from the corpus will not equal `step()`'s return value, and the vector gate must not compare them.** What the gate *can* compare against the `cycles` array is the sequence of memory and I/O accesses (address, direction, value, order), because those are bus cycles the EU asked for, not prefetch traffic; see [validation.md](validation.md). ## What arcade hosts need An arcade board driver schedules its video timer, sound CPU, and interrupt controller against the main CPU's clock. Instruction totals are enough for that as long as the host does the bookkeeping per scanline, which is how MAME's drivers work too (MAME's V30 core returns per-instruction clock counts it calls "99% accurate", `src/devices/cpu/nec/nec.cpp` header, and `m72.cpp` drives everything from a scanline timer). **Irem M72, R-Type** (MAME 0.285 `src/mame/irem/m72.cpp`, restated): - Master crystal 32 MHz. The V30 runs at `MASTER_CLOCK/2/2` = 8 MHz ("16 MHz external freq (8MHz internal)"). The Z80 sound CPU at 3.579545 MHz. - The pixel clock is `MASTER_CLOCK/4` = 8 MHz, the same as the CPU clock, with a 512 × 284 total raster (visible 384 × 256, columns 64-447, rows 0-255): **one scanline is 512 V30 clocks, one frame is 145,408 clocks, 55.02 Hz.** - The interrupt controller is a µPD71059C (8259-compatible) at I/O ports 0x40-0x43 (byte lanes only, `umask16(0x00ff)`), its INT output wired to the V30's INTR and its `inta_cb` supplying the vector. R-Type initialises it with 0x17 to port 0x40 and 0x20, 0x0F to port 0x42 (ICW1, ICW2 = vector base 0x20, ICW4); the MAME trace in [mame-oracle.md](mame-oracle.md) shows exactly those three OUTs at boot. - IR0 is asserted at scanline 256 (vertical blank, vector 0x20); IR2 at the scanline the game writes to the raster-position register (I/O ports 0x06-0x07, `irq_line_w`; vector 0x22); IR1 is the sprite-DMA-done line, which MAME does not model because every game's handler is empty; IR3/IR4 come from the external connectors and are unused. - Memory map for `rtype`: ROM 0x00000-0x3FFFF, work RAM 0x40000-0x43FFF, sprite RAM 0xC0000-0xC03FF, palettes 0xC8000-0xC8BFF and 0xCC000-0xCCBFF, video RAM 0xD0000-0xD3FFF and 0xD8000-0xDBFFF, a window onto the sound CPU's RAM at 0xE0000-0xEFFFF, and the reset vector ROM at 0xFFFF0-0xFFFFF. Ports: 0x00-0x05 inputs and DIP switches, 0x00 sound latch, 0x02 coin/sound-reset, 0x04 DMA trigger, 0x06 raster IRQ line, 0x80-0x85 scroll registers. A host for this board needs from the core: a `step()` that returns clocks so it can count 512 per line; an INTR request with a vector byte fetched from the host (the 8259 model) when IF is set; and the MOV-SS shadow, because the game's interrupt handler is entered from ordinary code. It does **not** need bus-cycle timing: nothing on the M72 is contended. **Other boards** from the same MAME listing: Raiden (`seibu/raiden.cpp`) runs two V30s at 10 MHz with a shared-RAM protocol between them; Raiden II a V30 at 16 MHz; Tatsumi's TX-1 two Intel 8086s at 5 MHz. All are instruction-total problems for the host. ## The V30 is a different clock table The NEC parts execute the same instructions in fewer clocks: a hardware multiplier (MULU reg8 21-22, reg16 29-30 clocks against 70-77 and 118-133), an effective-address generator that takes 2 clocks for every mode instead of 5-12, and a loop counter that makes counted shifts 7+n instead of 8+4n (NEC p. 3-1 to 3-3, 12-50 to 12-51). Word operands at odd addresses still cost extra on the V30 (MULU mem16: 39-40 odd, 35-36 even, p. 12-51). So the V30 milestone needs its own table, sourced from NEC section 12 (one form per page, "Clocks:" line) and cross-checked against MAME's `necinstr.hxx` `CLKS(v20, v30, v33)` macros, which is where MAME keeps the same numbers. An 8086-timed core driving an M72 host would run its game logic up to three times too slowly on multiply-heavy code; it would still be *correct*, which is why the 8086 core comes first.