Interrupt Tests End to End

An interrupt crosses four places that four teams built: the device, the interrupt map, ap_plic and the core. A chip test proves the whole journey for every source, handles edge and level requests correctly, and keeps its handlers honest.

You Will Be Able To

  • Trace an interrupt from its device to the completion that ends it.
  • Prove that every source reaches the handler of its own number.
  • Tell an edge source from a level source and write the handler each needs.
  • Turn an interrupt that never comes into a fail code.
Lesson
V4.7
Time
28 min
Runs on
Your browser
Check
5 questions

Every block raises its interrupt in its own bench. The core takes interrupts in its own bench. On the chip, the timer's interrupt arrives as the DMA's, and a handler that only counts interrupts is perfectly happy.

The Journey of One InterruptStep through one GPIO edge from the pad to the completion that ends it.
  • Pads
  • Peripheral
  • Processor
  • Data
  • Interrupt
The Journey of One InterruptStep through one GPIO edge from the pad to the completion that ends it. pads Pad IO8 the board pulses it peripheral GPIO edge latched inSTATUS peripheral ap_plic source 2: pending,priority, enable processor Core mie, mstatus: trap tomtvec processor Handler claim, clear thedevice, complete processor main resumes where it was edgerequestexternal interrupttrapmret
  1. pads Pad IO8the board pulses it
    • edge → GPIO
  2. peripheral GPIOedge latched in STATUS
    • request → ap_plic
  3. peripheral ap_plicsource 2: pending, priority, enable
    • external interrupt → Core
  4. processor Coremie, mstatus: trap to mtvec
    • trap → Handler
  5. processor Handlerclaim, clear the device, complete
    • mret → main
  6. processor mainresumes where it was
  1. Source

    The GPIO block latches a rising edge on pin 8 and raises its request.

  2. ap_plic

    Source 2 is pending. If it is enabled and its priority is above the threshold, ap_plic asks the core.

  3. Core

    With machine interrupts enabled, the core traps to the kit's entry stub.

  4. Handler

    The dispatcher claims source 2, calls its handler, which clears the device's condition, and completes the claim.

  5. Back to main

    mret returns to the interrupted instruction. One interrupt, one handler, one completion.

Read the diagram as text
  • Pad IO8: the board pulses it
  • GPIO: edge latched in STATUS
  • ap_plic: source 2: pending, priority, enable
  • Core: mie, mstatus: trap to mtvec
  • Handler: claim, clear the device, complete
  • main: resumes where it was
  • Pad IO8 → GPIO: edge
  • GPIO → ap_plic: request
  • ap_plic → Core: external interrupt
  • Core → Handler: trap
  • Handler → main: mret

ap_soc v1.5: GPIO is ap_plic source 2 (irq.csv). The lab below raises every device source.

Before You Start

Read Interrupt Handlers in C and The Interrupt Path first. Interrupt Registers in a Peripheral explains level, edge and write-1-to-clear.

Five questions this page answers
Question
1Why does a chip need interrupt tests when every block tested its own?
2Which places does one interrupt cross?
3How does a test prove that a source reached its own handler?
4What must a handler do for a level source that it need not do for an edge?
5What happens to a test when an interrupt never comes?

The Idea: The Journey Belongs to the Chip

A device raises a request. The interrupt map wires it to an input of ap_plic. ap_plic decides by enable, priority and threshold whether to ask the core. The core traps, and a handler claims the source, clears the device and completes. Each piece was built and tested by someone else; the wiring between them exists only on the chip.

An interrupt test proves the journey. For every source it makes the device ask, and it checks that the right handler ran, once. "An interrupt came" is not enough. It must be this source's interrupt, under this source's number.

It also proves the handler's side of the work. An edge request is latched once and cleared by a write. A level request is the device's own state, and it asks until the cause is gone. Handlers that get this wrong storm, or lose requests.

New Words

New words on this page
WordMeaning
Source numberThe input of ap_plic a device is wired to (irq.csv). UART0 1, GPIO 2, timer 3, SPI 4, DMA 5, ap_pwrctl 9.
ClaimA read of ap_plic's CLAIM register: the winning source, which stops it asking.
CompleteA write of the claimed number back to CLAIM: the source may ask again.
Edge sourceA request latched on a change of a pin, cleared by writing its status bit.
Level sourceA request that lasts as long as its cause, such as a pin held high.
Interrupt stormThe same request taken again and again, because the handler never removes its cause.

