题解 | #不重叠序列检测#
不重叠序列检测
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
);
// 011100
//S = {a,num} = {a,3'b1} = {,3'b}
parameter S1= {1'b0,3'b1};
parameter S2= {1'b1,3'b010};
parameter S3= {1'b1,3'b011};
parameter S4= {1'b1,3'b100};
parameter S5= {1'b0,3'b101};
parameter S6= {1'b0,3'b110};
parameter S0= {4'b0000};
reg [3:0] data_ray;
reg [2:0] data_num;
reg [3:0] st_now,st_next;
always@(posedge clk or negedge rst_n)
if (~rst_n) begin
data_ray = 0;
data_num = 1;
end
else begin
if (data_num == 7) begin
data_num = 1;
end
data_ray = {data,data_num};
data_num = data_num + 1;
end
always @(posedge clk or negedge rst_n) begin
if (~rst_n) begin
st_now <= S0;
end
else
st_now <= st_next;
end
always @(*) begin
case(st_now)
S0: st_next = (data_ray==S1)?S1:S0;
S1: st_next = (data_ray==S2)?S2:S0;
S2: st_next = (data_ray==S3)?S3:S0;
S3: st_next = (data_ray==S4)?S4:S0;
S4: st_next = (data_ray==S5)?S5:S0;
S5: st_next = (data_ray==S6)?S6:S0;
S6: st_next = (data_ray==S1)?S1:S0;
endcase
end
always @(*) begin
if (~rst_n) begin
match = 0;
not_match = 0;
end
else begin
if (st_next==S6) begin
match = 1;
end
else
match = 0;
if (data_num==7 && st_next==S0)
not_match = 1;
else
not_match = 0;
end
end
endmodule
match时序要比题目1个时钟周期提前才行。