Hardware interview practice
Model sparse banked memory with packed keys
Build a sparse SystemVerilog memory model addressed by an 8-bit bank and a 16-bit byte offset. Reads must distinguish an unwritten location from a location explicitly written with zero.
Starting point
Question code
typedef struct packed {
bit [7:0] bank;
bit [15:0] offset;
} addr_t;
bit [31:0] mem[addr_t];Reviewed example
Work through one case
Input
write address '{bank:8'h02, offset:16'h0010} with 32'h0000_CAFE, then read the same addressExpected output
data = 32'h0000_CAFE, uninitialized_read = 0The packed struct forms one stable integral key, so the later lookup finds exactly the previously written bank-and-offset pair.
What to cover
Requirements
- Store every valid write at the exact packed address key.
- Check exists() before indexing on a read.
- Return zero and assert uninitialized_read for an unwritten address.
- Delete all associative-array entries on reset or model flush.
