Part 4: FSM Sequential Circuit
Objective
This tutorial contains information about FSM sequential circuit and several design examples of FSM sequential circuit.
Source Code
This repository contains all of the code required in order to follow this tutorial.
References
Pong P. Chu, FPGA Prototyping by Verilog Examples, https://onlinelibrary.wiley.com/doi/book/10.1002/9780470374283
1. Finite State Machine
An FSM (finite state machine) is used to model a system that transits among a finite number of internal states. The transitions depend on the current state and external input. Unlike a regular sequential circuit, the state transitions of an FSM do not exhibit a simple, repetitive pattern.
1.1. Mealy and Moore Outputs
The basic block diagram of synchronous FSM is similar to the regular sequential circuit, as shown in the following figure. It consists of a state register, next-state logic, and output logic. An FSM is known as a Moore machine if the output is only a function of state and is known as a Mealy machine if the output is a function of state and external input.
1.2. FSM Representation
An FSM is usually specified by an abstract state diagram or algorithmic state machine (ASM) chart.
The following figure shows an example of a state diagram that consists of a single node and its transition arcs. The arc is taken when the corresponding expression is evaluated true. The Moore output value is placed inside the circle. The Mealy output is placed with the transition arcs.
The following figure shows an example of a ASM chart that consists of a single block. An ASM block consists of one state box and an optional network of decision boxes and conditional output boxes. A state box represents a state in an FSM, and the asserted Moore output values are listed inside the box.
A decision box tests the input condition and determines which exit path to take, true (T) or false (F). A conditional output box lists asserted Mealy output values and is usually placed after a decision box. It indicates that the listed output signal can be activated only when the corresponding condition in the decision box is met.
2. Finite State Machine with Data Path
An FSMD (finite state machine with data path) combines an FSM and regular sequential circuits. The FSM, which is sometimes known as a control path, examines the external commands and status and generates control signals to specify operation of the regular sequential circuits, which are known collectively as a data path.
2.1. Single RT Operation
An RT operation specifies data manipulation and transfer for a single destination register. It is represented by the notation
where is the destination register, , , and are the source registers, and specifies operation to be performed. The notation indicates that the contents of the source registers are fed to the function, which is realized by a combinational circuit, and the result is passed to the input of the destination register and stored in the destination register at the next rising edge of the clock.
Following are several representative RT operations:
. A constant 0 is stored in register.
. The summation of the and registers is written to the register.
. The register is shifted left two positions and then written back to itself.
. The content of the register is written back to itself.
A single RT operation can be implemented by constructing a combinational circuit for the function and connecting the input and output of the registers. The following figure shows an example circuit of RT operations. For clarity, we use the _reg
and _next
suffixes to represent the output and input of a register, respectively. For example, operation actually means that:
r_next = r_reg << 2;
r_reg <= r_next
at the rising edge of the clock.
In this circuit, the previous RT operations are implemented using a combinational circuit that consists of a 4-to-1 multiplexer, adder, and shifter. The state register state_reg
of the FSM controls the selection signal of the multiplexer and thus chooses the result of the desired RT operation.
2.2. ASMD Chart
The ASM chart can be extended to incorporate RT operations and call it an ASMD (ASM with data path) chart. The following figure shows an example of a ASMD chart that consists of a single block.
Depending on the a > b
condition, the FSMD performs either r2 ← r2 + a
or r2 ← r2 + b
. Note that all operations are done in parallel inside an ASMD block, so that wee need to use multiplexer to route the desired value to r2
as shown in the following figure.
2.3. FSMD System
The conceptual block diagram of an FSMD is divided into a data path and a control path, as shown in the following figure. The data path performs the required RT operations. It consists of:
Data registers: store the intermediate computation results.
Functional units: perform the functions specified by the RT operations.
Routing network: routes data between the storage registers and the functional units.
The data path follows the control
signal to perform the desired RT operations and generates the internal status
signal.
The control path is an FSM. As a regular FSM, it contains a state register, next-state logic, and output logic. It uses the external command
signal and the data path's status
signal as the input and generates the control
signal to control the data path operation. The FSM also generates the external status
signal to indicate the status of the FSMD operation.
3. Design Examples
3.1. FSM
In this example, we consider a simple FSM example. The FSM is represented both using a state diagram and an ASM chart as in the following figure. It consists of three states and both Mealy output x
, and Moore output y
. The inputs to the FSM are a
and b
.
The procedure of developing code for an FSM is similar to that of a regular sequential circuit. We first separate the state register and then derive the code for the combinational next-state logic and output logic.
In the following code, the output logic is separated as a continuous assignment, so the code has four segments (state register, next state logic, Mealy output, and Moore output). The key part is the next state logic. It uses a case statement. The code for each state basically follows state diagram and ASM chart.
An alternative code is to merge the output logic with the next state logic, so the code has only two segments (state register, next state logic and output logic). Both of the codes should produce the same results.
The following code is the testbench for the FSM example.
This is the simulation result of the four segments FSM example. The Moore output y
changes when the FSM enters state S1
. It follows the rising edge of the clock. But the Mealy output x
changes when a
and b
are both one. It doesn't follow the rising edge of the clock. It follows external signals. This is the difference between Moore and Mealy outputs.
This is the simulation result of the two segments FSM example. The result is similar to the four segment FSM example.
3.2. Fibonacci
In this example, we consider a Fibonacci example for an FSMD system. The Fibonacci numbers constitute a sequence defined as
The Fibonacci number can be calculated iteratively, from 0 to desired . This approach requires two temporary registers to store the two most recently calculated values i.e., and and one index register to keep track of the number of iterations.
In the following figure, t0
and t1
are the temporary storage registers and n
is the index register. There are additional signals, which are the start
signal that signals the beginning of operation and two status signals: ready
, which indicates that the circuit is idle and ready to take new input, and done_tick
, which is asserted for one clock cycle when the operation is completed.
This is the implementation of the Fibonacci module in Verilog.
The following code is the testbench for the Fibonacci module.
This is the simulation result of the Fibonacci module.
Then, we can add the Fibonacci module to the Vivado block design, add a virtual input/output (VIO) IP, and also add an integrated logic analyzer (ILA) IP.
This is the constraints for this block design. We only use clock signal that has a period of 8.0 ns (125 Mhz).
The following figure shows the synthesis result.
We can view the detailed resource utilization. The Fibonacci module only uses 83 LUTs and 64 registers. Most of the resources are for the VIO and ILA IP.
From the result, we can see that the timing requirements are met for this 125 MHz clock. There is setup WNS value which is 1.223 ns. This means that we could have asked for a clock period that is shorter by 1.223 ns, and it would still be OK. So the requested clock period could have been 8.0 - 1.223 ns = 6.777 ns, which is about 147 MHz.
The following figure shows how we can control the Fibonacci module using the VIO interface in Vivado (after programming the FPGA). First, we have to set the reset to 1, and then set the value i
; in this example, we set it to 10. Then the result of fib(10)
is 55, which will be updated as shown in the output.
The following figure shows how we can observe the waveform of the counter module using the ILA interface in Vivado (after programming the FPGA). First, we have to set up the trigger signal for this ILA, which is the start
signal. Then, on the VIO, we toggle the start to 1. Then the result of fib(10)
is 55, which will be produced as shown in the output.
4. Conclusion
In this tutorial, we covered the FSM sequential circuit and several design examples of FSM sequential circuit.
Last updated