Вы находитесь на странице: 1из 46

Subprograms and Overloading

By
Mr. Gaurav Verma
ECE Dept.
NIEC
ETEC-301- DCS-II

BY: GAURAV VERMA

ETEC-301- DCS-II BY: GAURAV


VERMA

Subprograms
Similar to subprograms found in other languages
Allow repeatedly used code to be referenced multiple times
without rewriting
Break down large blocks of code into small, more manageable
parts
VHDL provides functions and procedures

ETEC-301- DCS-II BY: GAURAV


VERMA

Subprograms (contd)
Contain sequential statements similar to processes
May declare local variables, constants
Executed when called from a sequential statement.
Local Variables are re-initialized every time a subprogram is called.
Parameters of calling routine are known as actuals, while the parameters
of the declared subprogram are known as formals.
Up level referencing to higher level variables and signals is allowed.
Recursive calls by functions and procedures are allowed
Attributes of signals cannot be accessed within subprograms
ETEC-301- DCS-II BY: GAURAV
VERMA

Produce a single return value


Called by expressions
Cannot modify the parameters passed to them
Require a RETURN statement
FUNCTION add_bits (a, b : IN BIT) RETURN BIT IS
BEGIN -- functions cannot return multiple values
RETURN (a XOR b);
END add_bits;
FUNCTION add_bits2 (a, b : IN BIT) RETURN BIT IS
VARIABLE result : BIT; -- variable is local to function
BEGIN
result := (a XOR b);
RETURN result; -- the two functions are equivalent
END add_bits2;
ETEC-301- DCS-II BY: GAURAV
VERMA

ETEC-301- DCS-II BY: GAURAV


VERMA

Functions

ETEC-301- DCS-II BY: GAURAV


VERMA

Functions

ETEC-301- DCS-II BY: GAURAV


VERMA

ETEC-301- DCS-II BY: GAURAV


VERMA

ETEC-301- DCS-II BY: GAURAV


VERMA

10

Example of a Function

ETEC-301- DCS-II BY: GAURAV


VERMA

11

Example of a Function (cont)

ETEC-301- DCS-II BY: GAURAV


VERMA

12

ARCHITECTURE behavior OF adder IS


BEGIN
PROCESS (enable, x, y)
BEGIN
IF (enable = '1') THEN
result <= add_bits(x, y);
carry <= x AND y;
ELSE
carry, result <= '0';
END PROCESS;
END behavior;

FUNCTION add_bits
(a, b : IN BIT)

Functions must be called by other statements


Parameters use positional association
ETEC-301- DCS-II BY: GAURAV
VERMA

13

ETEC-301- DCS-II BY: GAURAV


VERMA

14

Procedures (cont)

ETEC-301- DCS-II BY: GAURAV


VERMA

15

Procedures (cont)

ETEC-301- DCS-II BY: GAURAV


VERMA

16

Example of a Procedure

ETEC-301- DCS-II BY: GAURAV


VERMA

17

Another Example of a Procedure

ETEC-301- DCS-II BY: GAURAV


VERMA

18

Summary on Sequential Statements

ETEC-301- DCS-II BY: GAURAV


VERMA

19

May produce multiple output values


Are invoked by statements
May modify the parameters
PROCEDURE add_bits3 (SIGNAL a, b, en : IN BIT;
SIGNAL temp_result, temp_carry : OUT BIT) IS
BEGIN -- procedures can return multiple values
temp_result <= (a XOR b) AND en;
temp_carry <= a AND b AND en;
END add_bits3;

Do not require a RETURN statement


ETEC-301- DCS-II BY: GAURAV
VERMA

20

ARCHITECTURE behavior OF adder IS


BEGIN
PROCESS (enable, x, y)
BEGIN
add_bits3(x, y, enable,
result, carry);
END PROCESS;
END behavior;

The parameters must


be compatible in
terms of data flow
and data type

With parameter
passing, it is possible
to further simplify the
architecture

PROCEDURE add_bits3
(SIGNAL a, b, en : IN BIT;
SIGNAL temp_result,
temp_carry : OUT BIT)

ETEC-301- DCS-II BY: GAURAV


VERMA

21

Signal Resolution and Buses


Execution phase

Signal update phase


Transaction queue

OR

Bus Resolution Function

Resolved
signal

AND

ETEC-301- DCS-II BY: GAURAV


VERMA

22

Bus Resolution
Smoke Generator

VHDL does not allow multiple concurrent signal


assignments to the same signal

Multiple sequential signal assignments are allowed


LIBRARY attlib; USE attlib.att_mvl.ALL;
-- this code will generate an error
ENTITY bus IS
PORT (a, b, c : IN MVL; z : OUT MVL);
END bus;
ARCHITECTURE smoke_generator OF bus IS
SIGNAL circuit_node : MVL;
BEGIN
circuit_node <= a;
circuit_node <= b;
circuit_node <= c;
z <= circuit_node;
END smoke_generator;
ETEC-301- DCS-II BY: GAURAV
VERMA

23

Bus Resolution Functions


