# Timing and the bus: machine cycles, wait states, HOLD, the multiplexed 8-bit bus The TMS9900 is microprogrammed: each microinstruction is one **machine cycle**, and TI published the sequence of machine cycles for every instruction with the contents of the address and data buses in each [FSD pp. 4-89..4-110]. That table is the core's bus contract. Tags as in [cpu.md](cpu.md); all DOC unless marked. ## Three kinds of machine cycle [FSD 4-89, 4-40] | Cycle | Clocks | Bus | | --- | --- | --- | | ALU | 2 | no memory or CRU strobe; the address bus holds its previous value ("NC") or a new one the next cycle will use; the data bus shows the internal SD register (the last value written or the ALU's output) | | Memory read / write | 2 + W | MEMEN low, address valid; DBIN high on a read, WE low on a write; READY sampled on φ1 of the second clock, and each clock it is low adds a wait state (WAIT goes high) [DM 2.9.1] | | CRU in / out | 2 | CRU bit address on A3-A14; CRUOUT set up with the address and CRUCLK pulsed in the second clock (out), or CRUIN sampled in the second clock (in) | So an instruction's time is T = tc(φ) × (C + W × M) with C its clocks and M its memory accesses [DM 3.6], and the sequence says *where* in the instruction each access falls, which a board with wait states, a VDP or a GROM needs to know. Limits [FSD table 4-1]: at most 3 consecutive memory reads, 1 consecutive write, 1-51 consecutive ALU cycles, 1-16 consecutive CRU cycles. HOLD is honoured only at a non-memory cycle, so HOLD latency is bounded by the longest run of memory cycles [DM 2.9.2, FSD 4-40]. After a HOLD during a CRU operation the CPU spends one extra clock to reassert the CRU address [DM 2.9.2]. ## Data derivation sequences (one per addressing mode) [FSD 4-91..4-92] | Mode | Cycles (type: address) | Clocks, accesses added (Table A word / Table B byte) | | --- | --- | --- | | Rn | read WP+2n | 0, 0 (the read is counted in the instruction's base figure) | | *Rn | read WP+2n; ALU; read (Rn) | +4, +1 | | *Rn+ byte | read WP+2n; ALU; write WP+2n ← Rn+1; read (Rn) | +6, +2 | | *Rn+ word | read WP+2n; ALU; ALU; write WP+2n ← Rn+2; read (Rn) | +8, +2 | | @label | ALU; ALU; read PC+2 → label; ALU (DB = 0000); read label | +8, +1 | | @table(Rn) | read WP+2n; ALU; read PC+2 → base; ALU; read base + Rn | +8, +2 | The address-modification tables in [DM] (A for word operands, B for byte operands) agree with these sequences: byte auto-increment is one ALU cycle shorter. ## Instruction sequences (summary; the full list is in [FSD] and becomes `timing.py` data) Base figures with all operands in register mode, from [DM] table 3, checked against a count of [FSD]'s cycles (each ALU or memory cycle = 2 clocks with no waits): | Instruction | [DM] clocks / accesses | [FSD] cycle count | Agree | | --- | --- | --- | --- | | A, AB, S, SB, SOC(B), SZC(B), MOV(B) | 14 / 4 | 5 + Ns + Nd = 7 | yes | | C, CB, COC, CZC | 14 / 3 | 7, last cycle ALU with AB = DA | yes | | XOR | 14 / 4 | 7 | yes | | CLR, SETO, INV, NEG, INC(T), DEC(T), SWPB | 10 / 3 (NEG 12 / 3) | 4 + Ns = 5 | yes (NEG: see C4) | | ABS | 12 / 2 (positive), 14 / 3 (negative) | 6 / 7 | yes | | B | 8 / 2 | 3 + Ns = 4 (operand fetched, unused) | yes | | BL | 12 / 3 | 5 + Ns = 6 | yes | | BLWP | 26 / 6 | 12 + Ns = 13 | yes | | XOP | 36 / 8 | 17 + Ns = 18 | yes | | X | 8 / 2 + the executed instruction - 4 clocks and 1 access | 3 + Ns | yes | | MPY | 52 / 5 | the printed page (4-93, image checked) numbers 7 + Ns "16 ALU" then 24 + Ns, skipping 23: read as 16 ALU cycles, 25 + Ns = 26 cycles | yes, by that reading (C1) | | DIV | 16 / 3 overflow; 92-124 / 6 | overflow exits after 6 + Ns = 7 cycles (14 clocks); 13 + Ns + Ni with 32 ≤ Ni ≤ 48 | overflow: **no** (C2) | | LI | 12 / 3 | 6 | yes | | AI, ANDI, ORI | 14 / 4 | 7 | yes | | CI | 14 / 3 | 7 | yes | | LWPI | 10 / 2 | 5 | yes | | LIMI | 16 / 2 | 7 (14 clocks) | **no** (C3) | | STWP, STST | 8 / 2 | 4 | yes | | RTWP | 14 / 4 | 7 | yes | | Jump taken / not taken | 10 / 1, 8 / 1 | 5 / 4 | yes | | SBO, SBZ, TB | 12 / 2 | 6 (last is the CRU cycle) | yes | | LDCR | 20 + 2C / 3 (C = 0: 52) | 9 + Ns + C | yes | | STCR | 42 (C 1-7), 44 (C = 8), 58 (C 9-15), 60 (C = 0) / 4 | 12 + Ns + C + C' with C' = 8 - C - 1 (C ≤ 8) or 16 - C: 40, 40, 58, 58 | **no** for C ≤ 8 and C = 0 (C5) | | Shifts | 12 + 2C / 3; count from R0: 20 + 2N / 4; R0 = 0: 52 / 4 | 6 + C; 10 + N | yes | | CKON, CKOF, LREX, RSET, IDLE | 12 / 1 | 6 (one CRU cycle with the code on A0-A2) | yes | | Interrupt context switch | 22 / 5 | 11 | yes | | RESET, LOAD | 26 / 5, 22 / 5 | 13, 11 | yes | | Undefined opcode | 6 / 1 | not listed | (U1) | The disagreements are the brief's C1-C5. They matter: on the 99/4A nearly every access has four wait states, so a one-cycle difference in *where* a memory cycle falls moves the VDP's and the GROM's view of time. The specification follows [FSD]'s sequence (it is the more detailed source and both are TI's), keeps [DM]'s figure beside it, and marks each disagreement until a test program or measurement settles it. ## Wait states and READY The core does not invent wait states: the board answers each memory cycle with the number of clocks READY stayed low, and the core adds them to that cycle. ALU and CRU cycles never wait (CRU has no READY; the 99/4A's CRU timing is fixed at 2 clocks per bit, [CTD] A.6 "CRU timing"). ## The TI-99/4A's 16-to-8-bit multiplexer (board side, recorded here for the contract) From [CTD] A.6 and figure A (DOC; the schematic pages are scans to be read again for the gate level): - Only the 8 KiB console ROM (0000-1FFF, 4K × 16) and the 256-byte scratchpad RAM (8000-83FF, 128 × 16, at 8300 in software) sit on the CPU's 16-bit bus. Everything else, including the 32K expansion, cartridge ROM, GROM, sound and speech, is behind a 16-bit to 8-bit bus converter ("the memory bus (with data bus converted to eight bits wide)"). - A 16-bit access through the converter is **two 8-bit cycles, odd byte (A15 = 1) first, then even byte**, assembled into a word before the CPU sees it. TI's figure B shows the read with one wait state per byte; "the CPU will look at the full 16-bit data bus during the leading edge of φ1 of clock cycle 6": a 6-clock memory cycle, i.e. **4 wait states per access** (DOC, read from the timing text; the figure itself is a scan, U5). - A write "will always be preceded by an ALU cycle" and is also two 8-bit writes; MBE stays low for the whole 1.8 µs (≈ 5.4 clocks) [CTD A.6]. - Where the VDP sits: figure A draws the TMS9918A on D0-D7 of the CPU side of the converter. Whether VDP accesses still get the converter's wait states is U6 (the board's question; the core only needs the wait count per access). ## IAQ and the cycle trace IAQ is high on every instruction-fetch memory cycle (not for X's target, not for immediate or address words). The core's trace (`kind` bits in the bus contract, handoff brief) records each machine cycle as (clock, type, address, data, IAQ, waits); that trace is what tms9900-rust must reproduce cycle for cycle. ## What MAME's timing is (detector only) MAME 0.285 traced 161,014 instructions in 1.001 s of the 99/4A's title screen (frames 120-180, `scripts/mame_trace.py`, OBSERVED, closed box): 18.6 clocks per instruction on average, wait states included in whatever way MAME models them (its source is not read). It is a detector for instruction streams, not a judge for any figure above.