0
0
Verilogprogramming~20 mins

Three-block FSM coding style in Verilog - Practice Problems & Coding Challenges

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

Consider this three-block FSM code in Verilog. What will be the value of state after 3 clock cycles if reset is initially high and then goes low?

Verilog
module fsm(input clk, input reset, output reg [1:0] state);
  // State encoding
  localparam S0 = 2'b00, S1 = 2'b01, S2 = 2'b10;

  // State register block
  always @(posedge clk or posedge reset) begin
    if (reset)
      state <= S0;
    else
      state <= next_state;
  end

  // Next state logic block
  reg [1:0] next_state;
  always @(*) begin
    case(state)
      S0: next_state = S1;
      S1: next_state = S2;
      S2: next_state = S0;
      default: next_state = S0;
    endcase
  end

  // Output logic block (not used here)
endmodule
A2'b00
B2'b01
C2'b10
D2'b11
Attempts:
2 left
💡 Hint

Trace the state transitions starting from S0 on reset.

Predict Output
intermediate
2:00remaining
Output of FSM with output logic

Given this three-block FSM, what is the value of out_signal when the FSM is in state S1?

Verilog
module fsm(input clk, input reset, output reg out_signal);
  localparam S0 = 2'b00, S1 = 2'b01, S2 = 2'b10;
  reg [1:0] state, next_state;

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

  always @(*) begin
    case(state)
      S0: next_state = S1;
      S1: next_state = S2;
      S2: next_state = S0;
      default: next_state = S0;
    endcase
  end

  always @(*) begin
    case(state)
      S0: out_signal = 1'b0;
      S1: out_signal = 1'b1;
      S2: out_signal = 1'b0;
      default: out_signal = 1'b0;
    endcase
  end
endmodule
A1'b0
B1'b1
CHigh impedance (Z)
DUndefined (X)
Attempts:
2 left
💡 Hint

Check the output logic block for state S1.

🔧 Debug
advanced
2:00remaining
Identify the error in this three-block FSM code

What error will this three-block FSM code produce when compiled?

Verilog
module fsm(input clk, input reset, output reg [1:0] state);
  localparam S0 = 2'b00, S1 = 2'b01;
  reg [1:0] next_state;

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

  always @(*) begin
    case(state)
      S0: next_state = S1;
      S1: next_state = S0;
      default: next_state = S2;
    endcase
  end
endmodule
ATypeError: state and next_state size mismatch
BNo error, code compiles fine
CSyntaxError: 'S2' is not declared
DRuntime error: state stuck at S0
Attempts:
2 left
💡 Hint

Check the default case in the next state logic.

📝 Syntax
advanced
1:30remaining
Which option has correct sensitivity list for next state logic?

In a three-block FSM, the next state logic block should be sensitive to which signals?

Aalways @(posedge clk)
Balways @(state or input_signal)
Calways @(posedge clk or posedge reset)
Dalways @(*)
Attempts:
2 left
💡 Hint

Next state logic is combinational logic.

🚀 Application
expert
1:00remaining
Number of states in a three-block FSM

A three-block FSM uses a 3-bit state register. How many unique states can this FSM represent?

A8
B6
C3
D16
Attempts:
1 left
💡 Hint

Number of states = 2^(number of bits in state register).