How computers work

Logic gate: from transistors to logic

A gate is a few transistors connected so that the output voltage depends on the input voltages according to a logical rule: AND, OR, NOT, exclusive OR. Everything else is built from gates: adders, registers, caches, the whole processor. Here we look at how a CMOS gate is built inside, why some gates are simpler than others, and where delay and power consumption come from.

Open in 3D →

What a logic gate is

In a digital circuit, every wire at every moment carries one of two values: 0 or 1. Physically these are voltages: close to zero volts means zero, close to the supply voltage means one. Anything in between is considered a forbidden zone that the signal must pass through quickly.

A gate is a circuit with one or more inputs and one output that implements a logic function. The function is conveniently written as a truth table: for two inputs it has 4 rows, all the combinations 00, 01, 10, 11. For example, AND gives 1 only in row 11, OR in every row except 00. Two inputs are enough to define 16 different functions; in practice a few are enough, and all the others are built from them.

An important property of a CMOS gate is that it restores the signal. A slightly “damaged” voltage may arrive at the inputs, say 0.15 V instead of zero because of interference, yet the output will still be almost exactly 0 or almost exactly the supply voltage, because the output is connected directly to one of the rails. The margin within which input distortion does not change the result is called the noise margin. Thanks to it, a signal passes through thousands of gates in a row without “smearing.”

This level uses a switch-level model: each transistor is either on or off, and voltages and delays are not computed. A node equals 1 if it is connected to VDD through on transistors, and 0 if it is connected to GND.

Supply VDD and ground GND

Along the top of the schematic runs the supply rail VDD (in a processor core, 0.7–1.3 V depending on frequency and load), and along the bottom the ground GND, 0 V. A logical one is an output connected to the supply; a zero is an output connected to ground. A gate does not create energy and does not “compute” in the usual sense: it only chooses which of the two rails to connect its output to.

On the schematic, current is visible as a highlighted path: from VDD through on pMOS transistors to the output (output = 1), or from the output through on nMOS transistors to GND (output = 0). In a correctly built CMOS gate there is never a simultaneous path from VDD to GND; otherwise a short-circuit current would flow through it.

In a real chip, power and ground are a grid of wires on the upper metal layers, from which thousands of vias descend to each cell. A processor draws up to 200 A or more through it, so the power grid takes up a significant share of the wiring resources. If many gates in one place switch at the same time, the voltage there briefly droops; designers check that the droop does not exceed a few percent.

The base in the model is the schematic of a single gate: the VDD rail on top, GND at the bottom, the transistors between them, and the input and output wires.

pMOS pulls up, nMOS pulls down

Every static CMOS gate consists of two halves. The upper network of pMOS transistors (pull-up) connects the output to the supply, and the lower network of nMOS transistors (pull-down) connects it to ground. The networks complement each other (hence “complementary” in the name CMOS): for any combination of inputs, exactly one of them is on.

The networks are dual: if transistors are in series in the lower network, the corresponding ones in the upper network are in parallel, and vice versa. Series nMOS transistors conduct only when all of them are on, which is “AND”; parallel ones conduct when at least one is on, which is “OR.” Since the lower network pulls the output to zero, the function comes out inverted: series nMOS give NAND, parallel ones give NOR.

Why pMOS on top and nMOS at the bottom, and not the other way around? An nMOS passes a zero well but a one poorly: connected to the supply, it would raise the output only to VDD − Vth and then turn off. A pMOS, on the contrary, passes a one well. So each type is placed where it delivers a full level. For the same reason a CMOS gate “naturally” inverts: a one at the input turns on the lower nMOS and gives a zero.

Wires on the schematic are colored by value: light blue is 1, dark is 0. Click a transistor: a tooltip will show which input drives its gate and whether it is on right now. Double-clicking a transistor opens its cross-section on the Transistor level.

The inverter, the simplest gate

An inverter (NOT) is two transistors: a pMOS on top and an nMOS at the bottom. Their gates are connected and form the input; their drains are connected and form the output. An input of 1 turns the nMOS on and the pMOS off, so the output is connected to ground and becomes 0. An input of 0 does the opposite, giving 1 at the output. The truth table has two rows: 0 → 1, 1 → 0.

If you smoothly raise the input voltage from 0 to VDD, the output at first barely changes, then drops sharply in a narrow region around the middle, and after that stays near zero. The steepness of this transition is gain: a small change in the input produces a large change in the output. This is exactly why an inverter restores levels: “not quite zero” at the input becomes a clean one at the output.

