Localparam Protection
Reusable IP blocks often expose configuration options through parameters. However, exposing internal state machine encodings as parameters allows upper level modules to accidentally or maliciously override them, breaking the design. Internal constants must be protected using local parameters.
The state_decoder module computes the next state and status output for a system controller. It evaluates the current_state, enable, and restart inputs to determine the next_state. It also drives a parameterized status_bus based strictly on the current_state. This is a purely combinational module with no clock or asynchronous reset.
The state machine uses the following encodings: • IDLE = 3'b000 • ACTIVE = 3'b001 • DRAIN = 3'b010 • FAULT = 3'b011 • RECOVER = 3'b100 • HALT = 3'b101
The next_state is computed using these rules: • If restart is 1, the next state is always IDLE. • If enable is 1: • IDLE transitions to ACTIVE • ACTIVE transitions to DRAIN • DRAIN transitions to FAULT • FAULT transitions to HALT • RECOVER transitions to ACTIVE • HALT remains in HALT • If enable is 0: • IDLE remains in IDLE • ACTIVE transitions to IDLE • DRAIN transitions to RECOVER • FAULT transitions to RECOVER • RECOVER transitions to IDLE • HALT remains in HALT • Any invalid state (3'b110 or 3'b111) transitions to IDLE regardless of enable.
The status_bus is computed using these rules: • IDLE outputs 0 • ACTIVE outputs 1 • DRAIN outputs 2 • FAULT outputs 3 • RECOVER outputs 4 • HALT outputs all 1s • Any invalid state outputs all 1s
| Signal | Direction | Width | Description | |--------|-----------|-------|-------------| | current_state | input | 3 | Current state encoding | | enable | input | 1 | Advances the state progression when high | | restart | input | 1 | Forces the next state to IDLE when high | | next_state | output | 3 | Computed next state encoding | | status_bus | output | DATA_WIDTH | Status code for the current state |
Constraints
- You must declare
DATA_WIDTHas the onlyparameterin the module port list with a default value of 8. - You must declare all state encodings as
localparamwithin the module body. - The
restartinput has priority over theenableinput. - All outputs must be purely combinational.
- Invalid states must route to
IDLEon the next cycle and output all 1s on the status bus.
Topics
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.
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.