How to write a three-stage state machine for FPGA