# POKEY timing: what happens on which cycle Source names are those of [start-here.md](start-here.md). A fact marked **hw** was measured on real machines by the source cited; **die** means stated in prose by someone who read the die-derived schematics (ijor and phaeron in [RE]), or, from 2026-11-19, read here from ijor's schematic itself (`DIE: ijor PokeyReSchem-13, sheet N`; restart.md, amendment); **closed box** means measured here through the VHDL's pins; **mfr** Atari's documents; **emu** an emulator, run as a closed box. Nothing here was read from any model's or emulator's source ([provenance.md](provenance.md)). ## The unit and the convention The unit is one period of O2, the chip's only clock: a **cycle**. On the Atari 8-bit computers, the 5200 and the 7800 it is one CPU cycle, including the cycles in which ANTIC or MARIA has halted the CPU (the clock does not stop). [AHRM] numbers cycles from the bus access: "a write to STIMER at T+0" is the cycle in which the CPU puts the byte on the bus (the last cycle of `STA`), and "IRQST bit 0 asserted at T+8" means a read whose bus cycle is T+8 is the first to return the bit low. This document and the brief use the same convention, so every number here can be written as a test of the form *write in cycle T, read in cycle T+k, expect v*. A board ticks the core once per cycle and performs its bus access in the cycle it belongs to ([handoff-brief.md](handoff-brief.md), "The interface"). Inside the chip a write is not instantaneous: the address decoder's strobe is asserted in the cycle **after** the CPU's write cycle (ijor, [RE p1], 2021-09-19), and gates are clocked on one of two phases. None of that is visible to a host except as the latencies below. ## Dividers at the machine clock: the cycle table (hw) Channel 1 on the 1.79 MHz clock, AUDF1 = N, STIMER written at T+0 ([AHRM 5.3] "STIMER preemption", "Reload timing" and Figure 8; the same numbers from phaeron's 2021 post in [RE p1]; [ACID] `pokey_timertiming.s` tests them): | Event | Cycle | | --- | --- | | last AUDF1 write that changes the first period | T+2 | | the counter loads AUDF1 (first reload) | T+3 | | last STIMER write that stops the first underflow from firing | T+3+N ("T+4 to block ... T+8" for N = 0) | | IRQST bit 0 first reads 0 | **T+8+N** | | second reload; last AUDF1 write for the second period is one earlier | T+7+N | | then every | N+4 | For a joined pair (AUDF1 = AUDF2 = 0 as the example): timer 1's status bit first reads 0 at T+8, timer 2's three cycles after timer 1's last underflow of the period (T+11), the second reload of **both** counters is at T+9, and the pair's period is N+7 [AHRM 5.3, Figure 8]. The reload of both counters does not make the low timer fire again. At the 15 and 64 kHz clocks the counters only move on a clock tick, so STIMER on a counter that has not moved since its last reload changes nothing, and the three-cycle pipeline is absorbed into the wait for the next tick [AHRM 5.3]. ### What the VHDL does on the same questions (closed box, run 2026-11-11) `python -I scripts/run_vhdl.py --oss DIR timer init clocks`: | Question | Hardware | [VHDL] | | --- | --- | --- | | output half period, AUDF1 = 0, 1, 5 at 1.79 MHz | 4, 5, 9 | 4, 5, 9 | | IRQST bit 0 first reads 0 after STIMER, AUDF1 = 0, 1, 5 | +8, +9, +13 | **+7, +8, +12** | | RANDOM four cycles after leaving init, 9-bit | `$1F` | `$1F` | | RANDOM 114 cycles after leaving init, 9-bit | `$95` | `$95` | | RANDOM 114 cycles after leaving init, 17-bit | `$08` | `$08` | | IRQST bit 0 after leaving init, timer 1 at 64 kHz, AUDF1 = 0 | +24 (manual); **+25** (Acid800 `pokey_inittiming.s`, C10) | **+26** | | the same at 15 kHz | +83 (manual); **+84** (Acid800, C10) | **+84** | The periods and the three RANDOM values agree. Three absolute latencies differ, by -1, +2 and +1 cycles, and not by one common offset. The cause is not established (U1). (2026-11-19: ijor's schematic as drawn gives the box's +26 for the 64 kHz row, through a half-phase race where init enters the two prescalers on opposite phases [DIE: ijor PokeyReSchem-13, sheet 4]; the test's sled counts need +25 and the core keeps it: U1, "The die".) Candidates that can be examined without opening the model: the testbench's bus timing against the model's expectation (a write placed in one phi2 period may land in the model a cycle earlier or later); the stimulus for the two clock rows is not the sequence [ACID] `pokey_inittiming.s` uses on hardware; and the manual's two figures are stated in a sentence, not in a table of raw readings. Until U1 is closed, **absolute latency from a bus write is judged by the hardware numbers, and the VHDL is compared after aligning each stream**: see [validation.md](validation.md). ## The two derived clocks (hw) Both are polynomial counters cut short, not binary dividers; every cell of this table is [AHRM 5.2], "Clock reset timing": | Clock | Counter | Reset state | Tick pattern | First tick after leaving init | Then every | | --- | --- | --- | --- | --- | --- | | 64 kHz | 5 bits, XOR, x^5 + x^3 + 1 | `11111` | forced 1 at `00010` | 19 cycles | 28 | | 15 kHz | 7 bits, XNOR, x^7 + x^6 + 1 | `0000000` | forced 1 at `1001001` | 78 cycles | 114 | So neither clock starts a full period after the release: the first 64 kHz tick comes 19 cycles later, the first 15 kHz tick 78. With a period-0 timer on that clock and its interrupt enabled, IRQST shows it 24 and 83 cycles after the SKCTL write (5 more: [AHRM 5.2]); Acid800 `pokey_inittiming.s` expects 25 and 84, which the core follows (B2.1b; contradiction C10). The first slow pot-scan increment is at 81 + 114k cycles after that write [AHRM 5.9, "Scan timing"]. Only initialisation mode moves these phases; STIMER does not. Consequence for a 5200 or 8-bit board: the 15 kHz clock's offset within the scan line is set by the program (the cycle of its last SKCTL write), not by the board. A board that derives "the POKEY line clock" from its video counter is wrong; the core derives it from O2 alone. ## Polynomial counters (hw) - They shift once per cycle from the cycle initialisation mode is left; in init mode the audio side sees constant 0 from the 4-bit and 9/17-bit counters and constant 1 from the 5-bit [AHRM 5.5]. - Entering init mode: 1s are shifted into RANDOM from the left; from nine cycles after the SKCTL write RANDOM reads `$FF`; the remaining stages of the 17-bit register need nine more cycles [AHRM 5.5]. - Leaving init mode in 9-bit mode: `STA SKCTL` then `LDA RANDOM` back to back reads `$1F` (the read is four cycles after the write) [AHRM 5.5]. - [ACID] `pokey_noise.s`: write SKCTL = 3 on the last cycle of an instruction that follows `INC WSYNC`, read RANDOM with the next `INC WSYNC` / `LDA RANDOM`: 114 cycles later. Expected `$95` with AUDCTL = `$80` and `$08` with AUDCTL = 0. (The comments in that file give 105-108 and 108-111 as the cycles of the two reads; the distance that reproduces both values through the closed box is 114.) - RANDOM read once per scan line (`STA WSYNC` / `LDA RANDOM`, 9-bit mode) includes the run `00 DF EE 16 B9` [AHRM 5.5]. - The noise bit reaches channels 1 to 4 one cycle apart, channel 1 first; the 9/17-bit bit comes from the right-hand end of RANDOM, so the CPU sees every bit before the audio does [AHRM 5.5]. - Switching AUDCTL bit 7: "an additional eight bits are added to the shift register" in 17-bit mode [AHRM 5.5]; what the stages hold, and what RANDOM reads, across a switch while running is not specified by any permitted source (U2). **After STIMER** the polynomial counters are untouched: STIMER only reloads the dividers. A player that writes STIMER and then starts a note gets a noise timbre set by where the free-running counters happen to be, unless it also controls its own cycle count since the last init. "Resetting the poly counter with STIMER" in tracker documentation means aligning the divider, not the counter. ## Output path delays - The high-pass flip-flop updates about 1.5 cycles after the clocking channel's pulse; channels 1 and 3 at the same period with channel 3 two cycles ahead give a **half-cycle** pulse each period [AHRM 5.3]. A model sampled once per cycle cannot hold that pulse (U3). - In a joined pair the high timer's output and interrupt change three cycles after the low timer's last underflow [AHRM 5.3]. - Volume-only writes: nothing found states how many cycles after the AUDC write the pin changes (U4). Sample players write AUDCx once per timer interrupt or per scan line; their sample period is the player's, not the chip's. ## Ultrasonic settings With a channel at the machine clock and a small AUDF, a pure tone is far above hearing (AUDF = 0: 223.7 kHz at NTSC) and averages to half volume at the pin. Three uses depend on getting this right rather than muting the channel: the high-pass difference trick (two channels at 28409.1 Hz and 27965.2 Hz giving a 443.9 Hz sawtooth, [AHRM 5.3]); noise sampled at a high rate and aliased down by the pattern length (*Ray of Hope*, *Numen* use channels 3+4 joined at the machine clock with the 4-bit counter: [AHRM 5.10]); and volume modulation by a DC-biased ultrasonic carrier that pushes the mix into its saturating region. The core therefore produces the true per-cycle state and never "optimises away" an inaudible channel. ## Two-tone (hw) - When SOD toggles because of timer 1 or timer 2, both counters reload **two cycles after** the reload of the timer that caused it. A timer 1 at the machine clock that would fire at T+8, +12, +16 fires at T+8, +14, +20 in two-tone mode; at the slow clocks the two cycles are hidden [AHRM 5.6]. - The other timer may fire up to one cycle after the resyncing one without being pre-empted; with periods one cycle apart the two resync each other and three cycles are added [AHRM 5.6]. - The serial side's divide-by-two is not reset by STIMER; it is reset when two-tone mode is switched off [AHRM 5.6]. ## Interrupts (hw) - Enabling: the IRQEN write must be at least **four** cycles before the source fires or it is not latched. Disabling: at least **two** cycles before, so one cycle exists in which an interrupt still arrives after its enable was cleared [AHRM 5.7]. - From IRQST showing the bit to the 6502 starting its interrupt sequence: two or three unhalted cycles, differing between machines and with temperature [AHRM 5.7]. That is the CPU's sampling of the line, the board's and the CPU core's business; the POKEY core's contract ends at the /IRQ level per cycle. - [ACID] `pokey_irqtiming.s`, `pokey_timerirq.s`, `pokey_timergranularity.s` test these with the CPU in the loop. ## Serial port - A bit lasts two periods of the clocking divider. For 19,200 baud the OS uses channels 3+4 joined at the machine clock with AUDF3/4 = `$0028`: period 47 cycles, so the real rate is 19,040 baud [AHRM 5.6]. - The output shifter loads only on a rising edge of the serial clock, so the first SEROUT write waits up to one bit time before "output needed" appears and "transmission finished" clears [AHRM 5.6, "Shift timing"]. - Asynchronous receive: timers 3+4 start at the start bit's leading edge; the first sample is one timer period later (mid start bit), then every two; the byte completes 19 periods after the edge, SKSTAT bit 1 is low from mid start bit to mid stop bit [AHRM 5.6, Figure 10]. - Setting SKCTL bits 6-4 to 000 resets the two serial clock flip-flops; the output follows on the next output clock, the input after two input clocks [AHRM 5.6, "Serial port reset"]. - The fastest internally clocked rate is O2 / 7 / 2 (about 128 kbit/s) [AHRM 5.6]. - [DS 31] gives the pin timing (setup, hold, delay) of SID, SOD, BCLK and OCLK against O2 as drawings with symbolic times; the numeric table is on sheets 25-30 (not transcribed: scans, U5). ## Keyboard (hw, mfr) One code per 15 kHz tick, 64 ticks per scan (about 4.1 ms at NTSC). A debounced key press needs two scans; the fastest recognised typing is one key per four scans (256 ticks) [AHRM 5.8]. The keyboard interrupt's cycle within a scan line is therefore fixed by the 15 kHz clock's phase. With debounce off a held key is re-reported every scan; on the 5200, where each key sits at codes that alias, every 32 ticks [AHRM 13.3]. [DS 11] is Atari's flow chart, [DS 13] the scan line timing drawing, [DS 14] the block diagram. ## Pots (hw) - Slow scan: counter +1 per 15 kHz tick; the first increment after a POTGO is the next tick, not a full period later; 227 x 114 = 25,878 cycles after the first increment the counter reaches 228; any register still counting shows a stable `$E4` the cycle after, and ALLPOT reads `$00` one cycle after that [AHRM 5.9]. - Fast scan: counter cleared in the cycle after the POTGO write, +1 per cycle; ALLPOT is forced to `$00` from 231 cycles after the write; registers still counting hold 229 (`$E5`) [AHRM 5.9]. - ALLPOT and the latching of POTn are not clocked by the scan clock: a pin that crosses the threshold freezes its register on that cycle, and a pin that falls back below it lets the register follow the counter again [AHRM 5.9]. - Live reads: while a register is counting, a read in the increment cycle returns `old AND new`. In fast scan, the cycles after POTGO read `$00, $00, $00, $02, $00, $04, $04, $06, $00, $08, $08, $0A, $08, $0C, $0C, $0E, $00` [AHRM 5.9]. - The dump transistors are on when slow mode is selected and no scan is running; they take roughly 100 cycles to empty a charged capacitor; they are off whenever fast mode is selected [AHRM 5.9]. A POTGO before the previous scan has finished starts counting from zero with the capacitors part charged ("truncated scan"). - For the 5200: a working controller spans at least 160 counts side to side ([AHRM 13.3], citing Atari's PAM package p. 25); with the stick's power line off every pot reads 228. ## The bus [DS 25-31] [DS 29-31] has the write and read timing against O2 as drawings (setup, hold and data delay with symbolic names, values in the tables of sheets 25-28). A board with cycle-level timing does not need them: an access belongs to one cycle. Addresses with no read register drive `$FF` [AHRM 5.1].