10010序列检测器

10010序列检测器

重复检测输入,当检测到输入端顺序输入10010序列时,输出为1。

1.使用同步状态机实现

1.状态转移图

image-20220511194211451

2.Verilog 实现

module detect_10010(
  input clk,
  input rst_n,
  input in_a,
  output reg detect);

  parameter S0 = 3'b000,
            S1 = 3'b001,
            S2 = 3'b010,
            S3 = 3'b011,
            S4 = 3'b100;

  reg [2:0] state, next_state;

  always @(posedge clk) begin
    if(!rst_n)
      state <= S0;
    else
      state <= next_state;
  end

  always @(*) begin
    case(state)
      S0: if(in_a) next_state = S1;
          else next_state= S0;
      S1: if(~in_a) next_state = S2;
          else next_state = S1; 
      S2: if(~in_a) next_state = S3;
          else next_state = S1; 
      S3: if(in_a) next_state = S4;
          else next_state = S0; 
      S4: if(~in_a) next_state = S2;
          else next_state = S1; 
    endcase
  end

  always @(*) begin
    if(!rst_n) 
      detect = 1'b0;
    else begin 
      if(state == S4 && in_a == 1'b0)
        detect = 1'b1;
      else
        detect = 1'b0;
    end
  end
endmodule

3.TB代码

module tb();
  reg in_a;
  reg clk, rst_n;
  reg detect;

  initial begin
    in_a = 0;
    rst_n = 1;
    clk = 0;
    #40 rst_n = 0;
    #70 rst_n = 1;
  end

  initial begin
    @(posedge clk);
    @(posedge clk)  #70 in_a = 1;
    @(posedge clk)  #70 in_a = 0;
    @(posedge clk)  #70 in_a = 0;
    @(posedge clk)  #70 in_a = 1;
    @(posedge clk)  #70 in_a = 0;

    @(posedge clk)  #70 in_a = 0;
    @(posedge clk)  #70 in_a = 1;
    @(posedge clk)  #70 in_a = 0;
    @(posedge clk)  #70 in_a = 1;
    #150 $finish();
  end
    
  always #50 clk = ~clk;
    
  initial begin
    $fsdbDumpvars();
  end
  detect_10010 DUT(.clk(clk), .rst_n(rst_n), .in_a(in_a), .detect(detect));  
endmodule

4.波形

tmpD084

2.使用移位寄存器实现

使用一个5位的寄存器寄存5个连续输入值,并在下一个时钟周期对其移位,最后与需要的检查的序列(10010)进行对比,根据对比结果输出检测结果。

1.Verilog 实现

module detect_10010_shifter(
  input clk,
  input rst_n,
  input in_a,
  output reg detect
);
  reg [4:0] rega;

  always @(posedge clk) begin
    if(!rst_n) begin
      detect <= 0;
      rega <= 'b0;
    end
    else begin
      rega <= {rega[3:0], in_a};
    end
  end

  always @(*) begin
    if(rega == 5'b10010)
      detect = 1;
    else
      detect = 0;
  end

endmodule

2.TB代码

module tb();
  reg in_a;
  reg clk, rst_n;
  reg detect;

  initial begin
    in_a = 0;
    rst_n = 1;
    clk = 0;
    #40 rst_n = 0;
    #70 rst_n = 1;
  end

  initial begin
    @(posedge clk);
    @(posedge clk)  #70 in_a = 1;
    @(posedge clk)  #70 in_a = 0;
    @(posedge clk)  #70 in_a = 0;
    @(posedge clk)  #70 in_a = 1;
    @(posedge clk)  #70 in_a = 0;

    @(posedge clk)  #70 in_a = 0;
    @(posedge clk)  #70 in_a = 1;
    @(posedge clk)  #70 in_a = 0;
    @(posedge clk)  #70 in_a = 1;
    #150 $finish();
  end

  always #50 clk = ~clk;

  initial begin
    $fsdbDumpvars();
  end
  
  detect_10010_shifter  DUT(.clk(clk), .rst_n(rst_n), .in_a(in_a), .detect(detect));
  
endmodule

3.波形

tmpEFBE

参考

‘10010’序列检测器的两种实现方法(有限状态机、移位寄存器)_孤独的单刀的博客-CSDN博客_序列检测器

  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Verilog是硬件描述语言,可以用于实现数字电路设计和验证。要实现一个10010序列检测器,首先需要定义输入和输出信号以及内部的状态变量。我们可以使用有限状态机来实现这个序列检测器。 首先,我们定义一个输入信号input_data,用于输入检测序列。然后,我们定义一个输出信号sequence_detected,用于指示是否检测到了10010序列。接下来,我们定义一个状态变量state,用于表示当前的状态,初始状态可以是idle。 在Verilog中,我们可以使用always块来描述状态转移和输入信号的变化。在always块中,根据当前的状态和输入信号,我们可以更新状态变量和输出信号。当检测到输入信号为1时,我们可以根据当前状态变量的值更新状态。如果当前状态是idle,并且检测到了1,则状态变为s1。依此类推,当状态转移到s4时,如果检测到了0,则状态回到idle。在状态转移到s5时,如果检测到了1,则设置sequence_detected为1,表示检测到了10010序列。 除了状态转移逻辑外,我们也需要考虑顺序逻辑和时序逻辑。我们需要确保信号的稳定传输和状态变化的同步。因此,我们可以使用寄存器和时钟来实现这一点。 总的来说,要用Verilog实现一个10010序列检测器,我们需要定义输入和输出信号,状态变量,使用有限状态机描述状态转移逻辑,使用时钟和寄存器来实现时序逻辑和顺序逻辑。通过以上步骤,我们就可以完成10010序列检测器的Verilog实现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值