题解 | #输入序列连续的序列检测#

输入序列连续的序列检测

http://www.nowcoder.com/practice/d65c2204fae944d2a6d9a3b32aa37b39

`timescale 1ns/1ns module sequence_detect( input clk, input rst_n, input a, output reg match ); localparam idle =9'b000000000; localparam s1 =9'b000000001; localparam s2 = 9'b000000010; localparam s3 = 9'b000000100; localparam s4 = 9'b000001000;

localparam s5 = 9'b000100000;
localparam s6 = 9'b001000000;
localparam s7 = 9'b010000000;
localparam s8 = 9'b100000000;
reg [8:0] cs,ns;

always@(posedge clk or negedge rst_n) begin
    if(!rst_n) 
        cs <= 8'b0;
    else cs <= ns;
end

always@(*) begin
    case(cs)
        idle:begin
            if(a==0) ns = s1;
            else ns = idle;
        end
        s1:begin
            if(a==0) ns = s1;
            else ns = s2;
        end
        s2:begin
            if(a==0) ns = s1;
            else ns = s3;
        end
        s3:begin
            if(a==0) ns = s1;
            else ns = s4;
        end
        s4:begin
            if(a==0) ns = s5;
            else ns = idle;
        end
        s5:begin
            if(a==0) ns = s6;
            else ns = s2;
        end
        s6:begin
            if(a==0) ns = s7;
            else ns = s2;
        end
        s7:begin
            if(a==0) ns = s1;
            else ns = s8;
        end
        s8:begin
            if(a==0) ns = s1;
            else ns = s3;
        end
        default: ns = idle;
    endcase
end

always@(posedge clk or negedge rst_n) begin
    if(!rst_n) 
        match <= 0;
    else if(cs == s8)  match <= 1;
    else  match <= 0;

end

endmodule

全部评论

相关推荐

hwwhwh:同双非,有大厂实习其实也没啥用,主要看运气,等就行了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务