Theory of static and automatic I know
module tryfact;
// define the function
function integer factorial (input [31:0] operand);
begin
if (operand >= 2)
factorial = factorial (operand - 1) * operand;
else
factorial = 1;
end
endfunction: factorial
// test the function
integer result;
initial begin
for (int n = 0; n <= 7; n++) begin
result = factorial(n);
$display("%0d factorial=%0d", n, result);
end
end
endmodule: tryfact
and
why in above example function act according to automatic? why values are not increasing by default function and variable in module are static nature: execution of code not understand
module test();
function get;
int i;
i++;
$display(i);
endfunction
initial begin
repeat(10) begin
get();
end
end
endmodule
but in this example, we do not use any key word but here act as static that I understand. Above 1st example is confusing
In reply to yaten1993:
I do not understand what you are asking. When I run your first example, I see the static lifetime function behavior
# vsim
# run -all
# 0 factorial=1
# 1 factorial=1
# 2 factorial=1
# 3 factorial=1
# 4 factorial=1
# 5 factorial=1
# 6 factorial=1
# 7 factorial=1
# exit
But when I change the function to having an automatic lifetime
function automatic integer factorial (input [31:0] operand);
I see
# vsim
# run -all
# 0 factorial=1
# 1 factorial=1
# 2 factorial=2
# 3 factorial=6
# 4 factorial=24
# 5 factorial=120
# 6 factorial=720
# 7 factorial=5040
# exit
In reply to dave_59:
I guess the meaning of the question is different and I think, I got it. First of all, thank you yaten1993 for bringing this example to the forum.
Task and function are one of my favorite topics and most confusing topics. Favorite because I got to know a lot of things regarding how arguments are being passed and executions are being carried out upon the return value and confusing and rather frustrating as every time everyone will feel like we know nothing about it.
Coming to your question, you understood your 2nd example, right? The output will be 1 2 3 4 5 6 7 8 9 10.
In the first example, What is happening is that in the end the value which is being returned is 1 and the value of the operand has become 1 as the execution of the function is happening first. Try to bring the operand before, you will get different observations.
I have tried to cover most of the ways in the following code. Try to observe it. One more thing I would like to mention is that whenever we are declaring a task and function inside a module or program using automatic will be the safest way we declare it inside the class, then it’s fine because the class is automatic so the function will be automatic by default.
//(Q.1)
//1st Case
module tryfact;
// define the function
function integer factorial (int operand);
if (operand <=1)return 1;
return factorial (operand - 1) * operand;
endfunction: factorial
// test the function
integer result;
initial begin
for (int n = 0; n <= 7; n++) begin
result = factorial(n);
$display("%0d factorial=%0d", n, result);
end
end
endmodule: tryfact
//2nd Case
module tryfact;
// define the function
function integer factorial (input [31:0] operand);
begin
integer result = 1 ;
if (operand >= 2)
result = operand * factorial (operand - 1) ;
else
result = 1;
return result;
end
endfunction: factorial
// test the function
integer result;
initial begin
for (int n = 0; n <= 7; n++) begin
result = factorial(n);
$display("%0d factorial=%0d", n, result);
end
end
endmodule: tryfact
//3rd Case
module tryfact;
// define the function
function integer factorial (input [31:0] operand);
begin
integer result = 1 ;
repeat(operand)
result = result * (operand--);
return result;
end
endfunction: factorial
// test the function
integer result;
initial begin
for (int n = 0; n <= 7; n++) begin
result = factorial(n);
$display("%0d factorial=%0d", n, result);
end
end
endmodule: tryfact
//4th Case
module tryfact;
// define the function
function automatic integer factorial (input [31:0] operand);
begin
integer result = 1 ;
repeat(operand)
result = result * (operand--);
return result;
end
endfunction: factorial
// test the function
integer result;
initial begin
for (int n = 0; n <= 7; n++) begin
result = factorial(n);
$display("%0d factorial=%0d", n, result);
end
end
endmodule: tryfact
//5th Case
module tryfact;
// define the function
function integer factorial (input [31:0] operand);
begin
if (operand >= 2)
factorial = operand * factorial (operand - 1) ;
else
factorial = 1;
end
endfunction: factorial
// test the function
integer result;
initial begin
for (int n = 0; n <= 7; n++) begin
result = factorial(n);
$display("%0d factorial=%0d", n, result);
end
end
endmodule: tryfact
//6th Case
module tryfact;
// define the function
function automatic integer factorial (input [31:0] operand);
begin
if (operand >= 2)
factorial = factorial (operand - 1) * operand ;
else
factorial = 1;
end
endfunction: factorial
// test the function
integer result;
initial begin
for (int n = 0; n <= 7; n++) begin
result = factorial(n);
$display("%0d factorial=%0d", n, result);
end
end
endmodule: tryfact
//(Q.2)
// 1st Case
module test();
function get;
int i=0;
i++;
$display(i);
endfunction
initial begin
repeat(10) begin
get();
end
end
endmodule
// 2nd Case
module test();
function automatic get;
int i=0;
i++;
$display(i);
endfunction
initial begin
repeat(10) begin
get();
end
end
endmodule
And sir please correct me if I am wrong at any point.