Module 4: Stimulus and Drivers | Lesson 39

Constrained-Random Stimulus

Learn how to generate realistic randomized transactions while keeping stimulus legal, meaningful, reproducible, and useful for finding corner-case bugs.

Learning objectives

  • Explain why constrained-random stimulus is useful in UVM verification.
  • Distinguish randomization from constraints and from directed stimulus.
  • Write a sequence that generates constrained-random transactions.
  • Use inline constraints to target specific scenarios without rewriting the transaction class.
  • Check randomization failures and debug unexpected stimulus.
  • Understand how random stimulus complements coverage and checking.

Step 1: Why constrained-random stimulus?

Directed stimulus is excellent when you know exactly which scenario you want to test. But real designs can have an enormous number of possible transaction combinations. Writing every combination manually is slow and often misses unexpected corner cases.

Constrained-random stimulus solves this by allowing SystemVerilog to choose transaction values automatically while constraints define which values are legal or useful.

The basic idea is:

Randomization chooses the values.
Constraints define the allowed values.

For example, a bus transaction might allow any address from 0 to 255, but perhaps writes to address 8'hFF are reserved. The transaction can be randomized while the constraint prevents illegal traffic.

class bus_transaction extends uvm_sequence_item;

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

constraint legal_addr_c {
    addr inside {[8'h00:8'hFE]};
}


endclass

Now every successful call to randomize() produces a legal address, while the actual value can change from transaction to transaction.

Step 2: Directed vs. constrained-random stimulus

These approaches are not competitors. A strong verification environment normally uses both.

Directed stimulus Constrained-random stimulus
Exact values are chosen by the test Values are generated automatically
Highly predictable Explores many combinations
Excellent for specific requirements Excellent for broad exploration
Useful for reproducing a known bug Useful for finding unexpected bugs
Usually easy to read Requires constraints and debug information

A practical strategy is to start with directed smoke tests, then introduce constrained-random traffic to explore a much larger state space.

Step 3: Add constraints to the transaction

Constrained-random stimulus begins with a transaction containing rand fields and one or more constraints.

class bus_transaction extends uvm_sequence_item;

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

constraint legal_addr_c {
    addr inside {[8'h00:8'hFE]};
}

constraint useful_data_c {
    data != 8'h00;
}

`uvm_object_utils(bus_transaction)

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


endclass

The rand keyword tells SystemVerilog that a field may be randomized. The constraints restrict the solution space.

Constraints should describe legal or useful relationships, not implement the DUT's behavior. The DUT must still be responsible for producing the actual result.

Step 4: Randomize a transaction inside a sequence

The sequence creates the transaction, requests it from the sequencer, randomizes it, and sends it to the driver.

class random_bus_sequence
extends uvm_sequence #(bus_transaction);

`uvm_object_utils(random_bus_sequence)

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

task body();

    bus_transaction req;

    repeat (10) begin

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

        start_item(req);

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

        finish_item(req);

    end

endtask


endclass

Notice that the sequence does not drive valid, ready, clocks, or other DUT pins. It creates transaction intent.

The driver remains responsible for translating each transaction into legal protocol activity.

Sequence: What traffic should be generated?
Sequencer: Which sequence item should reach the driver?
Driver: How should that transaction appear on the DUT interface?

Step 5: Understand the randomization result

randomize() returns a status indicating whether SystemVerilog found a solution satisfying the constraints.

if (!req.randomize()) begin
`uvm_error("RANDFAIL", "No legal transaction could be generated")


end

A failure usually means the constraints are contradictory or overly restrictive. For example:

constraint impossible_c {
addr == 8'h10;
addr == 8'h20;


}

No value can satisfy both conditions, so randomization fails.

Treat randomization failure as a real verification problem. Do not silently continue with an invalid or partially initialized transaction.

Step 6: Use inline constraints for targeted randomness

Sometimes the base transaction constraints are correct, but a particular test needs to explore a smaller region.

SystemVerilog supports inline constraints for this purpose.

start_item(req);


if (!req.randomize() with {
write == 1'b1;
addr inside {[8'h20:8'h2F]};
data inside {8'h00, 8'hFF};
}) begin
`uvm_error("RANDFAIL",
"Inline constrained randomization failed")
end

finish_item(req);

This lets the sequence say: "Generate a random transaction, but keep it inside this scenario."

Inline constraints are especially useful for boundary testing, register ranges, protocol modes, error injection, and other targeted scenarios.

Step 7: Make random stimulus meaningful

Random does not automatically mean useful. A test that generates millions of transactions without checking meaningful scenarios may produce a lot of simulation activity but little verification value.

Good constrained-random stimulus usually has three parts:

  1. Legal stimulus: transactions obey protocol and design rules.
  2. Interesting stimulus: constraints encourage important cases and boundaries.
  3. Observable results: monitors, scoreboards, assertions, and coverage determine what happened.

This creates the verification loop:

Generate


↓
Drive
↓
Observe
↓
Check
↓
Measure coverage
↓
Improve stimulus

Coverage can reveal that random stimulus is spending most of its time in easy cases. You can then refine constraints or create targeted sequences to reach the missing cases.

Step 8: Avoid over-constraining

One of the most important skills in constrained-random verification is knowing when not to add another constraint.

