1 Functions and Procedures
1 Functions and Procedures
1 Functions and Procedures
Dr DC Hendry
May 8, 2006
VHDL supports both functions and procedures. These constructs are not how-
ever used in the manner that functions are used in a language such as C. In the C
language functions are used to divide code into manageable sized units, to make
the code more efficient to write and to maintain. In VHDL this task is largely
fullfilled by the use of a design hierarchy built with components instantiated
within a larger design.
2 Subprogram Syntax
Procedure Syntax
begin
procedure printsumxy
(constant x : in integer;
constant y : in integer) is
begin
xplusy := x + y;
write(l, Sum of x and y is );
write(l, xplusy, right, 5);
writeline(output, l);
For example:
Interface Classes
Each formal parameter has an interface class that is declared as part of the
formal parameters. For VHDL 93 there are four interface classes:
constant This is the default class for parameters of mode in. The actual
parameter may be a literal, a constant, a signal, a variable or an expression.
variable This is the default class for parameters of mode out. The actual
parameter must be a variable.
signal The actual parameter must be a signal.
file Only available with VHDL-93, the actual parameter must also be a file. In
VHDL-87, files are passed as parameters of class variable.
Function Syntax
<subprogram_declarations>
begin
<sequential_statements>
Functions
1. A pure function is one that for the same input arguments, returns the
same output. A function that returns the square of an input integer is
such a function.
2. Formal parameters of functions can onlybe constant of mode in.
3. An impure function is one that may return different data for the same
data input. The supplied function now that returns the current simulation
time is such a function.
4. The function declaration must give the type of the value to be returned.
5. The value to be returned is given in a return statement within the se-
quential statements. The type of the expression in the return statement
must match the type given in the function declaration.
3 Examples
ceillog2
Procedure Declarations:
For the last procedure the final two formal parameters have default values, so
we may write:
WRITE(Lin, invec);
and the final two parameters assume values of RIGHT and 0 respectively.