Ground Upby Rupesh Koirala

Part one: Electrons · Day 8 of 60

Logic from switches

Wire transistors together in pairs and they start to compute. Today you build NOT, NAND and NOR from individual transistors, see why one kind of gate is enough to build anything, and learn the formula for what switching costs in energy.

By the end of today you can

  • Draw a CMOS inverter and a NAND gate and say which transistors conduct for each input
  • Explain why NAND gates alone can build any logic function
  • Use the switching-power formula to say what lowering the voltage buys

A master's thesis

In 1937 a 21-year-old student at MIT, Claude Shannon, was looking after a mechanical computer whose control circuits were a tangle of relays, switches thrown by electromagnets. He had taken a philosophy course in which he met the algebra of George Boole, a way of calculating with true and false published in 1854.

Shannon saw that they were the same thing. Two switches in series behave like "and". Two in parallel behave like "or". Any statement in Boole's algebra could be built from switches, and any circuit of switches could be simplified with algebra. His thesis has been called the most important master's thesis of the century. Every digital circuit since rests on it.

Yesterday you got the switch. Today it starts to think.

Two levels, and why that wins

Digital circuits agree to care about only two voltages. Near the supply voltage is called high, or 1. Near ground is low, or 0. One such value is a bit.

The real voltage on a wire is never exactly either. It has noise on it, it sags, it overshoots. The trick is that every logic circuit treats anything above a middle level as 1 and anything below as 0, and then sends out a clean, full-strength 1 or 0 of its own. The allowance for error is called the noise margin. Because each stage wipes out the errors of the one before, a signal can pass through billions of stages and arrive perfect. An analog signal picks up a little damage at every stage and never loses it. That is the whole reason digital won.

NOT: the inverter

A logic gate is a small circuit whose output depends on its inputs by a fixed rule. The simplest is NOT: the output is the opposite of the input. It is also called an inverter.

A CMOS inverter with its input at 0

VDDGNDA = 0A = 0OUT = 1
The top transistor is PMOS, which conducts when its gate is low. The bottom is NMOS, which conducts when its gate is high. The input goes to both.

It takes two transistors, one of each kind from yesterday. The PMOS sits between the supply and the output. The NMOS sits between the output and ground. Both gates are tied to the input.

  • Input 0: the PMOS turns on and the NMOS turns off. The output is connected to the supply. It reads 1.
  • Input 1: the NMOS turns on and the PMOS turns off. The output is connected to ground. It reads 0.

This pairing is CMOS, complementary MOS, and one property made it the basis of almost every chip. In either state, one of the two transistors is off. So there is never a path from the supply straight to ground, and when nothing is changing the circuit draws almost no current. Earlier designs that used only one kind of transistor burned power all the time. CMOS took over in the 1980s for that reason.

The transistors that connect the output to the supply form the pull-up network. Those that connect it to ground form the pull-down network. In CMOS exactly one of the two conducts.

NAND and NOR

Give the same idea two inputs.

A CMOS NAND gate with both inputs at 1

VDDGNDA = 1B = 1A = 1B = 1OUT = 0
Two NMOS transistors in series pull the output down only when both are on. Two PMOS in parallel pull it up if either is on.

In a NAND gate the two NMOS transistors are in series, so the output is pulled to ground only when A and B are both 1. The two PMOS are in parallel, so if either input is 0 the output is pulled up. The output is 0 only when both inputs are 1. That is "not and".

A NOR gate swaps the arrangement: NMOS in parallel, PMOS in series. Its output is 1 only when both inputs are 0.

Each takes four transistors. A plain AND is not simpler. A single CMOS stage always inverts, so AND has to be built as a NAND followed by an inverter, six transistors in all. Chip designers think in NANDs and NORs for this reason.

A truth table lists the output for every combination of inputs.

A B AND OR NAND NOR XOR
0 0 0 0 1 1 0
0 1 0 1 1 0 1
1 0 0 1 1 0 1
1 1 1 1 0 0 0

The last column is the XOR gate, exclusive or: 1 when the inputs differ. It is the heart of addition, as tomorrow shows.

One gate is enough

Here is a fact that surprises most people. Every logic function there is can be built from NAND gates alone. NAND is a universal gate. So is NOR.

The proof is by construction. Tie a NAND's two inputs together and it is an inverter. Follow a NAND with that inverter and you have AND. Invert both inputs first and NAND becomes OR. From AND, OR and NOT you can build anything.

