NEWS, EDITORIALS, REFERENCE
NES to C64 Controller Mod
Just a few short years ago, it was almost a total mystery to me how the electronics of NES, SNES and other controllers worked. C64 controllers are easy. There is one wire for every button, plus one wire for ground. You push a button and that button's wire is connected to ground. 5 buttons plus ground is 6 wires. The 5 controller wires lead directly to pins on the CIA chip for either its Port A or Port B. The CIA port pins internally pull up, so every IRQ cycle your program can read them to see if they're low. Any port pin that's pulled low is because the button is depressed connecting it to ground. Simple.
But, how do these controllers with many buttons communicate with the computer? Especially when they have fewer wires but more buttons than a C64 controller. By what magic does this work?
This is gonna get a bit technical. Better buckle up!
How does the NES controller work?
First off, if you don't know much about digital electronics, but you really want to learn more, I recommend reading this: All About Circuits – Textbook – Vol IV – Digital
The whole site is basically free online education. Volume 4 of the textbook walks you through everything you need to know about digital electronics. I learned more about electronics from that tutorial than I had in my whole life up until that point. From there, the large fold–out C64 schematics, that came in the spiral bound C64 Programmers Reference Guide, actually started to make sense. And I began to be able to lookup the spec sheets on the chips to see how they work.
The short answer for how these controllers work, is that they use a shift register. But, to understand what a shift register is, we have to understand a number of more primative components. Let's quickly go over the components that combine together to build up to a shift register.
Remember, in digital electronics, one wire is one bit. If the wire is connected to ground, (if it is able to sink voltage,) then the bit value is 0 or off. If the wire is connected to some source of voltage, in a C64 it's near to +5VDC, then the bit value is 1 or on. Most people know this much.
The next concept is a transistor. A transistor is more or less a tap. Current can flow through the main tap if a positive current is applied to the control. These are also called amplifiers, because as you vary the amount of current on the control, it proportionally varies the amount of current that flows through the tap. The behaviors of transistors can be quite complex, and they are used in complex ways in analog electronics.
The next concept is a gate. Using an arrangement of transistors, logical units can be constructed such that one, two or more digital inputs combine together to produce a digital output. For example, a 2-input NOR gate is setup such that two wires lead into it and one out of it (plus power and ground.) If either one of those input wires is on (a source of voltage around +5V), the output wire will be off (sink's voltage to ground). But if both inputs are off, then the output will be on.
Below, you can see an example implementation of a NOR gate in Transistor-Transistor-Logic (TTL). It consists of 5 (!) transistors, plus several other simpler components (4 resistors and 2 diodes.) Source voltage comes in the top, ground is connected at the bottom. The two inputs are on the left and the output is on the right. Transistors are used, like this, inside the implementation of logic gates, but when thinking about digital electronics, one rarely needs to worry about the underlying behavior of the transistors. Instead, you can think of the gates as a black box. The gates are abstracted so much, their source and ground lines are rarely even shown, but in reality, every gate is connected to source and ground.
Stepping it up a level, we have the concept of a latch. Some very clever people once upon a time discovered that gates can be wired together with feedback, such that the output of one gate is fed back into one of the inputs of another, and vice versa. The nature of the arrangement allows the latch to be momentarily set, and henceforth retain its logic value in a stable feedback loop. So brilliant. Here's what a latch looks like:
That's an SR or Set-Reset latch. It's just two NOR gates feeding back on each other. One line sets the latch, the other resets the latch. The two outputs are labelled Q and Q/. Q is considered the binary value of the latch, while Q/ is the inverse value of Q, which can sometimes be useful to have.
The problem with a latch is that there is no regulation of when it should pay attention to its set or reset lines. Every time they change, its stored state picks up that change. So, one step beyond a latch is called a flip-flop. A flip-flop is a latch that also has an extra control unit (made up of other gates) that determines when its set and reset lines become sensitive to changes. A flip–flop thus serves as a fundamental unit of storage. One flip-flop (which as you can imagine may contain many tens of transistors) stores just one, single, solitary bit of information.
Above is shown an example implementation of a D-Flip-Flop, made up of 5 gates of three different kinds. There are also numerous kinds of flip-flops.
Now let's step it up again. A register is a series of flip-flops wired together in parallel. So, if you have 8 flip-flops in parallel, and each one stores one bit, then together the register stores 8-bits, or one byte. The control logic of each flip-flop can be wired together in parallel so that the entire register reads its set lines, or its reset lines, at the same time. This allows the register, for example, to be connected to a bus and to capture and store a whole byte. Now we're really starting to feel like we're using a computer.
Okay, so if a register is a bunch of flip-flops in parallel, then, what's a shift register? Pure genius, that's what it is. In a shift register the output of one flip-flop is fed into the input of the next flip-flop. So, by means of a little bit more control logic, the flip-flops can be put into two states, either they read all their values in parallel from the "bus" or whatever source they're connected to, or, they can shift. A shift means the state of flip-flop 1 is transferred into flip-flop 2. Meanwhile the state of 2 moves into 3, and 3 moves into 4, and so on all the way down the line. The state of the last flip-flop is usually lost. A shift happens on each pulse supplied to the whole, on a line that is called the "clock." It's the same concept as the CPU's main clock, but it isn't tied directly to the CPU's clock.
Okay, that's enough theory. What do we find inside a NES controller? There is precisely one chip. Printed on it is the number BU4021B. If you look that up, you'll find that the chip contains a single 8-bit static shift register! Here's what it looks like, and you can download the full datasheet here.
That's beautiful. Here you can actually see in the logic diagram how the register is structured at the level of the flip-flop. These are a kind of D-Flip-Flops. There are a variety of different kinds. You can see the clock line at the bottom, connected in parallel to the clock inputs of each flip-flop. At the top left, you can also see a P/S line that is connected in parallel to every flip-flop. This is the mode, it determines which input the flip-flop should read from at the moment when its clock line pulses.
The P and S stand for Parallel and Serial. If it's in parallel mode, then each flip-flop will read and set its value from the input coming in from the top. But if its mode is serial, then it will read its value coming in from the input on the left. Each left-input is connected to the output from the preceding flip-flop. That's a shift register, alright.
As it happens, in this particular chip, the outputs of the last three flip-flops are wired to pins available on the chip. These are labeled in parentheses as legs 2, 12 and 3. They're also labeled as Q6, Q7 and Q8. Those are the flip-flop values (Q) for the bit numbers 6, 7 and 8. All 8 of the flip-flop's parallel inputs are available as legs on the chip. These are labeled as P1 through P8, with their leg numbers in parentheses. Additionally, the common clock for all 8 flip-flops is available on chip leg 10. And the Parallel/Serial mode line, common to all flip-flops, is available on chip leg 9. Additionally, although I don't think it's relevant for this application, the serial input value for flip-flop 1 is also available as a leg. You could, in theory, wire up leg 3 to leg 11. This would connect the output of flip-flop 8 to the serial input of flip-flop 1. If you did this, when a shift occurs, the value of flip-flop 8, instead of being lost, would rotate around and feed back into flip-flop 1. After 8 shifts, the state of every flip-flop would be what it was when you started.
In total then, that's 8 parallel inputs, 1 serial input, 3 outputs, 1 clock, 1 mode, plus 1 line for voltage supply and 1 line for ground. That's 16 lines. And, in the chip block diagram on the right, it has 16 legs.
How does a NES controller use the BU4021B?
Now we know how a shift register works. And that the NES controller has an 8-bit shift register. But how does it use it?
There are 8 flip-flops, to store 8-bits. And, lo and behold, not coincidentally, there are exactly 8 buttons on a NES controller. Up, Down, Left, Right, A, B, Select, Start. (I feel like I'm putting in a game cheat.) Each button is paired, wired to the parallel input of one flip-flop.
Here's what the computer (the Nintendo in this case) needs to do. Set the mode to parallel. Pulse the clock line. When the clock line pulses, each flip-flop reads the up/down state of its button. Now, the entire register holds a byte of data, each bit representing the state of one button. Next, the computer sets the mode to serial. Then the computer reads the value of Q8, that's for whatever is button 8. Next, the computer pulses the clock. The flip-flops shift such that the original value 7 is now in 8. It reads Q8 again for button 7. It repeats the clocking 6 more times to read in all 8 bits. And its done.
Each time the computer reads in a bit, it shifts that bit onto its own memory byte somewhere in main memory. After looping through each bit, the computer now has a byte in memory that represents the state of all the buttons. Amazing sauce.
If the computer performs this entire procedure sufficiently frequently, like, say, 60 times a second, it always has an up to date representation of the up/down status of all 8 buttons. Now, for the computer to do this, it needs: 1 clock line, 1 mode line, 1 input line, plus, it needs to supply both voltage and ground to make the chip work. That's 5 lines total (for 8 buttons). The controller port on a Nintendo NES has 7 holes, but, 2 of them are unused or reserved for use with non-standard controllers. There are in fact only 5 wires inside the cable of a NES controller.
How do we use a NES controller on a C64?
There are two ways. One way is easy to do and to describe, but hard to use. The other way is harder to do, but is the most useful. So I'll describe it in the most detail.
Wire Map and Custom Software
There are only 5 wires in a NES controller cable. The easiest thing to do is to simply connect the 5 wires of the controller cable to 5 pins in the DB9 controller plug of a C64. You could do this by chopping off the original NES plug, and splicing on a DB9 plug. Or, you could do it by finding and buying a female NES controller jack and building a short adapter cable. Either way works fine, it just depends on how hard it is to find that rather custom jack, vs how much you don't want to mutilate the original controller.
The problem is how you can use this controller on your C64. The only way to use it is with a custom routine that manipulates the CIA to control the P/S, Clock, Serial Input, +5 and GND lines of the NES controller. You can do it, and you can support this in your games. In fact, I love the idea and would love to see some games that make use of all 8 buttons this way.
But the reality is that none of the existing C64 software, thousands and thousands of existing games, have support for this new controller protocol. For most people, this solution would be unsatisfying.
Rewire as a Standard C64 Joystick
What makes a lot more sense, for compatilibity with the vast existing C64 software library, is to rewire the buttons to the traditional layout of a C64 controller.
The problem with this, is that the C64 Joystick has only 5 buttons, not 8. We will wire the D-Pad to the traditional stick up/down/left/right. The Select and Start buttons will not be used at all. The B button (leftmost of B and A) will be wired to the traditional fire button. And the A button (rightmost of B and A) will get a bit of special love. We will mount a switch through the controller case that toggles button A between being a duplicate of button B (Fire), and a duplicate of the D-Pad Up. D-Pad Up is used by many C64 games for jump. Allowing button A to function as a jump button goes a long way to making most C64 games feel like they are controlled the way most Nintendo games work. B for attack, A for jump. Up, of course, will continue to work as jump in these games as well. I warn you, this will permenantly change your NES controller. It will never again be usable on a Nintendo Entertainment System.
If you want to buy some new NES controllers just for the purpose of modifying for your C64, though, these are readily available on Ebay, usually for just a few dollars a piece.
Remove the 6 screws that hold on the back panel of the NES controller. Inside, you'll find a circuit board with no traces on the back. Only the BU4021B IC, and the cable with 5 wires. You can easily lift the circuit board out of the front half of the controller. While the front half of the controller faces down, the buttons simply hang in their holes, and are covered over in groups by some sort of silicone rubber. These covers provide the buttons their springy feel, and also their electrical connections, closing the switches, when they're pressed against the circuit board.
This particular NES controller I converted to C64 when I was about 15 years old. It's safe to say that my aesthetic taste, and my self–pride have dramatically risen in the intervening 20 some–odd years. Originally, I cut off the NES cable leaving stubs of its wires behind. And soldered the joystick cable wires onto the underside of the PCB overtop of the IC's soldered legs.
It looks like shit.
And, it was so poorly soldered, that I had to use bits of masking tape to wrap the exposed wire so they wouldn't short against each other. Please, have some self–respect, and don't do it like the 15 year old version of myself did all those years ago.
The 8-Bit shift register is critical to making a NES controller work, the way that a NES expects it to work. And for transfering state data about 8 buttons over a mere 5 wires. But the C64 does all of the button scanning using a CIA chip on its main board. In a C64 joystick, there is nothing but 5 switches; normally open, momentary, push buttons, essentially. What this means is that we do not need the BU4021B IC at all.
I recommend removing it entirely. But, be gentle with it. I used a solder sucker and pulled the solder out of each hole, and with a bit of love was able to extract the chip unscathed. You won't need the IC for this project, but, hey, it's a perfectly good 8-bit static shift register! You should hang onto it for use in other electronics projects. You can also desolder the NES cable from its 5 holes. These holes and the cable are not used in this project. You could hang on to the NES cable too, you never know what you might be able to use it for.
The traces on the circuit board may be a bit old, so be careful as you're unsoldering and extracting the IC. I accidentally caused one of the traces to lift. I had to repair it by scraping away some of the non–conductive coating, and manually lay down a solder trace. It worked but it would have been a lot nicer if I hadn't needed to do that repair at all.
Speaking of the traces. There are not many of them, and they're easy to follow along with your eyes. I did this for you, which spares you the trouble of doing it yourself. This is to map each button to the original pins of the IC, so we know how to hook them up to our C64. Here's how the cleaned PCB looks, and the mapping of buttons to pins.
Here's the general idea. We saw in our block diagram above that PIN 8 of the BU4021B is ground. That means all the buttons have at least one trace that leads to the through–hole for this leg. The C64 doesn't need to provide +5V to anything, because the switches are all passive. Instead we want 5 of the 8 parallel in leg holes to be wired to our 5 button lines. Except for the special handling of the A button, that's actually all that's needed. When a button is pressed, it will connect ground to one of the P-leg holes, which is wired to one of the joystick lines. That connects that CIA leg to ground which is all the C64 joystick does.
According to investigation, the NES buttons are wired to the shift-register's P-bits like this:
|Parallel Input||Button||IC Pin Number|
I used the C64 Programmer's Reference Guide to know which pins of the DB9 joystick port are for which buttons, and a simple continuity test to determine which colors of wire were hooked to those pins in my joystick cable. Your mileage may vary here, I'm not sure if these colors are standard or not.
This time around, now that the IC has been removed, the joystick cable can be attached via the back side of the PCB. The cable wires can be passed through the holes for the original IC and soldered to the front side. Here's how that ended up looking. I'm not the best with a soldering iron, but this looks a hell of a lot better than my first hack job.
Hooking up the A Button
In the simplest case, this is all you need. You can seal your NES controller back up and start using it. Up, down, left and right will work, and the B button will work as fire. All the other buttons will just do nothing. But, if you want to have the A button do something more interesting, we can proceed to the next step.
If you want to hardwire the A button either to a duplicate of B or a duplicate of Up, you can simply add a short wire that will jump pin 1 to pin 15 (for A == B), or pin 4 to pin 15 (for A == Up). But I'd rather put in a switch to toggle that assignment.
Now's a good time for a lesson about switch terminology. I found these brilliantly labeled images and am showing them here for educational purposes.
The terms are, poles and throws. Let's cite the guide for dummies.
A switch pole refers to the number of separate circuits that the switch controls. A single-pole switch controls just one circuit. A double-pole switch controls two separate circuits.
A double-pole switch is like two separate single-pole switches that are mechanically operated by the same lever, knob, or button.
The number of throws indicates how many different output connections each switch pole can connect its input to. A single-throw switch is a simple on/off switch that connects or disconnects two terminals.
A double-throw switch connects an input terminal to one of two output terminals. Switches In Electronic Circuits: Poles And Throws — www.dummies.com
By this logic, what we want is a single pole double throw switch, that is always on, aka, it is always connecting its middle pin to one of either the two outer pins. The middle pin of the switch, we will connect to the A button, or, the hole for IC pin 1. The two outer pins of the switch shall be connected, one side to the B button, pin 15, the other side to the Up button, pin 4.
Surely these have gotta come in handy for other projects, or even for other NES Controller mods, right? So I bought a pack of 15 off Ebay, for very little money.
When the switch is in the former position, and you press the A button, A button will connect ground to the switch, and from the switch to the B button, thus A will produce a Fire. When the switch is in the latter position, and you press A, A will connect ground to the switch, and from the switch to Up, thus A will produce an Up.
There is a little bit of space inside the controller case, above and slightly left of the D-Pad. The switch will have to go there. I ran three short wires over to this corner. One wire, the pin 1 A wire, I ran across the back of the PCB, because it's the only wire in its hole. But the other two, they lead from B and Up, which already have wires, so I soldered these carefully to the pre-existing wires on the front of the PCB.
Let me be straight, the switches I bought barely fit in the case. But, I did successfully manage to fit one in. In order to make room I had to use my dremel to remove about half of one of the molding pegs for one of the screw holes. The screw still catches, but otherwise, there was no where else for the switch to fit. I screwed the two halves of the case together first, and drilled a circular hole right through the seam. This was to get an evenly shaped circular hole that was partially in the top case and partially in the bottom.
Solder the wires onto the switch. Pop the switch into its new home. And close the case down around it. It's a snug fit, but it fits. The switch comes with a little screw down ring for the outside. It's not necessary for keeping the switch in its place, but it does make the outside look a little cleaner.
And there you go folks, that's the finished mod. Here's how it looks.
Since we all love 8-bit Commodore porn, here's how it looks hooked up to my C64 Reloaded, with 1541 Ultimate II+, C64c topcase removed to reveal the glory.
And, last but not least, we have to test it out, right?
What better game to test it out on than the brilliant new Sam's Journey. This game, like many others, uses up for jump. There is another interesting tidbit about this game. Up is used both for jump, and for climbing vines. As it happens, it feels kind of weird to climb a vine by pressing A. However, up for jump and climb still works. So, as I actually play the game, I use A for jump and up for climb, and it feels very natural.
My older brother has two sons, about 9 and 12 years old. A few weeks ago they visited us when my parents were watching them for a weekend. My parents told them that their crazy uncle is into retro computers. They really wanted to play some games, so I booted up Sam's Journey, and gave the familiar NES controller to the older of the two. He loved it! He got right into it and started exploring the world. He found secrets, carried springy trampoline things around, and didn't want to leave it behind when it was time for them to go. Here's what's interesting, I told him, "B is to attack, A is to jump, and if you find a vine you can climb it by pressing up." He played for at least an hour, and never noticed that A and Up actually do the same thing.
I'd call this NES controller mod… a success. Until next time, keep lovin' your C64!
Do you like what you see?
You've just read one of my high–quality, long–form, weblog posts, for free! First, thank you for your interest, it makes producing this content feel worthwhile. I love to hear your input and feedback in the forums below. And I do my best to answer every question.
I'm creating C64 OS and documenting my progress along the way, to give something to you and contribute to the Commodore community. Please consider purchasing one of the items I am currently offering or making a small donation, to help me continue to bring you updates, in–depth technical discussions and programming reference. Your generous support is greatly appreciated.
Greg Naçu — C64OS.com