Wednesday, April 2, 2025

Entry 22: ALU


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."

Building a 1-bit adder (also called a full adder) using basic gates with Cin (Carry In) and Cout (Carry Out) is a great way to understand the fundamentals of arithmetic circuits, like the ALU you’re planning to add to your 4-bit system. A full adder takes three inputs (A, B, and Cin) and produces two outputs (Sum and Cout), allowing it to handle addition with carry propagation—perfect for scaling to your 4-bit setup later. Here’s how to design it step-by-step using basic gates (AND, OR, XOR), which you can implement with ICs like 74LS08 (AND), 74LS32 (OR), and 74LS86 (XOR).

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:

  Demonstration of a four bit adder
  Set A to 0110 (decimal 6), Left DIP 6,7 ON
  Set B to 0100 (decimal 4), Right DIP 6 ON
  Note that Sum = 1010 (decimal 10)
  Click on Cin, Carry In. This adds one, Sum = 1011 (decimal 11)
  Set A to 1011 (decimal 11)
  Set B to 0111 (decimal 7)
  Note that Sum = 0010 but Carry out is on. The sum is therefore 10010 (18 decimal)

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:

The write pin controls the tri-state buffers. Q has the sum of A and B while W = Q if Write = 1 (else W  = Z). Next, I want to test the ALU by connecting A and B to two registers... in the next entry.

No comments:

Post a Comment