Introduction: 8-bit ALU (Arithmetic Logic Unit)
In this Instructable, I will be going through the process of building and understanding the architecture behind an ALU (Arithmetic Logic Unit). The ALU is part of a computer's CPU (central processing unit) and it handles arithmetic and logic operations as the name suggests.
The ALU that we will be building is a far simpler version than what may exist on your computer. More complicated ALUs may handle 64-bit numbers and have many more functions than just addition and subtraction. The ALU that will be discussed here will only be able to perform addition and subtraction on 8-bit numbers and will be built from 74HC logic using XOR, AND, and OR gates. All of which will be explained in detail in this instructable.
Supplies
Supplies
- 26 AWG Multicore wire (4 meters)
- 4x 4-way DIP SPST switches
- 1x PTM Switch (through hole)
- 24x 470 Ohm resistors
- 24x 5V LEDs
- 5V Adapter
- 30 cm x 10 cm perf board
- 6x CD4070 (XOR gate)
- 4x LS7408 (AND gate)
- 4x LS7432 (OR gate)
- 5V Adapter
Tools
- Soldering Iron
- Solder
- Wire Cutter/Stripper
- Multimeter
Step 1: Binary
As with most digital circuits, data is transmitted in a binary format. Binary is a numerical system in base 2, meaning binary numbers are composed of either 0s or 1s. 0s can be represented as LOW (GND) signals whereas 1s can be associated with HIGH (for our purposes, 5V) signals. The decimal value of a binary number can be determined by positions of 0s and 1s. Each 1 or 0 value expresses a certain power of 2. Each 1 or 0 is referred to as a bit of data.
Take the 4-bit examples:
[1 0 0 1] = 2^3 + 0 + 0 + 2^0 = 8 + 1 = 9
[1 1 1 1] = 2^3 + 2^2 + 2^1 + 2^0 = 8 + 4 + 2 + 1 = 15
Step 2: Logic
Digital logic refers to the manipulation of binary values using integrated circuits (ICs). These circuits are known as logic gates. There are many types of 'gates', all of which are composed of a specific arrangement of transistors (if you would like to learn more about transistors, I'd recommend the following video from the channel Real Engineering). The three logic gates we will be focused on are AND, OR, and XOR logic gates.
Logic gates usually take 2 inputs and produce an output dependent on the inputs. An AND gate will only produce a HIGH output if both inputs are HIGH. OR gates will only produce a HIGH output if at least one input is HIGH. XOR logic gates will produce a HIGH output if and only if one input is HIGH and the other is LOW.
AND Gate Logic | ||
---|---|---|
INPUT 1 | INPUT 2 | OUTPUT |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
OR Gate Logic | ||
INPUT 1 | INPUT 2 | OUTPUT |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
XOR Gate Logic | ||
INPUT 1 | INPUT 2 | OUTPUT |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Step 3: Full Adder
Using 3 digital logic gates (AND, OR, and XOR), we can create what is known as a Full Adder circuit. A full adder takes in 3 inputs. A carry and two binary inputs. An adder is meant to 'add' two binary inputs. If A and B are both 0 (LOW signals), the output will be 0, assuming there is no carry. Likewise, If A or B is HIGH and the other is LOW, the output will result in a HIGH signal. If both are HIGH, and the carry-in is LOW, the carry-out will trigger HIGH while the sum will be LOW.
The carry output can then be routed into the carry-in of an additional adder circuit to increase the capacity of the Adder. Our 8-bit ALU will require an 8-bit adder which requires 8 of these circuits to be wired in series as demonstrated in the next step.
Step 4: ALU Design
As mentioned in the step above, our 8-bit ALU requires an 8-bit adder for each of the 8 binary values. As such, the design above demonstrates the configuration of 8 full adder circuits where the carry is passed on from each full adder and into the next.
What about subtraction?
Our ALU should be able to add and subtract 8-bit numbers. However, so far we have only discussed addition. Luckily, there is a relatively straight-forwarded solution to this. Subtracting numbers, say A - B, is the same as adding A + (-B). Using two's-complement, a number system that allows us to represent negative numbers in binary, we can change B to -B.
- The method involves first inverting all the numbers in B. Let B = 00011100 (28 in decimal):
- With all digits inverted, we get: 11100011
- Next, add 1:
- adding one gives us: 11100100
As such, -B = 11100100 which is (-28 in decimal - two's complement)
Implementation
We need a way of inverting all B inputs on command to signal subtraction. An XOR gate allows us to do just that. If we have 8 XOR gates and the first input into all of them is the corresponding B digit and the second input was a wire that we controlled to activate subtraction/addition, running the wire HIGH would invert the output of all gates (check the logic table for XOR gates earlier). As for adding one, we can run this same subtraction/addition line into the very first carry-in input to the first full adder circuit. When the line goes HIGH (indicating subtraction), it will add one through the carry-in. This implementation can be seen in the diagram on the left.
The images on the right detail the different operations of addition vs subtraction. If you would like to play around with this circuit yourself, the link to the circuit in Logisim can be found here.
Step 5: Scaled Down Prototype
Now that the design is complete, it's time to start testing the circuit components!
I would recommend creating a smaller 2-bit scaled-down version of the adder circuit. Keep in mind that each IC (LS7408, LS7432, and CD4070) has 4 respective logic gates. Since one full adder circuit requires 2 AND gates, 2 XOR gates, and 1 OR gate, one of each IC can be used to build at most a 2-bit adder circuit as shown on the breadboard above. This setup will allow you to ensure that your understanding of the circuit is correct and that all your IC components are working properly.
2 Bit Adder (output examples) | ||||||||
---|---|---|---|---|---|---|---|---|
NUMBER 1 | 0 | 0 | NUMBER 1 | 0 | 1 | NUMBER 1 | 1 | 1 |
NUMBER 2 | 0 | 0 | NUMBER 2 | 0 | 0 | NUMBER 2 | 0 | 1 |
SUM | 0 | 0 | SUM | 0 | 1 | SUM | 0 | 0 |
NUMBER 1 | 1 | 0 | NUMBER 1 | 1 | 0 | NUMBER 1 | 1 | 1 |
NUMBER 2 | 0 | 0 | NUMBER 2 | 0 | 1 | NUMBER 2 | 1 | 0 |
SUM | 1 | 0 | SUM | 1 | 1 | SUM | 0 | 1 |
Step 6: Building the ALU
Building the full ALU can be a very tedious process, but it is incredibly important to keep things organized!
- Start by getting familiar with the spacing on your perf board and position the ICs in the order to that suits it best. I went with 4 duplicates of the test adder we will above. The other two ICS on the left is the XOR gates that the B inputs run through.
- Solder the ICs in place, ensuring correct orientation. If you have access to IC holders, use those! They allow you to insert the ICs into the board after soldering is done. This protects them from the heat which could potentially damage internal circuits. Be quick but precise with the soldering to avoid this.
- Insert the 4x 4-way DIP switches along with the corresponding LEDs and solder the connections. Use multicore wire to connect the switches to the LEDs. The other end of the DIP switches can all be soldered together as a common VSS (5V source).
- Proceed by making sure all ICs are grounded and supplied with 5V power. Use a distinct multicore wire color to differentiate these wires. It'll be very helpful during the debugging process.
- Continue by configuring the 'subtraction line' connection 8 inputs of the XOR gates along with the carry input of the first full adder. Connect this to a PTM switch and a 10K resistor to GND (the other side should be connected 5V).
- Now start with all the adder connections. Everything from the inputs to the carry lines between the adder circuits.
- Finally, finish off by connecting the output from each adder circuit to the 8 final LEDs (use 470-ohm resistors to connect the other end to GND).
*Note about power: I supplied 5V and GND to the circuit from a 5V 1A adapter I found lying around. I cut off the end of the wire and stripped it to find the separate 5V and GND lines. I then attached these to the relevant locations on the perf board.
Step 7: Debugging
When you finally finish the circuit, it is bound to have bugs and errors. You could have used a wire that isn't conductive anymore, an LED that is burned out, or in the mess of all the solder, there could be a short between two pins. At this stage of the process, it is incredibly helpful to have a multimeter. The primary functions that you will be using are the DC voltage read and connectivity.
Problems that I encountered.
- LEDs randomly turning on and off - make sure the circuit is suspended in the air or properly secured on the table. Any angle or bend to the perf board can cause unintended shorts and grounding issues.
- No connectivity between wires/pins - sometimes, especially without the use of flux, dry joints between soldered joints can form due to the oxide layer that forms during soldering. Fix this by re-soldering the joint. The issue may also be caused by a bad wire, be sure to check connectivity before soldering these jumper cables.
- Inconsistent outputs - ensure that all ICs are receiving 5V and GND at the necessary pins.
- Floating voltage - these issues can generally be solved by securing a 10K resistor (pull-down resistor) from the pin to GND. But this is a bandage fix and often has other causes.
Debugging is often painful and tedious. If all else fails, follow the route of the current pin by pin until you discover the short/issue. A good quality and performing multimeter is your best friend in these circumstances.
Step 8: Next Steps...
The ALU is just one component of a computer. If you enjoyed this project and wanted to take it further, you might want to start making other parts of an 8-bit computer (RAM, registers, etc.). I'd recommend checking out this channel (Ben Eater) for more on designing the rest of an 8-bit computer from scratch. Such a project cements the basics of computer architecture which is both incredibly interesting and useful to learn about.
I hope you enjoyed the process of designing the building for this ALU :)