Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 37 ยท Concurrent and Sequential Code

Generate Statements

Create repeated and optional hardware in VHDL with for-generate and if-generate: instance arrays, bus bit-slices, generic-controlled features and case-generate in VHDL-2008.

A for loop inside a process repeats sequential statements. To repeat concurrent hardware, whole instances, assignments or processes, you use a generate statement. Generate statements are evaluated at elaboration time: they decide how many copies of the hardware exist, and they cost no logic themselves.

There are two main kinds: for ... generate (repeat) and if ... generate (include or exclude). VHDL-2008 adds case ... generate.

for ... generate

VHDL
label : for i in 0 to 7 generate
  -- concurrent statements that may use i
end generate label;

The label is required. The loop variable i is a constant inside the body, and the range must be static.

Eight copies of an inverter on a bus:

VHDL
inv_gen : for i in 0 to 7 generate
  y(i) <= not a(i);
end generate inv_gen;

Eight instances of a component, like the ripple-carry adder in Structural Design:

VHDL
gen_fa : for i in 0 to width - 1 generate
  u_fa : entity work.full_adder
    port map (a => a(i), b => b(i), cin => carry(i),
              sum => s(i), cout => carry(i + 1));
end generate gen_fa;

Because width is a generic, the same text produces a 4-bit or a 64-bit adder.

An array of registers, one process per bit-slice, is also valid:

VHDL
regs : for i in data'range generate
  process (clk)
  begin
    if rising_edge(clk) then
      q(i) <= d(i);
    end if;
  end process;
end generate regs;

(It is shorter to write one process over the whole vector, but this shows that processes can be generated too.)

if ... generate

if ... generate includes hardware only when a static condition is true. This is how generics switch features on and off:

VHDL
gen_parity : if with_parity generate
  parity_bit <= xor_reduce(data);
end generate gen_parity;

gen_no_parity : if not with_parity generate
  parity_bit <= '0';
end generate gen_no_parity;

In VHDL-93 there is no else branch; you write two generates with opposite conditions as above. VHDL-2008 adds elsif and else inside generate:

VHDL
g : if kind = 0 generate
  ...
elsif kind = 1 generate
  ...
else generate
  ...
end generate g;

and case ... generate:

VHDL
g : case mode generate
  when 0 => ...
  when 1 => ...
  when others => ...
end generate g;

Special cases: first and last element

A common pattern: a chain where the first or last instance differs. Use if ... generate inside for ... generate to wire the ends:

VHDL
chain : for i in 0 to n - 1 generate
  first : if i = 0 generate
    link(0) <= start;
  end generate first;
  other : if i > 0 generate
    link(i) <= stage_out(i - 1);
  end generate other;
end generate chain;

Labels inside nested generates must also be unique within their scope.

Elaboration-time repetition in C

The C preprocessor and loops can mimic elaboration. This stand-in "elaborates" a ripple chain of a width taken from a macro: it creates the instance list in a loop, prints the netlist it created, and then runs it for a test addition. It shows that the generate loop only creates structure, and all the instances then work concurrently.

C Stand-in (C): elaborating a generate loop

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.

Optional features

This second stand-in models if ... generate with a macro-like flag: whether to build parity hardware is decided before the "simulation" runs, so the disabled branch costs nothing.

C Stand-in (C): if-generate chooses hardware

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.

Use a descriptive label such as gen_fa or gen_parity. Tools show these labels in the hierarchy browser, where gen_fa(3) leads straight to the fourth adder.

for loop versus for-generate

for loop in a process for ... generate
Body contains Sequential statements Concurrent statements, instances, processes
Label Optional Required
Evaluated By the process, each activation Once, at elaboration
Typical use Folding a vector into a number Making many copies of hardware

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a for ... generate statement do?

    Generate statements are expanded at elaboration, giving one copy of the body per iteration.
  2. 2. What is required on a generate statement?

    Every generate statement must be labelled.
  3. 3. How do you build optional hardware controlled by a Boolean generic with_fifo?

    If-generate includes the hardware only when the static condition is true.
  4. 4. Which condition may be used in an if ... generate?

    Generate conditions are evaluated at elaboration, so they must be static.

Summary

  • Generate statements repeat or select concurrent hardware at elaboration time.
  • for ... generate makes copies (instances, assignments, processes); a label is required.
  • if ... generate includes hardware for static conditions; VHDL-2008 adds else, elsif and case ... generate.
  • Pair generate with generics to build width-independent designs.

Next: Delta Cycles and Simulation Time