0
0
Verilogprogramming~20 mins

FSM with output logic in Verilog - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FSM Output Logic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a Moore FSM with two states

What is the output z after the FSM processes the input sequence 1, 0, 1?

The FSM has two states: S0 (output 0) and S1 (output 1). It starts in S0. On input 1, it moves from S0 to S1. On input 0, it moves from S1 to S0. Output depends only on the current state.

Verilog
module fsm_moore(input clk, input rst, input x, output reg z);
  typedef enum logic {S0, S1} state_t;
  state_t state, next_state;

  always_ff @(posedge clk or posedge rst) begin
    if (rst) state <= S0;
    else state <= next_state;
  end

  always_comb begin
    case(state)
      S0: next_state = (x) ? S1 : S0;
      S1: next_state = (x) ? S1 : S0;
      default: next_state = S0;
    endcase
  end

  always_comb begin
    case(state)
      S0: z = 0;
      S1: z = 1;
      default: z = 0;
    endcase
  end
endmodule
A[1, 1, 1]
B[0, 0, 1]
C[0, 1, 1]
D[0, 1, 0]
Attempts:
2 left
💡 Hint

Remember, output depends only on the current state before input changes.

Predict Output
intermediate
2:00remaining
Output of a Mealy FSM with output on transitions

What is the output z sequence for input 1, 0, 1 on this Mealy FSM?

The FSM has states S0 and S1. It starts in S0. Output z depends on current state and input.

Verilog
module fsm_mealy(input clk, input rst, input x, output reg z);
  typedef enum logic {S0, S1} state_t;
  state_t state, next_state;

  always_ff @(posedge clk or posedge rst) begin
    if (rst) state <= S0;
    else state <= next_state;
  end

  always_comb begin
    case(state)
      S0: begin
        z = x;
        next_state = (x) ? S1 : S0;
      end
      S1: begin
        z = ~x;
        next_state = (x) ? S1 : S0;
      end
      default: begin
        z = 0;
        next_state = S0;
      end
    endcase
  end
endmodule
A[1, 1, 1]
B[0, 1, 0]
C[1, 0, 1]
D[0, 0, 1]
Attempts:
2 left
💡 Hint

Output depends on current state and input at the same clock.

🔧 Debug
advanced
2:00remaining
Identify the error in FSM output logic

What error will this FSM code cause when synthesized?

Verilog
module fsm_bug(input clk, input rst, input x, output reg z);
  typedef enum logic {IDLE, BUSY} state_t;
  state_t state, next_state;

  always_ff @(posedge clk or posedge rst) begin
    if (rst) state <= IDLE;
    else state <= next_state;
  end

  always_comb begin
    case(state)
      IDLE: begin
        if (x) next_state = BUSY;
        else next_state = IDLE;
      end
      BUSY: begin
        if (!x) next_state = IDLE;
        else next_state = BUSY;
      end
      default: next_state = IDLE;
    endcase
  end

  always_comb begin
    case(state)
      IDLE: z = 0;
      BUSY: z = 1;
      default: z = 0;
    endcase
  end
endmodule
AOutput z is not assigned in all cases causing latch
Bnext_state may be inferred as latch causing synthesis error
CState register is not reset properly causing undefined state
DSyntax error due to missing semicolon
Attempts:
2 left
💡 Hint

Check if next_state is assigned in every branch.

📝 Syntax
advanced
2:00remaining
Syntax error in FSM output logic block

Which option contains the correct syntax for the output logic in a Moore FSM?

Verilog
always_comb begin
  case(state)
    S0: z = 0
    S1: z = 1;
  endcase
end
Aalways_comb begin\n case(state)\n S0: z = 0;\n S1: z = 1;\n endcase\nend
Balways_comb begin\n case state\n S0: z = 0;\n S1: z = 1;\n endcase\nend
Calways_comb begin\n case(state)\n S0: z = 0;\n S1: z = 1\n endcase\nend
D
always_comb begin\n  case(state)\n    S0: z = 0;\n    S1: z = 1;\n  endcase\nendcase
end
Attempts:
2 left
💡 Hint

Check for missing semicolons and correct case syntax.

🚀 Application
expert
2:00remaining
Number of states in a FSM with output logic

A FSM has 3 inputs and 2 outputs. The FSM is Moore type with output depending only on state. How many states are needed to implement all possible output combinations?

A16
B8
C4
D64
Attempts:
2 left
💡 Hint

Think about how many unique output patterns are possible with 2 outputs.