always@(posedge clk)//下降沿触发
begin
if(rst)
begin
state=state0;
q=16'b0000000000000000;//把q全置零
end
else
begin
state=next_state;
end
case(state)
state0://从左到右奇数顺序亮
begin
if(q==16'b0000000000000000)
begin
q='b1000000000000000;
end
else
begin
if(count=='b0111)
begin
count=0;
q='b0000000000000010;
next_state=state1;
end
else
begin
q=q>>2;
count=count+1;
next_state=state0;
end
end
end
state1://从右到左奇数灯顺序亮
begin
if(count=='b0111)
begin
count=0;
q='b1000000000000010;
next_state=state2;
end
else
begin
q=q<<2;
count=count+1;
next_state=state1;
end
end
state2://从两边到中间奇数灯顺序亮
begin
if(count=='b0011)
begin
count=0;
q='b0000001010000000;
next_state=state3;
end
else
begin
q[15:9]=q[15:9]>>2;
q[7:1]=q[7:1]<<2;
count=count+1;
next_state=state2;
end
end
state3://从中间到两边奇数灯顺序亮
begin
if(count=='b0011)
begin
count=0;
q='b0100000000000000;
next_state=state4;
end
else
begin
q[15:9]=q[15:9]<<2;
q[7:1]=q[7:1]>>2;
count=count+1;
next_state=state3;
end
end
state4://从左到右偶数顺序亮
begin
if(count=='b0111)
begin
count=0;
q='b0000000000000001;
next_state=state5;
end
else
begin
q=q>>2;
count=count+1;
next_state=state4;
end
end
state5://从右到左偶数顺序亮
begin
if(count=='b0111)
begin
count=0;
q='b0000000101000000;
next_state=state6;
end
else
begin
q=q<<2;
count=count+1;
next_state=state5;
end
end
state6://从两边往中间偶数顺序亮
begin
if(count=='b0011)
begin
count=0;
q='b0100000000000001;
next_state=state7;
end
else
begin
q[6:0]=q[6:0]>>2;
q[14:8]=q[14:8]<<2;
count=count+1;
next_state=state6;
end
end
state7://从中间往两边偶数顺序亮
begin
if(count=='b0011)
begin
count=0;
q='b1000000000000000;
next_state=state0;
end
else
begin
q[6:0]=q[6:0]<<2;
q[14:7]=q[14:7]>>2;
count=count+1;
next_state=state7;
end
end
endcase
end
endmodule