CSCI 255 Finite State Machines

String recognition

Let’s start by designing a finite state machine that turns on a light if its input sequence contains the three consecutive bits 101 anywhere within the input seen so far. For example, the light is on after 0010100 or 1101 but not after 100100100100. Note that once the light goes on, it stays on.

Get out your pencils to help out.

You can also try out the following command on your Ubuntu system.

egrep "^(0|1)*101(0|1)*$"

You can also try out a Logisim implementation of a solution. Warning: This is hard to use the first time. You have to be very careful about the clock input.

Real programmers don’s write string recognition code. They use modules like Java’s Pattern class or Python’s re module or JavaScript’s RegExp objects.

Transmission Control Protocol (TCP)

If you look at RFC 793, the Transmission Control Protocol specification you will find an FSM specification of the three-way handshake used to establish a TCP connection. This FSM, included below, is evaluated every time a web page is loaded.

                              +---------+ ---------\      active OPEN  
                              |  CLOSED |            \    -----------  
                              +---------+<---------\   \   create TCB  
                                |     ^              \   \  snd SYN    
                   passive OPEN |     |   CLOSE        \   \           
                   ------------ |     | ----------       \   \         
                    create TCB  |     | delete TCB         \   \       
                                V     |                      \   \     
                              +---------+            CLOSE    |    \   
                              |  LISTEN |          ---------- |     |  
                              +---------+          delete TCB |     |  
                   rcv SYN      |     |     SEND              |     |  
                  -----------   |     |    -------            |     V  
 +---------+      snd SYN,ACK  /       \   snd SYN          +---------+
 |         |<-----------------           ------------------>|         |
 |   SYN   |                    rcv SYN                     |   SYN   |
 |   RCVD  |<-----------------------------------------------|   SENT  |
 |         |                    snd ACK                     |         |
 |         |------------------           -------------------|         |
 +---------+   rcv ACK of SYN  \       /  rcv SYN,ACK       +---------+
   |           --------------   |     |   -----------                  
   |                  x         |     |     snd ACK                    
   |                            V     V                                
   |  CLOSE                   +---------+                              
   | -------                  |  ESTAB  |                              
   | snd FIN                  +---------+                              
   |                   CLOSE    |     |    rcv FIN                     
   V                  -------   |     |    -------                     
 +---------+          snd FIN  /       \   snd ACK          +---------+
 |  FIN    |<-----------------           ------------------>|  CLOSE  |
 | WAIT-1  |------------------                              |   WAIT  |
 +---------+          rcv FIN  \                            +---------+
   | rcv ACK of FIN   -------   |                            CLOSE  |  
   | --------------   snd ACK   |                           ------- |  
   V        x                   V                           snd FIN V  
 +---------+                  +---------+                   +---------+
 |FINWAIT-2|                  | CLOSING |                   | LAST-ACK|
 +---------+                  +---------+                   +---------+
   |                rcv ACK of FIN |                 rcv ACK of FIN |  
   |  rcv FIN       -------------- |    Timeout=2MSL -------------- |  
   |  -------              x       V    ------------        x       V  
    \ snd ACK                 +---------+delete TCB         +---------+
     ------------------------>|TIME WAIT|------------------>| CLOSED  |
                              +---------+                   +---------+

You can find an implementation, written in C, of this FSM in every operating system that supports Internet Protocol (IP) networking.

You can also find uses of finite state machines in game development.

The Vending Machine

The vending machine is the most common classroom FSM example. Just do a Google image search for “finite state machine vending machine” to see hundreds of examples. The implementation of a vending machine with a finite state machine was once a common topic in the second programming course.

In this class, we’ll use my Nabs machine example, which is similar to Joe Daugherty’s CSCI 202 example. We will also try out an implementation of Nabs255 in either logisim or logisim-evolution.

By the way, the implementation was optimized by NOVA. The output of nova was in BLIF.

Old homework

Finally, let’s look at a homework assignment from the Fall 2015 semester.