Reputation: 6978
In SystemVerilog, one can use the disable
construct to terminate named blocks or tasks. But I found today that disabling a task within a class instance affects all copies of the task in all class instances.
Consider a class which has a few tasks defined within it. If, internal to the class, there is a call to disable <taskname>
, this will disable all copies of the task across all instances of the class. This is not the behavior I expected, but it is correct according to the LRM:
If a task is enabled more than once, then disabling such a task shall disable all activations of the task.
Is there a mechanism or workaround to disable only the task within a specific instance of a class?
The code below highlights the problem I am facing. Basically, I have a timer task which I want to disable due to some other event. This is all contained within a class. In my verification environment I have multiple instances of this class and they operate entirely independently. So disabling the timer within one instance should not affect the others. But the way disable
works it is affecting the other instances.
I tried disable this.timer
but that did not compile.
In this example, I have two instances of class c
which contains a timer
task. There is a disabler
task which is started in parallel which makes the call to disable timer
. The intent in this example is for timer
to get disabled half-way through its count. So c1
would get disabled at time 5, and c2
would get disabled at time 10. However, they both are disabled at time 5, because the disable timer
call originating in c1
disables the task in both c1
and c2
.
module top();
class c;
string name;
int count;
function new(string _name, int _count);
name = _name;
count = _count;
endfunction
task t1();
timer();
$display("%t %s timer completed", $time, name);
endtask
task run();
fork
t1();
disabler();
join
endtask
task timer();
$display("%t %s starting timer with count=%0d", $time, name, count);
repeat(count) #1;
endtask
task disabler();
repeat(count/2) #1;
disable timer;
endtask
endclass
class ex;
c c1;
c c2;
function new();
c1 = new("c1", 10);
c2 = new("c2", 20);
endfunction
task run();
fork
c1.run();
c2.run();
join_none
endtask
endclass
ex e = new;
initial begin
e.run();
end
endmodule
Output:
0 c1 starting timer with count=10
0 c2 starting timer with count=20
5 c2 timer completed
5 c1 timer completed
I know I could rewrite this to get it to work without using disable
, but it's such an easy way to terminate a task early that I'm hoping there is something I'm missing.
Upvotes: 4
Views: 11461
Reputation: 595
I think tasks can be isolated inside threads. The idea is that you isolate your task inside a thread. You should apply the disable fork only for one specific instance. So you must ask about the instance where we are. ere we have a problem because systemverilog doesn't allow you to e.g.
fork
begin
yourtaskcall;
end
join_none
if (class_instance_disable_member_id==1) begin
disable fork;
end
Afterwards you can wrap again the previous code so that you isolate the disable fork only for that task. e.g.
fork
begin
fork
begin
yourtaskcall;
end
join_none
if (class_instance_disable_member_id==1) begin
disable fork;
end
end
join
Lastly, from your top you should set the variable "class_instance_disable_member_id" to 1 (default value=0) for the specific instance you want to disable. This can be done from the outerclass or top class.
e.g. top.class1.class_instance_disable_member_id=1;
The task will be disable by the time the task is called. So you must set the "class_instance_disable_member_id=1 variable" before the call happens. If you use UVM you can use phases (build or connect before run phase). If it is simple systemverilog then you can use the new() constructor to set that variable for the correct instance from the top.
If you want to activate/deactivate a task "on the fly/dynamically" then you should have another thread/task on the top that modifies that "class_instance_disable_member_id" for your specific instance before the task you want to disable is called.
You can also detect changes on the class_instance_disable_member_id to allow killing task in the middle, with the following basic block
fork
begin
yourtaskcall;
end
begin
if (class_instance_disable_member_id==0) begin
@(posedge class_instance_disable_member_id);
end
end
join_any
if (class_instance_disable_member_id==1) begin
disable fork;
end
I hope it helps BR
Upvotes: 0
Reputation: 69
class a1;
process p1;
task timer();
p1=process::self();
begin
while(1)
begin
$display("example data %t",$time);
#10;
end
end
endtask
task timer2();
for(int i = 0; i < 10; i ++)
begin
#20;
$display("shouldnot killed",i);
end
endtask
endclass
module example_kill;
a1 ab,bc;
initial begin
ab = new();
bc= new();
fork
ab.timer();
ab.timer2();
bc.timer();
join_none
#50;
ab.p1.kill();
#100;
bc.p1.kill();
wait fork;
end
endmodule
Upvotes: 2
Reputation:
I don't think this is possible. Even using disable on named blocks will still stop all instances of that task.
Disabling an automatic task or a block inside an automatic task proceeds as for regular tasks for all concurrent executions of the task.
You could try to use a process class. I've never used them myself so this code likely contains errors.
class c;
process p1;
task run();
fork
begin
p1 = process::self(); //Get process of this begin..end block
$display("%t %s starting timer with count=%0d", $time, name, count);
repeat(count) #1;
end
disabler()
join_none
endtask
task disabler();
wait (p1 != null);
repeat(count/2) #1;
p1.kill();
endtask
Upvotes: 3