2.2. Data Types and Objects


VHDL provides a number of basic, or scalar, types, and a means of forming composite types. The scalar types include numbers, physical quantities, and enumerations (including enumerations of characters), and there are a number of standard predefined basic types. The composite types provided are arrays and records. VHDL also provides access types (pointers) and files, although these will not be fully described in this booklet.

A data type can be defined by a type declaration:

Examples of different kinds of type declarations are given in the following sections.

2.2.1. Integer Types

An integer type is a range of integer values within a specified range. The syntax for specifying integer types is:

The expressions that specify the range must of course evaluate to integer numbers. Types declared with the keyword to are called ascending ranges, and those declared with the keyword downto are called descending ranges. The VHDL standard allows an implementation to restrict the range, but requires that it must at least allow the range -2147483647 to +2147483647.

Some examples of integer type declarations:

There is a predefined integer type called integer. The range of this type is implementation defined, though it is guaranteed to include -2147483647 to +2147483647.

2.2.2. Physical Types

A physical type is a numeric type for representing some physical quantity, such as mass, length, time or voltage. The declaration of a physical type includes the specification of a base unit, and possibly a number of secondary units, being multiples of the base unit. The syntax for declaring physical types is:

Some examples of physical type declarations:

The predefined physical type time is important in VHDL, as it is used extensively to specify delays in simulations. Its definition is:

To write a value of some physical type, you write the number followed by the unit. For example:

2.2.3. Floating Point Types

A floating point type is a discrete approximation to the set of real numbers in a specified range. The precision of the approximation is not defined by the VHDL language standard, but must be at least six decimal digits. The range must include at least -1E38 to +1E38. A floating point type is declared using the syntax:

Some examples are:

There is a predefined floating point type called real. The range of this type is implementation defined, though it is guaranteed to includ -1E38 to +1E38.

2.2.4. Enumeration Types

An enumeration type is an ordered set of identifiers or characters. The identifiers and characters within a single enumeration type must be distinct, however they may be reused in several different enumeration types.

The syntax for declaring an enumeration type is:

Some examples are:

There are a number of predefined enumeration types, defined as follows:

Note that type character is an example of an enumeration type containing a mixture of identifiers and characters. Also, the characters '0' and '1' are members of both bit and character . Where '0' or '1' occur in a program, the context will be used to determine which type is being used.

2.2.5. Arrays

An array in VHDL is an indexed collection of elements all of the same type. Arrays may be one-dimensional (with one index) or multi-dimensional (with a number of indices). In addition, an array type may be constrained, in which the bounds for an index are established when the type is defined, or unconstrained, in which the bounds are established subsequently.

The syntax for declaring an array type is:

Subtypes, referred to in this syntax specification, will be discussed in detail in Section 2.2.7.

Some examples of constrained array type declarations:

An example of an unconstrained array type declaration:

The symbol '<>' (called a box) can be thought of as a place-holder for the index range, which will be filled in later when the array type is used. For example, an object might be declared to be a vector of 20 elements by giving its type as:

There are two predefined array types, both of which are unconstrained. They are defined as:

The types positive and natural are subtypes of integer, defined in Section 2.2.7 below. The type bit_vector is particularly useful in modeling binary coded representations of values in simulations of digital systems.

An element of an array object can referred to by indexing the name of the object. For example, suppose a and b are one- and two-dimensional array objects respectively. Then the indexed names a(1) and b(1, 1) refer to elements of these arrays. Furthermore, a contiguous slice of a one-dimensional array can be referred to by using a range as an index. For example a(8 to 15) is an eight-element array which is part of the array a.

Sometimes you may need to write a literal value of an array type. This can be done using an array aggregate, which is a list of element values. Suppose we have an array type declared as:

and we want to write a value of this type containing the elements 'f', 'o', 'o', 'd' in that order. We could write an aggregate with positional association as follows:

in which the elements are listed in the order of the index range, starting with the left bound of the range. Alternatively, we could write an aggregate with named association:

In this case, the index for each element is explicitly given, so the elements can be in any order. Positional and named association can be mixed within an aggregate, provided all the positional associations come first. Also, the word others can be used in place of an index in a named association, indicating a value to be used for all elements not explicitly mentioned. For example, the same value as above could be written as:

2.2.6. Records

VHDL provides basic facilities for records, which are collections of named elements of possibly different types. The syntax for declaring record types is:

An example record type declaration:

When you need to refer to a field of a record object, you use a selected name. For example, suppose that r is a record object containing a field called f. Then the name r.f refers to that field.

As for arrays, aggregates can be used to write literal values for records. Both positional and named association can be used, and the same rules apply, with record field names being used in place of array index names.

2.2.7. Subtypes

The use of a subtype allows the values taken on by an object to be restricted or constrained subset of some base type. The syntax for declaring a subtype is:

There are two cases of subtypes. Firstly a subtype may constrain values from a scalar type to be within a specified range (a range constraint). For example:

Secondly, a subtype may constrain an otherwise unconstrained array type by specifying bounds for the indices. For example:

There are two predefined numeric subtypes, defined as:

2.2.8. Object Declarations

An object is a named item in a VHDL description which has a value of a specified type. There are three classes of objects: constants, variables and signals. Only the first two will be discusses in this section; signals will be covered in Section 3.2.1. Declaration and use of constants and variables is very much like their use in programming languages.

A constant is an object which is initialised to a specified value when it is created, and which may not be subsequently modified. The syntax of a constant declaration is:

Constant declarations with the initialising expression missing are called deferred constants, and may only appear in package declarations (see Section 2.5.3). The initial value must be given in the corresponding package body. Some examples:

A variable is an object whose value may be changed after it is created. The syntax for declaring variables is:

The initial value expression, if present, is evaluated and assigned to the variable when it is created. If the expression is absent, a default value is assigned when the variable is created. The default value for scalar types is the leftmost value for the type, that is the first in the list of an enumeration type, the lowest in an ascending range, or the highest in a descending range. If the variable is a composite type, the default value is the composition of the default values for each element, based on the element types.

Some examples of variable declarations:

Assuming the type trace_array is an array of boolean, then the initial value of the variable trace is an array with all elements having the value false.

Given an existing object, it is possible to give an alternate name to the object or part of it. This is done using and alias declaration. The syntax is:

A reference to an alias is interpreted as a reference to the object or part corresponding to the alias. For example:

declares the name op_code to be an alias for the left-most eight bits of instr.

2.2.9. Attributes

Types and objects declared in a VHDL description can have additional information, called attributes, associated with them. There are a number of standard pre-defined attributes, and some of those for types and arrays are discussed here. An attribute is referenced using the `'´ notation. For example,

thing'attr

refers to the attribute attr of the type or object thing.

Firstly, for any scalar type or subtype T, the following attributes can be used:

For an ascending range, T'left = T'low, and T'right = T'high. For a descending range, T'left = T'high, and T'right = T'low.

Secondly, for any discrete or physical type or subtype T, X a member of T, and N an integer, the following attributes can be used:

For an ascending range, T'leftof(X) = T'pred(X), and T'rightof(X) = T'succ(X). For a descending range, T'leftof(X) = T'succ(X), and T'rightof(X) = T'pred(X).

Thirdly, for any array type or object A, and N an integer between 1 and the number of dimensions of A, the following attributes can be used: