module shumaxianshikongzhi( input clk,clear,
output reg[3:0] Q);
reg[1:0] state;
reg flag;
localparam S0=2'b00,
S1=2'b01,
S2=2'b10,
S3=2'b11;
always@(posedge clk)
begin
if(!clear)
begin
state<=S0;
Q<=4'b0000;
flag<=1'b0;
end
else
case (state)
S0: begin
if(Q==4'b1001)
begin
Q<=4'b0001;
state<=S1;
end
else
Q<=Q+1'b1;
end
S1: begin
if(Q==4'b1001)
begin
Q<=4'b0010;
state<=S2;
end
else
Q<=Q+2'b10;
end
S2: begin
if(Q==4'b1000)
begin
Q<=4'b0000;
state<=S3;
end
else
Q<=Q+2'b10;
end
S3: begin
if(Q==4'b0111)
begin
Q<=4'b0000;
flag<=1'b1;
end
else
if(Q==4'b0001 && flag==1'b1)
begin
Q<=4'b0000;
state<=S0;
end
else
Q<=Q+1'b1;
end
endcase
end
endmodule
output reg[3:0] Q);
reg[1:0] state;
reg flag;
localparam S0=2'b00,
S1=2'b01,
S2=2'b10,
S3=2'b11;
always@(posedge clk)
begin
if(!clear)
begin
state<=S0;
Q<=4'b0000;
flag<=1'b0;
end
else
case (state)
S0: begin
if(Q==4'b1001)
begin
Q<=4'b0001;
state<=S1;
end
else
Q<=Q+1'b1;
end
S1: begin
if(Q==4'b1001)
begin
Q<=4'b0010;
state<=S2;
end
else
Q<=Q+2'b10;
end
S2: begin
if(Q==4'b1000)
begin
Q<=4'b0000;
state<=S3;
end
else
Q<=Q+2'b10;
end
S3: begin
if(Q==4'b0111)
begin
Q<=4'b0000;
flag<=1'b1;
end
else
if(Q==4'b0001 && flag==1'b1)
begin
Q<=4'b0000;
state<=S0;
end
else
Q<=Q+1'b1;
end
endcase
end
endmodule
一周热门 更多>