Constant
Formal Definition
Constant is an object whose value
cannot be changed once defined for the design. Constants may
be explicitly declared or they may be sub-elements of explicitly
declared constants, or interface constants. Constants declared in
packages may also be deferred constants.
Simplified Syntax
constant constant_name :
type := value;
Description
A constant is an object
whose value may never be changed during the simulation process.
The constant declaration
contains one or more identifiers, a subtype indication and an
expression which specifies the value of the constant declared in the
particular statement. The identifiers specify names of the constants.
Each name appearing in the identifier list creates a separate object.
The object type in the constant declaration can be of scalar or
composite type and it can be constrained. A constant
cannot be of the file or access type. If a constant
is an array or a record then none of its elements can be of the file
or access type.
The expression used in the constant declaration
must refer to a value of the same type as specified for the constant
(Example 1).
If a constant is declared an
array other than string, bit_vector or std_logic_vector, then the
value for the constant must
be specified using aggregates
(Example 2).
A constant declared in a
package can be deferred, i.e. it can be declared without specifying
its value, which is given later on, in the package body (Example 3).
Constants improve the
clarity and readability of a project. Moreover, they simplify
incorporating changes in the project. For example, if a design
contains a bus with a fixed width, a constant
representing the number of bits in the bus can be used. When the
width of the bus is to be changed, it is sufficient to alter the constant
declaration only.
The visibility of constants
depends on the place of their declaration. The constants
defined in the package can be used by several design units. The constant
declaration in the design entity is seen by all the
statements of the architecture bodies of this entity. The constants
defined in the declaration part of the design unit is seen in
all bodies related to this design, including the process
statement. The constant
defined in the process can
only be used in this process.
Examples
Example 1
type WeekDay is (Mon,Tue,Wed,Thu,Fri,Sat,Sun);
constant StartDay : WeekDay
:= Sat;
constant LogicalGND : Bit := '0';
constant BusWidth,
QueueLength : Integer := 16;
constant CLKPeriod : Time :=
15 ns;
constant MaxSimTime : Time
:= 200 * CLKPeriod;
Each of the six constants above is of a scalar type. Both BusWidth
and QueueLength are expected to be integer numbers of the same value,
therefore they were specified using one declaration. Note that you
can either explicitly specify the constant's value or using an
expression based on other constants (see the MaxSimTime constant).
Example 2
type NumericCodeType is array
(7 downto 0) of Integer range
0 to 9;
constant EntryCode :
NumericCodeType := (2,6,4,8,0,0,1,3);
constant DataBusReset:
Std_Logic_Vector(7 downto 0)
:= "00000000";
Both constants are of complex types, but DataBusReset is of the
Std_Logic_Vector, thus it can be assigned its value directly.
EntryCode is also a one-dimensional array, but its elements are
integers so the value for the constant must be specified using an
aggregate numeric code type (2,6,4,8...).
Example 3
package Timing is
constant Reset : Std_Logic;
end package Timing;
package body
Timing is
constant Reset:
Std_Logic := '0';
end package body Timing;
Note that the Reset constant is declared in the package without a
concrete value assigned to it because the complete declaration of
this constant is given in the package body.
Important Notes
-
By definition, a constant may not be assigned any values by the
simulation process.
-
Use constants as often as possible as they create more readable and
maintainable code.
-
Use constants to define data parameters and lookup tables, which may
substitute function calls the simulation time of such lookups is
significantly shorter than that of function calls.
|