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.
- Cycle 2
The clock rises at 10 ns. Both store the 1 on
d. - Cycle 3
dfalls at 13 ns while the clock is high. The latch passes it at once; the flip-flop ignores it. - Cycle 6
With the clock low, the latch is shut too: the fall of
dat 25 ns changes nothing. - Cycle 8
Clock high again:
drises 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.
- I/O
- Control / FSM
- Datapath
- Storage
- Data
- Control
- Reset
- en = 1
One of S and R is 1, picked by
d: the latch is set or reset tod. - 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.
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
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.
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.
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.
Synthesize the design to measure its cells and inspect the mapped gates.
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 0Original 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
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_latchmakes a latch on purpose; an output unassigned on some path ofalways_combmakes one by accident.