题解 | #不重叠序列检测#
不重叠序列检测
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]cs;
reg [2:0]ns;
parameter S0=3'd0;
parameter S1=3'd1;
parameter S2=3'd2;
parameter S3=3'd3;
parameter S4=3'd4;
parameter S5=3'd5;
parameter S6=3'd6;
reg [2:0] cnt;
always @(posedge clk or negedge rst_n)begin
if(~rst_n)
cnt<=0;
else if(cnt==5)
cnt<=0;
else
cnt<=cnt+1;
end
always @(posedge clk or negedge rst_n)begin
if(~rst_n)
cs<=0;
else
cs<=ns;
end
always@(*)begin
case(cs)
S0:begin
if(data==0&&cnt==0)
ns=S1;
else
ns=S0;
end
S1:begin
if(data==1)
ns=S2;
else
ns=S0;
end
S2:begin
if(data==1)
ns=S3;
else
ns=S0;
end
S3:begin
if(data==1)
ns=S4;
else
ns=S0;
end
S4:begin
if(data==0)
ns=S5;
else
ns=S0;
end
S5:begin
if(data==0)
ns=S6;
else
ns=S0;
end
S6:begin
ns=S0;
end
default :ns=S0;
endcase
end
always @(posedge clk or negedge rst_n)begin
if(~rst_n)
match<=0;
else if(ns==S6)
match<=1;
else
match<=0;
end
always @(posedge clk or negedge rst_n)begin
if(~rst_n)
not_match<=0;
else if(cnt==5)
if(cs==S5)
not_match<=0;
else
not_match<=1;
else
not_match<=0;
end
endmodule
不重叠检测,按题干要求加入了计数器,当第一位不满足的时候,计满6个数以后才能检测,不满足就一直待在S0状态,同理,在检测过程中的时候,有一位不满足就说明本次检测失败了,直接回到起始位置。
查看9道真题和解析