A 3-state traffic light FSM (RED โ GREEN โ YELLOW) with timer-based transitions. Includes simulation, code coverage, synthesis, and OpenROAD physical design.
A 3-state FSM with a counter-based timer (state changes every 4 clock cycles)
module traffic_light (
input clk, rst,
output reg red, yellow, green
);
parameter S_RED=2'b00, S_GREEN=2'b01, S_YELLOW=2'b10;
reg [1:0] current_state, next_state;
reg [2:0] count;
// Sequential: state + timer
always @(posedge clk or posedge rst) begin
if (rst) begin
current_state <= S_RED; count <= 0;
end else if (count == 3) begin
count <= 0; current_state <= next_state;
end else
count <= count + 1;
end
// Next state: RED โ GREEN โ YELLOW โ RED
always @(*) case (current_state)
S_RED: next_state = S_GREEN;
S_GREEN: next_state = S_YELLOW;
S_YELLOW: next_state = S_RED;
default: next_state = S_RED;
endcase
// Output logic
always @(*) begin
{red, yellow, green} = 3'b000;
case (current_state)
S_RED: red = 1;
S_GREEN: green = 1;
S_YELLOW: yellow = 1;
endcase
end
endmodule
Simulate with Icarus Verilog, analyze code coverage with Covered
# Compile + Run
$ iverilog -o traffic_light traffic_light.v tb_traffic_light.v
$ vvp traffic_light
# Code Coverage
$ covered score -t traffic_light -v traffic_light.v -vcd traffic.vcd -o traffic.cdd
$ covered report traffic.cdd
# View waveforms
$ gtkwave traffic.vcd
| File | Origin | Purpose |
|---|---|---|
traffic_light.v |
โ๏ธ Written | RTL design |
tb_traffic_light.v |
โ๏ธ Written | Testbench with monitor |
traffic.vcd |
๐ค Generated | Waveform dump |
traffic.cdd |
๐ค Covered | Coverage database |
Map behavioral Verilog to Nangate45 standard cells
read_verilog traffic_light.v
synth -top traffic_light
dfflibmap -liberty Nangate45_typ.lib
abc -liberty Nangate45_typ.lib
opt_clean -purge
tee -o area_report.txt stat -liberty Nangate45_typ.lib
write_verilog traffic_synth.v
show -format dot -prefix traffic_synth
| Cell | Count | Role |
|---|---|---|
DFFR_X1 |
5 | D Flip-Flops with reset |
DFFS_X1 |
1 | D Flip-Flop with set |
INV_X1 |
7 | Inverters |
MUX2_X1 |
3 | 2-to-1 Multiplexers |
NAND2_X1 |
1 | 2-input NAND |
NOR2_X1 |
1 | 2-input NOR |
AND2_X1 |
1 | 2-input AND |
XOR2_X1 |
1 | 2-input XOR |
Floorplan โ Place โ CTS โ Route โ Layout
create_clock [get_ports clk] -name core_clock -period 10.0
set_input_delay 2.0 -clock core_clock [get_ports rst]
set_output_delay 2.0 -clock core_clock [get_ports red]
set_output_delay 2.0 -clock core_clock [get_ports yellow]
set_output_delay 2.0 -clock core_clock [get_ports green]
Nangate45.pdn.tcl and runs pdngen to build a proper power
distribution network before placement.$ openroad openroad_run.tcl
| File | Origin | Stage | Purpose |
|---|---|---|---|
traffic_light.v |
โ๏ธ Written | Design | 3-state FSM RTL |
tb_traffic_light.v |
โ๏ธ Written | Simulate | Testbench |
yosys_run.tcl |
โ๏ธ Written | Synthesize | Yosys script |
traffic_light.sdc |
โ๏ธ Written | PDA | Timing constraints |
openroad_run.tcl |
โ๏ธ Written | PDA | OpenROAD script |
traffic_synth.v |
๐ค Yosys | Synthesize | Gate-level netlist |
traffic_synth.png |
๐ค Yosys | Synthesize | Circuit schematic |
area_report.txt |
๐ค Yosys | Synthesize | Cell & area stats |
traffic.vcd |
๐ค iverilog | Simulate | Waveform dump |
traffic.cdd |
๐ค Covered | Coverage | Coverage database |
# 1. Simulate
$ iverilog -o traffic_light traffic_light.v tb_traffic_light.v && vvp traffic_light
# 2. Coverage
$ covered score -t traffic_light -v traffic_light.v -vcd traffic.vcd -o traffic.cdd
# 3. Synthesize
$ yosys -s yosys_run.tcl
# 4. Physical Design
$ openroad openroad_run.tcl