Arithmetic Logic Unit
The next suggested step by Grok was to add an ALU.
Adding an ALU (Arithmetic Logic Unit) is an exciting next step, taking your setup from a memory and register system to something closer to a full-fledged CPU. Let’s dive into what an ALU is, how it fits into your 4-bit address and data bus architecture, and how you can add it to perform operations like addition or logic functions on your data.
What Is an ALU?
- Definition: The Arithmetic Logic Unit is the part of a computer’s CPU that performs arithmetic operations (e.g., addition, subtraction) and logical operations (e.g., AND, OR, XOR) on binary data. It’s the “calculator” of the processor.
- Purpose: Takes two input operands (e.g., numbers from registers or memory), processes them based on a selected operation, and produces an output result, often with status flags (e.g., carry, zero).
Key Features
- Inputs: Two sets of bits (e.g., 4-bit A and B) and a control signal to choose the operation.
- Outputs: Result (e.g., 4-bit sum) and flags (e.g., carry, zero, sign).
- Operations: Common ones include:
- Arithmetic: Add, subtract, increment, decrement.
- Logic: AND, OR, XOR, NOT.
How It Fits Your Setup
- Your System: 4-bit address bus, 4-bit data bus, 16x4 SRAM, MAR, MDR, two registers, MUXes for address/data input.
- ALU Role: It’ll connect to the data bus, taking inputs from your registers, RAM, or MDR, performing an operation, and sending the result back to the data bus for storage (e.g., in a register or RAM).
- Size: A 4-bit ALU matches your 4-bit buses and registers perfectly.
Common ALU IC: 74LS181
(This seemed a bit complex, skip for now)
Alternative: 74LS283 (Simpler 4-Bit Adder)
If you want a simpler start, the 74LS283 is a 4-bit binary adder:
- Inputs: A0–A3, B0–B3, Cin.
- Outputs: Sum0–Sum3, Cout.
- Limitation: Only does addition (no logic or subtraction without extra gates).
74LS283 Simple 4-Bit Adder
Example:
- Set A to 0101 (decimal 5)
- Set B to 0011 (decimal 3)
- Set Cin (C4 pin 9) to 0 (no carry in)
- Then Sum is 1000 (decimal 8) and Cout = 0 (no carry out)
Example 2:
- Set A to 1011 (decimal 11)
- Set B to 1001 (decimal 9)
- Set Cin (C4 pin 9) to 0 (no carry in)
- The sum 20 is larger than what fits in four bits. 20 = 10100. Cout is 1 and Sum is 0100
XOR gate
I asked Grok about the design of a 74LS283 and it apparently uses and XOR gate, so let's start there. The XOR gate is a basic gate with the following symbol:
This is the truth table for XOR:An XOR gate can be built using transistors. It can also be built from other basic gates. There is also three input XOR gate:
The three input XOR is basically two regular XOR gates:
This is the truth table for a three input XOR:
Note that Y = 1 if an odd number of inputs are 1. Let's get back to the design of a 74LS283 and let's begin with addition of single bits.
Full adder
"How can you build a one bit adder using basic gates."
What a 1-Bit Full Adder Does
- Inputs:
- A: First bit (e.g., from Register 1).
- B: Second bit (e.g., from Register 2).
- Cin: Carry In from a previous bit (0 or 1).
- Outputs:
- Sum: The result of A + B + Cin (0 or 1).
- Cout: Carry Out to the next bit (0 or 1).
- Truth Table:
We see that "Sum" is precisely the XOR of A, B, and Cin. Cout is a bit trickier. Here is Grok's explanation translated into a picture:
It is not difficult to see that this setup gives the correct value for Cout as well. For example, in row 4, A = 1, B = 1 and Cin = 0. The upper AND gate is 1 and Cout is 1. This is the image I will use for a full adder:
ACEL has an IC called FullAdder implementing these gates. Try it out on Wokwi (Full Adder):
- In the picture, A = 1 and B = 1 while Cin = 0.
- 1 + 1 = 10 (decimal 2).
- Therefore, Sum = 0 (lowest order, right-most bit)
- And Cout = 1 (highest order, right-most bit)
Inside 74LS283
Scaling to 4 Bits (Your System)
To match your 4-bit setup:
- Build 4 full adders (one per bit: A0–A3, B0–B3).
- Chain them:
- Cout of bit 0 to Cin of bit 1.
- Cout of bit 1 to Cin of bit 2.
- Cout of bit 2 to Cin of bit 3.
- Cout of bit 3 as final carry.
So like this:
ACEL has an IC called FourBitAdder with four full adders connected as in the picture. Try it out on Wokwi (Four bit adder):
In the picture, A = 0110 (decimal 6), B = 0100 (decimal 4) and Sum = 1010 (decimal 10). Try out the following to fully understand the four bit adder:
ALU
For now, I am happy with an ALU that can just add. We'll improve on it later. Note that FourBitAdder has two state output. I also mad FourBitAdderTriState with four tri-state buffers. This is the symbol I will use for FourBitAdderTriState, the first version of the ALU: