CHAPTER 18

The joystick ports


The joystick ports are the I/O ports of the PIA chip. This means that they are bidirectional, capable of output as well as input. The joystick ports are usually set up for input. To read them, simply read the port registers. PORTA [$D300 (53016)] will read joystick ports 1 and 2. PORTB [$D301 (54017)] will read joystick ports 3 and 4. Joystick ports 3 and 4 are used for memory control on the XL/XE models and don't have external connectors.

Each bit of each port can be configured independently for input or output. To reconfigure a port, the port control registers, PACTL and PBCTL [$D302 (54018) and $D303 (54019)], are used. The port control registers also control some lines on the serial I/O connector.

               The port control registers

                7 6 5 4 3 2 1 0
     PACTL     -----------------
     or        |n 0 1 1 n n 0 n|
     PBCTL     -----------------
                1 6 3 1 8 4 2 1
                2 4 2 6
                8

       bits
                        PACTL

          7    Peripheral A interrupt status.  Set by peripheral
                interrupt; reset by reading PORTA.
          3    Cassette motor control (0 = on: 1 = off).
          2    0 = PORTA is now port A direction control.
                Writing to PORTA will now set bits for input
                or output.
                0 sets bit for input; 1 sets bit for output.
                1 = PORTA operational
          1    1 = peripheral A interrupt enabled.

PBCTL

7 Peripheral B interrupt status. Set by peripheral interrupt; reset by reading PORTB. 3 Serial connector command line. 2 0 = PORTB is now port B direction control. Writing to PORTB will now set bits for input or output. 0 sets bit for input; 1 sets bit for output. 1 = PORTB operational 1 1 = peripheral B interrupt enabled.

The electronic configuration of the controller ports is as follows.

       -----------     -----------  
       \0 1 2 3 R/     \4 5 6 7 R/  
        \t + - L/       \t + - L/   
         -------         -------    

0 through 7 are the binary data bits for port A or port B.

+ and - are +5 volts and ground respectively.

R and L are the left and right game paddles.

t is the joystick trigger line.

The data bits in the joystick ports are used as follows for the joysticks and game paddles.

                     The joysticks and the port registers

7 6 5 4 3 2 1 0 ----------------- PORTA |U|D|L|R|U|D|L|R| ----------------- 1 6 3 1 8 4 2 1 2 4 2 6 8

paddle | | | | triggers 3 2 1 0

PORTB ----------------- (400/800 |U|D|L|R|U|D|L|R| only) -----------------

paddle | | | | triggers 7 6 5 4

U = up D = down L = left R = right

The joysticks may be read either directly from the port registers or from the joystick shadow registers. During vertical blank, the data in the port registers is separated and put into the shadow registers. These registers are, STICK0 [$0278 (632)], STICK1 [$0279 (633)], STICK2 [$027A (634)] and STICK3 [$027B (635)]. The triggers may be read from the joystick trigger registers, TRIG0 - TRIG3 [$D010 - $D013 (53264 - 53267)]. These register have shadow registers, STRIG0 - STRIG3 [$0284 - 0287 (644 -647)]. If these registers read zero the associated triggers are pressed. The paddle triggers may be read from their shadow registers also. They are, PTRIG0 - PTRIG 7, [$027C - $0283 (236 - 643)].


THE GAME PADDLE REGISTERS

Although the game paddles are plugged into the joystick ports, they are not read from the port registers. The game paddles are read by first writing any number to the start-pot-scan register, POTGO [$D20B (53771)]]. This turns off the capacitor dump transistors and allows the pot reading capacitors to begin charging. It also sets the TV scan line counter to zero. As each capacitor crosses a certain trigger voltage, the number of TV lines scanned is put in the respective pot value register. When the scan counter reaches 228, the capacitor dump transistors are turned on and the number 228 is put into any pot value registers which are still empty.

Before reading the pot value registers, ALLPOT [$D208 (53768)] should be checked. In this register, each bit corresponds to the validity of a pot value register. If a bit is zero, its' associated pot value register is valid. If bit 2 of SKCTL, [$D20F (53775)], is 1, the pots go into the fast scan mode. In this mode the paddles are read in only 2 TV scan lines. They can also be read without regard to POTGO or ALLPOT.

The pot value registers contain the number of TV scan lines it last took for the paddle reading capacitors to charge (up to 228). These registers are POT0 - POT7 [$D200 - $D207 (53760 -53767)]. Their shadow registers are PADDL0 - PADDL7 [$0270 - $0277 (624 - 631)].


THE LIGHT PEN REGISTERS

Whenever a joystick trigger is pressed, the light pen registers, PENH and PENV are updated. PENH [$D40C (54284)] takes a value based on a color clock counter. The value can be from 0 to 227. PENV [$D40D [54285)] takes the 8 highest bits of the vertical line counter. A light pen is simply a photo transistor connected to a joystick trigger line and focused on the TV screen. When the electron beam strikes the part of the screen the light pen is focused on, the transistor turns on pulling the trigger line low. The light pen registers then contain numbers relative to where the light pen was pointing. The shadow register for PENH and PENV are LPENH [$0234 (564)] and LPENV [$0235 (566)).

     Useful operating system equates

TRIG0 $D010 (53264): joystick triggers | TRIG3 $D013 (53268): POT0 $D200 (53760): paddle value | POT7 $D207 (53767): ALLPOT $D208 (53768): reads validity of pot values POTGO $D20B (53771): starts paddle read SKCTL $D20F (53775): bit 2 enables fast pot scan PORTA $D300 (53016): port A data PORTB $D301 (53017): port B data PACTL $D302 (54018): port A control PBCTL $D303 (54019): port B control PENH $D40C (54284): light pen horizontal value PENV $D40D (54285): light pen vertical value

Shadow registers

LPENH $0234 (564): light pen horizontal value LPENV $0235 (566): light pen vertical value PADDL0 $0270 (624): game paddle values | PADDL7 $0277 (631) STICK0 $0278 (632): joystick registers | STICK0 $027B (635): PRTIG0 $027C (636): paddle triggers | PTRIG7 $0283 (643): STRIG0 $0284 (644): joystick triggers | STRIG3 $0287 (647):


Go to chapter 17
Go to chapter 19