The switching threshold, the point where the output equals the input, is placed around VDD/2 so that the noise margin is the same for zero and for one. This is done by choosing the size ratio of the pMOS and nMOS.

Inverters are the most common cells on a chip. Chains of inverters of increasing size serve as buffers: a small gate cannot quickly recharge a long wire or a hundred inputs, and a buffer amplifies the signal in stages. Two inverters closed in a loop store a bit; more on that below.

NAND and NOR, the universal cells

NAND (NOT-AND) is four transistors: two pMOS in parallel on top and two nMOS in series at the bottom. The output becomes 0 only when both nMOS are on, that is, when both inputs are 1. If at least one input is 0, at least one pMOS is on and the output is pulled up to one. Table: 00 → 1, 01 → 1, 10 → 1, 11 → 0.

NOR (NOT-OR) is built the other way around: pMOS in series, nMOS in parallel. The output is 1 only when both inputs are 0. Table: 00 → 1, 01 → 0, 10 → 0, 11 → 0.

NAND and NOR are called universal: any logic function can be built from NAND alone. NOT is a NAND with its inputs tied together (NAND(A, A) = NOT A), AND is NAND followed by NOT, and OR, by De Morgan’s law, is NAND of the inverted inputs: A OR B = NOT(NOT A AND NOT B). The same holds for NOR.

In practice NAND is preferred over NOR. In a NOR the pMOS transistors are in series, and holes are less mobile than electrons: two series pMOS pull the output up slowly, and they have to be made wide. In a NAND the fast nMOS transistors are in series. That is why NANDs with three or four inputs are common in libraries, while four-input NORs are avoided.

In the model you can click any cell and check: with inputs 1 and 1, the NAND lights up the path from the output through both nMOS to ground; with any other combination, the path from VDD through one or both pMOS.

AND and OR

Non-inverting gates cannot be built in a single CMOS stage: the lower network always pulls toward zero when the inputs are ones. So AND is built as a NAND with an inverter at the output, 6 transistors, and OR as a NOR with an inverter, also 6. The “simple” AND is actually more complex, slower, and more power-hungry than NAND.

Logic synthesis tools take this into account: logic is rewritten in terms of inverting gates whenever possible. For example, the function (A AND B) OR (C AND D) is better built as NAND(NAND(A, B), NAND(C, D)): two levels of NAND instead of AND and OR, and not a single extra inverter.

Compound gates are even more compact: AOI (AND-OR-INVERT) and OAI. For example, AOI21 computes NOT((A AND B) OR C) in a single stage of six transistors: in the lower network, a pair of series nMOS is in parallel with a third. Such cells are widely used in adders and multiplexers.

In the full adder one level up, AND gates detect where a carry arises (both bits are 1, or the half sum is 1 and the incoming carry is 1), and OR combines these two sources of carry.

XOR from four NANDs

Exclusive OR (XOR) outputs 1 when the inputs differ: 00 → 0, 01 → 1, 10 → 1, 11 → 0. In the model it is built from four NAND cells, 16 transistors: n1 = NAND(A, B), n2 = NAND(A, n1), n3 = NAND(B, n1), output = NAND(n2, n3). Click any cell on the upper schematic, and its transistors will open below.

Let’s trace the row A = 1, B = 0. n1 = NAND(1, 0) = 1. n2 = NAND(1, 1) = 0. n3 = NAND(0, 1) = 1. Output = NAND(0, 1) = 1: the inputs differ, and the result is 1. For A = B = 1: n1 = 0, so n2 = NAND(1, 0) = 1 and n3 = 1, and the output = NAND(1, 1) = 0.

XOR is the heart of addition: the sum of two bits without the carry is exactly A XOR B. XOR also compares: A XOR B = 0 if and only if the bits are equal, so an equality comparator for two 64-bit numbers is 64 XORs and an OR tree. XOR computes parity (error checking in memory) and underlies ciphers and hash functions.

In real processors XOR is made from 8–12 transistors using special circuits, for example with transmission gates, where a pair of nMOS and pMOS works as a bidirectional switch for the signal. The logic stays the same.

The Z and X states: when the rule is broken

Besides 0 and 1, the model knows two more states. Z (high impedance, “floating”): the output is connected neither to the supply nor to ground, because both networks are off. The voltage on such a wire is undefined and slowly drifts due to leakage and interference. In a correctly built static gate Z never occurs, but it is used deliberately in tri-state buses: several drivers are connected to one wire, and all but one are disconnected.

