Interrupts and the Model
An interrupt arrives when the chip decides, not when the program does. A model has no clock and no pins, so it must be told. This page shows how our bench tells it, how an open core's bench does, and what each method can and cannot check.
You Will Be Able To
- Explain why a model cannot predict when an interrupt is taken.
- Describe the record method our bench uses, and the notify method of the Ibex bench.
- Measure the skid between a pin and the record that takes the interrupt.
- State what the record method cannot check.
- Lesson
- V5.3
- Time
- 28 min
- Runs on
- Your browser
- Check
- 5 questions
Your lockstep bench passes every program that takes no interrupt. You turn the timer on, and the first run fails at once: the model took the interrupt early. The core is fine. The bench told the model the wrong moment.
- Peripheral
- Processor
- Debug
- Interrupt
- Data
- The pin rises
At cycle 98 MTIME passes MTIMECMP and the core's timer input goes high.
- In flight
Instructions already past execute cannot be stopped: 2 more records retire after the rise.
- The snapshot
The next instruction samples the lines as it enters execute. Its snapshot shows the timer: it is interrupted.
- The record
The interrupt record carries the stopped instruction's pc and the mip it was decided with, 00000080.
- The model
Given exactly that mip before its step, the model takes the same trap at the same record.
Read the diagram as text
- Timer pin: rises at cycle 98
- Instructions in flight: 2 records still retire
- The snapshot: mip sampled entering execute
- Interrupt record: cycle 103, pc 80000078, mip 00000080
- The model: given that mip, it takes the same trap
- Timer pin → Instructions in flight: level
- Instructions in flight → The snapshot: next to enter EX
- The snapshot → Interrupt record: trap
- Interrupt record → The model: record method
Recorded on ap_soc v1.5: the pin from a probe at the core's input, the records from its retire trace.
Before You Start
Read Lockstep With an Instruction-Level Model first. Interrupt Tests End to End shows the interrupt path from the device side.
| Question | |
|---|---|
| 1 | Why can a model not work out by itself when an interrupt is taken? |
| 2 | What does our bench give the model, and when? |
| 3 | How does the bench of an open core, Ibex, tell its model? |
| 4 | Why is a model told "the pin rose" early? |
| 5 | What can the record method not check? |
The Idea: The Model Must Be Told, Exactly
An interrupt is the chip's decision, made at a clock edge. The model has no clock and no devices: it only knows the program. So it must be told the lines, and the timing must be exact. Told one instruction early or late, it traps at another record and the compare fails on a good core.
A pipelined core adds a twist. It does not see a pin the cycle it rises. Each instruction samples the lines once, as it enters execute, and that snapshot decides. Instructions already past that point retire first. That delay is the skid.
There are two ways to tell the model. In the record method, the core writes its snapshot into every trace record, and the bench hands it to the model. In the notify method, the bench calls the model to set its lines, from what it observes of the core. Either way, the model needs the lines the instruction decided with, not the pins at the moment it retires. Our bench uses the record method.
New Words
| Word | Meaning |
|---|---|
| Snapshot | The interrupt lines an instruction sampled as it entered execute: the trace field mip. |
| Skid | The records that still retire after a pin rises, before one is interrupted. |
| Record method | The model is given each record's mip before it steps, and decides the trap itself. |
| Notify method | The bench calls the model to set its lines, from what it observes of the core, not from the trace. |
| Spike | An open instruction-level model of RISC-V, which the Ibex bench uses as its model. |
The Smallest Example: Count the Skid
The timer pin rose at cycle 98. Here are the records around it, with the timer bit (bit 7) of mip.
| cycle | pc | instruction | mip | trap |
|---|---|---|---|---|
| 98 | 80000078 | addi a1, a1, 3 | 00000000 | 0 |
| 99 | 8000007c | addi t3, t3, -1 | 00000000 | 0 |
| 100 | 80000080 | bne t3, zero, 0x80000078 | 00000000 | 0 |
| 103 | 80000078 | addi a1, a1, 3 | 00000080 | 1 |
| 107 | 80000118 | csrrs t5, mcause, zero | 00000080 | 0 |
| 111 | 8000011c | addi s1, s1, 1 | 00000080 | 0 |
| 112 | 80000120 | andi t6, t5, 15 | 00000080 | 0 |
Recorded on ap_soc v1.5.
The first record after the pin is at cycle 99; its mip shows no timer. The interrupt record is at cycle 103, 2 records later. A model told at cycle 98 would interrupt the first of them.
The Rules
Rule 1: The model decides on the core's snapshot.
Give it the record's mip before its step; on an interrupt record, the interrupt's own line too.
Rule 2: Set every line on every record.
A line that falls must fall in the model as well; never only raise.
Rule 3: Let the model decide the trap.
The model applies mie, mstatus and priority itself, so a core that takes or ignores the wrong one differs at once.
Rule 4: A record wakes the model from wfi.
How long the core slept is not compared; that the record came is.
Rule 5: The snapshot itself is not checked.
Whether a line reached the core, and how fast, needs other checks.
Sources for These Rules
The snapshot rule and the skid: docs/soc/ap_rv32-blueprint.md 7.4. WFI and lockstep: 7.5 and 9.2. Our compare: soc_iss_dpi.c. Its soc_iss_compare sets the lines from mip, adds the cause's line on an interrupt record, then wakes and steps the model.
The Ibex bench: the Ibex documentation, "Co-simulation System" (ibex-core.readthedocs.io, read 2026-11-28). Its model is Spike, called through DPI and stepped once per retired instruction. The bench calls set_mip, set_debug_req and set_nmi. The core's inputs are sampled as an instruction moves from fetch into decode and execute. The sample travels with the instruction to its RVFI output, where the calls are made. An interrupt is reported through RVFI with no retired instruction, so the order is kept. Instruction-side fetches are not checked directly, the page says, because they can be speculative.
Interrupt priority and entry: the RISC-V Privileged specification (RV-PRIV), machine interrupt registers.
Now Run It: Four Interrupts, Three Ways to Tell
The lab takes a software interrupt by MSIP, a timer interrupt during a loop and a GPIO interrupt through ap_plic. Then it sleeps in wfi until the timer wakes it.
order 16 cycle 35 pc 80000040: software interrupt, mip 00000008
order 57 cycle 103 pc 80000078: timer interrupt, mip 00000080
order 252 cycle 422 pc 800000c4: external interrupt, mip 00000800
order 277 cycle 483 pc 800000f0: timer interrupt, mip 00000080cycle 32: msip rises
cycle 50: msip falls
cycle 98: mtip rises
cycle 122: mtip falls
cycle 419: meip rises
cycle 438: meip falls
cycle 477: mtip rises
cycle 502: mtip fallsEvery interrupt record comes a few cycles after its pin: rules 1 and 2 at work. The wfi retired at cycle 459; the next record, at cycle 483, is the timer interrupt that woke it: rule 4.
The same records, replayed on the model three ways. Given each record's mip, all 298 agree. Given the mip of the record before, the model first differs at order 16, in next_pc: it missed the software interrupt. Given the pins as they stood when each record retired, a careless notify, it differs at order 13, in next_pc. It took the software interrupt before the core did.
Why It Is Built This Way
The record method needs nothing but the trace. The core writes its snapshot into every record, so the bench gives the model exactly what the core decided with. There is no timing to model and no race between the pin and the record.
The Ibex bench solves the same problem from the bench's side. It samples the core's inputs as each instruction leaves fetch, and carries the sample with that instruction to the RVFI port. Only there does it call set_mip. So Spike hears the lines the instruction decided with. It is the same snapshot, carried by the bench instead of by the trace. Interrupts are reported through RVFI too, so they keep their order.
Each method has the limit of its source. Ours trusts the core's snapshot: a timer wire crossed or late looks the same to the core and to the model. A notify bench trusts its own copy of the core's timing, which must move when the pipeline changes. Neither replaces a test that raises a line and checks the cause, which the page on core integration checks builds.
The Trap: The Line That Only Went Up
A first lockstep bench raises a line in the model whenever a record shows it, and never lowers it. Programs without interrupts pass. The first program whose handler clears its device fails after that handler returns: the model still sees the line and traps again.
Set all three lines on every record, from the record. The practice problem gives you a bench that tells the model at the pins. It fails on a good chip, and you repair it. The worksheet counts the skid by hand.
At Scale
This section looks ahead. ap_soc has one core and three interrupt lines.
Many sources, one line
A large chip has hundreds of sources behind one external line. The model still sees one line, so the controller needs its own checks.
Debug and NMI
A halt request or a non-maskable interrupt is told to the model the same way; our bench passes debug events too (soc_iss_debug).
Asynchronous errors
The model cannot tie a late bus error to one instruction. Benches report it with the record, as ours does for device errors.
Try It
Run the slice: it places the pin among the records. Then it counts the records from the first after the pin to the interrupt.
Change the rise cycle to 99. Which record is first after the pin now?
The predict question runs it on the software interrupt.
cycle 98 pc 80000078 mip 00000000 cycle 99 pc 8000007c mip 00000000 cycle 100 pc 80000080 mip 00000000 cycle 103 pc 80000078 mip 00000080 interrupt taken cycle 107 pc 80000118 mip 00000080 cycle 111 pc 8000011c mip 00000080 cycle 112 pc 80000120 mip 00000080 first record after the pin: 1; first record whose mip shows it: 3; skid 2 recordsCheck Yourself
Common Mistakes
Wrong Tell the model the moment a pin rises.
A pipelined core takes the interrupt a few records later, on its snapshot.
Fix Give the model each record's mip before its step.
Wrong Only raise lines in the model.
A line that falls stays high in the model, which traps again.
Fix Set all three lines on every record.
Wrong Expect the compare to catch a crossed interrupt wire.
The core and the model see the same wrong snapshot.
Fix Test each line end to end, with its cause and a limit.
Remember
- An interrupt is the chip's decision; the model must be told the lines, exactly.
- A pipelined core samples the lines as each instruction enters execute: a skid of a few records after the pin.
- Our record method gives the model each record's mip; Ibex's notify method calls Spike with a sample carried with each instruction.
- The record method cannot check the snapshot itself: wires and latency need end-to-end tests.