VHDL Programming

(C. Jardin) #1

Advanced Topics 211


supplied in VHDL only work with specific types. For instance, the +opera-
tor only works with integer, real, and physical types, while the &(concate-
nation) operator only works with array types. If a designer wants to use
a particular operator on a user-defined type, then the operator must be
overloaded to handle the user type. A complete listing of the operators and
the types supported by them can be found in the VHDL Language Refer-
ence Manual.
An example of a typical overloaded operator is the +operator. The +
operator is defined for the numeric types, but if the designer wants to add
two BIT_VECTORobjects, the +operator does not work. The designer must
write a function that overloads the operator to accomplish this operation.
The following package shows an overloaded function for operator +that
allows addition of two objects of BIT_VECTORtypes:

PACKAGE math IS
FUNCTION “+”( l,r : BIT_VECTOR) RETURN INTEGER;
END math;

PACKAGE BODY math IS
FUNCTION vector_to_int( S : BIT_VECTOR) RETURN INTEGER IS
VARIABLE result : INTEGER := 0;
VARIABLE prod : INTEGER := 1;
BEGIN
FOR i IN s’RANGE LOOP
IF s(i) = ‘ 1 ’ THEN
result := result + prod;
END IF;
prod := prod * 2;
END LOOP;

RETURN result;
END vector_to_int;

FUNCTION “+”(l,r : BIT_VECTOR) RETURN INTEGER IS
BEGIN
RETURN ( vector_to_int(l) + vector_to_int(r));
END;
END math;

Whenever the +operator is used in an expression, the compiler calls
the +operator function that matches the types of the operands. When the
operands are of type INTEGER, the built-in +operator function is called. If
the operands are of type BIT_VECTOR, then the function from package math
is called. The following example shows uses for both functions:

USE WORK.math.ALL;
ENTITY adder IS
PORT( a, b : IN BIT_VECTOR(0 TO 7);
Free download pdf