The Edge-Triggered D Flip-Flop
Build an edge-triggered flip-flop from two latches on opposite clock phases, and add a clear and an enable.
You Will Be Able To
- Explain how a master and a slave latch make an edge-triggered flip-flop.
- Read the clock-to-Q delay of a flip-flop from a waveform.
- Add an asynchronous clear and an enable to a flip-flop.
- Predict a flip-flop's output from a clock and data waveform.
- Lesson
- G5.6
- Time
- 25 min
- Runs on
- Your browser
- Check
- 5 questions
A latch passes its input for a whole clock phase. So a fast path can race through two latches in one cycle. A flip-flop takes its input at one instant, the clock edge. It is built from two latches that are never open at the same time.
- Cycle 2
Clock low: the master has followed
dto 1.qis still unknown. - Cycle 4
After the rising edge at 20 the slave shows the master:
q= 1. - Cycle 6
dfell at 30, but the clock is high and the master is shut. - Cycle 8
Clock low: the master now follows
dto 0. The slave is shut, soqholds. - Cycle 10
After the edge at 50,
qtakes the master's 0.
Measured on the master-slave circuit model below.
Master and Slave
Put two D latches in a row. The first, the master, is enabled by the inverted clock. The second, the slave, is enabled by the clock.
- I/O
- Storage
- Data
- clk = 0
The master follows
d. The slave is shut and holdsq. - Rising edge
The master shuts, holding the value
dhad at that moment. The slave opens and passes it toq. - clk = 1
The master is shut, so changes of
dstop there.qkeeps the sampled value.
Read the diagram as text
- d: data
- Master latch: open while clk = 0
- Slave latch: open while clk = 1
- q: changes only after a rising edge
- d → Master latch
- Master latch → Slave latch: master
- Slave latch → q
At no time is there an open path from d to q. That is what makes the pair edge-triggered.
Sampling at the Edge, and Clock-to-Q
The value of d at the rising edge is the value the flip-flop keeps. Changes of d at any other moment do not reach q until the next edge.
q does not change at the edge itself. The time from the clock edge to the new q is the clock-to-Q delay. In the gate model of the lab it is 3 ns when q rises and 2 ns when it falls. The two paths through the slave latch differ.
| Edge | d at the edge | q changes at | Clock-to-Q |
|---|---|---|---|
| 10 ns | 1 | 13 ns | 3 ns (q rises) |
| 30 ns | 0 | 32 ns | 2 ns (q falls) |
| 50 ns | 0 | no change | — |
| 70 ns | 1 | 73 ns | 3 ns (q rises) |
Measured in the lab, gate-level flip-flop, every gate 1 ns. These are model numbers, not a cell library's.
Clear and Enable
Real flip-flops add pins. An asynchronous clear forces q to 0 at once, with no clock. An enable decides whether an edge loads d or keeps q.
The RTL
always_ff @(posedge clk or negedge clr_n)
if (!clr_n) q <= 1'b0;
else if (en) q <= d;
In the lab it loads at 70 ns with en = 1 and holds at 90 ns with en = 0. It clears at once when clr_n falls at 92 ns, between edges.
What it stands for
// a mux in front of D:
// D = en ? d : q
// and a clear pin that
// overrides the clock
An enabled flop recirculates its own output when en = 0. The clock still ticks every cycle.
The enabled, resettable register is written in lesson G6.1 and practised in its problems. Here it is enough to see it as a flip-flop with a mux in front and a clear pin beside it.
Asked in interviews
How is an edge-triggered flip-flop built from latches?
Two D latches in series: a master enabled by clk' and a slave enabled by clk. At the rising edge the master shuts with the current d and the slave passes it on. They are never open together, so d cannot race through.
What is clock-to-Q delay?
The time from the active clock edge until q shows the new value. It is part of every path from one flop to the next, so it enters the setup check and the hold check.
Asynchronous or synchronous reset?
An asynchronous reset acts at once, without a clock, so it works even when the clock is stopped. Its release must be synchronized. A synchronous reset is an ordinary input sampled at the edge.
Try It
The gate-level flop dff changes only after rising edges: at 13, 32, 73 and 92 ns. The change of d at 15 ns, during the high phase, never reaches q.
The RTL flop dffre loads at 70 ns because en is 1, and holds at 90 ns because en is 0. The clear at 92 ns acts at once, without an edge.
Swap the enables of the two latches in dff and run again. On which edge does the flop sample now?
t=13 q=1 (3 ns after the edge at 10) t=32 q=0 (2 ns after the edge at 30) t=72 q2=1 (en = 1 at the edge at 70) t=73 q=1 (3 ns after the edge at 70) t=92 q2=1 (en = 0 at the edge at 90) t=92 q=0 (2 ns after the edge at 90) t=93 q2=0 (clear, between edges)Read included file: tb.sv
// tb.sv: d moves at several points of the clock; the bench lists every change of q (read-only).
`timescale 1ns/1ns
module tb;
logic clk = 0, d = 0, clr_n = 1, en = 1;
wire q;
logic q2;
time t_edge;
dff u1 (.clk(clk), .d(d), .q(q));
dffre u2 (.clk(clk), .clr_n(clr_n), .en(en), .d(d), .q(q2));
always #10 clk = ~clk; // rising edges at 10, 30, 50, 70, 90 ns
always @(posedge clk) t_edge = $time;
always @(q) if ($time > 5) $display("t=%0d q=%b (%0d ns after the edge at %0d)", $time, q, $time - t_edge, t_edge);
initial begin
#3 d = 1; // before the edge at 10
#12 d = 0; // t=15: clk is high, the master is shut
#20 d = 1; // t=35: clk is high again
#10 d = 0; // t=45: clk is low, the master follows
#17 en = 1; d = 1; // t=62
#10 $display("t=72 q2=%b (en = 1 at the edge at 70)", q2);
en = 0; d = 0;
#20 $display("t=92 q2=%b (en = 0 at the edge at 90)", q2);
clr_n = 0;
#1 $display("t=93 q2=%b (clear, between edges)", q2);
$finish;
end
endmodule
Check Yourself
Common Mistakes
Wrong Say the flip-flop samples while the clock is high.
While the clock is high the master is shut. In the lab d fell at 15 ns, during the high phase, and q kept its 1.
Fix A rising-edge flop takes d at the rising edge only.
Wrong Open both latches on the same clock phase.
Two latches open together are one wide latch: d passes straight through while that phase lasts.
Fix Enable the master with clk' and the slave with clk, so they are never open together.
Wrong Expect q to change exactly at the edge.
The slave needs time to pass the value. In the lab model a rising q takes 3 gate delays and a falling q takes 2.
Fix Count the clock-to-Q delay in every timing budget (G5.8).
Wrong Leave the clear out of the sensitivity list when it must act at once.
Then the block runs only at clock edges, and the clear waits for the next edge.
Fix Write always_ff @(posedge clk or negedge clr_n) for an asynchronous active-low clear.
Remember
- A master latch on clk' and a slave latch on clk make a rising-edge flip-flop; they are never open together.
- The flop keeps the value d had at the edge; changes at other times wait for the next edge.
- Clock-to-Q is the time from the edge to the new q. The lab gate model takes 3 ns rising and 2 ns falling.
- An enable is a mux that feeds q back; an asynchronous clear acts without a clock edge.