# CSCI 255 Finite State Machines

In these lectures we are going to learn about finite state machines (FM’s), both their design and implementation, using three examples.

I suggest you read the following reference materials for this section.

If you would like the see how FSM’s are used in other UNC Asheville courses, you can read Sections 3.1 to 3.4 of Digital Design and Computer Architecture, the CSCI 320 textbook available on-line from the UNC Asheville library. If you are seriously interested in programming games, you can consult the many on-line tutorials on games programming with finite state machines, such as the State module of Game Programming Patterns.

## 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 one.

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 are hard to use for the first time. You have to be very careful about the clock input.

### 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.

## 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.

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 the Nabs255.