Skip to content
code0.HDL

02Verification

Merge bytes through a mask

Problem

Intended behavior. byte_merge builds merged from two bytes. Every bit where mask is 1 comes from new_byte; every bit where mask is 0 comes from old_byte. A mask of 8'hF0 therefore replaces the upper four bits and keeps the lower four.

Current behavior. The starter compiles and the first public row looks right, but the other two rows do not. With mask = f0 the new upper bits never appear, and with mask = ff only the low digit of new_byte reaches the output.

Your task. Find the declaration that makes the module lose data and correct it. Keep the ports, the three assign statements, and the testbench unchanged.

Diagnose

Read the module top to bottom and follow one value through it. kept holds the bits of old_byte that survive; taken holds the bits of new_byte that the mask selects. Ask how many bits each intermediate wire can actually carry, and what happens to the remaining bits of an 8-bit expression assigned into it.

Before running, predict all three rows by hand using the declared widths exactly as written. Your prediction should reproduce the wrong output; if it does not, check the width of every name on the path from new_byte to merged.

Once your hand calculation reproduces the wrong rows, make the smallest declaration change that stops the loss, run again, and confirm that all three rows match the bitwise definition. Then explain why no assign needed to change: the expressions were right, but one name was too narrow to carry their result.

Completion check

  • merged combines the two inputs correctly for every mask, including masks that select only upper bits, only lower bits, all bits, or none.
  • The result agrees with the bitwise definition for many byte patterns, not only the three printed ones.
  • The structure stays the same: two intermediate wires and one combining assign.

Progressive hints

Hint 1

All three printed rows agree whenever the mask selects only bits 3 to 0. Look for something that can hold only those bits.

Hint 2

A wire stores nothing, but its declared range still fixes its width. Assigning a wider value to it silently drops the upper bits.

Hint 3

Compare the ranges in the two intermediate wire declarations with the 8-bit values assigned into them.

Transfer experiment

Suppose kept had been the 4-bit wire instead. Predict which of the three rows would go wrong. In that version the upper half of merged comes only from taken, so wherever the mask is 0 in the upper half, merged reads 0 instead of the old bits. Then explain why neither version can be repaired at merged: the bits are already gone before the final assign combines the two wires.

Review this concept
Ready
Main.v