Stereo POKEY Programming: Detecting and Driving a Second POKEY

Close-up of an Atari 130XE motherboard with a 40-pin AMI POKEY chip marked C012294B-01 in the centre

Every Atari 8-bit leaves the factory with one POKEY and four sound voices. Since Chuck Steinman's "Gumby" upgrade of December 1989, hobbyists have been piggybacking a second POKEY onto the first to get eight voices and true stereo. Today the stereo upgrade is so common, from simple add-on boards to FPGA replacements such as PokeyMAX, that many new games and most RMT music support it. This guide covers the programming side: where the second chip lives, why mirroring matters, how to set it up, how to detect it, and how to write to both chips.

What you'll learn: the second POKEY's register map, how to initialise it, two tested detection routines, and how to play sound on both chips from assembly and BASIC.

What you need: a stereo-upgraded Atari, or an emulator. In Altirra, turn on Stereo in the audio options of the system configuration. In atari800, start with -stereo. You also need an assembler (MADS) for the machine-code examples. Read POKEY Sound Basics and Advanced POKEY first if the AUDF/AUDC registers are new to you.

1. How the upgrade works

POKEY only looks at the lowest four address lines. Its 16 registers at $D200–$D20F therefore repeat ("mirror") through the whole $D2xx page: $D210 is the same register as $D200, $D220 too, and so on. The stereo upgrade adds address line A4 to the selection. With A4 clear, the even mirrors ($D200, $D220…) still select the original chip. With A4 set, the odd mirrors ($D210, $D230…) select the new one. The usual addresses are:

Register Left (chip 1) Right (chip 2)
AUDF1 / AUDC1 $D200 / $D201 (53760/53761) $D210 / $D211 (53776/53777)
AUDF2 / AUDC2 $D202 / $D203 $D212 / $D213 (53778/53779)
AUDF3 / AUDC3 $D204 / $D205 $D214 / $D215 (53780/53781)
AUDF4 / AUDC4 $D206 / $D207 $D216 / $D217 (53782/53783)
AUDCTL $D208 (53768) $D218 (53784)
STIMER $D209 (53769) $D219 (53785)
IRQEN (write) / IRQST (read) $D20E (53774) $D21E (53790)
SKCTL $D20F (53775) $D21F (53791)

The second chip is a complete POKEY, but its interrupt and I/O connections are missing: no IRQ from it ever reaches the CPU, and its serial port talks to nothing. Its timers still run and its interrupt status bits still change. Treat it as four extra voices plus its own AUDCTL, so it can have its own 16-bit pairs, filters and clock settings.

The design has been reproduced many times since 1989, for example by Candle's SimpleStereo (2003), the C.P.U. STEREO board by Bob!k and Raster (2006), Lotharek's Simple Stereo and Mark Watson's FPGA-based PokeyMAX, which includes a dual-POKEY mode. On Ultimate1MB machines, the board's M0 control output is conventionally used to switch a stereo upgrade on and off.

2. Mirroring: why stereo code breaks mono machines

On a stock machine, a write to $D210 doesn't vanish: it lands in the first POKEY, because $D210 is a mirror of $D200. A program that blindly drives eight voices on a mono machine has both halves of its music fighting over the same four voices.

You can also use this on purpose. RMT's player can be built in two "spread" modes (STEREOMODE 2 and 3) that play an ordinary four-voice tune with some voices on each chip. On a stereo machine the tune comes out in stereo. On a mono machine the right-chip writes simply reach the left chip, and you hear the normal mono tune with no detection code at all. One detail matters here. A filter pairs voices 1 and 3 (or 2 and 4) on the same chip, so when a filtered pair is split across two chips, the player must also write the partner's frequency to the other chip. RMT's mode-2 code does exactly that.

3. Initialising the second chip

POKEY has no reset line and powers up in an unpredictable state, possibly with its interrupt enables set. The OS only ever initialises the chip at $D200, and BASIC's SOUND statement only touches that chip. So before you use the second chip, reset it yourself: AUDCTL to 0, all AUDF/AUDC registers to 0, and SKCTL to 3 to take it out of initialisation mode. This routine does it for either chip:

POKEY1  = $D200

; reset one POKEY: X = $00 for chip 1, $10 for chip 2
pinit   lda #0
        sta POKEY1+$08,x ; AUDCTL
        ldy #7
pi1     sta POKEY1,x     ; AUDF1-4 / AUDC1-4 = 0
        inx
        dey
        bpl pi1
        lda #3
        sta POKEY1+$0F-8,x ; SKCTL (X has moved on by 8)
        rts

RMT's silence routine does the same for both chips: AUDCTL 0, SKCTL 3 and all eight voices zeroed. If a voice on the second chip seems dead, a missing SKCTL write is the first suspect, because in initialisation mode the 15 kHz and 64 kHz clocks stand still.

4. Detecting a second POKEY

A program that wants to support both kinds of machine needs to know which one it is on. Mirroring makes that testable: do something to "the second chip" and see whether the first chip notices.

Method 1: a timer interrupt. Start timer 1 on the chip at $D210 with a very short period and enable its interrupt. On a mono machine the writes reach the main POKEY, and its IRQ status register (IRQST, $D20E) soon shows the timer interrupt pending. On a stereo machine the timer runs inside the second chip and IRQST never changes. Seban of Slight published a routine on this principle in 1995–96. This version keeps the CPU's interrupt flag set during the test, so nothing is actually serviced, and restores the OS settings afterwards:

IRQEN   = $D20E
IRQST   = $D20E
NMIEN   = $D40E
POKMSK  = $10
P2      = $D210

