题解 | #状态机-重叠序列检测#

状态机-重叠序列检测

https://www.nowcoder.com/practice/10be91c03f5a412cb26f67dbd24020a9

这里就是和上一个题的本质区别,这里可以直接接着继续检测,即可以重叠
`timescale 1ns/1ns

module sequence_test2(
	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;
    
    reg    [2:0]    cur_state;
    reg    [2:0]    nex_state;
    
    
    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 ? One_st : Two_st;    //这里就是和上一个题的本质区别,这里可以直接接着继续检测,即可以重叠
                    default    :    nex_state = IDEL_st;
                endcase
            end
    end
    
    always@(posedge clk or negedge rst)
    begin
        if(!rst)
            flag <= 1'b0;
        else
            begin
                if(cur_state == Four_st)    //注意题目要求的寄存器输出,所以可以选择当前状态,这样可以延迟一拍
                    flag <= 1'b1;
                else
                    flag <= 1'b0;
            end
    end

//*************code***********//
endmodule

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务