FSM with output logic in Verilog - Time & Space Complexity
We want to understand how the time it takes to run a finite state machine (FSM) with output logic changes as the input size or clock cycles increase.
Specifically, we ask: how does the number of operations grow as the FSM processes more inputs?
Analyze the time complexity of the following code snippet.
module fsm_with_output(
input clk, reset, in,
output reg out
);
typedef enum logic [1:0] {S0, S1, S2} state_t;
state_t state, next_state;
always_ff @(posedge clk or posedge reset) begin
if (reset) state <= S0;
else state <= next_state;
end
always_comb begin
case(state)
S0: begin
out = 0;
next_state = in ? S1 : S0;
end
S1: begin
out = 1;
next_state = in ? S2 : S0;
end
S2: begin
out = 0;
next_state = in ? S2 : S0;
end
endcase
end
endmodule
This code models a simple FSM with three states and output logic based on the current state and input.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The FSM updates its state on every clock cycle.
- How many times: Once per clock cycle, repeating indefinitely as inputs arrive.
Each clock cycle causes the FSM to evaluate its current state and input, then update its state and output.
| Input Size (clock cycles) | Approx. Operations |
|---|---|
| 10 | 10 state updates and output calculations |
| 100 | 100 state updates and output calculations |
| 1000 | 1000 state updates and output calculations |
Pattern observation: The number of operations grows linearly with the number of clock cycles processed.
Time Complexity: O(n)
This means the time to process inputs grows directly in proportion to the number of clock cycles.
[X] Wrong: "The FSM processes all inputs instantly regardless of input length."
[OK] Correct: Each input requires a clock cycle to update the state and output, so processing time grows with input length.
Understanding how FSMs scale with input size helps you design efficient hardware and explain timing behavior clearly in interviews.
"What if the FSM had nested loops inside the output logic? How would the time complexity change?"
Practice
Solution
Step 1: Understand FSM output types
In FSMs, Moore outputs depend only on the current state, while Mealy outputs depend on state and input.Step 2: Identify output logic role
The output logic in a Moore machine produces outputs based solely on the current state stored in the state register.Final Answer:
Output logic in a Moore machine -> Option DQuick Check:
Moore output = state only [OK]
- Confusing next state logic with output logic
- Thinking outputs depend on inputs in Moore FSM
- Mixing state register role with output logic
state with 2 bits?Solution
Step 1: Recall Verilog register declaration
Registers are declared withregkeyword followed by bit width in brackets, e.g.,reg [1:0]for 2 bits.Step 2: Check each option
reg [1:0] state; correctly declares a 2-bit register namedstate. wire [2:0] state; useswireand wrong width. reg state[1:0]; uses incorrect syntax for bit width. state reg [1:0]; has wrong keyword order.Final Answer:
reg [1:0] state; -> Option AQuick Check:
reg [1:0] state; is correct syntax [OK]
- Using wire instead of reg for state registers
- Incorrect bit width syntax like reg name[1:0]
- Wrong keyword order like state reg
always @(state) begin
case(state)
2'b00: out = 1'b0;
2'b01: out = 1'b1;
2'b10: out = 1'b0;
default: out = 1'b0;
endcase
endWhat is the output
out when state is 2'b01?Solution
Step 1: Check case for state 2'b01
The case statement setsout = 1'b1whenstateis2'b01.Step 2: Confirm output value
Since the state matches 2'b01, outputoutis assigned 1'b1.Final Answer:
1'b1 -> Option CQuick Check:
State 01 -> out = 1 [OK]
- Confusing output with state value
- Assuming default applies instead of matching case
- Misreading bit values as decimal
always @(posedge clk) begin
case(state)
2'b00: out = 1'b0;
2'b01: out = 1'b1;
default: out = 1'b0;
endcase
endWhat is the main problem?
Solution
Step 1: Understand output logic timing
Moore FSM output logic is usually combinational and depends only on state, so it should be in an always block sensitive to state changes, not clock edges.Step 2: Analyze given code
The code usesalways @(posedge clk), making output synchronous, which is incorrect for pure output logic in Moore FSM.Final Answer:
Output logic should be in a combinational always block, not clocked -> Option AQuick Check:
Output logic = combinational, not clocked [OK]
- Using clocked always block for output logic
- Ignoring sensitivity list correctness
- Confusing output and state update blocks
out = 1 only when the current state is 2'b10 and input in is high. Which Verilog code snippet correctly implements this output logic?Solution
Step 1: Understand Mealy output logic
Mealy outputs depend on both current state and inputs, so sensitivity list must include bothstateandin.Step 2: Check each option
always @(state or in) begin\n if (state == 2'b10 && in)\n out = 1'b1;\n else\n out = 1'b0;\nend correctly usesalways @(state or in)and setsoutbased on both conditions. always @(state) begin\n if (state == 2'b10)\n out = 1'b1;\n else\n out = 1'b0;\nend ignores inputin. always @(posedge clk) begin\n if (state == 2'b10 && in)\n out <= 1'b1;\n else\n out <= 1'b0;\nend uses clocked block which is not typical for Mealy output logic. always @(in) begin\n if (in)\n out = 1'b1;\n else\n out = 1'b0;\nend ignores state.Final Answer:
always @(state or in) begin if (state == 2'b10 && in) out = 1'b1; else out = 1'b0; end -> Option BQuick Check:
Mealy output depends on state and input [OK]
- Leaving input out of sensitivity list
- Using clocked always block for Mealy output
- Ignoring state condition in output logic
Ready to prove it?
Pass the test and this pattern moves unknown
