Trace Transport: Funnel, Flush and Buffer
Trace bytes from several sources share one link into one small memory. The memory keeps the newest bytes, and the byte you need most can be the one it drops.
You Will Be Able To
- Follow bytes from two trace sources through a shared link, and say on which edges each one moves.
- Say what a flush asks for, and what its acknowledge means.
- Work out what a circular buffer still holds, and why a lost start packet spoils the rest.
- Lesson
- S8.10
- Time
- 25 min
- Runs on
- Your browser
- Check
- 5 questions
The capture is in. The buffer holds four bytes: two packets of the program and two timer events. The decoder reads them and prints nothing at all. Every byte is good, and the trace is useless.
Before You Start
You need Trace: a trace source, a packet, and the start packet without which nothing can be decoded.
It helps to have seen a wait on a bus in A Bus in Six Wires. The handshake here is defined on this page.
| Question | |
|---|---|
| 1 | How do two trace sources share one link without mixing their bytes? |
| 2 | On which clock edges does a byte move? |
| 3 | What does a flush ask for, and what does its acknowledge say? |
| 4 | What does a circular buffer hold when more bytes arrive than it has places? |
| 5 | Five bytes go into a buffer of four places. Which byte is lost, and why can that spoil the capture? |
The Idea: A Shared Link into a Memory That Forgets
A chip has more than one trace source: one for each processor, and often some for timers or buses. Their bytes must end in one place, where the debugger can fetch them. A link for each source would cost too much. So the sources share one trace link.
Sharing needs two things. Each byte must carry the name of its source, or the streams could never be parted again. And a source must be able to wait while another one uses the link, or while the far end is busy. A pair of wires, valid and ready, does that.
The far end is often a small memory on the chip, a circular buffer. It never fills up and never stops the trace. It keeps the newest bytes and writes each new byte over the oldest. That is what you want before a fault: the last moments, always.
It has a price, and it is the scene at the top of this page. The oldest byte of a capture is often its start packet. Lose that one byte and the decoder cannot place any of the others.
New Words
| Word | Meaning |
|---|---|
| Trace link | The wires that carry trace bytes from the sources toward the place where they are kept. |
| Funnel | A block that lets one of several trace sources use the trace link at a time. |
| Source ID | A number that travels with each byte and names the trace source it came from. |
| Valid | A wire from the sender. 1 means: there is a byte on the link. |
| Ready | A wire from the receiver. 1 means: I can take a byte on this edge. |
| Stalled byte | A byte on the link at a clock edge with valid at 1 and ready at 0. It does not move, and the sender must keep it there. Such an edge is called a stall on this page. |
| Circular buffer | A memory with a fixed number of places. When all are full, the newest byte replaces the oldest. |
| Flush | A request from the receiver: send every byte that you are holding now. |
| Flush acknowledge | The answer to a flush: every byte that was waiting when you asked has now moved. |
| On this page | On the ATB | Driven by |
|---|---|---|
| valid | ATVALID | The sender |
| ready | ATREADY | The receiver |
| source ID | ATID, seven bits | The sender |
| flush | AFVALID | The receiver |
| flush acknowledge | AFREADY | The sender |
One Picture: From Two Sources into the Buffer
- Processor
- Peripheral
- Bus
- Memory
- Control / FSM
- Two sources
Each trace source has bytes waiting. The core source sends the packets of the program. The timer source sends one byte for each timer event.
- One link
A link to the buffer for each source would cost too much. A funnel lets one source at a time use a shared link. It adds the ID of the source to each byte.
- A byte moves, or waits
The funnel says valid when it has a byte. The buffer says ready when it can take one. The byte moves on a clock edge where both are 1. If not, it waits.
- A memory that forgets
The buffer has four places. The fifth byte goes into the place of the first. At any moment the buffer holds the newest four bytes.
- A flush
Before the capture stops, the buffer asks for a flush: send what you are holding now. The request travels against the bytes. The bytes still travel forward, one by one.
Read the diagram as text
- Core trace source: sends the packets of the program, with ID 01
- Timer trace source: sends timer events, with ID 02
- Funnel: lets one source at a time use the link
- Circular buffer: four places; the newest byte replaces the oldest
- Debugger: reads the buffer after the run
- Core trace source → Funnel: bytes, ID 01
- Timer trace source → Funnel: bytes, ID 02
- Funnel → Circular buffer: ID and byte
- Circular buffer → Funnel: flush request
- Circular buffer → Debugger: the last four bytes
The way of a trace byte in our lab. The buffer could also be a port that sends the bytes through pins to a recorder.
The Smallest Example, Worked by Hand
Start with one source, three bytes and a buffer of two places. No simulator yet. Call the bytes P, Q and R. The buffer is ready on every edge except e1.
| Edge | Byte on the link | Valid | Ready | Does it move? | The buffer holds |
|---|---|---|---|---|---|
| e0 | P | 1 | 1 | Yes | P |
| e1 | Q | 1 | 0 | No: a stall | P |
| e2 | Q | 1 | 1 | Yes | P, Q |
| e3 | R | 1 | 1 | Yes | R, Q |
| e4 | none | 0 | 1 | Nothing to move | R, Q |
Look at e1 and e2. The byte Q is on the link at both edges. It moved once, at e2. A reader who counts every row with valid at 1 counts Q twice.
Look at e3. Both places were full, so R went into the place of the oldest byte, P. The buffer now holds Q and R, and P is gone.
Now add a second source. The funnel takes one byte at a time and adds an ID: 01 or 02. The buffer stores the ID beside the byte. The bytes of one source keep their order. Between two sources the order is the choice of the funnel.
The Rules
Rule 1: A byte moves on a clock edge where valid and ready are both 1.
On no other edge.
Rule 2: During a stall the sender holds the same byte and the same ID.
The same byte on two rows is one byte, not two.
Rule 3: Every byte travels with the ID of its source.
The bytes of one source stay in their order.
Rule 4: The order in which two sources arrive is not the order of their events.
A byte may have waited behind the other source.
Rule 5: A flush asks for the bytes that were waiting when it was requested.
The acknowledge comes one edge after the last of them has moved. A flush clears nothing.
Rule 6: A circular buffer of N places holds the newest N bytes.
Count the moves. Every move beyond N has cost the oldest byte.
Sources for These Rules
Rule 1: Arm AMBA ATB Protocol Specification, IHI 0032 issue C, section 3.1. Rule 2: section 3.2. Rule 3: sections 4.1 and 4.1.1. Rule 5: section 4.2. The names of the wires: section 2.1.
Rule 4 follows from section 4.1.1: a funnel may take its sources in any order.
Rule 6 is how a circular buffer works. The buffer of four places, the two IDs and the rule "core source first" are choices of our lab.
The same handshake on another link: Valid and Ready.
Now Run It
The lab at the end of this page has both sources, the funnel and a buffer of four places. The core source holds three bytes: 40, 01 and 00. They stand for a start packet, a branch taken and a branch not taken. The timer source holds a1 and a0. Each row shows the link just before a clock edge.
e0 valid=1 ready=1 id=01 byte=40 flush=0 ack=0
e1 valid=1 ready=0 id=01 byte=01 flush=1 ack=0
e2 valid=1 ready=1 id=01 byte=01 flush=1 ack=0
e3 valid=1 ready=1 id=01 byte=00 flush=1 ack=0
e4 valid=1 ready=1 id=02 byte=a1 flush=1 ack=0
e5 valid=1 ready=1 id=02 byte=a0 flush=1 ack=0
e6 valid=0 ready=1 id=02 byte=00 flush=1 ack=1
e7 valid=0 ready=1 id=02 byte=00 flush=0 ack=0
oldest+0 id:byte=0101
oldest+1 id:byte=0100
oldest+2 id:byte=02a1
oldest+3 id:byte=02a0- Cycle 0
A synchronization byte enters the ring.
- Cycle 1
The sink pauses.
- Cycle 2
The first outcome crosses.
- Cycle 5
The last pending event arrives.
- Cycle 6
The flush acknowledgement follows.
Rising-edge samples from the trace lab. Read a byte only where valid and ready meet.
Rule 1 at work, at e0. Valid and ready are 1, and the byte 40 moves into the buffer.
Rule 2, at e1 and e2. The byte 01 is on the link twice. At e1 ready is 0: a stall. It moves at e2. The program took that branch once.
Rule 3, at e4. The ID changes from 01 to 02. The byte a1 belongs to the timer source and must not be read as a packet of the program.
Rule 5, from e1 to e6. The flush is requested at e1. The four waiting bytes move at e2, e3, e4 and e5. At e6 the acknowledge is 1, one edge after the last of them.
Rule 6, in the last four rows. Five bytes moved: at e0, e2, e3, e4 and e5. The buffer has four places. The oldest row is 0101, the byte of e2. The byte 40 of e0 is gone, and it was the start packet. That is the scene at the top of this page.
This lab leaves things out. The sources are two lists that test code fills before the run, and nothing new arrives during the flush. The funnel always serves the core source first. Each move carries one byte. A real link can carry several bytes on one edge.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| A funnel and an ID on each byte | A link and a buffer for each source | One link and one buffer for the chip. A busy source can hold up a quiet one. |
| A stall when the receiver is busy | Drop the byte | No byte is lost on the link. The wait moves back into the source, which has little room of its own. |
| A circular buffer on the chip | Send every byte through pins to a recorder | No pins, and it works at full speed. Only the last few bytes are kept. |
The Trap: Four Good Bytes and No Start
Back to the capture that printed nothing. The engineer had asked for a flush before stopping, which is right. The flush pushed the last waiting bytes into the buffer, and one of them took the place of the start packet. The buffer was full of good bytes that could not be placed.
There are two cures. A larger buffer holds more. And a trace source that repeats its start packet gives the decoder a new place to begin, wherever the buffer was cut.
A second trap is at e1 of the lab. A decoder that takes a byte on every row with valid at 1 reads the branch twice. It then rebuilds one pass through a loop that the program never made.
A third trap is the order. The timer byte a1 reached the buffer after both branches. That does not mean the timer event came after them. It waited behind the core source. To order events of two sources, each byte needs the time at which it was made.
At Scale
This section looks ahead. Two sources and four places become a trace system with a budget.
A buffer, or a port
The bytes can end in a buffer on the chip, or leave through pins to a recorder with far more memory. Some chips do both.
A budget of bytes
Add up what all sources make in the busiest moment. Compare it with what the link can move. The rest has to wait somewhere.
Where the first byte was lost
A source that cannot wait any longer drops trace and says so. A larger buffer at the end cannot bring those bytes back.
Time on each byte
With the time of its making on each byte, the events of many sources can be put in their true order.
Try It
Run the lab. Count the rows in which valid and ready are both 1. Then compare the count with the four rows of the buffer.
Change edge_sample(0,1) to edge_sample(1,1), so that the buffer is always ready. On which edge does ack become 1 now?
Give the buffer a wait at the start: change the first edge_sample(1,0) to edge_sample(0,0). Which byte is the oldest in the buffer now?
Names Printed by the Lab
| Name | Meaning |
|---|---|
| e0, e1, ... | Rising edges of the clock. A row shows the link just before that edge. |
| valid, ready | The two wires of the handshake on the link. |
| id | The ID of the source whose byte is on the link: 01 the core source, 02 the timer source. |
| byte | The byte on the link, in hexadecimal. |
| flush, ack | The flush request, and the flush acknowledge. |
| oldest+0 to oldest+3 | The four places of the buffer after the run, from the oldest byte to the newest. |
| id:byte | One place of the buffer: two hex digits of ID, then two of the byte. |
| edge_sample(r, f) | One clock edge of the test code, with ready at r and the flush request at f. |
e0 valid=1 ready=1 id=01 byte=40 flush=0 ack=0 e1 valid=1 ready=0 id=01 byte=01 flush=1 ack=0 e2 valid=1 ready=1 id=01 byte=01 flush=1 ack=0 e3 valid=1 ready=1 id=01 byte=00 flush=1 ack=0 e4 valid=1 ready=1 id=02 byte=a1 flush=1 ack=0 e5 valid=1 ready=1 id=02 byte=a0 flush=1 ack=0 e6 valid=0 ready=1 id=02 byte=00 flush=1 ack=1 e7 valid=0 ready=1 id=02 byte=00 flush=0 ack=0 oldest+0 id:byte=0101 oldest+1 id:byte=0100 oldest+2 id:byte=02a1 oldest+3 id:byte=02a0Read included file: soc-s83-trace.svh
// A transaction-level source pair sharing one trace link. The queues contain
// prerecorded exercise events. A cursor advances only on its own transfer.
bit clk, ready, flush, ack, held, saved_owner;
int core_pos, irq_pos, cycle, accepted;
logic [7:0] core_events [0:2], irq_events [0:1];
logic [14:0] ring [0:3];
wire core_valid = core_pos < 3;
wire irq_valid = irq_pos < 2;
wire valid = core_valid || irq_valid;
wire owner = held ? saved_owner : !core_valid;
wire [6:0] source_id = owner ? 7'h02 : 7'h01;
wire [7:0] payload = !valid ? 0 :
owner ? irq_events[irq_pos] : core_events[core_pos];
always @(posedge clk) begin
$display("e%0d valid=%b ready=%b id=%02h byte=%02h flush=%b ack=%b",
cycle, valid, ready, source_id, payload, flush, ack);
cycle <= cycle + 1;
held <= valid && !ready;
saved_owner <= owner;
if (valid && ready) begin
ring[accepted % 4] <= {source_id,payload};
accepted <= accepted + 1;
if (owner) irq_pos <= irq_pos + 1;
else core_pos <= core_pos + 1;
end
// Both queues were filled before the request. No new events arrive.
if (flush && !ack)
ack <= !valid || (ready && ((3-core_pos) + (2-irq_pos) == 1));
else ack <= 0;
end
task automatic edge_sample(input bit sink_ready, flush_request);
ready = sink_ready; flush = flush_request;
#2; clk = 1; #2; clk = 0;
endtask
Check Yourself
Common Mistakes
Wrong Count a byte on every row where valid is 1.
During a stall the same byte stays on the link.
Fix Count a byte only where valid and ready are both 1.
Wrong Read the order of arrival as the order of events.
A byte can wait behind another source.
Fix Compare the times at which the bytes were made.
Wrong Expect a flush to empty or clear the buffer.
A flush pushes the waiting bytes forward, into the buffer.
Fix Count the places of the buffer to see what is kept.
Remember
- Trace sources share one link through a funnel. Every byte carries the ID of its source.
- A byte moves only on an edge where valid and ready are both 1.
- A flush asks for the bytes that are waiting now. It clears nothing.
- A circular buffer holds the newest bytes. The start packet can be the one it loses.