Module 4: Stimulus and Drivers | Lesson 32

Sequence Body Patterns

Learn how to structure body() so UVM sequences generate clear, reusable, deterministic, and constrained-random verification scenarios.

Learning objectives

  • Understand the role of body() in a UVM sequence.
  • Generate one or many sequence items using common loop patterns.
  • Separate transaction generation from reusable sequence behavior.
  • Use deterministic and constrained-random stimulus appropriately.
  • Use inline constraints for scenario-specific randomization.
  • Insert meaningful delays or synchronization between transactions when required.
  • Create helper tasks for readable and reusable sequence code.
  • Recognize common sequence-body mistakes and debug them systematically.

Step 1: What does body() do?

The body() task is the main execution point of a UVM sequence. It describes the stimulus scenario that the sequence will generate.

A simple sequence might generate one transaction:

task body();

bus_transaction req;

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

start_item(req);

req.write = 1;
req.addr  = 8'h10;
req.data  = 32'h55;

finish_item(req);


endtask

More realistic sequences usually generate many transactions and may organize them into meaningful phases such as reset, configuration, normal traffic, and corner-case traffic.

Core idea: body() is where a sequence describes the scenario, not the low-level protocol implementation.

Step 2: The basic sequence-item pattern

The most important pattern to recognize is:

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


start_item(req);

// Configure or randomize the item.

finish_item(req);

The sequence creates an item, waits for the sequence-item handshake, prepares the transaction, and finishes the item.

Once the item is completed, the sequencer can make it available to the driver.

This pattern will appear repeatedly throughout UVM code, so it is worth becoming comfortable reading it.

Step 3: Generate several transactions with repeat

One of the simplest sequence-body patterns is a repeat loop.

task body();

bus_transaction req;

repeat (4) begin

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

    start_item(req);

    req.write = 1;
    req.addr  = 8'h20;
    req.data  = $urandom();

    finish_item(req);

end


endtask

This generates four write transactions.

Notice that the address is deterministic while the data is randomized. This is often useful when you want to stress data behavior without changing the location being tested.

Beginner pattern: Change one dimension of the stimulus at a time. For example, keep the address fixed while randomizing data. This makes failures easier to understand.

Step 4: Use a for loop when the index matters

A for loop is useful when the transaction values depend on the transaction number.

task body();

bus_transaction req;

for (int i = 0; i < 8; i++) begin

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

    start_item(req);

    req.write = 1;
    req.addr  = i;
    req.data  = i * 10;

    finish_item(req);

end


endtask

The sequence now generates a predictable pattern:

address   data


---

0         0
1         10
2         20
3         30
...
7         70

Deterministic patterns like this are excellent during early testbench development because the expected behavior is easy to predict.

Step 5: Randomize the whole transaction

Once the deterministic path works, constrained-random stimulus can make the sequence much more powerful.

task body();

bus_transaction req;

repeat (20) begin

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

    start_item(req);

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

    finish_item(req);

end


endtask

The transaction's rand fields and constraints determine the legal values.

The sequence determines how many transactions are generated and how they are organized into a scenario.

Think in layers:
Constraints → legal values
Sequence → scenario and ordering
Driver → protocol timing

Step 6: Use inline constraints for a specific scenario

Sometimes the transaction's normal constraints are useful, but a particular sequence needs a narrower condition.

You can use an inline constraint during randomization:

start_item(req);


