1: // Example of a state machine: an R-S flip flop.  05/25/98  r@elilabs.com 

  3: #include "rs_ff_session.H"
  4: #include "rs_ff_state.H"


  7: // An active instance of a state machine is a session traversing a
  8: // state graph.  A state machine is created by injecting a session
  9: // into a state of a state graph.  A state machine proceeds by
 10: // responding to events.  Events are messages passed by function call.
 11: // Every session object has a virtual member function corresponding to
 12: // every possible event in the state graph's domain.


 15: // Make a state graph to simulate the behavior of an RS flip-flop.
 16: //
 17: //
 18: //
 19: //         LOGIC SYMBOL                    GATE LEVEL IMPLEMENTATION
 20: //
 21: //
 22: //         >------+
 23: //                |
 24: //                |                                   ______
 25: //              __o___                               |      \
 26: //             |      |                 S >----------|       \
 27: //             |  S   |                              | NAND   )o---+-----> Q
 28: //             |    Q |----->                    +---|       /     |
 29: //             |      |                          |   |______/      |
 30: //             |      |                          |                 |
 31: //             |    _ |                          +----\ /----------+
 32: //             |    Q |----->                          X
 33: //             |  R   |                          +----/ \----------+
 34: //             |______|                          |    ______       |
 35: //                o                              |   |      \      |
 36: //                |                              +---|       \     |       _
 37: //                |                                  | NAND   )o---+-----> Q
 38: //         >------+                     R  >---------|       /
 39: //                                                   |______/
 40: //
 41: //
 42: //
 43: //
 44: //                            STATE TRANSITION DIAGRAM
 45: //
 46: //
 47: //                                      _
 48: //                  _______             S             __________
 49: //                 /       \------------------------>/          \
 50: //                (  Set_S  )                       (  Set_Sbar  )
 51: //                 \_______/<------------------------\__________/
 52: //                     |                S         _  /  ^ ^
 53: //                     |                          R /   | |
 54: //                     |            __________     /   /  |
 55: //                   _ |     /-----/          \<--'   /   | _
 56: //                   R |  S /     (  Squashed  )     / R  | S
 57: //                     |   /    -->\__________/-----'     |
 58: //                     |  /    /_                         |
 59: //                     | |    / S         _               |
 60: //                  ___v_v___/__          R           ____|____
 61: //                 /            \<-------------------/         \
 62: //                (  Reset_Rbar  )                  (  Reset_R  )
 63: //                 \____________/------------------->\_________/
 64: //                                        R
 65: //
 66: //
 67: //
 68: //                              OUTPUT TRUTH TABLE
 69: //                                                        _
 70: //                       State        Output Q     Output Q
 71: //                       -----        --------     --------
 72: //                       Set_S           T            F
 73: //                       Set_Sbar        T            F
 74: //                       Squashed        T            T
 75: //                       Reset_Rbar      F            T
 76: //                       Reset_R         F            T
 77: //
 78: //
 79: //
 80: //                            STATE TRANSITION TABLE 
 81: //
 82: //                     \ Events         _                       _
 83: //             States   \   S           S           R           R 
 84: //             ----------\-------------------------------------------------
 85: //             Set_S      | ignored     Set_Sbar    ignored     Reset_Rbar
 86: //             Set_Sbar   | Set_S       ignored     ignored     Squashed
 87: //             Squashed   | Reset_Rbar  ignored     Set_Sbar    ignored
 88: //             Reset_Rbar | ignored     Squashed    Reset_R     ignored
 89: //             Reset_R    | ignored     Set_Sbar    ignored     Reset_Rbar


 92: // The main line entry point.
 93: //
 94: int main(int nargs, char* arg[]) {
 95:   int x = 0;

 97:   // THE STATE NODES
 98:   //
 99:   rs_ff_state Set_S;
100:   rs_ff_state Set_Sbar;
101:   rs_ff_state Squashed;
102:   rs_ff_state Reset_Rbar;
103:   rs_ff_state Reset_R;

105:   // THE STATE TRANSITION GRAPH
106:   //
107:   //                Output    Guard             Next State
108:   //                --------  ----------------  -------------------------------------------------
109:   // State          Q   Qbar  S  Sbar  R  Rbar  S             Sbar          R             Rbar
110:   // ------         -   ----  -  ----  -  ----  ----------    ----------    ----------    ----------   
111:   Set_S      .set(  T,  F,    F, T,    F, T,    &Set_S,       &Set_Sbar,    &Set_S,       &Reset_Rbar );
112:   Set_Sbar   .set(  T,  F,    T, F,    F, T,    &Set_S,       &Set_Sbar,    &Set_Sbar,    &Squashed   );
113:   Squashed   .set(  T,  T,    T, F,    T, F,    &Reset_Rbar,  &Squashed,    &Set_Sbar,    &Squashed   );
114:   Reset_Rbar .set(  F,  T,    F, T,    T, F,    &Reset_Rbar,  &Squashed,    &Reset_R,     &Reset_Rbar );
115:   Reset_R    .set(  F,  T,    F, T,    F, T,    &Reset_R,     &Set_Sbar,    &Reset_R,     &Reset_Rbar );
116:   //
117:   // NOTES: 
118:   //        1. The '.set' method establishes initial values for a state node.
119:   //        2. Exits marked "ignored" are implemented by FALSE guards.

121:   // An RS flip-flop in an indeterminate state, such as during power-up.
122:   //
123:   rs_ff_session flop(&Squashed);        

125:   flop.S();                        // raise S -- go to Reset_Rbar -- Q = lo; Qbar = hi;
126:   flop.Sbar();                        // lower S -- back to squashed -- Q = Qbar = hi;

128:   flop.R();                        // raise R -- go to Set_Sbar -- Q = hi; Qbar = lo;
129:   flop.Rbar();                        // lower R -- back to squashed -- Q = Qbar = hi;

131:   flop.S();                        // raise S -- go to Reset_Rbar -- Q = lo; Qbar = hi;
132:   flop.R();                        // raise R -- go to Reset_R -- Q = lo; Qbar = hi;

134:   flop.Rbar();                        // lower R -- go to Reset_Rbar -- Q = lo; Qbar = hi;
135:   flop.R();                        // raise R -- go to Reset_R -- Q = lo; Qbar = hi;

137:   flop.Sbar();                        // lower S -- go to Set_Sbar -- Q = hi; Qbar = lo;
138:   flop.S();                        // raise S -- go to Set_S -- Q = hi; Qbar = lo;

140:   flop.Rbar();                        // lower R -- go to Reset_Rbar -- Q = lo; Qbar = hi;
141:   flop.R();                        // raise R -- go to Reset_R -- Q = lo; Qbar = hi;

143:   return x;
144: }