POKEY's Serial Port: SKCTL, SKSTAT, SERIN and SEROUT Explained
POKEY is best known for sound, but it is also the Atari's serial port: every disk sector, cassette block and printer byte passes through its shift registers. The same chip scans the keyboard and reads the paddles. A handful of registers control all of this, chiefly SKCTL (write) and SKSTAT (read), which share address $D20F. Most programs never touch them, because the OS's SIO routines do it all. But custom loaders, high-speed SIO code, cassette tricks and anyone debugging odd noises need to know what they do. This guide is a practical reference to all of them.
What you'll learn: every bit of SKCTL and SKSTAT, initialisation mode and why it freezes RANDOM, the serial clock modes and the divisors for 600 and 19,200 baud, how input and output work, what overrun and framing errors mean, and the traps.
What you need: nothing for reading. The small BASIC test in section 3 was run in atari800. For the SIO protocol itself see SIO Explained, and for faster transfers High-Speed SIO.
1. The registers
| Address | Read | Write |
|---|---|---|
| $D20A (53770) | RANDOM | SKRES: reset SKSTAT bits 5–7 |
| $D20D (53773) | SERIN: last received byte | SEROUT: byte to send |
| $D20E (53774) | IRQST: interrupt status | IRQEN: interrupt enable |
| $D20F (53775) | SKSTAT: serial and keyboard status | SKCTL: serial and keyboard control |
The OS keeps a shadow of SKCTL called SSKCTL at 562 ($232). In our test it held 3 after boot: keyboard debounce and keyboard scan on, nothing else.
2. SKCTL bit by bit (write)
| Bit | Value | Function |
|---|---|---|
| 0 | 1 | Keyboard debounce |
| 1 | 2 | Keyboard scanning |
| 2 | 4 | Fast pot scan: paddles complete in two scan lines instead of a frame, less accurately |
| 3 | 8 | Two-tone mode: serial output sent as two audio tones, used for cassette |
| 4–6 | 16–64 | Serial clock mode (section 4) |
| 7 | 128 | Force break: hold the serial output at 0 |
Two-tone mode is how the cassette handler produces its mark and space tones. The Operating System User's Manual notes that POKEY is sometimes left in this mode after cassette I/O, causing a whistle, and that writing 3 to SKCTL (POKE 53775,3) cures it.
3. Initialisation mode
Clearing both bits 0 and 1 does more than switch off the keyboard. It puts POKEY into an initialisation mode that holds these in reset: the 15 kHz and 64 kHz clocks, all the polynomial noise generators including RANDOM, the serial input and output state machines, and the keyboard scan. Sound channels clocked from 15 or 64 kHz stop counting, and RANDOM locks at $FF. It does not reset IRQEN/IRQST, KBCODE, SERIN, the AUDF/AUDC/AUDCTL registers or the timers' current counts.
That is easy to see from BASIC:
10 REM POKEY SKCTL: INITIALISATION MODE FREEZES RANDOM
20 ? "SSKCTL SHADOW (562) = ";PEEK(562)
30 POKE 53775,0:R1=PEEK(53770):R2=PEEK(53770):POKE 53775,PEEK(562)
40 ? "RANDOM IN INIT MODE: ";R1;" ";R2
50 ? "RANDOM AFTERWARDS: ";PEEK(53770);" ";PEEK(53770)
In init mode, RANDOM read 255 both times. After the shadow value was written back, it returned to random values.
Programs use init mode deliberately in two ways. Writing $00 to SKCTL is the documented way to reset all of POKEY's serial functions: bits 4–6 at 0 reset the serial clock flip-flops, bits 0–1 at 0 reset the state machines and flush any queued output byte, and bit 3 at 0 resets the two-tone flip-flop. Leaving init mode at a chosen moment also restarts the 15 and 64 kHz clocks at a known point in the scan line for precisely known timing.
4. Serial clock modes
Bits 4–6 choose where the input and output bit clocks come from. In summary:
| Bits 6–4 | Input clock | Output clock |
|---|---|---|
| 000 | External | External (also resets the clock flip-flops) |
| 001 | Channels 3+4, asynchronous | External |
| 010 | Channel 4 | Channel 4 (driven out on the bidirectional clock line) |
| 011 | Channels 3+4, asynchronous | Channel 4, asynchronous |
| 100 | External | Channel 4 |
| 101 | Channels 3+4, asynchronous | Channel 4, asynchronous |
| 110 | Channel 4 | Channel 2 |
| 111 | Channels 3+4, asynchronous | Channel 2 |
Standard half-duplex SIO uses 001 to receive and 010 to transmit. Oscilloscope captures of real SIO traffic show SKCTL = $23 while sending and $33 while receiving at 19,200 baud. The timers run at twice the baud rate, because the serial clock toggles each time the timer expires. For precision, channels 3 and 4 are linked into a 16-bit counter on the 1.79 MHz clock. The divisors are $05CC for 600 baud (cassette) and $0028 for 19,200 baud (disk). Because of rounding at high frequencies, the real SIO rate is about 19,040 baud. The fastest internally clocked rate is 128 kilobaud.
Peripherals don't have to use the clock lines. The 810 drive, for example, ignores them and times bits with software loops.
5. Receiving and sending
Input. Each incoming frame (start bit, eight data bits from least significant up, stop bit) is shifted in, and the eight data bits are copied to SERIN, raising the serial input ready interrupt (IRQST/IRQEN bit 5, vector VSERIN at $020A). SERIN always holds the last byte received, error or not, and reading it has no side effects.
Output. Writing SEROUT queues one byte. When the output shift register takes it, the serial output ready interrupt fires (bit 4, VSEROR at $020C), and you can queue the next byte. When the last bit has gone, serial output complete fires (bit 3, VSEROC at $020E). Only one byte can be waiting. Writing SEROUT twice quickly replaces the waiting byte. The OS IRQ handler checks these three sources first and jumps through the vectors.
Polled operation. For rates too high for interrupts, enable the serial interrupt in IRQEN but keep the CPU's interrupts disabled (SEI), and poll IRQST. The status bit still sets.
6. SKSTAT bit by bit (read)
Apart from bit 4, which reads the raw line level, each bit reads 0 when its condition is true:
| Bit | Meaning |
|---|---|
| 7 | Serial input framing error: the stop bit was not 1. The byte is still delivered |
| 6 | Keyboard overrun latch |
| 5 | Serial input overrun latch |
| 4 | Raw state of the serial input line, read directly. The OS uses it to measure cassette speed before a block |
| 3 | SHIFT key held |
| 2 | Last key still held |
| 1 | Serial input shift register busy (a byte is being received) |
The error bits 5–7 are latched until you write anything to SKRES ($D20A). Atari's original hardware manual has the two overrun bits (5 and 6) swapped. Bit 5 is the serial input overrun. An overrun means a new byte arrived while the previous byte's serial-input interrupt was still pending, so the older byte was lost. Overrun detection can never be completely reliable, because the interrupt has to be briefly disabled to acknowledge it. There is no overrun detection for output at all.
Keyboard programs use bits 2 and 3. The OS's vertical-blank code checks bit 2 to decide whether to auto-repeat (see Writing an Interrupt-Driven Keyboard Handler).
Troubleshooting
- High-pitched whine after loading from tape: POKEY is still in two-tone mode.
POKE 53775,3. - RANDOM returns 255 every time: SKCTL bits 0 and 1 are both clear (init mode). Restore SSKCTL.
- Sound from your own player stops during disk I/O: SIO reprograms POKEY's channels 3 and 4 and AUDCTL for its baud rate. Re-initialise after I/O.
- Devices hang after an interrupted transfer: init mode doesn't clear the bit being driven on the SIO DATA OUT line, so an aborted send can leave the line low. Finish or reset transfers cleanly.
Further reading
- Avery Lee, Altirra Hardware Reference Manual, chapter 5 (POKEY): initialisation mode, serial port timing, overrun and framing errors.
- Ian Chadwick, Mapping the Atari: 562 (SSKCTL), 53770–53775.
- Atari, Inc., Operating System User's Manual: the cassette handler and the SKCTL whistle note.
- On this site: SIO Explained, High-Speed SIO, Turbo Tape Systems, POKEY Random Numbers.
Credits
- Picture: Writing 0 to SKCTL puts POKEY into initialisation mode: its random-number generator stops and reads 255 until normal operation resumes. — Program authors; screenshot by the site
- Altirra Hardware Reference Manual — chapter 5 POKEY: initialization mode, serial port reset, clock selection table, divisors $05CC/$0028, 19040 baud, 128 kbaud, SERIN/SEROUT, framing/overrun errors, SKSTAT D5/D6 note — Avery Lee (Altirra)
- Mapping the Atari — 562 SSKCTL, 53770 SKREST/RANDOM, 53773 SEROUT/SERIN, 53774 IRQEN/IRQST, 53775 SKCTL/SKSTAT — AtariArchives.org
- Atari Operating System User's Manual — cassette handler (SKCTL $03 whistle fix); OS source IRQ dispatcher (serial vectors) — ksquiggle
