Lesson 12: UVM Sequences and Virtual Sequences

A UVM sequence is a class whose body() makes and sends items, and it can be randomized like any object: its own rand fields, such as how many writes and how many idle clocks between them, describe a whole kind of traffic. Sequences can start other sequences, so traffic is built in layers. A virtual sequence goes one step further: it runs on a virtual sequencer that holds handles to several real sequencers, and starts sequences on each, in order or in parallel. That is how one test coordinates several interfaces, such as the write side and the read side of a FIFO.

Learning objectives

  • Write a sequence with rand fields and constraints, and randomize it with inline constraints before starting it
  • Build traffic in layers, with sequences that start other sequences, and keep a burst together with lock()
  • Coordinate two agents with a virtual sequencer and a virtual sequence, in series and in parallel
  • Write a sequence that reads coverage and steers the stimulus toward what is missing

Prerequisites

  • Lesson 11: items, sequences, the sequencer and driver handshake, and the FIFO environment reused here
  • Lesson 5: threads, fork and semaphores, which lock() resembles
  • The running example is the synchronous FIFO: 16 entries of 8 bits, one clock, flags full and empty and an occupancy count; writes when full are dropped, reads when empty are ignored, and read data appears one clock after the read. Its shared testbench code is in the site's repository under scripts/verification/fifo, and every example runs in AMD Vivado xsim 2025.2.

Sequences with their own random fields

fifo_wr_seq makes n writes, each followed by gap idle clocks, and both are rand fields with constraints. A test or another sequence picks the kind of traffic with an inline constraint before starting it. Each item is still sent with start_item and finish_item, so the driver handshake from Lesson 11 is unchanged.

fifo/fifo_uvm_vseq.svh: the write-side sequence
class fifo_wr_seq extends uvm_sequence #(fifo_item);
  `uvm_object_utils(fifo_wr_seq)
  rand int unsigned n;                 // writes to make
  rand int unsigned gap;               // idle clocks after each write
  constraint c_sizes { n inside {[1:64]}; gap inside {[0:3]}; }
  function new(string name = "fifo_wr_seq"); super.new(name); endfunction
  task body();
    repeat (n) begin
      send(1);
      repeat (gap) send(0);
    end
  endtask
  task send(bit active);
    fifo_item it = fifo_item::type_id::create("it");
    start_item(it);
    if (!it.randomize() with { txn.wr_en == active; txn.rd_en == 0; }) `uvm_fatal("SEQ", "randomize failed")
    finish_item(it);
  endtask
endclass
Sequence diagram with sequence, sequencer and driver lifelines: get_next_item, start_item, finish_item, item_done
Every item of every sequence still goes through the same handshake: start_item, randomize, finish_item, and the driver's item_done.

Two agents and a virtual sequencer

The FIFO's write side and read side get one agent each: a sequencer and a driver that drives only its own pins. One monitor still watches both, so Lesson 11's scoreboard and coverage subscriber are reused unchanged. The virtual sequencer runs no items; it only holds a handle to each real sequencer, which the environment fills in at connect_phase.

fifo/fifo_uvm_vseq.svh: the virtual sequencer
// The virtual sequencer runs no items itself: it holds a handle to each real sequencer.
class fifo_vsequencer extends uvm_sequencer;
  `uvm_component_utils(fifo_vsequencer)
  uvm_sequencer #(fifo_item) wr_sqr, rd_sqr;
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
endclass
A virtual sequencer driving write and read sequencers of two FIFO agents, with monitor and scoreboard
The virtual sequencer starts a write sequence and a read sequence on the two real sequencers. Each driver drives its own pins of fifo_if; the monitor samples them all and feeds the scoreboard and coverage.

Virtual sequences

A virtual sequence declares its virtual sequencer with `uvm_declare_p_sequencer, which gives it a typed handle, p_sequencer, to reach the real sequencers. Starting a sub-sequence one after the other runs phases in order; starting two inside fork and join runs them at the same time, which is the only way to get a write and a read on the same clock from two agents.