The Smallest Example: Map and Prove by Hand

irq.csv is the chip's interrupt map. Read it, then write down what a test must see for each source.

The device sources of ap_soc v1.5
SourceDeviceHow a test raises itThe handler must
1UART0The board sends a byteread RXDATA
2GPIOThe board pulses IO8write 1 to STATUS bit 8
3Timer and PWMA compare matchstop the timer, clear the match
4SPIA frame endswrite 1 to STATUS bit 1
5DMAA copy endswrite 1 to STATUS bit 1
9ap_pwrctlA software wake of PD_PERwrite 1 to the WAKE_CAUSE bits

Source numbers from tools/soc/ap_soc/irq.csv; registers from the chip's manual.

The Rules

  1. Rule 1: Every source, one at a time.

    Raise each device's request alone, so a crossed wire cannot hide.

  2. Rule 2: Each handler says who it is.

    The test checks which handler ran, not only that one did.

  3. Rule 3: Clear the cause before you complete.

    For a level source, remove or mask the cause; a write to STATUS does not end a level.

  4. Rule 4: Every wait for an interrupt has a limit.

    An interrupt that never comes must end the test with a code.

  5. Rule 5: A handler that ran without cause is a failure.

    Report it at once, from the handler; main may never run again.

Sources for These Rules

The interrupt map: tools/soc/ap_soc/irq.csv. ap_plic's registers: the generated header (PLIC_PRIORITY, PENDING, ENABLE, THRESHOLD, CLAIM). The kit's dispatcher claims once and completes the same number: tools/soc/fwkit/irq.c.

Claim and completion: the RISC-V PLIC specification (library key RV-PLIC), chapters 8 and 9. A claimed source is not forwarded again until its completion: section 1.5. A smaller ID wins a tie of priorities: section 1.4. ap_plic is a small PLIC of our design. Level and edge: the chip's GPIO registers.

The kit's dispatcher handles interrupt causes 7 (timer) and 11 (external). Any other interrupt becomes fail code 0xFFFF: tools/soc/fwkit/irq.c.

The fault in the lab is a seeded fault of stage E9: docs/soc/fault-table.md.

Now Run It: Six Sources, Two Chips

The lab raises each device source in turn and prints which handler ran. UART0 asks for a byte from the board, GPIO for a pulse. The timer asks on a match, SPI and the DMA when their work ends, and ap_pwrctl on a software wake.

Measured on our bench (soc_tb, Verilator 5.052 2026-09-05).
EVENT 490: 00000003
CONSOLE 1074: handler 00000001
EVENT 1095: 00000005
CONSOLE 1528: handler 00000002
CONSOLE 2028: handler 00000003
CONSOLE 2594: handler 00000004
CONSOLE 3063: handler 00000005
CONSOLE 3593: handler 00000009
interrupts=6 irq_external=6 ext_uart=1 ext_gpio=1 ext_timer=1

Six interrupts, six handlers, each the one of its source: rules 1 and 2. Then the same image ran on a chip with one seeded fault. Its interrupt map gives input 3 the DMA's request instead of the timer's.

The same test on the faulty chip (PLIC source three samples the DMA interrupt).
EVENT 490: 00000003
CONSOLE 1074: handler 00000001
EVENT 1095: 00000005
CONSOLE 1528: handler 00000002
CONSOLE 17784: handler 00000002
CONSOLE 34214: handler 00000004
reason=cycle_limit interrupts=1939

The timer's request went nowhere, so the wait for it ran out and printed the handler that ran last, 2. Later the DMA's request reached input 3 and input 5 at once. The timer's handler cleared the timer, not the DMA, and input 3 asked again: 1939 interrupts until the cycle limit. Rule 5 would have ended that run at the first wrong handler.

Level against edge, on GPIO pin 8 held high by the board. In level mode, a handler that only writes STATUS was taken 164 times until the cycle limit: rule 3. A handler that disabled the pin was taken once, and so was the pin in edge mode.

The storming handler of LEVEL_C.
static volatile unsigned taken;
static void on_pin(void)
{
    gpio_ack(1u << 8);                        /* the pad is still high: the request stays */
    taken = taken + 1;
}

Why It Is Built This Way

A test could raise all sources at once and count to six. It would pass on a chip with two crossed wires, because six interrupts still arrive. One source at a time, with handlers that name themselves, costs a few hundred cycles more and leaves no room for that.

