// ~/playground/one-gate

One Gate

Build a computer from one gate

This guide starts with one switch. It ends with a machine that can calculate anything. Each step uses only the parts from the step before.

Push the A and B inputs on the gate. The lamp shows the output. You can also click a row of the table.

1: high voltage 0: no voltage
Figure 1. A NAND gate. The output is 0 only when A and B are both 1.
1

The switch

A transistor is a switch with no moving parts. An electric field opens it and closes it.

A transistor has three connections. They are the source, the drain and the gate. Electrons can move from the source to the drain. The gate controls this movement.

The gate does not touch the silicon. A very thin layer of glass is between them. Glass is an insulator. Thus, no current flows into the gate.

When you put a voltage on the gate, the gate makes an electric field. The field goes through the glass. It pulls electrons up below the glass. These electrons make a path between the source and the drain. This path is the channel. Now the switch is closed.

When you remove the voltage, the electrons move away. The channel disappears. The switch is open.

Figure 2. A cut through one transistor. The arrows show the electric field from the gate.
  1. Push Gate voltage.
  2. Look at the electrons. They fill the channel and move from the source to the drain.
  3. Push Gate voltage again. The electrons go back.
Note:In a modern chip, the glass layer is only a few atoms thick. One chip has billions of these switches.
2

Four switches make a NAND gate

This gate is the base for all the other steps in this guide.

A chip uses two types of transistor:

TypeThe switch closes when the gate isSymbol in the figure
N1 (high voltage)Gate line without a circle
P0 (no voltage)Gate line with a small circle

The two types work as a pair. Engineers call this CMOS (complementary metal-oxide-semiconductor).

NAND means "not AND". The output is 0 only when A is 1 and B is 1. For all other inputs, the output is 1.

Figure 3. A NAND gate made of four transistors. The P switches are at the top. The N switches are at the bottom. You can also click the A and B circles in the figure.

Look at the top half of the figure. Two P switches connect OUT to the supply. They are side by side (in parallel). One closed P switch is sufficient to make OUT 1.

Look at the bottom half. Two N switches connect OUT to ground. They are one after the other (in series). Both N switches must close to make OUT 0.

When one half connects, the other half disconnects. Thus, no current flows from the supply to ground.

  1. Set A to 1 and B to 1.
  2. Look at the two N switches. They are closed.
  3. Set B to 0.
  4. Look at which half connects to OUT now.
Note:The gate uses power only when OUT changes. The light pulse on the wires shows this moment. Each change also makes a small quantity of heat.
3

NOT: one NAND with its inputs tied

NAND is universal. You can make all the other logic gates from NAND gates only. Start with NOT.

From this step, the guide shows each NAND gate as one symbol. Each symbol is the four switches from step 2.

A NOT gate changes 1 to 0 and 0 to 1. To make a NOT gate, connect the two inputs of a NAND gate together.

If A is 1, both NAND inputs are 1. Thus, the output is 0. If A is 0, both NAND inputs are 0. Thus, the output is 1.

Figure 4. NOT made from one NAND gate. The dot shows where the wire divides.
  1. Push A.
  2. Look at the output. It is always the opposite of A.
4

AND: NAND, then NOT

An AND gate gives 1 only when both inputs are 1.

A NAND gate gives the opposite of AND. Thus, put a NOT gate after the NAND gate. The NOT gate cancels the "not" in NAND.

This circuit uses two NAND gates. The second gate is the NOT gate from step 3.

Figure 5. AND made from two NAND gates.
Note:A truth table shows all the input combinations of a gate. Click a row to set the inputs.
5

OR: two NOTs, then NAND

An OR gate gives 1 when one input or both inputs are 1.

Put a NOT gate on each input. Then connect the two NOT outputs to a NAND gate.

Why does this work? A NAND gate gives 0 only when both of its inputs are 1. Here, both NAND inputs are 1 only when A is 0 and B is 0. Thus, the output is 0 only when A and B are both 0. That is OR.

Mathematicians call this rule De Morgan's law.

Figure 6. OR made from three NAND gates.
6

XOR: four NANDs

An XOR gate gives 1 when the two inputs are different.

XOR means "exclusive OR". It gives 0 when the two inputs are the same.

