How a CPU Works

The Central Processing Unit (CPU) is often called the brain of the computer. But unlike a biological brain, it doesn't "think" in vague terms. It executes precise, simple instructions one after another at blinding speeds.

In this article, you will find a step-by-step explanation of how a CPU works. With interactive simulations, you can see how each component works and how they work together.

NAND Gate Simulation

Step 0 / 2

We start with a single NAND Gate.

The CPU acts as the central command center for all computer operations.

D Flip FLop

Computes need to compute, and therefore they need to store numbers. How can we store numbers? With a flip flop!

D Flip-Flop (Latch)

Step 0 / 2

We start with 5 NAND Gates arranged for a D-Latch.

A D Flip Flop is a simple flip flop that stores a single bit.

Register

8-bit Register

View: 8 D-FlipFlops

To store a Byte (8 bits), we need 8 D-FlipFlops working in parallel. Wiring inputs, outputs, enable, and power to 8 independent circuits acts as a "Parallel Interface".

A Register is a collection of flip flops that store a byte.

ALU (Arithmetic Logic Unit)

The ALU is the mathematical heart of the CPU. By combining many logic gates, it can perform addition, subtraction, and logical operations. First how can we do arithmetic with binary numbers? THen how do we combine this into an ALU?

ALU (Arithmetic Logic Unit)

The ALU is the calculator of the CPU. It takes two numbers (A and B), performs a math operation, and outputs the result. Click the numbers to change them, and toggle the SUB switch to change from ADD to SUB.

The ALU takes two inputs and a selector to perform computation.

MUX

Multiplexer & Bus Transfer

This unit demonstrates how a Bus and Multiplexers (Mux) coordinate data movement.
Goal: Transfer a number from Register A to Register B.

  • 1. Set a value on the top-left Byte Source.
  • 2. Toggle LD A to load it into Register A.
  • 3. Enable AO (A Out) on the left Mux. The value is now on the Bus.
  • 4. Enable BI (B In) on the right Mux. Register B now sees the Bus value.
  • 5. Toggle LD B to load the value into Register B.

You can also move data from B back to the Bus using BO (B Out).

A MUX is a multiplexer that selects one of many inputs.

Program Counter

Program counter and the clock

Step 1 / 2

Step 1: Inside the Counter.
Here we see how 4 D-FlipFlops can be chained together to count pulses. Click the PULSE button on the left to manually advance the counter. Each FlipFlop toggles its state when it receives a rising edge provided by the previous FlipFlop.

The Program Counter is a register that stores the address of the next instruction to be executed.

The rest of the fucking owl

the ordering of the chapters about might need to change. We can introduce the bus sooner, and talk about register transfers we can introduce MUX and control signals at that point.

  1. Clock signal - Clock signal, we need to briefly discuss the clock signal. We need to know when to perform operations.
  2. Bus - Bus, we need to briefly discuss the bus. We have registers, we have the ALU, we have the RAM, how do we connect them to each other? We can just wire everything together, but that's not a good idea. We do register transfer here.
  3. RAM - Random Access Memory, we need to store data at some point. we need more than two bytes off course. We need to store the software, results, and instructions, how can we do that? We talk about:
    1. How ram works, how can we store data in ram, FlipFlops are too expensive for that.
    2. Adressing, how do you get things into and out of ram?
  4. Program counter - we need a program counter to keep track of where we are in the program. How does binary counting work? And how can we implement this on a cpu?
  5. Control signals - we need to control the cpu. How can we do that?
  6. Instruction decoder - we need to decode the instructions. How can we do that?
  7. Turing completenes - when is our computer a computer, what is missing?
  8. Coding - going from assembly to instructions
  9. CPU Flag register - we need to know when we overflow, underflow, divide by zero, etc. conditional jumping to make it turing complete
  10. Conditional jumping - we need to jump to a different instruction when a condition is met. How can we do that?
  11. The complete CPU - we can now build a cpu.
  12. Final thoughts - we can now build a cpu, but it's limited. How can does the cpu we created differ from the cpu in the device you are using right now? We talk about, pipelining, cache, smaller scale, clock speeds, etc.