1 题目描述
描述
请编写一个序列检测模块,检测输入信号a是否满足011XXX110序列(长度为9位数据,前三位是011,后三位是110,中间三位不做要求),当信号满足该序列,给出指示信号match。
程序的接口信号图如下:
程序的功能时序图如下:
请使用Verilog HDL实现以上功能,并编写testbench验证模块的功能。 要求代码简洁,功能完整。
输入描述:
clk:系统时钟信号
rst_n:异步复位信号,低电平有效
a:单比特信号,待检测的数据
输出描述:
match:当输入信号a满足目标序列,该信号为1,其余时刻该信号为0
方法一:采用循环左移的方法进行移位操作
`timescale 1ns/1ns
module sequence_detect(
input clk,
input rst_n,
input a,
output match
);
//第一种方法循环左移的方法
//reg define
reg [8:0] check_detect ; //定义一个九位的移位寄存器用于存放数据
reg match_h ; //定义一个高位的序列检测
reg match_l ; //定义一个低位的序列检测器
always @(posedge clk or negedge rst_n) begin
if(!rst_n) begin
match_h <= 0 ; //初始化,
end
else if ( check_detect[8:6] == 3'b011) begin
match_h <= 1; //检测高三位
end
else begin
match_h <= 0 ;
end
end
always @(posedge clk or negedge rst_n) begin
if(!rst_n) begin
match_l <= 0 ; //初始化,
end
else if ( check_detect[2:0] == 3'b110) begin
match_l <= 1; //检测低三位
end
else begin
match_l <= 1'b0 ;
end
end
always @(posedge clk or negedge rst_n) begin
if(!rst_n) begin
check_detect <= 9'b0 ;
end
else begin
check_detect <= {check_detect[7:0],a};
end
end
assign match = match_h&& match_l ;
endmodule
解题思路是将低三位信号和高三位信号进行匹配;如果匹配成功则输出,无关项不用管
方法二:思路是使用状态机,采用三段式状态机进行常规写法,先写出状态转换表用于描述状态的转移规律
`timescale 1ns/1ns
module sequence_detect(
input clk,
input rst_n,
input a,
output reg match
);
parameter idle = 0 ,
s1 = 1,
s2 = 2,
s3 = 3,
s4 = 4,
s5 = 5,
s6 = 6,
s7 = 7,
s8 = 8,
s9 = 9; //定义八个状态
// reg define
reg [7 :0] cur_state ; //定义现态
reg [7 :0] next_state ; //定义次态
always @ (posedge clk or negedge rst_n) begin
if(!rst_n) begin
cur_state <= idle ; //定义现态
end
else begin
cur_state <= next_state ; //次态赋值给现态
end
end
always @ (*) begin
case (cur_state)
idle : next_state = (a == 0) ? s1 : idle ;
s1 : next_state = (a == 0) ? s1 : s2 ;
s2 : next_state = (a == 0) ? s1 : s3 ;
s3 : next_state = s4 ;
s4 : next_state = s5 ;
s5 : next_state = s6 ;
s6 : next_state = (a == 0) ? s1 : s7 ;
s7 : next_state = (a == 0) ? idle : s8 ;
s8 : next_state = (a == 0) ? s9 : idle ;
s9 : next_state = (a == 0) ? s1 : idle ;
default : next_state = idle ;
endcase
end
always @(posedge clk or negedge rst_n) begin
if(!rst_n) begin
match <= 1'b0 ;
end
else case (cur_state)
s9 : match <= 1'b1 ;
default : match <= 1'b0 ;
endcase
end
endmodule
总结:方法一相对于方法二来说更为巧妙与精简,采用循环左移的方法,match信号进行匹配的时候不用与无关项进行匹配,方法二而言:更为常规,其中描述无关项转移的时候可以直接的跳转下一个状态,这一点与上一个题目不同