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

不重叠序列检测

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

全部评论

相关推荐

05-30 12:03
山西大学 C++
offer来了我跪着...:不是骗子,等到测评那一步就知道为啥这么高工资了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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