Module 3: Phases and Transactions | Lesson 29

Transaction Constraints

Learn how to control constrained-random transaction generation so your UVM sequences produce legal, meaningful, and useful stimulus.

Learning objectives

  • Explain why transaction constraints are important in constrained-random verification.
  • Write basic SystemVerilog constraints for a UVM sequence item.
  • Constrain transaction fields to legal ranges and sets of values.
  • Express relationships between multiple transaction fields.
  • Use conditional constraints to model different transaction types.
  • Understand the difference between constraints and procedural assignments.
  • Recognize and debug constraint conflicts and randomization failures.
  • Understand how constraints affect stimulus quality and verification coverage.

Step 1: Why do transactions need constraints?

In Lesson 28, we created sequence items with rand fields. For example:

class bus_transaction extends uvm_sequence_item;


rand bit        write;
rand bit [31:0] addr;
rand bit [31:0] data;

`uvm_object_utils(bus_transaction)

function new(string name = "bus_transaction");
super.new(name);
endfunction

endclass

The rand keyword tells SystemVerilog that these fields may be randomized.

But unrestricted randomization can generate values that are useless or illegal. A bus might only allow addresses from a particular memory region. A packet length might have a legal range. A write transaction might require data while a read transaction does not.

Constraints define the legal space in which randomization can operate.

Core idea: Randomization chooses values. Constraints define which values are allowed.

Step 2: Constraint syntax

A constraint block is declared with the constraint keyword:

constraint valid_addr {


addr inside {[32'h1000:32'h1FFF]};
}

The constraint belongs to the class and restricts the values that can be selected when the object is randomized.

A complete example looks like this:

class bus_transaction extends uvm_sequence_item;


rand bit        write;
rand bit [31:0] addr;
rand bit [31:0] data;

constraint valid_addr {
addr inside {[32'h1000:32'h1FFF]};
}

`uvm_object_utils(bus_transaction)

function new(string name = "bus_transaction");
super.new(name);
endfunction

endclass

Now the random solver can choose many different addresses, but only addresses inside the specified range are legal.

Step 3: Simple numeric constraints

The simplest constraints compare a randomized field against a constant.

constraint address_range {


addr >= 32'h1000;
addr <= 32'h1FFF;
}

This means:

1000 <= addr <= 1FFF

You can also constrain numeric values such as packet lengths:

rand int unsigned length;


constraint length_range {
length >= 1;
length <= 1500;
}

Constraints are expressions describing relationships that must be satisfied. They are not procedural statements that execute from top to bottom.

Important: Think of a constraint as a rule, not as an assignment. length <= 1500 does not set length to 1500. It restricts the values that randomization may choose.

Step 4: Use inside for legal sets and ranges

The inside operator is extremely useful for transaction constraints.

For a continuous range:

constraint valid_address {


addr inside {[32'h1000:32'h1FFF]};
}

For a specific set of values:

