In Part 1 we blinked an LED using the Commodore 64 User Port, and in Part 2 we read binary (on or off) input also using the User Port. In this part we will use the Control Ports to read Joystick input and also analog signals.

The Commodore 64 Control/Joystick Port
The C64 has two Joystick ports, but you will notice they are labelled Control Port 1 and Control Port 2. This is because, as you might infer, they are not just joystick ports!
In fact there are pins for an Atari-style joystick, paddles, mouse, and even a light pen.
The paddle pins are in fact able to not just read digital, on/off signals, but values from 0 to 255, because they are each connected to an analog to digital converter on the SID chip.
Control/Joystick Port Pins

1 | Up |
2 | Down |
3 | Left / Paddle Fire |
4 | Right / Paddle Fire |
5 | Paddle Y / Potentiometer |
6 | Fire / Light Pen |
7 | 5v |
8 | Ground |
9 | Paddle X / Potentiometer |
Basic Joystick Reading
There are two addresses to read joystick values, one for each control port. Joystick 1 is read at address 56321
, and joystick 2 with 56320
.
Due to the way port 1 conflicts with keyboard input on the CIA chip, most programmers have traditionally favoured port 2.


Reading Both Joysticks
10 j1=not(peek(56321)):j2=not(peek(56320))
20 print "{clear}"
30 if j1=-255 then print "j1 ";
40 if j2=-127 then print "j2 ";
50 if (j1 and 1) then print "j1 ^ ";
60 if (j2 and 1) then print "j2 ^ ";
70 if (j1 and 2) then print "j1 v ";
80 if (j2 and 2) then print "j2 v ";
90 if (j1 and 4) then print "j1 {arrow left} ";
100 if (j2 and 4) then print "j2 {arrow left} ";
110 if (j1 and 8) then print "j1 > ";
120 if (j2 and 8) then print "j2 > ";
130 if (j1 and 16)then print "j1 Q ";
140 if (j2 and 16)then print "j2 Q ";
150 goto 10
To read both joysticks you just need to grab the values of the two ports using PEEK
and then interpret the results appropriately.
When the Commodore 128 came out they added a JOY
command to replace having to PEEK
memory addresses. It works a little differently in terms of the values returned but it is a much more convenient approach.
Strangely, even though there were sufficient pins available, the C64 seemingly standardized on a single fire button during the entire production run outside of a few outliers, even when other systems were innovating with different controllers and inputs.
Reading Analog Signals

Speaking of different input controllers, we should talk about Paddles.
For those not around during the Pong heyday of the paddle, unlike joysticks and d-pad controllers that use switches and buttons, they are an analog type of input device. This means that rather than return a direction, the paddle returns a position between 0 and 255.
Really this means each paddle contains a “pot” (potentiometer), something found in a lot of electronics projects, and are used for dials, sliders, analog joysticks, etc.
This ability to provide an analog input also comes in handy for things like, say, reading the temperature.
The most simple project for people interested in retro games programming is obviously the analog joystick, so here we have the most basic stripped-down version:

As you can see, this analog stick takes pins for Ground, 5v, the X and Y, plus a switch.
On the control port these are reflected thusly:
As always, but especially here, be careful how you wire things up because it is easy to zap your computer by applying voltage where it shouldn’t go!
Output appears as a value between 0 and 255