Draft preview. Lessons are being written and reviewed.

Digital Design / Module 4: Sequential logic

21. The D flip-flop

Understand the edge from two latches.

Lecture coming soon

See it move

The idea

A D flip-flop captures its input at one instant, the clock edge, then holds that value until the next edge. It is the basic memory element of synchronous design, and it is usually built from two latches.

Two latches, opposite phases

For a rising-edge flop:

  • Clock low: the first (master) is open and follows D. The second latch (slave) is closed and keeps Q steady.
  • Clock high: the master closes and freezes what D was at the moment of the edge. The slave opens and passes that frozen value to Q.

At no point is there a transparent path from D to Q, because the two latches are never open together. That is why the flop appears edge-triggered although each part is level-sensitive. Step through the animation to watch this happen.

always_ff @(posedge clk)
  q <= d;

Clock-to-Q delay

Q doesn't change instantly. Clock-to-Q (Tcq) is the time from the edge until the new value appears at the output, and it's part of every timing path.

Why synchronous design works

Each flop samples just once per cycle, so any glitches or slow settling between flops don't matter, as long as the logic has settled by the next edge. This turns a messy analog reality into something you can reason about one cycle at a time.

Common mistake

Expecting Q to take the new value in the same cycle. If q <= d, then during this cycle q shows what d was at the previous edge. Each flop in a path adds exactly one cycle of . Off-by-one mistakes here are among the most common RTL bugs.

Key ideas

  • Master and slave latches are transparent on opposite clock phases.
  • Q updates only at the active edge.
  • Clock-to-Q is the delay before the new value appears.

Lab: Setup and hold lab

Your exercise

Build a flop from two latches and simulate it.

Find the bug

This code has one deliberate bug. Read the symptom, then click the line you think is wrong.

What goes wrong: The output changes on the falling clock edge, half a cycle later than the rest of the design expects.

Interview questions

coreHow do you make a flop from latches?

basicWhat does the Q output of a rising-edge D flip-flop do between two clock edges, even if D changes many times?

basicThree rising-edge flops are chained, d to f1 to f2 to f3. D changes from 0 to 1 a little before rising edge number 1. After which edge does f3 first show 1?

always @(posedge clk) begin
  f1 <= d;
  f2 <= f1;
  f3 <= f2;
end

coreIn a master-slave rising-edge flop, what does each latch do while the clock is low, and while it is high? Why is there never a straight path from D to Q?

coreThis code models a flop with two latches. Which clock edge does it capture on, and how would you change it to capture on the falling edge?

always @(*) if (!clk) m = d;  // master
always @(*) if (clk)  q = m;  // slave

coreAt a rising edge, flop A's output changes. Flop B, on the same clock, takes A's output as its D. Why does B capture A's old value rather than the new one?

coreThis was meant to be a rising-edge flop, but in simulation q sometimes changes on the falling edge of the clock. What is wrong?

always @(clk)
  q <= d;

advancedCombinational logic between flops often glitches before it settles. Why is that usually harmless, and in which cases does a glitch become a real bug?

advancedA master-slave flop is driven by a clock and an inverted copy of it. If the inverter is slow, both latches can be open at the same time for a short moment around the edge. What can go wrong?

Practise these 9 questions with animations