题解 | #不重叠序列检测#
不重叠序列检测
https://www.nowcoder.com/practice/9f91a38c74164f8dbdc5f953edcc49cc
`timescale 1ns/1ns
module sequence_detect(
input clk,
input rst_n,
input data,
output wire match,
output wire not_match
);
//--- shift register of data
reg [6:0] reg_data;
always @(posedge clk or negedge rst_n) begin
if(~rst_n)
reg_data <= 7'b000_0001;
else if(reg_data[6] == 1'b1)
reg_data <= {6'b00_0001,data};
else
reg_data <= {reg_data[5:0],data};
end
//--- output match
assign match = ((reg_data[6] == 1'b1) & (reg_data[5:0] == 6'b011100)) ? 1 : 0;
//--- output not match
assign not_match = ((reg_data[6] == 1'b1) & (reg_data[5:0] != 6'b011100)) ? 1 : 0;
endmodule
@牛客503184514号 网友的解法,很简洁(不过,修改了输出port的类型 reg->wire,看来系统不检测这个,只有输入输出port数量和位数对就OK :) )
另,有个笔误: else if(reg_data[7] == 1'b1) -----> else if(reg_data[6] == 1'b1)
~~ ~~
深圳虾皮信息科技有限公司公司福利 843人发布