Are used to determine the assigned value when
there are multiple signal drivers to the same signal
FUNCTION wired_and (drivers : MVL_VECTOR) RETURN MVL IS
VARIABLE accumulate : MVL := '1';
BEGIN
FOR i IN drivers'RANGE LOOP
accumulate := accumulate AND drivers(i);
END LOOP;
RETURN accumulate;
END wired_and;

Bus resolution functions may be user defined or


called from a package
ETEC-301- DCS-II BY: GAURAV
VERMA

24

Bus Resolution

Smoke Generator Fixed

A signal which has a bus resolution function associated with it may


have multiple drivers
LIBRARY attlib; USE attlib.att_mvl.ALL;
USE WORK.bus_resolution.ALL;
ENTITY bus IS
PORT (a, b, c : IN MVL; z : OUT MVL);
END bus;
ARCHITECTURE fixed OF bus IS
SIGNAL circuit_node : wired_and MVL;
BEGIN
circuit_node <= a;
circuit_node <= b;
circuit_node <= c;
z <= circuit_node;
END fixed;
ETEC-301- DCS-II BY: GAURAV
VERMA

25

Null Transactions
How can a driver be disconnected (i.e. not influence the output at
all)?
Use the null waveform element

Example
bus_out <= NULL AFTER 17 ns;

What happens if all drivers of a resolved signal are disconnected?


Use register kind in signal declaration to keep most recently determined value
Use bus kind in signal declaration if resolution function will determine the value

Example
signal t : wired_bus BUS;
signal u : BIT REGISTER;

ETEC-301- DCS-II BY: GAURAV


VERMA

26

Exists outside of a process but in an architecture


The process is itself a concurrent statement all processes
scheduled to run concurrently
Concurrent signal assignment is a short hand form for a single
statement process -- equivalent to process containing one
statement, sensitive to changes on the right hand side.
Used frequently in DATAFLOW style descriptions
ETEC-301- DCS-II BY: GAURAV
VERMA

27

The Process

ETEC-301- DCS-II BY: GAURAV


VERMA

28

Overloading
Overloading refers to using the same procedure
(or function) name to define two or more procedures
that operate on different types or number
of parameters.

procedure incr (a :in integer; n :in integer);


procedure incr (a :in bit_vector; n :in integer);
procedure incr (a :in integer);
VHDL also permits overloading of operator symbols
like +, -, * and so on.

ETEC-301- DCS-II BY: GAURAV


VERMA

29

Overloading: example
function + (left,right : bit_vector)
return bit_vector is
begin

end function;

variable a, b, c: bit_vector(7 downto 0);

c = a + b;

ETEC-301- DCS-II BY: GAURAV


VERMA

30

Concurrent Signal Assignment

ETEC-301- DCS-II BY: GAURAV


VERMA

31

Concurrent Assignment
Statements

ETEC-301- DCS-II BY: GAURAV


VERMA

32

Concurrent Signal Sensitivity


Concurrent Statements are sensitive to all signals on
the input side
If a signal appears on both sides, the statement is
sensitive to changes in its own output.
A <= A+B; will be evaluated when B changes. This will
change A and the statement will be evaluated again.
Time will not be able to advance because the
statement keeps executing.
ETEC-301- DCS-II BY: GAURAV
VERMA

33

Conditional Signal
Statement

ETEC-301- DCS-II BY: GAURAV


VERMA

34

Example of Conditional Signal


Statement

ETEC-301- DCS-II BY: GAURAV


VERMA

35

Selected Signal Statement

ETEC-301- DCS-II BY: GAURAV


VERMA

36

Example of Selected Signal


Assignment

ETEC-301- DCS-II BY: GAURAV


VERMA

37

Concurrent Procedure Call


IN, OUT and INOUT parameter modes
Allows return of more than 1 value (unlike
function call)
Considered a statement
Equivalent to a process containing the single
procedure call followed by a wait on parameters
of mode in or inout
ETEC-301- DCS-II BY: GAURAV
VERMA

38

Example of Concurrent
Procedure Call

ETEC-301- DCS-II BY: GAURAV


VERMA

39

Sequential vs. Concurrent


Statement in Simulation Cycle
VHDL is inherently a concurrent language
All VHDL processes execute concurrently
Concurrent signal assignment statements are actually
oneline processes
VHDL statements execute sequentially within a process
Concurrent processes with sequential execution within
a process offers maximum flexibility
Supports various levels of abstraction
Supports modeling of concurrent and sequential events
as observed in realETEC-301systems
DCS-II BY: GAURAV
40
VERMA

ETEC-301- DCS-II BY: GAURAV


VERMA

41

Blocks
Blocks are concurrent statements and provide a
mechanism to partition an architecture description
Items declared in declarative region of block are
visible only inside the block, e.g. :
signals, subprograms

Blocks may be nested to define a hierarchical


partitioning of the architectural description
Blocks may contain Guards for disabling drives.
ETEC-301- DCS-II BY: GAURAV
VERMA

42

ETEC-301- DCS-II BY: GAURAV


VERMA

43

ETEC-301- DCS-II BY: GAURAV


VERMA

44

ETEC-301- DCS-II BY: GAURAV


VERMA

45

VLSI, Ohio University, Prof. Starzyk


Professor K.J. Hintz.
California State University Northridge

ETEC-301- DCS-II BY: GAURAV


VERMA

46

Вам также может понравиться