# Timing: one tick timeline Citations as in [start-here.md](start-here.md): **Spec §n**, **Appx 2**, **LT** (lynx-tests guides: page-mode, timers, video-timing, sprite-performance), **LA** = the Lynx II logic-analyser captures in `reference/src/gearlynx/tests/logic-analyzer/` (tier 1; DSView `.dsl` files, 100 MHz; data recorded from a console, fetched without the emulator's source). Statements that rested on reading an emulator's source were struck on 2026-11-14 ([provenance.md](provenance.md)). The board keeps **one counter of 16 MHz ticks**. Every CPU cycle, Suzy step, timer prescale, DMA burst and LCD group is placed on it. There is no "CPU clock": a CPU cycle is as long as the bus makes it. ## CPU cycle cost (Spec §4.1.3) | Cycle | Min | Max | | --- | ---: | ---: | | Page-mode RAM or ROM read (opcode stream only) | 4 | 4 | | Normal RAM/ROM read or write | 5 | 5 | | "Available" hardware (most of Mikey) | 5 | 5 | | Mikey audio/timer DPRAM | 5 | 20 | | Mikey palette | 5 | 5 | | Suzy write (blind, no DTACK) | 5 | 5 | | Suzy read | 9 | 15 | | Cart read through Suzy (RCART0/1) | 15 | 15 (Spec §9.2) | Internal (no-bus) CPU cycles: there are none on a 65C02 (every cycle reads or writes: `w65c02-python`'s contract); a dummy read is charged as a read. The spec gives no separate cost (U1). ### Page mode (Spec §4.1.1; LT page-mode; LA `lynx2-mikey-page_mode.dsl`) The spec says only: opcode reads may use page mode (4 ticks), all writes and data reads are normal (5), and the decision is made by decoding the current opcode, not by comparing addresses. The hardware-observed rules (LT, from the capture of RAS/CAS/A0-A7 and controlled test ROMs on a Lynx II): - A fetch of an **instruction byte** (opcode or operand) takes 4 ticks when MAPCTL bit 7 is clear **and** the previous CPU memory cycle was also an instruction-byte fetch **and** `address & $0F != 0`. - Every 16-byte boundary (`$xxx0`) is a 5-tick fetch; it does not break the run. - Any data read or write, stack access, pointer read or vector read breaks the run: the next fetch is 5 ticks. - A not-taken branch keeps the run. A taken branch adds one 5-tick cycle (two if it crosses a 256-byte page) and breaks the run, except a branch with displacement 0. `JMP abs` keeps the run (observed). - A video DMA or refresh access between two fetches closes the DRAM row: the next CPU memory cycle is 5 ticks. - The MAPCTL access itself is a 5-tick data cycle. Whole-ROM oracle: lynx-tests `page-mode` Timer 6 results `$35`/`$41` (200 NOPs on/off), `$69`/`$81` (200 `LDX #`), `$74-$75`/`$81` (100 `LDA abs`). The measurement is the table of tick counts. Build to the counts. ### Hardware register waits (Spec §4.1.2) - Mikey's timer and audio registers are dual-ported RAM "addressed in a cyclical manner" with latency up to its cycle time (1.25 µs = 20 ticks): a read or write waits for that register's slot. lynx-tests `timers2` tests 7-9 measure 64 reads and 64 writes of every timer backup register and compare RAM / INTSET / SERCTL access time; their expected envelopes are the oracle. The exact slot order is not documented (U2). - Suzy reads take 9-15 ticks ("+1 tick, -0" clock synchronisation); writes are blind 5-tick cycles and Suzy needs up to 6 more ticks internally. Two consecutive CPU accesses to Suzy are forbidden by the spec; what happens is undefined (U3). - A cart write (to RCART0/1) is blind; Suzy must not be touched for 12 ticks after it (Spec §9.3; the spec's two statements of where the 12 ticks start disagree: C12). - Upper bound 128 ticks, "probable actual maximum 40". ## Who owns the bus Priority: video, refresh, CPU, Suzy (Spec Appx "bus interplay"). One master at a time. - **Video DMA** (LT video-timing; LA `lynx2-mikey-*`/`lynx2-lcd-*`): ten bursts per visible line, 8 bytes each (80 bytes = 160 pens), 192 ticks apart; the length of each burst's ownership is to be measured from the LA captures (the earlier figure came from an emulator's source and was struck). Burst position relative to the LCD group: to be re-read from LT video-timing and the captures. - **Refresh**: Spec §4.1.5 places refresh between video and CPU in priority; its period and length when DMA is off are unknown (U4). The earlier figure came from an emulator's source and was struck. - **CPU pause**: at the bus controller's chosen point in the CPU cycle (Spec Appx 7 "state machine", which is **not in any source found**: U5). - **Suzy**: yields to a Mikey request at her next safe cycle, at most 30 ticks of process + overhead = 40 ticks (Spec §4.1.5). She continues work that needs no RAM while Mikey has the bus. ## Timers (Spec §8; Appx 2; LT timers; lynx-tests `timers`, `timers2`) - Eight 8-bit down counters. Source select 0-6 = 1, 2, 4, 8, 16, 32, 64 µs (16 to 1024 ticks) from **one shared free-running prescaler** (enable order and phase tests: `timers2` 1-6); 7 = the linked predecessor's borrow. - Links: 0 → 2 → 4; 1 → 3 → 5 → 7 → audio 0 → audio 1 → audio 2 → audio 3 → 1. - A count of N borrows after N+1 source clocks; the first interval is N to N+1 periods. - At borrow: DONE set, reload if enabled, successor clocked, interrupt bit set if enabled (timer 4's slot is the UART). - Hardware defects that programs rely on, all in Spec §16.1 and tested: the DONE bit must be clear for a **non-reloading** timer to count; RESET_DONE (CTLA bit 6) is a level, not a pulse; a one-shot at zero with DONE cleared requests an interrupt **on every source clock** (AtariAge 347170, found on hardware by laoo with a VGM player that ran on a console and, as the thread says, on neither Handy nor Felix: C5). - The interrupt is requested when the counter is already zero and tries to borrow, not when it reaches zero (LT timers). - Writing CTLB bit 1 injects one clock (`timers` test 1: `CNT=$80`, write `$0A`, read `CTLB=$08`, `CNT=$7F`). ## The display (Spec §5; LT video-timing; LA) Standard setting: Timer 0 = 1 µs source, backup 158 (159 µs = 2,544 ticks per line); Timer 2 linked, backup 104 (105 lines); PBKUP 41. Frame = 16.695 ms = 59.90 Hz. MAME 0.285, run as a closed box, reports the same: 66,780 CPU cycles of 250 ns per frame (OBSERVED, [mame-oracle.md](mame-oracle.md)). - A line = Timer 0 borrow (IRQ request here) → leading non-active interval (624 ticks at the standard rate) → **1,920 ticks of LCD transfer, fixed**, in ten groups of 192 ticks, 16 pens each. - A frame = Timer 2 borrow (the vertical-blank IRQ) → 3 blank lines (the third latches DISPADR, low two bits masked) → 102 visible lines. `visible = min(102, TIM2BKUP - 2)`, `total = TIM2BKUP + 1`. - PBKUP only positions the P4/H pulse for the LCD drivers; out of its window the glass shows black or desynchronises. It does not change DMA or CPU timing. Formula: Spec §5.1, restated in ticks in LT. - Refresh rates seen in captures: 50 Hz (Lemmings), 52 (Krazy Ace Miniature Golf; the capture file spells it "kracy_ace"), 60 (Crystal Mines II), 75 (Batman Returns), 78 (laoo's slope test). - DISPCTL: bit 0 DMA enable, bit 1 flip (the buffer is read backwards from DISPADR: C11), bit 2 four-bit mode, bit 3 colour. Only 4-bit colour was shipped. ## Suzy's sprite engine The spec gives the structure (SCB fetch, 8-byte source FIFO, 12-bit unpacker, pen map, H/V accumulators, byte builder, 8-word pixel FIFO, video merger, collision unit: Spec §6) and **no cycle counts**. What exists: - **Measured slopes** (LT sprite-performance, from controlled ROMs on Lynx I and II): a row costs about the *maximum* of the overlapping paths (source/decode, pixel builder, RAM bus, collision) plus downstream work, not their sum. Examples: a 161-pen 4-bpp row downscaled to 20-80 outputs ≈ 394 ticks per warm row; one pen expanded to W outputs ≈ `72 + 2*W` ticks; XOR adds `2 * ceil(outputs / 2)`; the first row after a start is "cold" and costs more. - **Whole-ROM oracles**: lynx-tests `sprites1` to `sprites5` (40 cases: literal/packed, depths, scaling, clipping, the 8 types with CRCs of video and collision buffers, linked SCBs, DMA contention, stretch and tilt), each with a Timer 6 duration measured on hardware; laoo's SuzyTiming (how much of a 102 x 102 sprite is painted after N µs; the judge is a console photo, none found: U6). - **Emulators' models** are not read (restart rules); Holani and MAME may be run as closed boxes to detect differences. The tests are the judge. Bus cost while Suzy draws: each Suzy RAM access is a bus cycle; the CPU executes nothing; Mikey's DMA still interleaves. ## Math (Spec §12.1.3) Multiply 44 ticks (54 with sign and accumulate); divide `176 + 14*N` ticks, N = leading zeros of the divisor. The CPU keeps running: SPRSYS bit 7 reads "math working". lynx-tests `math` test 8 measures both with Timer 6. ## UART (Spec §8a; LT timers; lynx-tests `uart` 1-4) Timer 4's borrow, divided by 8, is the bit clock: baud = source / ((BKUP+1)*8). A frame is 11 bit times (start, 8 data, parity/9th, stop). TX and RX share one open-collector wire, so **a Lynx always receives what it sends**; single-player programs that poke the UART see their own echo about one bit after the frame (`uart3`). The interrupt is a level (Spec §16.1). Timing oracles: `uart` (6 cases at 62,500 / 9,600 / 2,400 / 1,200 baud), `uart2` (overrun, parity, live SERCTL changes, holding register), `uart3` (echo). ## Audio (Spec §7; lynx-tests `audio`, `audio2`) Each channel is a timer (same prescaler, same link chain) clocking a 12-bit LFSR whose taps are FEEDBACK bits 0-5 (taps 0-5), FEEDBACK bits 6-7 (taps 10, 11) and CTLA bit 7 (tap 7); the inverted XOR is shifted in and is the output bit. Output = ±VOL, or in integrate mode (CTLA bit 5) the running sum clipped to 8 bits signed. The four outputs are mixed and sent as PWM through a 4 kHz one-pole filter (Spec §7.2). Lynx II adds per-channel left/right enables (MSTEREO) and attenuation (ATTEN_A-D, MPAN): C8. ## What MAME does instead Observed from outside (closed box): its trace counts uniform 250 ns CPU cycles (16 MHz / 4) and its frame period on Zaku is 66,780 of them (whether it follows a game's timers is to be observed, not read). So MAME can check instruction flow and register values, never time. [mame-oracle.md](mame-oracle.md). (The earlier list of its internals came from its source and was struck.)