VHDL Programming

(C. Jardin) #1

Subprograms and Packages 139


result := s(i);
ELSE
result := undriven;
ASSERT FALSE
REPORT “multiple drivers detected”
SEVERITY ERROR;
END IF;
END IF;
END LOOP;
RETURN result;
END resolve_cluster;
END cluspack;

The package body statement is very similar to the package declaration,
except for the keyword BODYafter package. The contents of the two design
units are very different, however. This package body example contains
only two items: the deferred constant value for deferred constant undriven
and the subprogram body for subprogram resolve_cluster. Notice how
the deferred constant value specification matches the deferred constant
declaration in the package declaration, and the subprogram body matches
the subprogram declaration in the package declaration. The subprogram
body must match the subprogram declaration exactly in the number of
parameters, the type of parameters, and the return type.
A package body can also contain local declarations that are used only
within the package body to create other subprogram bodies, or deferred
constant values. These declarations are not visible outside of the package
body but can be very useful within the package body. Following is an
example of a complete package making use of this feature:

USE LIBRARY IEEE;

USE IEEE.std_logic_1164.ALL;
PACKAGE math IS
TYPE st16 IS ARRAY(0 TO 15) OF std_logic;

FUNCTION add(a, b: IN st16) RETURN st16;
FUNCTION sub(a, b: IN st16) RETURN st16;

END math;

PACKAGE BODY math IS

FUNCTION vect_to_int(S : st16) RETURN INTEGER IS
VARIABLE result : INTEGER := 0;
BEGIN
FOR i IN 0 TO 7 LOOP
result := result * 2;

IF S(i) = ‘ 1 ’ THEN
Free download pdf