Skip to content
code0.HDL

26Implementation

Build a width-checked suffix

Read the width equation

Start with the complete signature of appendTag. Its input width is n, its return width is m, and Add#(n, 4, m) requires m to be exactly four larger. Then inspect the editable taggedValue declaration and the unchanged return. mkTop provides a four-bit sample, so predict both its eight-bit result width and the position of suffix 4'hd before editing.

The ? placeholder does not provide a stable representation. The repair must work for every width admitted by the signature, not merely produce 8'had for the public sample. Edit only the declaration marked TODO; preserve the function signature, proviso, return, package, and mkTop.

Reason about the minimal repair

The width equation is already present. The expression must provide exactly the same structural equation: n bits from the argument plus four bits from the fixed suffix. Concatenation performs that assembly without runtime arithmetic. Its operand order matters because the requirement places the argument above the suffix. No cast, extension, register, or width-specific constant result is needed.

After editing, Run should compile with m = 8 at the visible call and show the argument as the high hexadecimal digit and d as the low digit.

Observable completion contract

Completion is a family of mappings, not one eight-bit sample:

  • the Add#(n, 4, m) proviso and polymorphic widths remain unchanged;
  • every result is exactly n + 4 bits;
  • the input occupies result bits [m-1:4] without alteration;
  • the low four bits are always 1101;
  • one-, two-, and four-bit inputs all follow the same layout.

Progressive hints

Hint 1: solve the type relationship

For a two-bit input, write down Add#(2, 4, m). The expected result width reveals why an eight-bit-only expression is insufficient.

Hint 2: match structure to the equation

Choose an operation whose result width is the sum of its operand widths. Keep the n-bit value as one operand and the four-bit suffix as the other.

Hint 3: place the suffix

Concatenation lists high fields before low fields. Put the existing argument on the high side and use the specified four-bit suffix on the low side when initializing taggedValue.

Transfer experiment

Add a temporary Bit#(2) local and a Bit#(6) result in mkTop, then call the same function. Predict the six-bit layout before running. This checks that the proviso determines a fresh m at each call rather than documenting a fixed width. Remove the extra display after the experiment.

Review this concept
Ready
Main.bsv