Lesson 5: Mailboxes, Semaphores and Events
A testbench runs several threads at once, and they need safe ways to pass data and signal each other. A mailbox is a queue between threads: one puts transactions in, another gets them out, and a bounded mailbox makes the producer wait when it is full. An event is a signal one thread triggers and others wait for. A semaphore is a set of keys that threads take and give back, so only one thread at a time uses a shared resource. In the FIFO testbench, the generator hands transactions to the driver through a mailbox.
Learning objectives
- Pass transactions between threads with a mailbox, and choose between a bounded and an unbounded one
- Signal between threads with events, and avoid the race between -> and @ in the same time step
- Protect a shared resource with a semaphore so a burst of transactions stays together
- Build a handshake that tells the generator when each transaction was driven
Prerequisites
- Lesson 2: the generator, driver and environment that this lesson looks inside
- Lesson 3: classes and handles, which is what a mailbox passes
- 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.
Threads in a testbench
Lesson 2's environment starts its generator, driver, monitor and scoreboard with fork and join_none: four threads that run at the same time and never call each other. Each pair that needs to talk shares a mailbox, and the environment waits for the end with a flag. This lesson looks at those connections.
Mailboxes
A mailbox #(fifo_txn) holds handles to transactions in order. put adds one, get removes one and waits if the mailbox is empty, and try_get returns at once with 0 when there is nothing to take. new() makes an unbounded mailbox; new(n) holds at most n, and put waits while it is full. The driver uses try_get so it can hold the FIFO idle on a clock with no transaction.
// The driver applies one transaction per clock, on the falling edge, and holds the FIFO idle when it has none.
class driver;
virtual fifo_if vif;
mailbox #(fifo_txn) from_gen;
function new(virtual fifo_if v, mailbox #(fifo_txn) m); vif = v; from_gen = m; endfunction
task run();
fifo_txn t;
forever begin
@(negedge vif.clk);
if (from_gen.try_get(t)) begin
vif.wr_en = t.wr_en; vif.rd_en = t.rd_en; vif.wr_data = t.data;
end else begin
vif.wr_en = 0; vif.rd_en = 0;
end
end
endtask
endclass
Worked example: bounded against unbounded
The generator below records the most transactions ever waiting and triggers an event, put_all, after its last put. The test runs 40 random transactions twice, with +BOUND=0 (unbounded) and +BOUND=1, and waits for the event.
class paced_generator extends generator;
event put_all; // triggered when the last transaction is in the mailbox
int max_held;
function new(mailbox #(fifo_txn) m); super.new(m); endfunction
task run();
foreach (plan[i]) begin
to_drv.put(plan[i]); // blocks while a bounded mailbox is full
if (to_drv.num() > max_held) max_held = to_drv.num();
end
$display("generator: last put at %0d ns, %0d waiting in the mailbox", $time, to_drv.num());
-> put_all;
done = 1;
endtask
endclass
env = new(vif);
env.g2d = new(bound); // 0: unbounded
g = new(env.g2d); env.gen = g; env.drv.from_gen = env.g2d;
repeat (40) begin
fifo_txn t = new();
if (!t.randomize()) $fatal(1, "randomize failed");
g.plan.push_back(t);
end
env.run();
@(g.put_all);
$display("test: put_all seen at %0d ns", $time);
env.finish();
$display("mailbox bound %0d: most waiting at once %0d", bound, g.max_held);
generator: last put at 20 ns, 40 waiting in the mailbox
test: put_all seen at 20 ns
44 clocks checked, 16 writes, 16 reads, 0 errors: PASS
mailbox bound 0: most waiting at once 40
generator: last put at 410 ns, 1 waiting in the mailbox
test: put_all seen at 410 ns
44 clocks checked, 16 writes, 16 reads, 0 errors: PASS
mailbox bound 1: most waiting at once 1
Both runs drive the same 40 transactions and pass. The difference is the generator's view of time. Unbounded, it finished at 20 ns, before the driver had applied a single transaction, so anything it does after its last put, such as reading coverage to choose the next stimulus, would be based on traffic that had not happened yet. Bounded to one, its last put returned at 410 ns, one clock before the driver applied it. A bounded mailbox keeps the generator in step with the pins.
Events
-> ev triggers an event; @(ev) waits for the next trigger. The trap is timing: if the trigger happens before the waiting thread reaches @(ev), even in the same time step, the trigger is missed and the thread waits forever. wait (ev.triggered) is true for the rest of the time step in which ev was triggered, so it catches a trigger that came first (Exercise 2). In the worked example the test reaches @(g.put_all) before the generator runs, because fork with join_none starts its threads only when the parent thread waits.
Semaphores
A semaphore holds keys. get(1) takes a key and waits if none is left; put(1) gives it back. With one key, only one thread holds it at a time, which is how two generators can share one driver without mixing their bursts: each takes the key, puts a whole burst, and gives the key back (Exercise 3).
Common mistakes
- Using an unbounded mailbox and then treating the generator's finish time as the end of the stimulus; it can finish before a single transaction reaches the pins.
- Waiting with @(ev) for an event that may already have been triggered in the same time step; use wait (ev.triggered) or trigger after the waiter is ready.
- Calling get in a driver that must keep the clock busy: get blocks, so the pins keep the last request until the next transaction arrives. Use try_get and drive idle.
- Taking a semaphore key and returning early on an error without putting it back, so every other thread waits forever.
- Reusing one transaction object for every put: the mailbox holds handles, so every entry is the same object.
Quick check
1. What does put do on a bounded mailbox that is full?
It waits until another thread takes a transaction out. try_put returns 0 instead of waiting.
2. Unbounded, the generator finished at 20 ns. Why is that a problem?
No transaction had reached the pins yet. Anything the generator decides after its last put, like steering toward coverage holes, would be based on traffic that had not happened.
3. Why did the test's @(g.put_all) not miss the trigger in the unbounded run, where the generator triggers at time 20?
fork with join_none starts its threads only when the parent waits. The test reached @(g.put_all) first, and only then did the generator run and trigger it.
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). Call try_put twice on a mailbox bounded to one. What does each call return?
The first succeeds and returns 1; the second finds the mailbox full and returns 0 at once, without waiting.
// E1: try_put never blocks: it returns 0 when a bounded mailbox is full.
mailbox #(fifo_txn) m = new(1);
fifo_txn a = new(1, 0, 1), b = new(1, 0, 2);
$display("E1: first try_put returned %0d, second %0d; %0d waiting", m.try_put(a), m.try_put(b), m.num());
end
E1: first try_put returned 1, second 0; 1 waiting
E2 (easy). Trigger an event, then in the same time step wait for it once with @ and once with wait (.triggered).
@(go) arrives after the trigger and waits for a next one that never comes. wait (go.triggered) sees the trigger, because triggered stays true for the rest of the time step.
// E2: an event triggered earlier in the same time step: @ misses it, wait (.triggered) does not.
event go;
bit at_woke, wait_woke;
fork
-> go;
begin #0 fork
begin @(go); at_woke = 1; end
begin wait (go.triggered); wait_woke = 1; end
join_none end
join
#100 $display("E2: after 100 ns: @(go) woke %0d, wait (go.triggered) woke %0d", at_woke, wait_woke);
end
E2: after 100 ns: @(go) woke 0, wait (go.triggered) woke 1
E3 (medium). Two generators, one writing and one reading, share the driver's mailbox. Keep each burst of four together with a semaphore, and show what happens without it.
With the key, each generator puts a whole burst before the other can start: WWWW, RRRR and so on. Without it, the first write burst is split after one write.
// E3: a generator that puts bursts of four, optionally holding a semaphore key for the whole burst.
class burst_generator;
mailbox #(fifo_txn) to_drv;
semaphore key;
bit wr, use_key;
function new(mailbox #(fifo_txn) m, semaphore s, bit wr, bit use_key);
to_drv = m; key = s; this.wr = wr; this.use_key = use_key;
endfunction
task run(int bursts);
repeat (bursts) begin
if (use_key) key.get(1);
repeat (4) begin
fifo_txn t = new(wr, !wr, 8'($urandom));
to_drv.put(t);
end
if (use_key) key.put(1);
end
endtask
endclass
// E3: two generators share the driver's mailbox (bounded to 1 so they take turns). +NOSEM drops the key.
semaphore key = new(1);
bit sem = !$test$plusargs("NOSEM");
mailbox #(fifo_txn) m = new(1);
burst_generator wg = new(m, key, 1, sem), rg = new(m, key, 0, sem);
string order;
fork wg.run(2); rg.run(2); join_none
repeat (16) begin
fifo_txn t;
@(posedge clk) m.get(t); // the driver takes one per clock
order = {order, t.wr_en ? "W" : "R"};
end
if (sem) $display("E3: with the semaphore: %s", order);
else $display("E3: without it: %s", order);
end
E3: with the semaphore: WWWWRRRRWWWWRRRR
E3: without it: WRRRRRRRRWWWWWWW
E4 (hard). Make the generator wait until each transaction has been driven before it puts the next one, the way a UVM sequence waits for item_done.
Give the driver a second mailbox and have it put each transaction back once it is on the pins; the generator gets the acknowledgement before its next put. driver::run is not virtual, so start the new driver's run directly.
// E4: a driver that answers every transaction on a second mailbox once it is on the pins, like item_done.
class acking_driver extends driver;
mailbox #(fifo_txn) acks;
function new(virtual fifo_if v, mailbox #(fifo_txn) m, mailbox #(fifo_txn) a); super.new(v, m); acks = a; endfunction
task run();
fifo_txn t;
forever begin
@(negedge vif.clk);
if (from_gen.try_get(t)) begin
vif.wr_en = t.wr_en; vif.rd_en = t.rd_en; vif.wr_data = t.data;
acks.put(t);
end else begin
vif.wr_en = 0; vif.rd_en = 0;
end
end
endtask
endclass
// E4: the generator waits for each acknowledgement before it puts the next transaction.
mailbox #(fifo_txn) acks;
acking_driver d;
int n;
acks = new(); // assigned here, not in the declarations: those run at time 0
d = new(vif, env.g2d, acks);
fork d.run(); env.mon.run(); env.sb.run(); join_none // d.run(): driver::run is not virtual
repeat (40) begin
fifo_txn t, done_t;
t = new();
if (!t.randomize()) $fatal(1, "randomize failed");
env.g2d.put(t);
acks.get(done_t);
n++;
end
$display("E4: %0d transactions acknowledged, the last at %0d ns", n, $time);
repeat (3) @(posedge clk);
#1 $display("E4: %0d clocks checked, %0d errors: %s", env.sb.cycles, env.sb.errors, env.sb.errors ? "FAIL" : "PASS");
end
E4: 40 transactions acknowledged, the last at 420 ns
E4: 43 clocks checked, 0 errors: PASS
Interview questions
- Q1. When would you use a bounded mailbox? Answer: when the producer must stay in step with the consumer, for example a generator that reads coverage to choose its next transaction. Unbounded, the FIFO generator finished at 20 ns with 40 transactions queued; bounded to one, it finished at 410 ns, one clock before the last was driven.
- Q2. What is the difference between @(ev) and wait (ev.triggered)? Answer: @(ev) waits for the next trigger, so it misses one that already happened in the same time step. wait (ev.triggered) is true for the rest of the time step after a trigger, so it does not.
- Q3. What is a semaphore used for in a testbench? Answer: to give one thread at a time access to a shared resource, such as a driver's mailbox or a bus, so a multi-transaction operation is not interleaved with another thread's.
Summary
- A mailbox passes transaction handles between threads in order; a bounded one makes the producer wait.
- Unbounded, the FIFO generator ran ahead of the pins; bounded to one, it stayed in step with the driver.
- Events signal between threads; @ can miss a trigger in the same time step, wait (.triggered) cannot.
- A semaphore keeps a shared resource with one thread at a time, so bursts stay together.
Related
Last verified
Run in Vivado xsim 2025.2 on 26 November 2026: the bounded and unbounded runs, the mailbox traces in the plot, and all four exercise solutions. Outputs are copied from those runs and kept in scripts/verification/results; lib/verificationCourse.test.js checks them against this page.