题解 |使用寄存器判断的 #含有无关项的序列检测#
含有无关项的序列检测
https://www.nowcoder.com/practice/cba67d06d6834a5d9b93e1087b56c8d8
//灵感来源FPGA探索者的视频解析https://www.bilibili.com/video/BV1KL4y1E7GG
//使用寄存器和计数器,原解只能每9bit数据检测一次,如果间隔有其他小于9bit的错误数据,那么正确序列到来后将无法改动。
//主要核心实现:compare_reg <= {compare_reg[7:0], a};
//特别注意!!!这里不可以使用
//compare_reg[0] <= a;
//compare_reg <= compare_reg << 1'b1;
//非阻塞赋值会有延迟,虽然可以编译但是仿真的时候会发现compare_reg无法存储任何值。
//
`timescale 1ns/1ns
module sequence_detect(
input clk,
input rst_n,
input a,
output reg match
);
reg [8:0]compare_reg;
reg [3:0]cnt;
reg flag;
always @ (posedge clk or negedge rst_n) begin
if (~rst_n)begin
match <= 1'b0;
compare_reg <= 9'b0;
flag <= 1'b0;
cnt <= 3'b0;
end
else begin
compare_reg <= {compare_reg[7:0], a};
if(cnt == 3'b111)
flag <= 1'b1;
else cnt <= cnt + 1'b1;
if( flag == 1'b1)
if(compare_reg[8:6] == 3'b011 && compare_reg[2:0] == 3'b110)
match <= 1'b1;
else match <= 1'b0;
end
end
endmodule
#verilog刷题记录#
查看4道真题和解析