0% found this document useful (0 votes)
90 views5 pages

Verilog 2022 ASSN2 Solution

The document describes designing a traffic signal controller using a finite state machine approach in Verilog. It specifies that the main highway signal remains green by default, but the country road signal turns green when cars are detected on that road. When the country road clears, its signal turns yellow and red to return the main highway to green. The design implements this behavior using a state machine with controllable delays between states. Source code and a stimulus testbench are provided.

Uploaded by

Sai Harshitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views5 pages

Verilog 2022 ASSN2 Solution

The document describes designing a traffic signal controller using a finite state machine approach in Verilog. It specifies that the main highway signal remains green by default, but the country road signal turns green when cars are detected on that road. When the country road clears, its signal turns yellow and red to return the main highway to green. The design implements this behavior using a state machine with controllable delays between states. Source code and a stimulus testbench are provided.

Uploaded by

Sai Harshitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

ASSIGNMENT -2

Design a Traffic Signal Controller using a finite state machine approach. Write the Verilog
code and also write the simulation waveform. Consider the following specifications.

•The traffic signal for the main highway gets highest priority because cars are continuously
present on the main highway. Thus, the main highway signal remains green by default.

• Occasionally, cars from the country road arrive at the traffic signal. The traffic signal for the
country road must turn green only long enough to let the cars on the country road go.

• As soon as there are no cars on the country road, the country road traffic signal turns yellow
and then red and the traffic signal on the main highway turns green again.

• There is a sensor to detect cars waiting on the country road. The sensor sends a signal X as
input to the controller. X = 1 if there are cars on the country road; otherwise, X= 0.

• There are delays on transitions from S1 to S2, from S2 to S3, and from S4 to S0. The delays
must be controllable.

Solution:
Source code
`define TRUE 1'b1
`define FALSE 1'b0
//Delays
`define Y2RDELAY 3 //Yellow to red delay
`define R2GDELAY 2 //Red to green delay
module sig_control
(hwy, cntry, X, clock, clear);

//I/O ports
output [1:0] hwy, cntry;

//2-bit output for 3 states of signal


//GREEN, YELLOW, RED;
reg [1:0] hwy, cntry;
//declared output signals are registers
input X;
//if TRUE, indicates that there is car on
//the country road, otherwise FALSE
input clock, clear;
parameter RED = 2'd0,
YELLOW = 2'd1,
GREEN = 2'd2;
//State definition HWY CNTRY
parameter S0 = 3'd0, //GREEN RED
S1 = 3'd1, //YELLOW RED
S2 = 3'd2, //RED RED
S3 = 3'd3, //RED GREEN
S4 = 3'd4; //RED YELLOW
//Internal state variables
reg [2:0] state;
reg [2:0] next_state;
//state changes only at positive edge of clock
always @(posedge clock)
if (clear)
state <= S0; //Controller starts in S0 state
else
state <= next_state; //State change
//Compute values of main signal and country signal
always @(state)
begin
hwy = GREEN; //Default Light Assignment for Highway light
cntry = RED; //Default Light Assignment for Country light
case(state)
S0: ; // No change, use default
S1: hwy = YELLOW;
S2: hwy = RED;
S3: begin
hwy = RED;
cntry = GREEN;
end
S4: begin
hwy = RED;
cntry = YELLOW;
end
endcase
end
//State machine using case statements
always @(state or X)
begin
case (state)
S0: if(X)
next_state = S1;
else
next_state = S0;
S1: begin //delay some positive edges of clock
repeat(`Y2RDELAY) @(posedge clock) ;
next_state = S2;
end
S2: begin //delay some positive edges of clock
repeat(`R2GDELAY) @(posedge clock);
next_state = S3;
end
S3: if(X)
next_state = S3;
else
next_state = S4;
S4: begin //delay some positive edges of clock
repeat(`Y2RDELAY) @(posedge clock) ;
next_state = S0;
end
default: next_state = S0;
endcase
end
endmodule

Stimulus for Traffic Signal Controller


//Stimulus Module
module stimulus;
wire [1:0] MAIN_SIG, CNTRY_SIG;
reg CAR_ON_CNTRY_RD;
//if TRUE, indicates that there is car on
//the country road
reg CLOCK, CLEAR;
//Instantiate signal controller
sig_control SC(MAIN_SIG, CNTRY_SIG, CAR_ON_CNTRY_RD, CLOCK, CLEAR);
//Set up monitor
initial
$monitor($time, " Main Sig = %b Country Sig = %b Car_on_cntry = %b", MAIN_SIG, CNTRY_SIG,
CAR_ON_CNTRY_RD);
//Set up clock
initial
begin
CLOCK = `FALSE;
forever #5 CLOCK = ~CLOCK;
end
//control clear signal
initial
begin
CLEAR = `TRUE;
repeat (5) @(negedge CLOCK);
CLEAR = `FALSE;
end
//apply stimulus
initial
begin
CAR_ON_CNTRY_RD = `FALSE;
repeat(20)@(negedge CLOCK); CAR_ON_CNTRY_RD = `TRUE;
repeat(10)@(negedge CLOCK); CAR_ON_CNTRY_RD = `FALSE;
repeat(20)@(negedge CLOCK); CAR_ON_CNTRY_RD = `TRUE;
repeat(10)@(negedge CLOCK); CAR_ON_CNTRY_RD = `FALSE;
repeat(20)@(negedge CLOCK); CAR_ON_CNTRY_RD = `TRUE;
repeat(10)@(negedge CLOCK); CAR_ON_CNTRY_RD = `FALSE;
repeat(10)@(negedge CLOCK); $stop;
end
endmodule

You might also like