if (!req.randomize() with {
write == 1;
addr inside {[8'h40:8'h4F]};
}) begin
`uvm_fatal("RANDFAIL",
"Could not randomize constrained write")
end

finish_item(req);

The transaction class can keep its reusable base constraints while the sequence adds scenario-specific restrictions.

This is useful for scenarios such as:

  • write-only traffic,
  • access to a particular address range,
  • boundary addresses,
  • specific packet sizes,
  • error or corner-case conditions.

Step 7: Build a meaningful scenario

A good sequence is often more than a random stream. It can represent a recognizable verification scenario.

For example, a simple register sequence might do this:

task body();

// 1. Write configuration.
send_write(8'h10, 32'h0000_0001);

// 2. Write test data.
send_write(8'h20, 32'h0000_0055);

// 3. Read the result.
send_read(8'h20);


endtask

This style is much easier to understand than a large block of unrelated transaction assignments.

The names of the operations communicate the intent of the scenario.

Step 8: Use helper tasks to reduce duplication

If a sequence repeatedly performs the same type of operation, a helper task can make the sequence easier to read.

task send_write(bit [7:0] addr,
            bit [31:0] data);

bus_transaction req;

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

start_item(req);

req.write = 1;
req.addr  = addr;
req.data  = data;

finish_item(req);


endtask

The main scenario can then become:

task body();

send_write(8'h10, 32'h0000_0001);
send_write(8'h20, 32'h0000_0055);
send_write(8'h30, 32'h0000_00AA);


endtask

The sequence is now communicating the scenario directly: write these three values to these three locations.

Good abstraction: If you have to repeat the same sequence-item setup several times, consider whether a helper task can express the operation more clearly.

Step 9: Add synchronization only when the scenario requires it

Sequences can contain time-consuming activity, because body() is a task.

For example, a sequence may need to wait between two operations:

task body();

send_write(8'h10, 32'h1);

#100ns;

send_write(8'h20, 32'h2);


endtask

However, be careful about using arbitrary delays. A fixed delay is usually less robust than synchronizing to a meaningful protocol or DUT event.

For example, a later sequence design might wait for a transaction completion event rather than simply assuming that #100ns is enough.

Debugging rule: Do not add random delays just to make a failing test pass. Understand what event should determine when the next transaction begins.

Step 10: Deterministic versus constrained-random sequences

Both styles are important in verification.

Deterministic sequence Constrained-random sequence
Known transaction values Values selected by randomization
Easy to reproduce Explores many legal combinations
Excellent for smoke tests Excellent for broader exploration
Useful for debugging Useful for finding unexpected corner cases
Simple expected behavior Requires constraints and coverage discipline

A professional verification environment normally uses both.

A useful progression is:

Directed stimulus
   |
   v


Known-good sequence
|
v
Constrained-random sequence
|
v
Coverage-driven stimulus

Step 11: Avoid putting everything into one giant body()

As sequences become larger, beginners sometimes create one enormous body() task containing hundreds of transactions.

That makes the scenario difficult to read, reuse, and debug.

Prefer meaningful sequence building blocks:

task body();

reset_dut();
configure_device();
send_normal_traffic();
send_boundary_cases();
verify_status();


endtask

These helper operations can themselves generate sequence items. The important point is that the top-level sequence communicates intent.

Think like a verification engineer: When someone reads the sequence six months later, they should be able to understand the test scenario without tracing every signal.

Step 12: Sequence ordering matters

The order of sequence items can be part of the verification requirement.

Consider a simple FIFO scenario:

write A


write B
write C
read
read
read

The ordering is meaningful because the expected read results depend on the writes that came before them.

A different ordering:

read


write A
write B

tests a completely different condition.

Therefore, sequence design is not just about generating random transactions. It is about generating the right temporal scenario.

Step 13: A complete sequence-body example

Here is a small sequence that combines several patterns from this lesson.

class basic_traffic_sequence
extends uvm_sequence #(bus_transaction);

`uvm_object_utils(basic_traffic_sequence)

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

task send_write(bit [7:0] addr,
                bit [31:0] data);

    bus_transaction req;

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

    start_item(req);

    req.write = 1;
    req.addr  = addr;
    req.data  = data;

    finish_item(req);

endtask