XOR built from four NAND gates

1234A = 1B = 0OUT = 1
Red wires carry 1 and blue wires carry 0. Follow it through: gate 1 gives 1, gate 2 gives 0, gate 3 gives 1, and gate 4 turns that pair into 1.

This is not a classroom curiosity. The computer that guided the Apollo spacecraft to the moon was built from about 5,600 gates, every one of them an identical three-input NOR. Using one kind of part made it easier to manufacture and test.

The rules for rearranging logic are Boolean algebra. The two most used are De Morgan's laws.

What switching costs

Logic is not free. Two costs set the limits of every chip.

Time. The output of a gate is connected to a wire and to the gates of the next transistors. Together they are a small capacitor, as on day 4. To change the output, the transistor must charge or discharge that capacitor through its own resistance, and that takes about R × C. For a modern gate the propagation delay is around ten picoseconds. A signal typically has to pass through twenty or so gates between one tick of the clock and the next. Twenty times ten picoseconds is 200, and one tick every 200 picoseconds is 5 gigahertz. That is where clock speeds come from.

Energy. Each time an output goes from 0 to 1, the supply delivers an energy of C × V². Half is stored on the wire and half becomes heat in the transistor. When the output returns to 0, the stored half becomes heat too.

The formula has only four levers. Make the capacitance smaller, which is what shrinking transistors does. Lower the voltage. Lower the frequency. Or arrange for fewer gates to switch.

From gates to chips

Nobody designing a chip draws transistors. They work with a library of ready-made gates called standard cells: inverters, NANDs, NORs and a few hundred others, each already laid out, tested and measured. An engineer describes what the logic should do, and software chooses cells from the library, places billions of them and wires them up. Days 12 and 13 follow that process.

One practical trap. Signals take time, and different paths through a circuit take different times. For a moment after an input changes, an output can show the wrong value before it settles. That brief error is a glitch. Tomorrow's clock exists largely to hide glitches.

Playground · 10 minutes

The gate bench

Flip the inputs and watch which transistors conduct. Then see AND, OR and XOR built from nothing but NAND gates.

Try these

Scenario · 2 decisions

What would you do?

This case is invented. A colleague is building a simple alarm panel. The siren should sound when the door is open and the system is armed, or whenever the panic button is pressed. The only logic chip in the drawer is a 74HC00, which holds four two-input NAND gates.

Quiz · 5 questions

Check what stayed

Bench work · 30 minutes

Take it to the bench

Things to measure, take apart, look up or work out by hand. No equipment is needed unless a prompt says so. Your notes are saved in this browser and collected on the Lab notes page.

Words to keep

Bit
One binary value, 0 or 1.
Logic gate
A small circuit whose output is a fixed function of its inputs.
Truth table
A list of a gate's output for every combination of inputs.
NOT gate
A gate whose output is the opposite of its input. Also called an inverter.
NAND gate
A gate whose output is 0 only when all its inputs are 1.
NOR gate
A gate whose output is 1 only when all its inputs are 0.
XOR gate
A gate whose output is 1 when its two inputs differ.
CMOS
Logic built from paired NMOS and PMOS transistors, so that almost no current flows except while switching.
Pull-up and pull-down networks
The transistors that connect a gate's output to the supply and to ground.
Noise margin
How far a signal can stray from its ideal level and still be read correctly.
Universal gate
A gate from which every logic function can be built. NAND and NOR are both universal.
Boolean algebra
The rules for calculating with true and false.
De Morgan's laws
The rules that turn a NOT of an AND into an OR of NOTs, and the reverse.
Propagation delay
The time a gate takes to change its output after an input changes.
Dynamic power
The power a chip uses in charging and discharging its wires as gates switch.
Standard cell
A ready-made, tested gate layout that design software places and connects.
Glitch
A brief wrong value on an output while signals are still settling.

Watch, read and build

  • Read: Code by Charles Petzold. Builds a computer from a flashlight and a relay, one idea at a time.
  • Play: NandGame, a free browser game in which you build a working computer starting with one NAND gate.
  • Watch: Ben Eater, "Making logic gates from transistors". The circuits in this lesson, wired up on a breadboard.
  • Read: Claude Shannon, "A Symbolic Analysis of Relay and Switching Circuits" (1938). The thesis itself is short and readable.