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

输入序列连续的序列检测

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






全部评论

相关推荐

03-29 12:10
门头沟学院 C++
挣K存W养DOG:散漫消极者淘汰,一眼坑爹。实习几个月转正的时候说你加班太少,能力还行态度不够积极裁了,马上老实。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务