Gate 1 calculates A NAND B. Gate 2 compares this result with A. Gate 3 compares it with B. Gate 4 combines the two results.

XOR is necessary for addition. The next step shows why.

Figure 7. XOR made from four NAND gates. Watch the signal move through the gates, one gate after the other.
7

Count in binary

Binary uses place values, the same as decimal. But the columns are worth 1, 2, 4, 8 and 16, not 1, 10, 100 and 1000.

In decimal, each column can hold ten digits: 0 to 9. When a column goes past 9, it goes back to 0. Then you carry 1 to the next column. Thus, each column is worth ten times the column to its right: 1, 10, 100, 1000.

In binary, each column can hold only two digits: 0 and 1. When a column goes past 1, it goes back to 0. Then you carry 1 to the next column. Thus, each column is worth two times the column to its right: 1, 2, 4, 8, 16.

To read a binary number, add the values of the columns that have a 1. For example, 10110 is 16 + 4 + 2 = 22.

Figure 8. A 5-bit binary number. Each lamp is one bit. Click a bit to change it.
  1. Click the bits. Look at the sum of the column values below them.
  2. Push Add 1 again and again. Look at the right column. When it goes past 1, it goes back to 0 and carries 1 to the left.
  3. Look at the target. Click the bits to make the target number.
Note:In binary, 1 + 1 = 10. In decimal, 9 + 1 = 10. These are the same event. The column is full, so it goes back to 0 and carries 1 to the next column.
8

Add two bits

One column of a binary addition has only four possible cases. Five NAND gates can do all four.

Look at one column of a binary addition. It adds bit A and bit B. Each bit is 0 or 1, so there are only four cases. Click a case to send it into the circuit.

Figure 9. A half adder. It adds A and B and gives a sum digit and a carry digit.

Each case gives two digits. The right digit is the sum. You write it in this column. The left digit is the carry. It goes to the next column on the left.

Now compare the four cases with the gates from the earlier steps:

CaseSum digitCarry digit
0 + 000
0 + 110
1 + 010
1 + 101
Same table asXOR (step 6)AND (step 4)

The sum digits are 0, 1, 1, 0. That is the truth table of XOR. The carry digits are 0, 0, 0, 1. That is the truth table of AND. The gates do not know arithmetic. Their truth tables are the same as the addition table. That is sufficient.

In the circuit, gates 1 to 4 are the XOR from step 6. Gate 5 is a NOT. It takes the output of gate 1, which is A NAND B. NOT (A NAND B) is A AND B. That is the carry. This circuit is a half adder.

It is a "half" adder because it cannot accept a carry from the column to its right. The next step solves this problem.

9

The full adder: three bits in

A full adder is two half adders and one OR gate. It adds A, B and the carry from the column to its right.

A half adder has only two inputs. But each column after the first one must add three bits: A, B and the carry in. Thus, a full adder has three inputs.

The full adder does the work in three parts:

  • Half adder 1 adds A and B. It gives sum 1 and carry 1.
  • Half adder 2 adds sum 1 and the carry in. It gives the final sum and carry 2.
  • The OR gate combines carry 1 and carry 2 into one carry out.

Swipe sideways to see the full diagram.

Figure 10. A full adder. Each half adder box is the five-gate circuit from step 8. The numbers in a box show its addition. Click the inputs, or click a row of the table.
  1. Set A, B and Carry in to 1.
  2. Watch the signal go through half adder 1, then half adder 2, then the OR gate.
  3. Click the rows of the table. Look at the Carry 1 and Carry 2 columns.

Why is an OR gate sufficient? Why do the two carries not add together? Look at the Carry 1 and Carry 2 columns of the table. They are never both 1.

If carry 1 is 1, then A and B are both 1. Thus, sum 1 is 0. Half adder 2 then adds 0 and the carry in. That addition cannot make a carry. Thus, a maximum of one carry is 1, and "one or the other" is the correct answer.

Note:Each half adder is 5 NAND gates. An OR gate is 3 NAND gates. Thus, this full adder is 13 NAND gates. Engineers share some gates between the parts and make a full adder from only 9 NAND gates.
Note:The 1s column always has a carry in of 0. Thus, it could use a half adder. But engineers usually use full adders in all the columns. Then all the columns are the same, and you can connect adders together to add larger numbers.
10

