A C test and an SV sequence run as two genuinely independent threads of execution, advancing at their own pace with no built-in awareness of each other. Most interesting SoC scenarios need them to meet at specific points anyway - the DMA has to be configured before the SV side injects a fault on its input, or the C test needs to know an interrupt actually arrived before it checks the result. Making that meeting happen reliably is its own design problem.

10 min read | Intermediate Level

What You'll Learn

  • Why a C test and an SV sequence need an explicit synchronization mechanism at all
  • The trade-off between polling a shared flag and waiting on an interrupt
  • How to coordinate a test that needs more than one handoff, using a small mailbox protocol
  • Why every wait on the other side needs a timeout, no matter which mechanism is used

Two Threads, One DUT

The CPU executes C instructions at whatever rate the core model runs at. An SV virtual sequence advances through its body() task at whatever rate simulation events let it. Neither one blocks for the other automatically - if a virtual sequence needs to inject traffic only after the C test has configured a peripheral, something has to make the sequence actually wait for that to happen, or it'll act on hardware that isn't ready yet.

That "something" is always the same basic idea in different clothes: one side writes a value to a location the other side is watching, and the watching side doesn't proceed until it sees the expected value show up.

Three ways a C test and an SV sequence synchronise: the SV side polls a done flag the C test writes, waits for an interrupt the C test causes, or steps through stage values in a shared mailbox

Pattern 1: Polling a Shared Flag

The simplest handshake is a spin loop. The C test writes a done flag to a fixed address once it reaches the synchronization point; the SV sequence repeatedly reads that same address until it sees the expected value. There are two quite different ways to implement that read, and they trade off differently.

The most direct version reads backdoor - the sequence peeks the memory model or a register field directly, with no bus activity involved, and simply keeps checking it in a loop:


virtual task body();
  bit [31:0] flag_val;
  do begin
    #(POLL_PERIOD_NS);
    mem_model.backdoor_read(SYNC_FLAG_ADDR, flag_val);
  end while (flag_val != SYNC_FLAG_DONE);
endtask

Polling is easy to write and easy to reason about, but the polling interval matters. Poll too tightly and the sequence burns real wall-clock simulation time doing nothing useful; poll too loosely and a handshake with a narrow valid window can be missed entirely, which behaves like a bug in the DUT rather than a bug in the poll rate.

The other version keeps the C side identical - it still just writes a flag to a fixed address - but moves the checking to the frontdoor side of the boundary instead of reading backdoor. The C test performs an ordinary register write over the real bus, and a passive monitor or agent that's already sitting on that interface - very likely reused from the block-level environment - observes the transaction go by and reports it through its analysis port, exactly the way it would report any other bus transaction:


class sync_flag_subscriber extends uvm_subscriber #(reg_bus_txn);
  `uvm_component_utils(sync_flag_subscriber)

  uvm_event sync_event;

  function new(string name, uvm_component parent);
    super.new(name, parent);
    sync_event = new("sync_event");
  endfunction

  function void write(reg_bus_txn t);
    if (t.is_write && t.addr == SYNC_FLAG_ADDR && t.data == SYNC_FLAG_DONE)
      sync_event.trigger();
  endfunction
endclass

The virtual sequence waits on that event the same way it would wait on an interrupt - with a timeout branch alongside it, since a wait with no timeout is a mistake regardless of which mechanism produces the event:


fork
  begin
    sync_event.wait_trigger();
  end
  begin
    #(TIMEOUT_NS);
    `uvm_fatal("SYNC_TIMEOUT", "Sync flag write never observed on the bus")
  end
join_any
disable fork;

The two versions look similar from the C side - both are just a single store to a fixed address - but they exercise very different parts of the DUT. Backdoor polling never sends a single bus transaction, so it proves nothing about the interconnect, the register interface, or any address decode logic sitting between the CPU and that flag's storage. Frontdoor recognition forces the write to actually travel the real path, and reuses a monitor that's probably already verified and maintained at the block level, instead of adding a second, parallel way of reading the same location purely for synchronization.

If a monitor for that interface already exists in the block-level environment, subscribing to its analysis port for synchronization is usually less work than writing a new backdoor poll loop, and it comes with whatever protocol checking that monitor already does for free.

