# Start here: the Atari 2600 as a board author needs it The 2600 (Atari Video Computer System, CX2600, 1977) is three chips and a cartridge: a **6507** (a 6502 die in a 28-pin package: 13 address lines, no IRQ, no NMI), the **TIA** (Television Interface Adaptor: picture, sound, two fire buttons and four paddle inputs), and the **RIOT** (MOS 6532: 128 bytes of RAM, two 8-bit ports, an interval timer). There is no frame buffer, no interrupt and no DMA. The program draws every line as the beam crosses the screen ("racing the beam"), and the console's only synchronisation aid is WSYNC, which pulls the CPU's RDY line low until the next line begins. That makes the board small and the timing everything. A 2600 board is correct when the TIA is right to the **colour clock** and the CPU's bus is right to the **cycle**; 6502-python already gives the second. Sources, for everything below unless a line cites otherwise: Steve Wright's *Stella Programmer's Guide* (1979, the official manual), Andrew Towers' *TIA Hardware Notes* (derived from the TIA schematics), the MOS 6532 and 6507 data sheets, and the TIA schematics themselves. Exact file names are in `scripts/pinned_files.json`. No emulator's GPL source is a source for this document ([restart.md](restart.md); what was struck is in [provenance.md](provenance.md)). ## The machines One board, with the differences as data (famicom-python's shape): | Machine | TIA | Colour clock | CPU (÷3) | Lines (by convention) | Phase | | --- | --- | --- | --- | --- | --- | | NTSC VCS (CX2600, CX2600A, 2600 Jr.) | TIA, NTSC colour | 3.579545 MHz | 1.193182 MHz | 262 | 1: reference | | PAL VCS | TIA, PAL colour | 3.546894 MHz | 1.182298 MHz | 312 | machine data from the start, certified later | | SECAM VCS | TIA, SECAM colour (8 colours) | as PAL | as PAL | 312 | machine data, later | | Atari 7800 in 2600 mode | same TIA + RIOT, MARIA off | | | | a future board ([handoff-brief.md](handoff-brief.md)) | The console models (heavy sixer, light sixer, four-switch, Jr.) differ in switches and case, not logic. What *does* differ between consoles is the TIA die revision: a handful of games show it (see "TIA revisions as data" in [undocumented-behavior.md](undocumented-behavior.md)). Each is a field on the machine row, default off, with its source. **Frames are the program's.** The TIA has no vertical counter. A frame is whatever lies between the program's VSYNC pulses: NTSC games aim for 262 lines (3 VSYNC, 37 VBLANK, 192 picture, 30 overscan), but Combat makes 263, Asteroids 265 (MAME measures both, [mame-oracle.md](mame-oracle.md)), and some games vary from frame to frame. The board produces lines and VSYNC/VBLANK as signals; the "television" that turns them into frames is a separate, replaceable stage. ## Clocks Everything is counted in **colour clocks** (the TIA's oscillator). One CPU cycle is three colour clocks: the TIA divides its oscillator by 3 and feeds the 6507 its φ0. One line is 228 colour clocks = 76 CPU cycles exactly, so the CPU/TIA phase is the same on every line (RSYNC can change it: [timing.md](timing.md)). Of the 228, the first 68 are horizontal blank and 160 are picture. The audio runs off the same clock, two ticks per line (31.4 kHz NTSC). The board's master time unit is therefore the colour clock, and a CPU cycle is three of them. famicom-python's pattern carries over unchanged: **each 6502-python bus callback is one CPU cycle**; the board advances the TIA by three colour clocks and the RIOT by one cycle inside it, never by `step()`'s returned count on top. ## The CPU side ### Memory map (13 bits: the 6507 has A0–A12) Decoding uses only A12, A9 and A7: | A12 | A9 | A7 | Device | Registers | | --- | --- | --- | --- | --- | | 1 | x | x | cartridge ($1000–$1FFF) | 4 KB window; bank switching extends it ([cartridge.md](cartridge.md)) | | 0 | x | 0 | TIA | write: A0–A5 (45 registers in $00–$2C); read: A0–A3 (14 in $0–$D) | | 0 | 0 | 1 | RIOT RAM | 128 bytes at $80–$FF, mirrored at $180–$1FF (the stack) | | 0 | 1 | 1 | RIOT I/O and timer | $280–$297 and mirrors | Everything else is a mirror. Programs are written for $F000–$FFFF (or $D000, $B000…) and the CPU keeps a 16-bit PC; only the board masks. Two consequences games use: the stack at $1FF is RAM $FF, and a push into page 1 below $180 lands in a TIA mirror (a stack pointer at $02 makes PHA a WSYNC strobe). ### The data bus (a board responsibility) TIA reads drive **only D7 and D6** (D7 alone for most); D5–D0 float and read back whatever was last on the bus, normally the last byte of the instruction (the operand that held the address). Games that read `BIT CXxx` or `LDA INPTx` and then test other bits depend on it, deliberately or not. The board keeps a data-bus latch (last byte driven by anyone) and composes TIA reads from it. Real chips vary (some pull the floating lines towards 0, Stella issue #173); Stella's user interface offers driving the unused TIA pins randomly as a developer option to find such games. Reads of empty space ($1xxx with no cartridge, unmapped mirrors) return the latch. Recorded in [undocumented-behavior.md](undocumented-behavior.md). ### The RIOT ($280–$297) | Address | Read | Write | | --- | --- | --- | | $280 SWCHA | port A: joysticks (P0 in D7–D4, P1 in D3–D0, active low), keypad rows, driving-controller Gray code | port A output bits (keypads drive rows) | | $281 SWACNT | port A direction | port A direction (1 = output) | | $282 SWCHB | port B: console switches (D0 reset, D1 select, D3 colour/B&W, D6/D7 difficulty P0/P1) | output bits (unused, readable back) | | $283 SWBCNT | port B direction | port B direction | | $284/$286 INTIM | timer value; clears the timer flag | | | $285/$287 TIMINT | D7 timer flag, D6 PA7 edge flag; clears the PA7 flag | | | $294–$297 TIM1T / TIM8T / TIM64T / T1024T | | load timer, interval 1/8/64/1024 cycles | | $284–$287 (A4 = 0, A2 = 1) | | PA7 edge select (A0: rising/falling); exact decode from the 6532 data sheet | The timer decrements once per interval; after it passes zero it sets its flag and **counts down once per cycle** from $FF, so a program reading late still sees how late it was. The RIOT's IRQ pin is not connected (the 6507 has none). The exact edge rules (the read on the cycle of underflow, the write in the same cycle) are in [timing.md](timing.md). ### Controllers and switches - **Joystick**: four directions on SWCHA, fire on INPT4/INPT5 (TIA D7, active low). VBLANK D6 latches INPT4/5 low once pressed until VBLANK D6 is cleared. - **Paddles**: two per port, read through INPT0–INPT3 as the time a capacitor takes to charge through the pot. VBLANK D7 grounds ("dumps") the capacitors; after release, D7 of INPTx goes high when the charge crosses the threshold, so the line on which it flips is the position. Buttons are on SWCHA. The board models the RC curve from the console schematic's resistor and capacitor ([timing.md](timing.md) "Inputs"); MAME (BSD-3 `tia.cpp`) uses a straight line ([mame-oracle.md](mame-oracle.md)). - **Keypad** (Keyboard Controller, Kid's Controller, Video Touch Pad): the program drives one of four rows on SWCHA and reads three columns on INPT0/1/4 (or 2/3/5); the column lines need time to settle after a row change. - **Driving controller**: two-bit Gray code on SWCHA pins 1-2 (Indy 500; milestone 6, `controllers.DrivingController`, from nocash). - **Booster Grip**: a joystick plus two buttons that charge INPT0/INPT1 (or INPT2/INPT3) like a paddle at 0 Ω (milestone 6, from nocash's 3-button adapter). - **SaveKey**: a 24LC256 I²C EEPROM bit-banged through SWCHA pins 3-4 of the right port (milestone 6, `savekey.py`; 18 homebrew dumps). - Console switches on SWCHB; the 2600 Jr. and four-switch consoles still read the same bits. SECAM consoles have no colour switch: SWCHB D3 is grounded (nocash). Not built: Kid Vid, MindLink, light gun (reads the TIA's own INPT4 on the beam), AtariVox speech. ### Power-up and reset The 6507's reset is the 6502's (7 cycles, three stack reads, vector at $FFFC → $1FFC). There is no reset on the TIA or RIOT pins: the console's reset "switch" is just SWCHB D0, which the program polls. So "reset" in this board means power-on only, plus the switch as an input. At power-on the TIA's registers, the RIOT's RAM and timer, and the CPU's A/X/Y/S are undefined, and the cartridge's starting bank is arbitrary. Games mostly clear everything themselves; the few that don't are why Stella randomises these by option. The board takes them as machine parameters (fill values, start bank), like famicom-python's power-on fields. ## The TIA's picture The TIA draws, per colour clock, one of: the background, the playfield (20 bits per half line, 4 clocks each, the right half repeated or mirrored), two players (8-pixel graphics, with copies and stretch modes from NUSIZx), two missiles and a ball (1–8 clocks wide). Each moveable object has its own position counter; the program places it by strobing RESxx at the moment the beam is where it wants it, then nudges it with HMxx + HMOVE, which sends extra clocks to the counters during the next horizontal blank and **lengthens that blank by 8 clocks** (the "HMOVE comb" of black bars at the left). Priority (CTRLPF D2), score mode (D1), vertical delay (VDELxx), reflection (REFPx) and fifteen collision latches complete it. The register set and its semantics are in the Programmer's Guide; the schematic-level truth (counters are LFSRs, decodes happen on specific counter states, writes land on specific clock phases) is in Towers' notes, and that is the level this board needs: [timing.md](timing.md) has the object pipeline. ### Output format - **Picture**: per colour clock, a 7-bit colour (4-bit hue, 3-bit luminance; D0 of COLUxx is ignored) or "blank". The board emits each line as 228 values (or the 160 picture values plus HBLANK/VBLANK flags) and the VSYNC level per clock; frames are cut by a separate `television` stage (line count, vertical hold behaviour when VSYNC is irregular). Palette to RGB in the frontend: NTSC, PAL and SECAM tables from the pinned sources, labelled with their origin (none is hardware-measured to the family's bar: [validation.md](validation.md)). - **Audio**: two channels, each a 4-bit volume times a 1-bit waveform from AUDC's polynomial counters and AUDF's divider, ticked twice per line. `take_samples()` floats, mixed through the console's resistor network as the schematic draws it (a fit, [timing.md](timing.md) "Audio"), at the TIA's 31.4 kHz tick rate, then resampled. The DPC cartridge mixes its own music in. ## Suggested module shape ``` src/a2600_python/ machine.py machines as data (NTSC/PAL/SECAM, TIA revision quirks, power-on) board.py the 13-bit bus, the data-bus latch, one cycle per callback, WSYNC as a stretched read, the cartridge on every cycle tia/ video.py horizontal counter, objects, playfield, HMOVE, collisions audio.py two channels behind a pin-shaped interface (extractable) inputs.py INPT0-5: dump/latch logic, paddle RC riot.py RAM, ports, timer, PA7 edge (extractable) cartridge/ schemes behind the connector; no imports from the board controllers.py port devices: joystick, paddles, keypad, driving, ... television.py lines + VSYNC -> frames; never needed by the board state.py, debug.py, trace.py, frontend.py, __main__.py ``` The family conventions (machines as data, `take_samples`, `frontend.py` on pygame-ce as an optional extra, `python -m a2600_python ROM --frames N --png out.png --wav out.wav`) come from sms-python and famicom-python.