Add larger numbers

Add one column at a time, from right to left. This is the same method as addition on paper.

When you add numbers on paper, you start at the right column. If a column is more than 9, you write the right digit and carry 1 to the top of the next column.

Binary addition is the same. But a binary column is full at 2, not at 10. And each column after the first one adds three bits: A, B and the carry from the column to its right.

Three bits can be at most 1 + 1 + 1 = 3. In binary, 3 is 11. Thus, the column writes 1 and carries 1. The carry is never more than 1.

Each box in the next figure is one full adder from step 9. Four full adders are in a line. The carry out of each column goes into the carry in of the column to its left.

Samples:

Figure 11. A 4-bit adder, drawn as an addition on paper. The top row shows the carries. Each box is one full adder. The text in a box is A + B + carry in = carry out and sum.
  1. Push Walk through by column.
  2. Push Next column. Read the explanation for each column.
  3. Look at the carry. It goes to the top of the next column, the same as on paper.
  4. Click the bits of A and B, or push a sample, to try other numbers.

In this adder, the carry goes through each full adder, one after the other. Engineers call this a ripple-carry adder.

Note:A 64-bit CPU adds two 64-bit numbers in less than one nanosecond. It uses faster methods to calculate the carry. But the base is the same: NAND gates.
11

Subtract with the same adder

A computer does not need a separate circuit to subtract. It changes B a little and uses the adder.

A 4-bit number has 16 values: 0 to 15. When a 4-bit result goes past 15, it goes back to 0. The carry out is lost. Thus, in 4 bits, 16 is the same as 0.

This gives a trick. To subtract 3, add 16 − 3 = 13. For example, 5 + 13 = 18. Four bits keep 18 − 16 = 2. And 5 − 3 = 2.

The adder makes 16 − B easily. Flip each bit of B, then add 1. For example, 3 is 0011. Flipped, it is 1100. That is 12. And 12 + 1 = 13.

An XOR gate can flip a bit on command. Look at its truth table: when one input is 1, the output is the opposite of the other input. When that input is 0, the other input goes through unchanged. Thus, one control wire, SUB, goes to an XOR gate on each bit of B. The same wire is the carry in of the adder. That is the "add 1".

Figure 12. An adder that also subtracts. SUB controls the XOR gates on B and the carry in.
  1. Click the bits of A and B, or click an example.
  2. Look at the row "B after XOR". When SUB is 1, each bit is flipped.
  3. Push SUB to change between add and subtract.
  4. Try 3 − 5. The result has a 1 in the left bit.
Note:When the left bit means "negative", engineers call the number format two's complement. In 4 bits, 1000 to 1111 are −8 to −1. Almost all computers use this format, because the same adder then works for positive and negative numbers.

The machine at the end of this page has a "Subtract 1" instruction. It is this circuit with B = 1.

12

Is it zero?

A computer makes decisions. The most important decision is: is this number zero?

A number is zero when all its bits are 0. Thus, the question is: is any bit 1?

An OR gate answers this question for two bits. More OR gates answer it for more bits. Connect them in a tree. Then a NOT gate inverts the answer. The output is ZERO.

Each OR gate is three NANDs (step 5). Each NOT gate is one NAND. Thus, this 4-bit test is ten NAND gates.

Figure 13. A zero test for a 4-bit number. Three OR gates in a tree, then a NOT gate.

The machine at the end of this page has the instruction "JZ: if the register is 0, go to another address". The zero test is the "if".

13

Choose: the multiplexer

One control bit selects which input goes to the output.

A multiplexer has two data inputs, A and B, and one select input, S. When S is 0, the output is A. When S is 1, the output is B. It is like an "if" statement in a program: if S then B, else A.

Gate 1 makes NOT S. Gate 2 lets A through only when NOT S is 1. Gate 3 lets B through only when S is 1. Gate 4 combines the two results. Only one of them can be active.

Figure 14. A multiplexer made from four NAND gates. The top half of the table has S = 0: OUT is the same as A. The bottom half has S = 1: OUT is the same as B.

For a number with many bits, put one multiplexer on each bit. The same S controls all of them. A CPU uses multiplexers everywhere: to select a register, to select the result of an operation, and to select the next address.

14

Decoder: pick one of four

