Thursday, January 21, 2016

difference between randomize(obj) and obj.randomize

If we randomize the object of a class using obj.randomize(), it will only consider the constraints of that class. Whereas if we pass the object to the randomize() function(equivalent to obj.randomize_with()), it will also consider the constraints pertinent to that object in the parent class. In the below example, test is the parent class to class config. The config class variable (b_v) is constrained in the test class and it will take effect if we pass the object of config class to randomize() in the test class.  


class config;
rand int b_v;
constraint b_c {  b_v inside {[1:5]}; }
function new();
endfunction

function void post_randomize();
  $display("post_randomize b_v=%0d",b_v);
endfunction
endclass : config

class test;
rand int a_v;
constraint b_a_c {  cfg.b_v==5;  }
config cfg;
function new();
  cfg =new();
endfunction

function void build_phase();
  $display("A build_phase");
  randomize(cfg);
  $display("b_v=%0d",cfg.b_v);
  cfg.randomize();
  $display("b_v=%0d",cfg.b_v);
  randomize(cfg);
  $display("b_v=%0d",cfg.b_v);
  cfg.randomize();
  $display("b_v=%0d",cfg.b_v);
endfunction

endclass : test

module test_randomize;
test A;
initial begin
 A=new();
 A.build_phase();
end
endmodule


Answer:-
A build_phase
post_randomize b_v=5
b_v=5
post_randomize b_v=3
b_v=3
post_randomize b_v=5
b_v=5
post_randomize b_v=1
b_v=1

No comments:

Post a Comment