题解 | #不重叠序列检测#
不重叠序列检测
https://www.nowcoder.com/practice/9f91a38c74164f8dbdc5f953edcc49cc
`timescale 1ns/1ns
module sequence_detect(
input clk,
input rst_n,
input data,
output reg match,
output reg not_match
);
reg [2:0] cnt;
// 这里计数器的逻辑,需要注意的是,在计数器计到6之后不是清零,而是从1开始计数
// 因为在计数器计数到6之后,下一位数据已经到来了,而不是最初状态那样从0个数据开始等待数据的到来
// 修改以上这一点后,本代码通过编译
// 参考提示:
// https://blog.nowcoder.net/n/6b37797eca5d48abbad279d920cef6c4
always @(posedge clk or negedge rst_n)
begin
if(~rst_n) begin
cnt <= 3'b0;
end
else begin
cnt <= (cnt==3'd6)? 3'b1 : cnt+1'b1;
end
end
reg [5:0] seq;
always @(posedge clk or negedge rst_n)
begin
if(~rst_n)
seq <= 6'b0;
else begin
seq <= {seq[4:0], data};
end
end
// 根据波形图做出的判断没有错,这里确实应该使用组合逻辑来做输出,
// 才能在接收到6bit的数据之后及时地输出信号(比第六位数据的到来延后一个周期)
always @(*) begin
if(~rst_n) begin
match = 1'b0;
not_match = 1'b0;
end
else begin
match = (cnt==3'd6 && seq==6'b011100);
not_match = (cnt==3'd6 && seq!=6'b011100);
end
end
endmodule
查看17道真题和解析