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

含有无关项的序列检测

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
全部评论

相关推荐

昨天 18:09
门头沟学院 Java
苍穹外卖和谷粒商城这俩是不是烂大街了,还能做吗?
想去重庆的鸽子在吐槽:你不如把这俩做完自己搞明白再优化点再来问 何必贩卖焦虑
点赞 评论 收藏
分享
看到这个内容真是闹麻了。。。。。。现在有了AI以后很多人面试都会作弊吗?&nbsp;那对老老实实面试的人岂不是不公平....
重生之我要干前端:放宽心,作弊很明显的,面试官也不是傻子,而且这世上更多的肯定是依靠自己的知识的人,所以放宽心提升自己最重要
点赞 评论 收藏
分享
VirtualBoo...:都去逗他了?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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