# Timing Source keys are in [start-here.md](start-here.md). Almost every number here is from [AHRM] chapter 4 (tier 3: community research, measured on computers, not on a 5200) and must be treated as the specification to test, not as settled fact; where a manufacturer document or the die schematic says the same or differs, that is noted. The judges are named in [validation.md](validation.md). ## One timeline The unit is the machine cycle (phi2 period, 1/1.79 MHz) = 2 colour clocks = 4 high-resolution pixels. Everything is counted from ANTIC's horizontal counter. | Quantity | Value | Source | | --- | --- | --- | | Cycles per line | 114 (0-113) | [AHRM] 4.2 Table 6; [HM] II | | Colour clocks per line | 228 | | | Lines per frame | 262 (0-261) NTSC | [AHRM] 4.2 | | Cycles per frame | 29,868 | measured in MAME as 29,866-29,871 between frame marks | | Displayable lines (P/M DMA, display list active) | 8-247 | [AHRM] 4.13, 4.6 | | VBI | start of line 248 | [AHRM] 4.8 | | Horizontal blank | "40 machine cycles... 20 machine cycles (40 colour clocks)" figure | [HM] II.20 (the scan is poor; see C7) | The board advances ANTIC, GTIA and POKEY **inside the CPU's bus callbacks**, one machine cycle per callback plus any stolen or stalled cycles in front of it ([sally-halt.md](sally-halt.md)); it never adds `step()`'s return value on top. The return value of `step()` counts CPU cycles only; machine time is the board's own cycle counter. Within a cycle the board needs an order. Proposed, to be tested by the Acid5200 timing tests: (1) ANTIC decides whether the cycle is DMA; (2) the bus access happens (ANTIC's or the CPU's); (3) register writes take effect with their documented delays ([AHRM] 6.10 Table 18 for GTIA, in colour clocks); (4) the two colour clocks of the cycle are produced; (5) POKEY is clocked; (6) interrupt lines are recomputed. ## DMA: which cycles ANTIC takes All by HALT. Per scan line ([AHRM] 4.13, 4.14 p. 86-93, with the timing charts on p. 90-93, which the build session must have open): | Cycle | What | Condition | | --- | --- | --- | | 0 | missile data | lines 8-247, DMACTL bit 2 or 3 | | 1 | display list instruction | first line of a mode line, display list DMA on (DMACTL bit 5) | | 2-5 | player 0-3 data | lines 8-247, DMACTL bit 3 | | 6, 7 | LMS or jump address, low and high | instruction with LMS, JMP or JVB | | from 10-28 | playfield and character DMA | by mode and width, below | | 25, 29, ... (9 cycles, every 4th) | RAM refresh | every line, vertical blank included; a refresh blocked by playfield DMA moves to the next free cycle; only one can be pending, further blocked ones are dropped | | 106+ | "virtual" playfield DMA | not a bus cycle and no HALT, but ANTIC still latches whatever is on the data bus | Playfield DMA ([AHRM] 4.14): - Widths narrow/normal/wide fetch over 64/80/96 cycles, centred alike. Horizontal scrolling uses the next wider fetch window and delays the fetches by one cycle per two colour clocks of HSCROL. - Mapped (bitmap) modes fetch every 8 cycles (modes 8, 9), 4 (A-C) or 2 (D-F), on the first scan line of the mode line only; first fetch at cycle 28, 20 or 12 for narrow, normal, wide. - Character modes fetch a name every 2 cycles (modes 2-5) or 4 (6, 7) on the first scan line, starting at cycle 26, 18 or 10, and the glyph row three cycles after each name on **every** scan line of the mode line. - A hardware stop ends playfield DMA after cycle 105. - P/M DMA must be switched in DMACTL two cycles ahead to take effect ([AHRM] 4.13). [HM] II.20-21 gives the same rules in prose (one cycle per byte, nine refresh cycles unless pre-empted, one refresh in the first line of a 40-column text row, character data refetched each line) and differs on two-line P/M resolution (C8). A halted cycle costs the CPU exactly one cycle, before whatever cycle it was about to run, write cycles included ([sally-halt.md](sally-halt.md)). ## WSYNC A write to $D40A sets a latch that pulls RDY low ([HM] II and III). - The CPU runs **one more cycle** after the write, then stops on its next read cycle until cycle 105; the instruction after `STA WSYNC` therefore appears to start at cycle 104 ([AHRM] 4.9 p. 77). - If the cycle after the write is DMA, the first cycle of the next instruction runs at 105 instead. - Playfield DMA reaching cycle 105 delays the restart by one cycle; a refresh pushed to 105 or 106 by one or two ([AHRM] 4.9). - A write at cycle 104 or later waits for the next line's cycle 105. - A write cycle directly after the WSYNC write (`INC WSYNC`) is performed; RDY takes hold on the following read. - An NMI taken right after the instruction that wrote WSYNC stalls on the interrupt sequence's first cycles instead. [HM] II.20 says the processor "is restarted exactly 7 machine cycles before the beginning of the next TV line" (C7). ## NMI ([AHRM] 4.8 p. 74-76.) - VBI at line 248, DLI on the last scan line of a mode line whose instruction has bit 7 set. NMIST bits are set from cycle 7 of that line, enabled or not; ANTIC pulls NMI at cycle 8 if NMIEN allows it. - NMIEN must be written by cycle 7 to enable and by cycle 8 to disable an interrupt; enabling on exactly cycle 7 delays the NMI one cycle. - The CPU's 7-cycle entry starts at cycle 10 at the earliest, plus the rest of the current instruction, plus any DMA. - An IRQ entry that starts on cycles 5-9 is taken over by the NMI (the 6502's vector hijack, already in 6502-python). - **An IRQ entry starting exactly on cycle 4 loses the NMI** ([AHRM] 4.8 "Missed NMIs", with footnote 17 guessing at a too-short NMI pulse). This is ANTIC's pulse width meeting the 6502's edge detector; 6502-python's `request_nmi()` is an edge latch, so the board must model the pulse (assert at cycle 8, release when [IJOR] says: read the `nmiReq`/`nmiHpos` logic on the schematic) rather than latch-and-forget. U6. - The 5200 has no System Reset key; NMIST bit 5 never sets ([BUCH]; [AHRM] 13.2 "Nothing is attached to the RNMI control line"). ## VCOUNT $D40B = line / 2. The new value is visible from cycle 111 of the line before ([AHRM] 4.10). On the frame's last line, cycle 111 reads $83 and cycle 112 onward $00. ## GTIA - Register write delays, in colour clocks after the write cycle: colour 1; PRIOR bits 0-3 and 5: 2; PRIOR bit 4: 2-3; PRIOR bits 6-7: 3-5; P/M graphics 3; P/M position and size 5 ([AHRM] 6.10 Table 18; the 2-3 and 3-5 ranges are temperature and unit dependent: model the first value and record the range). - Players and missiles are positioned by comparing HPOS registers with GTIA's own horizontal counter, which ANTIC resets through the AN lines during horizontal blank ([AHRM] 6.5; the positions of the playfield edges in HPOS units are in that section and are not restated here). - P/M data from DMA: GTIA takes "the first halted cycle within horizontal blank" as the missile fetch and latches the data bus on the cycles it counts from there ([AHRM] 6.5 "Player/missile graphics DMA", p. 146). With GRACTL latching on and ANTIC's P/M DMA off, players load whatever the CPU has on the bus at cycles 2-5, or a display-list byte is taken as missile data and players load at cycles 3-7. GTIA therefore needs the HALT pin and the data bus every cycle. Acid5200 `gtia_phantomdma` tests it. - Collisions accumulate per colour clock until HITCLR; triggers can latch (GRACTL bit 2). - CONSOL on the 5200: bits 0-1 select the keypad port, bit 2 powers the pots, bit 3 is the speaker ([BUCH]; [AHRM] 13.2). Reads return the lines' state. ## POKEY against the line (what the board must get right) Details of POKEY belong to `pokey-python`. The board-level facts: - **Pot scan.** A write to POTGO starts a count that advances once per 15.7 kHz tick (every 114 cycles, POKEY's own divider) up to 228 ([AHRM] 5.9). A full scan therefore takes 228 lines, most of a frame. The BIOS restarts the scan in its VBI (`STA $E80B` at $FCFC in the 4-port ROM) after copying the previous scan's values to shadows ([DANB] `5200BIOS.txt`). A stick position p means the POT input crosses threshold p ticks after POTGO; the board gives POKEY, per pot, the **time** (in cycles since POTGO) at which the input crosses, which is how both the stick and the trak-ball can be expressed. With CONSOL bit 2 low no input ever crosses and POKEY reports 228. - **Fast pot scan** (SKCTL bit 2) counts every cycle, stops at 229 and disables the dump transistors ([AHRM] 5.9): the board must then also model that a pot left charged stays above threshold. - **Keypad.** POKEY's 6-bit scan counter advances at 15.7 kHz, one full scan per 64 lines. With K0 and K5 unconnected each key matches twice per scan, and with debounce off a held key is reported every 32 lines ([AHRM] 13.3 p. 412). The board supplies POKEY with a function from the scan counter value to the KR1 and KR2 pin levels, given the port selected by CONSOL bits 0-1. - **IRQs** reach the CPU through one line; "a minimum 2-3 unhalted cycle delay" exists between IRQST and the CPU's response ([AHRM] 5.7). ## Power-on and reset Unknown beyond the generic: RAM contents undefined ([AHRM] 2.3), ANTIC's counters and POKEY's dividers in no documented state (U8). The BIOS writes 0 to every chip register before use, which hides most of it. Defaults: RAM zero, counters at line 0 cycle 0, POKEY as `pokey-python` defines its reset; each a field on the machine row.