VHDL Programming

(C. Jardin) #1

92 Chapter Four


This access returns the fourth element of the fourth row, which, in this
example, is a ‘ 1 ’.

UNCONSTRAINED ARRAY TYPES

An unconstrained array type is a type whose range or size is not completely
specified when the type is declared. This allows multiple subtypes to share
a common base type. Entities and subprograms can then operate on all of
the different subtypes with a single subprogram, instead of a subprogram
or entity per size.
Following is an example of an unconstrained type declaration:

TYPE BIT_VECTOR IS ARRAY(NATURAL RANGE <>) OF BIT;

This is the type declaration for type BIT_VECTORfrom the Standard
package. This type declaration declares a type that is an array of type BIT.
However, the number of elements of the array is not specified. The notation
that depicts this is:

RANGE <>

This notation specifies that the type being defined has an uncon-
strained range. The word NATURALbefore the keyword RANGE, in the type
declaration, specifies that the type is bounded only by the range of
NATURAL. Type NATURALis defined in the Standard package to have a range
from 0 to integer’high(the largest integer value). Type BIT_VECTOR, then,
can range in size from 0 elements to integer’highelements. Each element
of the BIT_VECTOR typeis of type BIT.
Unconstrained types are typically used as types of subprogram argu-
ments, or entity ports. These entities or subprograms can be passed items
of any size within the range of the unconstrained type.
For instance, let’s assume that a designer wants a shift-right function for
typeBIT_VECTOR. The function uses the unconstrained typeBIT_VECTORas
the type of its ports, but it can be passed any type that is a subtype of type
BIT_VECTOR. Let’s walk through an example to illustrate how this works.
Following is an example of an unconstrained shift-right function:

PACKAGE mypack IS
SUBTYPE eightbit IS BIT_VECTOR(0 TO 7);
SUBTYPE fourbit IS BIT_VECTOR(0 TO 3);
FUNCTION shift_right(val : BIT_VECTOR)
RETURN BIT_VECTOR;
END mypack;

PACKAGE BODY mypack IS
FUNCTION shift_right(val : BIT_VECTOR) RETURN BIT_VECTOR
Free download pdf