Vous êtes sur la page 1sur 26

Subprograms and overloading

Made By: Pooja Shukla 00113503110 B.Tech(IT), 5th semester

Subprograms
A subprogram defines a sequential algorithm that performs a certain computation. There are two kinds of subprograms: Function: Computes a single value. executes in zero simulation time

Procedure: can computes several values may not execute in zero simulation time, depending on whether it has a wait statement or not.

A subprogram can be defined using a subprogram body. The typical format is:
subprogram-specification is

subprogram-item-declarations
begin subprogram-statements end[function|procedure] [subprogram-name]

Subprogram specification specifies Name of the subprogram Defines its Interface : defines the formal parameter names, their class, their types, and their mode (in, out, inout).

Actual Parameters
Actuals in a subprogram call are used to pass values to and from a subprogram. The type of an actual in a subprogram call must match its corresponding formal parameter. e.g. Only an actual signal may be associated with a formal parameter of the signal class.

Subprogram- item-declarations: It contains a set of declarations that are accessible for use within the subprogram. These declarations come into effect every time the subprogram is called. Variables are created and initialized every time the subprogram is called. They remain in existence until the subprogram completes. Subprogram-statements: It contains sequential statements that define the computation to be performed by the subprogram. All the functions must have return statement.

Functions

Functions are used to describe frequently used sequential algorithms that return a single value. This value is returned to the calling program using a return statement. Common uses of Functions:
Resolution Functions Type Conversion Functions

Pure functions VS. impure functions


Syntax: [pure | impure] function function-name (parameter-list) return return-type

Pure function: return the same value each time the function is called with the same set of actuals. Impure function: potentially returns different values each time the function is called with the same set of actuals. For instances, NOW is an impure function because it returns different values when called at different times. If neither keyword pure or impure is present in the function specification, the function is, by default, a pure function

Types of formals and actuals must match except for formals which are constants (default) Formals which are constant match actuals which are variable, constant or signal Wait statements are not permitted in a function! The parameter-list describes the list of formal parameters for the function. The only mode allowed for parameters is mode in. Only constants and signal objects can be passed in as parameters.

The default object class is constant.

Placement of Functions
Architecture
function W

visible in processes A, B & C

process A

process B

process C

function X

function Y

function Z

visible only in process A

Place function code in the declarative region of the architecture or process

Function: Example
architecture behavioral of dff is function rising_edge (signal clock : std_logic) return boolean is variable edge : boolean := FALSE; begin edge := (clock = 1 and clockevent); return (edge); end rising_edge;

Architecture Declarative Region

begin output: process begin wait until (rising_edge(Clk)); Q <= D after 5 ns; Qbar <= not D after 5 ns; end process output; end architecture behavioral;

Procedures
Procedures allow decomposition of large behaviors into modular sections. In contrast to a function, a procedure can return zero or more values using mode out and inout.

Procedures can be used in sequential statement areas of a process concurrent area of an architecture

12

Essentials of Procedures
procedure read_v1d (variable f: in text; v :out std_logic_vector) --declarative region: declare variables local to the procedure -begin -- body -end read_v1d;

Parameters may be of mode in (read only) and out (write only) Default class of input parameters is constant Default class of output parameters is variable Variables declared within procedure are initialized on each call

Procedures: Placement
architecture behavioral of cpu is --- declarative region -- procedures can be placed in their entirety here -begin process_a: process -- declarative region of a process -- procedures can be placed here begin --- process body -end process_a; process_b: process --declarative regions begin -- process body end process_b; end architecture behavioral; visible to all processes

visible only within process_a

visible only within process_b

Placement of Procedures
Architecture procedure W visible in processes A, B & C

process A procedure X

process B procedure Y

process C procedure Z

visible only in process A

Placement of procedures determines visibility in its usage

Procedure call

Procedures are invoked by using procedure calls. A procedure call can be either sequential statement or o concurrent statement ; this is based on where the actual procedure call statement is placed. If the call is inside a process statement or another subprogram, it is a sequential procedure call statement; Otherwise , it is a concurrent procedure call statement. The syntax of a procedure call statement is:
[label:] procedure-name(list-of-actuals);

