Sequential Logic & Flip-Flops
Digital Logic Design · Hardware Foundations track
6 min read
In this module
Why Circuits Need Memory
Combinational circuits are powerful but amnesiac. Take an input away and the output collapses with it. A computer needs to remember things — the current value of a variable, the address of the next instruction, where it had got to in some half-finished computation. That's the territory of sequential logic.
Sequential circuits embed storage elements that latch a value and hold onto it until something explicitly tells them otherwise. Pair those with a clock signal that keeps everything marching in step and you get registers, counters, memory cells, and every other stateful piece of a processor.
The SR Latch: Simplest Memory
The SR latch (Set-Reset latch) is the simplest circuit that can hold onto a single bit. It's two cross-coupled NOR gates, or equivalently two cross-coupled NAND gates if you prefer.
Two inputs:
- S (Set) — pulse it high to force the output Q to
1. - R (Reset) — pulse it high to force Q to
0.
Two outputs:
- Q — the stored value.
- Q' — its complement.
| S | R | Q (next) | Behaviour |
|---|---|---|---|
| 0 | 0 | Q (hold) | No change |
| 1 | 0 | 1 | Set |
| 0 | 1 | 0 | Reset |
| 1 | 1 | Undefined | Invalid - avoid |
The whole trick is the "hold" state. When both S and R are 0, the latch keeps whatever value it last took on. It remembers. The invalid state (S=1, R=1) opens up a race condition, so real designs bolt extra logic on the front to keep that case from happening.
An SR latch uses cross-coupled feedback to store one bit. Setting S high stores a 1; setting R high stores a 0. When both inputs are low, the latch holds its previous value. This feedback mechanism is the foundation of all digital memory.
Try it on the NOR version below. Set, then drop S back to 0 and watch Q stay put. Then push both inputs to 1, see Q and Q' both fall to 0, and hit Release both together to watch the stored value turn into an X.
SR latch
Two cross-coupled NOR gates. S=1 sets Q to 1, R=1 resets it to 0, and with both at 0 the latch holds its value.
Inputs
Outputs
Change an input to start the timing diagram.
Each column is one change, not a fixed time. Outputs start at 0.
The D Flip-Flop
The D flip-flop (Data flip-flop) cleans up the SR latch's rough edges and is the standard storage element in modern digital design. It has one data input (D) and a clock input (CLK). On the rising edge of the clock — the instant CLK swings from 0 to 1 — the flip-flop snapshots whatever sits on D and holds that value at output Q until the next rising edge comes round.
| CLK Edge | D | Q (next) |
|---|---|---|
| Rising | 0 | 0 |
| Rising | 1 | 1 |
| No edge | X | Q (hold) |
How does it get from a level-sensitive latch to an edge? In two steps. First, a D latch feeds D and its complement into an SR latch's S and R inputs through a pair of gates enabled by the clock. That rules out the invalid S=R=1 case, but the latch is transparent: while CLK is high, Q follows D. Second, a master-slave flip-flop chains two D latches with opposite enables. The master follows D while CLK is low; the slave is closed. When CLK rises, the master freezes and the slave opens, passing on the value D had at that instant. That's the edge-triggered behaviour. (Many real cell libraries build flip-flops differently at the transistor level, but master-slave is the classic way to see it.)
Between clock edges the output stays put no matter what D does. This edge-triggered behaviour means every flip-flop updates at exactly the same instant, which keeps data from racing through the system out of order.
Raise the clock below, then wiggle D a few times while it's still high. The latch's Q chases every wiggle; the flip-flop's Q keeps whatever D was at the rising edge. Switch tabs to see the same run from the other element's side.
Sequential logic simulator
A positive-edge-triggered D flip-flop. Q copies D only at the instant CLK rises from 0 to 1.
Inputs
Clock
Outputs
Change an input to start the timing diagram.
Each column is one change, not a fixed time. Dashed lines: rising edges. Outputs start at 0.
A D flip-flop captures the value of its data input on the rising edge of the clock and holds it stable until the next edge. It stores exactly one bit. Edge-triggering ensures all storage elements in a circuit update simultaneously and predictably.
Clock Signals and Edge-Triggering
The clock is a square wave swinging between 0 and 1 at a fixed frequency. A 3 GHz processor's clock toggles three billion times every second. Each rising edge is a discrete instant where the processor's state is permitted to change.
┌──┐ ┌──┐ ┌──┐ ┌──┐CLK: ──┘ └──┘ └──┘ └──┘ └── ↑ ↑ ↑ ↑ Rising edges: state updates happen hereBetween edges, combinational logic chews on the current state and produces new values. The next edge captures those values into flip-flops. The clock period has to be long enough for the slowest signal path (the critical path) to settle before the next edge arrives, otherwise you latch in garbage.
Building Registers from Flip-Flops
One D flip-flop stores one bit. To store a multi-bit value you line up several flip-flops side by side, all driven by the same clock signal.
- 8 D flip-flops = an 8-bit register (one byte)
- 32 D flip-flops = a 32-bit register
- 64 D flip-flops = a 64-bit register
All 32 flip-flops share one clock, so every bit updates simultaneously. A write-enable signal usually rides along with this: the register only captures new data when the clock edge arrives and write-enable is asserted.
Here's a 4-bit version. Set a pattern on D3 to D0, give it a full clock pulse, then turn WE off and pulse again with a different pattern: the second one never gets in.
4-bit register
Four D flip-flops sharing one clock. On a rising edge with write enable (WE) at 1, all four bits load at once.
Inputs
Clock
Outputs
Change an input to start the timing diagram.
Each column is one change, not a fixed time. Dashed lines: rising edges. Outputs start at 0.
The register file inside a CPU — that bank of R0, R1, R2 and friends — is exactly this. A pile of multi-bit registers built from D flip-flops, fronted by decoding logic that picks which register to read or write.
From Bits to State
Sequential logic is what transforms a pile of gates into a machine that can step through a program. Strip out the flip-flops and you've got no program counter, no registers, no state surviving between clock cycles. Every concept from the previous module rides on the ability to keep bits stable from one clock edge to the next.