05 Jun 12:18
state
// State machine: state transition logic
always @(posedge PCB_CLK50) begin
state <= next_state;
timer <= timer + 1;
end
// State machine: next state and output logic
always @(*) begin
case (state)
CAR_GREEN: begin
car_light = 3'b001; // Green for cars
ped_light = 2'b10; // Red for pedestrians
if (request && timer >= GREEN_TICKS) begin
next_state = CAR_YELLOW;
timer = 0;
request = 0;
end else begin
next_state = CAR_GREEN;
end
end
CAR_YELLOW: begin
car_light = 3'b010; // Yellow for cars
ped_light = 2'b10; // Red for pedestrians
if (timer >= YELLOW_TICKS) begin
next_state = PED_GREEN;
timer = 0;
end else begin
next_state = CAR_YELLOW;
end
end
PED_GREEN: begin
car_light = 3'b100; // Red for cars
ped_light = 2'b01; // Green for pedestrians
if (timer >= GREEN_TICKS) begin
next_state = CAR_RED_YELLOW;
timer = 0;
end else begin
next_state = PED_GREEN;
end
end
CAR_RED_YELLOW: begin
car_light = 3'b110; // Red+Yellow for cars
ped_light = 2'b10; // Red for pedestrians
if (timer >= YELLOW_TICKS) begin
next_state = CAR_GREEN;
timer = 0;
end else begin
next_state = CAR_RED_YELLOW;
end
end
default: begin
car_light = 3'b001;
ped_light = 2'b10;
next_state = CAR_GREEN;
end
endcase
end