Randomization with function

In reply to boryah:

Function in constraints are used (evaluating from left to right) only to specify comparisons or specific states the variable should have. This means that you can include a checking mechanism or prints in there without any problem. Nevertheless, your case is not consistent in terms of solving since you are explicitly saying I wanna A to be 0 and in [1:5].


class A;
  rand int a;
  rand int b;
 
  function int foo(int ai,int bi);
    $display("call foo() with a: %0d, b: %0d", ai, bi);
    return 1;
  endfunction
 
  constraint b_c {    
    b inside {[0:5]};
  };
  constraint a_c {   
    foo(a,b) == 1;
    a inside {[1:5]};
  };
  
  // Use post Randomize alternatively to print out
  function void post_randomize();
    $display("Post Randomize call with a: %0d, b: %0d", a, b);
  endfunction
endclass
 
A a = new();
 
module tb;
  initial begin
    repeat(5) begin
    	void'(a.randomize());
    end
    $finish();
  end
endmodule

// Result
call foo() with a: 1, b: 0
post Randomize call foo() with a: 1, b: 0
call foo() with a: 5, b: 1
post Randomize call foo() with a: 5, b: 1
call foo() with a: 2, b: 0
post Randomize call foo() with a: 2, b: 0
call foo() with a: 1, b: 4
post Randomize call foo() with a: 1, b: 4
call foo() with a: 3, b: 3
post Randomize call foo() with a: 3, b: 3
$finish called from file “testbench.sv”, line 31.
$finish at simulation time 0