0
0
Verilogprogramming~30 mins

State diagram to Verilog translation - Mini Project: Build & Apply

Choose your learning style9 modes available
State diagram to Verilog translation
📖 Scenario: You are designing a simple traffic light controller using a state diagram. The traffic light has three states: Green, Yellow, and Red. Each state lasts for a fixed number of clock cycles. You want to translate this state diagram into Verilog code to control the traffic lights.
🎯 Goal: Build a Verilog module that implements the traffic light controller using a state machine. You will create the states, a counter for timing, and output signals for the lights.
📋 What You'll Learn
Create a Verilog module named traffic_light with inputs clk and reset.
Define three states: GREEN, YELLOW, and RED using localparam.
Use a register state to hold the current state.
Use a register counter to count clock cycles for timing each state.
Implement state transitions based on the counter reaching specific values.
Output signals green_light, yellow_light, and red_light that are high when the corresponding state is active.
💡 Why This Matters
🌍 Real World
Traffic light controllers are common examples of state machines used in embedded systems and digital design.
💼 Career
Understanding how to convert state diagrams into hardware description languages like Verilog is essential for FPGA and ASIC design engineers.
Progress0 / 4 steps
1
Create the module and define states
Write a Verilog module named traffic_light with inputs clk and reset. Inside the module, define three states using localparam: GREEN = 2'd0, YELLOW = 2'd1, and RED = 2'd2. Also declare a reg [1:0] state to hold the current state.
Verilog
Need a hint?

Remember to declare the module with inputs and use localparam to define states as 2-bit values.

2
Add a counter register for timing
Inside the traffic_light module, declare a 4-bit register named counter to count clock cycles for timing the states.
Verilog
Need a hint?

Use reg [3:0] counter; to declare a 4-bit register for counting.

3
Implement state transitions and counter logic
Add an always @(posedge clk or posedge reset) block inside the module. On reset, set state to GREEN and counter to 0. Otherwise, increment counter. When counter reaches 4 for GREEN, move to YELLOW and reset counter. When counter reaches 2 for YELLOW, move to RED and reset counter. When counter reaches 4 for RED, move back to GREEN and reset counter.
Verilog
Need a hint?

Use a synchronous reset and a case statement to handle state transitions and counter resets.

4
Add output signals for the traffic lights
Declare output registers green_light, yellow_light, and red_light. Inside an always @(*) block, set these outputs to 1 when the state matches their color, otherwise 0. Finally, add these outputs to the module's port list.
Verilog
Need a hint?

Use a combinational always @(*) block to set the output signals based on the current state.