all about vlsi DV
Constraints in SystemVerilog — Controlling Randomness Properly
Introduction
In the previous article, Alice learned how to randomize values.
That immediately created a new problem.
Alice said:
“Random values are good… but not all random values are valid.”
For example:
-
Address should be within a legal range
-
Data should not violate protocol rules
-
Some combinations must never occur
Randomization without control is noise.
Constraints exist to turn randomness into meaningful stimulus.
What Constraints Really Are
A constraint is a rule, not a value.
Alice does not say:
“Give me address 10.”
She says:
“Give me an address between 0 and 15.”
SystemVerilog then finds a value that satisfies the rule.
This is the mental shift interviewers look for.
Alice and Bob Mental Model
-
Alice defines rules (constraints)
-
System Verilog solves them
-
Bob verifies behavior
-
Alice never manually picks values
Alice controls what is allowed, not what is chosen.
Basic Constraint Example (Core Idea)
class transaction;
rand bit [7:0] addr;
rand bit [7:0] data;
constraint addr_limit {
addr < 16;
}
endclass
module constraint_basic;
initial begin
transaction t = new();
repeat (5) begin
t.randomize();
$display("addr=%0d data=%0d", t.addr, t.data);
end
end
endmodule
Here:
-
addr is random
-
but always less than 16
-
Alice never manually checks values
This is correct verification thinking.
Why Constraints Must Be Inside Classes
Constraints are part of object behavior.
They:
-
Depend on class variables
-
Are solved during randomize()
-
Can interact with other constraints
This is why constraints:
-
Cannot exist in modules
-
Cannot exist without rand variables
Interviewers ask this directly.
Inline Constraints (Very Common in Interviews)
Sometimes Alice wants a rule only once.
She does not want to modify the class.
t.randomize() with { addr inside {[4:8]}; };
This means:
-
Use existing class
-
Apply a temporary rule
-
Do not change the original constraints
Inline constraints are extremely common in real testbenches.
Combining Multiple Constraints
Alice often needs more than one rule.
class transaction;
rand bit [7:0] addr;
rand bit [7:0] data;
constraint c1 { addr < 32; }
constraint c2 { data != 0; }
endclass
All constraints must be satisfied together.
If any rule conflicts, randomization fails.
This is important for debugging.
Constraint Failure (Interview Favorite)
What happens if constraints cannot be satisfied?
if (!t.randomize())
$display("Randomization failed");
This tells Bob:
-
The rules contradict
-
No legal solution exists
Interviewers often ask:
“How do you detect randomization failure?”
This is the answer.
inside Constraint (Used Everywhere)
constraint addr_range {
addr inside {8, 16, 32};
}
This restricts values to a set, not a range.
Used heavily in:
-
Protocol fields
-
Enumerated behaviors
-
State modeling
Constraint Order (Subtle but Important)
Constraints are solved together, not sequentially.
Order in code does NOT mean priority.
Interview trick question:
“Which constraint is applied first?”
Correct answer:
None. The solver considers all together.
Common Beginner Mistakes
-
Thinking constraints execute sequentially
-
Using constraints to force exact values
-
Forgetting to check randomize() return value
-
Writing conflicting rules unknowingly
These cause silent failures.
How Interviewers Test This Topic
They usually don’t ask syntax.
They ask:
-
How do you restrict random values?
-
What happens when constraints conflict?
-
Difference between inline and class constraints?
-
How do you debug randomization failures?
If you can reason through Alice’s intent, you will answer correctly.
Interview Practice Questions (Embedded)
-
Why are constraints needed if randomization exists?
-
Can constraints exist outside a class?
-
What happens if two constraints contradict?
-
What is the difference between inline and class constraints?
-
How do you know randomization failed?
All of these are answerable from this article.
One Sentence to Remember
Randomization generates values, constraints define legality.
If this sentence is clear, you understand constraints.
What’s Next
Now Alice can:
-
Randomize
-
Restrict values
-
Generate legal stimulus
Next, she wants to:
“Control how values change across multiple randomizations.”
That leads to the final interview-heavy topic.
👉 Next Article:
Constraint Solve Order, randc, and Real Interview Traps