How to emulate Commodore 64 Paddles

There are many that started their computer life with a Commodore 64 (C64), and I am not one of them, but I have still written a C64 emulator. One of the last pieces was paddle support, and since that was non-trivial and not really documented, I am going to write it all down.

1. Hardware Overview: Paddles and the 4066

Paddles are analog controls, typically a wheel you turn, with a fire button added. They were popularized by Atari 2600 and the game Pong.

While uncommonly used, the C64 has support for 4 paddles, even though it has only two Joystick Ports and two analog-to-digital converters. That is because each Joystick Port has two pins that can be connected to the analog converters. They are called X and Y. The names X and Y hint at the expected use case, two axes in an analog joystick, but for us that is just names.

To connect 4 paddles, you used a cable that split X and Y from a single joystick plug into separate cables to two distinct paddles, and with two such twins you can have 4 paddles connected, one for each analog pin.

The analog part of a paddle is a potentiometer, a controllable resistance. By turning the paddle, the resistance changes from ~100 Ω to ~500 kΩ. This controls how much current flows through the paddle pin.

To solve the problem with only 2 analog-to-digital converters, the signals go through a 4066 chip. A 4066 is a chip that opens or closes connections depending on some other signal. The C64 has the 4066 chip controlled by bits 6 and 7 from CIA1 Port A, which is set through memory address 0xdc00 (56320). When bit 6 (bit mask 0x40) is set, the chip connects Joystick Port A to the analog-to-digital converter, when bit 7 (bit mask 0x80) is set, Joystick Port B is connected. Yes, they can both be connected at the same time, which is part of why this is complicated. Several games do that, most likely because they are buggy, but who knows.

When I say connected at the same time, I mean it literally, the wires will be connected to each other with no components in between.

2. How the SID Measures the Paddle

The analog-to-digital converter in the Commodore 64 is the sound chip, the SID. A bit unexpected, but that is how it works. It has two independent analog-to-digital converters, one connected to a pin called Paddle X, one to a pin called Paddle Y.

To turn something analog into an 8-bit value, the SID will, for each pin, run through a 512-cycle process over and over again, never stopping.

For 256 CPU clock cycles the SID does nothing (though see later why), then for the next 256 cycles it counts the cycles, waiting for the voltage to reach 2.25 V on its Paddle pin. The clock cycle the voltage reaches 2.25 V the count stops. This will be the digital number for the analog value. If it never reaches 2.25 V, the digital number will be 255.

A program can read a digital value from SID registers 0x19 (X) and 0x1a (Y). In a C64, those are mapped to addresses 0xd419 (54297) and 0xd41a (54298). In documentation they are typically called Paddle X and Paddle Y respectively.

As you can see, the paddles control resistance, while the SID measures when a voltage is reached. For this to work, and make sense, the C64 uses a small 1000 pF capacitor next to each Paddle pin. That capacitor will be charged by the current from the paddle, and the more paddle current, the faster it will charge. With the values used, roughly up to 500 kΩ in the paddle, and a capacitor of 1000 picofarads, it takes at most a bit over 256 cycles to charge it to the required level, 2.25 V. Each paddle and each capacitor is slightly different so the values are probably chosen to make sure everyone can reach 255, even if the components deviate a bit from the specifications.

The remaining 256 cycles, which I called "doing nothing" are used to let the capacitor discharge for the next check.

All this happens asynchronously and a program might have to wait up to 512 clock cycles from setting a CIA/4066 pin to 1, thereby connecting the paddle to the SID, until the correct value appears in the SID. I do not know exactly when in this process the new value replaces the old in the register, if it is when the count stops or after the full 512 cycles.

The CIA1 pins, 6 and 7, are also used by keyboard scanning routines so those routines will have to be disabled meanwhile or measurements might be completely off. That is a problem for programmers, not authors of emulators though.

3. The math behind capacitor charging

To go from a general idea about how it works to actual cycle times we need some math. How fast does a capacitor charge? If you are not interested in exact numbers, the important part is that the time is proportional to the resistance.

If you are interested in exact numbers, as I was, it’s time to dust off the electrical engineering knowledge from university. Or do some Internet searches. I did both.

In a capacitor connected to a fixed voltage, the charging starts fast and then slows down as the capacitor becomes more and more charged, and there is more and more voltage that fights against the incoming voltage.

