Hardware interview practice
Compile, program, and verify a bounded pattern matcher
Write the bounded pattern compiler, whole-record oracle, and transaction checks for literals, dot, and postfix star.
Starting point
Question code
typedef enum {
PAT_OK, PAT_BAD_ARG, PAT_FULL, PAT_MMIO, PAT_TIMEOUT
} pat_status_t;
pat_status_t pat_program(const char *pattern, size_t length,
uint32_t timeout);
typedef struct packed {
byte unsigned literal;
bit any;
bit star;
} pattern_token_t;
function automatic bit whole_record_match(
input byte unsigned record[$],
input pattern_token_t token[$]
);Reviewed example
Work through one case
Input
compile pattern="A.*B"; record="AXYB"Expected output
shadow tokens=[literal A, dot-star, literal B]; whole-record match=1The star binds only to the preceding dot, which consumes XY while the leading A and trailing B anchor the entire record.
What to cover
Requirements
- Reject a leading or repeated star, patterns above 48 bytes, and more than 24 compiled tokens before MMIO; an empty pattern is a valid committed program.
- Use canonical token encoding, zero unused shadow slots, issue a release barrier, and preserve the active program after rejected commit, bus error, or timeout.
- Build an independent prefix table for whole-record matching where star repeats only its preceding literal or dot zero or more times.
- Enforce command and frame error priority, bounded responses, stable stalls, silent abort, and reset that clears both active and shadow configurations.
