V9.4.1 Gates, Sleep and a Backstop

HardVerify30 minap_soc v1.5 · firmware · serverReview the topic

Write the clock-control test of the chip. Gates open at reset, peripheral clocks stop and restart, and the core sleeps with its own clock stopped. A sleep that never wakes is a hang, so give the test a backstop that turns a hang into a failure.

Hardware context: The clock gates of ap_soc v1.5, the core's gate in wfi and the always-on watchdog. A firmware test of gates and a gated sleep with a watchdog backstop.

Clock control is part of the chip, so the chip level tests it. Write a test with three parts. First, every peripheral clock gate is open after reset: CLK_GATE reads 0x1F. Second, a block keeps its state while its clock is stopped. Write 37 to UART0 DIV, stop every gate, and later read 37 back.

Third, the core sleeps with its own clock stopped. With every peripheral gate closed, set the CLINT timer to fire 60 cycles ahead, enable its line in mie and execute wfi. The core's clock stops and must start again when the timer fires. Check that the timer's pending bit (mip bit 7) is what woke it. Then open the gates again and pass.

A core that never wakes cannot report anything. Give the test a backstop: arm the watchdog just before the sleep and record in TB_ARG that you are asleep. If the watchdog resets the chip, the next boot finds that record and fails with code 3. Pass before the watchdog warns: it bites a few hundred cycles after arming.

Print clocks: sleep before the sleep. A faulty chip is caught when your test writes a fail word, or a check of the bench fails. A run that ends at the cycle limit is not caught.

Full specification
RegisterAddressUse
CLK_GATE0x1000_4014Bit 0 UART0, 1 GPIO, 2 Timer and PWM, 3 SPI, 4 DMA; 1 lets that clock run. 0x1F after every reset
UART0 DIV0x1000_0010Read and write; a block whose clock is stopped answers ERROR, so read it only with its gate open
WATCHDOG KEY, CONTROL0x1000_3000, 0x1000_3004KEY 0x51, then KEY 0xA2, then CONTROL 1 arms it. It cannot be disarmed except by a reset
TB_ARG0x1000_402CScratch; only power-on clears it

The core's clock stops in wfi when both its ports are idle and no enabled interrupt is pending. The watchdog runs on clk_aon, which nothing gates. Recorded on the good chip: the reference passes at cycle 589. An armed watchdog that is never kicked resets the chip about 338 cycles after arming.

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.
Watchdog · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x000KEY31:0valuewonot stored0x51, then 0xA2 on the next watchdog write, unlocks one write to CONTROL. Key word.
0x004CONTROL31:0valuewonot storedWrite 1 while unlocked: armed. Arms once. 1 arms the watchdog.
0x008SERVICE31:0valuewonot storedWrite 1: the kick. 1 is a kick.
UART0 · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x000TXDATA7:0datawonot storedThe low byte is the next byte to send. Byte to send.
0x004STATUS0roomroblockTransmit room and receive queue state. The transmitter can take a byte.
0x004STATUS1rx_readyroblockAt least one received byte waits.
0x004STATUS2rx_fullroblockThe receive queue is full.
0x008IRQ_ENABLE0roomrw0x0regsWhich status bits raise interrupt source 1. ROOM raises the request.
0x008IRQ_ENABLE1rx_readyrw0x0regsRX_READY raises the request (new in v1.5).
0x00CRXDATA7:0dataroblockThe oldest received byte. The read removes it from the queue. Received byte.
0x010DIV15:0cyclesrw0x0010regsBit time in clk_sys cycles (new in v1.5). clk_sys cycles a bit.

Examples

Example 1

The Good Chip
Input
One run: gates, a sleep, the wake
Output
console "clocks: sleep"; pass

The gates read 0x1F, the core sleeps about 60 cycles, the timer wakes it, and DIV still reads 37.

Example 2

A Chip Whose Sleep Never Ends
Input
a hidden chip
Output
fail code 3, after a watchdog reset

Without the backstop the run reaches the cycle limit and counts as not caught.

Example 3

A Chip with a Gate Closed After Reset
Input
a hidden chip
Output
fail code 1

Reset defines every gate, so one read of CLK_GATE finds it.

Constraints and Contract

  • C firmware with the kit, SRAM layout.
  • Print exactly clocks: sleep and nothing else.
  • One case, run on the good chip and on every hidden chip.

Hints

Hint 1

What happens to a test whose core never wakes? Which part of the chip keeps running when the core's clock stops?

Hint 2

After a reset, how does the program know it was asleep before?

Hint 3

Which register says what is pending, whatever woke the core?

What the grader checks: One case runs your firmware on the good chip and on every hidden faulty chip. The good chip must print the line and pass; each faulty chip must end with a fail word or a bench check. The grader cannot see whether the core really slept: it sees only what your checks report.

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 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 Good Chip

Input
One run: gates, a sleep, the wake
Expected output
console "clocks: sleep"; pass

The gates read 0x1F, the core sleeps about 60 cycles, the timer wakes it, and DIV still reads 37.

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

ap_soc v1.5 · firmware · server