题解 | #状态机-重叠序列检测#

状态机-重叠序列检测

https://www.nowcoder.com/practice/10be91c03f5a412cb26f67dbd24020a9

`timescale 1ns/1ns

module sequence_test2(
	input wire clk  ,
	input wire rst  ,
	input wire data ,
	output reg flag
);
//*************code***********//
parameter IDLE = 0;
parameter SECOND = 1;
parameter THIRD	 = 2 ;
parameter FOURTH = 3 ;
parameter FIFTH  = 4 ;
parameter LAST   = 5 ;
reg			[2:0]		cur_state ;
reg			[2:0]		next_state;
always @(posedge clk or negedge rst) begin
	if(!rst) begin
		cur_state <= IDLE ;
	end
	else begin
		cur_state <= next_state ;
	end
end
always @(*) begin
	case(cur_state)
		IDLE: begin
			if(data) begin
				next_state = SECOND ;
			end
			else begin
				next_state = IDLE ;
			end
		end
		SECOND: begin
			if(!data) begin
				next_state = THIRD ;
			end
			else begin
				next_state = SECOND ;
			end
		end
		THIRD: begin
			if(data) begin
				next_state = FOURTH ;
			end
			else begin
				next_state = IDLE ;
			end
		end	
		FOURTH: begin
			if(data) begin
				next_state = LAST ;
			end
			else begin
				next_state = THIRD ;
			end
		end		
		LAST: begin
			if(data) begin
				next_state = SECOND ;
			end
			else begin
				next_state = THIRD ;
			end
		end	
	endcase
end
always @(posedge clk or negedge rst) begin
	if(!rst) begin
		flag <= 1'b0 ;
	end
	else if(cur_state==LAST ) begin
		flag <= 1'b1 ;
	end
	else begin
		flag <= 1'b0 ;
	end
end

//*************code***********//
endmodule

全部评论

相关推荐

点赞 评论 收藏
分享
10-17 12:16
同济大学 Java
7182oat:快快放弃了然后发给我,然后让我也泡他七天最后再拒掉,狠狠羞辱他一把😋
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务