//*******************************************************************************************
//state transfer: input(state and din), output(state)
//*******************************************************************************************
always @( posedge clk )
if(rst) begin
state <= 2'd0;
cnt_high <= 6'd0;
cnt_low <= 6'd0;
end
else begin
case(state)
2'd0: begin
if(din) begin
cnt_low <= 6'd0;
if(cnt_high == `time_1ms) begin
cnt_high <= 6'd0;
state <= 2'd1;
end
else
cnt_high <= cnt_high + 1'b1;
end
else begin
cnt_high <= 6'd0;
if(cnt_low == `time_1ms) begin
cnt_low <= 6'd0;
state <= 2'd2;
end
else
cnt_low <= cnt_low + 1'b1;
end
end
2'd1: begin
if(!din) begin
cnt_high <= 6'd0;
if(cnt_low == `time_1ms) begin
cnt_low <= 6'd0;
state <= 2'd2;
end
else
cnt_low <= cnt_low + 1'b1;
end
else begin
cnt_low <= 6'd0;
end
end
2'd2: begin
if(din) begin
cnt_low <= 6'd0;
if(cnt_high == `time_1ms) begin
cnt_high <= 6'd0;
state <= 2'd1;
end
else
cnt_high <= cnt_high + 1'b1;
end
else begin
cnt_high <= 6'd0;
end
end
default: ;
endcase
end
//*******************************************************************************************
//give a value to dout according to state
//*******************************************************************************************
always @( posedge clk )
if(rst)
dout <= 1'b0;
else
case(state)
2'd0: dout <= 1'b0;
2'd1: dout <= 1'b1;
2'd2: dout <= 1'b0;
default: ;
endcase