# Timing: states, the bus, the queue Citations as in [start-here.md](start-here.md) (H2 = 900/H book ch. 3 part 2, HB = its appendix B lists; printed page numbers). ## The unit - Toshiba states every instruction's time in **states**, in the order 8-bit, 16-bit, 32-bit operand (HB CPU900H-157). "1 state = 100 ns at 20 MHz oscillation; 80 ns at 25 MHz": one state is **two oscillator clocks** on Toshiba's own parts (HB CPU900H-157; the same in DIFF CPU900-167 for the 900). - A basic bus cycle is **two states**, T1 and T2 (H2 CPU900H-32: "160 ns at 25 MHz"), with programmable waits inserted by the chip-select/wait controller (figures 7.2-7.5: 1 wait, 1 wait + n, 2 waits) and a one-state dummy cycle (figure 7.6). - Built-in memory is on a 16-bit internal bus with no wait; built-in I/O is on an 8-bit bus synchronised to the CLK pin, so **one wait is inserted automatically when CLK is high as ALE rises** (H2 CPU900H-31). On the NGP that means SoC register accesses can cost an extra state depending on phase: U5. - Dynamic bus sizing: an 8- or 16-bit external bus per chip-select area; a word at an odd address takes two bus cycles (H2 CPU900H-30). ## Per-instruction states (HB) Samples, read from the OCR text of the pages (8/16/32-bit; to be re-read on the page images when the table is transcribed): | Instruction | States | | Instruction | States | | --- | --- | --- | --- | --- | | `LD R,r` | 2/2/2 (900: 4/4/4) | | `JP #16` / `JP #24` | 5 / 6 | | `LD r,#` | 3/4/6 | | `JR cc,d8` | 5 taken / 2 not | | `LD R,#` (short form) | 2/3/5 | | `CALL #16` / `#24` | 9 / 10 | | `PUSH r` | 4/4/6 | | `RET` / `RETI` | 9 / 12 | | `MUL rr,#` | 12/15 | | `SWI n` | 19 | | `DIV rr,#` | 15/23 | | `EI n` | 3 | | `LDIR` | 7n+1 | | `LDF n` | 2 (H1 CPU900H-7: 160 ns at 25 MHz) | The full lists (10 pages, CPU900H-158 to -167) become the core's timing table, as data, one row per decode form (the JIT proposal's "timing as data"). ares's author notes that the manual's text and its list disagree about where `LDIR`'s extra read falls when BC = 0 (ares `instructions.cpp` comment): C2. ## The instruction queue The 900/H has a **4-byte instruction queue** (H1 CPU900H-2) and, unlike the 900/H2, fetches the branch destination only when a branch is taken (H1 CPU900H-1). The state counts above assume the queue keeps up ("instruction decoding completed" style conditions are not spelled out per instruction in the pages read; U6). How the queue refills on the bus (word fetches at even addresses, byte at odd) decides which bus access lands on which state, which is what the board sees (a K2GE register read mid-line, a write to the Z80's shared RAM). Neither Toshiba's pages in hand nor any hardware capture give the refill order; ares models a 3-byte-deep queue filled from leftover states (its `prefetch.cpp`; `PIQ.size() >= 3`, "PIC" counter), an emulator's choice, tier 5: U6. ## What the NGP adds - The crystal is 6.144 MHz. If a state is two crystal clocks, the CPU runs **3.072 M states/s**; MAME 0.285 and ares both run it at **6.144 M states/s** (MAME's frame of 102,485 cycles matches the screen's 515 x 199 at 6.144 MHz, and MAME charged 3 cycles for `EI` and 5 for `LD (#8),#` in our trace, exactly the 900/H state counts). The SoC may run its CPU from a doubled clock; nothing in hand settles it: **U2, the first timing question for the board**, decided by a probe ROM on a real NGPC (count instructions per K2GE line) when one is available. - The SoC has a clock gear (software slows the CPU to save power; ares cpu/io.cpp "clock.rate", default 4, "unconfirmed"): the core takes its state rate from the board. - Wait states for the cartridge flash and work RAM are SoC facts (board, U5). ## Instructions per second (for domains-and-speed) MAME's trace of the NGPC BIOS: 66,779 instructions in 307,454 states (3 frames): **4.6 states per instruction**, so about **1.33 M instructions/s** at 6.144 M states/s (0.67 M/s if U2 resolves to 3.072 M states/s). The BIOS spent most of those frames in a `bit`/`jr` polling loop (26,577 `jr`, 13,290 `cp`, 13,288 `inc`, 13,288 `bit`), so this is an upper-middle estimate; games with more memory operands average more states each.