Contents
Up
Previous
Next
Subtypes
A subtype declaration, unlike a type definition, does not create a new
type but rather restricts the use of an existing type. For example, a
subtype declaration can restrict the range of integers for a memory address
with the declaration
subtype address_type is integer range 0 to 2**32-1;
Note that this can also be done with an integer type definition. The
distinction to be made is that an integer can be assigned to an object
of address_type, but if address_type
was declared with an integer type definition, an integer could not be
assigned to an object of address_type. The difference is that a subtype
is not a new type just a restriction, so the assignment is between objects of
the same base type. However, the integer type definition declares a new
type, so that the assignment would be between objects of different types and
that is not allowed.
The subtype declaration has the following form:
constraint : RANGE range | index-constraint
subtype : [ simple-name(function) ] type [ constraint ]
subtype-declaration : SUBTYPE simple-name IS subtype ";"
This rule includes a new notation, the name(function). It means that
a name may appear, but that name should represent the name of a function.
The word in parentheses specifies that the object should be of
a certain kind or have a certain attribute.
In the section describing
entities and architectures, port and generic clauses contained declarations
for objects of some subtype. In that section the subtype was said to
name the type of the object. More specifically though, it may also specify
a subtype. The declarations we have already seen for objects of the
bit_vector type are actually constrained subtype specifications, not just a
type name. For example in
port (d1,d2 : in bit_vector (width-1 downto 0);
sum : in bit_vector (width-1 downto 0));
we see that bit_vector is the type name (an unconstrained array type)
and the (width-1 downto 0) part is the index constraint of the
subtype specification. The bit_vector was defined to be an unconstrained
array type so that many vectors could be constrained with different ranges,
using the same subtype specification, that still have the same base type. It
is also useful to use a subtype to constrain an array to different lengths
because a subtype specification may appear directly in a declaration and does
not require a new definition.
The function name part can be used to declare a resolved subtype. The
resolved subtype is discussed in the next chapter with resolved signals.