Friday, July 31, 2015

SubPrograms

Subprograms

A subprogram defines a sequential algorithm that performs a certain computation and executes in zero simulation time.

There are two kinds of subprograms:

Functions: These are usually used for computing a single value. A function call can be used within an expression.

Procedures: These are used to partition large behavioral descriptions. Procedures can return zero or more values. A procedure call may be a sequential or a concurrent statement.

It is possible for two or more subprograms to have the same name. This is called overloading.

A subprogram is defined using a subprogram body. The typical format or syntax for a subprogram body is

subprogram-specification is
subprogram-item-declarations
begin
subprogram-statements -- Same as sequential-statements.
end [ subprogram-name ];

The subprogram-specification specifies the name of a subprogram and defines its interface, that is, it defines the formal parameter names, their class (i.e., signal, variable, or constant), their type, and their mode (whether they are in, out, or inout).

Parameters of mode in are read-only parameters; these cannot be updated within a subprogram body. Parameters of mode out are write-only parameters; their values cannot be used but can only be updated within a subprogram body. Parameters of mode inout can be read as well as updated.

Actual values are passed to and from a subprogram via a subprogram call. Only a signal object may be used to pass a value to a parameter of the signal class. Only a variable object may be used to pass a value to a parameter of the variable class.

A constant or an expression may be used to pass a value to a parameter of constant class. When parameters are of a variable or constant class, values are passed to the subprogram by value.

Arrays may or may not be passed by reference.

For signal objects, the reference to the signal, its driver, or both are passed into the subprogram, i.e., any assignment to a signal in a procedure (signals cannot be assigned values in a function because the parameters are restricted to be of input mode) affects the actual signal driver immediately and is independent of whether the procedure terminates or not.

For a signal of any mode, the signal-valued attributes, STABLE, QUIET, DELAYED, and TRANSACTION cannot be used in a subprogram body.

The type of an actual value in a subprogram call must match that of its corresponding formal parameter. If the formal parameter belongs to an unconstrained type, the size of this parameter is determined from the actual value that is passed in.

The subprogram-item-declarations part contains a set of declarations (e.g., type and object declarations) that are accessible for use locally within the subprogram. These declarations come into effect every time the subprogram is called.

Variables are also created and initialized every time the subprogram is called. They remain in existence until the subprogram completes. This is in contrast with declarations in a process statement that get initialized only once, that is at start of simulation, and any declared variables persist throughout the entire simulation run.

The subprogram-statements part contains sequential statements that define the computation to be performed by the subprogram.

A return statement, which is also a sequential statement, is a special statement that is allowed only within subprograms. The format of a return statement is

return { expression];

The return statement causes the subprogram to terminate and control is returned back to the calling object.

All functions must have a return statement and the value of the expression in the return statement is returned to the calling program.

For procedures, objects of mode out and inout return their values to the calling program.

The subprogram-name appearing at the end of a subprogram body, if present, must be the same as the function or procedure name specified in the subprogram specification part.

No comments:

Post a Comment