Skip to content
code0.HDL

11Implementation

Zero-extend a nibble

Read the starter and predict

Start at the signature of zeroExtendNibble: a four-bit argument must become an eight-bit result. Then read the two editable declarations and the provided return expression. In { upper, lower }, each local contributes four bits, so the expression already has the promised width. Finally, read mkTop to find the one public input and the hexadecimal observation printed by Run.

Before editing, predict why the ? placeholders cannot establish a stable result. Also predict both placements for input 4'hf: which field would produce 8'h0f, and which would produce 8'hf0? This separates total width from field order.

Edit only the two declarations marked TODO. Keep the function signature, concatenation, package, and mkTop structure unchanged. A fixed eight-bit constant may match the visible sample but would ignore the argument.

Make the smallest repair

The return expression already assembles the fields in the correct high-to-low order. The minimal repair is therefore to give each local its intended role: one must provide a four-bit zero field independent of the input, and the other must preserve all four input bits. No arithmetic, register, rule, or wider intermediate value is needed.

After the edit, run the starter and inspect both hexadecimal digits. The first digit demonstrates that the high field is zero; the second demonstrates that the argument remains in the low field.

Observable completion contract

A complete solution satisfies the same combinational mapping for every Bit#(4) input, not only the displayed all-ones case:

  • the return type remains Bit#(8);
  • result bits [7:4] are always zero;
  • result bits [3:0] equal the input bit for bit;
  • zero, a single set bit, a mixed pattern, and all ones all obey that mapping.

Progressive hints

Hint 1: account for widths

Write the width equation for the existing concatenation. Both operands already have the size needed to make eight bits.

Hint 2: account for positions

Concatenation is written from high bits to low bits. Identify which local must be independent of value and which must carry it.

Hint 3: choose the declarations

Use a four-bit zero value for the high local. Assign the input to the low local; do not alter the return expression.

Transfer experiment

After the correct version runs, temporarily swap the two names inside the concatenation and use a nonzero, non-symmetric input such as 4'h3. Predict the new hexadecimal placement before running, then restore the original order. This experiment shows that equal total width does not prove a correct field layout.

Review this concept
Ready
Main.bsv