Computers are in everything today. From your smartphone, to your microwave, to your car, it's impossible to go through a day without using a computer of some sort. But how does a computer actually work? How does a computer... compute?

The goal of this post is to give a simple, interactive tutorial on how to build a CPU (Central Processing Unit). Index:1. Logic Gates2. Memory3. Multiplexer/Demultiplexer4. ALU5. CPU Layout6. Putting it all together

Logic Gates

AND Gate
The AND gate propagates a signal through if both of the inputs are on. (Click on the circles to the left of the gate to play around with it!)
OR Gate
The OR gate propagates a signal if either of the inputs are on.
NOT Gate
The NOT gate (also called an Inverter) propagates a signal that is the opposite of the input.
When a circle is drawn at the end of a gate, it is the same as putting a NOT gate at the end.
The XOR gate is short for Exclusive OR. This means that it is an OR gate that excludes when both of the inputs are on.


RS Latch
The RS Latch is a simple gate configuration that allows you to store a bit of memory.
RS FlipFlop
The RS FlipFlop is the same as before but has a little extra to allow for a clock input, which only allows the data to change when the middle switch is on.



Half Adder
These gate configurations allow for adding bits together.
Inputs  Outputs 0 + 0  =  00 1 + 0  =  01 0 + 1  =  01 1 + 1  =  10
Full Adder
The full adder allows carry bits which are necessary for multi-bit numbers. It uses a new input called a "carry in" (sometimes abbreviated Cin). Remember when you carry the 1 in addition? It is the exact same concept.I will be using abstraction for simplicity sake. By this, I mean I will represent complex gate structures by a "black box", or a rectangle with inputs and outputs. Notice how this does the same function as the full adder but is a lot easier to represent and understand.
4-bit Adder
This shows how the full adders cascade the bits when used together.

CPU Layout

Putting it all together