106 Chapter Four
The type integer encompasses the minimum range -2,147,483,647 to
2,147,483,647. In the Standard package (a designer should never redefine
any of the types used in the Standard package; this can result in incom-
patible VHDL, because of type mismatches), there is a subtype called NAT-
URALwhose range is from 0 to 2,147,483,647. This subtype is defined as
shown here:
TYPE INTEGER IS -2,147,483,647 TO 2,147,483,647;
SUBTYPE NATURAL IS INTEGER RANGE 0 TO 2,147,483,647;
After the keyword SUBTYPEis the name of the new subtype being created.
The keyword ISis followed by the base type of the subtype. In this exam-
ple, the base type is INTEGER. An optional constraint on the base type is
also specified.
So why would a designer want to create a subtype? There are two main
reasons for doing so:
To add constraints for selected signal assignment statements or
case statements.
To create a resolved subtype. (Resolved types are discussed along
with resolution functions in Chapter 5.)
When a subtype of the base type is used, the range of the base type can
be constrained to be what is needed for a particular operation. Any functions
that work with the base type also work with the subtype.
Subtypes and base types also allow assignment between the two types.
A subtype can always be assigned to the base type because the range of
the subtype is always less than or equal to the range of the base type. The
base type may or may not be able to be assigned to the subtype, depending
on the value of the object of the base type. If the value is within the value
of the subtype, then the assignment succeeds; otherwise, a range constraint
error results.
A typical example where a subtype is useful is adding a constraint to
a numeric base type. In the previous example, the NATURALsubtype con-
strained the integer base type to the positive values and zero. But what
if this range is still too large? The constraint specified can be a user-
defined expression that matches the type of the base type. In the following
example, an 8-bit multiplexer is modeled with a much smaller constraint
on the integer type:
PACKAGE mux_types IS
SUBTYPE eightval IS INTEGER RANGE 0 TO 7; --line 1
END mux_types;