Hardware interview practice
Bounded MMIO command driver
A test engine starts through one command register and reports DONE or ERROR in a status register. Write a C driver that issues one command and polls with a fixed bound.
Starting point
Question code
enum rc { OK, BUSY, HWERR, TIMEOUT };
enum rc run_cmd(volatile uint32_t *cmd,
volatile uint32_t *status, uint32_t value);
// status bits: BUSY=1,DONE=2,ERROR=4; DONE/ERROR W1CReviewed example
Work through one case
Input
The first status-register read has BUSY set.Expected output
Return BUSY with zero writes to cmd or status.The driver checks the initial BUSY bit before clearing stale flags or issuing the command, so it exits without modifying MMIO.
What to cover
Requirements
- If the first status read has BUSY set, return BUSY without writing either register.
- Clear old DONE and ERROR by writing bits 2|4 to status, then write value to cmd exactly once.
- Read status at most 100 times; ERROR has priority over DONE if both are observed.
- Return HWERR on ERROR, OK on DONE, and TIMEOUT after 100 reads with neither; do no extra MMIO after returning.
