VHDL Programming

(C. Jardin) #1

140 Chapter Five


result := result + 1;
END IF;
END LOOP;

RETURN result;
END vect_to_int;

FUNCTION int_to_st16(s : INTEGER) RETURN st16 IS
VARIABLE result : st16;
VARIABLE digit : INTEGER := 2**15;
VARIABLE local : INTEGER;
BEGIN
local : = s;
FOR i IN 15 DOWNTO 0 LOOP
IF local/digit >>= 1 THEN
result(i) := ‘ 1 ’;
local := local - digit;
ELSE
result(i) := ‘ 0 ’;
END IF;

digit := digit/2;

END LOOP;
RETURN result;
END int_to_st16;

FUNCTION add(a, b: IN st16) RETURN st16 IS
VARIABLE result : INTEGER;
BEGIN
result := vect_to_int(a) + vect_to_int(b);
RETURN int_to_st16(result);
END add;

FUNCTION sub(a, b: IN st16) RETURN st16 IS
VARIABLE result : INTEGER;
BEGIN
result := vect_to_int(a) - vect_to_int(b);
RETURN int_to_st16(result);
END sub;

END math;

The package declaration declares a type st16and two functions,add
and sub, that work with this type. The package body has function bodies
for function declarations addand suband also includes two functions that
are only used in the package body. These functions are int_to_st16and
vect_to_int. These functions are not visible outside of the package body.
To make these functions visible, a function declaration would need to be
added to the package declaration, for each function.
Free download pdf