Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 37 ยท Data Types and Signals

Subtypes, Enumerations and Records

Define your own VHDL types: subtypes with constraints, enumerations for states and opcodes, records for grouped signals, type attributes and encoding choices.

Built-in types are only the start. Good VHDL names things the way the design thinks about them: a state_t with named states, a byte_t for eight bits, a bus_t that groups an address, data and a valid flag. This lesson covers three tools: subtypes, enumerations and records.

Subtypes

A subtype is a type with a constraint. It is not a new type, so values of the base type and the subtype are compatible, but the subtype can restrict a range, fix an array size, or just give a meaningful name.

VHDL
subtype byte_t   is std_logic_vector(7 downto 0);
subtype addr_t   is unsigned(11 downto 0);
subtype digit_t  is integer range 0 to 9;

signal tx_data : byte_t;
signal pc      : addr_t;
signal ones    : digit_t;

If you later change the byte width in one place, everything using byte_t follows. Range violations of integer subtypes are caught in simulation.

Enumeration types

An enumeration is a list of named values. The compiler (and, for hardware, the synthesizer) picks the codes. Enumerations make state machines and instruction sets readable:

VHDL
type state_t  is (idle, load, run, done);
type color_t  is (red, green, blue);
type opcode_t is (op_add, op_sub, op_and, op_or);

signal state : state_t := idle;

Useful features:

  • Comparison and ordering: idle < load, and state = run.
  • Attributes: state_t'left (is idle), state_t'right (done), state_t'succ(idle) (load), state_t'pos(run) (2), state_t'val(3) (done).
  • They work in case statements, where the compiler checks that all values are covered.

You cannot use std_logic arithmetic with enumerations, and an enumeration is not automatically a bit vector. The synthesis tool chooses an encoding: binary, one-hot, gray, depending on options and attributes. Chapter 5 returns to this.

The stand-in below handles a C enum the same way: named values, a succ operation that stops at the last item, and a conversion to names.

C Stand-in (C): an enumeration with succ and pos

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.

Record types

A record groups fields of different types under one name, like a C struct:

VHDL
type bus_t is record
  addr  : unsigned(15 downto 0);
  data  : std_logic_vector(7 downto 0);
  valid : std_logic;
  write : std_logic;
end record bus_t;

signal cpu_bus : bus_t;

cpu_bus.addr  <= to_unsigned(16#4000#, 16);
cpu_bus.data  <= x"5A";
cpu_bus.valid <= '1';

Records are excellent for buses with many related wires, since you pass one signal instead of ten. They can contain other records and arrays, and records can be used as signals, constants and function parameters. Aggregates can create a record value:

VHDL
constant idle_bus : bus_t := (
  addr  => (others => '0'),
  data  => (others => '0'),
  valid => '0',
  write => '0'
);

cpu_bus <= idle_bus;

Records cannot be used as port types in the top-level entity of an FPGA design with many tools, because the pins need individual names. Use them freely inside the design, and flatten them at the boundary. VHDL-2019 adds interfaces for grouping ports, but tool support is still limited.

Records as a struct in C

Here is the bus_t record as a C struct with the idle_bus constant, a "write" operation that fills in the fields, and a check of the valid flag:

C Stand-in (C): a bus record

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.

Choosing the right kind of type

Need Use
A bus with a fixed width subtype of std_logic_vector or unsigned
States, opcodes, modes enumeration
Counts and indexes integer subtype
Several related wires record
Memory or register file array type

Your own types

Subtype Name and constrain
Enumeration States and opcodes
Record Group related fields
Array Memories and files

Put shared types in a package.

1 / 2

Shared types belong in a package, as in Libraries and Packages, so every module sees the same definition.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the difference between a subtype and a new type?

    A subtype adds a constraint to an existing type and remains compatible with it.
  2. 2. Given type state_t is (idle, load, run, done);, what is state_t'pos(run)?

    Positions start at 0, so idle=0, load=1, run=2.
  3. 3. How do you access the valid field of a record signal cpu_bus?

    Record fields are selected with a dot.
  4. 4. Who chooses the binary encoding of an enumeration's values in hardware?

    The synthesizer picks an encoding such as binary or one-hot unless you direct it.

Summary

  • Subtypes give names and constraints to existing types.
  • Enumerations make states and opcodes readable; use attributes like 'pos, 'succ, 'val.
  • Records group fields; build values with aggregates and access fields with a dot.
  • Put shared types in packages; flatten records at the chip boundary.

Next: Concurrent Assignments