An address goes in. Exactly one line comes out on.

The machine at the end of this page has four registers, R0 to R3. An instruction such as "INC R2" must find register 2. A decoder does this.

Two address bits can make four numbers: 00, 01, 10 and 11. The decoder has one output line for each number. Each line is an AND gate that looks for its own pattern. Line 2 looks for "2s bit is 1 and 1s bit is 0".

The two NOT gates make the opposite of each address bit. The four vertical wires carry A and NOT A for each bit. Each AND gate connects to one wire of each pair.

Figure 15. A 2-to-4 decoder: two NOT gates and four AND gates. Each AND gate is two NANDs.

Memory chips use very large decoders. A chip with 30 address bits has 230 lines, one for each location.

15

Memory: a loop that keeps one bit

Connect an output back to an input. Then the circuit can keep a value.

Until now, each output changes when its inputs change. The circuits cannot keep a value. They have no memory.

To make memory, use feedback. Connect the output of a gate back to the input of a different gate.

In the figure, the output of gate 1 goes into gate 2. The output of gate 2 goes back into gate 1. The two gates hold each other in position. This circuit is an SR latch. It keeps one bit.

Figure 16. An SR latch made from two NAND gates. The crossed wires are the feedback loop.
  1. Push SET. Q becomes 1.
  2. Release SET. Q stays 1. The latch keeps the value.
  3. Push RESET. Q becomes 0 and stays 0.
Note:SET and RESET are usually 1. When you push one, it becomes 0 for a short time. Engineers call this an active-low input.
Caution:Do not push SET and RESET at the same time. Then Q and not Q both become 1. When you release the two inputs, the result is not known.

The registers and the fast cache memory in a CPU keep their bits with loops like this one.

16

The D latch: save on command

One data input, D, and one control input, SAVE.

The SR latch has two problems. You must push SET or RESET to change it. And when you push both, the result is not known.

The D latch fixes both problems. It adds two gates in front of the SR latch. When SAVE is 1, gate 1 or gate 2 makes a SET or a RESET from D. They can never make both, because one gets D and the other gets NOT D. When SAVE is 0, both gates give 1. Then the latch keeps its value.

Figure 17. A D latch made from five NAND gates. Gates 3 and 4 are the SR latch from the last step.
  1. Set SAVE to 1. Change D. Q follows D.
  2. Set SAVE to 0. Change D. Q does not change.
17

Flip-flop: two latches, one moment

Two D latches in a row save a value at one moment only.

A D latch copies D for all of the time that SAVE is 1. In a CPU that is a problem. While SAVE is 1, a changed output can go through the gates and come back to D. Then the value changes again, and again.

The solution is two latches in a row, with opposite SAVE signals. The clock goes to latch 2 directly and to latch 1 through a NOT gate. Thus, one latch is always closed.

  • While the clock is 0, latch 1 copies D. Latch 2 is closed.
  • When the clock goes to 1, latch 1 closes with the value it has. Latch 2 opens and copies that value to Q.

Thus, Q changes only when the clock goes from 0 to 1. Engineers call this an edge-triggered flip-flop.

Figure 18. A flip-flop made from two D latches. The thick outline shows the latch that is open.
  1. Set D to 1. Latch 1 copies it. Q does not change.
  2. Push the clock to 1. Q becomes 1.
  3. Change D while the clock is 1. Q does not change.
18

The clock: all changes at the same time

A clock signal tells all the memory parts when to change.

A CPU has millions of memory bits. The signals through the gates do not all arrive at the same time. While a signal moves through the gates, the outputs can change many times.

Thus, the memory must wait. It must save a value only when all the gates are stable.

The clock is a square wave. It goes from 0 to 1 and back to 0. A modern CPU clock does this billions of times each second.

The flip-flop from step 17 looks at its input D at only one moment: when the clock goes from 0 to 1. This moment is the rising edge. Connect the same clock to all the flip-flops. Then all the memory changes at the same time, after the gates are stable.

D = 0 Q = 0

Figure 19. A timing diagram. Time moves from left to right. Each dashed line is a rising edge. A bright dashed line shows an edge where Q changed.
  1. Push Change D at any time.
  2. Look at the Q line. Q does not change immediately.
  3. At the next rising edge, Q becomes the same as D.
