Reading Joysticks, Paddles and Fire Buttons
The Atari's controller ports are the same nine-pin design used on the Atari 2600, so the classic CX40 joystick and CX30 paddles plug straight in. Reading them from a program is easy — but the numbers BASIC hands back (15, 14, 7, 11...) look arbitrary until you see the bits behind them. This guide explains both, and shows how to turn a joystick reading into movement quickly.
You will learn: the BASIC controller functions, the bit layout of a joystick reading, fast ways to convert it to X/Y movement, paddles and their triggers, the shadow and hardware registers, and how to do the same in 6502 assembly.
You need: a joystick (and optionally a pair of paddles) or an emulator with controller mapping. The 400 and 800 have four controller jacks; the XL and XE machines have two.
1. The BASIC functions
| Function | Returns | Shadow register |
|---|---|---|
STICK(n) |
joystick direction, 5–15 | 632–635 (STICK0–3) |
STRIG(n) |
joystick button: 0 = pressed, 1 = not | 644–647 (STRIG0–3) |
PADDLE(n) |
paddle knob, 0–228 | 624–631 (PADDL0–7) |
PTRIG(n) |
paddle button: 0 = pressed, 1 = not | 636–643 (PTRIG0–7) |
n counts from zero: STICK(0) is the joystick in the first jack. Each function simply reads an operating-system shadow register that the OS updates every vertical blank (60 times a second on NTSC), so PEEK(632) gives exactly the same value as STICK(0).
Try this, then waggle the stick:
10 PRINT STICK(0),STRIG(0):GOTO 10
2. Why the numbers are what they are
A joystick is just four switches (up, down, left, right) plus the fire button, each pulling a wire to ground when closed. The computer reads the four direction wires as the low four bits of a byte, and a closed switch reads as 0:
| Bit | Value | Direction |
|---|---|---|
| 0 | 1 | Up |
| 1 | 2 | Down |
| 2 | 4 | Left |
| 3 | 8 | Right |
With nothing pressed all four bits are 1, giving 15. Pushing right clears bit 3: 15 − 8 = 7. Diagonals clear two bits at once:
14
10 (up) 6
11 (left) 15 (right) 7
9 (down) 5
13
As Antic's "De Re Joystick" article put it, there really is logic to the values — it lies in binary.
3. Turning a reading into movement
Atari BASIC's AND is a logical operator, not a bitwise one, so you cannot simply mask bits. But relational tests return 1 for true and 0 for false, which lets you build the movement arithmetically. From the bit table: right is pressed whenever the value is below 8 (5, 6, 7); left when it is 9, 10 or 11; down for 5, 9, 13; up for 6, 10, 14.
10 GRAPHICS 7+16:X=80:Y=48
20 S=STICK(0)
30 X=X+(S<8)-(S>8 AND S<12)
40 Y=Y+(S=5 OR S=9 OR S=13)-(S=6 OR S=10 OR S=14)
50 IF X<0 THEN X=0
60 IF X>159 THEN X=159
70 IF Y<0 THEN Y=0
80 IF Y>95 THEN Y=95
90 COLOR STRIG(0):PLOT X,Y
100 GOTO 20
This is a tiny sketch pad: steer to draw, and hold the button to erase — COLOR STRIG(0) selects colour 1 normally and colour 0 (background) while the button is held.
A table lookup is faster still, because it replaces all the comparisons with two array reads:
10 DIM XD(15),YD(15)
20 FOR I=5 TO 15:READ A,B:XD(I)=A:YD(I)=B:NEXT I
30 DATA 1,1,1,-1,1,0,0,0,-1,1,-1,-1,-1,0,0,0,0,1,0,-1,0,0
40 S=STICK(0):X=X+XD(S):Y=Y+YD(S)
The DATA gives (dx,dy) pairs for values 5 through 15; values 8 and 12 never occur from a real joystick and are set to 0,0.
One more shortcut for horizontal-only games: the paddle-trigger lines are the same wires as the joystick's left/right switches, so PTRIG(1)-PTRIG(0) returns −1 for left, 0 for centre and +1 for right.
4. Paddles
Paddles come in pairs, and both paddles of a pair plug into one jack: paddles 0 and 1 use the first jack, 2 and 3 the second, and so on — up to eight paddles on a 400/800 and four on an XL/XE. PADDLE(n) returns 0 to 228, increasing as you turn the knob anti-clockwise.
To move something left-to-right as the knob turns clockwise, invert and scale the reading:
10 GRAPHICS 7+16:COLOR 1
20 P=PADDLE(0):X=INT((228-P)*159/228)
30 PLOT X,90
40 IF PTRIG(0)=0 THEN PRINT "FIRE!"
50 GOTO 20
(Since this is a +16 screen, line 40's PRINT will switch to a text screen — replace it with your own action in real use.)
When using paddle values directly as screen or player positions, remember that many televisions cannot show positions below about 48 or above about 208, and that plotting a raw value outside a mode's range gives ERROR 141. Scale and clamp.
Carl Evans's Atari BASIC Faster and Better builds a paddle-driven menu by dividing the 0–228 range into bands, one per option — and recommends no more than four options, because narrow bands are hard to hit with a knob.
Paddle games are a niche but a real one; titles that support them include Avalanche (APX, 1980), Blazing Paddles (Baudville, 1986) and Imagine's 1987 conversion of Arkanoid.
5. The hardware behind the shadows
The shadow registers are copied from the chips during the vertical blank:
- Joystick directions come from the PIA's PORTA register at 54016 ($D300): bits 0–3 are the first jack and bits 4–7 the second. On the 400/800, PORTB (54017) carries jacks three and four; on XL/XE machines PORTB is used for memory control instead — never POKE it casually there.
- Joystick buttons come from GTIA registers TRIG0–3 at 53264–53267 ($D010–$D013).
- Paddle positions come from POKEY's POT0–7 at 53760–53767 ($D200–$D207).
Reading PEEK(54016) gets both joysticks at once: S0=PEEK(54016)-16*INT(PEEK(54016)/16) would extract the first. In practice the shadow registers are simpler and are what most software uses; the hardware registers matter when you poll more often than once a frame or work with the OS vertical-blank processing turned off.
6. In assembly language
Shifting the stick value right moves each direction bit into the carry flag in turn. Carry clear means "pressed":
STICK0 = $0278
STRIG0 = $0284
LDA STICK0
LSR A ; bit 0 -> carry
BCS NOTUP
DEC YPOS ; up pressed
NOTUP LSR A ; bit 1 -> carry
BCS NOTDOWN
INC YPOS ; down pressed
NOTDOWN LSR A ; bit 2 -> carry
BCS NOTLEFT
DEC XPOS ; left pressed
NOTLEFT LSR A ; bit 3 -> carry
BCS NOTRGHT
INC XPOS ; right pressed
NOTRGHT LDA STRIG0
BNE NOFIRE ; 0 = button pressed
JSR FIRE
NOFIRE RTS
Add range checks on XPOS/YPOS as your game requires.
Troubleshooting
- STICK always returns 15: wrong jack, a broken cable, or (in an emulator) the joystick is not mapped to port 1. Remember
STICK(0)is the first port. - Diagonals do not work: worn contacts in an old joystick. A 1980s Antic article showed how to open a CX40 (four screws — watch the small spring) and how each wire maps to a switch.
- Paddle readings jitter: dirty potentiometers; a few readings averaged, or a dead band, helps in software.
- Movement is too fast: the loop runs many times per frame. Wait for the jiffy clock (location 20) to change before moving again.
Further reading
- Atari Graphics and Arcade Game Design — Jeffrey Stanton and Dan Pinal (Arrays, 1984).
- Dr. C. Wacko's Miracle Guide to Designing and Programming Your Own Atari Computer Arcade Games — David L. Heller, John F. Johnson and Robert Kurcina (Addison-Wesley, 1983).
- The Computer Controller Cookbook — Tom Riley and Kelda Riley (Creative Computing Software, 1983), for using the ports as general-purpose inputs.
Credits
- Mapping the Atari — locations 624-647 (PADDL, STICK, PTRIG, STRIG) and 54016 (PORTA) — AtariArchives.org
- Antic Vol. 5 No. 1 (May 1986) — De Re Joystick (Adelbert Fernandez) — AtariMagazines.com (Kevin Savetz), 1986
- Atari BASIC Faster and Better (Carl M. Evans) — Paddle Driven Menus — Atari8bit.net
- comp.sys.atari.8bit FAQ — 8.4 What programs use paddle controllers? — Michael Current (comp.sys.atari.8bit FAQ)
