Reading CoCo Joystick Buttons in Color BASIC
The joystick buttons on the TRS-80 Color Computer 1 (CoCo 1) are connected to the
PIA (Peripheral Interface Adapter). They can be read from memory location
$FF00, which is decimal 65280.
In Color BASIC, the value can be read using PEEK:
B=PEEK(65280)
The joystick buttons are active low. This means that a button that
is not pressed reads as a 1, and a pressed button pulls the bit to
0.
The button bits are:
| Bit | Mask | Button |
|---|---|---|
| 0 | 1 |
Right joystick button |
| 1 | 2 |
Left joystick button |
Color BASIC supports bitwise AND, which makes checking the buttons
straightforward:
10 B=PEEK(65280)
20 IF (B AND 1)=0 THEN PRINT "RIGHT BUTTON"
30 IF (B AND 2)=0 THEN PRINT "LEFT BUTTON"
40 GOTO 10
The parentheses are important. Color BASIC’s operator precedence can produce
surprising results if you write:
PEEK(65280) AND 128 = 0
This is not interpreted as:
(PEEK(65280) AND 128) = 0
Instead, the comparison happens first, effectively making it:
PEEK(65280) AND (128 = 0)
Since 128 = 0 is false, the expression becomes an AND with zero,
which will always produce zero.
The correct way to test a bit is to put the bit operation inside parentheses:
(PEEK(65280) AND 128) = 0
The evaluation order is:
PEEK(65280)reads the hardware register.AND 128masks the desired bit.= 0tests whether that bit is clear.
This is a good habit for Color BASIC programming: whenever using bit masks in
a comparison, put the masked expression in parentheses.
The CoCo’s joystick interface is simple, but remembering that the buttons are
active low and that Color BASIC’s operator precedence is different from many
modern languages will save a lot of debugging time.