Scroll to turn the page
Computer Architecture · Computer Architecture
circuits whose output depends only on their inputs now
Chapter 1 added numbers on paper. A processor adds them with circuits built from a handful of simple parts called logic gates.
The figure is a full adder: five gates that add three bits, the two bits of one column and the carry coming in, and produce a sum bit and a carry out. It steps through all eight combinations of inputs. Red wires carry a 1, grey ones a 0.
| A | B | Cin | Cout | S |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 1 |
| 0 | 1 | 0 | 0 | 1 |
| 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 0 | 1 |
| 1 | 0 | 1 | 1 | 0 |
| 1 | 1 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 |
Its output depends only on its inputs at this moment: it has no memory. Circuits like that are combinational. This chapter builds them up from transistors: gates, Boolean algebra, the multiplexer, adders and finally the ALU, the arithmetic heart of a processor. Chapter 3 adds memory.
CMOS: one switch up, one switch down
A transistor in a digital chip is used as a switch that one wire turns on and off. A modern processor has billions of them.
Chips use two kinds, together called CMOS (complementary metal-oxide-semiconductor):
The simplest gate, the inverter (NOT), is one of each. The pMOS connects the output to the supply, the nMOS to ground, and the input drives both. When A is 0 the pMOS is on and pulls Y up to 1. When A is 1 the nMOS is on and pulls Y down to 0. Exactly one is on at a time, so the output is always driven and almost no current flows between supply and ground, which is why CMOS uses so little power when idle.
The same idea builds every gate. A NAND puts two pMOS in parallel above the output and two nMOS in series below it: 4 transistors. An AND is a NAND followed by an inverter, 6 transistors, which is why designers think in NANDs and NORs.
six functions and their truth tables
A gate takes bits in and gives one bit out. Its truth table lists the output for every combination of inputs: with n inputs, 2n rows.
| A | B | AND | OR | XOR | NAND | NOR |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 | 0 | 0 |
In Boolean notation AND is written as a product (AB), OR as a sum (A + B), XOR as A ⊕ B, and NOT with a bar (A) or a prime.
NAND alone can build every other gate: NOT A is A NAND A, AND is NAND then NOT, and OR is NAND of the inverted inputs (next page). So can NOR. Gates with this property are called universal.
rules for rewriting circuits
Boolean algebra lets you rewrite a circuit into a smaller or faster one that computes the same function.
Most rules look like ordinary algebra: A·1 = A, A + 0 = A, and A(B + C) = AB + AC. Some do not: A + A = A, A + 1 = 1, A + A = 1, AA = 0, and A + AB = A.
The most useful pair is De Morgan’s laws:
AB = A + B and A + B = A·B
In words: NAND is OR with inverted inputs, and NOR is AND with inverted inputs. The figure checks the first law the only way a truth table can, row by row:
| A | B | NAND | NOT A or NOT B |
|---|---|---|---|
| 0 | 0 | 1 | 1 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 0 |
“Push the bubble” is how designers use it: an inverter can slide through a gate if the gate changes from AND to OR (or back) and every other wire through it gets inverted.
sum of products
Any truth table can be turned into a circuit mechanically: one AND for each row where the output is 1, and one OR to combine them.
The figure’s function is majority: Y is 1 when at least two of A, B, C are 1. It is 1 on 4 rows:
| A | B | C | Y |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 0 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 |
Each of those rows is a product (a minterm): ABC, ABC, ABC, ABC. Their OR is the sum of products, correct but large: four 3-input ANDs and an OR.
Simplify. ABC can join each of the others: ABC + ABC = BC(A + A) = BC, and likewise for AC and AB (the rule X + X = X lets ABC be used three times). The result is the figure’s circuit:
Y = AB + AC + BC
A Karnaugh map does the same grouping by eye for up to four or so inputs; design tools do it by algorithm for thousands. The full adder’s carry out is this same majority function.
choosing and selecting
A processor is full of choices: which register to read, which value to write back, which address comes next. The part that makes a choice is the multiplexer.
A 2-to-1 multiplexer (a mux) has two data inputs, D0 and D1, and a select input S. When S is 0 the output is D0; when S is 1 it is D1:
Y = S·D0 + S·D1
Each AND passes its data input only when the select allows it, and the OR takes whichever got through. A mux with 2k inputs needs k select bits. Wider data just repeats the circuit: a 32-bit 2-to-1 mux is 32 of them sharing one S.
A decoder does the reverse job: n input bits turn on exactly one of 2n outputs. A 2-to-4 decoder given 10 raises output 2. Decoders pick a register in the register file or a row in a memory.
| A1 | A0 | Y0 | Y1 | Y2 | Y3 |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 0 | 0 |
| 1 | 0 | 0 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 | 0 | 1 |
the half adder and the full adder
Adding two bits gives a 2-bit answer: 1 + 1 = 10. Its low bit is A XOR B, its high bit A AND B.
| A | B | C (carry) | S (sum) |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 0 |
That is the half adder: one XOR and one AND. It is “half” because it has no carry in, so it can only add the rightmost column.
Every other column has three bits to add. The full adder of the chapter’s first page is two half adders and an OR:
S = A ⊕ B ⊕ Cin Cout = AB + Cin(A ⊕ B)
Read the carry in words: a column carries out if it generates a carry by itself (AB: both bits 1), or if it propagates the carry coming in (exactly one bit is 1, and Cin is 1). Those two ideas, generate and propagate, are what faster adders are built on.
one full adder per bit, and a wait
To add n-bit numbers, put n full adders side by side and wire each carry out to the next carry in.
The figure adds 11 + 6 (1011 + 0110) with 4 full adders. Bit 0 is on the right with carry in 0. The answer, 17, needs a fifth bit: the carry out of the last stage.
The catch is time. Bit 3 cannot know its sum until its carry in is settled, and that waits for bit 2, which waits for bit 1. The carry ripples through every stage. If a stage takes 100 ps from carry in to carry out (hypothetical), a 32-bit ripple adder can take 32 × 100 = 3200 ps, 3.2 ns. At one addition per clock cycle that alone would limit the clock to about 0.31 GHz.
Faster adders compute the carries in parallel. A carry-lookahead adder works out, from the generate and propagate signals of a whole group of bits at once, whether a carry leaves the group. Prefix adders push this to a delay that grows with log₂ n instead of n, at the cost of more gates. Subtraction needs no new adder: A − B = A + B + 1, the inverted B with the carry in set to 1 (Chapter 1’s two’s complement).
one block, several operations
The arithmetic logic unit computes whatever the instruction asks: add, subtract, AND, OR, compare. It is an adder and some gates, with a multiplexer choosing the result.
A few control bits pick the operation. In a common textbook design for RISC-V (Harris and Harris), three bits do it:
| ALU control | operation | A = 12, B = 10 |
|---|---|---|
000 | add | 0001 0110 = 22 |
001 | sub | 0000 0010 = 2 |
010 | and | 0000 1000 = 8 |
011 | or | 0000 1110 = 14 |
101 | slt | 0000 0000 = 0 |
Subtract reuses the adder: invert B and set the carry in to 1, giving 12 + 1111 0101 + 1 = 2 (8 bits). Set less than (slt) outputs 1 if A < B and 0 otherwise; it is computed from the sign of A − B (with a correction for overflow).
The ALU also reports the zero flag, 1 when every result bit is 0. A branch like “if A equals B” subtracts and checks zero; Chapter 5 wires it to the program counter.
the critical path sets the speed
A gate does not answer instantly. Its propagation delay is the time from an input change to a settled output, tens of picoseconds in a modern chip.
A circuit’s delay is that of its slowest path from any input to any output, the critical path. With these hypothetical gate delays:
| gate | delay |
|---|---|
| XOR | 60 ps |
| AND | 30 ps |
| OR | 35 ps |
the paths through the full adder are:
| path | delay |
|---|---|
| A → XOR → XOR → S | 120 ps |
| A → XOR → AND → OR → Cout | 125 ps |
| A → AND → OR → Cout | 65 ps |
| Cin → XOR → S | 60 ps |
| Cin → AND → OR → Cout | 65 ps |
The critical path is A → XOR → AND → OR → Cout, 125 ps. The sum is ready sooner, but no output can be trusted until 125 ps after the inputs change.
This is where clock speed comes from. Chapter 3 puts registers before and after a block of logic; the clock can tick no faster than the slowest path between them settles. Designers shorten critical paths with faster structures (lookahead adders), fewer gates in series, or by splitting the work into stages, which is pipelining (Chapter 6).