【FPGA学习笔记】VL26 含有无关项的序列检测

描述

请编写一个序列检测模块,检测输入信号a是否满足011XXX110序列(长度为9位数据,前三位是011,后三位是110,中间三位不做要求),当信号满足该序列,给出指示信号match。

程序的接口信号图如下:

 

 

程序的功能时序图如下:


请使用Verilog HDL实现以上功能,并编写testbench验证模块的功能。 要求代码简洁,功能完整。

 

输入描述:

clk:系统时钟信号
rst_n:异步复位信号,低电平有效
a:单比特信号,待检测的数据
 

输出描述:

match:当输入信号a满足目标序列,该信号为1,其余时刻该信号为0
 
 
答:

移位寄存器

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module sequence_detect(
    input clk,
    input rst_n,
    input a,
    output reg match
    );
    reg [8:0] a_r;
     
    always@(posedge clk or negedge rst_n) begin
        if(~rst_n)
            match <= 1'b0;
        else
            match <= (a_r[2:0] == 3'b110) && (a_r[8:6] == 3'b011);
    end
     
    always@(posedge clk or negedge rst_n) begin
        if(~rst_n)
            a_r <= 9'b0;
        else
            a_r <= {a_r[7:0], a};
    end
endmodule

状态机

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module sequence_detect(
    input clk,
    input rst_n,
    input a,
    output reg match
);
    parameter ZERO=0, ONE=1, TWO=2, THREE=3, FOUR=4, FIVE=5, SIX=6, SEVEN=7, EIGHT=8, NINE=9;
    reg [3:0] state, nstate;
     
    always@(posedge clk or negedge rst_n) begin
        if(~rst_n)
            state <= ZERO;
        else
            state <= nstate;
    end
     
    always@(*) begin
        case(state)
            ZERO   : nstate = a? ZERO : ONE;
            ONE    : nstate = a? TWO  : ONE;
            TWO    : nstate = a? THREE: ONE;
            THREE  : nstate = FOUR;
            FOUR   : nstate = FIVE;
            FIVE   : nstate = SIX;
            SIX    : nstate = a? SEVEN: ONE;
            SEVEN  : nstate = a? EIGHT: ONE;
            EIGHT  : nstate = a? ZERO: NINE;
            NINE   : nstate = a? TWO  : ONE;
            default: nstate = ZERO;
        endcase
    end
     
    always@(posedge clk or negedge rst_n) begin
        if(~rst_n)
            match = 0;
        else
            match = state==NINE;
    end
   
endmodule
posted @ 2022-07-03 19:51  220nf绿波电龙  阅读(118)  评论(0编辑  收藏  举报