题解 | #不重叠序列检测#

不重叠序列检测

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 idle=0,a=1,b=2,c=3,d=4,e=5,f=6,g=7;
	reg [2:0]state,next;
	reg [2:0]cnt;
	always@(*)
		begin
			case(state)			
				a: next=data?idle:b;
				b: next=data?c:idle;
				c: next=data?d:idle;
				d: next=data?e:idle;
				e: next=data?idle:f;
				f: next=data?idle:g;
				g: next=data?idle:a;
				idle: next=(cnt==5)?a:idle;
			endcase
		end
	always@(posedge clk,negedge rst_n)
		begin
			if(!rst_n)
				state<=a;
			else
				state<=next;
		end
	always@(posedge clk,negedge rst_n)
		begin
			if(!rst_n)
				cnt<=0;
			else if(cnt==5)
				cnt<=cnt;
			else if(next==idle)
				cnt<=cnt+1;
		end
	always@(*)
		begin
			// if(!rst_n)begin
			// 	match<=0;
			// 	not_match<=0;end
			// else 
			// begin
				if(state==g)
					match<=1;					
				else
					match<=0;
				if(state==idle&cnt==5)
					not_match<=1;
				else
					not_match<=0;
			// end
		end
endmodule

全部评论

相关推荐

头像 会员标识
12-16 14:43
浙江大学 Java
投递牛客等公司6个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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