fifo/fifo_uvm_vseq.svh: the base virtual sequence
// Virtual sequences: they start the side sequences on the real sequencers, in order or in parallel.
class fifo_base_vseq extends uvm_sequence;
  `uvm_object_utils(fifo_base_vseq)
  `uvm_declare_p_sequencer(fifo_vsequencer)
  function new(string name = "fifo_base_vseq"); super.new(name); endfunction
  // The arguments are named count and idle, not n and gap: inside randomize() with { }, a bare n would mean the
  // sequence's own field, and the constraint would compare it with itself.
  task write(int unsigned count, int unsigned idle = 0);
    fifo_wr_seq s = fifo_wr_seq::type_id::create("wr");
    if (!s.randomize() with { n == count; gap == idle; }) `uvm_fatal("VSEQ", "randomize failed")
    s.start(p_sequencer.wr_sqr, this);
  endtask
  task read(int unsigned count, int unsigned idle = 0);
    fifo_rd_seq s = fifo_rd_seq::type_id::create("rd");
    if (!s.randomize() with { n == count; gap == idle; }) `uvm_fatal("VSEQ", "randomize failed")
    s.start(p_sequencer.rd_sqr, this);
  endtask
endclass
fifo/fifo_uvm_vseq.svh: fill, drain, then both sides at once
// Fill, drain, then both sides at once at different rates: writes every clock, reads every other clock, so the
// FIFO climbs to full and stays there with reads and writes together.
class fifo_phased_vseq extends fifo_base_vseq;
  `uvm_object_utils(fifo_phased_vseq)
  function new(string name = "fifo_phased_vseq"); super.new(name); endfunction
  task body();
    `uvm_info("VSEQ", "fill: 16 writes", UVM_NONE)
    write(16);
    `uvm_info("VSEQ", "drain: 16 reads", UVM_NONE)
    read(16);
    `uvm_info("VSEQ", "both sides at once: 64 writes, 32 reads one clock apart", UVM_NONE)
    fork
      write(64);
      read(32, 1);
    join
    `uvm_info("VSEQ", "drain: 40 reads", UVM_NONE)
    read(40);
  endtask
endclass

Worked example: phases that find planted bug 2

The test starts the phased virtual sequence on the virtual sequencer. In the both-sides phase the write side writes every clock while the read side reads every other clock, so the FIFO climbs to full and stays there with writes and reads together: exactly where planted bug 2 lives.

fifo/fifo_uvm_vseq.svh: the test
class fifo_vseq_test extends uvm_test;
  `uvm_component_utils(fifo_vseq_test)
  fifo_two_side_env env;
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
  function void build_phase(uvm_phase phase);
    env = fifo_two_side_env::type_id::create("env", this);
  endfunction
  task run_phase(uvm_phase phase);
    fifo_phased_vseq v = fifo_phased_vseq::type_id::create("v");
    phase.raise_objection(this);
    v.start(env.vsqr);
    repeat (3) @(posedge env.wr_drv.vif.clk);
    phase.drop_objection(this);
  endtask
endclass
xsim output: the real FIFO
UVM_INFO fifo_uvm_vseq.svh(147) @ 0: uvm_test_top.env.vsqr@@v [VSEQ] fill: 16 writes
UVM_INFO fifo_uvm_vseq.svh(149) @ 180000: uvm_test_top.env.vsqr@@v [VSEQ] drain: 16 reads
UVM_INFO fifo_uvm_vseq.svh(151) @ 340000: uvm_test_top.env.vsqr@@v [VSEQ] both sides at once: 64 writes, 32 reads one clock apart
UVM_INFO fifo_uvm_vseq.svh(156) @ 980000: uvm_test_top.env.vsqr@@v [VSEQ] drain: 40 reads
UVM_INFO fifo_uvm.svh(124) @ 1405000: uvm_test_top.env.cov [COV] level 100.0%, op 100.0%, level x op 70.0%
UVM_INFO fifo_uvm.svh(112) @ 1405000: uvm_test_top.env.scb [SCB] 139 clocks checked, 0 errors
UVM_ERROR :    0
[VSEQ]     4
xsim output: built with -d BUG=2 (the first errors and the summary)
UVM_ERROR fifo_uvm.svh(108) @ 665000: uvm_test_top.env.scb [SCB] clock 65: full is 1 with 15 entries
UVM_ERROR fifo_uvm.svh(108) @ 685000: uvm_test_top.env.scb [SCB] clock 67: full is 1 with 15 entries
UVM_INFO fifo_uvm.svh(112) @ 1405000: uvm_test_top.env.scb [SCB] 139 clocks checked, 33 errors
UVM_ERROR :   33

