Skip to content
code0.HDL

05Verification

Match a mask literal to its width

Problem

lowNibbleOf should keep the low four bits of an eight-bit input and clear the other four. The starter already performs the right operation, but its mask is a fill literal. In a Bit#(8) declaration, '1 fills all eight positions with ones, so the AND returns the input unchanged.

Repair the mask literal without changing the function signature or replacing the & expression.

Read and predict

Find the declared type of mask, then use that context to determine the value of '1. Before editing, predict the three visible calls:

  • 8'ha5 remains 8'ha5 instead of becoming 8'h05.
  • 8'hf0 remains 8'hf0 instead of becoming 8'h00.
  • 8'h3c remains 8'h3c instead of becoming 8'h0c.

Run the starter once and compare its trace with those predictions.

Make the smallest change

Replace only the mask literal with a sized hexadecimal literal whose upper four bits are zero and lower four bits are one. Keep mask as Bit#(8) and preserve return value & mask;.

After the change, the result must satisfy both parts of the operation: the high nibble is zero, and the low nibble matches the input.

Check the result

Run the public examples again. They should report:

  • 8'ha5 -> 8'h05
  • 8'hf0 -> 8'h00
  • 8'h3c -> 8'h0c

Explain why the corrected bit pattern fixes the mistake. A sized mask records both the number of bits and their pattern, while a fill literal takes its width from the surrounding type.

Progressive hints

Hint 1

Write the eight mask bits in two groups of four. Which group must be zero, and which group must be one?

Hint 2

Use an eight-bit hexadecimal literal so that the source states the mask width rather than relying on contextual filling.

Concept

A fill literal has no independent width. Context turns '1 into an all-ones value of the required size; here that is 8'hff, which cannot clear any input bit. Sized literals are preferable for fixed masks because their width and bit pattern are visible together.

Variation: keep three bits

After the required version works, temporarily preserve only the low three bits. For the visible 8'h3c input, predict 8'h04, derive the corresponding eight-bit mask, and confirm the result. Restore the low-four-bit mask before submitting.

Review this concept
Ready
Main.bsv