Two GPIO pins share one interrupt. One is a pulse line, counted edge by edge; the other is an alarm line that stays high until served. Write the handler that counts both, exactly, without drowning in a level that will not go away.
Hardware context: Interrupts of ap_soc v1.5 from each device through ap_plic and the core's wires to a C handler. An edge and a level on one shared source.
GPIO pin 8 carries pulses: count every rising edge. GPIO pin 9 is an alarm line: the board raises it and holds it high until it decides to lower it. Count each high period once. Both pins ask through the GPIO block, which is ap_plic source 2, so one handler serves both.
Write 0x7A00 to TB_EVENT when you are ready. The board then pulses and raises lines, and finally puts 0x7AFF into TB_ARG. Pulses come at least 400 cycles apart, and the alarm stays high for at least 300 cycles each time. Print edges, the count, alarms , the count, each as eight hex digits, and a new line. In some runs the alarm is already high before your event, and it must be counted too.
Full specification
| GPIO register | Offset | Use |
|---|---|---|
| IN | 0x04 | The synchronised pin levels |
| IRQ_ENABLE | 0x18 | A pin may ask only while its bit is 1 |
| LEVEL_MODE | 0x1C | 1: the pin asks while it is active; 0: it asks on an edge |
| ACTIVE_HIGH | 0x20 | 1: active high, rising edge |
| STATUS | 0x24 | Latched requests; write 1 to clear |
| MASKED_STATUS | 0x28 | STATUS and IRQ_ENABLE: which pins ask now |
A level request cannot be cleared while its pin is still active: writing STATUS does not quieten it. Each interrupt the core takes is counted by the bench (ext_gpio).
Given Files
Read-only files supplied by the bench.
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
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
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
GPIO · register manual
| Offset | Register | Bits | Field | Access | Reset | Held by | Meaning |
|---|---|---|---|---|---|---|---|
0x000 | DIR | 15:0 | value | rw | 0x0000 | regs | 1 turns the output driver of that pin on. One bit for each pin. |
0x004 | IN | 15:0 | value | ro | block | The pin levels, after two sampling flip-flops. One bit for each pin. | |
0x008 | OUT | 15:0 | value | rw | 0x0000 | regs | The saved output value. One bit for each pin. |
0x00C | SET | 15:0 | value | w1s | acts on OUT.value | A 1 sets that bit of OUT. One bit for each pin. | |
0x010 | CLR | 15:0 | value | w1c | acts on OUT.value | A 1 clears that bit of OUT. One bit for each pin. | |
0x014 | TGL | 15:0 | value | w1t | acts on OUT.value | A 1 inverts that bit of OUT. One bit for each pin. | |
0x018 | IRQ_ENABLE | 15:0 | value | rw | 0x0000 | regs | A 1 lets that pin's status bit raise interrupt source 2. One bit for each pin. |
0x01C | LEVEL_MODE | 15:0 | value | rw | 0x0000 | regs | 1: the pin is a level cause. 0: an edge cause. One bit for each pin. |
0x020 | ACTIVE_HIGH | 15:0 | value | rw | 0xFFFF | regs | 1: a high level, or a rising edge, is the event. One bit for each pin. |
0x024 | STATUS | 15:0 | value | w1c | block | Raw status. A written 1 clears the saved edge flag of that pin. One bit for each pin. | |
0x028 | MASKED_STATUS | 15:0 | value | ro | block | STATUS AND IRQ_ENABLE. One bit for each pin. |
## GPIO (base 0x1000_1000, interrupt source 2, clk_sys, PD_MAIN) | Offset | Register | Bits | Field | Access | Reset | Held by | Meaning | | --- | --- | --- | --- | --- | --- | --- | --- | | `0x000` | DIR | 15:0 | value | rw | `0x0000` | regs | 1 turns the output driver of that pin on. One bit for each pin. | | `0x004` | IN | 15:0 | value | ro | | block | The pin levels, after two sampling flip-flops. One bit for each pin. | | `0x008` | OUT | 15:0 | value | rw | `0x0000` | regs | The saved output value. One bit for each pin. | | `0x00C` | SET | 15:0 | value | w1s | | acts on OUT.value | A 1 sets that bit of OUT. One bit for each pin. | | `0x010` | CLR | 15:0 | value | w1c | | acts on OUT.value | A 1 clears that bit of OUT. One bit for each pin. | | `0x014` | TGL | 15:0 | value | w1t | | acts on OUT.value | A 1 inverts that bit of OUT. One bit for each pin. | | `0x018` | IRQ_ENABLE | 15:0 | value | rw | `0x0000` | regs | A 1 lets that pin's status bit raise interrupt source 2. One bit for each pin. | | `0x01C` | LEVEL_MODE | 15:0 | value | rw | `0x0000` | regs | 1: the pin is a level cause. 0: an edge cause. One bit for each pin. | | `0x020` | ACTIVE_HIGH | 15:0 | value | rw | `0xFFFF` | regs | 1: a high level, or a rising edge, is the event. One bit for each pin. | | `0x024` | STATUS | 15:0 | value | w1c | | block | Raw status. A written 1 clears the saved edge flag of that pin. One bit for each pin. | | `0x028` | MASKED_STATUS | 15:0 | value | ro | | block | STATUS AND IRQ_ENABLE. One bit for each pin. |
Examples
Example 1
Three Pulses, One Alarm- Input
three pulses and one alarm
- Output
edges 00000003 alarms 00000001
Four requests through source 2; the handler reads MASKED_STATUS to tell them apart.
Example 2
Two Alarms- Input
two alarms, no pulse
- Output
edges 00000000 alarms 00000002
Each high period counts once: the handler quietens pin 9 and main re-enables it after the line falls.
Example 3
An Early Alarm- Input
an alarm already high at the start
- Output
edges 00000001 alarms 00000001
The alarm was high before the event. In level mode it asks as soon as it is enabled.
Constraints and Contract
- C firmware with the kit, SRAM layout.
- Take both pins by interrupt. The bench requires as many GPIO interrupts as edges and alarms, and allows two more at most.
- Keep the event numbers and the line format.
Hints
Hint 1
Which register tells the handler which of the two pins asked?
Hint 2
What happens if the handler returns while pin 9 is still high and still enabled?
Hint 3
When is it safe to let pin 9 ask again?
What the grader checks: Each case drives the two pins in a chosen pattern. It compares the console line and the number of GPIO interrupts the core took. It cannot see the order in which your handler did its work.
8 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