The phased sequence reached the bug at clock 65, during the both-sides phase, and the run ended with 33 errors. Lesson 11's random traffic found the same bug at clock 326. Random stimulus still matters for what nobody thought of; a virtual sequence aims straight at a corner the plan names.

Common mistakes

  • Writing randomize() with { n == n; }: inside the braces a bare n means the sequence's own field, so the constraint compares it with itself and n stays random. Give the outside value another name, as write(count) does, or use local::.
  • Starting items directly on the virtual sequencer: it has no driver. Start sub-sequences on the real sequencers through p_sequencer.
  • Running both sides with a sequence each but in series, when the bug needs a write and a read on the same clock.
  • Forgetting unlock() after lock(), or calling lock() inside a sequence that never finishes, so every other sequence on that sequencer waits forever.
  • Creating components in connect_phase instead of build_phase: UVM stops the run with a fatal error.

Quick check

1. What does a virtual sequencer contain?

Handles to the real sequencers. It runs no items of its own; virtual sequences use the handles to start sequences on each agent.

2. Why does the both-sides phase use fork and join?

Each sequence runs on its own sequencer, and finish_item blocks until the item is driven. Started in series, the read sequence would only begin after all the writes; started in fork, both drivers get items on the same clocks.

3. What did lock() change in E3's output?

Without it the two write sequences took turns item by item (1 9 1 9 ...). With it, once the locking sequence got the sequencer, its four writes of 9 went out together.

Exercises

Graded from easy to hard. Try each one before you open the solution; every solution was run in xsim 2025.2, and its output is shown.

E1 (easy). Randomize a write sequence to between 5 and 8 writes with one idle clock after each, and run it.

Inline constraints on the sequence's own fields, before start.

fifo_vseq_ex_tb.sv: E1
// E1: randomize a sequence's own fields with constraints before starting it.
class fifo_e1_vtest extends fifo_ex_vtest;
  `uvm_component_utils(fifo_e1_vtest)
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
  task body();
    fifo_wr_seq s = fifo_wr_seq::type_id::create("s");
    if (!s.randomize() with { n inside {[5:8]}; gap == 1; }) `uvm_fatal("E1", "randomize failed")
    `uvm_info("E1", $sformatf("n = %0d writes, gap = %0d idle clock after each", s.n, s.gap), UVM_NONE)
    s.start(env.wr_sqr);
  endtask
endclass
xsim output
UVM_INFO fifo_vseq_ex_tb.sv(30) @ 0: uvm_test_top [E1] n = 6 writes, gap = 1 idle clock after each
E2 (easy). Write a sequence that runs three bursts of four writes by starting another sequence three times.

A sequence can start sub-sequences on its own sequencer, m_sequencer, passing itself as the parent.

fifo_vseq_ex_tb.sv: E2
// E2: a sequence that runs other sequences on its own sequencer: three bursts of four writes.
class fifo_three_bursts_seq extends uvm_sequence #(fifo_item);
  `uvm_object_utils(fifo_three_bursts_seq)
  function new(string name = "fifo_three_bursts_seq"); super.new(name); endfunction
  task body();
    repeat (3) begin
      fifo_wr_seq b = fifo_wr_seq::type_id::create("burst");
      if (!b.randomize() with { n == 4; gap == 0; }) `uvm_fatal("E2", "randomize failed")
      b.start(m_sequencer, this);
      `uvm_info("E2", "one burst of 4 writes done", UVM_NONE)
    end
  endtask
endclass

