题解 | #状态机-非重叠的序列检测#
状态机-非重叠的序列检测
https://www.nowcoder.com/practice/2e35c5c0798249aaa2e1044dbaf218f2
这个题目的非重叠检测的意思是如果出错了则可以接着前面的数据继续检测,但是如果是成功的检测结果,那么接下来的就是要重新开始,不能接着前面检测成功的数据进行检测 `timescale 1ns/1ns module sequence_test1( input wire clk , input wire rst , input wire data , output reg flag ); //*************code***********// localparam IDEL_st = 3'd0; localparam One_st = 3'd1; localparam Two_st = 3'd2; localparam Thre_st = 3'd3; localparam Four_st = 3'd4; localparam Five_st = 3'd5; //此题虽然名字是非重叠检测,但是不是之前的那个出错了就直接接下来不检测的方式,而是会重新开始检测或者接着检测 //这个题目的非重叠检测的意思是如果出错了则可以接着前面的数据继续检测,但是如果是成功的检测结果,那么接下来的就是要重新开始,不能接着前面检测成功的数据进行检测 reg [2:0] cur_state; reg [2:0] nex_state; reg [2:0] data_cnt; always@(posedge clk or negedge rst) begin if(!rst) data_cnt <= 3'b0; else begin if(data_cnt == 3'd4) data_cnt <= 3'b0; else data_cnt <= data_cnt + 1'b1; end end always@(posedge clk or negedge rst) begin if(!rst) cur_state <= IDEL_st; else cur_state <= nex_state; end always@(*) begin if(!rst) nex_state = IDEL_st; else begin nex_state = IDEL_st; case(cur_state) IDEL_st : nex_state = data ? One_st : IDEL_st; One_st : nex_state = data ? One_st : Two_st; Two_st : nex_state = data ? Thre_st : IDEL_st; Thre_st : nex_state = data ? Four_st : Two_st; Four_st : nex_state = data ? Five_st : Two_st; Five_st : nex_state = data ? One_st : IDEL_st; //这个地方的数据为0时,不能使其等于Two状态,因为文章中说明了只会通过一次,不能把之间检测成功的结果用来进行检测,而是要重新开始 default : nex_state = IDEL_st; endcase end end always@(posedge clk or negedge rst) begin if(!rst) flag <= 1'b0; else begin if(nex_state == Five_st) flag <= 1'b1; else flag <= 1'b0; end end //*************code***********// endmodule