Close a list of holes from the chip's coverage ledger with one firmware test, inside a total cycle budget.
Hardware context: The merge of eight coverage runs of ap_soc v1.5 across tests and across processor and seat. Closing holes from the ledger inside a budget.
The chip's coverage ledger (docs/soc/chip-coverage.json) lists 205 holes after its eight-run suite. Closing holes costs simulation time, and at chip level every cycle is expensive. This problem gives you 24 of those holes and a budget of 1,190 system cycles for all runs together.
Write one C firmware test that hits every bin in the table below and passes. It runs twice: once quiet, once with random board traffic on the UART line and pads IO8 to IO14. The system cycles of both runs are added, and 10% over the budget is allowed.
Recorded on the good chip (riscv64-unknown-elf-gcc (14.2.0+19) 14.2.0): the reference takes 1076 cycles for both runs. Writing one pad at a time takes 1520. A sleep of 400 ticks takes 1748. Both are over the ceiling of 1309. The counts depend on the compiler version. So the reference sits 233 cycles under the ceiling, and the nearest slow answer 211 over it. A sleep of only 4 ticks is cheaper, but the timer fires before the core sleeps and the wfi bin stays empty.
Full specification
| Bins | What earns a hit |
|---|---|
| pin.io0.sel1 to pin.io5.sel1, pin.io8.sel1 to pin.io15.sel1 | PINMUX selects value 1 (GPIO) for that pad: a change of its two bits to 1 |
| pin.io0.sel3 to pin.io4.sel3 | PINMUX selects value 3 (the auxiliary function) for that pad |
| gate.uart.shut, gate.gpio.shut, gate.dma.shut | That block's clock gate closes: CLK_GATE bit 0, 1 or 4 written 0 |
| irq.context.mtip.wfi, irq.taken.mtip | The machine timer line rises while the core sleeps in wfi; the core takes the trap |
| Register | Address | Use |
|---|---|---|
| SYSCTL_PINMUX | 0x1000_4010 | Bits 2n+1:2n are pad n's select |
| SYSCTL_CLK_GATE | 0x1000_4014 | Bit 0 UART0, 1 GPIO, 2 timer, 3 SPI, 4 DMA; 1 runs the clock |
| CLINT_MTIME, CLINT_MTIMECMP | 0x0200_BFF8, 0x0200_4000 | mtime counts cycles of clk_sys; the line rises when mtime reaches mtimecmp |
irq.h gives mtime_read, mtime_set_compare and mtime_enable(handler). The handler must move mtimecmp out of reach, or the line stays high. A wfi wakes the core when an interrupt enabled in mie is pending, even with mstatus.MIE clear. So you can clear MIE, test your flag and sleep, with no handler running in between.
Coverage Goal
Hit at least 24 of 24 declared bins across the runs. pin.io0.sel1, pin.io1.sel1, pin.io2.sel1, pin.io3.sel1, pin.io4.sel1, pin.io5.sel1, pin.io8.sel1, pin.io9.sel1, pin.io10.sel1, pin.io11.sel1, pin.io12.sel1, pin.io13.sel1, pin.io14.sel1, pin.io15.sel1, pin.io0.sel3, pin.io1.sel3, pin.io2.sel3, pin.io3.sel3, pin.io4.sel3, gate.uart.shut, gate.gpio.shut, gate.dma.shut, irq.context.mtip.wfi, irq.taken.mtip.
Total budget across runs is 1190 system cycles with a 10% grading margin.
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
ap_sysctl · register manual
| Offset | Register | Bits | Field | Access | Reset | Held by | Meaning |
|---|---|---|---|---|---|---|---|
0x000 | CHIP_ID | 31:0 | value | ro | 0x2A5310C5 | const | The chip's identity: the IDCODE of the TAP. Fixed number. |
0x004 | RESET_CAUSE | 0 | power_on | ro | block | Sticky record of reset sources since power-on. Power-on reset. | |
0x004 | RESET_CAUSE | 1 | pin | ro | block | Reset pin. | |
0x004 | RESET_CAUSE | 2 | watchdog | ro | block | Watchdog. | |
0x004 | RESET_CAUSE | 3 | software | ro | block | Software request. | |
0x004 | RESET_CAUSE | 4 | debug | ro | block | Debug request. | |
0x008 | SOFT_RESET | 0 | timer | wo | not stored | Reset requests from software. 1 arms one restart of the timer. | |
0x008 | SOFT_RESET | 1 | chip | wo | not stored | 1 requests the software reset (new in v1.5). | |
0x00C | STRAPS | 0 | boot | ro | block | Straps as sampled at power-up (new in v1.5). 0: start in the boot ROM. 1: start in SRAM. | |
0x010 | PINMUX | 31:0 | select | rw | 0x00000000 | regs | Two bits for each of sixteen pads: 0 released, 1 GPIO, 2 serial, 3 auxiliary. Pad n uses bits 2n+1 and 2n. |
0x014 | CLK_GATE | 0 | uart0 | rw | 0x1 | regs | 1 lets that block's clock run (new in v1.5). UART0. |
0x014 | CLK_GATE | 1 | gpio | rw | 0x1 | regs | GPIO. |
0x014 | CLK_GATE | 2 | timer | rw | 0x1 | regs | Timer and PWM. |
0x014 | CLK_GATE | 3 | spi | rw | 0x1 | regs | SPI. |
0x014 | CLK_GATE | 4 | dma | rw | 0x1 | regs | DMA. |
0x018 | LOCK | 0 | pinmux | w1s | 0x0 | regs | Write-once locks. Only a reset clears one (new in v1.5). 1: PINMUX ignores writes. |
0x018 | LOCK | 1 | clk_gate | w1s | 0x0 | regs | 1: CLK_GATE ignores writes. |
0x018 | LOCK | 2 | debug | w1s | 0x0 | regs | 1: the debug lock is closed. |
0x020 | TB_STATUS | 31:0 | value | rw | 0x00000000 | regs (power-on reset only) | Scratch word 0. The firmware kit reports progress, pass and fail here. Plain storage. |
0x024 | TB_PUTC | 31:0 | value | rw | 0x00000000 | regs (power-on reset only) | Scratch word 1. The firmware kit writes one character here. Plain storage. |
0x028 | TB_EVENT | 31:0 | value | rw | 0x00000000 | regs (power-on reset only) | Scratch word 2. The firmware kit writes an event number here. Plain storage. |
0x02C | TB_ARG | 31:0 | value | rw | 0x00000000 | regs (power-on reset only) | Scratch word 3. A bench leaves a word for the firmware here. Plain storage. |
## ap_sysctl (base 0x1000_4000, clk_sys, PD_MAIN) | Offset | Register | Bits | Field | Access | Reset | Held by | Meaning | | --- | --- | --- | --- | --- | --- | --- | --- | | `0x000` | CHIP_ID | 31:0 | value | ro | `0x2A5310C5` | const | The chip's identity: the IDCODE of the TAP. Fixed number. | | `0x004` | RESET_CAUSE | 0 | power_on | ro | | block | Sticky record of reset sources since power-on. Power-on reset. | | `0x004` | RESET_CAUSE | 1 | pin | ro | | block | Reset pin. | | `0x004` | RESET_CAUSE | 2 | watchdog | ro | | block | Watchdog. | | `0x004` | RESET_CAUSE | 3 | software | ro | | block | Software request. | | `0x004` | RESET_CAUSE | 4 | debug | ro | | block | Debug request. | | `0x008` | SOFT_RESET | 0 | timer | wo | | not stored | Reset requests from software. 1 arms one restart of the timer. | | `0x008` | SOFT_RESET | 1 | chip | wo | | not stored | 1 requests the software reset (new in v1.5). | | `0x00C` | STRAPS | 0 | boot | ro | | block | Straps as sampled at power-up (new in v1.5). 0: start in the boot ROM. 1: start in SRAM. | | `0x010` | PINMUX | 31:0 | select | rw | `0x00000000` | regs | Two bits for each of sixteen pads: 0 released, 1 GPIO, 2 serial, 3 auxiliary. Pad n uses bits 2n+1 and 2n. | | `0x014` | CLK_GATE | 0 | uart0 | rw | `0x1` | regs | 1 lets that block's clock run (new in v1.5). UART0. | | `0x014` | CLK_GATE | 1 | gpio | rw | `0x1` | regs | GPIO. | | `0x014` | CLK_GATE | 2 | timer | rw | `0x1` | regs | Timer and PWM. | | `0x014` | CLK_GATE | 3 | spi | rw | `0x1` | regs | SPI. | | `0x014` | CLK_GATE | 4 | dma | rw | `0x1` | regs | DMA. | | `0x018` | LOCK | 0 | pinmux | w1s | `0x0` | regs | Write-once locks. Only a reset clears one (new in v1.5). 1: PINMUX ignores writes. | | `0x018` | LOCK | 1 | clk_gate | w1s | `0x0` | regs | 1: CLK_GATE ignores writes. | | `0x018` | LOCK | 2 | debug | w1s | `0x0` | regs | 1: the debug lock is closed. | | `0x020` | TB_STATUS | 31:0 | value | rw | `0x00000000` | regs (power-on reset only) | Scratch word 0. The firmware kit reports progress, pass and fail here. Plain storage. | | `0x024` | TB_PUTC | 31:0 | value | rw | `0x00000000` | regs (power-on reset only) | Scratch word 1. The firmware kit writes one character here. Plain storage. | | `0x028` | TB_EVENT | 31:0 | value | rw | `0x00000000` | regs (power-on reset only) | Scratch word 2. The firmware kit writes an event number here. Plain storage. | | `0x02C` | TB_ARG | 31:0 | value | rw | `0x00000000` | regs (power-on reset only) | Scratch word 3. A bench leaves a word for the firmware here. Plain storage. |
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 Quiet Run- Input
one run of your test
- Output
pass; every bin hit; 538 cycles for the reference
Two PINMUX words close nineteen pin bins, two CLK_GATE writes close three gate bins, and one short sleep closes the timer pair.
Example 2
One Pad at a Time- Input
a test that walks the pads
- Output
1520 cycles for both runs: over budget
Every bin is hit, but thirty-two writes cost more than two.
Example 3
A Sleep That Is Too Short- Input
mtimecmp only 4 ticks ahead
- Output
irq.context.mtip.wfi stays empty
The line rises before the core reaches wfi, so the bench counts it as other_cpu, not wfi.
Constraints and Contract
- C firmware with the kit, SRAM layout.
- All runs together within 1,190 system cycles plus 10%.
- End with the pass word; leave PINMUX at 0 and every clock running before you do.
Hints
Hint 1
How many PINMUX writes does it take to give every pad select 1? What about select 3?
Hint 2
Which CLK_GATE value stops the three clocks of the list at once?
Hint 3
How long must the machine timer wait so that the core is already asleep, and no longer?
What the grader checks: Your firmware runs twice on the coverage build of the bench: once quiet, once with board traffic (gap 300, seed 7). Both must pass. Every bin must be hit in at least one run. The system cycles of both runs together must stay within 1,190 plus 10%. The cycle count depends on the compiler (recorded with riscv64-unknown-elf-gcc (14.2.0+19) 14.2.0); the budget leaves room for that. The bins do not check that the pads or clocks did anything useful while selected.
2 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