# The two MC6821 PIAs, the keyboard, joysticks, DAC and sound Sources: the MC6821 data sheet (two scans pinned: bitsavers' `MC6821.pdf`, the same file williams-python pins, SHA-256 78c85c87..., and the Color Computer Archive's), the TRM's memory map and "PIA" / "Digital to analog" / "Joystick interface" / "Sound output" sections, the MC14529B data sheet, LOM's FF00-FF3F reference, CA's PIA0/PIA1/MC14529 sections with his Dragon 64 measurements. DOC unless tagged. ## The PIA itself (MC6821, shared with williams-python) Per side: data register, data direction register, control register; RS0/RS1 = A0/A1; control bit 2 picks data or direction at the even address; bits 0-1 enable and choose the edge of CA1/CB1; bits 3-5 make CA2/CB2 an input or an output (bits 4-5 = 11: output equal to bit 3, the mode both machines use for their mux selects, cassette motor and sound enable); bits 6-7 are read-only flags, cleared by reading that side's data register (PIA data sheet; williams-python `docs/pia-cmos-watchdog.md` lists the same). IRQA/IRQB are open-drain and level: the 6809 samples them at instruction boundaries. **Shared core.** williams-python (tonight) proposes extracting the 6821 as `m6821-python` after its milestone 4 (its open decision 3); the Atari 400/800's 6520 is the third user and this board the fourth (two PIAs, plus the Dragon). Every user needs the same register model; what differs is wiring, which stays in each board. Recommendation (open decision 3): one core, extracted from whichever board builds it first, pinned by both; this board should not write its own. ## Wiring (CoCo from the TRM; Dragon from CA) PIA0 at FF00-FF03 (mirrored to FF1F; Dragon 64: FF04-FF07 is the ACIA), its IRQA/IRQB to the CPU's IRQ; PIA1 at FF20-FF23 (mirrored to FF3F), its IRQs to FIRQ (TRM control-register bit 0 texts: "IRQ* to CPU" for PIA0, "FIRQ* to CPU" for PIA1). | Line | CoCo 1/2 | Dragon 32/64 | | --- | --- | --- | | PIA0 PA0-PA6 (in) | keyboard rows 1-7; PA0/PA1 also right/left joystick button 1; PA2/PA3 right/left button 2 (LOM) | keyboard rows; PA0 right, PA1 left button | | PIA0 PA7 (in) | joystick comparator | same | | PIA0 PB0-PB7 (out) | keyboard columns 1-8 | keyboard columns and the printer data lines | | PIA0 CA1 | HS from the VDG (63.5 us) | !HS | | PIA0 CB1 | FS from the VDG (16.667 ms) | !FS | | PIA0 CA2, CB2 (out) | SEL1, SEL2: the MC14529B select | same ("CA2 bit 0, CB2 bit 1") | | PIA1 PA0 (in) | cassette data (zero-crossing detector) | same | | PIA1 PA1 (out) | RS-232 data out (printer) | parallel printer STROBE | | PIA1 PA2-PA7 (out) | 6-bit DAC, PA7 the MSB | same | | PIA1 PB0 (in) | RS-232 data in | printer !BUSY | | PIA1 PB1 | single-bit sound (an output only when used) | same | | PIA1 PB2 (in) | RAM size jumper (low = 4K, high = 16K) | Dragon 64: ROM select (32 or 64 mode BASIC); the Dragon 32 ROM sets the SAM for 16K if it reads high | | PIA1 PB3-PB7 (out) | CSS, GM0 & INT/EXT, GM1, GM2, A/G | CSS, GM0, GM1, GM2, !A/G | | PIA1 CA1 | CD: RS-232 carrier detect | printer ACK | | PIA1 CA2 (out) | cassette motor relay | same | | PIA1 CB1 | CART: cartridge interrupt (autostart) | same | | PIA1 CB2 (out) | SNDEN: enables the multiplexer's sound side | same | The keyboard matrices differ between the CoCo and the Dragon (same 7x8 wiring, different key positions: well known, but neither the TRM's matrix figure nor a Dragon matrix has been read yet, U9). The keyboard has no other controller: software scans it. ## Joysticks (analogue) Four potentiometer axes (0-5 V) go through side X of the MC14529B (selected by SEL1/SEL2, always enabled) to a comparator against the DAC; software does successive approximation to six bits and reads the result on PIA0 PA7 (TRM "Joystick interface"; CA). Buttons ground PA0/PA1 (and on later sticks PA2/PA3), shared with keyboard rows 1 and 2 ("a conflict may exist if both are used at once", TRM). The board models a stick as four voltages and two to four switches; the comparator's threshold behaviour at equality is U10. ## The DAC and sound - DAC: six resistors from a CMOS buffer on PIA1 PA2-PA7; TRM: "VOLTAGE = (N x 0.0715) + 0.25", 0.25 V to 4.75 V, filtered by C52. - The MC14529B's side Y, enabled by PIA1 CB2 (SNDEN), selects the sound source by SEL1/SEL2 (TRM Table 4): 00 the DAC, 10 (SEL1=1) the cassette input, 01 (SEL2=1) the cartridge's SND pin, 11 unused. With SNDEN low, PIA1 PB1 is the single-bit source, which must be programmed as an output; it shares the sound bus through 10K, so the two interact. - MEASURED by CA on one Dragon 64 (hardware measurement, tier 1 for that unit; reported on his page, not reproduced by us): | PB1 (single-bit line) configured as | DAC=0 | DAC=63 | tape | cart | | --- | --- | --- | --- | --- | | input | 0.20 V | 4.70 V | 2.30 +/- 0.25 V | 0.00 V | | output, low | 0.18 V | 3.02 V | 1.80 +/- 0.20 V | 0.00 V | | output, high | 1.30 V | 4.70 V | 2.60 +/- 0.25 V | 3.90 V | (Columns as in the page's HTML table, read 2026-11-26: the first header, "Single-bit", labels the rows.) So the single-bit line, when an output, pulls the shared sound bus: low costs a third of the DAC's range at full scale. It is the only measured mixing data found. - Selecting a sound source also moves the joystick axis (one select pair for both sides), so games that read sticks while playing DAC sound hear the effect: the board gets this for free by modelling the mux, not the outcome. ## Interrupt facts the board must keep HS (every line) and FS (every field) reach the CPU only through PIA0's flags and enables; Color BASIC enables FS and not HS (OBSERVED in MAME's `coco`: PIA0's control registers are written 34h at reset, then FF03 = B5h at 1.25 s, FS interrupt enabled on the falling edge; FF01 stays 34h, HS disabled; PIA1's FF23 = 37h, the CART FIRQ enabled on the rising edge, 0.4 ms after reset). The Dragon 32 ROM writes the same values. CART on PIA1 CB1 with a cartridge that ties Q to CART gives a FIRQ storm at the E rate, which the ROM uses to start the cartridge ([cartridge-cassette.md](cartridge-cassette.md)). ## Unknowns here U5 the PIA's output-pin readback and flag timing against E (shared with williams-python's U10); U9 the two keyboard matrices; U10 the comparator at equality and its settling time after a DAC write; U11 the CoCo's sound-bus mixing levels (CA measured a Dragon 64 only).