Hand-Shakes: "Bench, Start Traffic Now"

Many chip tests need the bench to act at the right moment: send a byte, pull a pin, load a vector. Firmware and bench run side by side and talk through events. Learn the hand-shake, and the race that waits in every careless one.

You Will Be Able To

  • Write a hand-shake from firmware to bench and back with TB_EVENT and TB_ARG.
  • Read the two lanes of a run from the bench's EVENT and ARG lines.
  • Explain why each round needs its own reply number.
  • Say which side of a hand-shake is wrong when a round fails.
Lesson
V4.4
Time
26 min
Runs on
Your browser
Check
5 questions

A UART receive test passes every night. Then the bench team makes its byte sender a little slower, and the test fails on round 2 with "no byte". Nothing in the chip changed. Two programs that never agreed on who waits for whom had been lucky.

Two Lanes, Events CrossingStep through one round: the firmware asks, the bench acts, the bench answers, the firmware goes on.
  • Processor
  • External
  • Pads
  • Data
Two Lanes, Events CrossingStep through one round: the firmware asks, the bench acts, the bench answers, the firmware goes on. processor Firmware: ask TB_EVENT = 0x102 atcycle 736 external Bench: wait_event its table row waits for0x102 pads Bench: act sends 0x42 on theUART pad external Bench: answer TB_ARG = 0x202 atcycle 1120 processor Firmware: go on tb_wait_event(0x202)returns; read the byte eventnext rownext rowreply
  1. processor Firmware: askTB_EVENT = 0x102 at cycle 736
    • event → Bench: wait_event
  2. external Bench: wait_eventits table row waits for 0x102
    • next row → Bench: act
  3. pads Bench: actsends 0x42 on the UART pad
    • next row → Bench: answer
  4. external Bench: answerTB_ARG = 0x202 at cycle 1120
    • reply → Firmware: go on
  5. processor Firmware: go ontb_wait_event(0x202) returns; read the byte
  1. The firmware asks

    A store to TB_EVENT carries a number the bench is waiting for.

  2. The bench acts

    Its table moves to the next row: here, a byte on the UART line. It may take as long as it needs.

  3. The bench answers

    Only when the action is done does it deposit the reply in TB_ARG.

  4. The firmware goes on

    It waits for this round's own reply number, then uses what the bench did.

Read the diagram as text
  • Firmware: ask: TB_EVENT = 0x102 at cycle 736
  • Bench: wait_event: its table row waits for 0x102
  • Bench: act: sends 0x42 on the UART pad
  • Bench: answer: TB_ARG = 0x202 at cycle 1120
  • Firmware: go on: tb_wait_event(0x202) returns; read the byte
  • Firmware: ask → Bench: wait_event: event
  • Bench: wait_event → Bench: act: next row
  • Bench: act → Bench: answer: next row
  • Bench: answer → Firmware: go on: reply

Round 2 of the lab run below; cycles recorded on ap_soc v1.5.

Before You Start

Read The Mailbox first. The bench's table rows are listed in its manual, tools/soc/ap_soc/bench/soc_tb/README.md.

Five questions this page answers
Question
1Why does a chip test need the bench to act at a chosen moment?
2How do the firmware and the bench signal each other?
3What makes a hand-shake safe against a slow side?
4What does a race between the two sides look like in a run?
5How do events reach a bench written in UVM?

The Idea: Two Programs, One Test

A chip test with the processor is really two programs. The firmware runs on the core. The bench runs beside it: models on the pads, a table of rows, or a UVM sequence. Much of what the firmware tests must come from outside. It may be a byte on the receive pin, a pulse, a reset or data. And it must come when the firmware is ready for it.

So the two programs hand the test back and forth. The firmware writes an event: "I am ready, do it now". The bench acts and, when it has finished, writes a reply into TB_ARG. The firmware waits for that reply and only then looks at the result.

Every hand-shake has two promises, one on each side. The firmware promises to wait for the reply; the bench promises to reply only when its action is done. Break either one and the test has a race.

New Words

New words on this page
WordMeaning
Hand-shakeOne request and one reply between firmware and bench, so that each side waits for the other.
EventA number the firmware writes to TB_EVENT; the bench may wait for it or act on it.
ReplyA number the bench deposits in TB_ARG to say its part is done.
RaceA result that depends on which side happens to be faster.
Stale replyA reply left in TB_ARG from an earlier round, which a careless wait takes as new.
Bench rowOne line of the bench's table: who, operation, address, data, mask, expected, delay, repeat.

The Smallest Example: Two Rounds by Hand

