User guide

24-86
SystemVerilog Testbench Constructs
Output:
First call to randomize,the guard g evaluates to FALSE
x = 9 y = 1 g = 0
Second call to randomize,the guard g evaluates to TRUE
x = 3 y = 1 g = 1
Third call to randomize, constraint mode of constraint block E is OFF
x = 1 y = 9 g = 1
In the first call to randomize, the guard is evaluated to FALSE.
Therefore, the constraint does not override the default constraint and
consequently x + y == 10. In the second call to randomize, the guard
is evaluates to TRUE. Therefore, the non-default constraint is valid
and it overrides the default constraint. In the third call to randomize,
the constraint block is turned OFF. Since the non-default constraint
is turned off, the default constraint is not overridden and x + y == 10.
program test;
class A;
rand reg[3:0] x;
rand reg[3:0] y;
default constraint T{x + y == 10;}
function void post_randomize();
$display("x = %0d y = %0d\n", x, y);
endfunction
endclass
initial
begin
integer ret;
A a = new;
$display("First call to randomize, rand mode of x
and y are ON\n");
ret = a.randomize();
$display("Second call to randomize, rand mode of x
and y are OFF\n");
a.x.rand_mode(0);
a.y.rand_mode(0);
a.x = 0;
a.y = 0;
ret = a.randomize();
$display("Return status of randomize is %0d" ret);
end
endprogram