Skip to content
code0.HDL

31Verification

Complete a parity frame

Problem

encodeFrame should place ready, a three-bit code, and a three-bit count in bits 7 through 1, then derive bit 0 from all seven body bits. The starter keeps that low bit at zero. It therefore appears correct for even-parity bodies but produces d6, rather than the annotated d7, for the sample in mkTop.

Only the function body is yours to repair. Preserve its arguments, result type, and field layout, and leave the supplied mkTop unchanged.

Read, predict, and make the smallest repair

  1. Read the type of body and account for its fields from left to right: pack(ready), code, then count.
  2. Before running, trace the sample body 1_101_011. Count its set bits and predict both the current low bit and the required parity bit.
  3. Run the starter and use the displayed byte to confirm that bits 7:1 are already correct. This localizes the defect to the final one-bit operand of the returned concatenation.
  4. Make one expression-level edit. Do not rearrange the body or duplicate its field construction; derive parity from the complete typed body.
  5. Run again and explain why the result is eight bits wide without relying on the sample's hexadecimal value.

Observable completion contract

  • Bit 7 is pack(ready), bits 6:4 are code, and bits 3:1 are count for every input combination.
  • Bit 0 is one exactly when the seven-bit body has an odd number of set bits.
  • An all-zero body yields a zero parity bit; a body with one set bit yields one; toggling any single body bit toggles parity.
  • The sample Run displays the expected frame, but the implementation must not special-case that sample.

Progressive hints

Hint 1 — identify the contract

Parity is a property of all seven body bits, not of one field and not of the partially constructed eight-bit frame.

Hint 2 — choose the result shape

The missing operand must produce exactly one bit so that concatenating it with Bit#(7) still satisfies the Bit#(8) return type.

Hint 3 — choose the BSV tool

Unary reduction XOR consumes one vector and returns one bit. Apply that tool to the existing local value, then keep that result in the low position.

Transfer experiment

After completing the task, use a scratch copy to define a layout with parity in bit 7 and the seven body bits in bits 6:0. Predict which operand order and decoder selections must change while the parity computation remains the same. Restore the graded layout before submitting.

Review this concept
Ready
Main.bsv