VHDL Programming

(C. Jardin) #1

216 Chapter Eight


expression may be required. A qualified expression states the exact type
that the expression should attain. For instance, when evaluating an
expression containing a mixture of overloaded subprograms and constant
values, the designer may need to qualify an expression to produce correct
results. Following is an example of such a situation:

PACKAGE p_qual IS
TYPE int_vector IS ARRAY(NATURAL RANGE <>) OF INTEGER;

FUNCTION average( a : int_vector) RETURN INTEGER;

FUNCTION average( a : int_vector) RETURN REAL;

END p_qual;

USE WORK.p_qual.ALL;
ENTITY normalize IS
PORT( factor : IN REAL;
PORT( points : IN int_vector;
PORT( result : OUT REAL);
END normalize;

ARCHITECTURE qual_exp OF normalize IS
BEGIN
result <= REAL’(average(points)) * factor;
END qual_exp;

Package p_qualdefines two overloaded functions named averageand
an unconstrained type,int_vector. The package body is left as an exercise
for the reader.
Architecture qual_exphas a single concurrent signal assignment state-
ment that calls function average. Because there are two functions named
average, there are two possible functions that can be used by this call. To
clarify which function to use, the expression has been qualified to return
a REALtype. The keyword REALfollowed by a ’specifies that the expres-
sion inside the parentheses return a type REAL.
The expression was qualified to make sure that the averagefunction
returning a REALnumber was called instead of the averagefunction that
returns an INTEGER. In this example, the expression required a qualified
expression to allow the architecture to compile. The compiler does not
make any random guesses about which function to use. The designer must
specify exactly which one to use in cases where more than one function
can match; otherwise, an error is generated.
Another use for a qualified expression is to build the source value for
an assignment statement. Based on the type of the signal assignment tar-
get, the source value can be built. Following is an example:
Free download pdf