Hi Everyone !!!,
In this session we are going to see the concepts and usage of Static keyword in System Verilog.
In General, Each instance of the class will be created by separate memory. If you want to share one memory for each instance that time we can use "static" keyword before that variable or method.
If you use "static" keyword only one memory will be shared across the all instance.
Key notes about Static Method(static task/static function):
- Static method has no access to non static members/variables.
- Static members/Variables can be accessed in Non Static method(tasks/functions)
- Static method can't be a virtual.
- Static method can be accessed outside of the class by using scope resolution operator without any class instantiation.
class example1;
static int a; //"a"is static variables
int b; //"b" is a non static variables
task incr;
a++;
b++;
$display(" TASK CALLING A Value %0d B Value =%0d",a,b);
endtask
endclass
module exam;
int n;
example1 x; // For class 3 instance(x,y,z) created.
example1 y;
example1 z;
initial begin
x=new();
y=new();
z=new();
x.incr(); // Calling a task by using different instance
y.incr();
z.incr();
end
endmodule
In example1 "a" is declared as "Static Variable" and "b" non static variable. Both the variables are accessed in non static task. For a class in module I am creating 3 instances such as x,y,z. By calling a task with each instance non static member will create a separate memory for each instance so "b" value gets resetted at each instance where as in static member shares the same memory so every call it wont't reset and keep on increase. By seeing output you will get the clear idea.
OUTPUT:
TASK CALLING A Value 1 B Value =1
TASK CALLING A Value 2 B Value =1
TASK CALLING A Value 3 B Value =1
V C S S i m u l a t i o n R e p o r t
Example 2:TASK CALLING A Value 2 B Value =1
TASK CALLING A Value 3 B Value =1
V C S S i m u l a t i o n R e p o r t
Accessing non static member in static method (static task) which is leads to compilation Error.
class example1;
int a;
static task incr;
a++; //Here in static method I am trying to access non static member so it leads to compilation issue
$display(" TASK CALLING %0d",a);
endtask
endclass
module exam;
int n;
example1 x;
initial begin
x=new();
repeat(5) begin
x.incr;
end
end
endmodule
Output:
Error-[SV-AMC] Non-static member access
testbench.sv, 6
$unit, "a"
Illegal access of non-static member 'a' from static method 'example1::incr'.
Error-[SV-AMC] Non-static member access
testbench.sv, 7
$unit, "a"
Illegal access of non-static member 'a' from static method 'example1::incr'.
2 errors
CPU time: .134 seconds to compile
testbench.sv, 6
$unit, "a"
Illegal access of non-static member 'a' from static method 'example1::incr'.
Error-[SV-AMC] Non-static member access
testbench.sv, 7
$unit, "a"
Illegal access of non-static member 'a' from static method 'example1::incr'.
2 errors
CPU time: .134 seconds to compile
Example 3:
Without Creating an object of the class,We can access the static methods to outside of the class.
class example1;
static int a;
static task incr;
a++;
$display(" TASK CALLING %0d",a);
endtask
endclass
module exam;
int n;
example1 v,w,x,y,z; // instances
initial begin
v.incr;
w.incr;
x.incr; //By using instance alone I am accessing the static methods without creating object
y.incr;
z.incr;
end
endmodule
OUTPUT:
TASK CALLING 1
TASK CALLING 2
TASK CALLING 3
TASK CALLING 4
TASK CALLING 5
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
TASK CALLING 2
TASK CALLING 3
TASK CALLING 4
TASK CALLING 5
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
Example 4:
By using class name we can access the static methods in outside of the class.
class example1;
static int a;
static task incr;
a++;
$display(" TASK CALLING %0d",a);
endtask
endclass
module exam;
initial begin
repeat(5) begin
example1::incr; //By using class name with scope resolution operator static task is called.
end
end
endmodule
OUTPUT:
TASK CALLING 1
TASK CALLING 2
TASK CALLING 3
TASK CALLING 4
TASK CALLING 5
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
TASK CALLING 2
TASK CALLING 3
TASK CALLING 4
TASK CALLING 5
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
Example 5:
Virtual is not allowed in static methods.It is illegal to use virtual keyword in static methods.
class example1;
static int a;
virtual static task incr;
a++;
$display(" TASK CALLING %0d",a);
endtask
endclass
module exam;
initial begin
repeat(5) begin
example1::incr; //By using class name with scope resolution operator static task is called.
end
end
endmodule
OUTPUT:
Error-[WUCIQ] Invalid qualifier usage
testbench.sv, 5
Invalid use of class item qualifiers. Cannot use virtual and static keywords
together for method declarations.
1 error
CPU time: .131 seconds to compile
testbench.sv, 5
Invalid use of class item qualifiers. Cannot use virtual and static keywords
together for method declarations.
1 error
CPU time: .131 seconds to compile
Lets we discuss about difference between task/function static and static task/function.
static task t1(); ... endtask // static class method with automatic variable lifetime
task static t2(); ... endtask // non-static class method with static variable lifetime
Example 1:
We can access a non static variable in inside of task static.
class example1;
int a;
task static incr; //Here "a" variable is accessed in task static
a++;
$display(" TASK CALLING %0d",a);
endtask
endclass
module exam;
example1 x;
initial begin
x=new();
repeat(5) begin
x.incr;
end
end
endmodule
OUTPUT:
TASK CALLING 1
TASK CALLING 2
TASK CALLING 3
TASK CALLING 4
TASK CALLING 5
V C S S i m u l a t i o n R e p o r t
TASK CALLING 2
TASK CALLING 3
TASK CALLING 4
TASK CALLING 5
V C S S i m u l a t i o n R e p o r t
Key notes of task /function static:
- We can access a non static member in inside of task
- We can't access a variable without object creation
- We can't access a variable by using class name in outside of class.
- Here Variable alone static life time.
EXAMPLE 2:
In inside of task static,the variable will be treated as static. Here "b" is declared in inside of task. And we are creating 5 different instance. For "b" same memory is allocated. So its value is keep on increasing where as for "a" its outside of class ,for every instance creation separate memory will be created while seeing this code and output you will get an clear idea.
class example1;
int a;
task static incr;
int b;
a++;
b++;
$display(" TASK CALLING A=%0d B=%0d",a,b);
endtask
endclass
module exam;
int n;
example1 x,y,z,v,w;
initial begin
x=new();
y=new();
z=new();
v=new();
w=new();
x.incr;
y.incr;
z.incr;
w.incr;
v.incr;
end
endmodule
OUTPUT:
TASK CALLING A=1 B=1
TASK CALLING A=1 B=2
TASK CALLING A=1 B=3
TASK CALLING A=1 B=4
TASK CALLING A=1 B=5
V C S S i m u l a t i o n R e p o r t
TASK CALLING A=1 B=2
TASK CALLING A=1 B=3
TASK CALLING A=1 B=4
TASK CALLING A=1 B=5
V C S S i m u l a t i o n R e p o r t
Generally in module tasks are static in default where as in class tasks are automatic in default.
Here some of you got confused, what is the difference between task and task static. Let me explain this,
Example 2:
In Normal task inside of the task it won't be static in nature. For each instance separate memory will be created for all variables. Kindly go through this code and output.
class example1;
int a;
task incr;
int b;
a++;
b++;
$display(" TASK CALLING A=%0d B=%0d",a,b);
endtask
endclass
module exam;
int n;
example1 x,y,z,v,w;
initial begin
x=new();
y=new();
z=new();
v=new();
w=new();
x.incr;
y.incr;
z.incr;
w.incr;
v.incr;
end
endmodule
OUTPUT:
TASK CALLING A=1 B=1
TASK CALLING A=1 B=1
TASK CALLING A=1 B=1
TASK CALLING A=1 B=1
TASK CALLING A=1 B=1
V C S S i m u l a t i o n R e p o r t
TASK CALLING A=1 B=1
TASK CALLING A=1 B=1
TASK CALLING A=1 B=1
TASK CALLING A=1 B=1
V C S S i m u l a t i o n R e p o r t
Hope this logic's will be useful to everyone !!!
Giving feedback is more precious than writing an article!!! 😊😊😊😊😊
Always welcome both positive and negative feedback's !!!
Feel free to post any queries related SV and UVM
Note:
Sample codes and outputs are tried in EDA and copied here.
“It's fine to celebrate success, but it is more important to heed the lessons of failure.”
—Bill Gates
No comments:
Post a Comment