The Default Assignment Trick
An output decoder with many signals and many branches will leave something unassigned somewhere, and that becomes a latch. Assigning every output a default value at the top of the always_comb block rules this out with one line per signal.
The solution module is a memory controller command decoder. It evaluates a 3-bit state and a 2-bit opcode to drive four combinational control signals: mem_read, mem_write, addr_update, and ready.
By default, all control signals must be 0. The outputs change from their default values only under the following specific conditions:
- During the IDLE state (
3'b000) and the HALT state (3'b100), thereadysignal is asserted to1. - During the FETCH state (
3'b001), bothmem_readandaddr_updateare asserted to1. - During the EXECUTE state (
3'b010),mem_writeis asserted to1if theopcodeis2'b10. - During the EXECUTE state (
3'b010),mem_readis asserted to1if theopcodeis2'b01.
All other unmapped states and opcode combinations must output the default values.
| Signal | Direction | Width | Description |
|---|---|---|---|
| state | input | 3 | Current state of the controller |
| opcode | input | 2 | Instruction opcode |
| mem_read | output | 1 | Memory read enable |
| mem_write | output | 1 | Memory write enable |
| addr_update | output | 1 | Address register update enable |
| ready | output | 1 | Controller ready flag |
Constraints
- The design must be purely combinational.
- No latches may be inferred.
- All unmapped states and opcodes must safely default to all zeros.
- Do not use sequential logic (
always @(posedge clk)); usealways_comboralways @(*).
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.
Free to solve. A Codiode account keeps your progress.
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 FSM Coding
Problem 2 of 15 in the FSM Coding track.
Related problems
Browse all problems · Verilog practice problems · Learning tracks