The Retire Trace as a Monitor
The core of ap_soc reports every instruction it finishes as one record of 24 fields. That stream is the best monitor a chip bench has. This page teaches how to read a record, and the rules that make every record mean one thing.
You Will Be Able To
- Read the 24 fields of a retire record and say what the instruction did.
- Tell a retired instruction, an exception and an interrupt apart in the trace.
- State the rules a trace must keep, and why a monitor depends on them.
- Say what the trace cannot show.
- Lesson
- V5.1
- Time
- 26 min
- Runs on
- Your browser
- Check
- 5 questions
A test fails deep into its run. The waveform shows two buses and dozens of signals, and the pipeline holds five instructions at once. Which instruction did what? The retire trace answers in one line per instruction.
- Processor
- Memory
- Data
- Which instruction
Fields 1 to 5: the record number, the address, the instruction word, where the core went next, the privilege.
- Did it trap
Fields 6 to 8. A trap record says the instruction did not happen; cause and tval say why.
- Registers
Fields 9 to 14: what it read from rs1 and rs2, and what it wrote to rd.
- Memory
Fields 15 to 19: the byte address, which bytes were read or written, and their data.
- CSR
Fields 20 to 22: a CSR written, and the value it holds after.
- State
Fields 23 and 24: mstatus after the record, and the interrupt lines it was decided with.
Read the diagram as text
- Which instruction: order 11, pc 8000002c, insn, next_pc, priv
- Did it trap: trap, cause, tval
- Registers: rs1, rs2 read; rd 11 = 000000ee
- Memory: addr 800000c5, rmask 1, rdata 000000ee
- CSR written: csr_write, csr_addr, csr_wdata
- State after: mstatus after, mip it was decided with
- Which instruction → Did it trap: then
- Did it trap → Registers: or
- Registers → Memory: and
- Memory → CSR written: and
- CSR written → State after: and
ap_rv32's retire trace (blueprint 3.5). The values are those of the lbu record recorded below.
Before You Start
Read The Life of a C Test and Traps: Exceptions, Interrupts and the Machine CSRs first. The Core as an IP shows the trace port among the core's other ports.
| Question | |
|---|---|
| 1 | Why does a chip bench want a retire trace when it already sees the buses? |
| 2 | What does one record say, and when does the core make one? |
| 3 | How does a trap look different from an instruction that retired? |
| 4 | Which rules must every record keep, and who depends on them? |
| 5 | What can the trace not show? |
The Idea: One Line per Finished Instruction
A pipelined core works on five instructions at once, and its buses carry fetches that may never count. Watching them tells you what the core tried, not what it did. The retire trace reports only what finished, in program order, one record each.
Each record is the instruction's whole effect. It says which registers it read and wrote, which bytes it touched and which CSR it changed. A trap gets a record too, saying the instruction did not happen and why. Nothing the program can see changes without a record that shows it.
That makes the trace the monitor every other check stands on. The reference model of the next lesson compares it, field by field. Coverage counts it. A debugger reads it. Each of them trusts that a record means one thing, so the trace has rules of its own.
New Words
| Word | Meaning |
|---|---|
| Retire | An instruction finishes: its effects become part of the program's state. |
| Retire record | One line of the trace for one commit-point event: a retired instruction, an exception or an interrupt entry. |
| Commit point | The pipeline stage where an instruction can no longer be cancelled. In ap_rv32 it is MEM; the record appears in WB. |
| Exception record | A record with trap = 1 and cause bit 31 clear: the instruction itself faulted. |
| Interrupt record | A record with trap = 1 and cause bit 31 set: an interrupt was taken before the instruction ran. |
| minstret | The CSR that counts retired instructions: records with trap = 0. |
| RVFI | The RISC-V Formal Interface: a similar record that riscv-formal reads. ap_rv32's fields follow it where it has one. |
The Smallest Example: Three Records by Hand
Here are three records of the lab, with eight of their fields. Read each one before the explanation.
| order | instruction | trap | cause | tval | rd = value | memory |
|---|---|---|---|---|---|---|
| 11 | lbu a1, 1(s1) | 0 | 00000000 | 00000000 | x11 = 000000ee | read 1 at 800000c5: 000000ee |
| 14 | ecall | 1 | 0000000b | 00000000 | none | none |
| 54 | lw t2, 0(t0) | 1 | 00000005 | 10007000 | none | none |
Recorded on ap_soc v1.5.
The lbu read one byte (mask 1) at 800000c5 and wrote it, zero-extended, to x11. The ecall is a trap with cause 11: no register, no memory. The last load faulted: cause 5, its address in tval, and its memory fields stay 0, because the access never happened.
The Rules
Rule 1: Records come in program order, numbered without gaps.
order counts every record from 0; minstret counts those with trap = 0.
Rule 2: A trap changes nothing by itself.
On a trap record the register, memory and CSR fields are 0; mepc, mcause and mtval are implied.
Rule 3: An interrupt names the instruction it stopped.
Its pc and insn are those of the instruction that did not run; that instruction retires later, as its own record.
Rule 4: A field with nothing to say is 0.
No rd: rd_wdata is 0. No access: mem_addr and the data are 0. No CSR write: its address and value are 0.
Rule 5: The fields follow what happened, not the instruction's name.
csrrs with x0 writes no CSR, so csr_write is 0; lui reads no register, so rs1_addr is 0.
Sources for These Rules
The 24 fields and rules T1 to T10: docs/soc/ap_rv32-blueprint.md, section 3.5. The text form (165 characters a record) and its reader: tools/soc/rv32/trace.mjs.
The Zicsr write rule behind rule 5: the RISC-V Unprivileged specification (library key RV-UNPRIV), the Zicsr chapter. Trap entry and mepc: the Privileged specification (RV-PRIV), the machine trap handling section.
RVFI is the interface of riscv-formal (github.com/YosysHQ/riscv-formal). The blueprint lists where ap_rv32's fields differ from it on purpose. For example, an interrupt has a record of its own.
Now Run It: One Record of Every Kind
The lab program makes one record of every kind. It reads a bench register, stores, loads a byte, runs lui and a CSR read that writes nothing. Then it takes an ecall, a timer interrupt in a waiting loop, and a load from a hole of the map.
lw a0, TB_ARG(s0) # a device read: the bench's word
la s1, buf
sw a0, 0(s1) # a store to SRAM
lbu a1, 1(s1) # one byte back
lui a2, 0x12345 # no source register
csrrs a3, mstatus, x0 # reads mstatus, writes nothing
ecall # an exception10 80000028 sw a0, 0(s1) trap=0 cause=00000000 tval=00000000 rd= 0 rd_wdata=00000000 mem=800000c4 r0 w15 csr_write=0 mip=00000000
11 8000002c lbu a1, 1(s1) trap=0 cause=00000000 tval=00000000 rd=11 rd_wdata=000000ee mem=800000c5 r1 w0 csr_write=0 mip=00000000
13 80000034 csrrs a3, mstatus, zero trap=0 cause=00000000 tval=00000000 rd=13 rd_wdata=00001800 mem=00000000 r0 w0 csr_write=0 mip=00000000
14 80000038 ecall trap=1 cause=0000000b tval=00000000 rd= 0 rd_wdata=00000000 mem=00000000 r0 w0 csr_write=0 mip=00000000
43 80000070 bne s4, zero, 0x8000007c trap=1 cause=80000007 tval=00000000 rd= 0 rd_wdata=00000000 mem=00000000 r0 w0 csr_write=0 mip=00000080
51 800000c0 mret trap=0 cause=00000000 tval=00000000 rd= 0 rd_wdata=00000000 mem=00000000 r0 w0 csr_write=0 mip=00000080
52 80000070 bne s4, zero, 0x8000007c trap=0 cause=00000000 tval=00000000 rd= 0 rd_wdata=00000000 mem=00000000 r0 w0 csr_write=0 mip=00000000
54 80000080 lw t2, 0(t0) trap=1 cause=00000005 tval=10007000 rd= 0 rd_wdata=00000000 mem=00000000 r0 w0 csr_write=0 mip=00000000Record 13 is the csrrs: csr_write = 0, rule 5 at work. Record 43 is the interrupt, at the pc of a bne that did not run. Record 52 is that bne, retiring after mret: rule 3. The load from the hole has no memory fields: rule 2.
The run made 67 records and 3 of them were traps, so minstret ended at 64. On the chip bench the same run compared all 67 records with the model, with 0 mismatches.
Why It Is Built This Way
A bench could watch the buses and the register file instead. It would have to rebuild what the pipeline did. Which fetch was cancelled, which write came from which instruction? That rebuilding is a second core, with its own bugs. The trace is the core's own statement, at the point where nothing can be undone.
The core makes the record in WB, from the stage where the instruction committed. So a record never shows a cancelled instruction. Unlike RVFI, an interrupt gets a record of its own here. So the model of the next lesson can be told exactly where it landed.
Know what the trace leaves out. It shows no bus timing, no wait states, and nothing another manager did. A DMA write to SRAM never appears in it. It shows what the core did, and the bench's other monitors show the rest.
The Trap: The Trace You Trusted
A trace port is RTL like any other, and it can be wrong while the core is right. A record that reports rd = 0 with a value, or a trap that also claims a write, sends every reader astray. The model compare then reports a core bug that is not there, and someone spends the afternoon on the wrong block.
So a bench checks the trace's rules on every record, beside the model. The checker is cheap: a record count, the last next_pc and one decode of the opcode. The practice problem builds it on the chip's own trace port. The first worksheet reads records by hand.
At Scale
This section looks ahead. ap_rv32 retires at most one instruction a cycle.
Several retires a cycle
A superscalar core retires two or more instructions a cycle; its trace has one channel each, in order.
Compressed trace
On silicon, a trace port sends only branches and exceptions, compressed; a decoder rebuilds the path from the program image.
The same monitor, three uses
The model compare, functional coverage and debug all read the same stream, so a bug in it costs three times.
Try It
Run the slice: it reads every record of the lab and prints the traps. It checks two rules: order with no gap, and the count of retired records.
Change one order number in the source. What does the last line say now?
The predict question below asks you to count before you run.
order 14 pc 80000038: exception, cause 11 order 43 pc 80000070: interrupt, cause 7 order 54 pc 80000080: exception, cause 5 records 67, gaps in order 0, retired 64Check Yourself
Common Mistakes
Wrong Read the buses to see what the core did.
Fetches can be cancelled and the buses show no register writes.
Fix Read the retire trace: one record per finished instruction.
Wrong Count records to get the instructions executed.
Trap records are records too, but their instruction did not run.
Fix Count records with trap = 0, as minstret does.
Wrong Take a record's values on trust.
The trace port is RTL; a fault there misleads the model, coverage and debug at once.
Fix Check the trace's own rules on every record.
Remember
- One record per commit-point event: a retired instruction, an exception or an interrupt entry, in program order.
- A trap record changes nothing by itself; an interrupt names the instruction it stopped, which runs later.
- A field with nothing to say is 0, and fields follow what happened, not the instruction's name.
- The trace is the core's own account; check its rules, and know it shows nothing other managers did.