This has driven me crazy before and going to ask if there is not some way around this. Consider bar and foo below where both have a var named “myvar” of type int. foo instantiates bar and I want the value of myvar to be the same in both. In other words, I want bar to randomize with myvar == foo.myvar. Nothing I try works other than to say, copy foo.myvar into another var like temp and constrain against temp. (That or change the name of myvar in one of the classes). It’s some sort of scoping issue I guess as it appears “this” below refers to “bar_inst” and not “foo”.
module test;
class bar_c;
rand int myvar;
static bit is_randomized;
constraint myvar_lc {soft myvar <=20 && myvar >=5;}
function new();
is_randomized = 0;
endfunction // new
function void post_randomize();
if (is_randomized) // ie: don't call me twice
return;
is_randomized = 1;
$display("bar post_randomize...");
endfunction // post_randomize
endclass
class foo_c;
rand int myvar;
rand bar_c bar_inst;
constraint myvar_lc {myvar == 27;}
function new();
endfunction
function void post_randomize();
bar_inst = new;
bar_inst.randomize() with {bar_inst.myvar == this.myvar;};
endfunction // post_randomize
endclass // foo
foo_c foo;
initial begin
foo = new();
foo.randomize();
$display("myvar=%0d",foo.myvar);
$display("foo.myvar = %0d, bar_inst.myvar=%0d",foo.myvar, foo.bar_inst.myvar);
end
endmodule // test