Tuesday, 7 October 2014

why a method using 'ref' as argument should be automaic?

As per SV guidelines, any method using 'ref' as keyword should be made automatic. Below are some key points to understand this.

1. methods inside a module/program block by default will have "static" life times.
2. methods defined inside a class by default will have "automatic" life time.

Consider an example below.

program main();
int a;
 
initial
begin
#10 a = 10;
#10 a = 20;
#10 a = 30;
#10 $finish;
end
 
task pass_by_val(int i);
forever
@i $display("pass_by_val: I is %0d",i);
endtask
 
 
task pass_by_ref(ref int i);
forever
@i $display("pass_by_ref: I is %0d",i);
endtask
 
initial
pass_by_val(a);
 
initial
pass_by_ref(a);
 
endprogram
 
 
This example has two static tasks.
In the example above, both tasks pass_by_val() and pass_by_ref() have static lifetimes. You could change the second initial block to
initial begin
      pass_by_val.i = 5;
      $display(pass_by_val.i); // will display 5
      fork 
         #11 pass_by_val(a); // a = 10, copied to pass_by_val.i
         #12 $display(pass_by_val.i); // will display "10"
         #30 pass_by_val.i = 2; // will display "pass_by_val: I is 2"
      join
  end
The $display at time 12 shows 10 because that was the value passed to i. The assignment statement at time 30 cause the @i event control to trigger and execut the $display inside pass_by_val(). If you change the third initial block to
initial begin
      pass_by_ref.i = 5;
      $display(pass_by_ref.i);
      pass_by_ref(a);
      ...
 
What variable is i referencing? We haven't called pass_by_ref yet, so 'a' is not referenced by i yet. This is just one of many problems where pass_by_ref.i could reference something that does not exist yet, or something that did exist, but no longer does. 
 
So, If task is static, we can access its variables hierarchically.
But, if arguments are "ref", and task is static, compiler
doesn't understand from where to take that ref value. So, if
task is defined as automatic, compiler doesn't map the ref
values until it is called. 
 
it is mandatory to make tasks/funtions automatic if they have
ref arguments and are being used in a module/program block. 
(in class, by default task/function is automatic). 

No comments:

Post a Comment