Implementing our first state machine
In general, a state machine takes in a number of events and, based on the events, moves through a set of states that can produce one or more outputs. A state machine can be quite simple or extremely complex. In the previous chapter, we designed a simple circuit to control our 7-segment display. The 7-segment controller contained two counters that cycled a zero through the cathodes and presented the anode data for each digit. We could have written a state machine to handle this; however, it was easier to write it the way we did.
Before we dive into our calculator project, we need to go over the two ways of coding state machines and the two traditional state machine implementations.
Writing a purely sequential state machine
The first way of coding a state machine is to write it in a single always block driven by a clock.
This kind of state machine would look something like this:
enum bit {IDLE, DATA} state; initial state = IDLE; /...