← ProblemsCircuits / Sequential Logic / Finite State Machines
PS/2 packet parser
15%fsmserial-protocolsThe PS/2 mouse protocol sends messages that are three bytes long. In a continuous byte stream, message boundaries are found using one rule: the first byte of every message has bit 3 set (in[3] = 1); bytes 2 and 3 may have any value.
Design an FSM that watches an incoming byte stream (one byte per clock cycle on in[7:0]) and frames the messages:
- When not inside a message, discard bytes until one arrives with
in[3] = 1. That byte is byte 1 of a message. - The next two bytes are bytes 2 and 3 of the message, unconditionally (their
in[3]values are irrelevant). - Assert
donefor exactly one clock cycle: the cycle immediately after byte 3 is received. - The byte arriving during that
donecycle is already examined as a potential byte 1 of the next message (messages can be back-to-back).
The active-high reset is synchronous and returns the FSM to the searching-for-byte-1 state (with done = 0).
A 4-state machine works nicely: BYTE1 (searching), BYTE2, BYTE3, DONE.