constraint legal_opcode {


opcode inside {8'h10, 8'h20, 8'h40};
}

You can also combine ranges and individual values:

constraint legal_opcode {


opcode inside {[8'h10:8'h1F], 8'h40, 8'h80};
}

This is often easier to read than writing many separate comparisons.

Step 5: Constrain relationships between fields

Constraints become much more powerful when multiple transaction fields are related.

Consider a packet with a starting address and a length:

rand bit [31:0] start_addr;


rand int unsigned length;

constraint packet_range {
start_addr inside {[32'h1000:32'h1FFF]};
length inside {[1:64]};
}

We can add a relationship so the packet does not cross the end of the memory region:

constraint no_overflow {


start_addr + length <= 32'h2000;
}

Now the solver must find values for both fields that satisfy all of the rules simultaneously.

Verification value: Cross-field constraints let you generate realistic legal scenarios without manually calculating every field in the sequence.

Step 6: Conditional constraints

Different transaction types often have different legal fields.

Suppose a transaction can be either a read or a write:

rand bit        write;


rand bit [31:0] addr;
rand bit [31:0] data;

We can describe different rules depending on the operation:

constraint operation_rules {


if (write) {
data inside {[32'h0000_0000:32'h0000_FFFF]};
}

}

A more complete example might constrain address regions differently:

constraint operation_rules {


if (write) {
addr inside {[32'h1000:32'h17FF]};
}
else {
addr inside {[32'h1800:32'h1FFF]};
}

}

The if/else here is part of the constraint description. It tells the random solver which rules apply to each transaction type.

Step 7: Constraints versus procedural assignments

This distinction is fundamental to constrained-random verification.

Procedural assignment

req.addr = 32'h1000;

This directly assigns one value.

Constraint

constraint valid_addr {


addr inside {[32'h1000:32'h1FFF]};
}

This allows the solver to choose any legal value in the range.

Technique Meaning Typical use
Assignment Choose this exact value Directed stimulus
Constraint Choose any value satisfying these rules Constrained-random stimulus

Good UVM environments use both techniques. Directed tests are useful for precise scenarios, while constraints help explore large legal input spaces.

Step 8: Calling randomize()

Defining rand fields and constraints does not automatically randomize the object. The object must be randomized.

bus_transaction req;


req = bus_transaction::type_id::create("req");

if (!req.randomize()) begin
`uvm_error("RANDFAIL", "Transaction randomization failed")
end

The call to randomize() asks the SystemVerilog constraint solver to find values satisfying all active constraints.

Always consider checking the return value when randomization failure would make the test invalid.

Key point: randomize() returns success or failure. A failure means the solver could not find a legal solution under the active constraints.

Step 9: Understand constraint conflicts

Constraints can accidentally contradict each other.

constraint rule_a {


addr inside {[32'h1000:32'h1FFF]};
}

constraint rule_b {
addr inside {[32'h3000:32'h3FFF]};
}

No address can satisfy both rules at the same time. Therefore, randomization can fail.

Another common mistake is making a legal range too small:

constraint address_rule {


addr >= 32'h1000;
addr <= 32'h0FFF;
}

This describes an impossible condition.

When randomize() fails, inspect the constraints before assuming the simulator or UVM sequence is broken.

Step 10: Inline constraints

Sometimes a base transaction has useful general constraints, but one particular test needs an additional restriction.

SystemVerilog supports an inline constraint:

if (!req.randomize() with {


addr inside {[32'h1200:32'h12FF]};
}) begin
`uvm_error("RANDFAIL", "Inline randomization failed")
end

The class-level constraints still apply. The inline constraint adds another requirement for this particular randomization call.

This is useful when a sequence wants to specialize the legal stimulus without modifying the reusable transaction class.

Design principle: Put broadly valid protocol rules in the transaction class. Use inline constraints for scenario-specific restrictions.

Step 11: Constraint inheritance and reuse

Transaction classes can be extended to create specialized transaction types.

class bus_transaction extends uvm_sequence_item;


rand bit [31:0] addr;
rand bit [31:0] data;

constraint valid_addr {
addr inside {[32'h1000:32'h1FFF]};
}

`uvm_object_utils(bus_transaction)

function new(string name = "bus_transaction");
super.new(name);
endfunction

endclass

A specialized transaction can add more rules:

class secure_bus_transaction extends bus_transaction;


constraint secure_region {
addr inside {[32'h1800:32'h18FF]};
}

`uvm_object_utils(secure_bus_transaction)

function new(string name = "secure_bus_transaction");
super.new(name);
endfunction

endclass

The derived transaction now has both the inherited rules and the additional specialization.

This becomes particularly useful when a verification environment supports multiple protocol modes or transaction types.

Step 12: Constraints should model legal behavior

One of the most important verification decisions is deciding what should be constrained.

If you constrain everything too tightly, the testbench may generate only a tiny subset of legal behavior.

If you constrain too little, the testbench may spend simulation time generating illegal or meaningless transactions.

The goal is not:

"Make every generated transaction look perfect."

The goal is:

"Generate legal, meaningful scenarios while preserving useful variation."
Verification mindset: Constraints define the legal stimulus space. Coverage tells you which parts of that space you have actually explored.

Step 13: Constraint debugging

When randomization fails or produces unexpected stimulus, use a systematic process.

  1. Confirm that the fields you expect to randomize are declared rand.
  2. Read every active constraint affecting those fields.
  3. Check for contradictory ranges or conditions.
  4. Check inline constraints added by the sequence.
  5. Look for hard-coded assignments that conflict with intended randomization.
  6. Temporarily simplify the constraints to identify the rule causing the conflict.
  7. Use simulator constraint-debugging features when available.

A useful mental model is:

Randomization failure
    |
    v


Which field?
|
v
Which constraints affect it?
|
v
Do those constraints have a common solution?
|
+---- No ----> Fix the constraint conflict
|
+---- Yes ---> Investigate another part of the stimulus flow

Step 14: A complete transaction example

The following example combines several concepts:

class bus_transaction extends uvm_sequence_item;


rand bit        write;
rand bit [31:0] addr;
rand bit [31:0] data;

constraint valid_address {
addr inside {[32'h1000:32'h1FFF]};
}

constraint write_data {
if (write) {
data inside {[32'h0000_0000:32'h0000_FFFF]};
}
}

`uvm_object_utils(bus_transaction)

function new(string name = "bus_transaction");
super.new(name);
endfunction

endclass

A sequence can then request a legal randomized transaction:

bus_transaction req;


req = bus_transaction::type_id::create("req");

if (!req.randomize()) begin
`uvm_error("RANDFAIL", "Could not generate legal bus transaction")
end

The resulting transaction satisfies the class constraints while still allowing the solver to choose different legal values.

Step 15: Common constraint mistakes

Mistake 1: Forgetting rand

A field that is not declared appropriately for randomization will not behave as expected when the object is randomized.

Mistake 2: Conflicting constraints

Multiple constraints must be satisfiable together. Adding more rules does not automatically make the stimulus better.

Mistake 3: Over-constraining

If only one or two values can ever be generated, constrained-random stimulus may provide very little variation.

Mistake 4: Under-constraining

If illegal protocol values are allowed, the testbench may spend most of its time generating scenarios that do not represent useful legal behavior.

Mistake 5: Treating constraints like assignments

Constraints describe allowable solutions. They do not execute sequentially like procedural code.

Mistake 6: Ignoring randomization failure

If randomize() fails, do not continue as though a valid transaction was generated.

Step 16: Directed versus constrained-random stimulus

Directed Constrained-random
Exact values chosen by the test Values selected by the solver
Highly predictable Provides variation
Excellent for specific corner cases Excellent for exploring legal input spaces
Easy to reproduce Can expose unexpected combinations
May require many manually written scenarios One constraint model can produce many scenarios

Professional verification environments usually use both. Constraints do not replace directed testing; they complement it.

Step 17: The bigger UVM picture

Transaction constraints fit into the UVM stimulus pipeline:

Test


|
v
Sequence
|
| creates and randomizes
v
Sequence Item
|
| constrained transaction
v
Sequencer
|
v
Driver
|
| pin-level protocol
v
DUT
|
v
Monitor
|
| observed transaction
v
Scoreboard / Coverage

Constraints therefore influence what the driver eventually sends to the DUT. Good constraint design directly affects the quality of the scenarios explored during simulation.

Try it yourself

Create a constrained-random fifo_item for the FIFO environment introduced in Lesson 28.

Include fields such as:

rand bit        write;


rand bit [7:0]  data;

Then create constraints for the following requirements:

  1. The data value must be between 8'h10 and 8'hEF.
  2. A write transaction should use the generated data value.
  3. A read transaction should not require a meaningful write-data value.
  4. The transaction should remain legal for both reads and writes.

Call randomize() several times and inspect the resulting values.

Challenge: Add an operation field with three possible operations: WRITE, READ, and RESET. Then create conditional constraints so that each operation has appropriate transaction data.
Debug challenge: Create two intentionally conflicting constraints for the same field. Observe that randomize() fails, then remove one constraint and verify that randomization succeeds.

Key takeaways

  • rand fields allow transaction data to participate in constrained-random generation.
  • Constraints define the legal values and relationships the solver must satisfy.
  • Use ranges and inside to describe legal value spaces clearly.
  • Constraints can express relationships between multiple transaction fields.
  • Conditional constraints let different transaction types follow different rules.
  • A constraint is a rule, not a procedural assignment.
  • Use randomize() to ask the solver for a legal transaction.
  • Always take randomization failure seriously.
  • Class-level constraints should describe broadly valid rules; inline constraints can specialize a particular scenario.
  • Over-constraining reduces useful variation, while under-constraining can produce invalid or meaningless stimulus.
  • Constraints define the legal stimulus space; coverage tells you how much of that space you have explored.
  • The next lesson will focus on inspecting, printing, comparing, and debugging transactions in a UVM environment.