next up previous contents
Next: Arithmetic operators Up: Expressions Previous: Conditional operators (``if'', ``case''   Contents

Representing state machines using conditionals

As an example, suppose we have a state machine with one boolean input ``choice'', that starts in state ``idle'', then depending on ``choice'' goes to either state ``left'' or ``right'', and finally returns to state ``idle''. Using a ``case'' expression, we could write:

        next(state) :=
          case{
            state = idle : choice ? left : right;
            default : idle;
          };

The equivalent using a switch statement would be:

        next(state) :=
          switch(state){
            idle : choice ? left : right;
            default : idle;
          };

The values in a switch statement can also be ``tuples'' (lists of expressions separated by commas, see section on tuples). Using this notation, we can write the above state machine as

        next(state) :=
          switch(state,choice){
            (idle,             1)    : left;
            (idle,             0)    : right;
            ({left,right}, {0,1})    : idle;
          };

If we want to add outputs ``left_enable'' and ``right_enable'' to our state machine, to indicate that the state is ``left'' and ``right'' respectively, we can use a switch expression that returns a tuple. Thus:

        (next(state),left_enable,right_enable) :=
          switch(state,choice){
            (idle,            1)     : (left,  0, 0);
            (idle,            0)     : (right, 0, 0);
            (left,        {0,1})     : (idle,  1, 0);
            (right,       {0,1})     : (idle,  0, 1);
          };
This provides a fairly succinct way of writing the truth table of a state machine, with current state and inputs on the left, and next state and outputs on the right.



2002-10-28