UVM virtual sequence and virtual sequencer complete guide cover: one bench, three agents, a 9-step tour, The Art of Verification

UVM Virtual Sequence and Virtual Sequencer: A Complete Guide With a Working Example

In short: A virtual sequence is a sequence that sends no items of its own. It starts other sequences on the real sequencers of several agents and decides their order: this first, these two together, that one only after both are done. A virtual sequencer is a sequencer with no driver. It just holds handles to those real sequencers, so a virtual sequence can reach them through p_sequencer. You need them as soon as one test has to steer more than one agent.

Why do you need a virtual sequence?

With one agent, life is simple. One sequence runs on one sequencer, which feeds one driver.

Now add a second agent. Say a block has an APB port to set it up and two data ports, A and B. A real test has to program the registers first, then send data on A and B, maybe at the same time, maybe one after the other. No single agent’s sequence can do that, because each one only sees its own sequencer.

That is the job of a virtual sequence. It sits above the agents and says who goes when. In this post I build one bench with three agents and walk through every way to do that, with the logs from real VCS runs.

The picture: think of a film director. The actors (the real sequences) do the actual work, each on their own stage (their sequencer and driver). The director never acts. The director says “lights first, then you two talk at the same time, then you walk in.” The virtual sequence is the director. The virtual sequencer is the director’s phone book: one place that holds the number of every stage.

A film director calling the shots for three actors, each on their own stage, as an analogy for a virtual sequence coordinating three agents
The director never acts: a virtual sequence only says who goes, and when.

What will you learn?

  1. Tell a virtual sequence, a virtual sequencer, m_sequencer and p_sequencer apart.
  2. Build both common styles: handles inside the sequence with start(null), and a virtual sequencer with p_sequencer.
  3. Order traffic across agents: setup first, then agents in parallel, and wait for them before you check.
  4. Nest virtual sequences, and start one by itself with default_sequence.
  5. Keep a group of items together on a busy sequencer with lock(), and know when grab() is the right call.
  6. Spot and debug the four virtual sequence mistakes I see most, each planted in the bench behind a +BUG switch.

What are the parts of a virtual sequence setup?

Five names do all the work. Here they are side by side before we look at each one.

PartWhat it isHas a driver?
Real sequenceMakes items for one agent, for example tav_strm_seqIts sequencer does
Real sequencerHands those items to one driverYes
Virtual sequenceStarts real sequences on several sequencers and sets their orderNo items of its own
Virtual sequencerA sequencer that only holds handles to the real onesNo
p_sequencerA typed handle to the sequencer a sequence runs onn/a
How a virtual sequence reaches three agents: the virtual sequencer holds handles to the APB, A and B sequencers, each feeding its own driver
The virtual sequence runs on tav_vsqr and reaches the real sequencers through p_sequencer.

What is a virtual sequence?

  • How it works: it extends uvm_sequence like any other sequence. Its body() never calls start_item(). It only calls start() on other sequences, each on the sequencer it belongs to.
  • When to use it: any time one test has to steer two or more agents. Setup over APB then data, two ports at once, a reset in the middle of traffic.
  • Why: the order between agents is the test. Putting it in one place makes it easy to read and easy to reuse.
  • Plus: one place to read the whole story of a test. Minus: one more layer to learn and to debug.

What is a virtual sequencer?

  • How it works: a class that extends uvm_sequencer and holds handles, nothing else. The env fills them in during connect_phase. No driver is connected to it.
  • When to use it: when many virtual sequences need the same handles, or you want to start sequences by path (default_sequence, command line).
  • Why: the handles get set once, in the env, instead of in every test.
  • Plus: tests stay short. Minus: a handle that was never set is a null pointer waiting to happen, so check them (my bench does it in end_of_elaboration_phase, see BUG=1).
class tav_vsqr extends uvm_sequencer;
  tav_apb_sqr  apb_sqr;
  tav_strm_sqr a_sqr, b_sqr;
endclass

m_sequencer vs p_sequencer: what is the difference?

  • m_sequencer is set by start() and always has the base type uvm_sequencer_base. You cannot see a_sqr through it.
  • p_sequencer comes from the macro `uvm_declare_p_sequencer(tav_vsqr). When the sequence starts, UVM casts m_sequencer to your type, so p_sequencer.a_sqr works.
  • If you start the sequence on the wrong kind of sequencer, that cast fails and UVM stops with a fatal. That is a feature: it fails loudly at the start, not halfway through. More in m_sequencer vs p_sequencer.

Approach 1 vs approach 2: where do the handles live?

Approach 1: handles in the sequenceApproach 2: virtual sequencer
How you start itvseq.start(null)vseq.start(env.vsqr)
Who sets the handlesthe test, before every startthe env, once
m_sequencernullthe virtual sequencer
Good forsmall benches, one-off testsbig benches, reuse, default_sequence
Watch out forforgetting a handle in one testforgetting a handle in the env (BUG=1), or the wrong one (BUG=4)

Approach 1, in pictures. The handles live inside the virtual sequence itself. Think of them as three sticky notes, each with the address of one real sequencer written on it. The test fills in the notes, then starts the sequence on nothing (start(null)), because it needs no sequencer of its own.

Approach 1: inside the test, the virtual sequence holds three handles, apb_sqr, a_sqr and b_sqr, each pointing at the real sequencer inside an agent; the agents drive the DUT; the test sets the handles and calls start(null)
Approach 1: the handles live inside the virtual sequence, which starts with start(null).
  1. The test copies each real sequencer into the sequence: s1.a_sqr = env.a_agt.sqr;
  2. The test starts it with s1.start(null).
  3. Inside, the sequence starts each real sequence on the sequencer its note points at: tav_vs::send(a_sqr, ...).

Approach 2, in pictures. The sticky notes move into one shared place in the env: the virtual sequencer. The env fills them in once. Any virtual sequence that runs on the virtual sequencer can read them through p_sequencer.

Approach 2: the virtual sequence runs on the virtual sequencer inside the env, which holds the apb_sqr, a_sqr and b_sqr handles; numbered steps show start on the virtual sequencer, the lookup through p_sequencer, the real sequence starting on the real sequencer, and the driver driving the DUT
Approach 2: the env fills a virtual sequencer once; every virtual sequence reads it through p_sequencer.

The only real difference is where the notes live. In approach 1 every test writes them. In approach 2 the env writes them once, and every test just says vseq.start(env.vsqr).

Both are fine. I use approach 2 on anything bigger than a block, because the handles get set in exactly one place.

How does a virtual sequence order traffic across agents?

With plain SystemVerilog. A virtual sequence uses the same tools you already know:

  • One after the other when order matters: setup before data, reset before traffic. In step 3 of my bench, 8 bytes on A and then 8 on B took 320 ns.
  • fork … join when agents should run at the same time. The same 16 bytes took 170 ns, because A and B each have their own sequencer and driver.

The bench also uses join_any (step 4) and join_none with wait fork (step 8), because real virtual sequences need them. How each flavour behaves, with its own timing diagram and examples, is a topic of its own. I keep it in a separate lesson: fork join vs join_any vs join_none in SystemVerilog.

The one rule to carry over: after join_none, always wait fork before you check results or let the test end. BUG=3 below shows what happens if you forget.

Nested virtual sequences

  • How it works: a top virtual sequence starts child virtual sequences on the same virtual sequencer: rk.start(p_sequencer, this). Passing this makes it the parent.
  • When to use it: build a library of small steps (re-key, traffic burst, reset) and string them into tests.
  • Plus: small pieces you can reuse. Minus: deep nesting makes logs harder to follow, so give each child a clear name.

default_sequence: starting a sequence by itself

  • How it works: the test sets a config entry. The sequencer finds it at the start of that phase and starts the sequence by itself.
uvm_config_db#(uvm_object_wrapper)::set(this, "env.vsqr.configure_phase",
                                        "default_sequence", tav_init_vseq::type_id::get());
  • When to use it: a fixed bring-up sequence in configure_phase or reset_phase that every test needs.
  • Watch out: nobody waits for it unless it raises an objection. In UVM 1.2, set_automatic_phase_objection(1) in the sequence’s constructor does that.
  • Minus: it hides the order. Many teams start everything explicitly from the test instead, and I do too, except for bring-up.

lock() and grab(): keeping items together

  • How it works: both give one sequence the sequencer to itself until it calls unlock() or ungrab(). Other sequences wait.
  • lock() gets in line behind what is already waiting. grab() goes to the front of the line. Neither one takes the sequencer away from a lock that is already held.
  • When to use it: a group of items that must not be split, like disable, write key, enable. Use grab() only for something truly urgent, like an interrupt handler.
  • Minus: forget the unlock() and that sequencer is stuck for good.
lock()grab()
Place in lineat the backat the front
Waits for a lock already held?yesyes
Typical useatomic setup stepsinterrupt or error handling
Release withunlock()ungrab()

Here is what that looked like on the APB sequencer in my run. Each box is one transfer, in the order the sequencer sent them:

lock and grab on the APB sequencer in the clean VCS run: without lock, 2 poll reads land between the 3 re-key writes; with lock, the 3 writes stay together; in step 7 the grab gets the sequencer before a poll that asked earlier, after the lock that was already held
lock() and grab() on the APB sequencer, as they ran in steps 6 and 7.

When you do NOT need a virtual sequencer

  • One agent: a normal sequence is enough.
  • Two agents in one small test: approach 1 (start(null)) is less code.
  • Agents that never need to be ordered against each other: start each sequence from the test directly.

What does the example bench look like?

The design is a small merge block, tav_merge. It has an APB port to set it up and two byte streams, A and B, that it merges round robin onto one output. Every byte leaves XORed with a key.

RegisterAddressWhat it does
CTRL0x00bit 0 en. While it is 0, the block takes bytes in and drops them.
KEY0x04every output byte is the input byte XOR KEY
CNT_A0x08read only: bytes from A that came out
CNT_B0x0Cread only: bytes from B that came out

The bench has three agents (APB, A and B), a passive monitor on the output, a scoreboard and the virtual sequencer tav_vsqr.

UVM testbench architecture: the test starts virtual sequences on tav_vsqr, which holds handles to the APB, A and B sequencers; three agents drive tav_merge; monitors feed the scoreboard
The testbench used in this guide: three agents, one virtual sequencer, one scoreboard.

The test is a tour in 9 steps, one idea per step:

StepVirtual sequenceIdea
0tav_init_vseqdefault_sequence: starts by itself in configure_phase. Setup, then traffic.
1tav_handles_vseqapproach 1: handles inside the sequence, start(null)
2tav_p_vseqapproach 2: virtual sequencer and p_sequencer
3tav_order_vseqA then B, against A and B in fork/join
4tav_any_vseqjoin_any, then wait fork
5tav_top_vseqnested: a top sequence starts two child virtual sequences
6tav_lock_vseqa 3-write re-key with and without lock() while another sequence keeps reading
7tav_grab_vseqgrab() goes to the front of the line
8tav_none_vseqjoin_none, other work, wait fork, then check the counters

How do you run it?

  1. Open EDA Playground and sign in (free).
  2. Paste tav_vseq_uvm.sv into testbench.sv and leave design.sv empty.
  3. Pick Synopsys VCS and UVM 1.2. Set the top module to tav_top.
  4. Run once with no option, then with +BUG=1 to +BUG=4 in Run Options.

The same file is in the tav-verification-basics repo on GitHub.

The virtual sequencer, the env wiring and a virtual sequence, all in one place

This is the part people usually have to dig out of five files. Here it is top to bottom, in the order you would write it. It is the same code the bench runs. I only took out the +BUG switches, the lesson’s print and count lines, and the other tour steps, and I wrote the small send helper inline so you can see every call. The full file is further down.

Read it with the approach 2 picture next to it: the five numbered parts below are the boxes and arrows in that picture.

// (1) The real sequence: makes items for ONE agent
class tav_strm_seq extends uvm_sequence #(tav_strm_item);
  `uvm_object_utils(tav_strm_seq)
  int       n = 4;
  bit [7:0] base;
  function new(string name = "tav_strm_seq"); super.new(name); endfunction
  task body();
    for (int i = 0; i < n; i++) begin
      req = tav_strm_item::type_id::create("req");
      start_item(req);
      req.data = base + i;
      finish_item(req);
    end
  endtask