; Returns carry set = stereo.
stereo_irq
        sei
        lda #0
        sta NMIEN       ; no VBI/DLI during the test
        sta P2+$01      ; AUDC1: silent
        sta P2+$08      ; AUDCTL: 64 kHz clock
        lda #3
        sta P2+$0F      ; SKCTL: out of init mode
        lda #1
        sta P2+$00      ; AUDF1: period of 2 x 28 cycles
        sta P2+$0E      ; IRQEN: timer 1 only
        sta P2+$09      ; STIMER: go
        ldx #0
wait    lda IRQST       ; first chip's status
        lsr             ; bit 0 clear = timer 1 fired here
        bcc mono
        dex
        bne wait        ; about 3,300 cycles: never fired
        sec             ; -> the writes went to a second chip
        bcs done
mono    clc
done    lda #0
        sta P2+$0E      ; second chip: no IRQs
        lda POKMSK
        sta IRQEN       ; first chip: restore the OS settings
        lda #$40
        sta NMIEN       ; VBI back on
        cli
        rts

Method 2: freeze RANDOM. Clearing SKCTL bits 0 and 1 puts a POKEY into initialisation mode, and while it is there its RANDOM register ($D20A) locks at $FF. Write 0 to $D21F and then read $D20A a few times. If it is stuck at $FF, you froze the first chip, so the machine is mono. If it keeps changing, the write went to a second chip:

RANDOM  = $D20A

; Returns carry set = stereo.
stereo_rnd
        lda #0
        sta P2+$0F      ; SKCTL = 0: init mode
        ldx #8
rloop   lda RANDOM
        cmp #$FF
        bne found       ; still changing: first chip unaffected
        dex
        bne rloop
        clc             ; stuck at $FF: mono
        bcc rdone
found   sec
rdone   lda #3
        sta P2+$0F      ; leave init mode again
        rts

Both routines report correctly in atari800 with and without -stereo. Each one briefly disturbs the main POKEY on a mono machine (method 1 rewrites voice 1, AUDCTL and the interrupt enables; method 2 pauses the keyboard scan), so run them at start-up, before any music starts. Method 1 also re-enables only the VBI, so set your DLIs up afterwards.

5. Writing to both chips

Once you know the second chip is there, using it is just a matter of writing 16 bytes higher. This program detects the upgrade, initialises the chips, and plays a C major chord on the left with the same chord an octave lower on the right:

POKEY2  = $D210

        org $2000
start   jsr stereo_irq  ; carry set = second POKEY present
        php
        ldx #$00        ; offset of chip 1
        jsr pinit
        plp
        bcc leftonly
        ldx #$10        ; offset of chip 2
        jsr pinit
        ldy #5          ; right chip: C, E, G one octave down
r1      lda right,y
        sta POKEY2,y
        dey
        bpl r1
leftonly ldy #5         ; left chip: middle C, E, G
l1      lda left,y
        sta POKEY1,y
        dey
        bpl l1
stop    jmp stop

left    dta 121,$A6, 96,$A6, 81,$A6     ; AUDF/AUDC pairs, volume 6
right   dta 243,$A6,193,$A6,162,$A6

Combined with pinit and stereo_irq it makes a 146-byte executable. Recording POKEY registers in atari800 (-pokeyrec -pokeyrec-stereo) confirms the result: with -stereo both chips hold their three notes, and without it only the left chord is written.

BASIC can do the same with POKEs. SOUND drives the left chip as usual:

10 REM SET UP THE SECOND POKEY
20 POKE 53784,0:POKE 53791,3
30 REM MIDDLE C ON THE LEFT CHIP
40 SOUND 0,121,10,6
50 REM C AN OCTAVE LOWER ON THE RIGHT
60 POKE 53776,243:POKE 53777,166
70 PRINT "PLAYING":GOTO 70

Press BREAK to stop it, then POKE 53777,0 to silence the right channel (END only silences the left chip).

A few design tips:

  • Keep a shadow copy. Compute all eight voices into a buffer during the frame and copy it to $D200–$D208 and $D210–$D218 in one burst. RMT's player works this way. A mono machine can then be served by copying only the first half, or a mono mix of both.
  • Pseudo-stereo is cheap. Writing a four-voice tune to both chips, the right one a few cycles later than the left, widens the sound. An AtariAge experiment found delays of 1–3 cycles pleasant and more than about 6 disturbing.
  • Test without the upgrade. Detection bugs only show on the configuration you don't use every day.

Troubleshooting

  • Right channel silent in the emulator. Stereo is off: check Altirra's audio options, or add -stereo to atari800.
  • Right channel silent on hardware, left fine. The second chip was never initialised. Set SKCTL ($D21F) to 3 and AUDCTL ($D218) to 0.
  • Music garbled on a mono machine. Your code writes eight voices without checking. Detect first, or use an RMT spread mode that degrades to mono.
  • Keyboard dead after detection. IRQEN was not restored from POKMSK.
  • Stereo tune plays mono in a SAP player. The SAP file was built with the mono player. Rebuild it as stereo.

Further reading

  • comp.sys.atari.8bit FAQ, section 6.11: the list of stereo upgrades from 1989 to today.
  • Avery Lee, Altirra Hardware Reference Manual, Chapter 5: POKEY addressing, the stereo modification and initialisation mode.
  • Mapping the Atari (Ian Chadwick), locations 53760–53775 and the note on mirrors at 53776–54015.
  • On this site: POKEY Sound Basics, Advanced POKEY, Getting Started with RASTER Music Tracker, and the hardware entries for stereo POKEY upgrades and PokeyMAX.

Credits