3. The State Machine (S/M) Maze



To help you get your head around state machines (S/Ms), you will now implement a simple state machine that acts as a state maze. This code will serve as a template for future state machines that you will need to create.

The design principle of the state machine maze is as follows. The aim of the maze is to get from the initial state (State 0) to the final state (State 7). Each state past the first has two directions it can follow, depending on input provided from one of 3 push buttons. Therefore the state maze can be completed by pressing these buttons 7 times in the correct order. Incorrect button pressing is punished by traversing to a state further away from the final state. This is shown by the following diagram.

It is important to note that the synchronous logic is driven by the 50MHz clock, and therefore every time a button is depressed, many thousands of state transitions (clock cycles) could occur. To account for this, each state can only be entered through the depression of a specific button, which will also cause it to stay within its new state. For example, to enter State 6 requires that BTN2 is pressed whilst within State 0 or State 4. Once within State 6, continued pressure on BTN2 will only result in staying in this state.