Tutorials
Learn More
Solve Before in constraints
As discussed in the bidirectional constraint, there is a possibility that the value of a variable can impact another variable value due to the bidirectional nature.
By default, a constraint solver has an equal probability solving algorithm. In certain cases, there is a requirement to solve constraints in a certain order. This changes the probability of value occurrence.
Important things to note:
- The dependency order of variables should not be bidirectional. 
For example: solve x before y; and solve y before x; // This is not allowed - Randc is not allowed.
 - Only integers are allowed.
 
Example without solve before
class seq_item;
  rand bit [7:0] val;
  rand bit en;
  
  constraint en_c { if(en == 1) { val inside {[0:100]}; } }
endclass
module constraint_example;
  seq_item item;
  
  initial begin
    item = new();
    
    repeat(5) begin
      item.randomize();
      $display("en = %0d, val = %0d", item.en, item.val);
    end
  end
endmodule		Output:
en = 0, val = 150
en = 0, val = 115
en = 0, val = 64
en = 0, val = 209
en = 0, val = 18		Example with solve before
class seq_item;
  rand bit [7:0] val;
  rand bit en;
  
  constraint en_c { solve en before val;
                    if(en == 1) { val inside {[0:100]}; }
                  }
endclass
module constraint_example;
  seq_item item;
  
  initial begin
    item = new();
    
    repeat(5) begin
      item.randomize();
      $display("en = %0d, val = %0d", item.en, item.val);
    end
  end
endmodule		Output:
en = 1, val = 11
en = 0, val = 115
en = 1, val = 32
en = 0, val = 209
en = 0, val = 18		System Verilog Tutorials