Finite State Machines - Synthesis
In questa pagina 6
Synthesis is the design of a synchronous sequential circuit starting from a verbal description. It is the inverse of analysis (Finite State Machines - AnalysisAnalysing a synchronous sequential circuit means deriving from the circuit the flip-flop input equations (next state) and output equations, the state table (2^(m+n) rows for m flip-flops and n inputs), the state diagram (circles are states, arrows carry input/output) and a timing simulation. Mealy: output depends on state and input (written on the arrows, may change between clock edges); Moore: output depends only on the state (written in the circle, changes only at the clock edge). Two states are equivalent if they give the same outputs and lead to equivalent states, and can be merged to save flip-flops.Finite State Machines - Analysis →), and the hardest step is the first: turning the specifications into a state diagram.
Procedure
- Get the specifications (inputs, outputs, behaviour, initial state).
- Draw the state diagram.
- Build the state and output table, merging equivalent states.
- Assign a binary encoding to each state.
- Choose the flip-flop type (here PET D).
- Derive the flip-flop input equations from the next states in the table.
- Derive the output equations.
- Minimise the equations (Karnaugh maps).
- Draw the circuit and verify it with a timing diagram and transitions.
Property (next state of a D flip-flop). Once the encoding is fixed, the design reduces to two combinational networks: one for the next state, one for the output. For a D flip-flop the next state is directly the input.
Example. If the table gives next state (state variables ), then and .
The state diagram
There is no algorithm: practice and care are needed. A state summarises the past history that is needed to decide the future behaviour. It helps to describe every state in words, precisely.
State descriptions.
- "The system is in if it received in the last three consecutive clock edges": it is there after or , not after or .
- "The system is in if it received on inputs , in order, (each repeated any number of times) with last".
How many states. Reuse existing states whenever their description still holds. If means "the last three inputs were 1" and another 1 arrives (sequence ) we can stay in . Look for the minimum number of states: a good state definition makes it evident.
Reset. At power-up a sequential system is in an undefined state; a known initial state is imposed by a reset signal. A synchronous reset needs a clock edge to act: it is realised with an AND at each D input (reset forces all flip-flops to 0). An asynchronous reset acts immediately on the flip-flop (VHDL - Latches, Flip-Flops and ResetsIn VHDL a memory element is a process in which some branch does not assign the signal: the D latch is process(D,C) with if C='1' then Q<=D, the positive-edge-triggered flip-flop is process(CLK) with if CLK'event and CLK='1'. Asynchronous reset: RST is in the sensitivity list and tested first (if RST='1' ... elsif clock edge); synchronous reset: only CLK in the list and RST tested inside the clock-edge branch, so a clock edge is needed for it to act. The testbench of a synchronous circuit has a process generating the clock and one applying inputs away from the active edge.VHDL - Latches, Flip-Flops and Resets →).
Sequence recogniser
Definition (sequence recogniser). A sequence recogniser detects that a given sequence has appeared at the input, whatever happened before. Example: input , output ; recognise (sequences may overlap); when has been seen and ; otherwise.
Example. With input we get .
Step 1: Mealy or Moore? The output depends on the state (having seen ) and on the input (): Mealy signals the recognition at the instant of the last bit, with no extra cycle. It is more convenient (Moore is possible with one more state).
Step 2: states. Start from ("nothing recognised") and add states along the right sequence:
- : first bit recognised ();
- : first two ();
- : first three ();
- from with : sequence complete, .
Then complete all other transitions, asking where each wrong input leads, reusing the longest tail of the received sequence that is still a prefix (overlap rule):
- , : stay in ;
- (), : received , no useful prefix, go to ;
- (), : received , last remain, stay in ;
- (), : received , no prefix, go to ;
- , (recognised, ): the last can start a new sequence, go to .
State and output table (next state / output ):
| state | ||
|---|---|---|
State encoding
Formula (bits to encode the states). To represent states we need bits (). There are many possible encodings; fewer bits (fewer flip-flops) does not always mean lower cost: the combinational network matters too. Optimal encoding is a hard problem solved with heuristics. Two encodings are compared.
Example. states need bits; need bits.
- Gray code: minimum number of bits; makes Karnaugh maps easy to fill. .
- One-hot: one flip-flop per state, a single 1 in the code; more FFs but usually simpler logic, since the logic to enter each state is independent of the others: .
Gray (2 flip-flops )
| next state | |||
|---|---|---|---|
| 00 () | 0 | 00 | 0 |
| 00 | 1 | 01 | 0 |
| 01 () | 0 | 00 | 0 |
| 01 | 1 | 11 | 0 |
| 11 () | 0 | 10 | 0 |
| 11 | 1 | 11 | 0 |
| 10 () | 0 | 00 | 0 |
| 10 | 1 | 01 | 1 |
- for : ;
- for , i.e. exactly when : ;
- only for : .
Cost (total gate inputs, not counting input inverters): : two 2-input ANDs plus a 2-input OR ; : none; : a 3-input AND . Network ; two flip-flops (a PET D flip-flop costs about inputs) ; total .
One-hot (4 flip-flops )
Read directly from the table: is reached from with ; from with ; from with ; from with . Network cost ; four flip-flops ; total . The reset must set only to 1, more complex than clearing all bits.
Conclusion: Gray costs about half; one-hot may still be easier to write, more reliable and sometimes faster.
Verification
Apply different inputs and check that at each edge the circuit makes the expected transitions, starting from a reset. For small circuits, go through all state-input combinations (here 8). A sequence that covers them all, after reset to : gives states and only at input position 6 (the last bit of in ). With input the output is at positions 5, 8, 13, even when sequences overlap.
Moore version
With Moore the output lives in the state, so one more state means "recognised": (no prefix), (), (), (), (, output 1).
| state (output) | ||
|---|---|---|
| (0) | ||
| (0) | ||
| (0) | ||
| (0) | ||
| (1) |
From with go to : the tail of the received sequence () ends with , the prefix recognised by . The output appears one cycle after the last bit, while in the Mealy machine it appears at the instant of the last bit. VHDL for this machine is in VHDL - Finite State Machines and Sequential TestbenchesA finite state machine is described in behavioral VHDL with an enumerated type for the states and three processes: the state register (reset and state <= next_state on the clock edge), the next state as a combinational function of state and input (case state, with default next_state <= state), and the output as a combinational function of the state alone (Moore) or of state and input (Mealy). The testbench has a clock process and an input process (initial reset, then a test sequence that visits all states).VHDL - Finite State Machines and Sequential Testbenches →.
Versione ripasso
- Procedure: specifications, state diagram, table (merge equivalent states), encoding, FF type (PET D), equations of and outputs, minimise, circuit, verify. With D FFs next state = . Reset: synchronous (AND at the inputs) or asynchronous.
- States: each summarises the useful history; reuse states while the description holds; aim for the minimum number.
- Recogniser of (Mealy, overlaps): none, (), (), (). Table (next/output for , ): , , , . Wrong inputs go back to the longest prefix still valid.
- Encoding: bits minimum. Gray : , , , total cost . One-hot : , , , , , cost about ; reset sets only . Gray about half the cost; one-hot easier and more reliable.
- Verification: after reset covers all 8 state-input pairs; only at position 6.
- Moore: five states , outputs 1 and is reached after ; output one cycle later than Mealy. From with go to .
- Mistakes: missing transitions; returning only to the initial state instead of the longest valid prefix; judging cost by flip-flops only; expecting Moore output in the same cycle as Mealy; no reset.