题解 | #含有无关项的序列检测#
含有无关项的序列检测
https://www.nowcoder.com/practice/cba67d06d6834a5d9b93e1087b56c8d8
`timescale 1ns/1ns
module sequence_detect(
input clk,
input rst_n,
input a,
output reg match
);
reg [8:0] seq;
// 数组位移
always @(posedge clk or negedge rst_n)
begin
if(~rst_n) begin
seq <= 9'b0;
end
else begin
seq <= {seq[6:0], a};
end
end
// 判断数列的头部三位和末尾三位是否满足
// 数列中间是无关项
always @(posedge clk or negedge rst_n)
begin
if(~rst_n)
match<= 1'b0;
else
match <= (seq[8:6]==3'b011 && seq[2:0]==3'b110);
end
//========================================================
// 引入casez 进行无关项判断
// 这样用的不是最基本的逻辑门,且当判断的位数多了之后不方便
//========================================================
/*
always @(posedge clk or negedge rst_n)
begin
if(~rst_n) begin
match <= 1'b0;
end
else begin
casez (seq)
9'b011_???_110: match <= 1'b1;
default:
match <= 1'b0;
endcase
// match <= (seq == 9'b011_???_110);
end
end
*/
endmodule
查看4道真题和解析