VIC 20 JOYSTICK/PADDLES/LIGHT PEN The following documentation is taken from the "VIC 20 Programmers Reference Manual". USING A JOYSTICK ON THE VIC Like all other input and output, the joysticks are controlled using the VIC'S 6522 Versatile Interface Adapters (VIAs). The 6522 is a very versatile and complex device. Fortunately, it isn't necessary to delve deeply into the mysteries of the 6522 VIA to read the joysticks. Each 6522 has two Input/Output ports, called port A and port B. Each of the ports has a control register attached, called the DATA DIRECTION REGISTER (DDR). This highly inportant register controls the direction of the port. By using this register you can use the port for input, output, or both at the same time. To set one bit of the port to output, set the corresponding bit of the Data Direction Register to 1. To set a bit of the port for input, set the corresponding bit of the DDR to 0. For example, to set bit 7 of port A to input, and the rest of the bits to output, poke 127 in the DDR for port A. To read the joystick, one port (and one DDR) of each of the 6522 VIAs on the VIC must be used. The joystick switches are arranged as follows: TOP FIRE BUTTON Switch 4 (FR) Switch 0 (S0) | | | Switch 2 ------ ------ Switch 3 (S2) | (S3) | | (S1) Switch 1 Switch 0, Switch 1, Switch 2, and the Fire button can be read from VIA #1, which is located beginning at location $9110. Switch 3 must be read from the other 6522 (VIA #2) which is located beginning at location $9120. Now the key locations for the joystick are as follows: HEX DECIMAL PURPOSE $9113 37139 Data direction register for I/O port A on VIA #1 $9111 37137 Output register A Bit 2 Joy switch 0 Bit 3 Joy switch 1 Bit 4 Joy switch 2 Bit 5 Fire Button $9122 37154 Data direction register for I/O port B on VIA #2 $9120 37152 Output register B Bit 7 Joy switch 3 IMPORTANT The relevant bit is one when a switch is open and zero when a switch is closed. 1 = open 0 = closed A switch is closed when the joystick is pointing in that direction and open if it isn't pointing in that direction. JOYSTICK NOTES It is important to note that the bits are one when the switch is open and not zero. Some VIC 20 manuals state that it is the other way around which is not the case. To read the joystick inputs, a VIC 20 program will firstly set the ports to input mode by setting the DDR to 0. Then the values of the switches can be read by the program. VIA #2 is also used for reading the keyboard. Setting the DDR can mess up the keyscan rather badly. So VIC 20 programs will make sure that they restore the DDR to the original condition.