Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 37 ยท Entities and Architectures

Generics and Constants

Make VHDL designs reusable with generics and constants: width parameters, default values, generic map, derived constants, ceil log2 and assertions.

Hardware designers rarely build only one 8-bit register or one 16-entry memory. Instead they write a design once and reuse it with different sizes. VHDL provides generics for this: parameters of an entity that are fixed when the design is built (elaborated). Together with constants, they let you describe a family of circuits with one source file.

Declaring generics

Generics go before the ports in the entity:

VHDL
entity register_n is
  generic (
    width : positive := 8;          -- default 8 bits
    reset_value : natural := 0
  );
  port (
    clk   : in  std_logic;
    reset : in  std_logic;
    d     : in  std_logic_vector(width - 1 downto 0);
    q     : out std_logic_vector(width - 1 downto 0)
  );
end entity register_n;

positive and natural are standard integer subtypes: positive is 1 to integer'high and natural is 0 to integer'high. Using them documents intent and gives automatic range checks.

Inside the architecture, a generic acts like a constant:

VHDL
architecture rtl of register_n is
begin
  process (clk)
  begin
    if rising_edge(clk) then
      if reset = '1' then
        q <= std_logic_vector(to_unsigned(reset_value, width));
      else
        q <= d;
      end if;
    end if;
  end process;
end architecture rtl;

(to_unsigned comes from ieee.numeric_std, covered in Numeric Arithmetic with numeric_std.)

Generic map

When you instantiate the design you give values with generic map:

VHDL
u_byte : entity work.register_n
  generic map (width => 8)
  port map (clk => clk, reset => reset, d => d_byte, q => q_byte);

u_word : entity work.register_n
  generic map (width => 32, reset_value => 255)
  port map (clk => clk, reset => reset, d => d_word, q => q_word);

The generic values are resolved at elaboration. In hardware, there is no "variable width": each instance becomes its own fixed-width circuit. Generics can be integers, booleans, strings, enumerations, and (in VHDL-2008) even types and subprograms.

Constants

A constant is a named value declared in an architecture, process or package:

VHDL
constant clk_freq_hz : positive := 50_000_000;
constant baud        : positive := 115_200;
constant divisor     : positive := clk_freq_hz / baud;   -- 434 (integer division)

Constants may be computed from generics and from other constants. The tool evaluates them at elaboration time, so they cost no hardware. Using a name instead of a magic number such as 434 keeps the design readable, and changing one value updates everything.

Computing widths: log2

A frequent need is "how many bits do I need to count to N?". VHDL-2008's ieee.math_real provides ceil and log2, so a helper function is common:

VHDL
function clog2(n : positive) return natural is
  variable bits : natural := 0;
  variable v    : natural := 1;
begin
  while v < n loop
    v    := v * 2;
    bits := bits + 1;
  end loop;
  return bits;
end function clog2;

Used like constant addr_bits : natural := clog2(depth);. Because it uses only integers, it is synthesizable when called with constant arguments.

Run the same computation in C to see the values for common depths. This is a stand-in for the VHDL function above.

C Stand-in (C): clog2 for address widths

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.

Parameterizing a design in C

Generics are similar to compile-time parameters. This stand-in uses a macro-like struct to model one register "instance" per width, applying reset_value truncated to the width, exactly the kind of check the to_unsigned call does.

C Stand-in (C): one design, several widths

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.

Checking generics with assertions

Bad generic values should stop the build early. Put an assert in the entity or architecture, as shown earlier in Entity Declarations:

VHDL
assert width >= 2 report "width must be at least 2" severity failure;

Assertions on constants are evaluated at elaboration, so the error appears before simulation starts.

Give generics sensible defaults. A design that elaborates without any generic map is easier to try, and the defaults document the common case.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. When is a generic's value fixed?

    Generics become constants of the instance during elaboration; there is no run-time variable width in hardware.
  2. 2. How do you pass a generic value to an instance?

    generic map (width => 16) supplies generics; port map connects ports.
  3. 3. How many address bits does a memory with 100 locations need (smallest n with 2^n >= 100)?

    2^6 = 64 is too small, 2^7 = 128 is enough, so 7 bits.
  4. 4. Why use positive for a width generic?

    positive is an integer subtype starting at 1, so a width of 0 is rejected automatically.

Summary

  • Generics parameterize an entity; set them with generic map; give them defaults.
  • Constants name values and can be computed from generics at elaboration time.
  • Use positive and natural for sizes; write a clog2 helper for address widths.
  • Use assert to reject invalid generic values early.
  • Each instance becomes fixed hardware; width is not a run-time variable.

Next: Structural Design and Components