Suppose a bus supports addresses from 0x00 through 0xFF. If you accidentally constrain every transaction to 0x20, randomization still succeeds—but your test is no longer exploring the address space.

This is called over-constraint.

The opposite problem is under-constraint: the random stimulus can generate combinations that are illegal or irrelevant to the scenario.

Good constraints narrow the search space without destroying useful variation.

Ask yourself:

  • Is this value always illegal?
  • Is this relationship required by the protocol?
  • Am I accidentally preventing important corner cases?
  • Does coverage show that my stimulus is exploring the intended space?

Step 9: Make random failures reproducible

Random stimulus is powerful because it explores many possibilities, but a failing test must still be debuggable.

When a randomized test fails, record enough information to reproduce the failure:

  • Simulation seed
  • Transaction values
  • Sequence being executed
  • Relevant configuration
  • First transaction that produced unexpected behavior

Printing transactions is often useful during early development:

`uvm_info("SEQ",
$sformatf("Generated transaction:\n%s", req.sprint()),
UVM_MEDIUM)

Once the failure is understood, a random failure can often be converted into a directed regression test using the failing transaction values.

Random finds the bug. Reproducibility lets you debug the bug.

Step 10: A practical constrained-random sequence

The following sequence combines repeated randomization with an occasional targeted scenario.

class bus_random_sequence
extends uvm_sequence #(bus_transaction);

`uvm_object_utils(bus_random_sequence)

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

task body();

    bus_transaction req;

    // General constrained-random traffic
    repeat (20) begin

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

        start_item(req);

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

        finish_item(req);

    end

    // Targeted random traffic
    repeat (5) begin

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

        start_item(req);

        if (!req.randomize() with {
            addr inside {8'h00, 8'h01, 8'hFE, 8'hFF};
            data inside {8'h00, 8'hFF};
        }) begin
            `uvm_error("RANDFAIL",
                "Boundary randomization failed")
        end

        finish_item(req);

    end

endtask


endclass

This pattern is useful because most traffic explores the normal legal space while a smaller portion deliberately targets important boundary values.

Step 11: Constrained-random stimulus in the complete flow

The complete path should now be familiar:

Sequence


│
│ randomize()
▼
Transaction
│
▼
Sequencer
│
▼
Driver
│
│ protocol activity
▼
DUT
│
▼
Monitor
│
▼
Scoreboard / Coverage / Assertions

The important separation is that randomization happens at the transaction level. The driver still owns protocol timing, and the monitor still reconstructs what actually happened on the interface.

This separation allows the same driver and monitor to be reused with directed sequences, constrained-random sequences, and later more advanced scenarios.

Common mistakes

  • Randomizing without constraints: the generated traffic may not represent meaningful legal scenarios.
  • Over-constraining: randomization succeeds, but important parts of the state space are never explored.
  • Ignoring randomization failure: always check the return value of randomize().
  • Putting protocol timing in the sequence: sequences should describe transaction intent; drivers should implement protocol timing.
  • Assuming random means coverage: random traffic does not guarantee that important functional scenarios were exercised.
  • Making failures impossible to reproduce: preserve the seed and transaction information needed for debugging.
  • Using random stimulus before the basic environment works: first prove the driver, monitor, and scoreboard with simple directed traffic.

Debugging constrained-random stimulus

When a randomized test behaves unexpectedly, debug from the transaction outward.

  1. Print the generated transaction.
  2. Check whether randomization succeeded.
  3. Verify that the constraints describe the intended legal space.
  4. Confirm that the sequencer delivered the item.
  5. Confirm that the driver translated it correctly.
  6. Check what the monitor observed.
  7. Compare the observed result with the expected result.
  8. Use coverage to determine whether the stimulus is exploring useful scenarios.

If the generated transaction is wrong, investigate randomization and constraints. If the transaction is correct but the pins are wrong, investigate the driver. If the pins are correct but the scoreboard sees something different, investigate the monitor or transaction reconstruction.

Try it yourself

Create a bus_random_sequence that generates 20 transactions. Constrain the address to 8'h10 through 8'h7F and prevent zero-valued data.

  1. Print each generated transaction.
  2. Check the return value of randomize().
  3. Add an inline constraint that randomly selects only boundary addresses.
  4. Run the test more than once and compare the generated transactions.
  5. Record the seed when a failure occurs and try to reproduce it.

Challenge

Extend the sequence so that approximately half of the transactions are writes and half are reads. Then create a targeted portion of the sequence that focuses on boundary addresses.

Ask yourself: How would you know whether the random stimulus actually improved verification?

The answer should involve more than transaction count. Look at functional coverage, assertions, scoreboard results, and the scenarios that were actually exercised.

Key takeaways

  • Constrained-random stimulus automatically explores many legal transaction combinations.
  • rand fields define what can be randomized; constraints define what values are allowed.
  • Use inline constraints to target specific scenarios without changing the base transaction model.
  • Do not confuse random stimulus with meaningful verification—checking and coverage provide the evidence.
  • Avoid both over-constraining and under-constraining the stimulus space.
  • Keep transaction intent in sequences and protocol implementation in drivers.
  • Always check randomization success and preserve enough information to reproduce failures.
  • Directed and constrained-random stimulus work together: directed tests target known scenarios, while random stimulus explores combinations you may not have anticipated.