题解 | #含有无关项的序列检测#

含有无关项的序列检测

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

`timescale 1ns/1ns
module sequence_detect(
    input clk,
    input rst_n,
    input a,
    output reg match
    );
    
    parameter S0 =10'b0000_0000_01;
    parameter S1 =10'b0000_0000_10;
    parameter S2 =10'b0000_0001_00;
    parameter S3 =10'b0000_0010_00;
    parameter S4 =10'b0000_0100_00;
    parameter S5 =10'b0000_1000_00;
    parameter S6 =10'b0001_0000_00;
    parameter S7 =10'b0010_0000_00;
    parameter S8 =10'b0100_0000_00;
    parameter S9 =10'b1000_0000_00;

    reg [9:0] CS;
    reg [9:0] NS;
    
    always@(posedge clk or negedge rst_n) begin
        if(!rst_n) begin
            CS    <=S0;
        end
        else begin
            CS     <=NS;
        end
    end
    
    always@(*) begin
        case(CS) 
        S0: begin
            if(a==1'b0) begin
                NS <=S1;
            end
            else begin
                NS <=S0;
            end
        end
            
            S1: begin
            if(a==1'b0) begin
                NS <=S1;
            end
            else begin
                NS <=S2;
            end
        end
            
            S2: begin
            if(a==1'b0) begin
                NS <=S1;
            end
            else begin
                NS <=S3;
            end
        end
            
            S3: begin
                NS <=S4;
            end

            
            S4: begin
                NS <=S5;
            end

            
            S5: begin
                NS <=S6;
            end

            
            S6: begin
            if(a==1'b0) begin
                NS <=S1;
            end
            else begin
                NS <=S7;
            end
        end
            
            S7: begin
            if(a==1'b0) begin
                NS <=S1;
            end
            else begin
                NS <=S8;
            end
        end
            
            S8: begin
                if(a==1'b1) begin
                NS <=S0;
            end
            else begin
                NS <=S9;
            end
        end
            
            S9: begin
            if(a==1'b0) begin
                NS <=S1;
            end
            else begin
                NS <=S2;
            end
        end
            default: NS <=S0;
        endcase
    end
    
    always@(posedge clk or negedge rst_n)  begin
        if(!rst_n) begin
        match<=1'b0;
        end
        else begin
            case(CS)                     //注意:这个地方应该是NS,三段式状态机时序输出应该case next_state,从时序图也可以看出,都采用moore状态机了,输出延迟了两个时钟周期,这是有问题的。但是答案是 case current_state
                S9: match<=1'b1;
                default: match<=1'b0;
            endcase
        end
    end
    
    
    
    
    
    
    
endmodule
全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 17:13
想去,但是听说加班强度实在难崩,所以拒绝了,现在有点心梗对面hr感觉也是实习生,打电话的时候怪紧张的,但是感觉人很好嘞
水中水之下水道的鼠鼠:哥们这不先去体验一下,不行再跑呗,大不了混个实习经历(有更好的转正offer就当我没说)
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-26 14:50
人力小鱼姐:有后面墨迹那两句的时间问题早回答完了
点赞 评论 收藏
分享
墨西哥大灰狼:如果你的校友卤馆还在的话,他肯定会给你建议的,可是卤馆注销了@ 程序员卤馆
点赞 评论 收藏
分享
水墨不写bug:疑似没有上过大学
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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