Write a readable FSM with an enumerated type and separate decisions from data calculations.
A synchronous FSM combines a state register, next-state logic, and control outputs. An enumeration and specialized blocks make those roles easy to locate.
// Chosen policy when state_q matches no state: recover to IDLE.
state_d = IDLE;
o_load = 1'b0;
o_busy = 1'b0;
o_done = 1'b0;
unique case (state_q)
IDLE: begin
state_d = IDLE;
if (i_start) state_d = LOAD;
end
LOAD: begin
o_load = 1'b1;
state_d = RUN;
end
RUN: begin
state_d = RUN;
o_busy = 1'b1;
if (i_last) state_d = DONE;
end
DONE: begin
o_done = 1'b1;
state_d = IDLE;
end
endcase
end
endmodule
i_start leaves IDLE. LOAD lasts one cycle, RUN waits for i_last, and DONE asserts completion for one cycle. always_ff stores state; always_comb chooses the next state and commands.
Cover paths without latches
The block first assigns every output and state_d. States that can persist explicitly assign their hold value. No combinational branch should request implicit storage.
The state_d = IDLE fallback defines a simulation response for an unmatched state. Safe physical recovery needs more: optimization and recoding can change that behavior. Check safe-FSM options and the netlist when recovery is required.
Treat unique as a promise
unique case promises one matching branch. Without default, the simulator can report no match or multiple matches. A default suppresses the no-match diagnostic by covering remaining values.
Synthesis can use the promise for optimization. Apply unique only when the contract justifies it. An uncovered value must not be an overlooked normal condition.
Keep commands readable
The FSM produces commands such as load, clear, enable, or select. Data registers, counters, and operators form the datapath.
This separation lets you review transitions independently of calculations. It also helps locate a slow path without mixing long arithmetic with control decisions.