Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 37 ยท Synthesis and FPGA

Inferred Hardware: Registers, Latches and Memories

See how VHDL code styles infer flip-flops, latches, multiplexers, shift registers and block RAM, and how to avoid accidental latches with complete assignments.

You never instantiate a flip-flop in everyday VHDL. You write a process in a certain style, and the synthesis tool infers the hardware that matches it. Learn the styles that give each kind of hardware and you control exactly what the tool builds. Learn the style that builds a latch by accident and you can avoid the most common synthesis bug.

As elsewhere on this track, VHDL listings are read-only; the C programs model the behavior of the inferred hardware.

Combinational logic: assign every signal on every path

A combinational process must give each of its outputs a value for every possible input. Then no memory is needed.

VHDL
process (sel, a, b)
begin
  if sel = '1' then y <= a;
  else              y <= b;
  end if;
end process;

This infers a 2-to-1 multiplexer. The else branch is what guarantees that y always has a value.

The accidental latch

VHDL
process (sel, a)
begin
  if sel = '1' then
    y <= a;
  end if;                -- no else!
end process;

When sel = '0', the code says nothing about y, and VHDL's rule is that a signal keeps its value if not assigned. Hardware must therefore remember the old y, and the tool builds a latch: a level-sensitive memory element that is transparent while sel = '1' and holds otherwise. Latches are fragile for three reasons: they break the clean clock-edge timing model, timing tools handle them poorly, and FPGAs implement them awkwardly. A warning such as "inferred latch for signal y" in a synthesis report is almost always a bug.

The same happens with an incomplete case:

VHDL
case state is
  when A => y <= '1';
  when B => y <= '0';
  -- other states: y not assigned -> latch
end case;

Three cures

  1. Give a default at the top of the process (y <= '0';) and override it in the branches.
  2. Add the missing else or when others.
  3. Use a clocked process on purpose when you really want memory.
C C stand-in: a latch remembers, a mux does not

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Look at the rows where sel = 0: the multiplexer outputs 0, but the latch still shows whatever a was the last time sel was 1.

Registers: the clock-edge process

VHDL
process (clk)
begin
  if rising_edge(clk) then
    q <= d;
  end if;
end process;

This is a D flip-flop: q copies d on the rising edge, and holds otherwise. The "missing else" here is intentional and harmless, because the flip-flop is meant to hold. Add reset, enable and more:

VHDL
process (clk)
begin
  if rising_edge(clk) then
    if rst = '1' then       q <= (others => '0');   -- synchronous reset
    elsif en = '1' then     q <= d;                 -- clock enable
    end if;
  end if;
end process;

The tool maps these to flip-flops with synchronous reset and clock-enable inputs, which FPGA registers have built in, so they cost no extra LUTs. An asynchronous reset is written outside the clock test:

VHDL
process (clk, rst)
begin
  if rst = '1' then
    q <= (others => '0');
  elsif rising_edge(clk) then
    q <= d;
  end if;
end process;

Prefer synchronous reset unless the device or the design requires otherwise (more on the choice in Timing and Constraints).

Use rising_edge(clk), not clk'event and clk = '1'. The older form also fires when clk goes from 'X' to '1', and the function form says exactly what you mean.

Shift registers and counters

A signal assigned in a clocked process becomes a flip-flop. Several assignments in the same process all see old values, so this forms a shift register automatically:

VHDL
process (clk)
begin
  if rising_edge(clk) then
    s1 <= din;
    s2 <= s1;
    s3 <= s2;       -- s3 is din delayed by three clock cycles
  end if;
end process;
C C stand-in: three flip-flops in series (signals update together)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

If you used variables instead of signals in that process, the assignments would happen immediately in order, and s3 would equal din with no delay. Variables are fine for local temporaries, but remember that signal assignments are what describe flip-flop behavior.

Memories

A memory is an array of words. Written in the right style, it maps to block RAM:

VHDL
type ram_t is array (0 to 255) of std_logic_vector(7 downto 0);
signal ram : ram_t;
signal rdata : std_logic_vector(7 downto 0);
...
process (clk)
begin
  if rising_edge(clk) then
    if we = '1' then
      ram(to_integer(unsigned(addr))) <= wdata;
    end if;
    rdata <= ram(to_integer(unsigned(addr)));   -- synchronous read
  end if;
end process;

The synchronous read (read inside the clocked process) is what lets the tool use dedicated block RAM. An asynchronous read (rdata <= ram(...) outside the clock) forces the memory into LUTs ("distributed RAM"), which is fine for small arrays and wasteful for large ones. Also note: when you read and write the same address in the same cycle, the value read is the old data on many devices ("read-first"). Check your device documentation.

Other inferences

Code style Inferred hardware
a + b on unsigned adder with carry chain
a * b (wide) DSP multiplier
if/elsif chain priority multiplexer
case parallel multiplexer / decoder
y <= a when sel = '1' else 'Z'; tri-state buffer (pins only)
Process with wait until at top clocked register (some tools)

Tri-state signals ('Z') should appear only at the chip's pins. Inside an FPGA, multiplexers replace internal tri-state buses.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does an if without an else, such as 'if sel = 1 then y <= a; end if;', in a combinational process infer a latch?

    An unassigned signal keeps its value, which requires memory; without a clock, that memory is a latch.
  2. 2. Which style prevents inferred latches?

    A default guarantees every signal has a value on every path.
  3. 3. In a clocked process, s1 <= din; s2 <= s1; creates

    Both right-hand sides use old values, so s2 follows s1 one cycle later.
  4. 4. What read style lets a tool infer block RAM?

    Block RAM has a registered read port, matching the synchronous style.

Summary

  • Hardware is inferred from coding style: clocked processes give flip-flops, complete combinational processes give logic.
  • A signal not assigned on every path of a combinational process becomes a latch; fix with defaults.
  • Synchronous reset and enable map to flip-flop control pins at no LUT cost.
  • Several signal assignments in one clocked process form a shift register.
  • Synchronous-read arrays become block RAM; keep tri-state at the pins.

Next: Timing and Constraints