Skip to content
code0.HDL

19Verification

Find the misplaced opcode

Problem

Intended behavior. OpcodeDecode reads a 7-bit instruction opcode. Five encodings are legal: load 0x03, op-imm 0x13, store 0x23, branch 0x63, and jal 0x6f. For a legal input valid is 1; every other pattern gives valid 0. memory is 1 for load and store, control is 1 for branch and jal, and both are 0 for an illegal input. bits returns the input bits unchanged.

Observed behavior. The starter compiles and most instructions decode correctly, but one legal instruction is reported as illegal.

Task. Find the declaration that disagrees with the instruction format and correct it. Keep the module name, its ports, and the use of Opcode.safe.

Run and compare

Run the starter. The public run prints the enum width and decodes the five legal encodings plus one illegal pattern, 0x05. Before reading any code, mark each printed line as right or wrong against the table above.

Diagnose

Opcode.safe sets its flag only when the input bits equal one of the values declared in Opcode. If a legal pattern is rejected, ask which number Chisel actually assigned to the matching name. Work through the declaration one line at a time and write down each value's number.

Then ask a second question: if one name holds the wrong number, is there some other input pattern that the decoder now accepts but should not?

Fix, run, and explain

Correct only the faulty entry, then run again. Every legal line must now match the table, and 0x05 must still be rejected. Explain which number the entry held before your fix, and which pattern the starter accepted in its place.

Completion check

  • Exactly the five listed encodings are valid, and all other 123 patterns are not.
  • memory and control are correct for every input, legal or not.
  • bits always equals the input.
  • The repair lives in the encoding table itself; a special case added to one output only hides the problem.

Progressive hints

Hint 1

Compare the printed lines with the specification. Only one legal opcode is rejected, so only one entry in the enum is suspect.

Hint 2

A Value without an argument does not leave a gap. Recall the rule for what number it takes after an explicit encoding.

Hint 3

When the bit pattern is an external contract, every entry that belongs to it needs its encoding written out.

Concept

Explicit encodings in a ChiselEnum model a format that someone else owns. A bare Value after an explicit one continues from the previous number, which is convenient for internal states and wrong for an opcode table. The mistake shows up twice: the intended pattern is rejected, and an unintended pattern one above the previous entry is accepted.

Extension experiment

Without running it, predict the decode of 0x24 for the starter and for your fixed design. Then predict what happens if the branch entry were moved below jal with its correct encoding.

Review this concept
Ready

CheerpJ runs Chisel in your browser. A cold Run downloads about 14.5 MB of Java and 47 MB of JARs. Compilation can take a minute. Licenses

Main.scala