Turn a state diagram into readable, complete RTL that is easy to verify.
A state machine remembers the current step of an exchange. It is useful when the next action depends on both an input and what has already happened.
Request an action and wait for acknowledgement
This controller waits for , holds until , then asserts for one cycle:
i_start
o_request
i_ack
o_done
module request_controller ( input wire i_clk, input wire i_reset, input wire i_start, input wire i_ack, output reg o_request, output reg o_done); localparam STATE_IDLE = 2'b00; localparam STATE_WAIT = 2'b01; localparam STATE_DONE = 2'b10; reg [1:0] state_reg; reg [1:0] state_next; always @(posedge i_clk) begin if (i_reset) state_reg <= STATE_IDLE; else state_reg <= state_next; end always @* begin state_next = state_reg; o_request = 1'b0; o_done = 1'b0; case (state_reg) STATE_IDLE: begin if (i_start) state_next = STATE_WAIT; end STATE_WAIT: begin o_request = 1'b1; if (i_ack) state_next = STATE_DONE; end STATE_DONE: begin o_done = 1'b1; state_next = STATE_IDLE; end default: begin state_next = STATE_IDLE; end endcase endendmodule
The clocked block stores state_reg. The combinational block calculates state_next and outputs. Default assignments cover every path and prevent latches.
STATE_DONE always returns to STATE_IDLE at the next edge. A continuously asserted i_start starts another request only after the controller has returned to idle.
default defines the RTL response to an unexpected encoding. It does not by itself guarantee physical recovery from state corruption: synthesis can recode or optimize the machine. Safety requirements need an appropriate implementation and verification.
Decide when outputs may change
Moore outputs depend on state. Mealy outputs also depend on inputs, so a response may travel through a combinational path during the current cycle.
This controller has Moore outputs. Their combinational decoding can still produce physical transients. Register an output when the interface requires glitch-free behavior, and account for the added latency.
Encoding, counters, and external inputs
Binary encoding uses fewer flip-flops. One-hot encoding may simplify decoding at the cost of more flip-flops. Results depend on the FPGA and timing constraints; inspect synthesis reports before choosing.
For a multi-cycle state, initialize its counter on entry and define the terminal count precisely. Comparing against LIMIT and LIMIT - 1 gives different durations.
A button or another clock domain needs CDC handling. Transfer buses coherently through a handshake or asynchronous FIFO instead of synchronizing each bit independently.
Check the sequence
Check reset in every phase, a held start request, immediate and delayed acknowledgements, and the exact length of o_done. Waveforms should match the interface's acceptance rule.