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

输入序列连续的序列检测

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






全部评论

相关推荐

点赞 评论 收藏
分享
专业嗎喽:个人信息名字太大,合到电话邮箱那一栏就行,有党员写过党,剩下其他全删,站空太大了 把实习经历丰富,放最前面,然后是个人评价,技能之类的,然后是学校信息。项目经历最后面,可以就选一个自己擅长的。 现在是学校不是92就扣分的,没必要放前面。 然后现在看重实习经历>竞赛经历(校园经历)>课程项目经历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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