终于分成模块了!谢谢大家的指点
//LAMP:灯的状态
//num: 倒计时
//numa: 倒计时赋值的中间变量
module traffic0(CLK,LAMP,nrst,num);
output[7:0] num;
output[2:0] LAMP;
input CLK,nrst;
reg[7:0] num,numa;
reg[3:0] CS,NS;
reg[2:0] LAMP;
reg temp;//倒计时赋值标志
parameter IDLE=4'b001, S1=4'b0010, S2=4'b0100,S3=4'b1000; //one_hot
parameter red=8'd15,yellow=8'd10,bule=8'd5; //红,黄,蓝各亮的时间
always @(posedge CLK )
if(!nrst)
CS<=IDLE;
else
CS<=NS;
always @(CS or temp )
begin
NS=4'bx;
case(CS)
IDLE:
if(temp)
NS=IDLE;
else
NS=S1;
S1:
if(temp)
NS=S1;
else
NS=S2;
S2:
if(temp)
NS=S2;
else
NS=S3;
S3:
if(temp)
NS=S3;
else
NS=S1;
default:NS=IDLE;
endcase
end
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
always @(NS or nrst)//倒计时模块 赋值模块
if(!nrst)
begin
numa=8'd0;
end
else
begin
case(NS)
IDLE:
numa=8'd0;
S1:
numa=red;
S2:
numa=yellow;
S3:
numa=bule;
endcase
end
always @(posedge CLK)//灯的状态模块
if(!nrst)
LAMP<=3'b000;
else
begin
case(NS)
IDLE:
LAMP<=3'b000;
S1:
LAMP<=3'b001;
S2:
LAMP<=3'b010;
S3:
LAMP<=3'b100;
endcase
end
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
always @(posedge CLK ) //倒计时模块
if(!nrst)
begin
num<=0;
temp<=0;
end
else
begin
if(!temp)
begin
num<=numa;
temp<=1;
end
else
begin
if(num>0)
begin
if(num[3:0]==0)
begin
num[3:0]<=9;
num[7:4]<=num[7:4]-4'b0001;
end
else
num[3:0]<=num[3:0]-4'b0001;
if(num==1)
temp<=0;
else
temp<=1;
end
else
temp<=0;
end
end
endmodule |