task body();

    // Deterministic setup.
    send_write(8'h10, 32'h0000_0001);

    // Repeated randomized traffic.
    repeat (5) begin

        bus_transaction req;

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

        start_item(req);

        if (!req.randomize() with {
            write == 1;
            addr inside {[8'h20:8'h7F]};
        }) begin
            `uvm_fatal("RANDFAIL",
                       "Could not randomize traffic item")
        end

        finish_item(req);

    end

endtask


endclass

This sequence first performs a known configuration write and then generates five constrained-random writes.

That is a simple example of combining deterministic and constrained-random stimulus in one scenario.

Step 14: Common sequence-body mistakes

Mistake 1: Forgetting finish_item()

If a sequence starts an item but never finishes it, the expected transaction flow may not complete.

Mistake 2: Randomizing without checking the result

req.randomize();

This hides randomization failures. Check the return value:

if (!req.randomize()) begin
`uvm_fatal("RANDFAIL", "Randomization failed")


end

Mistake 3: Using random delays to hide synchronization problems

Random delays make failures harder to reproduce and usually hide the real synchronization issue.

Mistake 4: Mixing protocol signals into the sequence

The sequence should generate transaction intent. The driver should implement protocol timing.

Mistake 5: Making one sequence responsible for everything

Large scenarios should be decomposed into reusable operations or smaller sequences as the environment grows.

Mistake 6: Generating random traffic before the basic path works

Random stimulus can make debugging much harder. First prove that one known transaction reaches the DUT correctly.

Step 15: Debug a sequence systematically

When a sequence does not behave as expected, trace the sequence execution in order.

  1. Was the sequence created?
  2. Was the sequence started?
  3. Did body() execute?
  4. Did the expected loop execute the correct number of times?
  5. Was the sequence item created?
  6. Did randomization succeed?
  7. Did start_item() execute?
  8. Did finish_item() execute?
  9. Did the sequencer deliver the item?
  10. Did the driver receive it?

Temporary messages can make sequence progress visible:

`uvm_info("SEQ",
       "Starting traffic sequence",
       UVM_MEDIUM)


`uvm_info("SEQ",
$sformatf("Sending address=%0h data=%0h",
req.addr, req.data),
UVM_MEDIUM)

Once the problem is understood, remove or reduce temporary debugging output rather than leaving the simulation flooded with messages.

Step 16: The sequence-body mental model

A useful way to think about body() is as a small scenario program.

body()


|
+-- choose scenario
|
+-- create item
|
+-- start item
|
+-- configure/randomize item
|
+-- finish item
|
+-- repeat or perform next operation
|
+-- complete scenario

The sequence decides the scenario. The sequence item carries the data. The sequencer manages transfer. The driver implements the protocol.

Core UVM rule: A good sequence is readable at the scenario level. If understanding the scenario requires reading every driver signal assignment, the abstraction boundary is probably wrong.

Try it yourself

Create a fifo_traffic_sequence that generates a simple FIFO scenario.

  1. Generate four deterministic write transactions.
  2. Generate four read transactions.
  3. Use a loop rather than copying the same code eight times.
  4. Create a fresh sequence item for each transaction.
  5. Use start_item() and finish_item().
  6. Print enough information to identify each transaction.
  7. Run the sequence and confirm that the driver receives all transactions in order.
Challenge: Replace the deterministic writes with constrained-random writes, but keep the reads deterministic. Add an inline constraint that restricts write data to values between 32'h0000_0000 and 32'h0000_00FF.

Key takeaways

  • body() is the main execution task of a sequence.
  • Use repeat and for loops to generate multiple transactions.
  • Deterministic sequences are excellent for smoke tests and debugging.
  • Constrained-random sequences explore many legal transaction combinations.
  • Inline constraints let a sequence specialize a reusable transaction for one scenario.
  • Helper tasks can make repeated sequence operations easier to read and maintain.
  • Sequence ordering can be an important part of the verification requirement.
  • Avoid arbitrary delays and keep protocol-specific timing in the driver.
  • Do not make one giant sequence responsible for every possible scenario.
  • Debug the sequence path from creation and body() through the sequencer and driver.