-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraffic_controller.v
More file actions
54 lines (46 loc) · 1.44 KB
/
traffic_controller.v
File metadata and controls
54 lines (46 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
Alex Yazdani
18 November 2024
Traffic Controller Module
*/
module traffic_controller(
input clk, reset, ped_req,
output [1:0] veh_light,
output ped_light
);
// State Encoding
parameter IDLE = 2'b00;
parameter GREEN = 2'b01;
parameter YELLOW = 2'b10;
parameter RED = 2'b11;
reg [1:0] state, next_state;
reg [3:0] counter;
// Counter Logic
always @(posedge clk or posedge reset) begin
if (reset) counter <= 0;
else if (counter == 4'd9 && state == GREEN) counter <= 0;
else if (counter == 4'd4 && state == YELLOW) counter <= 0;
else if ((counter == 4'd9 && !ped_req) || (counter > 4'd9)) counter <= 0;
else if (state != next_state) counter <= 0;
else counter <= counter + 1;
end
// State Memory
always @(posedge clk or posedge reset) begin
if (reset) state <= IDLE;
else state <= next_state;
end
// Next State Logic (NSL)
always @(*) begin
next_state = state;
case (state)
IDLE: next_state = GREEN;
GREEN: if (counter == 4'd9) next_state = YELLOW;
YELLOW: if (counter == 4'd4) next_state = RED;
RED: if (((counter == 4'd9) && !ped_req) || (counter > 4'd9)) next_state = GREEN;
default: next_state = IDLE;
endcase
end
// Output Logic (OFL)
assign veh_light = state;
assign ped_light = (state == RED);
endmodule