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

输入序列连续的序列检测

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

使用状态机

`timescale 1ns/1ns
module sequence_detect(
	input clk,
	input rst_n,
	input a,
	output reg match
	);
    reg [3:0] pre_state;
    reg [3:0] next_state;
    parameter S0 = 4'b0000;
    parameter S1 = 4'b0001;
    parameter S2 = 4'b0010;
    parameter S3 = 4'b0011;
    parameter S4 = 4'b0100;
    parameter S5 = 4'b0101;
    parameter S6 = 4'b0110;
    parameter S7 = 4'b0111;
    parameter S8 = 4'b1000;
    
    always@(posedge clk or negedge rst_n) begin
        if(!rst_n) begin
            match <= 0;
            pre_state <= S0 ;
            //next_state <= S0;
        end else begin
            pre_state <= next_state;  
        end
        
    end
    
    always@(posedge clk )begin
        case(pre_state)
            S0: next_state = (a == 1) ? S0 : S1;
            S1: next_state = (a == 1) ? S2 : S1;
            S2: next_state = (a == 1) ? S3 : S1;
            S3: next_state = (a == 1) ? S4 : S1;
            S4: next_state = (a == 1) ? S0 : S5;
            S5: next_state = (a == 1) ? S2 : S6;
            S6: next_state = (a == 1) ? S2 : S7;
            S7: next_state = (a == 1) ? S8 : S1;
            S8: next_state = (a == 1) ? S3 : S1;
            default: next_state = S0;
        endcase  
    end
    
    always@(posedge clk or negedge rst_n)begin
        case(pre_state)
            S0: match = 0;
            S1: match = 0;
            S2: match = 0;
            S3: match = 0;
            S4: match = 0;
            S5: match = 0;
            S6: match = 0;
            S7: match = 0;
            S8: match = 1;
            default: match = 0;
        endcase
    end
    
   // assign match = (pre_state == S8);
    
//     always@(posedge clk or negedge rst_n) begin
//         if(!rst_n)
//             match <= 0;
//         else if(pre_state == S8)
//             match <= 1;
//         else
//             match <= 0;
//     end
    
    
    
endmodule



不使用状态机,使用序列缓存对比法

`timescale 1ns/1ns
module sequence_detect(
    input clk,
    input rst_n,
    input a,
    output reg match
);
    reg [7:0] a_tmp;
    
    always@(posedge clk or negedge rst_n) begin
        if(!rst_n)
            a_tmp <= 8'b0;
        else
            a_tmp <= {a_tmp[6:0], a};
    end
    
    always@(posedge clk or negedge rst_n) begin
        if(!rst_n)
            match <= 0;
        else if(a_tmp == 8'b01110001)
            match <= 1;
        else 
            match <= 0;
    end
    
endmodule






全部评论

相关推荐

八股刚起步,看了javaguide,小林coding,还有面渣,感觉面渣是体验最好的,请问只看面渣够用吗,有不完善的需要补吗?
码农索隆:先背最基础的知识,然后理解情景题,现在面试大多数喜欢问情景题,更考验面试者的基础和临场发挥情况
点赞 评论 收藏
分享
06-23 11:28
门头沟学院 Java
牛客91966197...:也有可能是点拒绝的时候自动弹的话术
点赞 评论 收藏
分享
06-15 02:05
已编辑
南昌航空大学 数据分析师
Eason三木:你如果想干技术岗,那几个发公众号合唱比赛的经历就去掉,优秀团员去掉,求职没用。然后CET4这种不是奖项,是技能,放到下面的专业技能里或者单独列一个英语能力。 另外好好改改你的排版,首行缩进完全没有必要,行间距好好调调,别让字和标题背景黏在一起,你下面说能做高质量PPT你得展现出来啊,你这简历排版我用PPT做的都能比你做的好。 然后自我评价,你如果要干数据工程师,抗压能力强最起码得有吧。
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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