module debouncer(
	input clk,rst_n,pushbutton,  //push-button is active-low
	output key                   //output is active high for easier use with other modules
	); 

	reg pushbutton_1=1'b0;       
	reg[31:0] cnt=32'd0;         //debounce timer   
	assign key=pushbutton_1;

	always @(posedge clk,negedge rst_n)
	begin
		if(!rst_n) 
			begin 
				pushbutton_1<=1'b0; 
				cnt<=32'd0;
			end
		else if(pushbutton_1!=(!pushbutton)) 
			begin 
				pushbutton_1<=(cnt==32'd500_000)?(!pushbutton_1):pushbutton_1; //pushbutton must stabilize for 500_000 cycles(10ms for 50MHz) before output changes
				cnt<=(cnt==32'd500_000)?20'd0:(cnt+1'b1);
			end
		else cnt<=32'd0;
	end

	
endmodule