V4.8.2 Make the Crossed Wire Show

MediumBreak It20 minap_soc v1.5 · firmware · serverReview the topic

A faulty chip has one wire of its interrupt map crossed. Every block still works alone. Write the firmware that passes on the good chip and makes the faulty one fail visibly.

Hardware context: A scenario on ap_soc v1.5 where the DMA, UART0, GPIO and ap_plic work at once. Making a crossed interrupt wire fail visibly.

Here is the faulty chip, stated plainly. Its generated top level wires ap_plic input 2 to the request of the Timer and PWM block. Input 2 belongs to GPIO. The timer still asks on its own input 3, and GPIO itself works. Every block bench would pass it.

Write firmware that passes on the good chip and fails visibly on this one. A fail word of yours counts, and so does a bench check. A hang to the cycle limit does not count. On the good chip, print timer and gpio apart and pass.

Full specification
SourceDeviceKit
2GPIOgpio_rising_irq(mask) enables a rising-edge interrupt; gpio_ack(mask) clears it. TB_EVENT 5 makes the board pulse IO8
3Timer and PWMtimer_start(top, duty, compare) enables its match interrupt; timer_stop clears it

irq_enable(id, handler) enables a source at ap_plic with priority 1 and gives it a handler. The kit claims each external interrupt once and calls the handler of the claimed number. Of two pending sources of equal priority, the smaller number is claimed first.

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

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

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

GPIO · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x000DIR15:0valuerw0x0000regs1 turns the output driver of that pin on. One bit for each pin.
0x004IN15:0valueroblockThe pin levels, after two sampling flip-flops. One bit for each pin.
0x008OUT15:0valuerw0x0000regsThe saved output value. One bit for each pin.
0x00CSET15:0valuew1sacts on OUT.valueA 1 sets that bit of OUT. One bit for each pin.
0x010CLR15:0valuew1cacts on OUT.valueA 1 clears that bit of OUT. One bit for each pin.
0x014TGL15:0valuew1tacts on OUT.valueA 1 inverts that bit of OUT. One bit for each pin.
0x018IRQ_ENABLE15:0valuerw0x0000regsA 1 lets that pin's status bit raise interrupt source 2. One bit for each pin.
0x01CLEVEL_MODE15:0valuerw0x0000regs1: the pin is a level cause. 0: an edge cause. One bit for each pin.
0x020ACTIVE_HIGH15:0valuerw0xFFFFregs1: a high level, or a rising edge, is the event. One bit for each pin.
0x024STATUS15:0valuew1cblockRaw status. A written 1 clears the saved edge flag of that pin. One bit for each pin.
0x028MASKED_STATUS15:0valueroblockSTATUS AND IRQ_ENABLE. One bit for each pin.
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_plic · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x004 + 4 × (n − 1), n = 1 to 16PRIORITY2:0levelrw0x0blockPriority of source n, 0 to 7. 0 never competes. Priority.
0x001000PENDING16:1sourcesroblockPending bits; bit number = source ID. One bit for each source.
0x002000ENABLE16:1sourcesrw0x0000blockEnable bits; bit number = source ID. One bit for each source.
0x200000THRESHOLD2:0levelrw0x0blockThe core sees a request only above this priority. Threshold.
0x200004CLAIM4:0idrw0x00blockRead: claim, returns the winner's ID and clears its pending bit. Write: complete, names the ID that is done. Source ID.

Examples

Example 1

The Good Chip
Input
one run
Output
console "timer and gpio apart"; pass

The timer's interrupt runs only its own handler; GPIO's runs only for an edge.

Example 2

The Faulty Chip
Input
one run
Output
a fail word

When the timer matches, GPIO's handler runs too. The test sees a handler that should not have run.

Example 3

What Does Not Count
Input
a test that hangs
Output
not a catch

A wait for the edge with no limit never ends on the faulty chip; the judge needs a visible failure.

Constraints and Contract

  • C firmware with the kit, SRAM layout.
  • The good chip runs first and must pass with the console line; the faulty chip must then fail visibly.

Hints

Hint 1

On the faulty chip, what does ap_plic see when the timer matches and both sources are enabled?

Hint 2

Which handler would run, and how would your test know it should not have?

Hint 3

A wait that never ends is not a failure the judge can see.

What the grader checks: Your firmware runs on the good chip, which must pass with the console line. Then it runs on the stated faulty chip, which must fail visibly.

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

Report a problem with this page

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

The Good Chip

Input
one run
Expected output
console "timer and gpio apart"; pass

The timer's interrupt runs only its own handler; GPIO's runs only for an edge.

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

ap_soc v1.5 · firmware · server