A Compact State Machine Engine by Ron Braithwaite

Basically there is a state variable, holding the current state, and a table. The first part of the table has a 16 bit offset to the start of the entries for the current state. The state entries have an input mask, an input match which is compared to the AND value of the input and the mask, the next state (if there is a match), and a subroutine that is executed and passed the input value. It is essential that the last state entry for every state have a mask and match of zero, with the subroutine an error handler. I always coded this in assembler for speed. The assembler pseudo-code looked something like this, with the assumption of 16 bit input and 16 bit offsets: {Ed: Ron is quoting from memory here}

move.w inputValue, r1
move.w currentState, r2
leftshift #4, r2 ; multiply by 16
move.w (r2+tableBase), r3
$1: move.w (r3)+, r4
and.w r1, r4
cmp.w r4, (r3)+ ; r3 pointed at inputMatch, inc to nextState
jmp.eq $2
add #ENTRYOFFSET, r3 ; offset to start of next state entry
jmp $1
$2: move.w (r3)+, r2
move.w r2, currentState
call (r3) ; subroutine must end with a return
return
This is pretty fast and can handle pretty much anything. If this is the core of an interrupt service routine, don't forget to turn interrupts back on as soon as possible.