Suppose every round waits for the same reply number, 0x201. Follow TB_ARG through two rounds.

TB_ARG over two rounds
StepTB_ARGFirmwareSafe?
Ask round 10waits for 0x201Yes
Bench replies 0x2010x201goes onYes
Ask round 20x201waits for 0x201: already thereNo: the wait ends at once
Bench replies 0x202, later0x202already reading the resultToo late

TB_ARG keeps its last value until the bench writes again.

With a reply per round, 0x201 then 0x202, the second wait cannot end early. That is all the safety there is to it.

The Rules

  1. Rule 1: Ask, then wait for your own reply.

    Each round gets its own reply number, so an old value cannot end a new wait.

  2. Rule 2: The bench replies after it acts.

    The reply means "done", so it is the last row of the bench's part.

  3. Rule 3: Never wait on time.

    A fixed delay works until one side is slower. Wait on the reply.

  4. Rule 4: Set the bench's TB_ARG with a number nobody waits for.

    An argument at the start must not look like a reply.

  5. Rule 5: Give every wait a limit and a fail code.

    A side that never answers becomes a report, not a hang.

Sources for These Rules

The kit's calls: tb_event, tb_arg and tb_wait_event in tools/soc/fwkit/tb.c; "use distinct IDs for consecutive replies" in its README.

The bench's rows and its deposit of TB_ARG: tools/soc/ap_soc/bench/soc_tb/README.md; docs/soc/ap_soc-facts.md 27.12.

Hand-shakes between processor code and a test bench through shared registers are common practice. Not tied to one book of the library.

Now Run It: The Same Bench, Two Firmwares

The bench's rows answer three requests. Each sends one byte on the UART receive pad and then replies. The second request is served 300 cycles late, as a slower bench would.

The bench's rows (who op address data mask expect delay repeat).
tb wait_event 00000101 00000000 ffffffff 00000000 0 1
tb uart_send 00000000 00000041 ffffffff 00000000 0 1
tb arg 00000000 00000201 ffffffff 00000000 0 1
tb wait_event 00000102 00000000 ffffffff 00000000 0 1
tb wait 00000000 00000000 00000000 00000000 300 1
tb uart_send 00000000 00000042 ffffffff 00000000 0 1
tb arg 00000000 00000202 ffffffff 00000000 0 1
tb wait_event 00000103 00000000 ffffffff 00000000 0 1
tb uart_send 00000000 00000043 ffffffff 00000000 0 1
tb arg 00000000 00000203 ffffffff 00000000 0 1
HANDSHAKE_C: each round waits for its own reply.
#include "drivers/uart.h"
#include "tb.h"

int main(void)
{
    reg_write32(SYSCTL_PINMUX, 2u << 2);     /* pad IO1 carries UART RX */
    reg_write32(UART0_DIV, 8);
    for (unsigned r = 1; r <= 3; r++) {
        tb_event(0x100 + r);                 /* bench: send byte r now */
        tb_wait_event(0x200 + r);             /* bench: it has been sent */
        unsigned status = reg_read32(UART0_STATUS);
        tb_puts("round ");
        tb_put_hex(r);
        if (FIELD_GET(UART0_STATUS_RX_READY, status)) { tb_puts(" byte "); tb_put_hex(reg_read32(UART0_RXDATA)); }
        else tb_puts(" none");
        tb_putc('\n');
    }
    return 0;
}
Measured on our bench (soc_tb, Verilator 5.052 2026-09-05): HANDSHAKE_C.
EVENT 240: 00000101
ARG 324: 00000201
CONSOLE 723: round 00000001 byte 00000041
EVENT 736: 00000102
ARG 1120: 00000202
CONSOLE 1519: round 00000002 byte 00000042
EVENT 1532: 00000103
ARG 1616: 00000203
CONSOLE 2015: round 00000003 byte 00000043

Each round reads its byte. In round 2 the request went out at cycle 736 and the reply came at 1120. The firmware simply waited: rules 1 and 3 at work.

Then the same rows ran with a firmware that waits for 0x201 in every round.

STALE_C: the same rounds, one reply number.
EVENT 236: 00000101
ARG 320: 00000201
CONSOLE 718: round 00000001 byte 00000041
EVENT 733: 00000102
CONSOLE 1029: round 00000002 none
EVENT 1044: 00000103
ARG 1116: 00000202
ARG 1198: 00000203
CONSOLE 1340: round 00000003 none

Round 2 asked at cycle 733 and its wait ended at once, on the old reply. At cycle 1029 it found no byte. The run still passed, because this lab firmware does not fail on "none". A real test would have reported a UART fault that does not exist.

