Constraints and Distributions
Learn how to control random stimulus by defining legal values, relationships between fields, and how often different values should occur.
Learning objectives
- Explain why verification needs constraints in addition to randomization.
- Create simple constraints using SystemVerilog constraint blocks.
- Restrict random values to legal ranges and sets.
- Use relational constraints to describe relationships between fields.
- Use
insideto specify legal value sets and ranges. - Understand how multiple constraints work together.
- Use
distto control the relative distribution of random values. - Recognize and debug basic randomization failures.
- Understand the difference between legal values and weighted values.
Step 1: Why do we need constraints?
In the previous lesson, you learned how to use
rand and randomize() to generate stimulus.
But randomization alone does not understand your design's protocol.
Imagine a bus transaction:
class bus_transaction;
rand bit [7:0] addr;
rand bit [31:0] data;
rand bit write;
endclass
The address can be any 8-bit value from 0 to
255.
But suppose our protocol requires:
- Addresses must be aligned to 4 bytes.
- Addresses must be between
0x20and0x7F. - Writes and reads must be selected according to a desired test mix.
Plain randomization does not know these rules.
Constraints allow us to describe the rules that randomized values must follow.
Step 2: Create your first constraint
A constraint is defined inside a constraint block.
class packet;
rand bit [7:0] addr;
constraint addr_range {
addr inside {[8'h20:8'h7F]};
}
endclass
The constraint says that addr must be within the range
0x20 through 0x7F.
When we call:
pkt.randomize();
SystemVerilog searches for a value that satisfies the constraint.
Random value
↓
Must satisfy the constraint
↓
Legal randomized value Step 3: Use ranges
One of the most common constraint patterns is restricting a value to a range.
constraint data_range {
data inside {[32'h0000_1000:32'h0000_1FFF]};
}
This allows any value from 0x00001000 through
0x00001FFF.
You can also use ordinary comparison operators:
constraint data_range {
data >= 32'h0000_1000;
data <= 32'h0000_1FFF;
}
Both approaches describe a range. The inside form is
often easier to read when describing legal sets and ranges.
Step 4: Use sets with inside
Sometimes a field can only contain specific values.
For example, suppose a command field can only be one of four values:
rand bit [2:0] command;
constraint valid_command {
command inside {0, 2, 4, 7};
}
The randomized value can be 0, 2,
4, or 7.
Values such as 1, 3, 5,
and 6 are not legal according to this constraint.
variable inside {value1, value2, value3};
Step 5: Combine ranges and sets
An inside list can contain individual values and ranges.
constraint valid_addr {
addr inside {
8'h00,
8'h10,
[8'h20:8'h2F],
[8'h80:8'h8F]
};
}
This allows:
0x000x100x20through0x2F0x80through0x8F
This is useful when a protocol has several legal address regions rather than one continuous range.
Step 6: Constrain relationships between fields
Constraints are not limited to individual variables. They can describe relationships between multiple random variables.
class transaction;
rand int unsigned start_addr;
rand int unsigned end_addr;
constraint address_order {
end_addr >= start_addr;
}
endclass
Now the two fields are related.
The solver must find values where:
end_addr >= start_addr
This is extremely useful for modeling protocol relationships.
For example:
- A burst length must match the amount of transferred data.
- An end address must be greater than or equal to a start address.
- A packet length must remain within a protocol limit.
- A mode field can determine which values are legal for another field.
Step 7: Multiple constraints work together
A class can contain more than one constraint.
class bus_transaction;
rand bit [7:0] addr;
rand bit [31:0] data;
rand bit write;
constraint addr_range {
addr inside {[8'h20:8'h7F]};
}
constraint addr_alignment {
addr[1:0] == 2'b00;
}
endclass
The address must satisfy both constraints.
Therefore, the solver must find an address that is:
- Between
0x20and0x7F. - Aligned to a 4-byte boundary.
Step 8: Randomization can fail
If constraints are impossible to satisfy at the same time, randomization can fail.
class packet;
rand bit [7:0] addr;
constraint rule1 {
addr < 8'h20;
}
constraint rule2 {
addr > 8'h80;
}
endclass
No 8-bit value can be both less than 0x20 and greater
than 0x80.
Therefore, there is no legal solution.
This is why the return value of randomize() is important.
if (!pkt.randomize()) begin
$fatal("Randomization failed");
end
Step 9: What is a distribution?
Constraints answer:
"Which values are legal?"
But sometimes we need to answer a different question:
"How often should each legal value occur?"
This is where distributions become useful.
For example, suppose a test wants:
- 80% writes
- 20% reads
We can express a relative weighting using dist.
Step 10: Use dist
The dist constraint lets you assign relative weights
to values or ranges.
class transaction;
rand bit write;
constraint write_distribution {
write dist {
1 := 8,
0 := 2
};
}
endclass
The weights are relative:
write = 1 → weight 8
write = 0 → weight 2
So the intended distribution is approximately:
write = 1 → 80%
write = 0 → 20%
Step 11: Weighted ranges
Distributions can also be applied to ranges.
rand bit [7:0] addr;
constraint addr_distribution {
addr dist {
[8'h00:8'h1F] := 2,
[8'h20:8'h7F] := 6,
[8'h80:8'hFF] := 2
};
}
Here, the ranges receive relative weights.
This lets a verification engineer intentionally exercise some regions more often than others.
This is useful when certain protocol regions deserve more verification attention.
Step 12: Legal values vs. weighted values
| Feature | Question it answers | Example |
|---|---|---|
| Randomization | What values can be generated? | rand bit [7:0] addr; |
| Constraint | Which values are legal? | addr inside {[8'h20:8'h7F]}; |
| Distribution | Which legal values should be favored? | write dist {1 := 8, 0 := 2}; |
rand
↓
random values
rand + constraint
↓
legal random values
rand + constraint + dist
↓
legal random values with intentional weighting Step 13: A complete transaction example
Let's combine the ideas into one realistic beginner transaction.
class bus_transaction;
rand bit [7:0] addr;
rand bit [31:0] data;
rand bit write;
constraint addr_range {
addr inside {[8'h20:8'h7F]};
}
constraint addr_alignment {
addr[1:0] == 2'b00;
}
constraint write_distribution {
write dist {
1 := 8,
0 := 2
};
}
endclass
Now randomize the transaction:
bus_transaction tr;
initial begin
tr = new();
repeat (10) begin
if (!tr.randomize())
$fatal("Transaction randomization failed");
$display(
"addr=%h data=%h write=%b",
tr.addr,
tr.data,
tr.write
);
end
end
The resulting transactions must obey the address constraints,
while the write field uses the requested relative
weighting.
Step 14: Constraints describe intent
One of the biggest advantages of constraints is that they describe what must be true rather than forcing the test writer to manually calculate every value.
Compare these two approaches.
Manual stimulus:
addr = 8'h20;
addr = 8'h24;
addr = 8'h28;
addr = 8'h2C;
...
Constraint-based stimulus:
constraint aligned_addr {
addr inside {[8'h20:8'h7F]};
addr[1:0] == 2'b00;
}
The second approach describes the rule once. The randomization engine can then generate many legal values.
Try it yourself
Create a class called transaction with:
addr— 8 bitsdata— 32 bitswrite— 1 bitlength— 4 bits
Add these rules:
addrmust be between0x20and0x7F.addrmust be 4-byte aligned.lengthmust be between 1 and 8.writeshould have a higher weight than read.
Randomize the transaction ten times and print every generated value.
length to be greater than
4 whenever write is 1.
Quick knowledge check
Question 1
What is the primary purpose of a constraint?
- To create a class
- To restrict randomized values to legal conditions
- To start a UVM phase
- To connect a driver
Question 2
What does this constraint allow?
constraint c {
addr inside {[8'h10:8'h1F]};
}
- Only
0x10 - Only
0x1F - Values from
0x10through0x1F - All 8-bit values
0x10 through
0x1F.
Question 3
What happens when two constraints contradict each other?
- SystemVerilog ignores both
- The first constraint always wins
- Randomization can fail because there is no legal solution
- The simulator automatically changes the constraints
Question 4
What does dist control?
- Object inheritance
- Relative weighting of random values
- Clock frequency
- UVM phase ordering
dist controls relative weighting in the
randomization distribution.
Question 5
Which statement best describes the difference between a constraint and a distribution?
- A constraint defines legal values; a distribution weights values
- A distribution defines legal values; a constraint creates objects
- They are exactly the same
- Neither affects randomization
Common beginner mistakes
Constraints do not generate stimulus by themselves. They restrict the values considered during randomization.
If no value can satisfy all active constraints,
randomize() can fail.
A weight controls relative likelihood. A small sample may not exactly match the requested percentage.
Start with simple rules. Add complexity only when the protocol actually requires it.
Always develop the habit of checking the return value of
randomize(), especially as constraints become
more complex.
Key takeaways
- Randomization generates possible values; constraints control which values are legal.
- The
constraintkeyword defines rules for randomized properties. insideis useful for describing legal values and ranges.- Constraints can describe relationships between multiple properties.
- Multiple constraints must be satisfied at the same time.
- Contradictory constraints can cause
randomize()to fail. distprovides relative weighting for randomized values and ranges.- Distribution weights are not guarantees of exact percentages in every test run.
- Constraint-based stimulus lets verification engineers describe legal behavior instead of manually listing every transaction.
- UVM sequences will later use these SystemVerilog features to generate controlled constrained-random stimulus.
What's next?
So far, you have learned how to generate and control individual randomized values.
Real verification environments also need to store and move large numbers of transactions.
SystemVerilog provides several useful data structures for this, including queues, arrays, and mailboxes.
In the next lesson, you will learn how these structures help verification components organize, buffer, and communicate data.