Module 1: SystemVerilog for UVM | Beginner UVM

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 inside to specify legal value sets and ranges.
  • Understand how multiple constraints work together.
  • Use dist to 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 0x20 and 0x7F.
  • 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.

Key idea: Randomization generates values. Constraints define which values are legal.

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.

Think of a constraint as a rule:
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.

Useful pattern:
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:

  • 0x00
  • 0x10
  • 0x20 through 0x2F
  • 0x80 through 0x8F

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:

  1. Between 0x20 and 0x7F.
  2. Aligned to a 4-byte boundary.
Important: Think of constraints as a set of rules that must all be true at the same time.

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};
Remember the progression:
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.

UVM connection: This transaction is very similar to the type of sequence item you will later randomize inside a UVM sequence.

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.

Verification mindset: Describe the legal space instead of manually enumerating every transaction you want to test.

Try it yourself

Create a class called transaction with:

  • addr — 8 bits
  • data — 32 bits
  • write — 1 bit
  • length — 4 bits

Add these rules:

  1. addr must be between 0x20 and 0x7F.
  2. addr must be 4-byte aligned.
  3. length must be between 1 and 8.
  4. write should have a higher weight than read.

Randomize the transaction ten times and print every generated value.

Challenge: Add a constraint requiring length to be greater than 4 whenever write is 1.

Quick knowledge check

Question 1

What is the primary purpose of a constraint?

  1. To create a class
  2. To restrict randomized values to legal conditions
  3. To start a UVM phase
  4. To connect a driver
Answer: B — Constraints define rules that randomized values must satisfy.

Question 2

What does this constraint allow?

constraint c {


addr inside {[8'h10:8'h1F]};
}
  1. Only 0x10
  2. Only 0x1F
  3. Values from 0x10 through 0x1F
  4. All 8-bit values
Answer: C — The range includes every value from 0x10 through 0x1F.

Question 3

What happens when two constraints contradict each other?

  1. SystemVerilog ignores both
  2. The first constraint always wins
  3. Randomization can fail because there is no legal solution
  4. The simulator automatically changes the constraints
Answer: C — If there is no solution satisfying all active constraints, randomization fails.

Question 4

What does dist control?

  1. Object inheritance
  2. Relative weighting of random values
  3. Clock frequency
  4. UVM phase ordering
Answer: B — dist controls relative weighting in the randomization distribution.

Question 5

Which statement best describes the difference between a constraint and a distribution?

  1. A constraint defines legal values; a distribution weights values
  2. A distribution defines legal values; a constraint creates objects
  3. They are exactly the same
  4. Neither affects randomization
Answer: A — Constraints define the legal solution space, while distributions influence the relative selection of values.

Common beginner mistakes

1. Thinking constraints generate values

Constraints do not generate stimulus by themselves. They restrict the values considered during randomization.

2. Writing contradictory constraints

If no value can satisfy all active constraints, randomize() can fail.

3. Treating distribution weights as exact percentages

A weight controls relative likelihood. A small sample may not exactly match the requested percentage.

4. Making constraints unnecessarily complicated

Start with simple rules. Add complexity only when the protocol actually requires it.

5. Forgetting to check randomization failure

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 constraint keyword defines rules for randomized properties.
  • inside is 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.
  • dist provides 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.