Functions and Tasks constructs
These constructs are used to include related logic in a block, to improve design readability and re-usability.
Well, of course these can be replaced with a separate hierarchical module entity to bind the related logic. However, the problem with this approach is that it increases hierarchy, and would restrict the tool from leveraging it's optimization engine fully. Thus 'functions' and 'tasks' improve design re-usability and readability not adding any hierarchy.
Functions:
Syntax:
//invoking from always block
always@(in1, in2)
out1[3:0] = func1(in1, in2) ;
function [3:0] func1
input [3:0] in1, in2 ;
reg [3:0] out_temp ;
begin
out_temp = in1 + in2 ;
func1 = out_temp * in2 ;
end
endfunction
Properties of a function:
1. Call by value. Thus, only values of variables are passed while function invoking, and the original values of these variables stay intact.
2. There is no return, break, exit statements allowed in function body. The values assigned to a variable whose name is same as function name is returned when the function is exited.
3. The formal and actual arguments should match exactly in width and order.
4. Internal variables only of type reg are allowed in the function body.
5. Output ports can be of different width, they are adjusted accordingly.
6. All the code in the function body apart from variable declaration should be inside begin...end block.
7. All blocking assignment statements.
8. No #delays allowed inside body.
9. Should always be invoked on RHS statement of an always block.
Tasks:
Syntax:
//invoking from always block
always@(in1, in2)
task1(in1, in2, out1) ;
task task1
input [3:0] in1, in2 ;
output [3:0] out1 ;
reg [3:0] out_temp ;
begin
out_temp = in1 + in2 ;
out1 = out_temp * in2 ;
end
endtask
Properties of a task:
1. Call by value. Thus, only values of variables are passed while function invoking, and the original values of these variables stay intact. (Same as function)
2. There is no return, break, exit statements allowed in function body. The values assigned to a variable whose direction is output is returned when the task is exited.
3. The formal and actual arguments should match exactly in width and order. (Same as function)
4. Internal variables only of type reg are allowed in the function body. (Same as function)
5. Output ports can be of different width, they are adjusted accordingly. (Same as function)
6. All the code in the function body apart from variable declaration should be inside begin...end block. (Same as function)
7. All blocking assignment statements. (Same as function)
8. #delays allowed inside body.
9. Should always be an independent statement inside always block.
« Previous Next »
Well, of course these can be replaced with a separate hierarchical module entity to bind the related logic. However, the problem with this approach is that it increases hierarchy, and would restrict the tool from leveraging it's optimization engine fully. Thus 'functions' and 'tasks' improve design re-usability and readability not adding any hierarchy.
Functions:
Syntax:
//invoking from always block
always@(in1, in2)
out1[3:0] = func1(in1, in2) ;
function [3:0] func1
input [3:0] in1, in2 ;
reg [3:0] out_temp ;
begin
out_temp = in1 + in2 ;
func1 = out_temp * in2 ;
end
endfunction
Properties of a function:
1. Call by value. Thus, only values of variables are passed while function invoking, and the original values of these variables stay intact.
2. There is no return, break, exit statements allowed in function body. The values assigned to a variable whose name is same as function name is returned when the function is exited.
3. The formal and actual arguments should match exactly in width and order.
4. Internal variables only of type reg are allowed in the function body.
5. Output ports can be of different width, they are adjusted accordingly.
6. All the code in the function body apart from variable declaration should be inside begin...end block.
7. All blocking assignment statements.
8. No #delays allowed inside body.
9. Should always be invoked on RHS statement of an always block.
Tasks:
Syntax:
//invoking from always block
always@(in1, in2)
task1(in1, in2, out1) ;
task task1
input [3:0] in1, in2 ;
output [3:0] out1 ;
reg [3:0] out_temp ;
begin
out_temp = in1 + in2 ;
out1 = out_temp * in2 ;
end
endtask
Properties of a task:
1. Call by value. Thus, only values of variables are passed while function invoking, and the original values of these variables stay intact. (Same as function)
2. There is no return, break, exit statements allowed in function body. The values assigned to a variable whose direction is output is returned when the task is exited.
3. The formal and actual arguments should match exactly in width and order. (Same as function)
4. Internal variables only of type reg are allowed in the function body. (Same as function)
5. Output ports can be of different width, they are adjusted accordingly. (Same as function)
6. All the code in the function body apart from variable declaration should be inside begin...end block. (Same as function)
7. All blocking assignment statements. (Same as function)
8. #delays allowed inside body.
9. Should always be an independent statement inside always block.
« Previous Next »
Comments
Post a Comment