All episodes · From One Switch to a CPU · Episode 7

How a Circuit Remembers: Latches, Flip-Flops and Registers

Pressing play loads the video from YouTube.

Every circuit so far forgets its answer the moment you let go of the switch. Now we build one that remembers.
A loop of two gates that holds its own output, the set-reset latch, the D latch, why a computer needs a clock, flip-flops that only change on the tick, and finally a register and a counter that count along with the clock.

Every animation in this video is drawn by code, and the narration is an AI voice.

Transcript

Press this button, and the light turns on. Let go, and it stays on. Every circuit we've built so far would have forgotten by now. This one remembers. Today, we build memory out of the same gates.

The trick is a loop. Take an OR gate, and feed its own output back into one of its inputs. Press the button: the output becomes one. That one flows back in, so the OR gate keeps saying one, even after you let go. It remembers. But now it can never forget: nothing can turn it off.

So we use two NOR gates instead. A NOR gate is an OR gate with a NOT on its output: it gives one only when both inputs are zero. Cross them: each gate's output feeds the other gate's input. One input on each side is left free: Set, and Reset. Press Set. The bottom gate's output drops to zero, the top gate now sees two zeros, and Q turns on. Let go of Set. Q stays on, because it's holding the bottom gate down by itself. Press Reset, and Q turns off. Let go, and it stays off. That's one bit of memory, called a set-reset latch. The only rule: don't press both at once.

To make it easier to use, add two AND gates and a NOT in front. Now there's a data line, D, and a write line, W. While W is one, Q simply copies D. When W drops to zero, the gates in front close, and Q holds the last value of D, no matter what D does next. This is a D latch: write when told, and hold the rest of the time.

Now imagine thousands of these latches working together. They need to agree on when to change. So a computer has a clock: a signal that ticks between zero and one, over and over. A modern processor's clock ticks about three billion times every second.

Put two latches in a row, and let the clock open them one after the other. The result is a flip-flop. A flip-flop copies D only at the instant the clock goes from zero to one. The rest of the time, it ignores D completely. Look: D wiggles all the time, but Q only changes on the ticks.

Line up four flip-flops on the same clock, and you have a register: it holds a four-bit number. On each tick, the register takes in a new number. Between ticks, it holds the old one steady, for everything else to use.

Now connect the register to an adder that adds one, and feed the sum back into the register. Tick: one. Tick: two. Tick: three. That's a counter. The processor uses one just like it to keep track of where it is in a program. More on that in a few episodes.

So: a loop of gates holds one bit, a clock tells every bit when to change, and a row of flip-flops holds a number. But a register holds only a few bits, and a computer needs billions. Next time: what's inside a memory chip. See you there.

Next episode: what's inside a memory stick.

Buy the Developer a Coffee

Entirely up to you: it works the same either way.

Every cup turns into the next update.

WeChat Pay QR code
WeChat Pay
Alipay QR code
Alipay
PayPal QR code
PayPal