VHDL Programming

(C. Jardin) #1

Advanced Topics 207


ELSE

result(i) := a(i + 1);
END IF;
END LOOP;

RETURN result;
END shiftr;

FUNCTION shiftr( a : s_int) return s_int IS
BEGIN
RETURN (a/2);
END shiftr;
END p_shift;

The package p_shiftcontains two functions both named shiftr. Both
functions provide a right-shift capability, but each function operates on a
specific type. One function works only with type s_int, and the other
works only with type s_array. The compiler picks the appropriate function
based on the calling argument(s) and return argument.
In the following example, different types of function calls are shown,
and the results obtained with each call:

USE WORK.p_shift.ALL;
ENTITY shift_example IS
END shift_example;

ARCHITECTURE test OF shift_example IS
SIGNAL int_signal : s_int;
SIGNAL array_signal : s_array;
BEGIN
-- picks function that works with s_int type
int_signal <= shiftr(int_signal);

-- picks function that works with
-- s_array type
array_signal <= shiftr(array_signal);

-- produces error because no function
-- will match
array_signal <= shiftr(int_signal);
END test;

The architecture test contains three calls to function shiftr. The first
calls shiftrwith an argument type of s_intand a return type of s_int.
This call uses the second function described in package body p_shift, the
function with input arguments, and return type of s_int.
The second call to shiftruses the array type s_array, and therefore
picks the first function defined in package p_shift. Both the input
Free download pdf