In reply to zero_h:
task static:
Variables declared inside a static task, function, or block are local in scope and default to a static lifetime.
static task:
A static method is different from a task with static lifetime. The former refers to the lifetime of the method within the class, while the latter refers to the lifetime of the arguments and variables within the task.
Keyword Static has different meaning when used to left and to the right of the task keyword. When used on the left side, i.e. static task incr();, task declared is static and associated with the particular class and not with the instance of the class. Static methods (task and functions) can operate only on static properties (data members) since they have no “this” pointer.
when used on right hand side, i.e. task static incr(), static refers to lifetime of local variables, parameters of the task. In this case, local variable and parameters are static, not passed on stack and initilized before start of simulation. Corresponding qualifier is “automatic” where local variables and parameters are passed on the stack and are initilized every time you enter the scope in which they are declared. In your case, lifetime of j is static, it is initialzed only once, each task instance shares the same copy, so it is incremented sequentially
rgs,
-sunil
Thanks Dave, I am new to Systemverilog so I might not fully understand what “illegal” means. Please bear with me. However, I can compile the code with “task static” and rum simulation without any error. It’s just that “task static” and “static task” will give me different results…
When used on the left side, i.e. static task incr();, task declared is static and associated with the particular class and not with the instance of the class.
Hi puranik.sunil, can you please give me some examples of the benefits of using a “static task” vs. just a regular “task”? Is it the only purpose to be able to call a task in a class without instantiating an object?
I didn’t mean to ask for tool support. I was trying to point out that the tool I used is a common/popular legit tool and the version should be new enough (just in case some readers of this forum want to test run my code).
And as a followup, I looked up “SystemVerilog 3.1a Language Reference Manual” page 116,
//A static method is different from a method with static lifetime. The former refers to the lifetime of the method within the class, while the latter refers to the lifetime of the arguments and variables within the task.
class TwoTasks;
static task foo(); ... endtask // static class method with
// automatic variable lifetime
task static bar(); ... endtask // non-static class method with
// static variable lifetime
endclass
This is something good to know about.
It’s surprising that 2015.07 version of QuestaSim doesn’t reflect such changes. I wish those major parties/organizations will have better rules communication.
Do you know which tool does reflect such errors so we can avoid future effort on similar issues?
examples of the benefits of using a “static task” vs. just a regular “task”? - the static methods are used to manipulate the static properties - static data members of the class. Static data members are shared by all the instances of class unlike ordinary data members, which have one copy per instance. You can use static data members (and static methods) if, for example you would like to keep a running count of number of objects of particular class which are created. Example is given below :
class Transaction;
static int count = 0; // Number of objects created
int id; // Unique instance ID
function new();
id = count++; // Set ID, bump count
endfunction
endclass
Transaction t1, t2;
initial begin
t1 = new(); // 1st instance, id=0, count=1
t2 = new(); // 2nd instance, id=1, count=2
$display("Second id=%d, count=%d", t2.id, t2.count);
end
Another use for a static variable is when every instance of a class needs information
from a single object. For example, a transaction class may refer to a configuration
object for a mode bit. If you have a nonstatic handle in the Transaction class, every
object will have its own copy, wasting space. Example below shows how to use a static
variable instead:
``` verilog
class Transaction;
static Config cfg; // A handle with static storage
MODE_E mode;
function new();
mode = cfg.mode;
endfunction
endclass
Config cfg;
initial begin
cfg = new(MODE_ON);
Transaction::cfg = cfg;
...
end