# The PIA, the keyboard, the console keys and the controllers ## 6520 PIA (in this board; no other family board uses one) Facts from [AHRM] 2.5 unless marked; the chip itself from [MCS] (the MCS6520 chapter; its PDF has no text layer, so page citations wait for OCR, U15). - At $D300-$D3FF, four registers (PORTA, PORTB, PACTL, PBCTL) repeated 64 times. Some machines carry a 6820 or 6821 instead, "pin- and software-compatible". - Bit 2 of a control register selects between the data-direction register and the output register at the same address. - **Port A reads back output bits as output AND external level; port B reads back the output latch** whatever the pins do. Caverns of Mars depends on port A's behaviour ([AHRM] 2.9). - **Control lines**: CA2 = SIO motor, CB2 = SIO command (both driven by control bits 3-5: 110 low, 111 high); CA1 = SIO proceed, CB1 = SIO interrupt. Bits 7 and 6 are the CA1/CB1 and CA2/CB2 flags, read-only; **reading the output register (not the DDR) clears both flags**; disabling an interrupt does not clear a pending flag; switching CA2/CB2 to output clears bit 6. Spurious flags arise on output-to-input switches of CA2/CB2 in the conditions [AHRM] 2.5 gives, and the CB2 case is part of normal SIO traffic. - **Reset**: all registers to $00 (DDR selected, every pin an input). On the 800 only at power-on; on the XL/XE also by the Reset key. - **Port B's floating bits** on the XL/XE: [memory-map.md](memory-map.md). - Judges: Acid800 `pia_basic`, `pia_irq` (CA1/CB1 untested by the suite: "a lack of a known method to do so without external hardware"). ## Joysticks and paddles - Directions: PIA port A (ports 1-2) and port B (ports 3-4 on the 400/800 only; the XL/XE use port B for banking). Triggers: GTIA TRIG0-TRIG3 (TRIG2/TRIG3 are wired to board lines on the XL/XE). Paddles: POKEY POT0-POT7 through pokey-python's `rc.PotRC`, as on the 5200, with the pot-scan timing the core already models. - Other controllers ([AHRM] chapter 7): driving controller, keyboard controllers, the CX-85 keypad, light pens and guns (ANTIC's PENH/PENV latched from a trigger line: needs the ANTIC core's light-pen input, [reuse.md](reuse.md)). Proposed: joystick, paddles and keyboard in the first build; the rest later as controller sockets. ## Keyboard (through POKEY) POKEY scans the 64-key matrix itself ([AHRM] 5.8): on a new key it latches the scan code in KBCODE bits 0-5 with Shift and Control in bits 6-7, raises the keyboard IRQ if enabled and holds SKSTAT bit 2 while the key is down; SKSTAT bit 3 follows Shift; repeated presses of the same key leave KBCODE unchanged. Break has its own IRQ. The scan codes are [AHRM] Tables 11-12 (scan-matrix and 130XE layouts). **pokey-python's rung 6 already models the scan counter, debounce, KBCODE with Shift/Control, Break and overrun** (its status table, nine tests); the board supplies the matrix as pokey-python's `key_response` callback, as a5200-python does for the keypad. - The 1200XL adds F1-F4 and Help (codes in Table 11) and the XL/XE OS handles them ([XLA]). - The XEGS keyboard is detachable; GTIA TRIG2 reads 1 when it is connected ([AHRM] 2.8). - A host keyboard map (PC keys to the Atari matrix) is a frontend matter, outside the specification. ## Console keys and the speaker (through GTIA) CONSOL ($D01F) drives and senses four lines: writing 1 to bit n pulls line n low (it then reads 0); bits 0-2 are Start, Select and Option, bit 3 the console speaker ([AHRM] 6.11). On the 5200 the same four lines are outputs selecting controllers; here they are inputs too, so the GTIA core needs the keys' levels as inputs ANDed with its own pull-downs ([reuse.md](reuse.md)). Acid800 `gtia_consol` checks that GTIA can pull the lines low as well as read them. Reset is not a CONSOL key: RNMI on the 400/800, the reset line on the XL/XE ([memory-map.md](memory-map.md)). The 1200XL also has four option jumpers on unused pot lines (J1 on POT4 forces a self-test) and two LEDs on PORTB ([AHRM] 2.8, [XLA]).