`timescale 1ns/1ns

module fsm1(
	input wire clk  ,
	input wire rst  ,
	input wire data ,
	output reg flag
);
//*************code***********//
parameter	s0	=	2'b00,
			s1	=	2'b01,
			s2	=	2'b10,
			s3	=	2'b11;

reg	[1:0]	cur_state,nxt_state;

always@(posedge	clk or negedge rst)
	if(!rst)
		cur_state	<=	s0;
	else	
		cur_state	<=	nxt_state;

always@(*)	begin
	if(!rst)
		nxt_state	<=	s0;
	else	case(cur_state)	
		s0:begin
			if(data==1'b1)
				nxt_state	<=	s1;
			else
				nxt_state	<=	s0;
		end
		s1:begin
			if(data==1'b1)
				nxt_state	<=	s2;
			else
				nxt_state	<=	s1;
		end
		s2:begin
			if(data==1'b1)
				nxt_state	<=	s3;
			else
				nxt_state	<=	s2;			
		end	
		s3:begin
			if(data==1'b1)
				nxt_state	<=	s0;
			else
				nxt_state	<=	s3;			
		end
	default:	nxt_state	<=	s0;
	endcase	
end

always@(posedge clk or negedge rst)	begin
	if(!rst)
		flag	<=	1'b0;
	else	case(cur_state)
		s3:begin
			if(data==1'b1)
				flag	<=	1'b1;
			else	
				flag	<=	1'b0;
		end
	
	default:	flag	<=	1'b0;
	endcase
end

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