From a Transistor to a Computer
By Amiro
A step-by-step lesson for the Digital Electronics course: how a transistor works as a voltage-controlled switch, how transistors make logic gates, how gates add binary numbers and remember bits, and how those parts become a working computer that fetches, decodes and executes a program.
A lesson of Digital Electronics
Transcript
- 0:00 From a transistor to a computer: how tiny electronic switches, wired together layer by layer, become a machine that computes.
- 0:08 Every computer is built from transistors, and a modern processor holds billions of them. In this lesson we climb from a single transistor to a working computer, one layer at a time. Transistors make logic gates. Gates make circuits that do arithmetic, and circuits that remember. Together, those become a computer. And each layer is built only from the layer below it.
- 0:33 Part one: bits, and how a single wire carries them.
- 0:38 Inside a chip, information travels as voltages on wires, and a digital circuit uses just two levels. A voltage near the supply voltage, called V D D, is high, and stands for one. A voltage near zero volts is low, and stands for zero. Here is a signal changing over time. Reading it slot by slot gives zero, one, one, zero, one, zero. Each one or zero is called a bit, short for binary digit.
- 1:09 Why only two levels? Real wires pick up noise from their surroundings, so the voltages wobble. But a wobbly high is still clearly high, and a wobbly low is still clearly low. The gap between the two bands is a safety margin, so every bit is still read correctly. And every circuit restores clean levels at its output, so noise never piles up. That robustness is the great strength of digital electronics.
- 1:36 To compute with bits, we need one more ingredient: a switch that is opened and closed by a voltage, so that one bit can control another. While the control bit is zero, the switch is open, and the lamp stays dark. When the control bit becomes one, the switch closes, current flows, and the lamp lights up. This voltage-controlled switch has a name: the transistor. It is the hero of this lesson.
- 2:03 Part two: the transistor, a switch made of silicon.
- 2:08 Here is a slice through an n-channel MOS transistor, or NMOS. Its body is silicon doped to be p-type, which leaves it with almost no free electrons. Into it are added two n-type regions that are rich in free electrons: the source and the drain. On top, between them, lies a very thin insulating layer of oxide, and on that sits the gate, a conducting plate.
- 2:34 With zero volts on the gate, the free electrons stay inside the source and the drain. The p-type body between them has none to spare, so nothing can carry a current from drain to source. The transistor is off: it behaves like an open switch.
- 2:50 Now raise the gate voltage. The positive charge on the gate pulls electrons up to the surface, just under the oxide. Once the gate voltage passes a threshold, called V T, enough electrons gather to form a thin channel that joins the source to the drain. Current can now flow. The transistor is on, like a closed switch.
- 3:11 In circuit diagrams, the NMOS transistor is drawn with this symbol: gate on the left, drain on top, and source at the bottom. The oxide insulates the gate, so almost no current flows into it: the gate controls the switch by its voltage alone. So we can think of it simply. Gate at one: switch closed, on. Gate at zero: switch open, off. In short, an NMOS is on when its gate is one.
- 3:43 Its twin is the p-channel transistor, or PMOS. It is built the other way round, with p-type source and drain in an n-type body, so it behaves in the opposite way. Its symbol has a small circle on the gate, which means not. Gate at zero: switch closed, on. Gate at one: switch open, off. So a PMOS is on when its gate is zero.
- 4:10 One more detail shapes every chip. An NMOS passes a zero perfectly, but passes a one only weakly, because it starts to switch itself off as its output rises toward its gate voltage. A PMOS is the mirror image: it passes a strong one, but a weak zero. So NMOS transistors are placed between the output and ground, to pull the output down to zero. PMOS transistors go between the supply and the output, to pull it up to one.
- 4:41 Part three: building logic gates out of transistors.
- 4:46 Now put the two types together. A PMOS connects the supply, V D D, to an output wire, Y. An NMOS connects Y to ground. And both gates are joined to a single input, A. Pairing the two complementary types like this is called CMOS.
- 5:05 Set A to zero. The PMOS sees zero on its gate, so it turns on. The NMOS turns off. Y is connected to the supply and cut off from ground, so Y is one, the opposite of A.
- 5:20 Now set A to one. The PMOS turns off, and the NMOS turns on. Y is pulled down to ground, so Y is zero, again the opposite of A.
- 5:32 Whatever A is, Y is the opposite. This is the NOT gate, also called an inverter: a triangle with a small circle, written Y equals A bar. And notice: in both states one transistor is off, so current never flows straight from the supply to ground. That is why CMOS chips waste so little power, and why almost every chip is built this way.
- 5:57 With four transistors, we can combine two inputs, A and B. Two PMOS transistors sit side by side, in parallel, between the supply and Y. Two NMOS transistors sit one above the other, in series, between Y and ground. Input A drives one transistor of each pair, and input B drives the other one in each pair.
- 6:20 The path down to ground needs both NMOS on at once, because they are in series. Take the first case, A and B both zero: both PMOS are on, so Y is one. Next, A zero and B one: the left PMOS is still on, and the path to ground is broken, so Y is one. Then A one and B zero: now the right PMOS holds Y up, and Y is still one.
- 6:49 Only when A and B are both one do both NMOS turn on, while both PMOS turn off. Then Y is pulled down to ground, and Y is zero.
- 7:01 So the output is zero only when both inputs are one. That is the opposite of AND, so this gate is called NAND, for not-and. Its symbol is the AND shape with an inverting circle, and we write Y equals A dot B, with a bar over it.
- 7:18 Now swap the arrangement: put the two PMOS in series, one above the other, and the two NMOS side by side, in parallel. Now Y can reach the supply only through both PMOS, which happens only when A and B are both zero. Then Y is one. But if either input is one, its NMOS turns on and pulls Y down to ground, so Y is zero in every other case.
- 7:45 So Y is one only when both inputs are zero. That is the opposite of OR, so this is the NOR gate, for not-or. Its symbol is the OR shape with an inverting circle, and we write Y equals A plus B, with a bar over it.
- 8:02 Add an inverter after a NAND, and the two inversions cancel: the result is the AND gate, which outputs one only when both inputs are one. In the same way, a NOR followed by a NOT gives the OR gate, which outputs one when at least one input is one. In CMOS the inverting gates come naturally, so AND and OR each cost one extra inverter: six transistors instead of four.
- 8:30 One more gate will be essential. Exclusive OR, or XOR, outputs one when its two inputs are different, and zero when they are the same. We write it A circled-plus B. It can be built from the gates we already have, because XOR is one when A or B is one, but not both. So an OR gate and a NAND gate feed into an AND gate, and that is XOR.
- 8:56 In fact, NAND alone can build any logic at all. Tie its two inputs together, and it becomes a NOT. Follow a NAND with that NOT, and you get AND. Invert both inputs first, and you get OR, because A or B is the same as: not, not-A and not-B. That rule is called De Morgan's law. So a whole chip could be built from NAND gates alone.
- 9:22 This is our first step up the ladder. From here on, we stop drawing transistors. Each gate is just a symbol with a truth table, and we trust the transistors inside to do their job. Hiding detail like this is called abstraction. It is how engineers manage designs with billions of transistors.
- 9:41 Part four: writing numbers with bits.
- 9:45 Gates work on single bits. To do arithmetic, we need numbers made of bits. First, recall how ordinary decimal numbers work. In four hundred and three, each place is worth ten times the place to its right: ones, tens, hundreds. So the number means four hundreds, no tens, and three ones.
- 10:07 Binary works the same way, but with only two digits, zero and one, and each place is worth twice the place to its right: one, two, four, eight. So one, zero, one, one means one eight, no four, one two, and one one, which adds up to eleven in decimal.
- 10:29 Here is how counting goes in binary: zero, one, one-zero, one-one, one-zero-zero, and so on. Whenever a place overflows, it resets and carries into the next place. With four bits we can write sixteen different numbers, from zero to fifteen. Every extra bit doubles that, so n bits give two to the power n numbers. A modern processor works with words of sixty-four bits at a time.
- 10:58 Adding binary numbers works just like adding by hand in decimal: column by column, starting from the right. For a single column there are only four cases. The only new fact is that one plus one is two, which in binary is one-zero. So we write zero, and carry the one into the next column.
- 11:18 Let us add six and seven: zero-one-one-zero plus zero-one-one-one. Start at the right: zero plus one is one. Next column: one plus one is two, so write zero and carry one. Third column: the carry, plus one, plus one, makes three, which is one-one: write one and carry one again. Last column: the carry, plus zero, plus zero, makes one. The result, one-one-zero-one, is thirteen. Exactly right.
- 11:52 Part five: an adder, built from logic gates.
- 11:56 Start with the simplest case: adding two single bits, A and B. The result can be zero, one or two, so it needs two output bits: a carry bit, C, and a sum bit, S. Now look at the columns. The sum is one when exactly one input is one: that is XOR. The carry is one only when both inputs are one: that is AND, written A dot B.
- 12:25 So two gates make a half adder: an XOR gate for the sum, and an AND gate for the carry. Try one plus one: set both inputs to one. The XOR sees two equal inputs, so it outputs zero. The AND outputs one. Carry one, sum zero: one-zero, which is two in binary.
- 12:47 Every other column has to add three bits: A, B, and the carry coming in from the column on its right, called C in. The answer can be anything from zero to three, so it still fits in two bits: a sum bit S, and a carry out, C out, that goes to the next column. In fact, C out and S simply count how many of the three inputs are one. A circuit that does this is called a full adder.
- 13:14 We can build it from two half adders. The first one adds A and B, giving a partial sum, s one. The second adds that partial sum to C in, giving the final sum, S. A carry can come out of either half adder, so an OR gate merges the two carries into C out.
- 13:33 Let us check it with all three inputs at one. The first half adder adds one and one: sum zero, carry one. The second adds that zero to C in, which is one: so the sum is one, with no carry. The OR gate passes the first carry through. So C out is one, and S is one: one-one, which is three, exactly right.
- 13:56 Now wrap the full adder in a box, and call it F A. Inside are five gates and a few dozen transistors, but from now on we only need its pins: A, B and C in go in; S and C out come out.
- 14:12 To add four-bit numbers, line up four full adders, one for each column, with bit zero on the right. Each adder's carry out feeds the next adder's carry in, just like carrying by hand. The very first carry in is zero. This chain is called a ripple-carry adder.
- 14:30 Feed in six and seven. The stages settle one after another, as the carry ripples from right to left. Stage zero: sum one, carry zero. Stage one: sum zero, carry one. Stage two: sum one, carry one. Stage three: sum one, carry zero. The output is one-one-zero-one: thirteen, exactly as we found by hand.
- 14:57 Gates are fast, but not instant: each one takes a few picoseconds to switch. Here, the last sum bit has to wait while the carry passes through every stage, so the delay grows with the number of bits. That is why real processors use faster designs, such as carry-lookahead adders, which work out all the carries in parallel.
- 15:18 Subtraction needs negative numbers. The standard trick is called two's complement: the leftmost bit is worth minus eight instead of plus eight. So one-zero-one-one means minus eight, plus two, plus one: minus five. To negate any number, flip every bit and add one. Five is zero-one-zero-one; flipped, one-zero-one-zero; plus one, one-zero-one-one: minus five. So A minus B is simply A plus not-B, plus one.
- 15:53 And that is easy with our adder. Put an XOR gate on each B input, controlled by a signal called SUB. An XOR with one input at one flips the other input, and with zero it passes it unchanged. So when SUB is one, every bit of B is flipped, and the same SUB signal enters as the first carry, adding the one. Seven minus five becomes seven plus one-zero-one-zero plus one, which gives two. The final carry out is simply ignored.
- 16:24 Part six: choosing between results, and the arithmetic logic unit.
- 16:29 A processor must also choose between results. The circuit that chooses is called a multiplexer. Here it picks between two inputs, A and B, using a select bit, S. With S at zero, the inverter turns the top AND gate's lower input to one, so A passes straight through, while the bottom AND gate is held at zero. So Y copies A. With S at one, it is the other way round, and Y copies B. A selector, built only from gates.
- 17:01 Now we can build an arithmetic logic unit, or A L U. Inputs A and B go to several circuits at once: the adder-subtractor, a row of AND gates, and a row of OR gates. All of them compute in parallel, all the time. A multiplexer then picks just one result, selected by a two-bit operation code, called op. The whole unit is drawn with this notched symbol, with the op code entering from below.
- 17:30 The ALU also reports two facts about its result, called flags. The zero flag, Z, is one when every bit of the result is zero. That takes a single NOR gate across the result bits. The carry flag, C, is simply the adder's final carry out. Later, these flags will let a program make decisions.
- 17:52 Part seven: memory, built from a loop of gates.
- 17:57 Every circuit so far is combinational: its output depends only on its inputs at this moment. Change the inputs, and the old answer is gone, without a trace. But to work step by step, keeping a running total, or remembering where it is in a program, a computer must store results. It needs memory, a way to hold on to bits.
- 18:19 The key idea is feedback. Connect two inverters in a loop. If Q is one, the lower inverter makes Q bar zero, and the upper inverter turns that zero back into one. The loop holds itself, as long as the power is on. But Q equal to zero is just as stable. Two stable states: that is one bit of memory.
- 18:42 To write into the loop, replace each inverter with a NOR gate, which gives it a second input: S, for set, and R, for reset. With both at zero, each NOR acts just like an inverter, so the loop simply holds. Now pulse S to one: the lower NOR outputs zero, and Q becomes one. When S drops back to zero, Q stays at one. It remembers! A pulse on R resets Q to zero in the same way. This is the SR latch. S and R must never be one at the same time.
- 19:17 A latch is easier to use with a single data input. Put two AND gates and an inverter in front of it: S becomes D, and R becomes not-D, but only while an enable signal, E, is one. While E is one, Q follows D, whatever it does. When E drops to zero, both AND gates output zero, so the latch holds the last value of D. This is the D latch.
- 19:44 A computer moves in steps, set by a clock: a signal that switches between zero and one at a steady rate, billions of times per second in a modern processor. The moment it rises from zero to one is called the rising edge. We want memory that changes only at that instant, and never in between those instants.
- 20:05 Now put two D latches in a row, and drive their enables with opposite phases of the clock. While the clock is low, the first latch, the master, follows D, while the second, the slave, holds. When the clock rises, the master freezes, and the slave copies the master's value to Q. So Q changes only at the rising edge, and takes the value D had at that instant. This is the D flip-flop, the basic memory cell of every processor.
- 20:34 Here is how it behaves over time. D may change whenever it likes, but Q looks at D only at each rising edge, marked by the dots, and holds that value until the next edge. A short glitch on D between two edges is simply ignored. That is exactly what we need: memory that updates in step with the clock.
- 20:55 Part eight: registers, counters, and memory chips.
- 21:00 Line up four D flip-flops, and drive them all with the same clock. Together they store a four-bit number. Put a number on the D inputs, and at the next rising edge, all four bits are captured at once. This is a register. A sixty-four-bit processor has registers made of sixty-four flip-flops side by side.
- 21:21 Usually a register should not take a new value on every tick. So a multiplexer goes in front of each flip-flop. When the load signal is zero, the multiplexer feeds Q back into D, so at every clock edge the flip-flop simply reloads its own value: it keeps it. When load is one, the new data passes through instead, and it is stored at the next clock edge, replacing the old value.
- 21:47 Now feed a register's output through an adder that adds one, and back into its own input. On every clock edge, it stores its own value plus one: zero, one, two, three, and so on. This is a counter. The processor uses one, called the program counter, or PC, to step through its instructions, one by one.
- 22:10 To store many numbers, we must pick one of them by its number, called its address. A decoder does this. It takes a two-bit address and turns on exactly one of four output lines. Each output is a single AND gate that recognizes one pattern, with inverters on the bits that must be zero. Address one-zero, for example, turns on line two, and only line two, while the other three stay at zero.
- 22:37 Stack four registers in rows, and you have a memory. To write, the decoder uses the address to enable loading in one row only, and the data on the input lines is stored there at the clock edge. To read, a multiplexer uses the same address to pick that row's bits for the output. Any row can be reached directly, in the same short time, which is why this is called random access memory, or RAM.
- 23:02 Real memory chips use the same ideas, with smaller cells. A static RAM cell is our inverter loop again, plus two access transistors that connect it to the bit lines when the word line is on: six transistors per bit, fast enough for the caches inside a processor. Main memory is dynamic RAM: each bit is a tiny charge on a capacitor, behind a single transistor. The charge leaks away, so every cell is refreshed many times per second. Sixteen gigabytes of it hold about one hundred and thirty-seven billion bits.
- 23:36 Part nine: putting it all together into a computer.
- 23:41 We now have every part we need. The last big idea is that instructions are numbers too, stored in the same memory as the data. In our small computer, each instruction is eight bits. The first four bits are the operation code, or opcode; the last four are a memory address. For example, zero-zero-one-zero, one-one-one-zero means: add the number stored at address fourteen to A, the processor's working register.
- 24:10 Here is the whole machine, built from the parts we have made. Down the middle runs a bus: a set of shared wires that carries one number at a time from one part to another. On the left are the program counter, the memory address register, the memory itself, and the instruction register. On the right are register A, the ALU, a second register B, and the control unit, which directs everything.
- 24:36 Here is a small program, already in memory. Address zero: load the number at fourteen into A. Address one: add the number at fifteen. Address two: store A at address thirteen. Address three: halt. The data sits right beside it: six at fourteen, and seven at fifteen.
- 24:58 Each instruction runs in three steps, timed by the clock. Step one is fetch. The program counter holds zero, and its value travels over the bus into the memory address register, so the memory knows which address to read. The instruction stored there comes back over the bus into the instruction register. Then the PC counts up by one, ready for the next instruction.
- 25:22 Step two is decode. The opcode half of the instruction goes to the control unit. The control unit is just a decoder plus a few gates. For each opcode, it switches on the right control signals: which part puts its number on the bus, which part loads it, and which operation the ALU should do.
- 25:41 Step three is execute. For load fourteen, the address part of the instruction travels over the bus into the memory address register. The memory reads address fourteen, which holds six, and the six travels over the bus into register A. Then the whole cycle starts again: fetch, decode, execute.
- 26:02 Next cycle. Fetch brings in add fifteen, and the PC becomes two. The address fifteen goes to the memory, and its value, seven, is copied into register B. The ALU adds A and B, using the very ripple-carry adder we built, and at the next clock edge the sum, thirteen, is stored back in A.
- 26:23 Then store thirteen. Its address goes to the memory, and A's value travels over the bus into address thirteen. Finally, the processor fetches halt, and stops. Our program has computed six plus seven, and the answer is waiting in memory for whoever needs it.
- 26:41 One more kind of instruction makes this a real computer: the jump. A jump loads a new address into the program counter, so the program can go back and repeat a loop. A conditional jump does so only if a flag is set, for example when the last result was zero. With loops and decisions, this simple machine can, given enough memory and time, compute anything that any computer can compute.
- 27:06 Let us look back down the ladder. A transistor is a switch, controlled by a voltage. A few transistors make a logic gate. Gates make adders, multiplexers and an ALU. Loops of gates make latches, flip-flops, registers and memory. And with a clock, a program counter and a control unit, these become a computer, one that runs any program you give it.
- 27:31 Now count the transistors. An inverter needs two. A NAND gate, four. A full adder, about twenty-eight, and a D flip-flop about twenty. Our little computer needs a few thousand. A modern processor uses the very same ideas, with tens of billions of transistors, each switching billions of times every second.
- 27:52 So everything a computer does comes down to this: tiny switches, turning on and off, billions of times a second, wired together layer upon layer. Thanks for watching.
More explanations on Sifr