class fifo_e2_vtest extends fifo_ex_vtest;
  `uvm_component_utils(fifo_e2_vtest)
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
  task body();
    fifo_three_bursts_seq s = fifo_three_bursts_seq::type_id::create("s");
    s.start(env.wr_sqr);
  endtask
endclass
xsim output
UVM_INFO fifo_vseq_ex_tb.sv(44) @ 60000: uvm_test_top.env.wr_sqr@@s [E2] one burst of 4 writes done
UVM_INFO fifo_vseq_ex_tb.sv(44) @ 100000: uvm_test_top.env.wr_sqr@@s [E2] one burst of 4 writes done
UVM_INFO fifo_vseq_ex_tb.sv(44) @ 140000: uvm_test_top.env.wr_sqr@@s [E2] one burst of 4 writes done
E3 (medium). Two write sequences share one sequencer. Make one of them send its four writes together.

lock() gives one sequence the sequencer until unlock(); without it the sequencer arbitrates item by item.

fifo_vseq_ex_tb.sv: E3
// E3: two write sequences share the write sequencer; one locks it so its four writes go out together.
class fifo_data_seq extends uvm_sequence #(fifo_item);
  `uvm_object_utils(fifo_data_seq)
  bit [7:0] value;
  int unsigned n;
  bit use_lock;
  function new(string name = "fifo_data_seq"); super.new(name); endfunction
  task body();
    if (use_lock) lock();
    repeat (n) begin
      fifo_item it = fifo_item::type_id::create("it");
      start_item(it);
      if (!it.randomize() with { txn.wr_en == 1; txn.rd_en == 0; txn.data == value; }) `uvm_fatal("E3", "randomize failed")
      finish_item(it);
    end
    if (use_lock) unlock();
  endtask
endclass
xsim output
UVM_INFO fifo_vseq_ex_tb.sv(111) @ 135000: uvm_test_top [E3] without lock(): 1 9 1 9 1 9 1 9 1 1 
UVM_INFO fifo_vseq_ex_tb.sv(110) @ 345000: uvm_test_top [E3] with lock(): 1 9 9 9 9 1 1 1 1 1 
E4 (hard). Write a virtual sequence that reads the coverage and, with the FIFO full, runs whichever operation the full row still lacks.

Give the sequence a handle to the covergroup's hit counts and to the interface. Each step tops the FIFO up to full first, then runs an idle clock, a write, both sides at once or a read, whichever bin is still empty.

fifo_vseq_ex_tb.sv: E4
// E4: a virtual sequence that reads the coverage and, with the FIFO full, runs whichever operation the full row is
// still missing: an idle clock, a write alone, a read alone (then a write to refill), or both together.
class fifo_hover_full_vseq extends fifo_base_vseq;
  `uvm_object_utils(fifo_hover_full_vseq)
  fifo_coverage c;
  virtual fifo_if vif;
  int steps;
  function new(string name = "fifo_hover_full_vseq"); super.new(name); endfunction
  task body();
    write(16);
    while (steps < 20 && (c.hits[4][0] == 0 || c.hits[4][1] == 0 || c.hits[4][2] == 0 || c.hits[4][3] == 0)) begin
      steps++;
      while (!vif.full) write(1);                    // top up: every step starts from a full FIFO
      if (c.hits[4][0] == 0) @(negedge vif.clk);
      else if (c.hits[4][2] == 0) write(1);
      else if (c.hits[4][3] == 0) fork write(1); read(1); join
      else read(1);
      repeat (2) @(negedge vif.clk);
    end
    `uvm_info("E4", $sformatf("after %0d steps the full row has idle %0d, read %0d, write %0d, both %0d", steps,
              c.hits[4][0], c.hits[4][1], c.hits[4][2], c.hits[4][3]), UVM_NONE)
    read(20);
  endtask
endclass
xsim output
UVM_INFO fifo_vseq_ex_tb.sv(137) @ 280000: uvm_test_top.env.vsqr@@v [E4] after 3 steps the full row has idle 2, read 1, write 2, both 1

Interview questions

  • Q1. What is a virtual sequence for? Answer: coordinating stimulus across several agents from one place. It runs on a virtual sequencer that holds handles to the real sequencers and starts sequences on them, in series or in parallel with fork.
  • Q2. How do you stop another sequence from interleaving with a burst? Answer: lock() (or grab()) the sequencer for the length of the burst and unlock() after. The sequencer then gives that sequence every slot until it is released.
  • Q3. Directed sequences found a bug faster than random traffic here. Why keep random traffic at all? Answer: a directed sequence only reaches the corners someone thought of. Random traffic finds the ones nobody listed, and coverage tells you which corners it missed so directed sequences can close them.

Summary

  • A sequence is a randomizable object: its rand fields and constraints describe a kind of traffic.
  • Sequences start other sequences, and lock() keeps a burst together on a shared sequencer.
  • A virtual sequencer holds handles to the real sequencers; a virtual sequence coordinates them in series or with fork.
  • The FIFO's phased virtual sequence found planted bug 2 at clock 65.

Related

Sources

Last verified

Run in Vivado xsim 2025.2 with its bundled UVM 1.2 on 27 November 2026: the phased virtual sequence on the real FIFO and on planted bug 2, and all four exercise tests. Outputs are copied from those runs and kept in scripts/verification/results; lib/verificationCourse.test.js checks them against this page.

Keep going