Note:The clock speed has a limit. Before each rising edge, the slowest path through the gates must become stable. If the clock is too fast, a flip-flop saves a wrong value. Then the computer can crash.

Where the clock comes from

The memory loop in step 15 has two gates. A signal goes around the loop and comes back the same. Thus, the loop is stable.

Make a loop with three NOT gates. Now the signal comes back inverted. The first gate must change. That change goes around the loop and comes back inverted again. The loop never becomes stable. It makes a square wave: a clock.

Figure 20. A ring oscillator. An odd number of NOT gates in a loop is never stable. An even number keeps a value.
Note:A computer's clock starts with a quartz crystal that vibrates at a steady rate. A circuit in the CPU multiplies that rate to billions of ticks each second. Its fast part is often a ring oscillator like this one.
19

The program counter

A register, an adder and a multiplexer. Together they say which instruction is next.

A program is a list of instructions in memory. Each instruction has an address. The program counter (PC) keeps the address of the instruction to do.

The PC is a register: four flip-flops, one for each bit. At each rising edge of the clock, the flip-flops save a new address. Two parts calculate that address:

  • An add-1 unit calculates PC + 1. This is the adder from step 10 with B = 1.
  • A multiplexer selects PC + 1 or a jump address. The JUMP signal is its select input.

Usually JUMP is 0 and the PC counts up: 0, 1, 2, 3. When JUMP is 1, the PC goes to a different address. That is how a program makes a loop or skips a part.

Figure 21. A 4-bit program counter. The bright input of the multiplexer is the one it selects.
  1. Push Clock tick a few times. The PC counts up.
  2. Set a jump address. Set JUMP to 1. Push Clock tick. The PC goes to the jump address.
  3. Set JUMP to 0 again. The PC counts up from the new address.

In the machine, the instruction decides the JUMP signal. For JMP, JUMP is always 1. For JZ, JUMP comes from the zero test.

20

The machine

Adders calculate. Flip-flops keep numbers. The clock moves the machine one step at a time.

This machine has four registers, R0 to R3. Each register is eight flip-flops. Thus, each register keeps a number from 0 to 255. The program is in memory. Each line of the program has an address.

The machine knows only five instructions:

InstructionWhat it does
INC rAdd 1 to register r.
DEC rSubtract 1 from register r. If r is 0, do nothing.
JZ r, nIf register r is 0, go to address n.
JMP nGo to address n.
HALTStop.

The program counter (PC) keeps the address of the instruction to do. The machine does these four steps again and again:

  • Fetch: read the instruction at the address in the PC.
  • Decode: find which operation the instruction must do.
  • Execute: do the operation.
  • Next: put the next address into the PC. Usually, this is PC + 1.

A jump is not a special operation. It only puts a different number into the PC.

You know every part of this machine. Each register is eight flip-flops. Add 1 and subtract 1 are the adder and its subtract trick. The zero test is step 12. A decoder selects the register. The program counter and its multiplexer do the jumps.

