# The two 6522 VIAs: wiring, what the VIC-20 needs, and the shared core Tags as in [memory-map.md](memory-map.md). **VIA-T** is the R6522 data sheet as text (Kontros, corrected by Wolfgang Lorenz); **SY** Synertek's SY6522 sheet (scans). vectrex-python's [via.md](../../vectrex-python/docs/via.md) pins the Rockwell, MOS 1977, Synertek and WDC sheets as PDFs; this repository does not duplicate those pins. ## Wiring (PRG pp. 176-177, SCH-E) | | VIA1, $9110 (UDE8, CS1 = A4) | VIA2, $9120 (UDE7, CS1 = A5) | | --- | --- | --- | | port A | PA0 serial CLK in, PA1 serial DATA in, PA2-PA4 joystick 0-2, PA5 light pen / fire, PA6 cassette switch sense, PA7 serial ATN out | keyboard rows (input) | | port B | user port PB0-PB7 (RS-232 lines by the KERNAL's convention) | keyboard columns (output); PB3 cassette write; PB7 joystick 3 (right) | | CA1 | RESTORE key (pulled up, R26) | cassette read | | CA2 | cassette motor | serial CLK out | | CB1 | user port (RS-232 received data) | serial SRQ in | | CB2 | user port (RS-232 transmitted data) | serial DATA out | | IRQ out | **CPU NMI** (RESTORE and the KERNAL's RS-232 timing: PRG p. 244, "NMI ... just like RUN/STOP-RESTORE"; RS-232: "The 6522 generates NMI requests for RS-232 processing", PRG p. 251) | **CPU IRQ** (timer 1 the 60 Hz jiffy, PRG p. 177 and p. 243) | The NMI is edge-triggered on the 6502, so the board detects VIA1's IRQ output going low and requests one NMI per edge (6502-python's `request_non_maskable_interrupt` is a latch). The serial lines go through 7406 inverters on SCH-E: what software writes and what the bus sees are inverted; drive-1541.md has the socket. Initial values the KERNAL writes (MAME 0.285 trace, OBSERVED, mame-oracle.md): both IERs $7F (all off), ACRs $40 (T1 free-run, no PB7), PCRs $FE (VIA1) and $DE (VIA2), VIA1 DDRB $00 and DDRA $80, VIA2 DDRB $FF and DDRA $00, VIA1 IER $82 (CA1: RESTORE), VIA2 IER $C0 (T1), VIA2 T1 = $4289 (NTSC). ## What the VIC-20 reaches that the Vectrex does not The Vectrex uses one VIA with T1 one-shot + PB7, T2, shift-register mode 6, manual CA2 and CB2, and CA1 edges (vectrex-python via.md). The VIC-20 adds: - **two instances** with different IRQ destinations; - **T1 free-run** as the system tick and, in games, raster interrupts timed by T1 and T2 (`via_t1irqack` reproduces the game *Bandits*' technique); - **port A with and without handshake** ($xxx1 vs $xxxF), CA1/CB1 as edge inputs, CA2/CB2 as manual outputs driving the serial bus and the cassette motor; - **the shift register** in every mode (`via_sr`: all modes with hardware dumps) and CB1 external clocks on the user port; - **PB7 output** (via_pb7, "reference data checked on my vic20"); - **port reads with outputs fighting inputs** (`joystick`: VIA2 PB as output reads values that jump between 0 and 247, and 247 and 255, on hardware: the keyboard matrix and the joystick pull lines against the outputs); - **the 1541 drive's two VIAs** if the drive is emulated (drive-1541.md). ## Judges (tier, what they decide) | Judge | Tier | Decides | | --- | --- | --- | | R6522 / MCS6522 / SY6522 data sheets | 2 | the programmer's model, timer pulse lengths, shift modes | | `VIC20/via_pb7` | 3 (readme: "reference data checked on my vic20") | PB7 vs ACR bits 7/6 before, during and after a one-shot | | `VIC20/via_t1irqack` | 3 ("confirmed on my vic20 (MOS VIAs)", "confirmed to work the same on synertek VIA") | writing the T1 high latch clears the T1 flag (C4) | | `VIC20/viavarious` (via1-via21 with `*ref.bin`) | 3 where the reference was measured ("at the top ... the measured data, at the bottom the reference data") | timers, IFR timing, one-shot/free-run corners | | `VIC20/via_sr` (`dump*.bin`) | 3 | shift modes and IFR timing per mode | | `VIC20/via_wrap` | detector (no hardware statement) | $FFFF seen for one cycle at wraparound | | `VIC20/via_defaults` | partly 3 (marks which values are "definately the default reset values") | reset state | | MAME `6522via.cpp` | 5 (BSD-3-Clause; not opened tonight: closed box by the brief) | detector | | VICE `viacore.c` | 5, GPL-2.0+: closed box, never fetched | detector through emu-tools-gpl if ever built | The VICE tests need a VIC-20 to run on: this board **is** the harness they were waiting for (vectrex-python's open decision 3 named exactly this). They become the VIA core's gate once the board reaches milestone 1. ## Conflicts - **C4 T1 high-latch write and the T1 flag.** Synertek's notes (as quoted by the test's readme) say writing the high latch does not clear the T1 interrupt flag; `via_t1irqack` was confirmed on MOS and Synertek VIAs in real VIC-20s to rely on it clearing, and the game *Bandits* depends on it. Proposal: clears (hardware result); vectrex-python's via.md cites the same behaviour from VICE's comments. - The shift register's CB1-clock bug (SY p. 8): reachable on the VIC-20 through the user port and the serial SRQ line; the KERNAL bit-bangs the serial bus instead of using the shift register (U12: confirm by reading the KERNAL's serial routines as a program). ## Where the VIA lives: the shared core **Aubrey decided on 2026-11-12** (vectrex-python handoff-brief, decision 2): the 6522 "moves to `mos6522-python` when a second family machine needs one". The VIC-20 is a second machine; tonight's c64-python Phase 0 names the 1541 as one too and proposes the name `via6522-python`, and a BBC Micro Phase 0 may propose the same. **vectrex-python has no VIA code yet** (Phase 0 only: no `via.py` exists), so there is nothing to extract with history: the core is new. Proposal (open decision 2): build **`mos6522-python`** (the name Aubrey already gave; package `mos6522_python`) as a chip core with pin callbacks and a per-cycle `tick()`, variants as rows (`MOS6522`, `R6522`, `SY6522`, later `W65C22`), its gate the data sheets plus the VICE VIC-20 tests run through vic20-python as a test-only harness. Users: vectrex-python (1), vic20-python (2), c1541-python (2), bbcmicro-python (2), and later PET, Oric, Apple Mockingboard. Until it exists, vic20-python writes nothing VIA-shaped of its own.