V12.2.1 Interrupts in Every Core State

MediumImplement30 minap_soc v1.5 · firmware · serverReview the topic

Write firmware that makes the timer interrupt arrive while the core sleeps. Then make a second interrupt arrive while the core is inside a handler. The bench's coverage model counts both.

Hardware context: The bench-side sampler of ap_soc v1.5 and the firmware coverage channel through TB_EVENT. Firmware that makes interrupts arrive in every core state.

A block bench can prove that the timer raises its request. It cannot prove what the chip does when that request arrives at a bad moment. The core may be asleep, or already inside a handler. Those are integration points, and the chip's coverage model counts them from the bench side, at the moment each request rises.

Write C firmware for the processor of ap_soc. Start the timer with the divider the bench puts in TB_ARG, compare 2 and its interrupt enabled. Then sleep in wfi until the timer's handler has run. Inside that handler, stop the timer, clear its match, and raise a machine software interrupt by writing 1 to CLINT MSIP. Take that interrupt too, and clear MSIP in its handler.

Then print timer and the count of timer interrupts, soft and the count of software interrupts, and top with DIV_TOP read back from the timer. Pass, as in the example. Every wait needs a limit. The coverage build of the bench counts the five bins below; all five must be hit.

Full specification
BinWhat earns a hit (the bench's coverage model, chip_cov.sv)
irq.context.3.wfiThe timer's request (PLIC source 3) rises while the core sleeps in wfi
irq.context.msip.handlerThe software interrupt line rises while the core is inside a handler: traps retired minus mret retired
irq.claimed.3A claim read of ap_plic returns 3
irq.completed.3A completion write of 3 for a claimed, enabled source
irq.taken.msipThe core retires a trap with the software interrupt's cause
RegisterAddressUse
TIMER_RUN, TIMER_DIV_TOP, TIMER_COMPARE0x1000_2000, 0x1000_200C, 0x1000_2010A slot lasts DIV_TOP + 1 cycles; the match is the slot equal to COMPARE
TIMER_IRQ_CLEAR, TIMER_IRQ_ENABLE0x1000_2014, 0x1000_2018Write 1 to clear the match; bit 0 lets the match raise source 3
CLINT_MSIP0x0200_0000Bit 0 raises the machine software interrupt
SYSCTL_TB_ARG0x1000_402CThe bench's divider for this case

irq.h: irq_enable(TIMER_IRQ, handler) enables source 3 at ap_plic and the external interrupt in mie. The kit's dispatcher claims before your handler and completes after it. msip_enable(handler) enables the software interrupt. Both set mstatus.MIE.

Coverage Goal

Hit at least 5 of 5 declared bins across the runs. irq.context.3.wfi, irq.context.msip.handler, irq.claimed.3, irq.completed.3, irq.taken.msip.

Given Files

Read-only files supplied by the bench.

tb.h
#ifndef AP_TB_H
#define AP_TB_H
_Noreturn void tb_pass(void);
_Noreturn void tb_fail(unsigned code);
void tb_putc(char byte);
void tb_puts(const char *text);
void tb_put_hex(unsigned value);
void tb_event(unsigned id);
/* Slot zero is the single bench-to-firmware argument word. */
unsigned tb_arg(unsigned n);
void tb_wait_event(unsigned id);
#endif

hal.h
#ifndef AP_HAL_H
#define AP_HAL_H
#include "ap_soc_regs.h"

/* V13.4 supplies these two functions through DPI in a host-native build. */
#ifdef AP_HOST_DPI
unsigned reg_read32(unsigned address);
void reg_write32(unsigned address, unsigned value);
#else
static inline unsigned reg_read32(unsigned address)
{
    return *(volatile unsigned *)address;
}
static inline void reg_write32(unsigned address, unsigned value)
{
    *(volatile unsigned *)address = value;
}
#endif

#define FIELD_GET(field, word) (((word) & field##_MASK) >> field##_SHIFT)
#define FIELD_PREP(field, value) (((unsigned)(value) << field##_SHIFT) & field##_MASK)
#endif

irq.h
#ifndef AP_IRQ_H
#define AP_IRQ_H
typedef void (*irq_handler)(void);
unsigned plic_claim(void);
void plic_complete(unsigned id);
void irq_enable(unsigned id, irq_handler handler);
unsigned long long mtime_read(void);
void mtime_set_compare(unsigned long long deadline);
void msip_enable(irq_handler handler);
void mtime_enable(irq_handler handler);
#endif

Timer and PWM · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x000RUN0runrw0x0regsStarts and stops the counter. 1: the counter runs.
0x004DUTY2:0slotsrw0x0regsHow many of the four slots drive the PWM pin high. 0 to 4.
0x008CAPTURED1:0slotroblockThe slot number saved when the capture input fired. Slot number.
0x00CDIV_TOP7:0toprw0x00regsPrescaler: the last count before the slot number steps. A write restarts the prescaler. Last prescaler count.
0x010COMPARE1:0slotrw0x0regsThe slot number that raises match. Slot number.
0x014IRQ_CLEAR0matchwonot storedWriting 1 to bit 0 clears the stored match flag. 1 clears the flag.
0x018IRQ_ENABLE0matchrw0x0regsLets the stored match flag raise interrupt source 3. 1 enables the request.
0x020EVENT0enablerw0x0regsSix teaching fields (S6.8). Plain read and write.
0x020EVENT1onlineroblockA level from the hardware.
0x020EVENT2kickwonot storedA written 1 makes one pulse.
0x020EVENT3pendingw1cblockSet by an event; a written 1 clears it.
0x020EVENT4seenrcblockSet by an event; a read clears it.
0x020EVENT5routerw0x0blockTakes the first write only.
0x024EVENT_COUNT_LOW31:0valueroblockLow 32 bits of the 64-bit event counter. Count, low word.
0x028EVENT_COUNT_HIGH31:0valueroblockHigh 32 bits of the event counter. Count, high word.
ap_sysctl · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x000CHIP_ID31:0valuero0x2A5310C5constThe chip's identity: the IDCODE of the TAP. Fixed number.
0x004RESET_CAUSE0power_onroblockSticky record of reset sources since power-on. Power-on reset.
0x004RESET_CAUSE1pinroblockReset pin.
0x004RESET_CAUSE2watchdogroblockWatchdog.
0x004RESET_CAUSE3softwareroblockSoftware request.
0x004RESET_CAUSE4debugroblockDebug request.
0x008SOFT_RESET0timerwonot storedReset requests from software. 1 arms one restart of the timer.
0x008SOFT_RESET1chipwonot stored1 requests the software reset (new in v1.5).
0x00CSTRAPS0bootroblockStraps as sampled at power-up (new in v1.5). 0: start in the boot ROM. 1: start in SRAM.
0x010PINMUX31:0selectrw0x00000000regsTwo bits for each of sixteen pads: 0 released, 1 GPIO, 2 serial, 3 auxiliary. Pad n uses bits 2n+1 and 2n.
0x014CLK_GATE0uart0rw0x1regs1 lets that block's clock run (new in v1.5). UART0.
0x014CLK_GATE1gpiorw0x1regsGPIO.
0x014CLK_GATE2timerrw0x1regsTimer and PWM.
0x014CLK_GATE3spirw0x1regsSPI.
0x014CLK_GATE4dmarw0x1regsDMA.
0x018LOCK0pinmuxw1s0x0regsWrite-once locks. Only a reset clears one (new in v1.5). 1: PINMUX ignores writes.
0x018LOCK1clk_gatew1s0x0regs1: CLK_GATE ignores writes.
0x018LOCK2debugw1s0x0regs1: the debug lock is closed.
0x020TB_STATUS31:0valuerw0x00000000regs (power-on reset only)Scratch word 0. The firmware kit reports progress, pass and fail here. Plain storage.
0x024TB_PUTC31:0valuerw0x00000000regs (power-on reset only)Scratch word 1. The firmware kit writes one character here. Plain storage.
0x028TB_EVENT31:0valuerw0x00000000regs (power-on reset only)Scratch word 2. The firmware kit writes an event number here. Plain storage.
0x02CTB_ARG31:0valuerw0x00000000regs (power-on reset only)Scratch word 3. A bench leaves a word for the firmware here. Plain storage.

Examples

Example 1

Divider 40
Input
The timer with divider 40
Output
console "timer 00000001 soft 00000001 top 00000028"; pass; all five bins hit

With a divider of 40 the match comes about 80 cycles after RUN, long after the core went to sleep.

Example 2

A Firmware That Never Sleeps
Input
the same program with a busy loop for wfi
Output
pass, but irq.context.3.wfi stays empty

The request rises while the core runs its loop: the bench counts irq.context.3.other_cpu instead.

Example 3

No Second Request
Input
a timer handler that does not write MSIP
Output
irq.context.msip.handler and irq.taken.msip empty

Nothing rises while the core is inside a handler, so the nested-arrival bin cannot fill.

Constraints and Contract

  • C firmware with the kit, SRAM layout.
  • Print exactly one line: timer, soft and top with their values as eight hex digits.
  • Every wait needs a limit; a run must end in a pass or a fail word.

Hints

Hint 1

Which instruction makes the core sleep, and what wakes it?

Hint 2

Where in your program is the core certainly inside a handler? Raise the second request there.

Hint 3

The handler of a level request must remove its cause, or the request rises again at once.

What the grader checks: Each case runs your firmware once on the coverage build of the bench (Verilator 5.052 2026-09-05) with a different divider in TB_ARG. Each must pass and print the expected line; the five bins are merged over all cases and must all be hit. The bins say a request rose in a core state. They do not check what your handlers did with it, except through the printed counts.

3 checks on ap_soc v1.5 · firmware · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain your text only; the chip, the bench and the hidden checks stay on the server. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 2 problems on this path

Report a problem with this page

Your code CSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Divider 40

Input
The timer with divider 40
Expected output
console "timer 00000001 soft 00000001 top 00000028"; pass; all five bins hit

With a divider of 40 the match comes about 80 cycles after RUN, long after the core went to sleep.

Run samples executes all 1 public checks. Check all tests runs 3 checks on the server.

ap_soc v1.5 · firmware · server