题解 | #不重叠序列检测#
不重叠序列检测
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 ); parameter s0=0,s1=1,s2=2,s3=3,s4=4,s5=5; parameter sn0=6,sn1=7,sn2=8,sn3=9,sn4=10,sn5=11; parameter idle=12; reg [3:0]cs,ns; always@(posedge clk or negedge rst_n)begin if(!rst_n)begin cs<=idle; end else begin cs<=ns; end end always@(*)begin if(!rst_n)begin ns=s5; match=0; not_match=0; end else begin case(cs) s0:begin ns=(data)?s1:sn1; match=0; not_match=0; end s1:begin ns=data?s2:sn2; match=0; not_match=0; end s2:begin ns=data?s3:sn3; match=0; not_match=0; end s3:begin ns=data?sn4:s4; match=0; not_match=0; end s4:begin ns=data?sn5:s5; match=0; not_match=0; end s5:begin ns=data?sn0:s0; match=1; not_match=0; end sn0:begin ns=sn1; match=0; not_match=0; end sn1:begin ns=sn2; match=0; not_match=0; end sn2:begin ns=sn3; match=0; not_match=0; end sn3:begin ns=sn4; match=0; not_match=0; end sn4:begin ns=sn5; match=0; not_match=0; end sn5:begin ns=data?sn0:s0; match=0; not_match=1; end idle:begin ns=data?sn0:s0; match=0; not_match=0; end default:begin ns=idle; match=0; not_match=0; end endcase end end endmodule