VHDL Programming

(C. Jardin) #1

110 Chapter Five


Subprograms


Subprograms consist of procedures and functions. A procedure can return
more than one argument; a function always returns just one. In a function,
all parameters are input parameters; a procedure can have input para-
meters, output parameters, and inout parameters.
There are two versions of procedures and functions: a concurrent pro-
cedure and concurrent function, and a sequential procedure and sequential
function. The concurrent procedure and function exist outside of a process
statement or another subprogram; the sequential function and procedure
exist only in a process statement or another subprogram statement.
All statements inside of a subprogram are sequential. The same state-
ments that exist in a process statement can be used in a subprogram,
including WAITstatements.
A procedure exists as a separate statement in an architecture or process;
a function is usually used in an assignment statement or expression.

Function


The following example is a function that takes in an array of the
std_logictype (described in Chapter 9,“Synthesis”and Appendix A,
“Standard Logic Package”) and returns an integer value. The integer value
represents the numeric value of all of the bits treated as a binary number:

USE LIBRARY IEEE;

USE IEEE.std_logic_1164.ALL;
PACKAGE num_types IS
TYPE log8 IS ARRAY(0 TO 7) OF std_logic; --line 1
END num_types;

USE LIBRARY IEEE; USE IEEE.std_logic_1164.ALL;
USE WORK.num_types.ALL;
ENTITY convert IS
PORT(I1 : IN log8; --line 2
O1 : OUT INTEGER); --line 3
END convert;

ARCHITECTURE behave OF convert IS
FUNCTION vector_to_int(S : log8) --line 4
RETURN INTEGER is --line 5
VARIABLE result : INTEGER := 0; --line 6
BEGIN
FOR i IN 0 TO 7 LOOP --line 7
result := result * 2; --line 8
Free download pdf