Pattern 2: An Interrupt-Driven Handshake

Instead of a flag the SV side has to keep checking, the C test can trigger a real interrupt the DUT asserts, and the SV sequence simply waits on that interrupt signal going active. This is usually the more realistic choice, because it's the same mechanism a real driver would use, and it doesn't waste simulation cycles re-reading a location that hasn't changed yet:


virtual task body();
  // ... drive setup traffic on other agents while the C test
  // configures the DUT and arms the interrupt ...

  fork
    begin
      wait (dut_irq_vif.irq_line === 1'b1);
    end
    begin
      #(TIMEOUT_NS);
      `uvm_fatal("SYNC_TIMEOUT",
        "C test never asserted completion interrupt")
    end
  join_any
  disable fork;

  // interrupt seen - safe to proceed
endtask

The C side of this handshake is just whatever operation naturally causes the DUT to raise that interrupt - there's no explicit "signal SV" step to write, which is part of the appeal: the synchronization point falls out of real hardware behavior instead of being bolted on.

Pattern 3: A Mailbox for More Than One Handoff

A scenario with a single "I'm done" moment is the easy case. A scenario that needs several handoffs in sequence - configure, signal ready, inject a fault, signal injected, check the response - needs something richer than one flag with two states.

A small mailbox at a fixed address, using a distinct value per stage, covers this without much more complexity than the single-flag version:


volatile unsigned int * const SYNC_MAILBOX = (unsigned int *) 0x80002010;

#define STAGE_IDLE       0x0
#define STAGE_CONFIGURED 0x1
#define STAGE_FAULT_SEEN 0x2
#define STAGE_DONE       0x3

void _start(void) {
    configure_dma();
    *SYNC_MAILBOX = STAGE_CONFIGURED;

    while (*SYNC_MAILBOX != STAGE_FAULT_SEEN) { /* wait for SV */ }

    handle_fault();
    *SYNC_MAILBOX = STAGE_DONE;
    while (1) { }
}

The SV virtual sequence drives the matching half of the same handshake - waiting for STAGE_CONFIGURED, injecting its fault, writing STAGE_FAULT_SEEN, then waiting for STAGE_DONE before checking the final result. Each stage value only ever means one thing, which avoids the same overloaded-status problem that comes up with a plain pass/fail flag.

The mailbox handshake in time order: the C test configures the DMA and writes STAGE_CONFIGURED, the SV sequence injects a fault and writes STAGE_FAULT_SEEN, the C test handles it and writes STAGE_DONE, then SV checks the result

Keep the mailbox's stage values monotonically increasing and never reused within a test. If a stage value can legitimately appear twice, a sequence that's waiting for it can't tell whether it just observed the first occurrence or the second, and the two situations usually call for different actions.

Common Mistakes

Mistake: Waiting on the other side with no timeout at all.

Any wait for a flag, an interrupt, or a mailbox stage that never actually arrives - because of a genuine DUT bug - needs to fail with a clear message instead of hanging the simulation until an external watchdog kills the run with no useful diagnostic.

Mistake: Both sides waiting on each other at the same synchronization point.

A C test waiting for an SV-side signal that only gets written after the SV sequence sees a C-side signal is a deadlock by construction. Tracing through the intended order of every handoff, on paper, before writing either side, catches this long before it becomes a hung regression.

Mistake: Polling a backdoor address too fast for the mailbox write to ever land cleanly.

A poll loop reading the same cycle a multi-cycle write is still in flight can observe a transient, incorrect value. Backdoor writes should either be atomic with respect to the poll, or the poll should tolerate reading a stale-but-valid previous stage rather than garbage.

Summary

In this article, you learned:

  • A C test and an SV sequence advance independently, so any scenario that needs them to meet at a specific point requires an explicit handshake
  • Polling a shared flag is simple but trades off simulation time against how easily a narrow window can be missed
  • An interrupt-driven handshake reuses real DUT behavior instead of a synthetic flag, and avoids wasted polling cycles
  • A multi-stage mailbox, with distinct values per stage, extends the same idea to scenarios that need more than one handoff - and every wait, regardless of mechanism, needs a timeout
Did you find what you were looking for?

Quiz

Test Your Knowledge

1 of 5 Score: 0 / 5