Codiode

Instruction Decoder using Casez

MediumVerilog / SystemVerilogBuild1 solved

An opcode decoder only cares about some bits of the instruction; the rest are register numbers and immediates. casez lets those bits be written as ? instead of listing every combination.

The module evaluates a 16-bit instruction word and classifies it into one of eight categories using a 3-bit output instr_type. It also asserts an is_valid signal for recognized instructions. The instruction set uses variable-length opcodes. The number of bits that define the instruction type changes depending on the instruction class.

The instruction set architecture defines the following decode rules, where ? represents a bit that can be either 0 or 1:

  • NOP: 16'b0000_0000_0000_0000 sets instr_type to 3'b000 and is_valid to 1
  • ADD: 16'b0001_????_????_???? sets instr_type to 3'b001 and is_valid to 1
  • SUB: 16'b0010_????_????_???? sets instr_type to 3'b010 and is_valid to 1
  • LOAD: 16'b0100_????_????_???? sets instr_type to 3'b011 and is_valid to 1
  • STORE: 16'b0101_????_????_???? sets instr_type to 3'b100 and is_valid to 1
  • JMP: 16'b10??_????_????_???? sets instr_type to 3'b101 and is_valid to 1
  • BEQ: 16'b110?_????_????_???? sets instr_type to 3'b110 and is_valid to 1
  • INVALID: Any other bit pattern sets instr_type to 3'b111 and is_valid to 0
Signal Direction Width Description
instr input 16 The 16-bit instruction word to be decoded
instr_type output 3 The decoded instruction category
is_valid output 1 Asserted to 1 if the instruction matches a recognized pattern, 0 otherwise

Constraints

  • The design must be purely combinational with no clocks, latches, or reset logic.
  • All 65,536 possible input combinations must resolve to deterministic outputs.
  • Unrecognized instructions must explicitly set is_valid to 0 and instr_type to 3'b111.

Topics

CombinationalDecodeCasez

Solve this problem

Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.

This problem is part of Codiode Pro. The statement above is free to read.

The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.

Part of Combinational RTL

Problem 5 of 15 in the Combinational RTL track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.