AiTechWorlds
AiTechWorlds
Build a combinational circuit — a full adder, a multiplexer, a decoder — and it has a peculiar property: it has no memory whatsoever. Remove the inputs, and the outputs go dark. Change the inputs, and the outputs instantly change to reflect only the new values. The circuit doesn't remember what happened a millisecond ago. It doesn't even know time exists.
This is useful for arithmetic. But a computer needs to remember things. It needs to hold the result of the last calculation while it fetches the next instruction. It needs to track whether a loop has finished. It needs to store a variable across thousands of clock cycles.
The solution is a sequential circuit — a circuit whose output depends on both the current inputs AND the history of past inputs. It has state. It remembers.
The key insight: you can build memory from logic gates by feeding the output of a circuit back into its own input. When you do this carefully, the circuit can latch onto a value and hold it indefinitely, even when the original inputs change.
Consider a CPU executing the instruction x = x + 1. It must:
x from a registerSteps 1 and 3 require the same memory location to hold a value across time. Combinational logic cannot do this — it computes and immediately forgets. Flip-flops solve this problem.
"A flip-flop is a 1-bit memory cell. A CPU register is 64 flip-flops. A CPU with 16 general-purpose registers has 1,024 flip-flops just for register storage."
The SR latch (Set-Reset latch) is the foundation of all sequential circuits. It is built from two cross-coupled NOR gates.
S ──→ [NOR] ──→ Q
↑ ↓
R ──→ [NOR] ──→ Q̄
The cross-coupling is the magic: each gate's output feeds back as the other gate's input, creating a stable feedback loop that holds state.
| S | R | Q (Next) | Q̄ (Next) | State |
|---|---|---|---|---|
| 0 | 0 | Q (no change) | Q̄ (no change) | Hold |
| 0 | 1 | 0 | 1 | Reset |
| 1 | 0 | 1 | 0 | Set |
| 1 | 1 | 0 | 0 | Forbidden |
The SR latch is asynchronous — it changes state whenever S or R change. Synchronous circuits need an element that only updates at a specific time: the clock edge.
The D flip-flop (Data flip-flop) captures the value of its D input on the rising edge of the clock and holds it until the next rising edge.
Behavior: On each ↑ clock edge, Q ← D. Between clock edges, Q is frozen.
Implementation: A D flip-flop is essentially two SR latches in a master-slave configuration controlled by the clock. When CLK=0, the master latch is transparent; when CLK=1, the slave latch captures the master's output.
At each rising clock edge, Q captures the current value of D and holds it.
The JK flip-flop eliminates the forbidden state of the SR latch. J (Set) and K (Reset) correspond to S and R, but when both J=1 and K=1, the output toggles (Q becomes Q̄).
| J | K | Q (Next) | Action |
|---|---|---|---|
| 0 | 0 | Q | Hold |
| 0 | 1 | 0 | Reset |
| 1 | 0 | 1 | Set |
| 1 | 1 | Q̄ | Toggle |
JK flip-flops were widely used before D flip-flops became standard. Today they appear primarily in counters and certain FSM (Finite State Machine) designs.
The T (Toggle) flip-flop has a single input T:
It is a JK flip-flop with J=K=T. Primary use: binary counters — connect T=1 permanently and Q toggles every clock cycle, dividing the clock frequency by 2.
A register is simply N D flip-flops sharing a common clock, storing an N-bit value.
An 8-bit register stores one byte. A 64-bit register stores a 64-bit integer (like long in Java or int64_t in C).
Modern CPUs have:
A shift register is a chain of D flip-flops where each flip-flop's Q output is connected to the next flip-flop's D input. On each clock edge, data shifts one position left (or right).
Uses:
A 4-bit counter uses 4 T flip-flops and counts from 0000 to 1111 (0 to 15), then wraps back to 0000.
FF0: T=1 always → toggles every clock → LSB (1, 0, 1, 0...)
FF1: T=Q0 → toggles when FF0 output goes LOW
FF2: T=Q1 → toggles when FF1 output goes LOW
FF3: T=Q2 → toggles when FF2 output goes LOW
Output sequence: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1...
CPU use: Program counter increments by 1 (or 2, 4) each cycle; loop counters in hardware multiply units; timer circuits.
The clock is a square wave that synchronizes all sequential elements in a chip. Every D flip-flop in the CPU updates simultaneously on the rising (or falling) edge.
A modern CPU's clock distribution network:
For a D flip-flop to reliably capture data:
Violate these constraints and the flip-flop enters a metastable state — an undefined output that resolves randomly. Metastability is a real engineering concern in asynchronous clock domain crossing.
| Flip-Flop | Inputs | Behavior | Forbidden State? | Primary Use |
|---|---|---|---|---|
| SR Latch | S, R | Set/Reset/Hold | Yes (S=R=1) | Debounce switches, basic memory |
| D Flip-Flop | D, CLK | Captures D on clock edge | No | Registers, pipeline stages |
| JK Flip-Flop | J, K, CLK | Set/Reset/Hold/Toggle | No | FSMs, legacy counters |
| T Flip-Flop | T, CLK | Hold or Toggle | No | Binary counters, frequency dividers |
Get this course's notes on Telegram!
Free cheat sheets, summaries & practice exercises