Chapter 5
Game
Controllers 161
Since four switches are in the same memory location, the numbers in
the boxes are used with the AND operator to isolate the bit for a particular
switch from the others PEEKed from the same memory location. This use
of
AND was discussed in the "Boolean Operators" section
of
Chapter
3.
In the
result
of
the
AND
operation, all bits except the one
we
want
to look
at
will
be forced
to
a 0 value. (This is called masking.
Just
as you use masking tape
to cover up woodwork while painting the walls, computer programs use bit
masks to cover up the bits they don't want to test.)
For
example, the
following statement determines if the Fire button
is
being pushed:
10
FB
=
(PEEK(37137)
AND
32)
• 0
"AND
32" eliminates the other switch values by "covering
up"
all bits except
32. The program then compares the result to
O.
The joystick switches supply
a 0 signal to their VIA pins when the switch
is
closed and a 1 when open.
When the switch
is
closed, the
button
is
being pushed.
Let's look
at
the AND operation in binary.
PEEK location 37137
AND bit number 32
0001100
0010000
0000000
The result
of
the AND
is
0,
since 1 AND 0
is
always
O.
The switch
is
closed,
the signal
is
0,
and the button
is
pushed.
Try entering and running the following program to see how moving the
joystick affects location 37137:
10
PRINT
PEEK(37137)
20
FOR
1=1
TO
250
:
NEXT
I
REM
WAIT
ABOUT
HALF
A
SECOND
30
(laTa
10
The program checks and displays the value in memory location 37137
every half second.
As
you move the joystick, notice the changes in the values
displayed on the screen. When a switch in the joystick
is
closed, its mask
value (32, 16,8,
or
4)
is
subtracted from the displayed number. Moving the
joystick to the right has no effect because the program looks only
at
location
37137, not at location 37152. Testing this switch requires some additional
programming.