CodiodeCodiode
Home
Problem Solving
Skill Tracks
My Assignments
Contests
Leaderboard
Community
Settings
Codiode/Problems/Combinational Logic

Instruction Decoder using Casez

MediumVerilog / SystemVerilogBuild

Microprocessors decode fetched instructions to determine the required datapath operations. This decoding step must quickly identify the instruction type by examining specific opcode bits while completely ignoring payload bits like register addresses, offsets, or immediate values. Standard Verilog case statements require explicit enumeration of every possible bit pattern, which is highly inefficient for variable-length opcodes.

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 a real testbench — you get the waveform back, not a stored answer.

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

Sign in to solveSee what Pro unlocks

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.

Related problems

  • Single Continuous AssignmentEasy
  • Basic Vector ConcatenationEasy
  • Ternary Operator MuxEasy
  • Underscores for ReadabilityEasy
  • Left Hand Side ConcatenationEasy
  • Inout Port MechanicsEasy
  • Explicit Binary LiteralsEasy
  • Vector Port EndiannessEasy

Browse all problems · Learning tracks