You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.4 KiB
Coq

7 months ago
module traffic_light(
input wire sys_clk ,
input wire sys_rst_n ,
output reg [3: 0] led
);
parameter MAX_S = 26'd50_000_000;//1s
parameter MAX_83 = 7'd83;//83s
//,
parameter IDLE = 2'd0,
RED = 2'd1,
GREEN = 2'd2,
YELLOW = 2'd3;
//
parameter ACT1 = 4'b0000,
ACT2 = 4'b0100,
ACT3 = 4'b0010,
ACT4 = 4'b0001;
reg [1: 0] c_state;//current state
reg [1: 0] n_state;//next state
reg [25: 0] cnt_s;//1s
reg [6: 0] cnt_83;//83s
wire start;//绿
assign start = 1'b1;
//cnt_s
always @(posedge sys_clk or negedge sys_rst_n) begin
if(!sys_rst_n)begin
cnt_s <= 26'd0;
end
else if(cnt_s == MAX_S - 1'd1)begin
cnt_s <= 26'd0;
end
else begin
cnt_s <= cnt_s + 1'd1;
end
end
//cnt_83
always @(posedge sys_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin
cnt_83 <= 7'd0;
end
//83-11s
else if((cnt_83 == MAX_83 - 1'd1) && (cnt_s == MAX_S - 1'd1))begin
cnt_83 <= 7'd0;
end
//1s1
else if(cnt_s == MAX_S - 1'd1)begin
cnt_83 <=cnt_83 + 1'd1;
end
//
else begin
cnt_83 <= cnt_83;
end
end
//
always @(posedge sys_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin
c_state <= IDLE;//IDLE
end
else begin
c_state <= n_state;//沿
end
end
//
always @(*)begin
case(c_state)
IDLE : begin
if(start == 1'b1)begin//
n_state = RED;//
end
else begin
n_state = IDLE;//
end
end
RED : begin
if((cnt_83 == 7'd59) && (cnt_s == MAX_S - 1'd1))begin
n_state = GREEN;
end
else begin
n_state = RED;
end
end
GREEN : begin
if((cnt_83 == 7'd79) && (cnt_s == MAX_S - 1'd1))begin
n_state = YELLOW;
end
else begin
n_state = GREEN;
end
end
YELLOW : begin
if((cnt_83 == MAX_83 - 1'd1) && (cnt_s == MAX_S - 1'd1))begin
n_state = RED;
end
else begin
n_state = YELLOW;
end
end
default : begin
n_state = IDLE;
end
endcase
end
//
always@(*)begin
case(c_state)
IDLE : led = ACT1;
RED : led = ACT2;
GREEN : led = ACT3;
YELLOW : led = ACT4;
default : led = ACT1;
endcase
end
endmodule