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.
- Processor
- External
- Pads
- Data
- The firmware asks
A store to TB_EVENT carries a number the bench is waiting for.
- 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.
- The bench answers
Only when the action is done does it deposit the reply in TB_ARG.
- 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.
| Question | |
|---|---|
| 1 | Why does a chip test need the bench to act at a chosen moment? |
| 2 | How do the firmware and the bench signal each other? |
| 3 | What makes a hand-shake safe against a slow side? |
| 4 | What does a race between the two sides look like in a run? |
| 5 | How 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
| Word | Meaning |
|---|---|
| Hand-shake | One request and one reply between firmware and bench, so that each side waits for the other. |
| Event | A number the firmware writes to TB_EVENT; the bench may wait for it or act on it. |
| Reply | A number the bench deposits in TB_ARG to say its part is done. |
| Race | A result that depends on which side happens to be faster. |
| Stale reply | A reply left in TB_ARG from an earlier round, which a careless wait takes as new. |
| Bench row | One 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.
| Step | TB_ARG | Firmware | Safe? |
|---|---|---|---|
| Ask round 1 | 0 | waits for 0x201 | Yes |
| Bench replies 0x201 | 0x201 | goes on | Yes |
| Ask round 2 | 0x201 | waits for 0x201: already there | No: the wait ends at once |
| Bench replies 0x202, later | 0x202 | already reading the result | Too 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
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.
Rule 2: The bench replies after it acts.
The reply means "done", so it is the last row of the bench's part.
Rule 3: Never wait on time.
A fixed delay works until one side is slower. Wait on the reply.
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.
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.
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#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;
}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 00000043Each 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.
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 noneRound 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
This section looks ahead. This chip's bench is a table of rows.
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.
Many cores
With several cores, events carry the core's number, and the bench serves each core's requests in order.
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.
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 replyCheck Yourself
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.