endclass

// (2) The virtual sequencer: no driver, only handles to the real sequencers
class tav_vsqr extends uvm_sequencer;
  `uvm_component_utils(tav_vsqr)
  tav_apb_sqr  apb_sqr;
  tav_strm_sqr a_sqr, b_sqr;
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
  // Catch a missing handle at time 0, before any sequence trips over it.
  function void end_of_elaboration_phase(uvm_phase phase);
    if (apb_sqr == null) `uvm_fatal("VSQR", "apb_sqr handle is null. Set it in the env's connect_phase.")
    if (a_sqr   == null) `uvm_fatal("VSQR", "a_sqr handle is null. Set it in the env's connect_phase.")
    if (b_sqr   == null) `uvm_fatal("VSQR", "b_sqr handle is null. Set it in the env's connect_phase.")
  endfunction
endclass

// (3) The env: build the agents and the virtual sequencer, then wire the handles
class tav_env extends uvm_env;
  `uvm_component_utils(tav_env)
  tav_apb_agent  apb_agt;
  tav_strm_agent a_agt, b_agt;
  tav_out_mon    out_mon;
  tav_sb         sb;
  tav_vsqr       vsqr;
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
  function void build_phase(uvm_phase phase);
    apb_agt = tav_apb_agent::type_id::create("apb_agt", this);
    a_agt   = tav_strm_agent::type_id::create("a_agt", this);
    b_agt   = tav_strm_agent::type_id::create("b_agt", this);
    b_agt.is_b = 1;
    out_mon = tav_out_mon::type_id::create("out_mon", this);
    sb      = tav_sb::type_id::create("sb", this);
    vsqr    = tav_vsqr::type_id::create("vsqr", this);     // just another component
  endfunction
  function void connect_phase(uvm_phase phase);
    apb_agt.mon.ap.connect(sb.apb_imp);
    a_agt.mon.ap.connect(sb.in_imp);
    b_agt.mon.ap.connect(sb.in_imp);
    out_mon.ap.connect(sb.out_imp);
    // Fill the virtual sequencer's handles: the agents exist now
    vsqr.apb_sqr = apb_agt.sqr;
    vsqr.a_sqr   = a_agt.sqr;
    vsqr.b_sqr   = b_agt.sqr;
  endfunction
endclass

// (4) The virtual sequence: runs on tav_vsqr, reaches the handles through p_sequencer
class tav_base_vseq extends uvm_sequence;
  `uvm_object_utils(tav_base_vseq)
  `uvm_declare_p_sequencer(tav_vsqr)
  function new(string name = "tav_base_vseq"); super.new(name); endfunction
  task send_a(int n, bit [7:0] base);
    tav_strm_seq s = tav_strm_seq::type_id::create("a_seq");
    s.n = n; s.base = base;
    s.start(p_sequencer.a_sqr, this);                    // real sequence on the real sequencer
  endtask
  task send_b(int n, bit [7:0] base);
    tav_strm_seq s = tav_strm_seq::type_id::create("b_seq");
    s.n = n; s.base = base;
    s.start(p_sequencer.b_sqr, this);
  endtask
endclass

class tav_p_vseq extends tav_base_vseq;
  `uvm_object_utils(tav_p_vseq)
  function new(string name = "tav_p_vseq"); super.new(name); endfunction
  task body();
    send_a(4, 8'h20);                                    // A first ...
    send_b(4, 8'hA0);                                    // ... then B
  endtask
endclass

// (5) The test: start the virtual sequence on the virtual sequencer
class tav_vseq_test extends uvm_test;
  `uvm_component_utils(tav_vseq_test)
  tav_env env;
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
  function void build_phase(uvm_phase phase);
    env = tav_env::type_id::create("env", this);
  endfunction
  task main_phase(uvm_phase phase);
    tav_p_vseq s2 = tav_p_vseq::type_id::create("s2");
    phase.raise_objection(this);
    s2.start(env.vsqr);
    phase.drop_objection(this);
  endtask
endclass

What each part does, in plain words:

  1. The real sequence knows only its own agent. It has no idea other agents exist.
  2. The virtual sequencer is an empty shelf with three labelled slots. No driver is connected to it, ever.
  3. The env builds everything in build_phase, then fills the slots in connect_phase, because only then do the agents’ sequencers exist. This is the line BUG=1 leaves out and BUG=4 gets wrong.
  4. The virtual sequence declares p_sequencer with the macro, so p_sequencer.a_sqr works. It starts real sequences on real sequencers and passes this as the parent.
  5. The test only says start(env.vsqr). It never touches an agent.

That is the whole pattern. Everything else in the bench (lock, grab, nesting, default_sequence) is built on these five parts.

The scoreboard checks data. The last step of the full bench also checks intent: it reads CNT_A and CNT_B and compares them with what the virtual sequences meant to send. You will see why both are needed in BUG=4.

The full code

// tav_vseq_uvm.sv : UVM virtual sequences and virtual sequencers, a complete working example (UVM 1.2)
// The Art of Verification | theartofverification.com
// Run on Synopsys VCS X-2025.06-SP1 with UVM 1.2 (EDA Playground), 25 Nov 2026.
// EDA Playground: Synopsys VCS, UVM 1.2, top module "tav_top".
// Paste this whole file into testbench.sv and leave design.sv empty.
//
// What is in here:
//   the design       tav_merge: an APB config port (CTRL, KEY, CNT_A, CNT_B) and two stream inputs,
//                    A and B, merged round robin onto one output. Each byte leaves XORed with KEY.
//   three agents     an APB agent (config) and two stream agents (A and B), each with its own
//                    sequencer, driver and monitor, plus a passive output monitor
//   a scoreboard     predicts every output byte from what went in and the KEY seen on the bus
//   a virtual        tav_vsqr: no driver, just handles to the three real sequencers
//   sequencer
//   the tour         steps 0 to 8, one idea per step:
//     0  default_sequence   tav_init_vseq starts by itself in configure_phase: setup, then traffic
//     1  approach 1         tav_handles_vseq: the sequence holds the sequencer handles, start(null)
//     2  approach 2         tav_p_vseq: runs on the virtual sequencer and uses p_sequencer
//     3  serial vs fork     A then B, against A and B in fork/join. Compare the times.
//     4  join_any           carry on when the short stream ends, then wait fork for the long one
//     5  nested             a top virtual sequence starts two child virtual sequences
//     6  lock               a 3-write re-key with and without lock() while a poll sequence runs
//     7  grab vs lock       grab() jumps ahead of a lock() that asked first
//     8  join_none          start A and B, do other work, wait fork, then check the counters
//
// Register map (APB, byte addresses):
//   0x00 CTRL   en [0] RW. Reset 0. While en is 0 the block takes input and drops it.
//   0x04 KEY    key [7:0] RW. Reset 0. Every output byte is the input byte XOR KEY.
//   0x08 CNT_A  [15:0] RO. Bytes from A that came out.
//   0x0C CNT_B  [15:0] RO. Bytes from B that came out.
//
// Run options (one per run). All four are bench mistakes, not design bugs:
//   (none)    the clean bench
//   +BUG=1    the virtual sequencer's b_sqr handle is never set
//   +BUG=2    tav_init_vseq forks setup and traffic instead of running them in order
//   +BUG=3    step 8 uses join_none and forgets wait fork
//   +BUG=4    b_sqr points at agent A's sequencer (a copy-paste slip in connect_phase)

`timescale 1ns/1ns
`include "uvm_macros.svh"
import uvm_pkg::*;

int tav_bug = 0;   // set from +BUG=n in the test's build_phase

// ---------------------------------------------------------------------------
// The design: two streams merged onto one output, set up over APB
// ---------------------------------------------------------------------------
module tav_merge (input  logic        clk, rst_n,
                  // APB config port
                  input  logic        psel, penable, pwrite,
                  input  logic [7:0]  paddr,
                  input  logic [31:0] pwdata,
                  output logic [31:0] prdata,
                  output logic        pready,
                  // stream A and stream B
                  input  logic        a_valid, b_valid,
                  input  logic [7:0]  a_data, b_data,
                  output logic        a_ready, b_ready,
                  // merged output
                  output logic        o_valid,
                  output logic [7:0]  o_data,
                  output logic        o_src);          // 0 = from A, 1 = from B
  logic        en, rr, pick_b;
  logic [7:0]  key;
  logic [15:0] cnt_a, cnt_b;

  // Round robin: when both are waiting, take turns. rr = 1 means B goes next.
  assign pick_b  = b_valid && (!a_valid || rr);
  assign a_ready = a_valid && !pick_b;
  assign b_ready = pick_b;
  assign pready  = 1'b1;

  always_ff @(posedge clk or negedge rst_n)
    if (!rst_n) begin
      en <= 0; key <= 0; rr <= 0; cnt_a <= 0; cnt_b <= 0;
      o_valid <= 0; o_data <= 0; o_src <= 0;
    end else begin
      if (psel && penable && pwrite) begin
        if (paddr == 8'h00) en  <= pwdata[0];
        if (paddr == 8'h04) key <= pwdata[7:0];
      end
      o_valid <= 0;
      if (a_valid || b_valid) begin
        if (en) begin                                   // enabled: pass it on
          o_valid <= 1;
          o_data  <= (pick_b ? b_data : a_data) ^ key;
          o_src   <= pick_b;
          if (pick_b) cnt_b <= cnt_b + 1; else cnt_a <= cnt_a + 1;
        end                                             // disabled: taken and dropped
        if (a_valid && b_valid) rr <= !pick_b;
      end
    end

  always_comb
    case (paddr)
      8'h00:   prdata = {31'h0, en};
      8'h04:   prdata = {24'h0, key};
      8'h08:   prdata = {16'h0, cnt_a};
      8'h0C:   prdata = {16'h0, cnt_b};
      default: prdata = 32'h0;
    endcase
endmodule

// ---------------------------------------------------------------------------
// Interfaces
// ---------------------------------------------------------------------------
interface tav_apb_if (input logic clk, input logic rst_n);
  logic        psel, penable, pwrite, pready;
  logic [7:0]  paddr;
  logic [31:0] pwdata, prdata;
endinterface

interface tav_strm_if (input logic clk, input logic rst_n);
  logic       valid, ready;
  logic [7:0] data;
endinterface

interface tav_out_if (input logic clk, input logic rst_n);
  logic       valid, src;
  logic [7:0] data;
endinterface

// ---------------------------------------------------------------------------
// Lesson bookkeeping: counters the tour prints. Not part of a normal bench.
// ---------------------------------------------------------------------------
class tav_tally;
  static int    sent_a, sent_b;      // bytes the virtual sequences meant for A and for B
  static int    polls;               // poll reads granted so far
  static string out_order = "";      // "A" or "B" per output byte, cleared by each step
  static string lock_order[$];       // which re-key sequence got the APB sequencer first
endclass

// ---------------------------------------------------------------------------
// Transactions
// ---------------------------------------------------------------------------
class tav_apb_item extends uvm_sequence_item;
  bit [7:0]  addr;
  bit [31:0] data;
  bit        write;
  `uvm_object_utils(tav_apb_item)
  function new(string name = "tav_apb_item"); super.new(name); endfunction
  function string convert2string();
    return $sformatf("%s 0x%02h data 0x%02h", write ? "WR" : "RD", addr, data);
  endfunction
endclass

class tav_strm_item extends uvm_sequence_item;
  bit [7:0] data;
  bit       src;                     // filled by the monitors: 0 = A, 1 = B
  `uvm_object_utils(tav_strm_item)
  function new(string name = "tav_strm_item"); super.new(name); endfunction
endclass

typedef uvm_sequencer #(tav_apb_item)  tav_apb_sqr;
typedef uvm_sequencer #(tav_strm_item) tav_strm_sqr;

// ---------------------------------------------------------------------------
// APB agent
// ---------------------------------------------------------------------------
class tav_apb_drv extends uvm_driver #(tav_apb_item);
  `uvm_component_utils(tav_apb_drv)
  virtual tav_apb_if vif;
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
  function void build_phase(uvm_phase phase);
    if (!uvm_config_db#(virtual tav_apb_if)::get(this, "", "vif", vif))
      `uvm_fatal("NOVIF", "no APB vif")
  endfunction
  task run_phase(uvm_phase phase);
    vif.psel = 0; vif.penable = 0; vif.pwrite = 0; vif.paddr = 0; vif.pwdata = 0;
    wait (vif.rst_n === 1'b1);
    forever begin
      seq_item_port.get_next_item(req);
      @(negedge vif.clk);                                   // setup phase
      vif.psel = 1; vif.penable = 0; vif.pwrite = req.write;
      vif.paddr = req.addr; vif.pwdata = req.data;
      @(negedge vif.clk) vif.penable = 1;                   // access phase
      @(posedge vif.clk);
      while (!vif.pready) @(posedge vif.clk);
      if (!req.write) req.data = vif.prdata;
      @(negedge vif.clk) begin vif.psel = 0; vif.penable = 0; end
      seq_item_port.item_done();
    end
  endtask
endclass

class tav_apb_mon extends uvm_monitor;
  `uvm_component_utils(tav_apb_mon)
  virtual tav_apb_if vif;
  uvm_analysis_port #(tav_apb_item) ap;
  function new(string name, uvm_component parent); super.new(name, parent); ap = new("ap", this); endfunction
  function void build_phase(uvm_phase phase);
    if (!uvm_config_db#(virtual tav_apb_if)::get(this, "", "vif", vif))
      `uvm_fatal("NOVIF", "no APB vif")
  endfunction
  task run_phase(uvm_phase phase);
    forever begin
      @(posedge vif.clk);
      if (vif.rst_n && vif.psel && vif.penable && vif.pready) begin
        tav_apb_item t = tav_apb_item::type_id::create("t");
        t.addr = vif.paddr; t.write = vif.pwrite;
        t.data = vif.pwrite ? vif.pwdata : vif.prdata;
        #1 ap.write(t);      // a write takes effect at this edge, so tell the scoreboard just after it
      end
    end
  endtask
endclass

class tav_apb_agent extends uvm_agent;
  `uvm_component_utils(tav_apb_agent)
  tav_apb_sqr sqr; tav_apb_drv drv; tav_apb_mon mon;
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
  function void build_phase(uvm_phase phase);
    sqr = tav_apb_sqr::type_id::create("sqr", this);
    drv = tav_apb_drv::type_id::create("drv", this);
    mon = tav_apb_mon::type_id::create("mon", this);
  endfunction
  function void connect_phase(uvm_phase phase);
    drv.seq_item_port.connect(sqr.seq_item_export);
  endfunction
endclass

// ---------------------------------------------------------------------------
// Stream agent (used twice: A and B)
// ---------------------------------------------------------------------------
class tav_strm_drv extends uvm_driver #(tav_strm_item);
  `uvm_component_utils(tav_strm_drv)
  virtual tav_strm_if vif;
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
  function void build_phase(uvm_phase phase);
    if (!uvm_config_db#(virtual tav_strm_if)::get(this, "", "vif", vif))
      `uvm_fatal("NOVIF", "no stream vif")
  endfunction
  task run_phase(uvm_phase phase);
    vif.valid = 0; vif.data = 0;
    wait (vif.rst_n === 1'b1);
    forever begin
      seq_item_port.get_next_item(req);
      @(negedge vif.clk) begin vif.valid = 1; vif.data = req.data; end
      @(posedge vif.clk);
      while (!vif.ready) @(posedge vif.clk);                // the byte is taken at this edge
      @(negedge vif.clk) vif.valid = 0;
      seq_item_port.item_done();
    end
  endtask
endclass

class tav_strm_mon extends uvm_monitor;
  `uvm_component_utils(tav_strm_mon)
  virtual tav_strm_if vif;
  bit is_b;
  uvm_analysis_port #(tav_strm_item) ap;
  function new(string name, uvm_component parent); super.new(name, parent); ap = new("ap", this); endfunction
  function void build_phase(uvm_phase phase);
    if (!uvm_config_db#(virtual tav_strm_if)::get(this, "", "vif", vif))
      `uvm_fatal("NOVIF", "no stream vif")
  endfunction
  task run_phase(uvm_phase phase);
    forever begin
      @(posedge vif.clk);
      if (vif.rst_n && vif.valid && vif.ready) begin
        tav_strm_item t = tav_strm_item::type_id::create("t");
        t.data = vif.data; t.src = is_b;
        ap.write(t);
      end
    end
  endtask
endclass

class tav_strm_agent extends uvm_agent;
  `uvm_component_utils(tav_strm_agent)
  tav_strm_sqr sqr; tav_strm_drv drv; tav_strm_mon mon;
  bit is_b;
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
  function void build_phase(uvm_phase phase);
    sqr = tav_strm_sqr::type_id::create("sqr", this);
    drv = tav_strm_drv::type_id::create("drv", this);
    mon = tav_strm_mon::type_id::create("mon", this);
    mon.is_b = is_b;
  endfunction
  function void connect_phase(uvm_phase phase);
    drv.seq_item_port.connect(sqr.seq_item_export);
  endfunction
endclass

// Passive monitor on the merged output
class tav_out_mon extends uvm_monitor;
  `uvm_component_utils(tav_out_mon)
  virtual tav_out_if vif;
  uvm_analysis_port #(tav_strm_item) ap;
  function new(string name, uvm_component parent); super.new(name, parent); ap = new("ap", this); endfunction
  function void build_phase(uvm_phase phase);
    if (!uvm_config_db#(virtual tav_out_if)::get(this, "", "vif", vif))
      `uvm_fatal("NOVIF", "no output vif")
  endfunction
  task run_phase(uvm_phase phase);
    forever begin
      @(posedge vif.clk);
      if (vif.rst_n && vif.valid) begin
        tav_strm_item t = tav_strm_item::type_id::create("t");
        string        c = vif.src ? "B" : "A";
        t.data = vif.data; t.src = vif.src;
        tav_tally::out_order = {tav_tally::out_order, c};
        ap.write(t);
      end
    end
  endtask
endclass

// ---------------------------------------------------------------------------
// Scoreboard
// ---------------------------------------------------------------------------
`uvm_analysis_imp_decl(_apb)
`uvm_analysis_imp_decl(_in)
`uvm_analysis_imp_decl(_out)

class tav_sb extends uvm_scoreboard;
  `uvm_component_utils(tav_sb)
  uvm_analysis_imp_apb #(tav_apb_item,  tav_sb) apb_imp;
  uvm_analysis_imp_in  #(tav_strm_item, tav_sb) in_imp;
  uvm_analysis_imp_out #(tav_strm_item, tav_sb) out_imp;
  bit       en;                      // the block's state, as seen on the APB bus
  bit [7:0] key;
  bit [7:0] exp_q[2][$];             // expected output bytes, one queue per source
  int n_in[2], n_ok, n_bad, n_drop;
  function new(string name, uvm_component parent);
    super.new(name, parent);
    apb_imp = new("apb_imp", this); in_imp = new("in_imp", this); out_imp = new("out_imp", this);
  endfunction
  function void write_apb(tav_apb_item t);
    if (!t.write) return;
    if (t.addr == 8'h00) en  = t.data[0];
    if (t.addr == 8'h04) key = t.data[7:0];
  endfunction
  function void write_in(tav_strm_item t);
    n_in[t.src]++;
    if (!en) begin
      n_drop++;
      `uvm_error("SB", $sformatf("%s byte 0x%02h went in while CTRL.en was 0, so the block dropped it. Traffic started before setup finished.",
                 t.src ? "B" : "A", t.data))
      return;
    end
    exp_q[t.src].push_back(t.data ^ key);
  endfunction
  function void write_out(tav_strm_item t);
    bit [7:0] exp;
    if (exp_q[t.src].size() == 0) begin
      n_bad++;
      `uvm_error("SB", $sformatf("unexpected output 0x%02h from %s", t.data, t.src ? "B" : "A"))
      return;
    end
    exp = exp_q[t.src].pop_front();
    if (t.data == exp) n_ok++;
    else begin
      n_bad++;
      `uvm_error("SB", $sformatf("%s: expected 0x%02h, got 0x%02h", t.src ? "B" : "A", exp, t.data))
    end
  endfunction
  function void check_phase(uvm_phase phase);
    foreach (exp_q[s])
      if (exp_q[s].size() != 0)
        `uvm_error("SB", $sformatf("%0d bytes from %s went in and never came out", exp_q[s].size(), s ? "B" : "A"))
  endfunction
  function void report_phase(uvm_phase phase);
    `uvm_info("SB", $sformatf("in: A=%0d B=%0d | out checked: %0d ok, %0d bad | dropped: %0d",
              n_in[0], n_in[1], n_ok, n_bad, n_drop), UVM_NONE)
  endfunction
endclass

// ---------------------------------------------------------------------------
// The virtual sequencer: no driver, only handles to the real sequencers
// ---------------------------------------------------------------------------
class tav_vsqr extends uvm_sequencer;
  `uvm_component_utils(tav_vsqr)
  tav_apb_sqr  apb_sqr;
  tav_strm_sqr a_sqr, b_sqr;
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
  // Catch a missing handle at time 0, before any sequence trips over it.
  function void end_of_elaboration_phase(uvm_phase phase);
    if (apb_sqr == null) `uvm_fatal("VSQR", "apb_sqr handle is null. Set it in the env's connect_phase.")
    if (a_sqr   == null) `uvm_fatal("VSQR", "a_sqr handle is null. Set it in the env's connect_phase.")
    if (b_sqr   == null) `uvm_fatal("VSQR", "b_sqr handle is null. Set it in the env's connect_phase.")
  endfunction
endclass

// ---------------------------------------------------------------------------
// Env
// ---------------------------------------------------------------------------
class tav_env extends uvm_env;
  `uvm_component_utils(tav_env)
  tav_apb_agent  apb_agt;
  tav_strm_agent a_agt, b_agt;
  tav_out_mon    out_mon;
  tav_sb         sb;
  tav_vsqr       vsqr;
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
  function void build_phase(uvm_phase phase);
    apb_agt = tav_apb_agent::type_id::create("apb_agt", this);
    a_agt   = tav_strm_agent::type_id::create("a_agt", this);
    b_agt   = tav_strm_agent::type_id::create("b_agt", this);
    b_agt.is_b = 1;
    out_mon = tav_out_mon::type_id::create("out_mon", this);
    sb      = tav_sb::type_id::create("sb", this);
    vsqr    = tav_vsqr::type_id::create("vsqr", this);
  endfunction
  function void connect_phase(uvm_phase phase);
    apb_agt.mon.ap.connect(sb.apb_imp);
    a_agt.mon.ap.connect(sb.in_imp);
    b_agt.mon.ap.connect(sb.in_imp);
    out_mon.ap.connect(sb.out_imp);
    // Fill the virtual sequencer's handles
    vsqr.apb_sqr = apb_agt.sqr;
    vsqr.a_sqr   = a_agt.sqr;
    if (tav_bug == 4)      vsqr.b_sqr = a_agt.sqr;          // BUG=4: copy-paste slip
    else if (tav_bug != 1) vsqr.b_sqr = b_agt.sqr;          // BUG=1: never set
  endfunction
endclass

// ---------------------------------------------------------------------------
// Real sequences: one per agent type
// ---------------------------------------------------------------------------
// n bytes on one stream: base, base+1, base+2, ...
class tav_strm_seq extends uvm_sequence #(tav_strm_item);
  `uvm_object_utils(tav_strm_seq)
  int       n = 4;
  bit [7:0] base;
  function new(string name = "tav_strm_seq"); super.new(name); endfunction
  task body();
    for (int i = 0; i < n; i++) begin
      req = tav_strm_item::type_id::create("req");
      start_item(req);
      req.data = base + i;
      finish_item(req);
    end
  endtask
endclass

// One APB read or write
class tav_apb_rw_seq extends uvm_sequence #(tav_apb_item);
  `uvm_object_utils(tav_apb_rw_seq)
  bit [7:0]  addr;
  bit [31:0] data;
  bit        write;
  function new(string name = "tav_apb_rw_seq"); super.new(name); endfunction
  task body();
    req = tav_apb_item::type_id::create("req");
    start_item(req);
    req.addr = addr; req.write = write; req.data = data;
    finish_item(req);
    data = req.data;
  endtask
endclass

// Reads CNT_A over and over until told to stop (the background traffic for steps 6 and 7)
class tav_apb_poll_seq extends uvm_sequence #(tav_apb_item);
  `uvm_object_utils(tav_apb_poll_seq)
  bit stop;
  function new(string name = "tav_apb_poll_seq"); super.new(name); endfunction
  task body();
    while (!stop) begin
      req = tav_apb_item::type_id::create("req");
      start_item(req);
      tav_tally::polls++;                                   // counted when granted
      req.addr = 8'h08; req.write = 0;
      finish_item(req);
    end
  endtask
endclass

// Re-key: CTRL.en=0, KEY=k, CTRL.en=1. These three writes belong together.
class tav_apb_rekey_seq extends uvm_sequence #(tav_apb_item);
  `uvm_object_utils(tav_apb_rekey_seq)
  bit [7:0] key;
  bit       use_lock, use_grab;
  int       p_first, p_last;         // poll reads granted so far, at the first and the last write
  function new(string name = "tav_apb_rekey_seq"); super.new(name); endfunction
  task body();
    bit [7:0]  addr[3] = '{8'h00, 8'h04, 8'h00};
    bit [31:0] data[3];
    data = '{32'h0, {24'h0, key}, 32'h1};
    if (use_grab) grab(); else if (use_lock) lock();
    tav_tally::lock_order.push_back(get_name());
    for (int i = 0; i < 3; i++) begin
      req = tav_apb_item::type_id::create("req");
      start_item(req);
      if (i == 0) p_first = tav_tally::polls;
      if (i == 2) p_last  = tav_tally::polls;
      req.addr = addr[i]; req.write = 1; req.data = data[i];
      finish_item(req);
    end
    if (use_grab) ungrab(); else if (use_lock) unlock();
  endtask
endclass

// ---------------------------------------------------------------------------
// Helpers both approaches share: start a real sequence on a real sequencer
// ---------------------------------------------------------------------------
class tav_vs;
  // intent_b says which stream the caller means. The tally counts intent, not what happened.
  static task send(tav_strm_sqr sqr, bit intent_b, int n, bit [7:0] base, uvm_sequence_base parent);
    tav_strm_seq s = tav_strm_seq::type_id::create(intent_b ? "b_seq" : "a_seq");
    s.n = n; s.base = base;
    if (intent_b) tav_tally::sent_b += n; else tav_tally::sent_a += n;
    s.start(sqr, parent);
  endtask
  static task wr(tav_apb_sqr sqr, bit [7:0] addr, bit [31:0] data, uvm_sequence_base parent);
    tav_apb_rw_seq s = tav_apb_rw_seq::type_id::create("wr");
    s.addr = addr; s.data = data; s.write = 1;
    s.start(sqr, parent);
  endtask
  static task rd(tav_apb_sqr sqr, bit [7:0] addr, output bit [31:0] data, input uvm_sequence_base parent);
    tav_apb_rw_seq s = tav_apb_rw_seq::type_id::create("rd");
    s.addr = addr; s.write = 0;
    s.start(sqr, parent);
    data = s.data;
  endtask
  static function void step(string title);
    tav_tally::out_order = "";
    `uvm_info("STEP", $sformatf("---- %s ----", title), UVM_NONE)
  endfunction
  static task done(string what);
    #20;                                                    // let the last byte reach the output
    `uvm_info("STEP", $sformatf("%s | output order: %s", what, tav_tally::out_order), UVM_NONE)
  endtask
endclass

// ---------------------------------------------------------------------------
// Approach 1: the virtual sequence holds the handles itself. Start it with start(null).
// ---------------------------------------------------------------------------
class tav_handles_vseq extends uvm_sequence;
  `uvm_object_utils(tav_handles_vseq)
  tav_apb_sqr  apb_sqr;              // the test fills these in before start(null)
  tav_strm_sqr a_sqr, b_sqr;
  function new(string name = "tav_handles_vseq"); super.new(name); endfunction
  task body();
    `uvm_info("STEP", $sformatf("m_sequencer is %s. This sequence runs on no sequencer.",
              m_sequencer == null ? "null" : m_sequencer.get_full_name()), UVM_NONE)
    tav_vs::send(a_sqr, 0, 4, 8'h10, this);
    tav_vs::send(b_sqr, 1, 4, 8'h90, this);
    tav_vs::done($sformatf("step 1 done at %0t", $time));
  endtask
endclass

// ---------------------------------------------------------------------------
// Approach 2: virtual sequences that run on tav_vsqr and reach the handles through p_sequencer
// ---------------------------------------------------------------------------
class tav_base_vseq extends uvm_sequence;
  `uvm_object_utils(tav_base_vseq)
  `uvm_declare_p_sequencer(tav_vsqr)
  function new(string name = "tav_base_vseq"); super.new(name); endfunction
  task send_a(int n, bit [7:0] base); tav_vs::send(p_sequencer.a_sqr, 0, n, base, this); endtask
  task send_b(int n, bit [7:0] base); tav_vs::send(p_sequencer.b_sqr, 1, n, base, this); endtask
  task setup(bit [7:0] key);
    tav_vs::wr(p_sequencer.apb_sqr, 8'h04, key, this);
    tav_vs::wr(p_sequencer.apb_sqr, 8'h00, 1, this);
  endtask
endclass

// Step 0: the default_sequence. Setup first, then traffic.
class tav_init_vseq extends tav_base_vseq;
  `uvm_object_utils(tav_init_vseq)
  function new(string name = "tav_init_vseq");
    super.new(name);
    set_automatic_phase_objection(1);                   // keeps configure_phase open while it runs
  endfunction
  task body();
    uvm_phase ph = get_starting_phase();
    tav_vs::step($sformatf("0. default_sequence: started by itself in %s", ph == null ? "?" : ph.get_name()));
    if (tav_bug == 2)
      fork                                              // BUG=2: setup and traffic race
        setup(8'h3C);
        begin send_a(4, 8'h00); send_b(4, 8'h80); end
      join
    else begin
      setup(8'h3C);
      send_a(4, 8'h00);
      send_b(4, 8'h80);
    end
    tav_vs::done($sformatf("step 0 done at %0t", $time));
  endtask
endclass

// Step 2: same traffic as step 1, now through p_sequencer
class tav_p_vseq extends tav_base_vseq;
  `uvm_object_utils(tav_p_vseq)
  function new(string name = "tav_p_vseq"); super.new(name); endfunction
  task body();
    `uvm_info("STEP", $sformatf("m_sequencer is %s, p_sequencer is a %s",
              m_sequencer.get_full_name(), p_sequencer.get_type_name()), UVM_NONE)
    send_a(4, 8'h20);
    send_b(4, 8'hA0);
    tav_vs::done($sformatf("step 2 done at %0t", $time));
  endtask
endclass

// Step 3: serial, then parallel
class tav_order_vseq extends tav_base_vseq;
  `uvm_object_utils(tav_order_vseq)
  function new(string name = "tav_order_vseq"); super.new(name); endfunction
  task body();
    time t0;
    t0 = $time;
    send_a(8, 8'h30);
    send_b(8, 8'hB0);
    tav_vs::done($sformatf("step 3a serial (A then B, 16 bytes) took %0t", $time - t0));
    tav_tally::out_order = "";
    t0 = $time;
    fork
      send_a(8, 8'h40);
      send_b(8, 8'hC0);
    join
    tav_vs::done($sformatf("step 3b fork/join (A and B together, 16 bytes) took %0t", $time - t0));
  endtask
endclass

// Step 4: join_any, then wait fork
class tav_any_vseq extends tav_base_vseq;
  `uvm_object_utils(tav_any_vseq)
  function new(string name = "tav_any_vseq"); super.new(name); endfunction
  task body();
    fork
      send_a(3, 8'h50);
      send_b(9, 8'hD0);
    join_any
    `uvm_info("STEP", $sformatf("join_any returned at %0t: A (3 bytes) is done, B (9 bytes) is still sending. Output so far: %s",
              $time, tav_tally::out_order), UVM_NONE)
    wait fork;
    tav_vs::done($sformatf("step 4 wait fork returned at %0t", $time));
  endtask
endclass

// Step 5: virtual sequences inside a virtual sequence
class tav_rekey_vseq extends tav_base_vseq;
  `uvm_object_utils(tav_rekey_vseq)
  bit [7:0] key = 8'h5A;
  function new(string name = "tav_rekey_vseq"); super.new(name); endfunction
  task body(); setup(key); endtask
endclass

class tav_traffic_vseq extends tav_base_vseq;
  `uvm_object_utils(tav_traffic_vseq)
  function new(string name = "tav_traffic_vseq"); super.new(name); endfunction
  task body();
    fork send_a(4, 8'h60); send_b(4, 8'hE0); join
  endtask
endclass

class tav_top_vseq extends tav_base_vseq;
  `uvm_object_utils(tav_top_vseq)
  function new(string name = "tav_top_vseq"); super.new(name); endfunction
  task body();
    tav_rekey_vseq   rk = tav_rekey_vseq::type_id::create("rk");
    tav_traffic_vseq tr = tav_traffic_vseq::type_id::create("tr");
    rk.start(p_sequencer, this);                        // child 1 on the same virtual sequencer
    tr.start(p_sequencer, this);                        // child 2, only after child 1 is done
    `uvm_info("STEP", $sformatf("children ran as %s and %s", rk.get_full_name(), tr.get_full_name()), UVM_NONE)
    tav_vs::done($sformatf("step 5 done at %0t", $time));
  endtask
endclass

// Step 6: three writes that belong together, while another sequence keeps reading
class tav_lock_vseq extends tav_base_vseq;
  `uvm_object_utils(tav_lock_vseq)
  function new(string name = "tav_lock_vseq"); super.new(name); endfunction
  task body();
    tav_apb_poll_seq  poll  = tav_apb_poll_seq::type_id::create("poll");
    tav_apb_rekey_seq plain = tav_apb_rekey_seq::type_id::create("rekey_plain");
    tav_apb_rekey_seq held  = tav_apb_rekey_seq::type_id::create("rekey_locked");
    plain.key = 8'h66;
    held.key  = 8'h77; held.use_lock = 1;
    fork poll.start(p_sequencer.apb_sqr, this); join_none
    #40;
    plain.start(p_sequencer.apb_sqr, this);
    held.start(p_sequencer.apb_sqr, this);
    poll.stop = 1;
    wait fork;
    `uvm_info("STEP", $sformatf("without lock: %0d poll reads got in between the 3 re-key writes",
              plain.p_last - plain.p_first), UVM_NONE)
    `uvm_info("STEP", $sformatf("with lock:    %0d poll reads got in between the 3 re-key writes",
              held.p_last - held.p_first), UVM_NONE)
    if (held.p_last != held.p_first)
      `uvm_error("LOCK", "a poll read got in while the lock was held")
    tav_vs::done($sformatf("step 6 done at %0t, %0d poll reads in all", $time, tav_tally::polls));
  endtask
endclass

// Step 7: a grab goes to the front of the line, but it still waits for a lock that is already held
class tav_grab_vseq extends tav_base_vseq;
  `uvm_object_utils(tav_grab_vseq)
  function new(string name = "tav_grab_vseq"); super.new(name); endfunction
  task body();
    tav_apb_poll_seq  poll = tav_apb_poll_seq::type_id::create("poll");
    tav_apb_rekey_seq lk   = tav_apb_rekey_seq::type_id::create("lock_first");
    tav_apb_rekey_seq gb   = tav_apb_rekey_seq::type_id::create("grab_later");
    int p_before;
    lk.key = 8'h11; lk.use_lock = 1;
    gb.key = 8'h22; gb.use_grab = 1;
    tav_tally::lock_order.delete();
    p_before = tav_tally::polls;
    fork begin #5; poll.start(p_sequencer.apb_sqr, this); end join_none   // poll asks at +5 ns
    fork
      lk.start(p_sequencer.apb_sqr, this);                                 // lock taken at +0 ns
      begin #25; gb.start(p_sequencer.apb_sqr, this); end                  // grab asks at +25 ns
    join
    `uvm_info("STEP", $sformatf("order the APB sequencer was given out: %s, %s, then the poll (waiting since +5 ns)",
              tav_tally::lock_order[0], tav_tally::lock_order[1]), UVM_NONE)
    `uvm_info("STEP", $sformatf("poll reads granted from the start of step 7 until the grab's last write: %0d",
              gb.p_last - p_before), UVM_NONE)
    if (tav_tally::lock_order[0] != "lock_first" || tav_tally::lock_order[1] != "grab_later" || gb.p_last != p_before)
      `uvm_error("GRAB", "the APB sequencer was not given out in the expected order")
    poll.stop = 1;
    wait fork;
    tav_vs::done($sformatf("step 7 done at %0t", $time));
  endtask
endclass

// Step 8: start traffic, do other work, then wait fork before checking the counters
class tav_none_vseq extends tav_base_vseq;
  `uvm_object_utils(tav_none_vseq)
  function new(string name = "tav_none_vseq"); super.new(name); endfunction
  task body();
    bit [31:0] key, ca, cb;
    fork
      send_a(10, 8'h70);
      send_b(10, 8'hF0);
    join_none
    tav_vs::rd(p_sequencer.apb_sqr, 8'h04, key, this);                    // other work meanwhile
    `uvm_info("STEP", $sformatf("join_none returned at once. Meanwhile KEY reads 0x%02h at %0t", key, $time), UVM_NONE)
    if (tav_bug != 3) wait fork;                                           // BUG=3: forgotten
    tav_vs::rd(p_sequencer.apb_sqr, 8'h08, ca, this);
    tav_vs::rd(p_sequencer.apb_sqr, 8'h0C, cb, this);
    `uvm_info("STEP", $sformatf("at %0t: CNT_A=%0d (meant %0d), CNT_B=%0d (meant %0d)",
              $time, ca, tav_tally::sent_a, cb, tav_tally::sent_b), UVM_NONE)
    if (ca != tav_tally::sent_a)
      `uvm_error("CHECK", $sformatf("CNT_A is %0d but the virtual sequences sent %0d bytes to A", ca, tav_tally::sent_a))
    if (cb != tav_tally::sent_b)
      `uvm_error("CHECK", $sformatf("CNT_B is %0d but the virtual sequences sent %0d bytes to B", cb, tav_tally::sent_b))
    tav_vs::done($sformatf("step 8 done at %0t", $time));
  endtask
endclass

// ---------------------------------------------------------------------------
// The test
// ---------------------------------------------------------------------------
class tav_vseq_test extends uvm_test;
  `uvm_component_utils(tav_vseq_test)
  tav_env env;
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
  function void build_phase(uvm_phase phase);
    void'($value$plusargs("BUG=%d", tav_bug));
    `uvm_info("TEST", $sformatf("Running with BUG=%0d", tav_bug), UVM_NONE)
    env = tav_env::type_id::create("env", this);
    // Step 0: the virtual sequencer starts tav_init_vseq by itself in configure_phase
    uvm_config_db#(uvm_object_wrapper)::set(this, "env.vsqr.configure_phase", "default_sequence",
                                            tav_init_vseq::type_id::get());
    uvm_top.set_timeout(200us, 1);
  endfunction
  task main_phase(uvm_phase phase);
    tav_handles_vseq s1 = tav_handles_vseq::type_id::create("s1");
    tav_p_vseq       s2 = tav_p_vseq::type_id::create("s2");
    tav_order_vseq   s3 = tav_order_vseq::type_id::create("s3");
    tav_any_vseq     s4 = tav_any_vseq::type_id::create("s4");
    tav_top_vseq     s5 = tav_top_vseq::type_id::create("s5");
    tav_lock_vseq    s6 = tav_lock_vseq::type_id::create("s6");
    tav_grab_vseq    s7 = tav_grab_vseq::type_id::create("s7");
    tav_none_vseq    s8 = tav_none_vseq::type_id::create("s8");
    phase.raise_objection(this);
    tav_vs::step("1. approach 1: the sequence holds the handles, start(null)");
    s1.apb_sqr = env.apb_agt.sqr; s1.a_sqr = env.a_agt.sqr; s1.b_sqr = env.b_agt.sqr;
    s1.start(null);
    tav_vs::step("2. approach 2: a virtual sequencer, reached through p_sequencer");
    s2.start(env.vsqr);
    tav_vs::step("3. serial vs fork/join");
    s3.start(env.vsqr);
    tav_vs::step("4. fork/join_any, then wait fork");
    s4.start(env.vsqr);
    tav_vs::step("5. nested virtual sequences");
    s5.start(env.vsqr);
    tav_vs::step("6. lock(): keep three writes together");
    s6.start(env.vsqr);
    tav_vs::step("7. grab() vs lock()");
    s7.start(env.vsqr);
    tav_vs::step("8. fork/join_none, then wait fork, then check the counters");
    s8.start(env.vsqr);
    #1us;                                                   // let any bytes still in flight finish
    phase.drop_objection(this);
  endtask
  function void report_phase(uvm_phase phase);
    uvm_report_server rs = uvm_report_server::get_server();
    int n = rs.get_severity_count(UVM_ERROR) + rs.get_severity_count(UVM_FATAL);
    `uvm_info("RESULT", $sformatf("%s: %0d errors", n == 0 ? "PASS" : "FAIL", n), UVM_NONE)
  endfunction
endclass

// ---------------------------------------------------------------------------
// Top
// ---------------------------------------------------------------------------
module tav_top;
  logic clk = 0, rst_n = 0;
  always #5 clk = ~clk;
  initial begin repeat (3) @(negedge clk); rst_n = 1; end

  tav_apb_if  apb_if (clk, rst_n);
  tav_strm_if a_if   (clk, rst_n);
  tav_strm_if b_if   (clk, rst_n);
  tav_out_if  o_if   (clk, rst_n);

  tav_merge dut (.clk(clk), .rst_n(rst_n),
                 .psel(apb_if.psel), .penable(apb_if.penable), .pwrite(apb_if.pwrite),
                 .paddr(apb_if.paddr), .pwdata(apb_if.pwdata), .prdata(apb_if.prdata),
                 .pready(apb_if.pready),
                 .a_valid(a_if.valid), .a_data(a_if.data), .a_ready(a_if.ready),
                 .b_valid(b_if.valid), .b_data(b_if.data), .b_ready(b_if.ready),
                 .o_valid(o_if.valid), .o_data(o_if.data), .o_src(o_if.src));

  initial begin
    uvm_config_db#(virtual tav_apb_if)::set(null, "uvm_test_top.env.apb_agt*", "vif", apb_if);
    uvm_config_db#(virtual tav_strm_if)::set(null, "uvm_test_top.env.a_agt*", "vif", a_if);
    uvm_config_db#(virtual tav_strm_if)::set(null, "uvm_test_top.env.b_agt*", "vif", b_if);
    uvm_config_db#(virtual tav_out_if)::set(null, "uvm_test_top.env.out_mon", "vif", o_if);
    run_test("tav_vseq_test");
  end
endmodule

What does the clean run show?

I ran the file on Synopsys VCS X-2025.06-SP1 with UVM 1.2 on EDA Playground. The clean run ends at 3,430 ns with 0 errors: 96 bytes in (45 on A, 51 on B), 96 checked, 0 bad, 0 dropped.

Here is what each step printed, cut down to the line that matters:

step 0 done at 250  | output order: AAAABBBB              <- default_sequence, configure_phase
m_sequencer is null. This sequence runs on no sequencer.    <- step 1, start(null)
m_sequencer is uvm_test_top.env.vsqr, p_sequencer is a tav_vsqr   <- step 2
step 3a serial (A then B, 16 bytes) took 320 | output order: AAAAAAAABBBBBBBB
step 3b fork/join (A and B together, 16 bytes) took 170 | output order: ABABABABABABABAB
join_any returned at 1230: A (3 bytes) is done, B (9 bytes) is still sending. Output so far: BABAB
children ran as uvm_test_top.env.vsqr.s5.rk and uvm_test_top.env.vsqr.s5.tr
without lock: 2 poll reads got in between the 3 re-key writes
with lock:    0 poll reads got in between the 3 re-key writes
order the APB sequencer was given out: lock_first, grab_later, then the poll (waiting since +5 ns)
join_none returned at once. Meanwhile KEY reads 0x22 at 2170
at 2410: CNT_A=45 (meant 45), CNT_B=51 (meant 51)
[SB] in: A=45 B=51 | out checked: 96 ok, 0 bad | dropped: 0
[RESULT] PASS: 0 errors

What the numbers say:

  • Step 3: the same 16 bytes took 320 ns one after the other and 170 ns in fork/join. The output order shows why: AAAAAAAABBBBBBBB against ABABABAB. Parallel really is parallel here, because A and B have their own sequencers and drivers.
  • Step 4: join_any came back at 1,230 ns, as soon as A’s 3 bytes were done, while B still had bytes to send. wait fork then held on until 1,340 ns.
  • Step 5: the child names show the nesting: vsqr.s5.rk and vsqr.s5.tr live under their parent s5.
  • Step 6: without lock(), 2 poll reads landed between the 3 re-key writes. With lock(), 0.
  • Step 7: the grab asked 20 ns after the poll, yet got the sequencer first. It still waited for lock_first to finish.
  • Step 8: join_none came back at once, the KEY read ran while the traffic was going, and after wait fork the counters matched what the sequences sent.
Results of five VCS runs: the clean run passes; BUG=1 stops at time 0; BUG=2 drops 3 bytes; BUG=3 checks the counters too early; BUG=4 sends every B byte through A while the scoreboard stays clean
What each planted mistake looked like in five VCS runs.

What do the four planted mistakes look like?

RunMistakeWhat catches itScoreboard
cleannonenothing to catch96 ok, 0 bad
+BUG=1b_sqr never setuvm_fatal in the virtual sequencer at time 0never runs
+BUG=2setup and traffic forkedscoreboard: 3 bytes went in while CTRL.en was 0; CNT_A is 42, meant 4593 ok, 3 dropped
+BUG=3join_none, no wait forkCNT_A 37 (meant 45), CNT_B 45 (meant 51)96 ok, 0 bad
+BUG=4b_sqr points at A’s sequencerCNT_A 92 (meant 45), CNT_B 4 (meant 51)96 ok, 0 bad

BUG=1: a handle that was never set

UVM_FATAL testbench.sv(381) @ 0: uvm_test_top.env.vsqr [VSQR] b_sqr handle is null. Set it in the env's connect_phase.

The check in end_of_elaboration_phase stops the run at time 0 with the name of the missing handle. Without it, the first send_b() would start a sequence on a null sequencer, and you would be hunting for the cause in the middle of a test instead.

Debug tip: put a null check for every handle in the virtual sequencer. It costs three lines and saves an hour.

BUG=2: traffic before setup

UVM_ERROR @ 45: [SB] A byte 0x00 went in while CTRL.en was 0, so the block dropped it. Traffic started before setup finished.
UVM_ERROR @ 65: [SB] A byte 0x01 went in while CTRL.en was 0, ...
UVM_ERROR @ 85: [SB] A byte 0x02 went in while CTRL.en was 0, ...
step 0 done at 190 | output order: ABBBB

The start-up sequence forked setup and traffic. The first 3 A bytes reached the block before the CTRL write did, so the block dropped them. The output order shows it plainly: one A instead of four. The counter check at the end flags it again: CNT_A 42, meant 45.

Debug tip: when the first few items go missing and the rest are fine, look at the order between setup and traffic first.

BUG=3: join_none without wait fork

at 2230: CNT_A=37 (meant 45), CNT_B=45 (meant 51)
[SB] in: A=45 B=51 | out checked: 96 ok, 0 bad | dropped: 0

The counters were read at 2,230 ns, 90 ns into the last burst, while 8 A bytes and 6 B bytes were still on their way. Nothing was lost: the scoreboard is clean because the bytes finished during the 1 µs drain at the end of the test. The check just ran too early. In a real bench the same slip lets a test end, or a reset start, in the middle of traffic.

Debug tip: a counter that is “a bit low” while the scoreboard is clean usually means a check ran before the traffic finished. Look for join_none.

BUG=4: the wrong handle

step 3b fork/join (A and B together, 16 bytes) took 320 | output order: AAAAAAAAAAAAAAAA
at 2880: CNT_A=92 (meant 45), CNT_B=4 (meant 51)
[SB] in: A=92 B=4 | out checked: 96 ok, 0 bad | dropped: 0

This is the one I want you to remember. The scoreboard is perfectly clean, 96 ok, because every byte that went in came out right. It just went in on the wrong port. Only the intent check (what did the test mean to send where?) catches it. The 4 B bytes that did arrive are the ones from step 1, the only step that does not use the virtual sequencer’s handles.

The log had other clues too. Step 3b’s “parallel” traffic took 320 ns, exactly as long as the serial run, because both streams were taking turns on one sequencer.

Debug tip: if parallel traffic takes as long as serial traffic, two of your handles point at the same sequencer.

Common mistakes with virtual sequences

  1. Not setting a handle in the virtual sequencer (BUG=1). Check them all in end_of_elaboration_phase.
  2. Pointing a handle at the wrong sequencer (BUG=4). A scoreboard built from input monitors will not catch it. Check what the test meant to send, per port.
  3. Forking setup with traffic (BUG=2). Setup goes first, in order, unless the spec says otherwise.
  4. join_none without wait fork (BUG=3). The parent returns while its children still run.
  5. Starting a p_sequencer sequence with start(null). There is no sequencer to cast, so p_sequencer stays null and the first access crashes.
  6. lock() with no unlock(). The sequencer is held for good, and the test hangs until the timeout.
  7. default_sequence with no objection. The phase can end before the sequence does anything.

How do you debug a virtual sequence?

  • Print get_full_name() of each child sequence. It tells you which sequencer it really ran on.
  • Print the output order per step, like this bench does. A missing letter is a missing stream.
  • Time each step. Parallel that is not faster than serial is a shared sequencer.
  • Use +UVM_CONFIG_DB_TRACE to see who set (or did not set) a handle or a default_sequence.
  • Check intent at the end: per-port counts against what the sequences meant to send.

How can AI help you debug a virtual sequence?

The task: find the cause of BUG=4 from the log, the hardest of the four because the scoreboard says everything is fine.

The tool: Claude (Opus 5.5), in a fresh chat with no other context.

The prompt I gave it (the log lines above plus the virtual sequencer, the env’s connect_phase and the base virtual sequence, with the slip in place):

I'm a verification engineer. My UVM 1.2 test fails and my scoreboard says everything is fine.
Find the root cause, point to the exact line, and give the fix. Then tell me which other
symptoms in the log point to the same cause.

The design merges two byte streams, A and B, onto one output. CNT_A and CNT_B count bytes
that came out from each port. My scoreboard compares every output byte against a prediction
made from the input monitors.

[the BUG=4 log: steps 0 to 5 and step 8]
[tav_vsqr, tav_env::connect_phase, tav_base_vseq]

What it answered (shortened):

Root cause: in tav_env::connect_phase, vsqr.b_sqr = a_agt.sqr;. Fix: vsqr.b_sqr = b_agt.sqr;. Every send_b() goes to the A sequencer. Step 1 sets its handles directly, so it is the only step that uses port B.

Why the scoreboard is fine: it predicts from the input monitors. The A monitor really did see those bytes go into A, so prediction and output agree.

Other symptoms: steps 0, 2, 3a, 3b, 4 and 5 show only A. The numbers add up: 4 B bytes from step 1 went to B, the other 47 went to A, so CNT_A = 45 + 47 = 92. Steps 3a and 3b both take 320, because both threads share one sequencer. In step 4, 4 A bytes came out when only 3 A bytes were sent, so a B byte already came out as A.

Next time: compare bytes per port against intent, use the port field the item carries, or assert in end_of_elaboration_phase that a_sqr != b_sqr.

How I checked it: against the bench and the log. The line and the fix are right. The arithmetic is right: 51 – 4 = 47, and 45 + 47 = 92. It also caught two clues I had not pointed out to it: the 320 ns “parallel” step and the extra A byte in step 4.

Where it fell short:

  • I handed it connect_phase with the slip in it. With three lines of handle code in front of it, the bug is easy to see. On a real bench the handles are set in a file you did not think to paste. The symptoms list is the part that earns its keep: it tells you where to look.
  • One of its fixes would not work here. “Use the port field the item carries” sounds right, but in this bench (and most benches) that field is filled in by the monitor, from the port the byte actually used. It can never disagree with the port. Only a check against what the test meant to send can catch this bug.
  • The a_sqr != b_sqr assertion is a good idea for this exact slip, but it would not catch b_sqr pointing at some other agent’s sequencer of the same type.

My take: give AI the log and ask for the symptoms first, before you hand it code. Then check every fix it suggests against how your own bench is built.

Interview questions on virtual sequences

  1. What is the difference between a virtual sequence and a virtual sequencer? The sequence holds the order of traffic across agents. The sequencer only holds handles to the real sequencers. You can have the first without the second.
  2. Why is there no driver on a virtual sequencer? It never sends items. Its sequences start other sequences on real sequencers, and those have drivers.
  3. What does uvm_declare_p_sequencer do? It declares a p_sequencer handle of your sequencer type and casts m_sequencer into it when the sequence starts, so you can reach the handles.
  4. How do you start a virtual sequence that has no sequencer? vseq.start(null), after you set its handles yourself.
  5. What happens with fork/join_none and no wait fork? The sequence returns while its children are still running. Anything after it, like a check or the end of the test, runs too early.
  6. lock() vs grab()? Both give one sequence the sequencer alone. A lock waits its turn, a grab goes to the front. Neither breaks a lock that is already held.
  7. How do you make a sequence start by itself in a phase? default_sequence through uvm_config_db, on <sequencer path>.<phase>_phase. Raise an objection or the phase may end without it.
  8. Your scoreboard is clean but a port got no traffic. How? The data was right, it just went to the wrong place. Check intent too, for example per-port counters against what the test meant to send (BUG=4).

Practice challenge

Add a reset step: a tav_reset_vseq that pulls rst_n low in the middle of step 3’s parallel traffic, then sets the block up again and sends 4 more bytes on each port. You will need the virtual sequencer to hold the reset interface too. Then decide what the scoreboard should do with bytes that were in flight when reset hit.

FAQ

What is a virtual sequence in UVM?

A sequence that sends no items itself. It starts other sequences on the sequencers of several agents and decides their order.

What is a virtual sequencer in UVM?

A sequencer with no driver that holds handles to the real sequencers, so virtual sequences can reach them through p_sequencer.

Do I always need a virtual sequencer to use a virtual sequence?

No. You can keep the handles inside the sequence and start it with start(null).

Why do I start a virtual sequence with null?

Because it sends no items, it does not need a sequencer. start(null) just runs its body.

Where should I set the virtual sequencer’s handles?

In the env’s connect_phase, after the agents are built. Check them in end_of_elaboration_phase.

Can a virtual sequence start another virtual sequence?

Yes. Start the child on the same virtual sequencer and pass this as its parent.

When should I use lock() instead of grab()?

Use lock() for a group of items that must stay together. Keep grab() for urgent work like interrupt handling.

References

Related lessons

2 Comments

Comments are closed.