Door latch

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.

Transparent D latch schematic and waveform

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

EDQ
111
100
01Latched D value
00Latched 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“.

[Synth 8-614] signal 'd' is read in the process but is not in the sensitivity list. [Synth 8-327] inferring latch for variable 'q_reg'

And similarly, Intel Quartus will say “Info (10041): Inferred latch for XX at YY“, as shown in the image below.

Info (10041): Inferred latch for "q" at top.vhd(22)

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.

Timing diagram for latch paths versus flip-flop in FPGAs

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.

Register as latch in Vivado Resource Utilization report

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.

Similar Posts

11 Comments

  1. Yes, I have used it frequently for flight test vehicles. Some situations require a permanent (reset on power-up) or temporary (special reset sequence) latch. But, in the case described above it was used in a design of GPS (Global Positioning System) and EVS (Enhanced Vision System). Certain times the unwanted thing behavior can be used to advantage.

  2. Hi Jonas.
    Might sound silly, but as a hardware developer I have many times needed to let an Address but go through a Latch to let it through, and “lock” it when the latch was disabled. A Flip-Flop cannot do that, It will not pass through before clocked.

  3. Latches are handy in case you need to save power and/or area, since they require less transistors to be implemented.
    The propagation time is also lower then that of a FF.

  4. hello jonas,
    we checked and this code indeed create a FF!!
    We checked the code in quartus16.1 and quartus18.1 and we saw that the synthesis tool does produce FF !! why? thanks

    process(clk)
    begin
      if clk = '1' then   
        q <= d; 
      end if;
    end process;
    
    1. Dear Vizel,

      My guess: your “clk” signal is indeed known to the synthesis as a “real clock”, either because its defined as a clock or you have generated it via a pll, and in that case, the router trys to make use of existing clock inputs on registers / LUTs, which will turn your “if clk=’1′” into the same as an rising_edge clause in the if statement. I think the synthesis is assuming, that you originally wanted a clock triggered / synchronous process.

      With regards, Marek

Leave a Reply

Your email address will not be published. Required fields are marked *