基于System Verilog的序列检测器

本文通过system verilog,实现了一个10010序列检测器

状态机设计

状态机是数字电路设计中一个十分重要的概念,许多复杂的控制都可以通过状态机完成,本文要实现的10010序列检测器,同样也可以通过状态机来实现。
下面是状态机设计的思路:
S0表示当前检测到的序列为0,如果检测到1,则转换到S1,否则保持原状态不变;
S1表示当前检测到的序列为1,如果检测到1,则保持状态不变,如果检测到0,则跳到状态S2;
S2表示当前检测的序列为10,如果检测到1,则跳转到S1,否则跳转至S3;
S3表示当前检测到的序列为100,如果检测到1,则跳转至S4,否则跳转至S0;
S4表示当前检测到的序列为1001,如果检测到0,则跳转到S5,否则跳转至S1。
S5表示当前检测到的序列为10010,如果检测到0,则跳到S0,否则跳转至S1
状态机如下图所示:
基于System Verilog的序列检测器_第1张图片

RTL实现

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2022/02/23 13:50:20
// Design Name: 
// Module Name: Sequence_detection
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module Sequence_detection(
input logic clk,
input logic rst,
input logic bit_in,
output logic detected
    );

typedef enum bit [2:0] 
{
S0,
S1,
S2,
S3,
S4,
S5}
FSM_STATE;
//
FSM_STATE cur_state,next_state;
always_ff@(posedge clk,posedge rst)
if(rst)
    cur_state<=S0;
else 
    cur_state<=next_state;
//
always_comb
begin
    case(cur_state)
        S0:if(bit_in)
               next_state=S1;
           else
               next_state=S0;
        S1:if(bit_in)
               next_state=S1;
           else
               next_state=S2;           //10
        S2:if(bit_in)
               next_state=S1;
            else
               next_state=S3;           //100
        S3:if(bit_in)
               next_state=S4;            //1001
            else
               next_state=S0;
        S4:if(bit_in)
                next_state=S1;
            else
                next_state=S5;           //10010
        S5:if(bit_in)
                next_state=S1;
            else
                next_state=S0;
        default:next_state=S0;
    endcase
end
//
assign detected=(cur_state==S5)?1'b1:1'b0;
endmodule

测试平台,利用随机数产生输入序列

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2022/02/23 14:00:13
// Design Name: 
// Module Name: test_tb
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module test_tb;
logic clk;
logic rst;
logic bit_in;
logic detected;
initial begin
    clk=0;
    forever begin
        #5 clk=~clk;
    end
end
initial
begin
    rst=1;
    #20
    rst=0;
end
//
always_ff@(posedge clk,posedge rst)
if(rst)
    bit_in<=0;
else 
    bit_in<=$random%2;

Sequence_detection U(.*);
// input logic clk,
// input logic rst,
// input logic bit_in,
// output logic detected
//     );
endmodule

仿真波形示意:
基于System Verilog的序列检测器_第2张图片

你可能感兴趣的:(FPGA,IC相关,FPGA,数字IC设计)