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.

Inside a Master-Slave Flip-FlopSampled every 5 gate delays of the master-slave circuit model, plus the last column at t = 53.
012345678910clkdmasterqedgeedge
Scroll horizontally ↔
  1. Cycle 2

    Clock low: the master has followed d to 1. q is still unknown.

  2. Cycle 4

    After the rising edge at 20 the slave shows the master: q = 1.

  3. Cycle 6

    d fell at 30, but the clock is high and the master is shut.

  4. Cycle 8

    Clock low: the master now follows d to 0. The slave is shut, so q holds.

  5. Cycle 10

    After the edge at 50, q takes 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.

Two Latches, Opposite PhasesArrows show data. Only one latch is open at any time.
  • I/O
  • Storage
  • Data
Two latches, opposite phasesArrows show data. Only one latch is open at any time. io d data storage Master latch open while clk = 0 storage Slave latch open while clk = 1 io q changes only after arising edge master
  1. io ddata
    • Master latch
  2. storage Master latchopen while clk = 0
    • master → Slave latch
  3. storage Slave latchopen while clk = 1
    • q
  4. io qchanges only after a rising edge
  1. clk = 0

    The master follows d. The slave is shut and holds q.

  2. Rising edge

    The master shuts, holding the value d had at that moment. The slave opens and passes it to q.

  3. clk = 1

    The master is shut, so changes of d stop there. q keeps 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.

Master and Slave
Master and Slaveg0 · NOTnclk=0g1 · NOTm_nd=1g2 · ANDm_s=0g3 · ANDm_r=0g4 · NORmaster=0g5 · NORm_qn=1g6 · NOTs_nd=1g7 · ANDs_s=0g8 · ANDs_r=1g9 · NORq=0g10 · NORs_qn=1d=0clk=1
d=0 clk=1
g0 · NOTclk=1 → nclk=0
g1 · NOTd=0 → m_nd=1
g2 · ANDd=0, nclk=0 → m_s=0
g3 · ANDm_nd=1, nclk=0 → m_r=0
g4 · NORm_r=0, m_qn=1 → master=0
g5 · NORm_s=0, master=0 → m_qn=1
g6 · NOTmaster=0 → s_nd=1
g7 · ANDmaster=0, clk=1 → s_s=0
g8 · ANDs_nd=1, clk=1 → s_r=1
g9 · NORs_r=1, s_qn=1 → q=0
g10 · NORs_s=0, q=0 → s_qn=1
Read the connections
GateKindInputsOutput
g0NOTclknclk
g1NOTdm_nd
g2ANDd, nclkm_s
g3ANDm_nd, nclkm_r
g4NORm_r, m_qnmaster
g5NORm_s, masterm_qn
g6NOTmasters_nd
g7ANDmaster, clks_s
g8ANDs_nd, clks_r
g9NORs_r, s_qnq
g10NORs_s, qs_qn

Outputs: q. MUX inputs: d0, d1, select. Latch/flip-flop inputs: data, enable/clock. Unknown values are marked x; high impedance is folded into x in this model.

Gate delay 53. Settled after 53 gate delays from t = 0.

Output values at each gate delay01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253qxxxxxxxxxxxxxxxxxxxxxxx1111111111111111111111111111100
Scroll horizontally ↔
Every computed frame
Timeq
0x
1x
2x
3x
4x
5x
6x
7x
8x
9x
10x
11x
12x
13x
14x
15x
16x
17x
18x
19x
20x
21x
22x
231
241
251
261
271
281
291
301
311
321
331
341
351
361
371
381
391
401
411
421
431
441
451
461
471
481
491
501
511
520
530

  1. Step 13 · Clock low. The master is open: it follows `d`, which rose at 10. The slave is shut.
  2. Step 23 · Rising edge at 20. The master shuts with `d` = 1 and the slave opens: `q` becomes 1 three gate delays after the edge.
  3. Step 33 · d falls at 30, clock high. The master is shut, so the change cannot pass. `q` stays 1.
  4. Step 45 · Clock low again. The master follows `d` to 0, but the slave is shut: `q` still 1.
  5. Step 52 · Rising edge at 50. The slave shows the master's 0. This time `q` falls two gate delays after the edge.

The master-slave preset: two gated D latches, the master enabled by clk', the slave by clk. Unit delays.

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.

Edged at the edgeq changes atClock-to-Q
10 ns113 ns3 ns (q rises)
30 ns032 ns2 ns (q falls)
50 ns0no change—
70 ns173 ns3 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
  1. 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.

  2. 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.

  3. 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?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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

Read the waveformA rising-edge D flip-flop starts with q = 0. The clock rises at the end of every cycle. What is q in the last cycle?
A rising-edge D flip-flop
01234clkd
Scroll horizontally ↔

The edge at the end of each cycle samples that cycle's d.

ConceptWhich latch of a rising-edge master-slave flop is open while the clock is low?
ConceptThe master is enabled by clk and the slave by clk'. What does the pair do?
ConceptWhy does an enabled flip-flop need no gate on its clock?
Predict the outputThe RTL flop gets one edge with en = 1 and d = 1. Then clr_n pulses low between edges. Then en = 0 and one more edge. Predict the exact line.
`timescale 1ns/1ns
module tb;
  logic clk = 0, clr_n = 1, en = 1, d = 1, q;
  always_ff @(posedge clk or negedge clr_n)
    if (!clr_n)  q <= 1'b0;
    else if (en) q <= d;
  initial begin
    #5 clk = 1; #5 clk = 0;   $write("%b", q);
    #1 clr_n = 0; #1 clr_n = 1; $write("%b", q);
    en = 0;
    #5 clk = 1; #5 clk = 0;   $display("%b", q);
  end
endmodule

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.

Practice

Report a problem with this page