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

不重叠序列检测

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
	);

reg [2:0] curr_st;
reg [2:0] next_st;
reg [2:0] cnt;

parameter M0 = 3'b0;
parameter M1 = 3'b01;
parameter M2 = 3'b10;
parameter M3 = 3'b11;
parameter M4 = 3'b100;
parameter M5 = 3'b101;
parameter M6 = 3'b110;

always @(posedge clk or negedge rst_n)begin
   if(rst_n == 1'b0)
      curr_st <= M0;
	else
	   curr_st <= next_st;
end

always@(*)begin
case(curr_st)
    M0:
        if(data == 1'b0)
	        next_st = M1;
		else
			next_st = M0;
	M1:
	    if(data == 1'b1)
		    next_st = M2;
		else
		   next_st = M0;
	M2:
	    if(data == 1'b1)
		    next_st = M3;
		else
		    next_st = M0;
	M3:
	    if(data == 1'b1)
		   next_st = M4;
		else
		   next_st = M0;
	M4:
	    if(data == 1'b0)
		    next_st = M5;
		else
		   next_st = M0;
	M5:
	    if(data == 1'b0)
		    next_st = M6;
		else
		    next_st = M0;
	M6: if(data == 1'b0)
	        next_st = M1;
		else
		    next_st = M0;  
endcase	
end


always@(posedge clk or negedge rst_n)begin
	if(rst_n == 1'b0)
	   cnt <= 'b0;
	else if(cnt < 3'b101)
	   cnt <= cnt + 1'b1;
	else
	   cnt <= 'b0;
end

always@(posedge clk or negedge rst_n)begin
	if(rst_n == 1'b0)begin
	   match <= 0;
	   not_match<=0;
	end	   
	else if(cnt == 3'b101 && next_st == M6)begin
	   match <= 1;
	   not_match <= 0;
	end
	else if(cnt == 3'b101 && next_st != M6)begin
	   match <= 0;
	   not_match <= 1;
	end
	else begin
		match <= 0;
	    not_match<=0;
	end
end


endmodule

全部评论

相关推荐

兄弟们,实习都是在接各种api,该怎么包装简历
仁者伍敌:感觉我自己做小项目也是各种api啊,我要怎么包装简历
点赞 评论 收藏
分享
06-26 17:24
已编辑
宁波大学 golang
迷失西雅图:别给,纯kpi,别问我为什么知道
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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