23Verification
Read a request field
Diagnose the starter
Intended behavior. A Request keeps an eight-bit starting address, a
four-bit transfer length, and a read/write flag in named fields. lastAddr
must add the zero-extended length to req.addr; arithmetic remains eight bits,
so a sum past 8'hff wraps around.
Current behavior. The starter compiles, but lastAddr packs the whole
request and assumes its low eight bits are the address. Those bits actually
mix parts of several fields, so changing length or write can corrupt the
address used by the calculation.
Fix. Read the starting address through the struct field intended to hold
it. Keep the Request declaration, constructor, and function signatures
unchanged.
Read and predict
Read the struct declaration and constructor before lastAddr. For a fixed
address, predict whether toggling write should affect the result, then explain
why slicing pack(req) violates that expectation.
mkTop builds a request with address 8'h40, length 3, and write set, then
prints addr and lastAddr. The first declared field occupies the high end of
the packed value. Work out which field bits land in the low eight positions and
predict the starter's lastAddr before running it once.
Repair path
Change only the source of the starting address. Keep the existing explicit
extension for length so the addition occurs at eight bits. After the repair,
the public run should print hexadecimal 43 for lastAddr. Explain why that value no longer
depends on write, then try cases that change length and write
independently, including one sum that wraps.
Completion contract
- Compute the result from the named
addrandlengthfields. - Make the result independent of the
writefield. - Preserve eight-bit wraparound.
- Keep
Request,makeRequest, and both function signatures unchanged. - Work across field combinations and the 8-bit boundary; matching the packed layout of one request is insufficient.
Progressive hints
Hint 1
The dot operator selects a struct field by name, without exposing where its bits happen to sit in the packed representation.
Hint 2
The length is already read as req.length. The starting address has a field
that can be read in exactly the same way.
Concept
A struct gives each part of a value a stable name. deriving (Bits) also gives
the complete struct an encoding, but bit positions are a storage detail rather
than the interface between fields and logic. Named selection keeps the source
correct if field widths or declaration order later change.
Transfer experiment
Imagine the fields are reordered or a new flag is inserted. Explain why named selection survives both changes while a fixed packed slice must be audited.