Codiode

Traffic Light Controller with Timer

MediumVerilog / SystemVerilogBuild

If each second of a light's phase were its own state, the FSM would have hundreds of states. Keep the FSM to the light colours and let a separate timer count the seconds.

The module coordinates a standard three-color traffic light cycle: Red, Green, Yellow, and back to Red. It interfaces with an external timer module to control the duration of each color. When the FSM enters a new state, it asserts a start signal for exactly one clock cycle. It then holds its current color output until the external timer asserts its done signal, at which point the FSM advances to the next color and issues a new start signal.

The circuit is driven by a positive-edge triggered clock and an asynchronous active-low reset. Upon reset, the FSM must enter the RED state and immediately assert the timer start signal to kick off the initial countdown. If the timer done signal is asserted on the exact cycle the reset is released, it must be ignored until the next clock cycle.

Worked Trace

Cycle 1: rst_n=0 → state=RED, light_state=00, timer_start=1
Cycle 2: rst_n=1, timer_done=0 → state=RED, light_state=00, timer_start=0
Cycle 3: timer_done=1 → state=GREEN, light_state=01, timer_start=1
Cycle 4: timer_done=0 → state=GREEN, light_state=01, timer_start=0
Cycle 5: timer_done=1 → state=YELLOW, light_state=10, timer_start=1

Diagram

Port Table

Signal Direction Width Description
clk input 1 Positive-edge triggered clock
rst_n input 1 Asynchronous active-low reset; resets state to RED and asserts timer_start
timer_done input 1 Asserted by external timer when the wait period completes
timer_start output 1 Asserts for exactly one cycle when entering a new state or upon reset
light_state output 2 Current traffic light color (00=RED, 01=GREEN, 10=YELLOW)

Constraints

  • Clock is positive-edge triggered.
  • Reset is asynchronous and active-low.
  • Output light_state must be 2'b00 (RED) on reset.
  • Output timer_start must be 1'b1 when reset is active, and for exactly one cycle after entering any new state.
  • State transitions must occur exactly when timer_done is 1'b1.
  • If timer_done is asserted during reset, it must be ignored until reset is released.
  • State encoding must strictly follow: RED = 2'b00, GREEN = 2'b01, YELLOW = 2'b10.

Topics

FSMSequential LogicState MachineControl Logic

Solve this problem

Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.

This problem is part of Codiode Pro. The statement above is free to read.

The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.

Part of FSM Coding

Problem 9 of 15 in the FSM Coding track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.