题解 | #不重叠序列检测#

不重叠序列检测

https://www.nowcoder.com/practice/9f91a38c74164f8dbdc5f953edcc49cc

题目中的时序图和结果的时序图不一致,结果中需要在第6个数据到来的同时输出结果。
`timescale 1ns/1ns
module sequence_detect(
	input clk,
	input rst_n,
	input data,
	output reg match,
	output reg not_match
	);
    
        
    localparam S0 = 3'd0, S1 = 3'd1, S2 = 3'd2, S3 = 3'd3, S4 = 3'd4, S5 = 3'd5;
    reg [3:0] state, next_state;
    reg [3:0] match_cnt;
    
    always @(posedge clk or negedge rst_n) begin
        if(~rst_n) 
            state <= S0;
        else
            state <= next_state;
    end
    
    always @(*) begin
        if(~rst_n)
            next_state = S0;
        else begin
            case(state)
                S0  : next_state = S1;
                S1  : next_state = S2;
                S2  : next_state = S3;
                S3  : next_state = S4;
                S4  : next_state = S5;
                S5  : next_state = S0;
                default: next_state = S0;
            endcase
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if(~rst_n)
            match_cnt <= 'd0;
        else begin
            case(state)
                S0  : begin
                        if(data == 1'b0)
                            match_cnt <= 3'd1;
                        else
                            match_cnt <= 3'd0;
                      end
                S1  : if(data == 1'b1 && match_cnt == 3'd1) match_cnt <= 3'd2;
                S2  : if(data == 1'b1 && match_cnt == 3'd2) match_cnt <= 3'd3;
                S3  : if(data == 1'b1 && match_cnt == 3'd3) match_cnt <= 3'd4;
                S4  : if(data == 1'b0 && match_cnt == 3'd4) match_cnt <= 3'd5;
                S5  : if(data == 1'b0 && match_cnt == 3'd5) match_cnt <= 3'd6;
                default: match_cnt <= 'd0;    
            endcase
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if(~rst_n)
            match <= 1'b0;
        else if(state == S5 && match_cnt == 3'd5 && data == 1'b0)
            match <= 1'b1;
        else
            match <= 1'b0;
    end
    
    always @(posedge clk or negedge rst_n) begin
        if(~rst_n)
            not_match <= 1'b0;
        else if(state == S5 && match_cnt != 3'd5)
            not_match <= 1'b1;
        else
            not_match <= 1'b0;
    end       
    
endmodule


#职场打工人实录#
全部评论

相关推荐

spiritecs:没实习非985211硕很难很难,只能说祝早日成功
点赞 评论 收藏
分享
kl_我是东山啊:《相关公司:阿里巴巴》
投递阿里巴巴等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务