Traffic Light Controller
Traffic Light Controller
1. States: The system has six states (s0 to s5) for different
traffic light combinations (Green, Yellow, Red) for EW and NS.
Design Code:
module tlc(
input clk, reset, x,
output reg [1:0] EW, NS
);
parameter s0 = 3'b000, s1 = 3'b001, s2 = 3'b010,
s3 = 3'b011, s4 = 3'b100, s5 = 3'b101;
parameter red = 2'b00, yellow = 2'b01, green = 2'b10;
reg [2:0] state, next_state;
s1: begin
EW = yellow;
NS = red;
end
s2: begin
EW = red;
NS = red;
end
s3: begin
EW = red;
NS = green;
end
s4: begin
EW = red;
NS = yellow;
end
s5: begin
EW = red;
NS = red;
end
endcase
end
endmodule
TestBench Code:
module tlc_tb( );
reg clk;
reg reset;
reg x;
wire [1: 0] EW;
wire [1: 0] NS;
tlc uut (
.clk (clk) ,
.reset (reset),
.x (x),
.EW (EW),
.NS (NS)
);
always #10 clk = ~clk;
task apply_reset;
begin
reset = 1;
#40;
reset = 0;
end
endtask
initial begin
clk=0;
reset = 0;
x= 0;
apply_reset;
$display("Test case 1: No traffic on NS road(x = 0)");
x = 0;
#100;
$display("Test case 2: Traffic detected on NS road(x = 1)") ;
x = 1;
#100;
$display("Test case 3: Traffic stops on NS road (x = 0)");
x= 0;
#100;
$display("Simulation complete.");
$stop;
end
initial begin
$monitor ("Time: %t | State: EW=tb NS=tb | Sensor
x=tb",$time, EW, NS, x);
end
endmodule
Output:
# Time: 0 | State: EW=tb NS=tb | Sensor x=tb200
# Test case 1: No traffic on NS road (x = 0)
run
# Test case 2: Traffic detected on NS road (x = 1)
# Time: 140 | State: EW=tb NS=tb | Sensor x=tb201
# Time: 150 | State: EW=tb NS=tb | Sensor x=tb101
# Time: 170 | State: EW=tb NS=tb | Sensor x=tb001
# Time: 190 | State: EW=tb NS=tb | Sensor x=tb021
run
# Test case 3: Traffic stops on NS road (x = 0)
# Time: 240 | State: EW=tb NS=tb | Sensor x=tb020
# Time: 250 | State: EW=tb NS=tb | Sensor x=tb010
# Time: 270 | State: EW=tb NS=tb | Sensor x=tb000
# Time: 290 | State: EW=tb NS=tb | Sensor x=tb200
run
# Simulation complete.