Skip to content
code0.HDL

04Implementation

Diagnose four integer literals

Problem

literalConstant already compiles, but each selector returns the wrong sixteen-bit value. The four local bindings demonstrate different mistakes: one decimal value is incorrect, octal digits were copied without conversion, a short hexadecimal value was widened after information was omitted, and an ordinary one was used where every bit must be one.

The required behavior is:

SelectorRequired value
zero42
one42
two48879
threeall sixteen bits set

Edit only the four local right-hand sides. Keep the case and mkTop unchanged.

Diagnose before editing

Run the starter and treat the printed row as evidence, not an answer key. For each binding, separate three questions:

  1. What value do its digits denote in the stated base?
  2. How many bits belong to the literal before assignment?
  3. Does assignment or extension create missing information, or only preserve what is already present?

Calculate the decimal value of the octal binding by place value. Then inspect the hexadecimal binding from the inside out: zeroExtend can add leading zeroes, but it cannot recover high bits absent from its eight-bit argument. Finally compare a numeric value of one with a value that has one in every bit position.

Repair the value contract

Correct all four bindings so the selector table is satisfied. Use the binding names to practice decimal, octal, hexadecimal, and context-filled literal reasoning, but do not infer that compiled behavior records how a value was spelled. Equivalent expressions that return the same sixteen bits are not observably different.

After editing, run all four selector paths and explain why the first two now agree even though their source bases can differ. Also explain why the full hexadecimal value needs all of its significant bits and why the fill form gets its width from Bit#(16).

Completion check

  • Selectors zero and one both return the numeric value 42.
  • Selector two returns the numeric value 48879 without losing high bits.
  • Selector three returns one in every position of its sixteen-bit result.
  • No selector, return arm, or public runner structure changes.

Progressive hints

Hint 1

Read a sized literal as width, base, then digits. In base eight, each place is a power of eight; reusing decimal-looking digits does not preserve the decimal value.

Hint 2

Widening changes width, not history. Start with a hexadecimal literal wide enough to contain the complete target rather than extending an incomplete piece.

Hint 3

A concrete integer one sets only the least-significant bit. A fill literal has no independent width and repeats its fill bit to match the surrounding type.

Transfer experiment

In scratch code, change the destination context from sixteen bits to eight. Predict which requested numeric value no longer fits and which all-bits value adapts to the new context without adding an explicit width.

Review this concept
Ready
Main.bsv