Write the interrupt test of the chip: it passes on ap_soc and fails visibly on every faulty chip of a hidden set. The faults sit on the way from the devices to the core: a device's request, ap_plic, and the core's own interrupt wires.
Hardware context: Interrupts of ap_soc v1.5 from each device through ap_plic and the core's wires to a C handler. An interrupt test that catches hidden faults.
Write a test of the interrupt system that passes on ap_soc and fails on each of a set of hidden faulty chips. Each hidden chip has one fault between a device and a handler. It may be in a device's own request, in ap_plic itself, or on the wires into the core.
Your test runs once on each chip. The board helps on request: TB_EVENT 3 sends a byte to the UART receive pad, TB_EVENT 5 pulses IO8. Print interrupts ok and pass when every check of yours holds.
A faulty chip is caught only when your test fails visibly with a fail word (or a check of the bench). A run that waits for ever for an interrupt that never comes is not caught.
Full specification
| Source | Device | How firmware raises it |
|---|---|---|
| 1 | UART0 | RX interrupt enabled, TB_EVENT 3 |
| 2 | GPIO | Rising edge on pin 8, TB_EVENT 5 |
| 3 | Timer and PWM | timer_start with a compare |
| 4 | SPI | A frame (SPI_DATA), DONE interrupt enabled |
| 5 | DMA | A copy, DONE interrupt enabled |
| MTIP | ap_clint | MTIMECMP below MTIME, mie bit 7 |
| ap_plic | Offset | Rule |
|---|---|---|
| PRIORITY(n) | 0x04 × n | 0 never interrupts; of two pending sources the higher priority is claimed first |
| PENDING | 0x1000 | One bit a source, read only |
| ENABLE | 0x2000 | One bit a source |
| THRESHOLD | 0x20_0000 | Only a priority above it interrupts |
| CLAIM | 0x20_0004 | Read: the winning source, 0 if none; write: complete |
Given Files
Read-only files supplied by the bench.
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
drivers/gpio.h
#ifndef AP_GPIO_H
#define AP_GPIO_H
#include "hal.h"
static inline void gpio_drive(unsigned outputs, unsigned value)
{
reg_write32(GPIO_OUT, value);
reg_write32(GPIO_DIR, outputs);
}
static inline unsigned gpio_read(void)
{
return reg_read32(GPIO_IN);
}
/* Select the rising-edge inputs for this test and discard stale edges. */
static inline void gpio_rising_irq(unsigned pins)
{
reg_write32(GPIO_LEVEL_MODE, 0);
reg_write32(GPIO_ACTIVE_HIGH, pins);
reg_write32(GPIO_STATUS, pins);
reg_write32(GPIO_IRQ_ENABLE, pins);
}
static inline void gpio_ack(unsigned pins)
{
reg_write32(GPIO_STATUS, pins);
}
#endif
drivers/timer.h
#ifndef AP_TIMER_H
#define AP_TIMER_H
#include "hal.h"
static inline void timer_stop(void)
{
reg_write32(TIMER_RUN, 0);
reg_write32(TIMER_IRQ_CLEAR, TIMER_IRQ_CLEAR_MATCH_MASK);
}
static inline void timer_start(unsigned top, unsigned duty, unsigned compare)
{
timer_stop();
reg_write32(TIMER_DIV_TOP, FIELD_PREP(TIMER_DIV_TOP_TOP, top));
reg_write32(TIMER_DUTY, FIELD_PREP(TIMER_DUTY_SLOTS, duty));
reg_write32(TIMER_COMPARE, FIELD_PREP(TIMER_COMPARE_SLOT, compare));
reg_write32(TIMER_IRQ_ENABLE, TIMER_IRQ_ENABLE_MATCH_MASK);
reg_write32(TIMER_RUN, TIMER_RUN_RUN_MASK);
}
#endif
drivers/spi.h
#ifndef AP_SPI_H
#define AP_SPI_H
#include "hal.h"
static inline unsigned spi_transfer(unsigned char command)
{
while (reg_read32(SPI_STATUS) & SPI_STATUS_BUSY_MASK) { }
reg_write32(SPI_STATUS, SPI_STATUS_DONE_MASK);
reg_write32(SPI_DATA, command);
while (!FIELD_GET(SPI_STATUS_DONE, reg_read32(SPI_STATUS))) { }
return FIELD_GET(SPI_DATA_RECEIVED, reg_read32(SPI_DATA));
}
#endif
drivers/uart.h
#ifndef AP_UART_H
#define AP_UART_H
#include "hal.h"
static inline void uart_flush(void)
{
while (!FIELD_GET(UART0_STATUS_ROOM, reg_read32(UART0_STATUS))) { }
}
static inline void uart_putc(unsigned char byte)
{
uart_flush();
reg_write32(UART0_TXDATA, byte);
}
static inline unsigned char uart_getc(void)
{
while (!FIELD_GET(UART0_STATUS_RX_READY, reg_read32(UART0_STATUS))) { }
return reg_read32(UART0_RXDATA);
}
static inline void uart_enable_rx_irq(void)
{
reg_write32(UART0_IRQ_ENABLE, UART0_IRQ_ENABLE_RX_READY_MASK);
}
#endif
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
ap_plic · register manual
| Offset | Register | Bits | Field | Access | Reset | Held by | Meaning |
|---|---|---|---|---|---|---|---|
0x004 + 4 × (n − 1), n = 1 to 16 | PRIORITY | 2:0 | level | rw | 0x0 | block | Priority of source n, 0 to 7. 0 never competes. Priority. |
0x001000 | PENDING | 16:1 | sources | ro | block | Pending bits; bit number = source ID. One bit for each source. | |
0x002000 | ENABLE | 16:1 | sources | rw | 0x0000 | block | Enable bits; bit number = source ID. One bit for each source. |
0x200000 | THRESHOLD | 2:0 | level | rw | 0x0 | block | The core sees a request only above this priority. Threshold. |
0x200004 | CLAIM | 4:0 | id | rw | 0x00 | block | Read: claim, returns the winner's ID and clears its pending bit. Write: complete, names the ID that is done. Source ID. |
## ap_plic (base 0x0C00_0000, clk_sys, PD_MAIN) | Offset | Register | Bits | Field | Access | Reset | Held by | Meaning | | --- | --- | --- | --- | --- | --- | --- | --- | | `0x004` + 4 × (n − 1), n = 1 to 16 | PRIORITY | 2:0 | level | rw | `0x0` | block | Priority of source n, 0 to 7. 0 never competes. Priority. | | `0x001000` | PENDING | 16:1 | sources | ro | | block | Pending bits; bit number = source ID. One bit for each source. | | `0x002000` | ENABLE | 16:1 | sources | rw | `0x0000` | block | Enable bits; bit number = source ID. One bit for each source. | | `0x200000` | THRESHOLD | 2:0 | level | rw | `0x0` | block | The core sees a request only above this priority. Threshold. | | `0x200004` | CLAIM | 4:0 | id | rw | `0x00` | block | Read: claim, returns the winner's ID and clears its pending bit. Write: complete, names the ID that is done. Source ID. |
ap_clint · register manual
| Offset | Register | Bits | Field | Access | Reset | Held by | Meaning |
|---|---|---|---|---|---|---|---|
0x000000 | MSIP | 0 | msip | rw | 0x0 | block | Machine software interrupt request of hart 0. 1 raises the request. |
0x004000 | MTIMECMP_LOW | 31:0 | value | rw | 0xFFFFFFFF | block | Low word of mtimecmp. Compare value, low word. |
0x004004 | MTIMECMP_HIGH | 31:0 | value | rw | 0xFFFFFFFF | block | High word of mtimecmp. Compare value, high word. |
0x00BFF8 | MTIME_LOW | 31:0 | value | rw | 0x00000000 | block | Low word of mtime. Time, low word. |
0x00BFFC | MTIME_HIGH | 31:0 | value | rw | 0x00000000 | block | High word of mtime. Time, high word. |
## ap_clint (base 0x0200_0000, clk_sys, PD_MAIN) | Offset | Register | Bits | Field | Access | Reset | Held by | Meaning | | --- | --- | --- | --- | --- | --- | --- | --- | | `0x000000` | MSIP | 0 | msip | rw | `0x0` | block | Machine software interrupt request of hart 0. 1 raises the request. | | `0x004000` | MTIMECMP_LOW | 31:0 | value | rw | `0xFFFFFFFF` | block | Low word of mtimecmp. Compare value, low word. | | `0x004004` | MTIMECMP_HIGH | 31:0 | value | rw | `0xFFFFFFFF` | block | High word of mtimecmp. Compare value, high word. | | `0x00BFF8` | MTIME_LOW | 31:0 | value | rw | `0x00000000` | block | Low word of mtime. Time, low word. | | `0x00BFFC` | MTIME_HIGH | 31:0 | value | rw | `0x00000000` | block | High word of mtime. Time, high word. |
Examples
Example 1
The Good Chip- Input
one run
- Output
console "interrupts ok"; pass
Every source arrives under its own number, the priorities and the threshold behave, the timer ticks once.
Example 2
A Source Under the Wrong Number- Input
a hidden chip
- Output
your fail word
An interrupt arrives, but another handler runs. Only a check of who ran catches it.
Example 3
A Source That Never Arrives- Input
a hidden chip
- Output
your fail word
A time limit turns the wait into a report.
Constraints and Contract
- C firmware with the kit, SRAM layout.
- One case, run on the good chip and on every hidden chip.
- You may mask interrupts in mstatus (bit 3) to claim sources by hand.
Hints
Hint 1
A handler that runs is not proof: whose handler ran?
Hint 2
How many separate wires bring interrupts into the core?
Hint 3
What does your test do while it waits for an interrupt that a faulty chip never raises?
What the grader checks: Your test runs once on the good chip and once on each hidden faulty chip. The good chip must pass; each faulty chip must end with a fail word or a bench check. The page says only whether a faulty chip passed. No hidden chip has a fault that only a check of priority order or of the threshold would find. So a test without those checks can be accepted here, though it would not be complete on a real chip.
1 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 3 problems on this path