# The VIA, SCC, RTC, mouse, keyboard, SCSI and ADB ## VIA (SY6522 / R6522) One 6522 at $EFE1FE, upper byte, registers 512 bytes apart, phase 2 = E clock (783.36 kHz), accessed with /VPA, IRQ on level 1 (DOC: IM3 III-39..43; GMFH ch. 4). IM3 names a Synertek SY6522; MAME's `macse` names a "Rockwell R65NC22". The core is the shared `mos6522-python` (not yet built); this board owns only the wiring: | Port bit | 128K / 512K / Plus | SE | | --- | --- | --- | | PA7 (in) | vSCCWReq: SCC /W/REQA and /W/REQB wire-ORed | the same | | PA6 | vPage2: 0 = alternate screen | the same | | PA5 | vHeadSel: the drive's SEL | the same | | PA4 | vOverlay: 1 = ROM overlay | vDriveSel: 0 = upper internal drive | | PA3 | vSndPg2: 0 = alternate sound buffer | vSync: synchronous modem clock on channel A | | PA2-0 | sound volume (111 loudest) | the same | | PB7 | vSndEnb: 0 = sound on (also timer 1's PB7 output) | the same | | PB6 (in) | vH4: 1 in horizontal blanking | **out**: SCSI IRQ mask | | PB5, PB4 (in) | mouse Y2, X2 quadrature | **out**: ADB state ST1, ST0 | | PB3 (in) | vSW: 0 = mouse button down | vFDBInt: 0 = ADB interrupt | | PB2-0 | RTC enable (0 = on), clock, data | the same | | CA1 | vertical blanking (IFR bit 1) | the same | | CA2 | RTC one-second (IFR bit 0) | the same | | CB1, CB2, SR | keyboard clock and data through the shift register (IFR bits 4, 3, 2) | ADB through the shift register | (DOC: IM3 III-39..42; GMFH Tables 4-2, 4-3, 4-11, 4-12.) ROM initial values: DDRA $7F, ORA $7B; DDRB $87, ORB $07 (DOC: IM3 III-43..44). OBSERVED (MAME): the first two VIA writes of the Plus boot are $87 to DDRB and $87 to ORB, at cycles 268 and 289. The PCR's fields: bits 5-7 keyboard data, 4 keyboard clock, 1-3 one-second, 0 VBL (IM3 III-40). The 6522's own behaviour (timer reload timing, the PB7 toggle, the shift-register modes, the IFR/IER rules) belongs to the core and its data sheets (R6522, SY6522). ## SCC (Z8530) - At $9FFFF8 (read base) and $BFFFF9 (write base); offsets bCtl 0, aCtl 2, bData 4, aData 6 (DOC: IM3 III-25, III-44); PCLK and both RTxC 3.672 MHz (the baud-rate generator source, divided by 16 normally); channel A = modem port, B = printer port; level-2 interrupts; "don't access the SCC chip more often than once every 2.2 µsec" (DOC: IM3 III-22..25). On the SE, VIA PA3 can route channel A's RTxC from the modem port's GPi pin (DOC: GMFH Table 4-3). - The mouse's X1 and Y1 go to **DCDA and DCDB**: the SCC's external/status interrupts on DCD edges carry the mouse (below). /W/REQ A and B reach VIA PA7. - The register model, the pointer-write protocol and the interrupt daisy chain come from Zilog's technical manual (SCCTM). Model level: registers, baud-rate generator timing, the DCD/CTS external-status latch and interrupt; serial data through a socket (a host pipe or LocalTalk later). - Other family users of a Z8530: none built; the Apple IIgs (apple2-python's later board) has one; other family machines were not surveyed for it. The chip rule keeps it in this board for now. ## Mouse Quadrature: X1/Y1 (interrupt signals) to the SCC's DCDA/DCDB, X2/Y2 (quadrature) to VIA PB4/PB5, button to PB3 (0 = down). On a rising X1 edge, X2 low = left, high = right; on a falling edge the reverse; likewise Y (down/up). The button is polled at each VBL (DOC: IM3 III-27..28). The board's mouse socket takes host motion and produces the four signals at a rate the SCC can follow (a model choice: one edge per N CPU clocks, recorded in save states). ## Real-time clock A custom chip (343-0040 on the 128K/512K, 343-0042 on the Plus and SE per MAME's device lists; IM4 IV-251: "a new custom chip" with "additional parameter RAM reserved by Apple"): a 4-byte seconds counter, 20 bytes of battery-backed parameter RAM on the old chip, a one-second output to VIA CA2 (DOC: IM3 III-36..38). - Serial through VIA PB0 (data), PB1 (clock, always driven by the CPU), PB2 (enable, active low; raising it aborts). Bytes are MSB first; the CPU sets the bit and raises the clock to send; for a read it turns PB0 to input after the command, lowers the clock and reads (DOC: IM3 III-37). - Commands (z = 1 read, 0 write): z0000001, z0000101, z0001001, z0001101 seconds 0-3; 00110001 test (write only), 00110101 write-protect (write only; bit 7 set blocks all writes); z010aa01 PRAM $10-$13; z1aaaa01 PRAM $00-$0F (DOC: IM3 III-38). The Plus's extended PRAM (256 bytes on the 343-0042 by community knowledge) has no Apple command table in the documents read: U13. - PRAM contents and the clock are host state (saved with the machine; set from the host clock or fixed for determinism: a host choice, recorded). ## Keyboard (128K/512K/Plus) - RJ-11: ground, clock, data, +5 V. The keyboard drives the clock; data is bidirectional; 8 bits MSB first. Keyboard to Mac: eight 330 µs clock cycles (160 low, 170 high), data set 40 µs before the falling edge, sampled by the VIA on the rising edge. Mac to keyboard: eight 400 µs cycles (180 low, 220 high), the Mac sets data on the falling edge, the keyboard reads 80 µs after the rising edge. Only the Mac starts a transfer (by pulling data low) (DOC: IM3 III-30). - Commands: Inquiry $10 (a transition, or Null $7B after 1/4 s), Instant $14, Model Number $16 (resets the keyboard; reply bit 0 = 1, bits 1-3 model, 4-6 next device, 7 another device), Test $36 (ACK $7D / NAK $77). Key transitions: bit 7 = up, bit 0 = 1. The keypad answers $79 and passes the keyboard through (DOC: IM3 III-30..31); the Plus keyboard imitates the keypad ($79, or $71 then $79 for shifted keypad keys) (DOC: IM4 IV-250). - MCUs: the M0110 keyboard holds an **Intel 8021** (DOC: IM3 III-29; MAME `mackbd_m0110`: "Intel 8021 @ 3.00 MHz", ROM `ip8021h_2173.bin`); the Plus's M0110A an **Intel 8048** per MAME (`341-0332-a.bin`, "Intel 8048 @ 6.00 MHz"); IM4 says only "a microprocessor". [mcs48-python](../../mcs48-python) lists both parts. **Proposed: a protocol-level keyboard first** (scan codes from IM3 Figure 9 and IM4 Figure 3, timing above), the 8021/8048 at chip level as a later option behind the same socket (open decision 3). ## SCSI (Plus): NCR 5380 - At $580000 (reads, even) / $580001 (writes, odd); register = address bits 6-4; bit 9 asserts DACK for pseudo-DMA on the data registers ($580260 read, $580201 write). Reads: 0 current data, 1 ICR, 2 MR, 3 TCR, 4 current bus status, 5 bus and status, 6 input data, 7 reset parity/interrupt. Writes: 0 output data, 1 ICR, 2 MR, 3 TCR, 4 select enable, 5 start DMA send, 6 start DMA target receive, 7 start DMA initiator receive (DOC: IM4 IV-252..254). - **The 5380's IRQ is not connected on the Plus**; DRQ is not used as DMA: the CPU polls, "142K bytes per second for nonblind transfers and 312K bytes per second for blind" (DOC: IM4 IV-252). On the SE the IRQ reaches level 1 through VIA PB6's mask, and the BBU adds hardware handshaking (DOC: GMFH Table 4-12, ch. 3). Blind transfers rely on the target keeping up: what happens when it does not (the SE's /DTACK hold-off versus the Plus's nothing) is U14. - Devices (hard disks, CD-ROM) live behind a `ScsiTarget` socket; a raw-image hard disk first. The register model from NCR5380 (the design manual). ## ADB (SE row, later) The SE replaces the keyboard port with ADB through a transceiver MCU (342S0440-B) talking to the VIA's shift register with two state lines (PB4/PB5) and an interrupt (PB3) (DOC: GMFH Table 4-12, ch. 8). MAME runs it as HLE. The transceiver's identity and protocol timing are U5; the GMFH ch. 8 tables and Snow's `adb/` are the sources to read when the SE row starts. ## Seeded-fault candidates The VIA on the low byte; vH4 with inverted polarity; the RTC accepting a byte with fewer than 8 clocks; the write-protect bit ignored; X2 sampled on the wrong DCD edge; the keyboard's model reply without bit 0; SCSI reads at odd addresses decoded as reads; the 5380 IRQ wired on the Plus.