50Implementation
Narrow a scalar BDPI carrier
Problem
mkScalarAdapter.encode passes a 32-bit BSV value through scalarCarrier and
then into foreignEchoByte, whose foreign signature takes and returns an 8-bit
carrier. The C side expects the low byte of the value. The editable
scalarCarrier returns zero, so every input reaches the boundary as 8'h00
and the public run prints carrier = 00 for 32'h1234.
Change only the body of scalarCarrier. Keep its Bit#(32) -> Bit#(8) type,
the interface, the adapter, the foreign-function declaration, and mkTop.
Read and predict
- Read
scalarCarrier's signature. The result type already states the target width. - Follow the result through
encodeintoforeignEchoByte. In the default Run, theelsebranch defines a BSV stand-in with the same signature that returns its argument, so the display shows exactly what would cross the C boundary. DefiningPRACTICE_REAL_BDPIselects the import branch instead; the build must also link the C implementation. The typed contract is the same. - Before pressing Run, predict the starter's output, then predict the
output after a correct repair for
32'h1234. Which byte survives, and which bits are discarded?
Make the repair
The conversion is a narrowing: keep the low eight bits and drop the upper twenty-four. Replace only the returned expression, run again, and compare with your prediction. Explain why the output depends only on the low byte for every 32-bit input, not just the sample.
Completion contract
- For every 32-bit input, the carrier equals the input's low eight bits.
- Changing only the upper twenty-four bits never changes the carrier.
- The function's types and the foreign signature are unchanged.
- Returning
8'h34as a constant matches only the visible sample.
Progressive hints
Hint 1: the width is already decided
The result type fixes the target width at eight bits. The task is a width conversion, not arithmetic.
Hint 2: which conversion
Use the standard BSV conversion that discards high bits and lets the expected result type determine how many remain.
Transfer experiment
Try an input whose upper twenty-four bits differ but whose low byte is still
8'h34, and predict why the carrier is unchanged. Then change the low byte and
predict the new carrier. Restore the original input afterward.