Consider that a class already has well written constraints and there is a need to randomize the class variables with a set of different constraints decided by the user. By using the with construct, users can declare in-line constraints at the point where the randomize() method is called. These additional constraints will be considered along with the object's original constraints by the solver.

Example

 
class Item;
  rand bit [7:0] id;
 
  constraint c_id { id < 25; }
 
endclass
 
module tb;
 
  initial begin
    Item itm = new ();
    itm.randomize() with { id == 10; };     // In-line constraint using with construct
    $display ("Item Id = %0d", itm.id);
  end
endmodule
 

Notice that inline constraint has been applied here and hence the randomized id is 10.

Simulation Log
run -all;
# KERNEL: Item Id = 10
# KERNEL: Simulation has finished. There are no more test vectors to simulate.

If the original constraint c_id is fixed to 25 as follows and we provide a conflicting in-line value, then the randomization will fail.

 
class Item;
  rand bit [7:0] id;
 
  constraint c_id { id == 25; }
endclass
 
module tb;
  initial begin
    Item itm = new ();
    if (! itm.randomize() with { id < 10; })
      $display ("Randomization failed");
    $display ("Item Id = %0d", itm.id);
  end
endmodule
 
Simulation Log
ncsim> run
    if (! itm.randomize() with { id < 10; })
                      |
ncsim: *W,SVRNDF (./testbench.sv,10|22): The randomize method call failed.
Observed simulation time : 0 FS + 0
ncsim: *W,RNDOCS: These constraints contribute to the set of conflicting constraints:

  constraint c_id { id == 25; }; (./testbench.sv,4)
    if (! itm.randomize() with { id < 10; }) (./testbench.sv,10)
ncsim: *W,RNDOCS: These variables contribute to the set of conflicting constraints:

rand variables:
       id [./testbench.sv, 2]

Randomization failed
Item Id = 0
ncsim: *W,RNQUIE: Simulation is complete.

For the next case, we'll see what happens when the original constraint is fixed and the in-line value also tries to fix the variable to another value.

 
  ...
  if (! itm.randomize() with { id == 10; })
  ...
 
Simulation Log
ncsim> run
    if (! itm.randomize() with { id == 10; })
                      |
ncsim: *W,SVRNDF (./testbench.sv,10|22): The randomize method call failed.
Observed simulation time : 0 FS + 0
ncsim: *W,RNDOCS: These constraints contribute to the set of conflicting constraints:

  constraint c_id { id == 25; }; (./testbench.sv,4)
    if (! itm.randomize() with { id == 10; }) (./testbench.sv,10)
ncsim: *W,RNDOCS: These variables contribute to the set of conflicting constraints:

rand variables:
       id [./testbench.sv, 2]

Randomization failed
Item Id = 0
ncsim: *W,RNQUIE: Simulation is complete.

The takeaway here is that constraints provided should not conflict with each other and in-line method of providing constraints does not override but instead is also considered along with the original by the solver.

You may also like:

You consent to our cookies if you continue to use our website. To know more about cookies, see our privacy policy. I accept cookies from this site.

Agree