Constants

Constants are objects that are assigned a value once, when declared, and do not change their value during simulation. Constants are useful for creating more readable design descriptions, and they make it easier to change the design at a later time. The following code fragment provides a few examples of constant declarations:

 

architecture sample1 of consts is

    constant SRAM: bit_vector(15 downto 0) := X"F0F0";

    constant PORT: string  := "This is a string";

    constant error_flag: boolean := True;

begin

    . . .

    process( . . .)

        constant CountLimit: integer := 205;

    begin

        . . .

    end process;

 

end arch1;

 

Constant declarations can be located in any declaration area in your design description. If you want to create constants that are global to your design description, then you will place the constant declarations into external packages. If a constant will be used only within one segment of your design, you can place the constant declaration within the architecture, block, process or subprogram that requires it.

 

Literals

Explicit data values that are assigned to objects or used within expressions are called literals. Literals represent specific values, but they do not always have an explicit type. (For example, the literal '1' could represent either a bit data type or a character.) Literals do, however, fall into a few general categories.

 

Character literals

Character literals are 1-character ASCII values that are enclosed in single-quotes, such as the values '1', 'Z', '$' and ':'. The data type of the object being assigned one of these values (or the type implied by the expression in which the value is being used) will dictate whether a given character literal is valid. The literal value '$', for example, is a valid literal when assigned to a character type object, but it is not valid when assigned to a std_logic or bit data type.

 

String literals

String literals are collections of one or more ASCII characters enclosed in double-quote characters. String literals may contain any combination of ASCII characters, and they may be assigned to appropriately sized arrays of single-character data types (such as bit_vector or std_logic_vector) or to objects of the built-in type string.

 

Bit string literals

Bit string literals are special forms of string literals that are used to represent binary, octal, or hexadecimal numeric data values.

When representing a binary number, a bit string literal must be preceded by the special character 'B', and it may contain only the characters '0' and '1'. For example, to represent a decimal value of 36 using a binary format bit string literal, you would write B"100100".

 

When representing an octal number, the bit string literal must include only the characters '0' through '7', and it must be preceded by the special character 'O', as in O"446".

 

When representing a hexadecimal value, the bit string literal must be preceded by the special character 'X', and it may include only the characters '0' through '9' and the characters 'A' through 'F', as in X"B295". (Lower-case characters are also allowed, so 'a' through 'f' are also valid.)

 

The underscore character '_' may also be used in bit string literals as needed to improve readability. The following are some examples of bit string literals representing a variety of numeric values:

 

B"0111_1101"  (decimal value 253)

O"654"    (decimal value 428)

O"146_231"  (decimal value 52,377)

X"C300"  (decimal value 49,920)

 

Note: In VHDL standard 1076-1987, bit string literals are only valid for the built-in type bit_vector. In 1076-193, bit string literals can be applied to any string type, including std_logic_vector.

 

Numeric literals

There are two basic forms of numeric literals in VHDL, integer literals and real literals.

 

Integer literals are entered as you would expect, as decimal numbers preceded by an optional negation character ('-'). The range of integers supported is dependent on your particular simulator or synthesis tool, but the VHDL standard does specify a minimum range of -2,147,483,647 to +2,147,483,647 (32 bits of precision, including the sign bit).

 

Real literals are entered using an extended form that requires a decimal point. For large numbers, scientific notation is also allowed using the character 'E', where the number to the left of the 'E' represents the mantissa of the real number, while the number to the right of the 'E' represents the exponent. The following are some examples of real literals:

 

5.0

-12.9

1.6E10

1.2E-20

 

The minimum and maximum values of real numbers are defined by the simulation tool vendor, but they must be at least in the range of -1.0E38 to +1.0E38 (as defined by the standard). Numeric literals may not include commas, but they may include underscore characters ("_") to improve readability, as in:

 

1_276_801   -- integer value 1,276,801

 

Type checking is strict in VHDL, and this includes the use of numeric literals. It is not possible, for example, to assign an integer literal of 9 to an object of type real. (You must instead enter the value as 9.0.)

 

Based literals

Based literals are another form of integer or real values, but they are written in non-decimal form. To specify a based literal, you precede the literal with a base specification (such as 2, 8, or 16) and enclose the non-decimal value with a pair of '#' characters as shown in the examples below:

 

2#10010001#  (integer value 145)

16#FFCC#  (integer value 65,484)

2#101.0#E10  (real value 5,120.0)

 

Physical literals

Physical literals are special types of literals used to represent physical quantities such as time, voltage, current, distance, etc. Physical literals include both a numeric part (expressed as an integer) and a unit specification. Physical types will be described in more detail later in this chapter. The following examples show how physical literals can be expressed:

 

300 ns     (300 nanoseconds)

900 ps     (900 picoseconds)

40 ma     (40 milliamps)