# The 6821 PIAs, the CMOS RAM and the watchdog ## The MC6821 PIA Three on every first-generation board, one more per extra sound board: | PIA | Port A | Port B | Control lines | IRQ | Evidence | | --- | --- | --- | --- | --- | --- | | ROM board (C80C-C80F; Defender CC0x) | coin door switches (input) | sound command (output) and diagnostic LEDs | CA1 COUNT 240, CA2 coin door slam, CB1 VA11, CB2 a sound handshake "not implemented in DEFENDER" | IRQA and IRQB both to the CPU's IRQ (ORed) | DEF-T "ROM board PIA/decoder", "MPU"; SR-HW | | Widget / interface (C804-C807) | player controls through two multiplexers | more controls, the multiplexer select on later games | game-specific | not used for interrupts on the first generation | SR-HW; DEF-T "Interface board" | | Sound board (0400-0403) | the MC1408 DAC (output) | the command from the main board (input) | CB1 the command strobe; CA2/CB2 to the speech module on Sinistar | IRQA and IRQB joined to the 6808's IRQ | SIN-DS p. 17; SR-HW | What the board must model, from the data sheet (DOC: PIA): - Per side: a data register, a data direction register and a control register; RS0-RS1 pick the register, control bit 2 chooses between data and direction at the data address. - Control bits 0-1 enable the CA1/CB1 interrupt and choose its edge; bits 3-5 set CA2/CB2 as an input (with edge and enable) or an output (handshake, pulse, or set/reset by bit 3); bits 6 and 7 are the read-only interrupt flags. - **The flags clear when the CPU reads that side's data register.** IRQA/IRQB stay asserted until then, and the 6809's IRQ is level-sensitive, so the core must resample IRQ at every instruction boundary (m6809-python's timing.md). - Reset clears every register, making all lines inputs (DEF-T "Reset circuit": "forces all PIA ports to inputs"). - What reading a port bit programmed as an output returns, and the timing of flag setting against E, are where MAME's behaviour and the data sheet must be compared (U10). **Chip rule.** The 6821 is also on the Atari 400/800 board as a pin- and software-compatible 6520 (atari8bit-python's `docs/pia-keyboard-console.md`, which says "no other family board uses one", now no longer true), and on Williams's pinball and second-generation boards. This board makes it the second family user: the extraction is open decision 3. Until decided it lives here behind a pin-shaped class (`Pia6821(read_port_a, write_port_a, ..., irq_a, irq_b)`) that does not reach into the board. ## CMOS RAM - Later boards: 1K x 4 bits at CC00-CFFF, battery-backed (Bubbles 8 bits wide); CC00-CCFF write-protected while the coin door is shut (SR-MAP; SR-HW). Reads of a 4-bit RAM return undriven upper bits (U11: what value; Bubbles reads 8). - Defender: 256 x 4 at C400-C7FF; C600-C7FF (adjustments) protected while the coin door is shut, C400-C5FF (audits) always writable (DEF-T "CMOS RAM"). The RAM is disabled by CR during reset so a falling supply cannot corrupt it (DEF-T). - A fresh (blank) CMOS makes Robotron and Joust restore factory settings and wait for the Advance switch with interrupts masked (OBSERVED: m6809-python's trace; Sinistar too in this phase's run, [mame-oracle.md](mame-oracle.md)). The board ships no CMOS image: a blank RAM (its power-on value is U11) and the player's saved one, kept by SHA-1 of the romset. ## Watchdog - A counter of vertical scans, cleared by writing 39h (Defender: 38h or 39h) to CBFF (Defender C3FF with page 0); after 8 scans uncleared, about 133 ms, it pulses WDR into the reset gate (DEF-T "Watchdog circuit"; SR-HW). A pad on the board disables it (DEF-T). - Defender's program writes 38h to **C3FC** (OBSERVED), so the watchdog decode ignores the low address bits; the decoder gates on data bits as well (DEF-T: "gated with the MPU Address and Data bits"): the exact data pattern accepted is U13. - The watchdog's reset resets the CPU and the PIAs; whether it resets the video counter (CR) is U13. ## Reset The reset circuit (DEF-T "Reset circuit") holds RESET about one second at power-up, and CR follows on the next rising E edge, synchronising the video counter and the 6 MHz divider to E. The board's power-on is: everything held, then CR released in phase with E.