X (conflict): the output is connected to both the supply and ground at the same time. A short-circuit current flows through the on transistors, the voltage sits somewhere in the middle, and the next gate may interpret it either way. In a real circuit this is a design error: it wastes energy, heats the chip, and gives an unpredictable result.

A brief through-current does occur in any CMOS gate, though, at the moment of switching, when the input passes the middle of the range and both transistors are partly on. This is part of the switching energy, which is why designers try to make signal edges steep.

Delay and switching energy

A gate’s output is not just transistors but also capacitance: the wire to the next gates and the gates of their transistors. For the output to change from 0 to 1, the on pMOS network must charge this capacitance from the supply; to change back, the nMOS network discharges it to ground. The recharge time is the gate delay.

The delay depends on the strength of the transistors (their width), on the load, and on the supply voltage. The more inputs an output drives (the fan-out), the longer the switching takes. A convenient measure of a process’s speed is the delay of an inverter driving four copies of itself (FO4); in modern processes it is about 5–10 ps.

This is what limits the clock frequency. A clock cycle of a 5 GHz processor lasts 200 ps. In that time a signal must travel from one register through all the logic of a stage to the next register. Accounting for the delay of the registers themselves and the wires, only a chain of 10–30 gates fits into one cycle, which is why complex operations are cut into pipeline stages.

Energy: each time a capacitance C is recharged from a supply V, a charge C·V is drawn from the rail, and half the energy is dissipated in the transistors during charging and half during discharging. One switch costs on the order of C·V², femtojoules for a typical gate. But there are billions of gates, and they switch billions of times per second, so in total it comes to tens or hundreds of watts.

Frequency and voltage are linked: for gates to switch faster, more current is needed, which means a higher voltage. That is why overclocking raises the voltage, and power grows almost as the cube of frequency.

From gates to memory: the latch and the flip-flop

Gates by themselves remember nothing: the output depends only on the current inputs. To store a bit, the output has to be fed back to the input. Two inverters closed in a loop have two stable states: 1 on one and 0 on the other, or vice versa. Each one sustains the other, and the bit is held as long as there is power.

A static memory (SRAM) cell is exactly such a loop of two inverters (4 transistors) plus two nMOS access switches through which the bit is read and written: 6 transistors per bit. Register files and all the processor’s caches are made of such cells: tens of megabytes, hundreds of millions of cells.

A latch is a loop with an input through which a new value can be written into it while an enable signal is active. A flip-flop is two latches in a row that open in turn, so a new value is accepted only at the edge of the clock signal. All synchronous logic rests on flip-flops: registers, the program counter, pipeline registers.

Hence the main pattern of any processor: flip-flops → combinational logic made of gates → flip-flops. On every clock cycle the flip-flops capture new values, the logic between them has time to process them, and on the next edge the result is captured again.

Standard cells: how gates get into a chip

Nobody draws billions of transistors by hand. The process manufacturer and the designers prepare a standard cell library: hundreds of pre-drawn and pre-characterized gates (inverters, NAND and NOR with 2–4 inputs, AOI/OAI, XOR, multiplexers, flip-flops), each in several sizes (drive strengths). All cells have the same height, and the VDD and GND rails run along their top and bottom edges, so cells are placed in rows right next to each other, like bricks.

A processor is described in a hardware description language (Verilog, VHDL) as a set of registers and logic expressions. A synthesis program turns the description into a network of library cells, a place-and-route program positions them and lays out the wires, and analysis programs check the delay of every path and the power consumption. Only the most critical places are optimized by hand, for example SRAM arrays and adders.

The core of a modern processor contains millions of standard cells. Process density is often expressed as cell height in metal tracks: in 3 nm processes it is about 5–6 tracks of the lowest layer, that is, a cell roughly 150–170 nm tall.

Common misconceptions

“A one is 5 volts.” That was true in the TTL logic of the 1970s–1980s. In the core of a modern processor a one is 0.7–1.3 V, in DDR5 memory about 1.1 V, and on external interfaces you find 1.8 and 3.3 V.

“A gate constantly draws current.” A CMOS gate spends energy mostly when switching; at rest, only leakage. That is why a processor’s power consumption depends heavily on its load.

“AND is simpler than NAND.” In CMOS it is the other way around: NAND is a single stage of 4 transistors, AND is 6 transistors in two stages.

“Logic functions are performed by a program.” A gate is pure physics: the output settles by itself as soon as the inputs change, without any program. A program appears much higher up, once the gates have been assembled into a device that reads instructions from memory.

Open in 3D →

All articles

Overviews

Logic: from transistor to arithmetic

Processor

Computer

Servers and data centers

Networks and the internet

Software