A latch is a logic element that can sample and hold a binary value, much like a flip-flop (register). But unlike a flip-flop, which is edge-triggered, the latch is level-triggered.
To memorize how the digital latch works you can think of the door locking mechanism that it’s named after, shown in the example photo.
While the bolt is retracted, the door will open and anything can pass through. But as soon as we push the bolt into the receptacle, it locks the door.
How a transparent D latch works
A digital latch works much like its analog counterpart. When open, it will let the input value pass through to the output, and when closed, it will lock in whatever value was already being forwarded.
According to the Modeling Latches and Flip-Flops document from Xilinx, the schematic below describes the behavior of a D latch in their FPGAs. The naming convention comes from the data input, often denoted as D.
Observe the values as they change in the logic gate animation. If you pay attention, you will notice that the value on the D input propagates to the Q output only when the E (Enable) input is ‘1’.
|0||1||Latched D value|
|0||0||Latched D value|
We can say that the latch is transparent as long as the enable input is active. It’s as if the latch wasn’t there. That’s why the D latch is often called a transparent latch.
When we assign ‘0’ to the E input, the Q output will stop reacting to changes on D. The value is frozen, or latched, until we set E to ‘1’ again.
The truth table for the transparent latch is comparable to that of a flip-flop without reset. But the latch samples the value on the falling edge of the Enable input. And it lets the input propagate during the high period of the Enable (usually the clock), while the flip-flop doesn’t.
How to infer a latch with VHDL
If you really want a latch, this is how to create one:
process(enable, d) begin if enable = '1' then q <= d; end if; end process;
But most latches are accidentally inferred because there’s something wrong with your VHDL process.
The code below shows a typical accidental latch inference. The designer probably wanted to create an edge-sensitive process, but forgot to use
if clk'event and clk = '1' then or
if rising_edge(clk) then.
process(clk) begin if clk = '1' then q <= d; end if; end process;
When synthesized in Vivado, the code produces the familiar “[Synth 8-327] inferring latch for variable XX” warning, accompanied by “[Synth 8-614] signal XX is read in the process but is not in the sensitivity list“.
And similarly, Intel Quartus will say “Info (10041): Inferred latch for XX at YY“, as shown in the image below.
Another common error that will result in latches is to forget the Else clause in a combinational process. The example below may produce a latch because data_out is not given a value when read_data is ‘0’.
process(read_data, fifo_out) begin if read_data = '1' then data_out <= fifo_out; end if; end process;
The solution is to add an Else clause to the If statement. If we assign something to the output for all possible combinations of the control signals, it won’t generate latches.
What’s the problem with latches?
The synthesis tool will warn you about latches because there’s no reason to have them. I’ve never needed a latch in my career. Please let me know in the comment section if you see any use cases for latches.
Latches essentially do the same thing as flip-flops, but with inferior timing capabilities. Study the illustration shown below to see what I mean.
In the first half of the example, we have a latch whose output goes through combinational logic to a flip-flop.
We don’t know what comes before the latch, and therefore we must assume that its input can change at any time. The only time the latch output is stable is during the low half period of the clock cycle.
The latch gives the place and route tool less flexibility to meet timing.
In the second half of the schematic, the combinational logic is between two flip-flops. Because both flip-flops sample the data input instantaneously, the signal has the full clock period to propagate through the combinational logic.
Furthermore, the FPGA implements both latches and flip-flops using the same configurable register primitives. We can see evidence of this in the resources utilization report from Vivado, shown below.
A latch doesn’t save logic resources in the FPGA because it uses the same primitive as a flip-flop.
While your logic may work well with latches, they are probably not what you want. Latches put strains on the router and offer no added benefit compared to flip-flops.