描述
设计一个状态机,用来检测序列 10111,要求:
1、进行非重叠检测 即101110111 只会被检测通过一次
2、寄存器输出且同步输出结果
注意rst为低电平复位
`timescale 1ns/1ns
module sequence_test1(
input wire clk ,
input wire rst ,
input wire data ,
output reg flag
);
//*************code***********//
reg [3:0] state,next_state;
always@(posedge clk or negedge rst)begin
if(!rst)
state<=0;
else
state<=next_state;
end
always@(*)begin
case(state)
4'd0 : next_state <= (data==1) ? 4'd1 : 4'd0;
4'd1 : next_state <= (data==0) ? 4'd2 : 4'd0;
4'd2 : next_state <= (data==1) ? 4'd3 : 4'd0;
4'd3 : next_state <= (data==1) ? 4'd4 : 4'd0;
4'd4 : next_state <= (data==1) ? 4'd5 : 4'd0;
4'd5 : next_state <= (data==1) ? 4'd1 : 4'd0;
default:next_state <= 4'd0 ;
endcase
end
always@(posedge clk or negedge rst)begin
if(!rst)
flag <= 0 ;
else if(state==4'd4 & data==1)//要求同步输出结果
flag <= 1 ;
else
flag <= 0 ;
end
//*************code***********//
endmodule