A Sequential procedure call statement is executed sequentially with respect to the sequential statements surrounding it. A concurrent procedure call statement is executed whenever an event occurs on one of the parameters that is a signal of mode in or inout.

Concurrent Procedure Calls

architecture structural of serial_adder is component comb port (a, b, c_in : in std_logic; z, carry : out std_logic); end component; procedure dff(signal d, clk, reset : in std_logic; signal q, qbar : out std_logic) is begin if (reset = 0) then q <= 0 after 5 ns; qbar <= 1 after 5 ns; elsif (rising_edge(clk)) then q <= d after 5 ns; qbar <= (not D) after 5 ns; end if; end dff;

signal s1, s2 : std_logic; begin C1: comb port map (a => a, b => b, c_in => s1, z =>z, carry => s2); --- concurrent procedure call -dff(clk => clk, reset =>reset, d=> s2, q=>s1, qbar =>open); end architectural structural;

Equivalent Sequential Procedure Call

architecture structural of serial_adder is component comb port (a, b, c_in : in std_logic; z, carry : out std_logic); end component; procedure dff(signal d, clk, reset : in std_logic; signal q, qbar : out std_logic) is begin if (reset = 0) then q <= 0 after 5 ns; qbar <= 1 after 5 ns; elsif (clkevent and clk = 1) then q <= d after 5 ns; qbar <= (not D) after 5 ns; end if; end dff;

signal s1, s2 : std_logic; begin C1: comb port map (a => a, b => b, c_in => s1, z =>z, carry => s2); --- sequential procedure call -process begin dff(clk => clk, reset =>reset, d=> s2, q=>s1, qbar =>open); wait on clk, reset,s2; end process; end architecture structural;

A procedure body can have wait statement, while a function cannot. Functions are used to compute values that are available instantaneously. Therefore, a function cant be made to wait
For ex. it cant call a procedure with wait statement in it.

Subprogram Overloading

Overloading: two or more subprograms with the same name.


function COUNT (ORANGES : INTEGER) return INTEGER; function COUNT (APPLES : BIT) return INTEGER; Both functions are overloaded since they have the same name, COUNT. When a call to either function is made, it is possible to identify the exact function to which the call is made from the type of actuals passed. COUNT (20) COUNT (1)

Hiding

If two overloaded subprograms have the same parameter types and result types , it is possible for one subprogram to hide the other subprogram. This can happen if a subprogram is declared within another subprogram scope

Hiding Example
architecture HIDING of DUMMY_ENTITY is function ADD (A, B : BIT_VECTOR) return BIT_VECTOR is begin -- body of function here. end ADD; begin SUM_IN_ARCH <= ADD (IN1, IN2); process function ADD (C, D : BIT_VECTOR) return BIT_VECTOR is begin -- body of function here. end ADD; begin SUM_IN_PROCESS <= ADD (IN1, IN2) ; SUM_IN_ARCH <= HIDING.ADD (IN1, IN2); end process; end HIDING;

Use Clause

It is possible for two overloaded subprograms to be directly visible within a region , by using use clauses. In such a case, a subprogram call may be ambiguous, and hence an error,if it is not possible to determine which of the overloaded subprograms is being called.

Use package
package P1 is function ADD (A, B : BIT_VECTOR) return BIT_VECTOR; end P1; package P2 is function ADD (X, Y : BIT_VECTOR) return BIT_VECTOR; end P2;

use WORK.P1.all, WORK.P2.all; architecture OVERLOADED of DUMMY_ENTITY is begin


SUM_CORRECT <= ADD (X => IN1, Y => IN2); SUM_ERROR <= ADD (IN1, IN2); -- this will give an error: ambiguious end OVERLOADED;

Resolving calls
Two overloaded subprograms can also have a different number of parameters but have the same parameter types and result types. In this case the no. of actuals supplied in the subprogram call identifies the correct subprogram.

A call to an overloaded subprogram is ambiguous, and hence an error, if it is not possible to identify the exact subprogram being called using the following information:

Subprogram name
Number of actuals Types and order of actuals Names of formal parameters( if named association is used) Result type ( for functions)

Vous aimerez peut-être aussi