The kit's dispatcher claims once, calls the handler of the claimed number and completes it. That keeps the claim and the completion right in every test. The test's own handlers only clear their device and record what happened.

The order inside a handler is the claim-and-complete discipline. The claim clears the source's pending bit, and its gateway forwards nothing more from that source until the completion. So the device must be quiet before the completion is written. Complete a level source while its cause is still there, and it asks again at once: the storm in the lab. Forget the completion, and the source goes silent for the rest of the test.

Know what the lab test leaves out. It raises one source at a time, with every priority at 1 and the threshold at 0. So it never shows which of two pending sources is claimed first, or that a source at the threshold must wait. A complete interrupt test checks both. This chip's seeded faults include none that only those checks would find. So its graded problem cannot demand them, though the reference makes them.

The kit has a gap here too. It gives the machine timer and external interrupts a handler, but not the software interrupt of ap_clint (MSIP). A test that enables that interrupt ends with fail code 0xFFFF when it arrives, so this module does not test it.

The Trap: The Handler That Counted Everything

A first interrupt test has one counter. Every handler adds one to it, and main waits until it reaches the number of sources. On a chip with a crossed wire it passes: the wrong handler ran, and it counted. On a chip with a missing wire it hangs, and the report says "cycle limit".

The cure is two habits: each handler records its own source, and each wait has a limit and a code. The practice problems build both. One shares a GPIO source between an edge and a level; one faces a hidden set of faulty chips.

At Scale

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. ap_soc has six device sources and one core.

  1. Hundreds of sources

    Large chips have hundreds; tests are generated from the interrupt map, one per source, like the register sweeps of module V2.

  2. Many cores

    An interrupt can be routed to several cores; tests prove the routing table and that only one core claims.

  3. Other controllers

    Arm GIC and vectored controllers differ in detail, but the journey and the rules are the same.

Try It

Run the slice: it checks each raised source against irq.csv and the handler that ran.

Swap two handlers in the list. Which lines turn WRONG?

The predict question below feeds it the first three sources of the faulty chip.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: raised 1 (UART0): handler 1, right raised 2 (GPIO): handler 2, right raised 3 (Timer and PWM): handler 3, right raised 4 (SPI): handler 4, right raised 5 (DMA): handler 5, right raised 9 (ap_pwrctl): handler 9, right

Check Yourself

Predict the outputThe slice now reads the first three sources of the run on the faulty chip. What is the last line it prints?
// The interrupt sources of irq.csv that have a device, and the handler one run reported for each source
// it raised, in order. A source must reach the handler of its own number.
module tb;
  localparam int N = 3;
  int raised [N], handled [N];
  function automatic string device(int id);
    case (id)
      1: return "UART0";
      2: return "GPIO";
      3: return "Timer and PWM";
      4: return "SPI";
      5: return "DMA";
      6: return "Watchdog (early warning)";
      9: return "ap_pwrctl";
      default: return "no device";
    endcase
  endfunction
  initial begin
    raised[0] = 1; handled[0] = 1;
    raised[1] = 2; handled[1] = 2;
    raised[2] = 3; handled[2] = 2;
    for (int i = 0; i < N; i++)
      $display("raised %0d (%s): handler %0d, %s", raised[i], device(raised[i]), handled[i], raised[i] == handled[i] ? "right" : "WRONG");
  end
endmodule
ConceptWhy does a chip need interrupt tests when every block tested its own?
ConceptWhat must a handler do for a level source?
ConceptA handler runs although its device did not ask. What should the test do?
ConceptWhy raise one source at a time?

Common Mistakes

Wrong Count interrupts with one shared counter.

A crossed wire still delivers the right number of interrupts.

Fix Let each handler record its own source, and check it.

Wrong Clear a level request by writing its status bit.

The cause is still there, so the request comes straight back.

Fix Remove the cause, or mask the source until it is gone.

Wrong Wait for an interrupt with no limit.

A missing wire becomes a hang.

Fix Wait with a limit and fail with the source's code.

Remember

  • An interrupt crosses the device, the map, ap_plic and the core: the journey is the chip's.
  • Raise each source alone and check that its own handler ran.
  • Edge requests are cleared by a write; level requests by removing their cause.
  • Every wait for an interrupt has a limit; a handler without cause fails the test.

Practice

Report a problem with this page