0
0
Verilogprogramming~20 mins

Traffic light controller FSM in Verilog - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Traffic Light 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 traffic light FSM
What is the output of the traffic light signals after the first clock cycle in this FSM code?
Verilog
module traffic_light_fsm(input clk, input reset, output reg [1:0] light);
  typedef enum reg [1:0] {RED=2'b00, GREEN=2'b01, YELLOW=2'b10} state_t;
  state_t state, next_state;

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

  always @(*) begin
    case(state)
      RED: next_state = GREEN;
      GREEN: next_state = YELLOW;
      YELLOW: next_state = RED;
      default: next_state = RED;
    endcase
  end

  always @(*) begin
    light = state;
  end
endmodule
Alight = 2'b00 (RED)
Blight = 2'b01 (GREEN)
Clight = 2'b10 (YELLOW)
Dlight = 2'b11 (undefined)
Attempts:
2 left
💡 Hint
Remember the FSM starts at RED state on reset.
🧠 Conceptual
intermediate
1:30remaining
Understanding state transitions in traffic light FSM
In the traffic light FSM, what is the next state after the GREEN state?
ARED
BYELLOW
CGREEN
DOFF
Attempts:
2 left
💡 Hint
Check the case statement for the GREEN state.
🔧 Debug
advanced
2:30remaining
Identify the error in this traffic light FSM code
What error will this Verilog code produce when simulating the traffic light FSM?
Verilog
module traffic_light_fsm(input clk, input reset, output reg [1:0] light);
  typedef enum reg [1:0] {RED=2'b00, GREEN=2'b01, YELLOW=2'b10} state_t;
  state_t state, next_state;

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

  always @(*) begin
    case(state)
      RED: next_state = GREEN;
      GREEN: next_state = YELLOW;
      YELLOW: next_state = RED;
      default: next_state = RED;
    endcase
  end

  always @(*) begin
    light = next_state;
  end
endmodule
Alight output is always zero
BSyntax error: missing semicolon after typedef
CRuntime error: state never updates
Dlight output shows the next state, not current state
Attempts:
2 left
💡 Hint
Check which state variable is assigned to light output.
📝 Syntax
advanced
1:30remaining
Find the syntax error in this traffic light FSM snippet
Which option correctly fixes the syntax error in this code snippet?
Verilog
always @(posedge clk or posedge reset) begin
  if reset
    state <= RED;
  else
    state <= next_state;
end
Aif (reset) { state <= RED; }
Bif reset: state <= RED;
Cif (reset) state <= RED;
Dif reset then state <= RED;
Attempts:
2 left
💡 Hint
Verilog requires parentheses around conditions in if statements.
🚀 Application
expert
2:00remaining
Number of states in a traffic light FSM with pedestrian crossing
A traffic light FSM has states: RED, GREEN, YELLOW, and a PEDESTRIAN_WAIT state. How many unique states does this FSM have?
A4
B6
C3
D5
Attempts:
2 left
💡 Hint
Count all named states including pedestrian wait.