Instruction Decoder using Casez
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_0000setsinstr_typeto3'b000andis_validto1 - ADD:
16'b0001_????_????_????setsinstr_typeto3'b001andis_validto1 - SUB:
16'b0010_????_????_????setsinstr_typeto3'b010andis_validto1 - LOAD:
16'b0100_????_????_????setsinstr_typeto3'b011andis_validto1 - STORE:
16'b0101_????_????_????setsinstr_typeto3'b100andis_validto1 - JMP:
16'b10??_????_????_????setsinstr_typeto3'b101andis_validto1 - BEQ:
16'b110?_????_????_????setsinstr_typeto3'b110andis_validto1 - INVALID: Any other bit pattern sets
instr_typeto3'b111andis_validto0
| 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_validto0andinstr_typeto3'b111.
Topics
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