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.

An Interrupt Lands Between Two InstructionsStep through the recorded timer interrupt: the pin rises, the core samples it, one instruction is stopped, the handler runs.
  • Peripheral
  • Processor
  • Debug
  • Interrupt
  • Data
An Interrupt Lands Between Two InstructionsStep through the recorded timer interrupt: the pin rises, the core samples it, one instruction is stopped, the handler runs. peripheral Timer pin rises at cycle 98 processor Instructions inflight 2 records still retire processor The snapshot mip sampled enteringexecute debug Interrupt record cycle 103, pc80000078, mip00000080 processor The model given that mip, ittakes the same trap levelnext to enter EXtraprecord method
  1. peripheral Timer pinrises at cycle 98
    • level → Instructions in flight
  2. processor Instructions in flight2 records still retire
    • next to enter EX → The snapshot
  3. processor The snapshotmip sampled entering execute
    • trap → Interrupt record
  4. debug Interrupt recordcycle 103, pc 80000078, mip 00000080
    • record method → The model
  5. processor The modelgiven that mip, it takes the same trap
  1. The pin rises

    At cycle 98 MTIME passes MTIMECMP and the core's timer input goes high.

  2. In flight

    Instructions already past execute cannot be stopped: 2 more records retire after the rise.

  3. The snapshot

    The next instruction samples the lines as it enters execute. Its snapshot shows the timer: it is interrupted.

  4. The record

    The interrupt record carries the stopped instruction's pc and the mip it was decided with, 00000080.

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

Five questions this page answers
Question
1Why can a model not work out by itself when an interrupt is taken?
2What does our bench give the model, and when?
3How does the bench of an open core, Ibex, tell its model?
4Why is a model told "the pin rose" early?
5What 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

New words on this page
WordMeaning
SnapshotThe interrupt lines an instruction sampled as it entered execute: the trace field mip.
SkidThe records that still retire after a pin rises, before one is interrupted.
Record methodThe model is given each record's mip before it steps, and decides the trap itself.
Notify methodThe bench calls the model to set its lines, from what it observes of the core, not from the trace.
SpikeAn 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.

Records around the rise
cyclepcinstructionmiptrap
9880000078addi a1, a1, 3000000000
998000007caddi t3, t3, -1000000000
10080000080bne t3, zero, 0x80000078000000000
10380000078addi a1, a1, 3000000801
10780000118csrrs t5, mcause, zero000000800
1118000011caddi s1, s1, 1000000800
11280000120andi t6, t5, 15000000800

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

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

  2. Rule 2: Set every line on every record.

    A line that falls must fall in the model as well; never only raise.

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

  4. Rule 4: A record wakes the model from wfi.

    How long the core slept is not compared; that the record came is.

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

The four interrupt records. On the chip bench all 298 records were compared: 0 mismatches.
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 00000080
The core's three interrupt inputs, as a probe saw them change.
cycle  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 falls

Every 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

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. ap_soc has one core and three interrupt lines.

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

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

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

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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 records

Check Yourself

Predict the outputThe slice now places the software interrupt's pin, which rose at cycle 32, among its records. What is the last line it prints?
// The software interrupt's pin rose at cycle 32. Records of the core around it: cycle, pc, and mip as the
// core sampled it. The record method takes the interrupt where mip first shows it; a model told "the pin rose" at
// the first record after the rise would take it earlier.
module tb;
  localparam int N = 6;
  int cycle [N];
  bit [31:0] pc [N], mip [N];
  bit trap [N];
  int told = -1, took = -1;
  initial begin
    cycle[0] = 32; pc[0] = 32'h80000034; mip[0] = 32'h00000000; trap[0] = 0;
    cycle[1] = 33; pc[1] = 32'h80000038; mip[1] = 32'h00000000; trap[1] = 0;
    cycle[2] = 34; pc[2] = 32'h8000003c; mip[2] = 32'h00000000; trap[2] = 0;
    cycle[3] = 35; pc[3] = 32'h80000040; mip[3] = 32'h00000008; trap[3] = 1;
    cycle[4] = 39; pc[4] = 32'h80000118; mip[4] = 32'h00000008; trap[4] = 0;
    cycle[5] = 43; pc[5] = 32'h8000011c; mip[5] = 32'h00000008; trap[5] = 0;
    for (int i = 0; i < N; i++) begin
      if (told < 0 && cycle[i] > 32) told = i;
      if (took < 0 && mip[i][3]) took = i;
      if (trap[i]) $display("cycle %0d pc %h mip %h interrupt taken", cycle[i], pc[i], mip[i]);
      else $display("cycle %0d pc %h mip %h", cycle[i], pc[i], mip[i]);
    end
    $display("first record after the pin: %0d; first record whose mip shows it: %0d; skid %0d records", told, took, took - told);
  end
endmodule
ConceptWhy can a model not work out by itself when an interrupt is taken?
ConceptWhat does our bench give the model, and when?
ConceptHow does the Ibex bench tell Spike about an interrupt?
ConceptWhat can the record method not check?

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.

Practice

Report a problem with this page