41Implementation
Split a frame by result width
Problem
FrameSplitter.fields must divide a 12-bit frame into a 4-bit kind (the high
bits) and an 8-bit payload (the low bits), returned as a tuple in that order.
The starter returns tuple2(0, 0), so the public run prints kind = 0 payload = 00 for the sample 12'ha5c.
Implement the method body with split, as the starter's TODO asks. Keep the
interface, the method's result type, the module structure, and mkTop
unchanged. Do not spell out bit indices.
Read and predict
- Read the method signature. The input is
Bit#(12)and the result isTuple2#(Bit#(4), Bit#(8)). Write down the two result widths and check that they add up to the input width. - Read
mkTop. Amatchassignment receives the tuple and printskindfirst, thenpayload. - Before pressing Run, predict the starter's line, then predict the line
after a correct repair for
12'ha5c: which hex digit belongs tokindand which two belong topayload?
Run the starter once to confirm that both fields are zero.
Make the repair
split takes one vector and returns a pair whose widths come from the type the
caller expects, not from arguments. Because the method already declares that
type, the result relation is fixed before you write any expression. Replace
only the returned value, then run again and compare with your prediction.
Explain why the first field receives the most significant bits.
Completion contract
- For every 12-bit frame, the first field equals its high four bits and the second equals its low eight bits.
- No input bit is lost or duplicated between the two fields.
- The width relation comes from the declared result type, not from hand-written bit ranges.
- Returning the fields for
12'ha5cas constants matches only the sample.
Progressive hints
Hint 1: where the widths come from
The declared return type gives split both result widths. The type checker
also proves that 4 + 8 = 12, so the two fields cover the input exactly.
Hint 2: what split needs
split takes a single argument. Find the one value in the method whose type is
the 12-bit whole that the result must be cut from.
Transfer experiment
On paper, change the result type to Tuple2#(Bit#(5), Bit#(7)). Predict where
the boundary moves for 12'ha5c, and which type-level sum must still hold.