问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

数字电路设计:两种序列检测方法详解

创作时间:
作者:
@小白创作中心

数字电路设计:两种序列检测方法详解

引用
CSDN
1.
https://m.blog.csdn.net/qq_45771963/article/details/139423484

在数字电路设计中,序列检测是一个常见的应用场景。本文将介绍两种实现方法:状态机法和序列缓存对比法。通过详细的Verilog代码实现、测试代码以及仿真波形图,帮助读者更好地理解这两种方法的原理和实现过程。

状态机法

状态机法是一种常用的序列检测方法,可以通过Moore型或Mealy型状态机实现。本文采用Moore型状态机实现非重叠检测。

代码实现

`timescale 1ns/1ns
module xulie(
    input clk,
    input rst_n,
    input a,
    output reg match
    );
//Moore型状态机检测01110001序列
reg [3:0] state_c;
reg [3:0] state_n;
parameter IDLE=4'b0000;
parameter S1=4'b0001;
parameter S2=4'b0011;
parameter S3=4'b0010;
parameter S4=4'b0110;
parameter S5=4'b0111;
parameter S6=4'b0101;
parameter S7=4'b0100;
parameter S8=4'b1100;
always @(posedge clk or negedge rst_n) begin
    if(!rst_n) begin
        state_c<=IDLE;
    end else begin
        state_c<=state_n;
    end
end
always @(*) begin
   case(state_c)
   IDLE:state_n=a?IDLE:S1;
   S1:state_n=a?S2:S1;
   S2:state_n=a?S3:S1;
   S3:state_n=a?S4:S1;
   S4:state_n=a?IDLE:S5;
   S5:state_n=a?IDLE:S6;
   S6:state_n=a?S2:S7;
   S7:state_n=a?S8:S1;
   S8:state_n=a?IDLE:S1;
   default:state_n=IDLE;
   endcase
end
always @(posedge clk or negedge rst_n) begin
    if(!rst_n) begin
        match<=1'b0;
    end else if(state_n==S8) begin
        match<=1'b1;
    end else begin
        match<=1'b0;
    end
end 
endmodule  

测试代码

`timescale 1ns/1ns
module xulie_tb();
    reg clk,rst_n;
    reg a;
    wire match;
    
initial begin
    clk = 0;
    rst_n = 0;
    #10;
    rst_n=1;
    forever #20 a=({$random}%2);
    #1000;
    $stop;
end
always #10 clk=~clk;
xulie dut(
    .clk(clk),
    .rst_n(rst_n),
    .a(a),
    .match(match)
);
endmodule  

序列缓存对比法

序列缓存对比法通过将输入信号缓存到一个数组中,然后与目标序列进行对比,实现序列检测。

实现步骤

  1. 声明一个8位的寄存器数组,用于缓存输入信号。
  2. 通过位截取和位拼接操作实现数据的移位。
  3. 将缓存数组与目标序列进行对比。

代码实现

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

测试代码

`timescale 1ns/1ns
module sequence_detect_tb();
    reg clk,rst_n;
    reg a;
    wire match;
    
initial begin
    $dumpfile("out.vcd");
    $dumpvars(0,sequence_detect_tb);
    clk = 0;
    rst_n = 0;
    #30;
    rst_n=1;
    forever #20 a=({$random}%2);
    #1000;
    $stop;
end
always #10 clk=~clk;
sequence_detect dut(
    .clk(clk),
    .rst_n(rst_n),
    .a(a),
    .match(match)
);
endmodule  

仿真波形


遗留问题

在牛客网的在线编译环境中,可能会出现显示异常的问题,建议使用其他仿真工具进行验证。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号