Skip to content
code0.HDL

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

  1. Read scalarCarrier's signature. The result type already states the target width.
  2. Follow the result through encode into foreignEchoByte. In the default Run, the else branch defines a BSV stand-in with the same signature that returns its argument, so the display shows exactly what would cross the C boundary. Defining PRACTICE_REAL_BDPI selects the import branch instead; the build must also link the C implementation. The typed contract is the same.
  3. 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'h34 as 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.

Review this concept
Ready
Main.bsv