Why It Is Built This Way

The bench could watch the chip and act on its own, for example "send a byte 500 cycles after reset". That is a hidden schedule: it breaks the day the firmware gets faster or slower. Events make the schedule explicit and let either side take as long as it needs.

Replies through TB_ARG cost a back-door deposit and a few polls. They need no interrupt, and they work the same on every platform the mailbox works on. When the event must be a real pin, as for a wake-up or a reset, the bench uses a pad instead.

The Trap: The Bench That Answered Too Soon

A hand-shake can be broken from the bench side too. A bench author writes the reply row first "to keep the firmware busy" and the action after. The firmware keeps its promise, waits for its own reply, and looks at a result that is not there yet. It is the firmware that fails, so people debug the firmware.

Read a failing round as two lanes: what did each side do, in which order? The bench's EVENT and ARG lines and the firmware's loads give both. The second practice problem is a recorded run where you decide which side broke its promise.

At Scale

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. This chip's bench is a table of rows.

  1. UVM benches

    In a UVM chip bench a mailbox monitor turns each event into a transaction. A virtual sequence waits for it and starts pad sequences. The third practice problem pairs a C test with such a sequence on this chip.

  2. Many cores

    With several cores, events carry the core's number, and the bench serves each core's requests in order.

  3. Real pins

    Some events must be pins, such as a wake pad or a reset. The bench pulses a pad, and the firmware sees a real interrupt.

Try It

Run the slice: it lays the good run's EVENT and ARG lines on two lanes.

Feed it the stale run's lanes. Which round ends on an old reply?

The predict question below uses the first five lines of the stale run.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 240 firmware -> bench request 1 324 bench -> firmware reply 1 723 wait 1 over, after its own reply 736 firmware -> bench request 2 1120 bench -> firmware reply 2 1519 wait 2 over, after its own reply 1532 firmware -> bench request 3 1616 bench -> firmware reply 3 2015 wait 3 over, after its own reply

Check Yourself

Predict the outputThe slice now reads the first five lines of the stale run. What is the last line it prints?
// One run's hand-shakes, as the bench logged them: EVENT lines are the firmware's requests 0x101..,
// ARG lines the bench's replies 0x201... CONSOLE lines show when each wait was over.
module tb;
  localparam int N = 5;
  int          cycle [N];
  string       lane  [N];
  logic [31:0] value [N];
  int asked = 0, reply_at = -1;
  logic [31:0] last_reply = 0;
  initial begin
    cycle[0] = 236; lane[0] = "EVENT"; value[0] = 32'h00000101;
    cycle[1] = 320; lane[1] = "ARG"; value[1] = 32'h00000201;
    cycle[2] = 718; lane[2] = "CONSOLE"; value[2] = 32'h00000000;
    cycle[3] = 733; lane[3] = "EVENT"; value[3] = 32'h00000102;
    cycle[4] = 1029; lane[4] = "CONSOLE"; value[4] = 32'h00000000;
    for (int i = 0; i < N; i++) begin
      if (lane[i] == "EVENT") begin asked = value[i] - 32'h100; $display("%5d firmware -> bench  request %0d", cycle[i], asked); end
      else if (lane[i] == "ARG") begin last_reply = value[i]; $display("%5d bench -> firmware  reply %0d", cycle[i], value[i] - 32'h201 + 1); end
      else if (last_reply == 32'h200 + asked) $display("%5d wait %0d over, after its own reply", cycle[i], asked);
      else $display("%5d wait %0d over, on an old reply: a race", cycle[i], asked);
    end
  end
endmodule
ConceptWhy does each round of a hand-shake need its own reply number?
ConceptWhen should the bench write its reply?
ConceptA round fails with "no byte". The firmware waited for its own reply. Where do you look first?
ConceptWhat does a race look like in the bench's log?

Common Mistakes

Wrong Wait for the same reply number in every round.

The old reply ends the next wait at once.

Fix Give each round its own reply number.

Wrong Wait a fixed number of cycles for the bench.

It breaks the day the bench is slower.

Fix Wait for the reply, with a limit and a fail code.

Wrong Reply before acting in the bench.

The firmware then reads a result that is not there yet.

Fix Make the reply the last row of the bench's part.

Remember

  • Firmware asks with TB_EVENT; the bench acts, then replies in TB_ARG.
  • Each round needs its own reply number; TB_ARG keeps the last one.
  • The bench replies only after it acts; nobody waits on time.
  • When a round fails, read both lanes and find the broken promise.

Practice

Report a problem with this page