序列检测
a是一位的信号,检测连续的一串信号
遍历法

从第一位往后不断和目标串比较
`timescale 1ns/1ns
module sequence_detect(
input clk,
input rst_n,
input a,
output reg match
);
reg [7:0] a_tem;
always @(posedge clk or negedge rst_n)
if (!rst_n)
begin
match <= 1'b0;
end
else if (a_tem == 8'b0111_0001)
begin
match <= 1'b1;
end
else
begin
match <= 1'b0;
end
always @(posedge clk or negedge rst_n)
if (!rst_n)
begin
a_tem <= 8'b0;
end
else
begin
a_tem <= {a_tem[6:0],a};
end
endmodule
报错


不用管是不是阻塞非阻塞的含义了,难以理解,用了一定不会出问题,不用可能出问题,分析不出来
含有无关项的序列检测

还是和之前一样,保留一串,不过检测的时候分为两部分,两部分检测都通过了就表示匹配成功
`timescale 1ns/1ns
module sequence_detect(
input clk,
input rst_n,
input a,
output match
);
reg [8:0] a_tem;
reg match_f;
reg match_b;
always @(posedge clk or negedge rst_n)
if (!rst_n)
begin
match_f <= 1'b0;
end
else if (a_tem[8:6] == 3'b011)
begin
match_f <= 1'b1;
end
else
begin
match_f <= 1'b0;
end
always @(posedge clk or negedge rst_n)
if (!rst_n)
begin
match_b <= 1'b0;
end
else if (a_tem[2:0] == 3'b110)
begin
match_b <= 1'b1;
end
else
begin
match_b <= 1'b0;
end
always @(posedge clk or negedge rst_n)
if (!rst_n)
begin
a_tem <= 9'b0;
end
else
begin
a_tem <= {a_tem[7:0],a};
end
assign match = match_b && match_f;
endmodule
需要注意

这里判断的时候,从左至右依次对应的是切片的高位到低位,而不是直觉上理解
报错


需要注意的是,切片必须左位为高位,右侧为低位,好像不能颠倒


之所以报错,是因为情况考虑的还不完善
++完整的判断逻辑应该是,是否为复位信号,是了直接复位++
++不是,判断是否满足相应条件,满足应该怎么办++
++不满足又该怎么办++
如果只考虑了满足,而没有考虑不满足的情况,就会导致不满足时,锁存的时满足的情况
或者说是,一旦满足了,在没有考虑不满足的情况时,就会一直锁存的是满足时的情况,就不会再发生变化了,所以就会一直输出1,发生错误
这里就是因为只写了满足条件时的语句,没写不满足时的,导致不满足时发生错误,没有相应的条件判断,使其锁存为之前的状态
而且,最好还是别合在一起写进同一个always里了,非常容易漏情况,最好还是一个always里就确定好一个量就行
always模板
1.直接赋值,无条件判断
一般不会在这里出现问题
always@(posedge clk,negedge rst_n)begin
if(!rst_n)begin
temp<=9'b0;
end
else begin
temp<={temp[7:0],a};
end
end
always@(posedge clk,negedge rst_n)begin
if(!rst_n)begin
复位时的情况,一般为0
end
else begin
正常工作时的赋值语句
end
2.需要条件判断的
漏写情况就会出问题,概率很大
always@(posedge clk,negedge rst_n)begin
if(!rst_n)begin
mb<='b0;
end
else begin
if(temp[2:0]==3'b110)
mb<=1'b1;
else
mb<=1'b0;
end
end
always@(posedge clk,negedge rst_n)begin
if(!rst_n)begin
复位时的情况,一般为0
end
else begin
if(判断条件1)begin
满足时的情况
end
else if(判断条件2)begin
满足时的情况
end
else begin//必须要写,不写就会在都不满足时锁存上一次满足的情况的结果,导致出问题
都不满足时的情况
end
end
end
三目使2转1
值得注意的是,当只有一个判断条件时,可以用三目判别式,进而用1来实现,如

3.涉及case
同2差不多,就是需要注意都不满足时default的情况,不然一旦出现未知情况,就会锁存上一次的值,出大问题
如果十分确定会出现的所有情况,可以自信的不写
需要注意的点
1.必须要用<=,不要试图去分析到底用=还是<=,只要always,都用<=,无脑,好用,不出错
2.一个always块里只设置一个变量的值,不要试图一个always里干好多事情,不然大概率会出错
不重叠序列检测

此时就不能前两个的遍历法,因为只要不匹配就直接要判否,而不能回到之前的状态

判否的时候也不能阻止剩余部分的输入,这时候需要一个cnt
状态机每6个一循环
