# Timing Unit: the **machine (instruction) cycle**, two CKI periods (`T = 2 x tCKIHCKIH`, 2-16, 6-7, data sheet electrical tables). QSound: CKI = 60 MHz from an oscillator (CPS-2 A-board schematic sheet 16, tier 3), so 30 MHz machine cycles, 33.3 ns: the slower of the DSP16A's two original speed grades (25 ns parts arrived with the May 1989 addendum). MAME's `totalcycles` for the DSP counts machine cycles (30,000,000 per emulated second, measured). ## Per instruction (manual, tier 2) From table 3-3 (3-8), 3.5-3.8 and the instruction summary (3-20 onwards): | Class | T | Words | Cycles out of / in the cache | | --- | --- | --- | --- | | multiply/ALU, no transfer or `Y` (postmodify only) | 00110 | 1 | 1 / 1 | | multiply/ALU with a RAM read: `aT[l] = Y`, `x = Y`, `y[l] = Y` | 00111, 10110, 10111 | 1 | 1 / 1 | | multiply/ALU with a RAM write: `Y = aT[l]`, `Y = y[l]` | 00100, 11100, 10100 | 1 | 2 / 2 | | multiply/ALU with compound addressing: `Z : aT[l]`, `Z : y[l]`, `Z : y x = X` | 00101, 10101, 11101 | 1 | 2 / 2 | | multiply/ALU with RAM and ROM reads: `y = Y x = X`, `y = aT x = X` | 11111, 11001, 11011 | 1 | **2 / 1** | | special function `if CON F2`, `ifc CON F2` | 10011, 10010 | 1 | 1 / 1 | | short immediate (9-bit) | 00010, 00011 | 1 | 1 / 1 | | data move `R = Y`, `Y = R`, `Z : R`, `R = aS`, `aT = R` | 01111, 01100, 01101, 01001/01011, 01000 | 1 | 2 / 2 | | long immediate `R = N` | 01010 | 2 | 2 / not cacheable | | `goto JA`, `call JA`, `return`, `goto pt`, `call pt`, `ireturn` | 0000x, 1000x, 11000 | 1 | 2 / not cacheable | | `if CON` + its branch | 11010 + branch | 2 | 3 for the pair (taken or not) | | `icall` | 11010, SI = 1 | 1 | 3 | | `do K` | 01110, NI > 0 | 1 | 1 | | `redo K` | 01110, NI = 0 | 1 | 2 | | interrupt entry (the "jammed" `goto 1`) | | | 2 (as a `goto`; measured in MAME, implied by 2-15) | The reason for the pattern (MAME's header, consistent with the manual's statements): one ROM access and one RAM access per cycle; out of the cache the ROM port fetches the next instruction, so a ROM data read costs a cycle; a RAM write happens only in an instruction's second cycle. ## The cache (3.8.1, 3-25, 4-6 worked example) For `do K { NI instructions }`: 1. `do` itself: 1 cycle. 2. First pass (loading): every instruction at its out-of-cache time, **except the last, which takes 2**. 3. Passes 2 to K-1: in-cache times. 4. Last pass: in-cache times except the last instruction, at its out-of-cache time. `redo K`: 2 cycles, then K passes at in-cache time, except the last instruction of the last pass at out-of-cache time. The manual's own check: `do 40 { y = aN }` = 1 + 2 + 38 x 2 + 2 = 81; `do 40 { y = aN x = *pt++ }` = 1 + 2 + 38 x 1 + 2 = 43 (4-6). Nothing in the cache is interruptible, nor is `do`/`redo`, nor any branch or the qualifier with its branch (2-15, 3-25). A `do` block that is one instruction long (NI = 1) is the case jtdsp16 calls tricky; the rules above cover it (the 81/43 example is NI = 1). ## Measured agreement: MAME 0.285 against the manual `scripts/cycle_audit.py` charges every instruction of a MAME trace by the rules above and compares with MAME's `totalcycles`: | Trace | Instructions | MAME cycles | Manual cycles | Instructions that differ | | --- | --- | --- | --- | --- | | `tone` (idle host, 1.000-1.017 s) | 402,704 | 499,999 | 499,999 | 0 | | `busy` (a host write every ~35 samples, 11 interrupts) | 402,627 | 499,999 | 499,999 | 0 | So MAME's machine-cycle model and the manual agree on every instruction class the program's steady state executes (24 of the 31 T values, cache passes, conditional branches, interrupt entry). This is a detector agreeing with the manufacturer, not a hardware check (U18). Classes the program does not execute (`Z : aT`, `Z : R`, `ifc`, `icall`, `Z : y x = X`, `y = a0 x = X` in the cache) are checked against the manual only, by directed tests (rung 1). ## The sample period The program's loop, from one write of `sdx` (left word) to the next, takes **1,245 machine cycles** on both its paths (command or no command): 30 MHz / 1,245 = **24,096.39 Hz**. The right word is written 337 cycles after the left. Per sample: 1,002-1,004 instruction executions (cache passes counted); about 760 one-cycle, 233 two-cycle, 2-4 three-cycle; six `do`/`redo` loops. The widely quoted 1,248 (24,038.46 Hz) is contradiction C1. The second mode's loop (state 0x061A) is not measured (U17). The interrupt path: entry 2, the service routine at 0x0001-0x000B 16, `ireturn` 2. The program enables INT for exactly one instruction per sample (at 0x0542, between `pioc = 0x3820` at 0x0540 and `pioc = 0x3800` at 0x0543: the enable takes effect one instruction late, 6-4), so at most one host command is taken per sample, always at the same point, and the no-command path is padded to the same length. ## The serial port `sioc = 0x02E8`: active clocks at CKI / 12 = 5 MHz (one bit every 6 machine cycles; jotego's capture shows a 200 ns OCK period, tier 1); a 16-bit word is 96 machine cycles. Both words of a sample fit easily in 1,245 cycles, so the program never waits on OBE. Event-driven output (compute each word's bits when `sdx` is written and the load edge arrives) is exact; stepping the serial unit per CKI is not needed. Details in [peripherals.md](peripherals.md). ## The parallel port Strobe width from `pioc` bits 14-13: 1 to 4 T (the program uses 2 T). In active mode a `pdx` read returns the current input register and starts a strobe that refills it (the latent read, 6-5). Timing diagrams 6-6 to 6-8. The core charges the instruction's cycles only; the strobe's start and length are reported to the host through the callback (the QSound glue does not depend on sub-cycle timing: it latches on strobe edges).