VIC 20 JOYSTICK/PADDLES/LIGHT PEN The following documentation is taken from the "VIC 20 Programmers Reference Manual". I have added a few comments here and there to do with using this information to emulate joysticks in a VIC 20 emulator. 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 EMULATION 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. This highlights the importance to have the DDR registers properly emulated. There are two ways to emulate the VIC 20 joystick. The first one is to use keys on the keyboard, and the second is to use the IBM joystick. Using the cursor keys, for example, will mean that in your keyboard interrupt handler you will need to store which IBM keys are being pressed down at any one time. When a VIC 20 program reads the joystick locations, you will need to use this information to determine what value to return. Remember, if a cursor key is being held down, the corresponding joystick bit needs to be changed from one to a zero. Using the IBM joystick is a bit of a hassle. For this reason you might want to stick to the cursor keys and make available the JOYKEYS utility by Bret Johnson: Bret Johnson 6775 South Logan Street Littleton, CO 80122-1249 (303) 795-5084 Email: bretjohn@aol.com Johnson.Bret.E@tci.com URL: http://members.aol.com/bretjohn This utility runs in the background and translates joystick directions to key presses on the keyboard that you define. The beauty of this utility is that it doesn't appear to slow things down at all. Although the above utility is all very nice, the ideal situation would be to have the emulator use its own joystick routines. I have discovered that reading the joystick the way most programs do slows down the emulator quite considerably. I was therefore quite keen to discover how JOYKEYS managed to make things tick so swiftly. Here's the authors description of what takes place: HOW JOYKEYS WORKS ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ This explanation requires a small amount of electronics experience, as well as a small amount of software experience. If you have neither of these, you may become easily lost. If you have extensive experience in both, this will seem simple-minded, but you still may be interested in case you want to use the Game Port for something yourself. First of all, let's talk a little about how the Computer in general works, and then about the Game Port in particular. The IBM PC is an Interrupt driven device; i.e., when a program or a piece of hardware wants to do something, it generates what is called an Interrupt. This Interrupt is how you get the attention of the computer's Central Processing Unit (CPU). Most peripheral hardware you attach to a computer goes through ports that have Interrupt Control built into their design already. For example, the Serial, Parallel, and Keyboard ports you normally use to connect Mice, Modems, Printers, and Keyboards already have Interrupt control inherently built into them. Well, a Game Port on a computer is really stupid - it doesn't do anything on its own. In fact, calling it a Game Port is even a misnomer - it's really just an input port that happens to be used for Joysticks most of the time. A Game Port will accept up to 8 inputs from any source - 4 digital inputs (usually 2 buttons on each of 2 Joysticks) and 4 analog inputs (usually an X and a Y coordinate for each of 2 Joysticks). The analog inputs are potentiometers (variable resistors) - but the Game Port doesn't directly tell you what the resistor values are. You are able, however, to indirectly measure the resistor values by starting electronic one-shots into the resistors and measuring the amount of time the one-shots stay on (which are proportional to the resistor values). The Game Port card gives you enough hardware to do this - you just have to write the software to look at the Game Port when you care about it and somehow measure the amount of time the one-shots stay on. In a PC, there is really only 1 oscillator that can be used as a general- purpose timing device to measure small increments of time (microseconds or milliseconds). The PC has a 1.19318 MHz oscillator that is divided into three separate clocks, which are used for 3 separate things: to update the computer's real-time clock, to keep the RAM refreshed, and to (usually) control the speaker. JOYKEYS "grabs" Interrupt 8 (the Timer), which occurs approximately 18 times per second (this is the same Interrupt your computer uses to update its real-time clock, and uses the oscillator mentioned above to do it). Whenever this Interrupt occurs, JOYKEYS (if ENABLEd) looks at the status of the Joystick(s), one by one. It measures the electronic one-shots with the oscillator mentioned above, compares them to the center values it found when the program was installed or updated last, and STUFFs characters into the Keyboard Buffer or simulates KEYBOARD hardware interrupts accordingly. Because it uses the 1.19318 MHz oscillator, which (at least as far as I know) is the same on all IBM compatibles, regardless of CPU type (8088- Pentium) or speed (4.77MHz-100MHz), this program should give consistent operation on all computers. This also means that you can change CPU speed on your computer (if you have a "turbo" model that lets you do that) while JOYKEYS is installed, and JOYKEYS still knows where the center of the Joystick is. Most programs that access a Joystick use a loop counter to measure the coordinates of the Joystick, rather than a real clock. This means that a faster computer (especially a computer where you can change speeds) will give you different readings depending on what speed it's running at that particular instant. If you want to change speeds, you have to recenter the Joystick. That problem doesn't happen with JOYKEYS. USING PADDLES ON THE VIC The paddles are read using both the VIC chip and the VIC's 6522 Versatile Interface Adapters (VIAs). The values of the paddles are read through the VIC chip. There are two registers, one for each paddle, which will contain the current value of the paddle. This data will be in digitized form, as a value from 0 to 255. The switches on each paddle are read from the VIA chips. They are read in the same manner as discribed above for the joystick. The joystick switches are arranges as follows: Paddle X ---- ---- Paddle Y (S2) (S3) Switch 2 can be read from VIA #1, and Switch 3 from VIA #2. The key locations for the paddle are as follows: HEX DECIMAL PURPOSE $9008 36872 Digitized value of PADDLE X $9009 36873 Digitized value of PADDLE Y $9113 37139 Data direction register for I/O port A on VIA #1 $9111 37137 Output register A Bit 4 PADDLE SWITCH X $9122 37154 Data direction register for I/O port B on VIA #2 $9120 37152 Output register B Bit 7 PADDLE SWITCH Y Once again, the relevant bits will be one when the switches are open and zero when they are closed. Note that these bit locations are also used by the joystick. I assume that the paddles will never be used when the joystick is being used. PADDLE EMULATION NOTES The emulation can once again be down by used keys on the keyboard as for the joystick. Its possible that using the PC mouse for paddle control might be the way to go since the there are no PC paddles that I know of. In this case you might want to look at Bret Johnsons MOUSKEYS program. USING A LIGHT PEN ON THE VIC One of the benefits of using the VIC chip as the controller for the VIC 20 is that it is easy to add certain input/output devices for games and educational software. It is as easy to add a light pen as it is to add game paddles and joysticks. The principle behind the light pen is simple. Basically, the pen is a light detector, set to detect either the presence or absence of light. The television picture is not put on the screen all at once - rather, it is put on the screen one row at a time, scanning from left to right very quickly. When the scan passes the area where the pen is, a signal is sent to the VIC chip. When the signal is received, the VIC chip, which keeps track of where the scan line is at any particular moment, will record the exact location of the scan in two registers, 36870 ($9006) for the X direction and 36871 ($9007) for the Y direction. You can read and use this information in your programs. The light pen trigger is connected to pin 6 of the game port. The light pen trigger input can also be reached from pin 7 of the user port. Note that you can't use a joystick and a light pen at the same time, because the same line that is used as the light pen trigger input is used as the joystick fire button input (you would get false readings). The VIC chip constantly keeps track of the scan position on the television in two registers. When the light pen trigger input is brought low, the VIC freezes the two registers. You can then read and use this information. After reading the two registers, the trigger line will be cleared, so that scan information can be placed again in the two registers.