题解 | #输入序列不连续的序列检测#

输入序列不连续的序列检测

https://www.nowcoder.com/practice/f96d0e94ec604592b502b0f1800ed8aa

`timescale 1ns/1ns
module sequence_detect(
	input clk,
	input rst_n,
	input data,
	input data_valid,
	output reg match
	);
parameter idle=4,s0=0,s1=1,s2=2,s3=3;
reg[2: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=idle;
		match=0;
	end
	else begin
		case(cs)
		s0:begin
			ns=data_valid?(data?s1:s0):s0;
			match=0;
		end
		s1:begin
			ns=data_valid?(data?s2:s0):s1;
			match=0;
		end
		s2:begin
			ns=data_valid?(data?idle:s3):s2;
			match=0;
		end
		s3:begin
			ns=data_valid?(data?idle:s0):idle;
			match=1;
		end
		idle:begin
			ns=(data_valid& ~data)?s0:idle;
			match=0;
		end
		endcase

	end
end
  
endmodule

全部评论
采用是是二段式状态机,下面是对应的状态转移图
点赞 回复 分享
发布于 2023-09-19 10:53 陕西

相关推荐

不愿透露姓名的神秘牛友
昨天 20:30
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务