The D Flip-Flop: From Level to Edge
From the outside, a D flip-flop looks almost identical to the gated D latch: one data input D, one clock input CLK, outputs Q and Q̄. The behavior inside is completely different, and that difference is exactly the fix the previous tutorial promised.
Level-Sensitive vs. Edge-Sensitive
A level-sensitive latch cares about a signal's value over some stretch of time ("while E is high"). An edge-sensitive flip-flop cares about a signal's transition at one specific moment ("the instant CLK goes from 0 to 1", the rising edge), and is completely blind to D the rest of the time. Every instant other than that edge, no matter what D is doing, Q simply holds.
Built From Two Latches: Master and Slave
The circuit below is wired the way real hardware builds one: two gated D latches in series, a master and a slave, with the master's enable wired to CLK inverted and the slave's enable wired to CLK directly. Fifteen gates total, and every one is a NAND or a NOT you already know.
CLK = 0: master enable = 1 (transparent), slave enable = 0 (holding)
master follows D, slave holds its last value
CLK = 1: master enable = 0 (holding), slave enable = 1 (transparent)
master freezes whatever D was an instant before CLK rose,
slave immediately copies that frozen value out to QBecause the two enables are inverted copies of each other, they can never both be transparent at the same time. That's what turns "two level-sensitive latches" into one edge-sensitive flip-flop: at every instant exactly one of the two is open, and D can only reach Q by passing through both, master then slave, which only happens in the narrow window right as CLK transitions from low to high.
Try It: Sampling Once Per Tick
Toggle D as many times as you like while CLK stays put, nothing happens to Q, because nothing you're touching is a clock edge. Now toggle CLK from 0 to 1 and back. Watch the master latch (left half) freeze the instant CLK rises, right as the slave latch (right half) opens and copies whatever the master was holding out to Q. That handoff, master closing exactly as slave opens, is the entire mechanism, laid out in gates instead of asserted as a black box.
Why Registers Use Flip-Flops, Not Latches
Chain several D flip-flops together, output of one into the input of the next, all sharing a single clock line, and you get the building block a shift register, a counter, or a processor's register file is made of. Because every flip-flop in the chain samples at the exact same instant, each stage advances by precisely one step per clock tick. No stage can race ahead into the next one mid-cycle, because for all but that one instant, every flip-flop in the chain is simply holding.
That guarantee is the entire reason edge-triggered storage exists. A multi-stage design built from plain gated latches would need careful, error-prone timing analysis to avoid race-through on every single chain of them, exactly the problem raised at the end of the last tutorial.
How the Simulator Actually Implements This
Worth calling out directly: the underlying simulation engine doesn't give a flip-flop any special "memory cell" data structure. Even the built-in primitive version does what this gate-level circuit does by construction. A flip-flop's Q output is just an ordinary output pin, and the engine's runtime state already tracks what every pin in the circuit is currently driving, so the stored bit is that pin's drive state, nothing more. Detecting the rising edge works by comparing a net's value immediately before and immediately after it's resolved during the same simulation step, a comparison the engine already computes for every net that changed.
The Full Arc, In One Line Each
1. A feedback loop turns two ordinary gates into an SR latch.
2. Gating logic (D, E) turns the SR latch into a gated D latch, no forbidden state.
3. Two gated D latches with inverted enables turn that into a D flip-flop, edge-triggered.
4. A D flip-flop, wired up N times, is an N-bit register.Nothing above a plain logic gate was ever required to get here.