complete

  • Image of the 6 state FSM (+ decoder) example designed in this guide that outputs Red, Yellow or Green, simulating traffic lights. Changes output every 2 clock cycles from red to yellow to green. IN, emulates a pedestrian pressing a button that always jumps to state 5 (green).

A finite state machine is a system that moves through a set of discrete states, with transitions determined by the present state and optional external inputs (optional external outputs which would be a mealy machine), it is decribed with a state table and using sequential logic. It is implemented with memory (flip-flops) holding the state represented by bits, a clock that synchronizes state changes and combinational logic that computes the next state and outputs.

The difference between truth tables and state tables is that truth tables describe purely combinational logic (external inputs to outputs), while state tables describe sequential logic (like combinational logic but with internal input and output - the present state and future state - in addition).

Finite state machines in addition are the foundation of all computer algorithms, whether directly implemented through physical logic gates in hardware or translated through software to physical hardware (higher level algorithms/programs → processor instructions → hardware (registers and ALUs)). They are the heart of computing, and virtually all conceivable digital circuits and devices that we use day to day are built around them, whether physically or logically, which is why they are very important to understand and know how to use.

The purpose of this guide is to illustrate the workflow, process, and theory of designing and implementing a hardware finite state machine, from a high level description and state diagram to excitation tables to boolean expressions that can be directly translated into a combinational logic circuit that can be combined with a register (memory) and clock to form the full finite state machine ready for implementation + any decoders, while explaining how finite state machines work.

The methodology fundamentally revolves around using a high level description of the machine that can gradually be peeled away into lower and lower level descriptions that describe the same thing, until it can be directly transferred onto the circuit with logic gates and memory.

1. Describe the machine

The first step involves simply describing what the machine should do: how many states there will be (and consequently how much memory is needed), the desired IO, and what type of machine is needed. For example, suppose I want to implement a circuit that continuously loops through 3 states: red, yellow, green and on input 1, jumps to green regardless of the current state, as a simple example of traffic lights. Working through this comes down to a few questions:

  • Is memory necessary? To go through states at all, a machine needs to know what state it’s currently in. That requires memory, which is what makes this a sequential circuit (combinational circuits have no memory and rely on logic gates alone). The defining difference between a combinational circuit and sequential circuit is that comb. circuits have external inputs alone, while seq. circuits have internal inputs (i.e. memory) in addition to optional external inputs.

  • Is a separate output needed? If the function of the machine requires a discrete output other than just the next state, a Mealy machine is needed; if not, a Moore machine will do. This traffic light example doesn’t require a separate output, so a Moore machine is used.

The final part of this step is to visually describe the machine through a state diagram and define it in a table.

state diagram Moore machine state diagram. The additional states W0, W1, W2 are intermediary states identical to the previous state in what they intend to be used for (emitting the same light), with the end goal of each light emission lasting two clock ticks instead of one.

state transition table State transition table

  • Note: x here is a symbol for a don’t care (DC) value (input can be 0 or 1, the desired next state is the same either way).

2. Build the state table

encoded table

  • Note: Q2, Q1, Q0 refer to the present state; outputs labeled with delta represent the next state.

This is the most important step: encode the states in the above table and create a full state table, which is used for deriving the boolean expressions of the circuit. DC values should be expanded so that 0 and 1 are separate minterms, and the state column should be expanded to the appropriate number of inputs for fully representing the encoded states.

It’s necessary to consider the number of states that need to be encoded in order to determine the number of bits (inputs) required. In this example there are 6 states, and since 2² = 4 is not enough but 2³ = 8 is, the minimum number of bits to represent that is 3.

Additionally, outputs may need to be adjusted depending on the type of flip-flop being used. Check the excitation table for the flip-flops and implement it in columns labeled by flip-flop type. In this case D flip-flops are being used, which transfer directly to our desired outputs, so no adjustment is necessary (D is identical to Q^Δ). If a JK or T flip-flop were used instead, you’d need to look up its own excitation table and fill in those columns accordingly.

3. Derive the boolean expressions

The next step is to use the excitation table to derive the boolean expressions, solving for each output. We can solve for them manually using Karnaugh maps or with a tool if very large, but it is good to know how to do manually with kmaps even so.

kmap1 kmap2

To solve kmaps, plot IO in a table with grey code ordering; then circle retangular groups of ones (groups must be powers of 2). Groups should be made as large as possible and as few as possible to cover all 1s. For each group, write the variables that stay constant and drop the ones that don’t. Write the variable plain if constant at 1 or NOT if constant at 0. Finally, OR together all terms, resulting in a SOP expression. Use De Morgan’s theorem twice to convert between SOP and POS.

4. Plot the combinational logic circuit

combinational_logic

Plot the boolean expressions into a logic circuit. Each output Q0^Δ, Q1^Δ, Q2^Δ is a function of inputs Q0, Q1, Q2, and IN. Follow standard order of operations when plotting. Macrons over arithmetic operators invert them and macrons over variables correspond to NOT; multiplications correspond to AND and sums to OR. Simplify using De Morgan’s theorem where appropriate, comparing SOP and POS forms, and weigh propagation delay against number of logic gates. Fewer gates chained in series generally means less delay, even if it costs more gates overall, so it’s worth comparing forms rather than just taking the first simplification you land on.

5. Wire memory, clock, reset pins and decoder

complete

After creating the main circuit, the next thing to do is simply wire the register – in this case synchronous d flipflops - and feed outputs into internal inputs according to the excitation table (q2, q1, q0 are internal inputs; IN is an external input and should be left alone). They’re wired to the same clock and reset pin. The reset pin is important because without it, the flip-flops’ output is undefined at the beginning, which could create logic issues or not start where it should. The goal of the clock is to generate pulses that signals the flip-flops to read input, as a synchronization signal. An example of a clock is an astable multivibrator: an explanation of how astable multivibrators work here: /writeups/astable-multivibrator. In practice a timer IC like NE555 will be used that has different modes other than just astable multivibrator. It’s also important not to leave the rest of the flip-flop’s pins undefined, they need to be pulled up: set should be grounded unless you plan to use it, and load should be set to a constant 1.

encoded

Now, if the clock is turned on, the outputs emitted by the flip-flops will match the encoded states, as demonstrated in the video. All that’s left is adding a combinational circuit that outputs R, Y, or G for the respective encoded states, essentially a decoder. Once again, derive the boolean expressions or use a tool, and then plot it into the main circuit.

decoder_table

  • Truth table of the decoder used to translate states 000 & 001 to red, 010 & 011 to yellow and 100 and 101 to green

decoder

  • Logic circuit of the decoder and outputs
  • Video demonstration of states and transitions in the complete build

6. Power and reset

All that’s left is adding power and wiring for the components. To reset the flip-flops automatically on power-on, use a resistor and capacitor, so that while the capacitor charges, current can flow, resetting the flip-flops. Once the capacitor is fully charged, current stops flowing to the reset pin.

With all 6 steps done, the FSM, should be complete. At every step test it against the previous step to ensure you are progressing correctly, for example, compare the outputs of the logical circuit against its excitation table, the states against the encoded states in the running circuit… This is the full pipeline for bringing what started as a mere description to ready for physical implementation.

Next steps for physical implementation: /writeups/fsm-practice