How Joysticks work

First of all a definition of a joystick is necessary for this simple tutorial. A joystick is a hand held device that allows a user to interact with a game by often moving a character around on a screen. When Nolan Bushnell first entered the game design business, he developed arcade machines and stashed them in bars. The machines came equipped with a set of controllers, which included a stick controller, and various buttons. Later these would come to be known as the ‘joystick’ and ‘fire button’ .

This was made popular by the first video game system (VCS) known as the Atari 2600 in the late 70’s.

Hardware of a Joystick

Moving the joystick makes contact with the metal on the board sending information through the wires

A joystick contains a board with switches embedded around a plastic covering. In the center is often a long control (known as the ‘stick’) that can be moved in any direction which includes up, down, left,  right, and any diagonal position.

Below is a simple hardware view of the control board of a joystick. If you were to open a joystick and look inside you would see a PCB (Printed Circuit Board) that contains metal contacts at the base of the stick. Whenever the joystick is pressed in a direction, the board receives electronic current that tracks the direction (within memory locations on the Commodore 64 and its’ line of computers). The Commodore’s CIA (Complex Interface Adapter) chip manages this information and the transfer of voltage to the appropriate connected devices. It can also be used to communicate with other peripheral input and output devices.

Joystick Pinout Information

Each joystick for the Atari, Commodore, and other 8-bit line of computers, includes a set of pins numbered 1-8 that receive current from the wires inside the controller. If you ever want to construct or repair your own joystick, you will need a map of the pin-out diagram. I found an article while browsing the Atari age forums that detailed this information, included with some nice screenshots referencing the pin-out locations.

Joystick Tester Project

I became really intrigued with how the hardware and electronics work on these devices. One day I was surfing through YouTube and came across a channel under the name Ms Mad Lemon. There was a woman who was working on creating a joystick testing project. That left me fixed to the screen and devouring more of her work over time. I hope to eventually create my own tester for the Commodore 64. Be sure to check out her channel at Ms Mad Lemon and subscribe if you like her work.  She often talks about the Commodore 64, but her primary focus is usually on Amiga computers.

C64 Basic Joystick Program

Also the joystick could also be inserted into the second port. So depending on which joystick is connected, information will be received for that device.

This information is then sent through the connected wires out to the input device on the joystick, which then interprets the electrical current as information to the connected computer. According to the book Mapping the Commodore 64 by Sheldon Leeman, the memory address 56320 ($DC00) manages the connected port. The program below is used to read the joystick and print a result on the screen.

10 S1=NOT PEEK(56320)AND15
20 PRINTS1
30 GOTO10

The values generated on the screen are bits read from the memory address received by the lead wires in the joystick passed down through the port(s).

Joystick Bit Values

Joystick bit values are read as follows according to the direction the controller is moved in. Be sure to use the values when you are writing for your own joystick program.

S1 = 1 : Joystick moved up
S1 = 2 : Joystick moved down
S1 = 4 : Joystick moved left
S1 = 5 : Joystick moved to upper left
S1 = 6 : Joystick moved down to left
S1 = 8 : Joystick moved right
S1 = 9 : Joystick moved up to right
S1 = 10 : Joystick moved down to right

Fire Button Basic Program Example

The joystick can also record information when the fire button is pressed. This is also tracked by a metal contact located directly below the fire button and once again sends electrical current along the wires back through the connected port and is controlled by the Commodore 64 memory address 56320. So the program printed in the same book will read the joystick’s fire button (from either port or both), based on how many are connected to the system.

10 FI=(PEEK(56320)AND16)/16
20 PRINTFI
30 GOTO10

When the program is run, the number 1 will scroll up the screen. This is reading the bit set to 1. When the fire button is pressed, the value will flip to zero.

As joysticks became more popular, many other distributors continued to create a variety of different types of controls. There are too many to list here. Some were much larger than the original Atari joysticks, and later shapes adapted a fancier type of controller. Nintendo created their own style of joystick that didn’t involve a stick controller, but instead utilized smaller directional pads to move a character around, complete though still with the traditional fire buttons.

Finally before we wrap up for this session, I present a quick joystick routine that plots a PET ASCII character on the screen that can be moved around with the joystick. Pressing the fire button will change the character shape on the screen and alternate through random colors.

If you are new to programming in Basic and find the code below a little intimidating, then I highly encourage you to check out the article for my Commodore Basic Programming tutorials.

5 PRINTCHR$(147)
8 X1=12:Y1=4:CO=0
10 FI=(PEEK(56320)AND16)/16
15 JO=NOT PEEK(56320)AND15
20 IFJO=1THENGOSUB210
25 IFJO>0THENPOKE53280,JO+(FI+2)
30 IFJO=2THENGOSUB240
40 IFJO=4THENGOSUB270
50 IFJO=8THENGOSUB300
60 :
70 POKE214,Y1:PRINT:POKE211,X1:POKE646,CO:PRINTCHR$(65+INT(CO));
80 IFFI<1THENCO=1+RND(0)*15
100 GOTO10
200 REM MOVE UP
210 Y1=Y1-1:IFY1<1THENY1=19 220 RETURN 230 REM MOVE DOWN 240 Y1=Y1+1:IFY1>19THENY1=1
250 RETURN
260 REM MOVE LEFT
270 X1=X1-1:IFX1<1THENX1=39 280 RETURN 290 REM MOVE RIGHT 300 X1=X1+1:IFX1>39THENX1=1
310 RETURN

As always my dear reader, I hope you found helpful and possibly fascinating information on this page.  Bookmarking this website is idea as I often release new blogs and update the website frequently. I always welcome feedback and comments. Thank you!