07Verification
Match a status word format
Problem
Intended behavior. A peripheral publishes a 16-bit status word with this documented layout:
| Bits | Field | Width |
|---|---|---|
| 15 | valid | 1 |
| 14–12 | channel | 3 |
| 11–0 | count | 12 |
StatusWord packs the Status input in into word and unpacks the flat
input raw into the Status output out. With valid = 1, channel = 5, and
count = 0x123, word must be d123; raw = a00f must read back as
valid = 1, channel = 2, count = 00f.
Current behavior. The starter compiles and runs, but both printed rows disagree with the table.
Task. Find the cause and fix it so that packing and unpacking both follow the table. Keep the port names, the field names, and the field widths.
Read the starter
Both conversions are one-liners, asUInt and asTypeOf, and neither mentions
a bit position. Ask where they get the layout from, then compare that source
with the table, field by field, from the top bit down.
Predict before running
Write the 16 bits of the starter's packed word for the sample input by laying
the fields out in the order the code implies. Do the same for a00f in the
unpack direction. Your predictions should match the starter's 891d and its
channel=7 count=401.
Run and explain
891d is 1 followed by 0001_0010_0011 and then 101: the count sits
directly under valid, and the channel occupies the bottom three bits. The
unpack row is the same layout read backwards. One declaration feeds both
conversions, so one mistake breaks both. After the fix, the rows read d123
and valid=1 channel=2 count=00f.
Completion check
- Every combination of field values packs to the documented bit positions.
- Every 16-bit
rawvalue unpacks into the documented fields. - Packing and unpacking agree with each other; fixing only one direction is incomplete.
Progressive hints
Hint 1
asUInt and asTypeOf do not know the peripheral's format. They read the bit
layout from the Bundle declaration and nowhere else.
Hint 2
The first declared field takes the most significant bits. Compare the order of
the second and third fields in Status with the table. A patched Cat in the
pack direction leaves the unpack direction wrong.
Concept
A Bundle that describes an external format is a specification: its field order
is the bit layout. Correcting the declaration fixes every conversion that uses
it, while manual slicing at individual use sites lets the two directions drift
apart.
Transfer experiment
Suppose the format moved valid to bit 0 and shifted the other fields up by
one. Predict the packed word for the sample input, and state which declaration
change produces it.