Program memory

    Control

    PC
    0
    IR
    none
    1. Fetch
    2. Decode
    3. Execute
    4. Next
    • Add 1
    • Subtract 1
    • Zero test
    • Jump
    • Halt

    Clock steps: 0. Instructions done: 0.

    Registers

    Figure 22. A small computer. Each lamp in a register is one flip-flop. You can change the start values with + and − before the first step.
    1. Select a program.
    2. Use the + and − buttons to set the start values.
    3. Push Step to do one step. Or push Run.
    4. To change the start values again, push Reset.
    21

    Turing complete

    With these five instructions, the machine can calculate anything that any computer can calculate.

    A system is Turing complete when it can do all the calculations that a computer can do. It must have sufficient time and memory. It needs three things:

    • Memory that it can read and change.
    • A conditional jump: an operation that goes to one place or a different place, related to a value.
    • A method to do the same steps again (a loop).

    This machine has all three. The registers are the memory. JZ is the conditional jump. JMP makes a loop.

    In 1961, the mathematician Marvin Minsky proved a related fact. A machine with only add 1, subtract 1, a zero test and two registers is Turing complete. The registers must have no upper limit.

    This demo stops at 255. A true Turing complete machine needs memory with no limit. Real computers also have limits, but their limits are very large.

    Minecraft redstone, Magic: The Gathering and PowerPoint animations are Turing complete for the same reason. They can branch, and they can remember.

    1. Transistor
    2. NAND
    3. NOT, AND, OR, XOR
    4. Adder and zero test
    5. Multiplexer and decoder
    6. Latch
    7. Flip-flop and clock
    8. Program counter
    9. Machine

    Nowhere in this chain is there a part that understands. There are only switches, in a very good arrangement.

    // more circuits

    More circuits from the same parts

    The machine is complete. These circuits use the same parts in different arrangements. You find them in almost all electronic devices.

    22

    The shift register

    Flip-flops in a row. At each tick, every bit moves one place.

    Connect the output Q of each flip-flop to the input D of the next flip-flop. All of them get the same clock. At each rising edge, each flip-flop saves the value of its left neighbor. Thus, the pattern moves one place to the right.

    The flip-flops save at the same moment, so each one saves the old value of its neighbor. The edge-triggered flip-flop from step 17 makes this possible. With D latches, one bit would go through all of them at once.

    Shift registers have many uses:

    • A USB cable or a network cable sends one bit after the other. A shift register collects the bits into bytes again.
    • A shift to the right divides a number by 2. A shift to the left multiplies it by 2.
    • LED chasers and scanners move a light along a row of LEDs.

    Figure 23. An 8-bit shift register. Each lamp is one flip-flop. Feed IN sends new bits in from the left. Loop connects the end to the start. Scanner adds a direction.
    1. Set IN. Push Clock tick. The bit goes into FF1, and all the other bits move one place.
    2. Push Loop. Click some flip-flops to make a pattern. Push Run.
    3. Push Scanner.
    23

    Random numbers from a shift register

    Add one XOR gate. The register then makes numbers that look random.

    A computer cannot easily make true random numbers. All its circuits do the same thing each time. But it can make numbers that look random.

    Take a 4-bit shift register. Do not feed IN from outside. Instead, connect FF3 and FF4 to an XOR gate, and connect the XOR output to IN. Engineers call this a linear-feedback shift register (LFSR).

    This LFSR goes through all 15 values from 1 to 15 in a mixed order. Then it starts the same list again. It never shows 0. If all bits are 0, the XOR gives 0, and the register stays at 0 for all time.

    Figure 24. A 4-bit LFSR. The squares show the values that the register has visited. The die uses the remainder after division by 6.
    Note:This die is not fair. There are 15 values, and 15 does not divide by 6. Thus, some faces come up three times in the list, and others only two times. Real programs use many more bits, so the difference becomes very small.

    LFSRs are everywhere. The noise sound of the NES game console comes from a 15-bit LFSR. Wi-Fi and GPS use them to mix their signals.

    24

    The seven-segment display

    Four bits go in. Seven lamps come on in the shape of a digit.

    The display on a clock or a microwave has seven segments: a to g. Each segment is one lamp. A circuit must decide, for each 4-bit input, which segments are on.

    Each segment has its own truth table with 16 rows. A simple method makes any truth table. First, use a decoder (step 14) with 16 lines: one line for each input value. Then, for each output, connect an OR gate to the lines where the output must be 1.

    Engineers then make the circuit smaller, but this method shows an important fact: NAND gates can make any truth table.

    Figure 25. A seven-segment display for the values 0 to 15 (0 to F in hexadecimal). Click a segment. The bright squares show the values that switch it on.
    25

    A traffic light: a state machine

    A register keeps the state. Logic calculates the next state.

    A traffic light at a crossroad has four states. A 2-bit register keeps the state: 00, 01, 10 or 11. A counter is the timer.

    At each tick, logic gates calculate the next state from three things: the current state, the timer, and a sensor in the road. If no car waits on the East–West road, the North–South light stays green.

    This is a state machine. The program counter and the machine in this guide are state machines too. They have more states and more inputs.

    Figure 26. A traffic light controller. The bright state in the diagram is the value in the 2-bit register.
    1. Wait until the timer is done. The North–South light stays green.
    2. Push "Car arrives on E–W". The lights change.

    The text in this guide follows the rules of ASD-STE100 Simplified Technical English: short sentences, active voice, one instruction in each step, and one meaning for each word.