parameter s0=3'b000,s1=3'b001,s2=3'b011,s3=3'b010,
s4=3'b110,s5=3'b111,s6=3'b101,s7=3'b100; //定义符号状态值
always@(posedge clk or negedge reset) //边沿敏感 begin
if(!reset)
x<=8'd0; //若reset为0,则x为0
else if(x==8'd120) //若reset为1,且x为120 x<=8'd1; //那么x为1 else
x<=x+8'b00000001; //否则x加1赋给x end
always@(x) //敏感变量为x begin
case(x) //case语句,x为敏感变量 8'd1:y<=1; //若x=1,则y=1 8'd31:y<=1; //若x=1,则y=1 8'd36:y<=1; //若x=1,则y=1 8'd56:y<=1; //若x=1,则y=1 8'd61:y<=1; //若x=1,则y=1 8'd91:y<=1; //若x=1,则y=1 8'd96:y<=1; //若x=1,则y=1 8'd116:y<=1; //若x=1,则y=1 default:y<=0; //除此之外,y=0 endcase end
always@(posedge y or negedge reset) //边沿敏感 begin
if(!reset) //如果reset=0 z<=4'b0000; //那么z=0
else if(z==4'b1000) //若reset=1,且z==4'b1000 z<=4'b0001; //那么z为1 else
z<=z+4'b0001; //否则z加1 end
always @(posedge clk or negedge reset) //边沿敏感 begin
if(!reset) //若reset为0
CS<=s0; //那么现态为s0 else
CS<=NS; //否则现态到次态翻转 end
always @(CS or reset) //敏感变量为CS和reset begin
case(CS)
s0: begin //当前为s0状态
NS=(30-x==0)?s1:s0; //若30-x为0,则转换到s1,否则仍为s0 end
s1: begin //当前为s1状态
NS=(35-x==0)?s2:s1; //若35-x为0,则转换到s2,否则仍为s1 end
s2: begin //当前为s2状态
NS=(55-x==0)?s3:s2; //若55-x为0,则转换到s3,否则仍为s2 end
s3: begin //当前为s3状态
NS=(60-x==0)?s4:s3; //若60-x为0,则转换到s4,否则仍为s3 end
s4: begin //当前为s4状态
NS=(90-x==0)?s5:s4; //若90-x为0,则转换到s5,否则仍为s4 end
s5: begin //当前为s5状态
NS=(95-x==0)?s6:s5; //若95-x为0,则转换到s6,否则仍为s5 end
s6: begin //当前为s6状态
NS=(115-x==0)?s7:s6; //若115-x为0,则转换到s7,否则仍为s6 end
s7: begin //当前为s7状态
NS=(120-x==0)?s0:s7; //若120-x为0,则转换到s0,否则仍为s7 end endcase end
always@(posedge clk or negedge reset) //边沿敏感 begin
case(z) //case语句
8'd1:begin HS<=31-x;HL<=36-x;SS<=61-x;SL<=96-x;end //当z=1时,各倒计时的表达式
8'd2:begin HS<=36-x;HL<=36-x;SS<=61-x;SL<=96-x;end //当z=2时,各倒计时的表达式
8'd3:begin HS<=121-x;HL<=56-x;SS<=61-x;SL<=96-x;end //当z=3时,各倒计时的表达式
8'd4:begin HS<=121-x;HL<=61-x;SS<=61-x;SL<=96-x;end //当z=4时,各倒计时的表达式
8'd5:begin HS<=121-x;HL<=121-x;SS<=91-x;SL<=96-x;end //当z=5时,各倒计时的表达式
8'd6:begin HS<=121-x;HL<=121-x;SS<=96-x;SL<=96-x;end //当z=6时,各倒计时的表达式
8'd7:begin HS<=121-x;HL<=121-x;SS<=121-x;SL<=116-x;end //当z=7时,各倒计时的表达式
8'd8:begin HS<=121-x;HL<=121-x;SS<=121-x;SL<=121-x;end //当z=8时,各倒计时的表达式 endcase end
always @(CS) //CS为敏感变量 begin
case(CS)
s0: begin //当前为s0状态
{HSR,HSG,HSY}=3'b010;{HLR,HLG,HLY}=3'b100; //东西直行为绿灯 {SSR,SSG,SSY}=3'b100;{SLR,SLG,SLY}=3'b100; //其他方向为红灯 end
s1: begin //当前为s1状态
{HSR,HSG,HSY}=3'b001;{HLR,HLG,HLY}=3'b100; //东西直行为黄灯 {SSR,SSG,SSY}=3'b100;{SLR,SLG,SLY}=3'b100; //其他方向为红灯 end
s2: begin //当前为s2状态
{HSR,HSG,HSY}=3'b100;{HLR,HLG,HLY}=3'b010; //东西左拐为绿灯 {SSR,SSG,SSY}=3'b100;{SLR,SLG,SLY}=3'b100; //其他方向为红灯 end
s3: begin //当前为s3状态
{HSR,HSG,HSY}=3'b100;{HLR,HLG,HLY}=3'b001; //东西左拐为黄灯 {SSR,SSG,SSY}=3'b100;{SLR,SLG,SLY}=3'b100; //其他方向为红灯 end
s4: begin //当前为s4状态
{HSR,HSG,HSY}=3'b100;{HLR,HLG,HLY}=3'b100; //南北直行为绿灯 {SSR,SSG,SSY}=3'b010;{SLR,SLG,SLY}=3'b100; //其他方向为红灯 end
s5: begin //当前为s5状态
{HSR,HSG,HSY}=3'b100;{HLR,HLG,HLY}=3'b100; //南北直行为黄灯 {SSR,SSG,SSY}=3'b001;{SLR,SLG,SLY}=3'b100; //其他方向为红灯 end
s6: begin //当前为s6状态
{HSR,HSG,HSY}=3'b100;{HLR,HLG,HLY}=3'b100; //南北左拐为绿灯 {SSR,SSG,SSY}=3'b100;{SLR,SLG,SLY}=3'b010; //其他方向为红灯 end
s7: begin //当前为s7状态
{HSR,HSG,HSY}=3'b100;{HLR,HLG,HLY}=3'b100; //南北左拐为黄灯 {SSR,SSG,SSY}=3'b100;{SLR,SLG,SLY}=3'b001; //其他方向为红灯
end endcase end
assign HS0[7:4]=HS/4'b1010; //令HS/4'b1010作为HS0的高4位 assign HS0[3:0]=HS%4'b1010; //令HS%4'b1010作为HS0的低4位 assign HL0[7:4]=HL/4'b1010; //令HL/4'b1010作为HL0的高4位 assign HL0[3:0]=HL%4'b1010; //令HL%4'b1010作为HL0的低4位 assign SS0[7:4]=SS/4'b1010; //令SS/4'b1010作为SS0的高4位 assign SS0[3:0]=SS%4'b1010; //令SS%4'b1010作为SS0的低4位 assign SL0[7:4]=SL/4'b1010; //令SL/4'b1010作为SL0的高4位 assign SL0[3:0]=SL%4'b1010; //令SL%4'b1010作为SL0的低4位
endmodule
四、仿真测试图
多个周期仿真图
单个周期仿真图
相关推荐: