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.

Five questions this page answers
Question
1How do two trace sources share one link without mixing their bytes?
2On which clock edges does a byte move?
3What does a flush ask for, and what does its acknowledge say?
4What does a circular buffer hold when more bytes arrive than it has places?
5Five bytes go into a buffer of four places. Which byte is lost, and why can that spoil the capture?

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

New words on this page
WordMeaning
Trace linkThe wires that carry trace bytes from the sources toward the place where they are kept.
FunnelA block that lets one of several trace sources use the trace link at a time.
Source IDA number that travels with each byte and names the trace source it came from.
ValidA wire from the sender. 1 means: there is a byte on the link.
ReadyA wire from the receiver. 1 means: I can take a byte on this edge.
Stalled byteA 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 bufferA memory with a fixed number of places. When all are full, the newest byte replaces the oldest.
FlushA request from the receiver: send every byte that you are holding now.
Flush acknowledgeThe answer to a flush: every byte that was waiting when you asked has now moved.
The same wires on ATB, the Advanced Trace Bus of Arm
On this pageOn the ATBDriven by
validATVALIDThe sender
readyATREADYThe receiver
source IDATID, seven bitsThe sender
flushAFVALIDThe receiver
flush acknowledgeAFREADYThe sender

One Picture: From Two Sources into the Buffer

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.

Three bytes and one stall, by hand
EdgeByte on the linkValidReadyDoes it move?The buffer holds
e0P11YesP
e1Q10No: a stallP
e2Q11YesP, Q
e3R11YesR, Q
e4none01Nothing to moveR, 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

  1. Rule 1: A byte moves on a clock edge where valid and ready are both 1.

    On no other edge.

  2. 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.

  3. Rule 3: Every byte travels with the ID of its source.

    The bytes of one source stay in their order.

  4. 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.

  5. 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.

  6. 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.

Printed by the lab on Icarus. IDs and bytes are in hexadecimal.
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
One Byte Waits at the FunnelRising-edge samples from the trace lab. Read a byte only where valid and ready meet.
01234567clkvalidreadyid0102byte400100a1a000flushack
Scroll horizontally ↔
  1. Cycle 0

    A synchronization byte enters the ring.

  2. Cycle 1

    The sink pauses.

  3. Cycle 2

    The first outcome crosses.

  4. Cycle 5

    The last pending event arrives.

  5. 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 choiceThe alternativeWhat the choice buys, and costs
A funnel and an ID on each byteA link and a buffer for each sourceOne link and one buffer for the chip. A busy source can hold up a quiet one.
A stall when the receiver is busyDrop the byteNo 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 chipSend every byte through pins to a recorderNo 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 page2 sources, 4 places
A phone chiptens of sources

This section looks ahead. Two sources and four places become a trace system with a budget.

  1. 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.

  2. 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.

  3. 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.

  4. 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
NameMeaning
e0, e1, ...Rising edges of the clock. A row shows the link just before that edge.
valid, readyThe two wires of the handshake on the link.
idThe ID of the source whose byte is on the link: 01 the core source, 02 the timer source.
byteThe byte on the link, in hexadecimal.
flush, ackThe flush request, and the flush acknowledge.
oldest+0 to oldest+3The four places of the buffer after the run, from the oldest byte to the newest.
id:byteOne 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.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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
Read 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

Predict the outputIn this run the last byte of the timer source is af in place of a0. What is the last row? Its form is: oldest+3 id:byte=....
`timescale 1ns/1ns
module tb;
  `include "soc-s83-trace.svh"
  initial begin
    core_events[0]=8'h40; // Sync at program offset 0x40.
    core_events[1]=8'h01; // First conditional branch taken.
    core_events[2]=8'h00; // Next conditional branch falls through.
    irq_events[0]=8'ha1; irq_events[1]=8'haf;
    edge_sample(1,0); edge_sample(0,1); edge_sample(1,1);
    repeat (4) edge_sample(1,1);
    edge_sample(1,0);
    for (int i=0; i<4; i++)
      $display("oldest+%0d id:byte=%04h", i, ring[(accepted+i)%4]);
    $finish;
  end
endmodule
Read 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
ConceptHow do two trace sources share one link without mixing their bytes?
ConceptOn which clock edges does a byte move?
ConceptWhat does the flush acknowledge say?
ConceptFive bytes go into a circular buffer of four places. What is lost, and why can that spoil the capture?

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.

Practice

Report a problem with this page