注意事项:1,不重叠检测,也就是一旦出错立马回到空状态; 2,不满足时还需要输出不满足信号; 3,满足信号和不满足信号都维持一个时钟周期;
我们使用状态机来写;
`timescale 1ns/1ns
module sequence_detect(
input clk,
input rst_n,
input data,
output reg match,
output reg not_match
);
parameter s0 = 1;
parameter s1 = 1;
parameter s2 = 2;
parameter s3 = 3;
parameter s4 = 4;
parameter s5 = 5;
parameter s6 = 6;
reg[3:0]c_state;
reg[3:0]n_state;
always@ (posedge clk or negedge rst_n)
if (!rst_n)
c_state <= s0;
else
c_state <= n_state;
always@ (*)
if (!rst_n)
n_state = s0;
else
begin
case(c_state)
s0: n_state = !data ? s1 : s0 ;
s1: n_state = data ? s2 : s0 ;
s2: n_state = data ? s3 : s0 ;
s3: n_state = data ? s4 : s0 ;
s4: n_state = !data ? s5 : s0 ;
s5: n_state = !data ? s6 : s0 ;
s6: n_state = s0 ;
default: n_state = s0;
endcase
end
always @ (*)
if (!rst_n)
begin
match = 0;
not_match = 0;
end
else if (c_state == 4'd6)
begin
match = 1;
not_match = 0;
end
else
begin
match = 0;
not_match = 1;
end
endmodule
显示不通过: 好家伙,问题大了;改改 测试脚本试试: 状态机压根不动,parameter s0 = 1;问题出在这,改了之后还是01跳; 好了 破案了 我的激励加错了;
前边一直忽略了一个问题就是match和notmatch都是在6个数据之后,才输出一个周期的,不是电平信号; 所以我加入一个加法器,循环计数。当计数到6就输出一次;
`timescale 1ns/1ns
module sequence_detect(
input clk,
input rst_n,
input data,
output reg match,
output reg not_match
);
parameter s0 = 0;
parameter s1 = 1;
parameter s2 = 2;
parameter s3 = 3;
parameter s4 = 4;
parameter s5 = 5;
parameter s6 = 6;
reg[3:0]c_state;
reg[3:0]n_state;
always@ (posedge clk or negedge rst_n)
if (!rst_n)
c_state <= s0;
else
c_state <= n_state;
reg [3:0] cnt;
always@ (posedge clk or negedge rst_n)
if (!rst_n)
cnt <= 0;
else if(cnt < 4'd6)
cnt <= cnt + 1;
else
cnt <= 0;
always@ (*)
if (!rst_n)
n_state = s0;
else
begin //011 100
case(c_state)
s0: n_state = !data ? s1 : s0 ;
s1: n_state = data ? s2 : s0 ;
s2: n_state = data ? s3 : s0 ;
s3: n_state = data ? s4 : s0 ;
s4: n_state = !data ? s5 : s0 ;
s5: n_state = !data ? s6 : s0 ;
s6: n_state = s0 ;
default: n_state = s0;
endcase
end
always @ (posedge clk or negedge rst_n)
if (!rst_n)
begin
match <= 0;
not_match <= 0;
end
else if (cnt == 4'd6)
if (c_state == 4'd6)
begin
match <= 1;
not_match <= 0;
end
else
begin
match <= 0;
not_match <= 1;
end
else
begin
match <= 0;
not_match <= 0;
end
endmodule
还是不行,我的输出match慢了一个周期,我改为always@*试试; 好了我尽力了; 看我自己的图:
结束;
其实用移位寄存器会简单一点;