牛客刷题<26>含有无关项的序列检测

题目:含有无关项的序列检测_牛客题霸_牛客网

解法一:最容易想到的casex

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

 解法二:移位方式

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

解法三:三段式状态机

`timescale 1ns/1ns
module sequence_detect(
    input clk,
    input rst_n,
    input a,
    output reg match
);
    parameter s0 = 4'd0,
              s1 = 4'd1,
              s2 = 4'd2,
              s3 = 4'd3,
              s4 = 4'd4,
              s5 = 4'd5,
              s6 = 4'd6,
              s7 = 4'd7,
              s8 = 4'd8,
              s9 = 4'd9;
    reg [3:0] curr_state,next_state;
    always@(posedge clk or negedge rst_n)begin
        if(!rst_n)
            curr_state <= s0;
        else
            curr_state <= next_state;
    end
    always@(*)begin
        case(curr_state)
            s0:next_state = a ? s0 : s1;
            s1:next_state = a ? s2 : s1;
            s2:next_state = a ? s3 : s1;
            s3:next_state = s4;
            s4:next_state = s5;
            s5:next_state = s6;
            s6:next_state = a ? s7 : s1;
            s7:next_state = a ? s8 : s1;
            s8:next_state = a ? s0 : s9;
            s9:next_state = a ? s0 : s1;
            default:next_state = s0;
        endcase
    end
    always@(posedge clk or negedge rst_n)begin
        if(!rst_n)
            match <= 0;
        else
            match <= curr_state == s9;
    end
endmodule

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值