The D Latch

Build a D latch from an SR latch, watch it pass its input while enabled, and tell it from a flip-flop.

You Will Be Able To

  • Build a gated D latch from an SR latch and two AND gates.
  • Explain transparency and level sensitivity on a waveform.
  • Write a latch with always_latch and recognize an accidental latch.
  • Tell a latch from a flip-flop on the same input.
Lesson
G5.5
Time
20 min
Runs on
Your browser
Check
5 questions

A designer wrote a register with if (clk) in place of an edge, and the tests started to see data one cycle early. The block was a latch. While the clock was high it passed every change of its input straight through.

A Latch and a Flip-Flop on the Same d and ClkOne column per 4 ns, from the lab below. clk is high from 10 to 20 ns and from 30 to 40 ns.
01234567891011clkdq_latchq_floplatch follows dagain
Scroll horizontally ↔
  1. Cycle 2

    The clock rises at 10 ns. Both store the 1 on d.

  2. Cycle 3

    d falls at 13 ns while the clock is high. The latch passes it at once; the flip-flop ignores it.

  3. Cycle 6

    With the clock low, the latch is shut too: the fall of d at 25 ns changes nothing.

  4. Cycle 8

    Clock high again: d rises at 35 ns and the latch passes it. The flop keeps the 0 it took at 30 ns.

Measured: the lines the lab prints.

From the SR Latch to the D Latch

An SR latch needs two inputs and has a forbidden pair. A D latch needs one data input d and an enable en. Two AND gates turn them into S and R.

The Gated D LatchArrows show signals. S = d AND en, R = d' AND en, so S and R are never both 1.
  • I/O
  • Control / FSM
  • Datapath
  • Storage
  • Data
  • Control
  • Reset
The gated D latchArrows show signals. S = d AND en, R = d' AND en, so S and R are never both 1. io d data control en enable datapath AND S = d · en datapath AND R = d' · en storage SR latch two NOR gates io q stored bit invertedSR
  1. io ddata
    • AND
    • inverted → AND
  2. control enenable
    • AND
    • AND
  3. datapath ANDS = d · en
    • S → SR latch
  4. datapath ANDR = d' · en
    • R → SR latch
  5. storage SR latchtwo NOR gates
    • q
  6. io qstored bit
  1. en = 1

    One of S and R is 1, picked by d: the latch is set or reset to d.

  2. en = 0

    Both AND gates give 0. The SR latch sees hold and keeps q.

Read the diagram as text
  • d: data
  • en: enable
  • AND: S = d · en
  • AND: R = d' · en
  • SR latch: two NOR gates
  • q: stored bit
  • d → AND
  • d → AND: inverted
  • en → AND
  • en → AND
  • AND → SR latch: S
  • AND → SR latch: R
  • SR latch → q

The inverter on d makes S and R complements while en is 1, so the forbidden pair cannot occur.

A Gated D Latch
A Gated D Latchg0 · NOTl_nd=1g1 · ANDl_s=0g2 · ANDl_r=0g3 · NORq=1g4 · NORl_qn=0d=0en=0
d=0 en=0
g0 · NOTd=0 → l_nd=1
g1 · ANDd=0, en=0 → l_s=0
g2 · ANDl_nd=1, en=0 → l_r=0
g3 · NORl_r=0, l_qn=0 → q=1
g4 · NORl_s=0, q=1 → l_qn=0
Read the connections
GateKindInputsOutput
g0NOTdl_nd
g1ANDd, enl_s
g2ANDl_nd, enl_r
g3NORl_r, l_qnq
g4NORl_s, ql_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 25. Settled after 25 gate delays from t = 0.

Output values at each gate delay012345678910111213141516171819202122232425qxxx00000000111111111111111
Scroll horizontally ↔
Every computed frame
Timeq
0x
1x
2x
30
40
50
60
70
80
90
100
111
121
131
141
151
161
171
181
191
201
211
221
231
241
251

  1. Step 3 · Enabled. With `en` = 1 the latch is *transparent*: `q` takes the value of `d`, here 0.
  2. Step 11 · d rises at 8. The set path opens and `q` follows to 1, three gate delays later.
  3. Step 17 · en falls at 16. Both AND gates now give 0: the SR latch inside sees hold.
  4. Step 25 · d falls at 24. `q` keeps its 1. The latch is *opaque* while `en` = 0.

The d-latch preset: d and d' are gated by en into the S and R of a NOR latch. Unit delays.

Transparent While Enabled

While en = 1 the latch is transparent: q copies d and follows every change. While en = 0 it is opaque: q holds the value d had when en fell. Because it reacts to the level of en, not to an edge, a latch is level-sensitive.

A flip-flop is different: it looks at d only at the clock edge (G5.6). The hook trace shows both on the same signals.

Latches in Code

SystemVerilog has a block for latches, always_latch. Tools check that it really describes a latch.

A latch on purpose

always_latch
  if (en) q = d;

Synthesized on this site: one latch cell, $_DLATCH_P_, with the warning "1 latch cell(s) inferred".

A latch by accident

always_comb
  if (sel) y = a;   // no else

G4.1 measured it: Synthesize refuses the block with a logic loop, and Lint reports a latch.

