Formal Definition
Functions provide a means of splitting code into small parts that are frequently used in a model.
Simplified Syntax
function type_or_range identifier;
parameter_declaration;
input_declaration;
register_declaration;
event_declaration;
statement;
endfunction
Description
Functions can only be declared inside a module declaration.
Function definition begins with the function keyword and ends with the endfunction keyword. The returned type or range declaration followed by a function identifier and semicolon should appear after the function keyword. Function can contain declarations of range, returned type, parameters, input arguments, registers and events (these declarations are similar to module items declaration). Net declarations are illegal. Declaration of parameters, registers, events and returned type or range are not required. A function without a range or return type declaration, returns a one-bit value. Functions should have at least one input declaration and a statement that assigns a value to the register with the same name as the function.
Any expression can be used as a function call argument. Functions cannot contain any time-controlled statements, and they cannot enable tasks. Functions can return only one value.
Examples
Example 1
function [15:0] negation;
input [15:0] a;
negation = ~a;
endfunction
A function returning 16-bit value.
Example 2
function real multiply;
input a, b;
real a, b;
multiply = ((1.2 * a) * (b * 0.17)) * 5.1;
endfunction
A function returning real value.
Example 3
real a;
wire [15:0] b;
wire c, d, e, f;
assign b = negation ({4{c, d, e, f}}); // (#1)
initial begin
a = multiply(1.5, a); // (#2)
$display(" b=%b ~b=%b”, b, negation(b)); // (#3)
end
Examples of function calls declared in example 1 and 2 (#1 - continuous assignment, #2 and #3 - procedural statements).
Important Notes
· Functions cannot contain time-control statements.
· A function returns value when invoked by its name.
· Functions should have at least one input argument.
· Functions cannot have output or inout arguments.
· Functions can only be declared inside a module declaration.
· Can use 'include to insert function code
No comments:
Post a Comment
Please provide valuable comments and suggestions for our motivation. Feel free to write down any query if you have regarding this post.