2. 2
OVERLOADING
Overloading allows the designer to write much more readable
code & maintainable
An object is overloaded when the same object name exists for
multiple subprograms or type values.
Overloading can be:
Subprogram overloading:(subprograms to operate on
different types )
Operator overloading:(same operation on multiple types)
3. 3
OPERATOR OVERLOADING
Most useful feature in VHDL.
Operators work only on specific types
+ : only on integer ,real, physical types
&: only on arrays
If a designer want to use a particular operator on user defined
types , the operator should be overloaded.
Example : + is only for numeration type ,to add 2 bit vector it doesn’t work
So designer need to write a function that overload the operator for this
operation.
4. 4
Operator Overloading
Definition:
Operator overloading is a declaration of a function
whose designator is an operator symbol.
Simplified Syntax:
function "operator" (parameters) return type is
function "operator" (parameters) return type is
declarations
begin
sequential statements
end function;
FUNCTION “+” (a,b :bit_vector ) RETURN INTEGER
5. 5
The + operator is used in an expression, the compiler calls the +
operator function that matches the types of the operands.
When the operands are of type INTEGER, the built-in + operator
function is called. If the operands are of type BIT_VECTOR, then the
function from package math is called.
USE WORK.math.ALL;
ENTITY adder IS
PORT( a, b : IN BIT_VECTOR(0 TO 1);
PORT( c : IN INTEGER;
PORT( dout : OUT INTEGER);
END adder;
ARCHITECTURE test OF adder IS
SIGNAL internal : INTEGER;
BEGIN
internal <= a + b;
dout <= c + internal;
END test;
6. 6
When standard operator symbol is made to behave differently based on
the type of its operand, the operator is said to be overloaded.
Predefined operators are defined only for operands of predefined types.
Eg :and ,or
Operators have two types of notation
standard operator notation
standard function call notation: operator enclosed with double
quotes
7. 7
Example 1
Type Log4 is ('0', '1', 'Z', 'X');
function "nand" (Left, Right: Log4) return Log4;
function "or" (Left, Right: Log4) return Log4;
signal S1, S2: Log4;
S1 <= "or" ('1', 'Z');
S2 <= "nand"(S1, 'X');
Functions or and nand implement basic logic operations for operands of type Log4
overloading the predefined operations of the same names. In the above example
these functions are called using the standard call syntax function (prefix).
Example 2
signal S3, S4: Log4;
S3 <= (S1 nand S2) or 'X';
The operators or and nand are overloaded through the declarations as in the Example
1. Here the overloaded operators are used in the infix form, i.e. with the operator
name between the operands.