Most RTL designs use flip-flops only. A latch needs its own timing analysis and is easy to create by accident. So a latch in a report is usually a bug to fix, unless it was planned, as in G5.10.

Asked in interviews
  1. What is the difference between a latch and a flip-flop?

    A latch is level-sensitive: it passes d to q while its enable is active and holds when it is not. A flip-flop is edge-triggered: it samples d only at a clock edge, so changes between edges never reach q.

  2. How does an accidental latch appear in RTL?

    A combinational block that does not assign an output on every path. The output must keep its old value on that path, so synthesis builds a latch. A default assignment at the top of the block or a complete if or case removes it.

  3. Why is the D latch better than the SR latch?

    It has one data input, and the gating makes S and R complements while enabled. So the forbidden S = R = 1 case cannot happen.

Try It

Both outputs are x until the first rising edge at 10 ns: neither has stored anything yet.

At 16 ns the clock is high and the latch shows the 0 that d took at 13 ns. The flip-flop still shows the 1 it took at 10 ns.

At 28 ns the clock is low: d fell at 25 ns, and neither output moved. At 36 ns the latch passes the new 1; the flop keeps the 0 from the edge at 30 ns.

Press Synthesize: one latch cell and one flip-flop cell. Then change always_latch to always_comb and press it again.

RTLIcarus Verilog · in your browser · Lint: Verilator service · Synthesize: Yosys service
Edit the design, then Run, Lint or Synthesize
Expected: t clk d q_latch q_flop 4 0 1 x x 8 0 1 x x 12 1 1 1 1 16 1 0 0 1 20 0 1 1 1 24 0 1 1 1 28 0 0 1 1 32 1 0 0 0 36 1 1 1 0 40 0 1 1 0 44 0 1 1 0 48 0 1 1 0

Original design · 2 cells · 1 flip-flops · 1 latches · 0 logic levels. Synthesis uses Yosys; counts depend on the toolchain.

Read included file: tb.sv
// tb.sv: d changes while clk is high and while it is low; one line every 4 ns (read-only).
`timescale 1ns/1ns
module tb;
  logic clk = 0, d = 0;
  logic q_latch, q_flop;
  latch_and_flop dut (.clk(clk), .d(d), .q_latch(q_latch), .q_flop(q_flop));
  always #10 clk = ~clk;                          // rising edges at 10, 30, 50 ns
  initial begin
    $display(" t  clk d  q_latch q_flop");
    #3  d = 1;
    #10 d = 0;                                    // t=13: clk is 1, the latch is open
    #4  d = 1;                                    // t=17
    #8  d = 0;                                    // t=25: clk is 0, the latch is shut
    #10 d = 1;                                    // t=35: clk is 1
    #13 $finish;
  end
  initial for (int t = 2; t <= 46; t += 4) #4 $display("%2d   %b  %b    %b       %b", $time, clk, d, q_latch, q_flop);
endmodule

Check Yourself

ConceptA D latch has en = 1. Its input d changes three times. How many times does q change?
ConceptWhy can the gated D latch not reach the forbidden S = R = 1 state?
Read the waveformA latch is enabled while en is 1. Which value does q hold in the last cycle?
A D latch
01234end
Scroll horizontally ↔

en falls at the start of cycle 2.

ConceptA Synthesize report shows one $_DLATCH_P_ cell that nobody planned. What is the most likely cause?
Predict the outputThe lab design gets a short high pulse on clk. Inside it d rises and falls again. Predict the exact line.
`timescale 1ns/1ns
module tb;
  logic clk = 0, d = 0, q_latch, q_flop;
  always_latch if (clk) q_latch = d;
  always_ff @(posedge clk) q_flop <= d;
  initial begin
    #5 clk = 1;
    #1 d = 1;
    #1 d = 0;
    #1 clk = 0;
    #1 $display("q_latch=%b q_flop=%b", q_latch, q_flop);
  end
endmodule

Common Mistakes

Wrong Call a latch a flip-flop.

A latch passes its input for the whole enabled phase. A flip-flop takes it at one edge. In the lab the latch followed d at t = 16; the flop did not.

Fix Latch: level-sensitive. Flip-flop: edge-triggered.

Wrong Leave a branch unassigned in a combinational block.

The output must then keep its old value, and the only way to hold a value without a clock is a latch.

Fix Assign every output on every path, or give it a default at the top of the block (G4.1).

Wrong Drive a latch enable from glitchy logic.

A latch is open for as long as its enable is 1. A glitch on the enable opens it for that moment and lets data in.

Fix Drive enables from a clock or a flip-flop, never from raw combinational logic.

Wrong Expect the latch to be a cheaper flip-flop with the same timing.

Data that arrives while the latch is open goes straight through, so the timing of the next stage changes.

Fix Use latches on purpose, in two-phase designs (G5.10), and flip-flops by default.

Remember

  • A D latch is an SR latch gated by en: S = d · en, R = d' · en, so the forbidden pair cannot occur.
  • While enabled it is transparent and q follows d; while disabled it holds.
  • A latch is level-sensitive; a flip-flop samples at an edge.
  • always_latch makes a latch on purpose; an output unassigned on some path of always_comb makes one by accident.

Practice

Report a problem with this page