The formula for the voltage at the capacitor, the voltage that has to reach 2.25 V to trigger the SID, is:

V c = V s ( 1 e t R C )

Vc is Voltage at the capacitor, Vs is the Voltage that drives the resistance. R is the resistance in ohm (Ω) and C is the capacitance in farad (F).

Turning this around is a bit tricky but we get:

t = R × C × ln ( V s V s V c )

With our numbers, 125 Ω to 470 kΩ, 5 V, and 2.25 V we get about 0.07 µs to 281 µs. A clock cycle in the C64 is about 1 µs since it runs the CPU close to 1 MHz (slightly different frequencies for American and European C64s to match the different analog TV systems).

So we have about 0 to 281 clock cycles, which works out rather well. There will be a dead zone at the extreme counter-clockwise where the last 10% will all cap at 255 clock cycles, but that is it.

4. Multiple Paddles enabled

The next interesting part comes when a programmer has enabled multiple paddles at the same time. From the SID’s point of view, nothing will change, but with multiple paddles connected, there will be more current to the capacitor and it will charge faster.

When having two paddles connected, and enabled through the CIA pins, the potentiometers will become parallel resistances. The effective resistance is

R = 1 1 R 1 + 1 R 2

The behaviour will therefore depend on what position the other paddle has. If it is to the extreme clockwise position, then its potentiometer will have its lowest possible resistance, the capacitor will always charge in 0 cycles through that paddle, and you will be locked to the right edge in a game.

If set to the extreme counter-clockwise position, it will contribute some current but not as much and the capacitor will just charge a bit faster. Doing the math, it will charge in 0 – 150 cycles, and you might find it hard to reach the far left edge in your games. For that reason, at least Sea Wolf seems to not require extreme values.

If no paddle is connected, then the resistance will be infinite, there won’t be any current, and the game bug doesn’t matter.

I wonder if players of Sea Wolf noticed that disconnecting the other two paddles helped things. While Sea Wolf is designed to be played by two players, they would both be using paddles connected to Joystick Port A and with a split cable. Maybe those players didn’t even have anything to connect to Joystick port B and would never notice. Maybe that was also true of the Sea Wolf developer(s).

5. Emulation steps

To emulate paddles with gamepads, I went through the following steps:

5.1 Gamepad → resistance

First I got gamepad X and Y coordinates, which in raylib were returned in the form of a position in a circle. Those I transformed to be in a square with sides from -1 to +1, since the potentiometers need to be able to get to the extremes independently of each other. The resulting position was then converted to a resistance. I have used the 125 Ω to 470 kΩ range mentioned above, but people say that some paddles have much higher resistances so that might change.

5.2 Selecting and merging resistances

Depending on what flags were set in the CIA1 port A register, I picked the resistance from Joystick Port A, B, or merged them, or if neither was enabled, returned a practically infinite resistance.

5.3 Converting resistances to SID register values

Whenever someone accessed the Paddle registers in the SID, I converted the resistance to an 8-bit value by calculating the time that resistance would need to charge the capacitor, according to the formulas above, in clock cycles, clamping to 0-255.

I have not implemented the whole charge-discharge async cycle at this time, but it might be necessary for some program. If so, I will have to, for each cycle, check the current resistance, calculate how much charge has been added since the last cycle and how that has affected Vc, the capacitor voltage.

Possibly worth noting is that I’ve idealized the charging a bit. There are texts that say that the SID chip itself leaks current and helps charge the capacitor up to 1 V or so. I have not modelled that yet.

5.4 Fire button

There are also fire buttons on the paddles, two per Joystick Port, which could mean one per paddle, or two on an analog joystick. Those work exactly like joystick buttons, and actually share the pins with joystick left and right. Pressing such a paddle button would move a character in a game expecting a joystick, and since the pins are also shared with the keyboard, paddle buttons can, like joystick use, generate characters if activated while a keyboard scanning routine is running.

I assume it saved on cost to connect everything to the same pins, but it does make it harder for programmers that will have to figure out what everything means.

I hope this text will be helpful for the next person to write a C64 emulator. It is after all a very enjoyable hobby, so I expect there to be more people coming after me. If anything is unclear, let me know and I will update the article and answer your question to the best of my abilities.

Comments