V12.5.1 Close the Holes on a Budget

HardOptimize35 minap_soc v1.5 · firmware · serverReview the topic

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
pin.io6.sel1, pin.io7.sel1, every sel2 bin, gate.timer.shut and gate.spi.shut were already hit by the E11 suite, so they are not in this list.
BinsWhat earns a hit
pin.io0.sel1 to pin.io5.sel1, pin.io8.sel1 to pin.io15.sel1PINMUX selects value 1 (GPIO) for that pad: a change of its two bits to 1
pin.io0.sel3 to pin.io4.sel3PINMUX selects value 3 (the auxiliary function) for that pad
gate.uart.shut, gate.gpio.shut, gate.dma.shutThat block's clock gate closes: CLK_GATE bit 0, 1 or 4 written 0
irq.context.mtip.wfi, irq.taken.mtipThe machine timer line rises while the core sleeps in wfi; the core takes the trap
RegisterAddressUse
SYSCTL_PINMUX0x1000_4010Bits 2n+1:2n are pad n's select
SYSCTL_CLK_GATE0x1000_4014Bit 0 UART0, 1 GPIO, 2 timer, 3 SPI, 4 DMA; 1 runs the clock
CLINT_MTIME, CLINT_MTIMECMP0x0200_BFF8, 0x0200_4000mtime 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
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.
ap_clint · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x000000MSIP0msiprw0x0blockMachine software interrupt request of hart 0. 1 raises the request.
0x004000MTIMECMP_LOW31:0valuerw0xFFFFFFFFblockLow word of mtimecmp. Compare value, low word.
0x004004MTIMECMP_HIGH31:0valuerw0xFFFFFFFFblockHigh word of mtimecmp. Compare value, high word.
0x00BFF8MTIME_LOW31:0valuerw0x00000000blockLow word of mtime. Time, low word.
0x00BFFCMTIME_HIGH31:0valuerw0x00000000blockHigh 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

Report a problem with this page

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

The Quiet Run

Input
one run of your test
Expected 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.

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

ap_soc v1.5 · firmware · server