CONSTRAINTS
IN SYSTEM VERILOG
CONTENTS
Types of constraints in system Verilog
1. Hard Constraint
2. Soft Constraint
3. Iterative Constraint (foreach)
4. Unique Constraint
5. Solve-Before / Variable Ordering Constraint
6. Inline constraint
7. Implication Constraint (->)
8. If-Else Constraint
9. Weighted Distribution Constraint (dist)
10. Unique-If Constraint
11. Inheritance in Constraints
12. Set Membership Constraint (inside)
13. Extern Constraint
Generally randomization done in two ways:
1.Unconstrained Randomization → random values without restrictions.
2.Constrained Randomization → random values within specified rules/limits.
What is meant by constraints?
➢ Constraints are rules you apply on rand variables to control their randomized values.
➢ Without constraints, randomization is uniform distribution across full range.
➢ With constraints, you restrict possible values.
➢ We use constraint blocks inside the class to control the random values.
We can control the constraint mode
❖ constraints can be enabled/disabled.
❖ constraint_mode(1) → enable constraint
❖ constraint_mode(0) → disable constraint
Two Ways to Write Constraints
1. In-class Constraints
▪ Declared inside the class using the constraintkeyword.
▪ Automatically applied whenever randomize() is called.
▪ Can be enabled/disabled dynamically.
EXAMPLE:
class packet;
rand bit [7:0] addr;
constraint addr_c { addr inside {[10:50]}; } // In-class constraint
endclass
2. Inline Constraints
▪ Written directly in the randomize() call.
▪ Apply only for that particular randomization.
▪ Useful for one-time or temporary constraints.
EXAMPLE:
packet p = new();
p.randomize() with { addr > 100; }; // Inline constraint
All types of constraints:
1. Hard Constraints
➢ Default type.Must always be satisfied.
➢ If solver cannot satisfy → randomization fails (randomize() returns 0).
➢ Use when rule is mandatory.
Example:
class pkt;
rand int addr;
constraint c1 { addr < 100; } // must always hold
endclass
2. Soft Constraints
➢ Provide default rules.Can be overridden by stronger (hard) constraints or inline constraints.
➢ Useful when you want defaults but allow flexibility.
Example:
class pkt;
rand int addr;
constraint c2 { soft addr == 50; } // default = 50
endclass
module tb;
initial begin
pkt p = new();
p.randomize() with { addr == 80; }; // overrides soft constraint
$display("addr=%0d", p.addr); // prints 80
end
endmodule
3. Iterative Constraints (foreach)
➢ Apply constraints across arrays.
➢ Automatically iterates over array indices.
Example:
class pkt;
rand int arr[5];
constraint c3 { foreach(arr[i]) arr[i] inside {[1:10]}; }
endclass
4. Unique Constraint
➢ Ensures that all variables/elements in a set are distinct.
➢ Good for test vectors with no repetition.
Example:
class pkt;
rand int arr[4];
constraint c4 { unique {arr}; }
endclass
5. Solve-Before Constraint
➢ Explicitly sets dependency solving order.
➢ Useful when one variable must be solved first.
Example:
class pkt;
rand int x, y;
constraint c11 { solve x before y; y == x * 2; }
endclass
6. Inline Constraints
➢ Applied only during a specific randomize() call.
➢ Does not permanently affect class.
Example:
pkt p = new();
p.randomize() with { addr > 200; }; // one-time
7. Implication Constraints (->)
➢ Express conditional relationships.
➢ Syntax: (condition) -> (constraint);
Example:
class pkt;
rand int mode, addr;
constraint c6 { (mode == 1) -> (addr < 50); }
endclass
8. If-Else Constraints
➢ Provide branching rules inside constraints.
➢ More readable for multiple conditions.
Example:
class pkt;
rand int addr;
rand bit flag;
constraint cn { if(flag) addr inside {[0:20]};
else addr inside {[100:150]}; }
endclass
9. Weighted Distribution (dist)
➢ Gives probability weight to values/ranges.
➢ Useful for biased testing.
Example:
class pkt;
rand int addr;
constraint c8 { addr dist { 10 := 30, 20 := 70 }; } // 10 with 30% chance, 20 with 70%
endclass
10. Unique-If Constraint
➢ Applies uniqueness only under condition.
Example:
class pkt;
rand int a, b, c;
constraint cn{ unique if(a < 5) {a, b, c}; }
endclass
11. Inheritance in Constraints
➢ Child class inherits parent constraints.
➢ Can extend/override them.
Example:
class base;
rand int addr;
constraint base_c { addr < 50; }
Endclass
class child extends base;
constraint child_c { addr > 20; } // refines parent
endclass
12. Set Membership (inside)
➢ Restricts variable to specific set/range.
➢ Very commonly used.
Example:
class pkt;
rand int addr;
constraint c10 { addr inside { [0:15], 25, 30 }; }
endclass
13. Extern Constraints
➢ Declared outside the class body.
➢ Useful to separate declaration & definition.
Example:
class pkt;
rand int addr;
extern constraint addr_c;
endclass
constraint pkt::addr_c { addr < 100; }