Salta al contenuto
Note per Studenti Finite State Machines - Synthesis Report

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

  1. Get the specifications (inputs, outputs, behaviour, initial state).
  2. Draw the state diagram.
  3. Build the state and output table, merging equivalent states.
  4. Assign a binary encoding to each state.
  5. Choose the flip-flop type (here PET D).
  6. Derive the flip-flop input equations from the next states in the table.
  7. Derive the output equations.
  8. Minimise the equations (Karnaugh maps).
  9. 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 DD input.

Example. If the table gives next state 0101 (state variables A,BA,B), then DA=0D_A=0 and DB=1D_B=1.

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 S1S_1 if it received X=1X=1 in the last three consecutive clock edges": it is there after 0011100111 or 1010111110101111, not after 0001100011 or 011100011100.
  • "The system is in S2S_2 if it received on inputs X1X2X_1X_2, in order, 00,01,11,1000,01,11,10 (each repeated any number of times) with 1010 last".

How many states. Reuse existing states whenever their description still holds. If S1S_1 means "the last three inputs were 1" and another 1 arrives (sequence 001111001111) we can stay in S1S_1. 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 XX, output ZZ; recognise 11011101 (sequences may overlap); Z=1Z=1 when 110110 has been seen and X=1X=1; Z=0Z=0 otherwise.

Example. With input X=0,1,1,0,1X=0,1,1,0,1 we get Z=0,0,0,0,1Z=0,0,0,0,1.

Step 1: Mealy or Moore? The output depends on the state (having seen 110110) and on the input (X=1X=1): 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 S1S_1 ("nothing recognised") and add states along the right sequence:

  • S2S_2: first bit recognised (11);
  • S3S_3: first two (1111);
  • S4S_4: first three (110110);
  • from S4S_4 with X=1X=1: sequence complete, Z=1Z=1.

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):

  • S1S_1, X=0X=0: stay in S1S_1;
  • S2S_2 (11), X=0X=0: received 1010, no useful prefix, go to S1S_1;
  • S3S_3 (1111), X=1X=1: received 111111, last 1111 remain, stay in S3S_3;
  • S4S_4 (110110), X=0X=0: received 11001100, no prefix, go to S1S_1;
  • S4S_4, X=1X=1 (recognised, Z=1Z=1): the last 11 can start a new sequence, go to S2S_2.

State and output table (next state / output ZZ):

state X=0X=0 X=1X=1
S1S_1 S1/0S_1/0 S2/0S_2/0
S2S_2 S1/0S_1/0 S3/0S_3/0
S3S_3 S4/0S_4/0 S3/0S_3/0
S4S_4 S1/0S_1/0 S2/1S_2/1

State encoding

Formula (bits to encode the states). To represent mm states we need n=⌈log⁡2m⌉n=\lceil\log_2m\rceil bits (2n≥m2^n\ge m). 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. m=4m=4 states need ⌈log⁡24⌉=2\lceil\log_24\rceil=2 bits; m=5m=5 need ⌈log⁡25⌉=3\lceil\log_25\rceil=3 bits.

  • Gray code: minimum number of bits; makes Karnaugh maps easy to fill. S1=00, S2=01, S3=11, S4=10S_1=00,\ S_2=01,\ S_3=11,\ S_4=10.
  • 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: S1=1000, S2=0100, S3=0010, S4=0001S_1=1000,\ S_2=0100,\ S_3=0010,\ S_4=0001.

Gray (2 flip-flops A,BA,B)

ABAB XX next state DADBD_AD_B ZZ
00 (S1S_1) 0 00 0
00 1 01 0
01 (S2S_2) 0 00 0
01 1 11 0
11 (S3S_3) 0 10 0
11 1 11 0
10 (S4S_4) 0 00 0
10 1 01 1
  • DA=1D_A=1 for (A,B,X)=011,110,111(A,B,X)=011,110,111: DA=BX+ABD_A=BX+AB;
  • DB=1D_B=1 for 001,011,101,111001,011,101,111, i.e. exactly when X=1X=1: DB=XD_B=X;
  • Z=1Z=1 only for 101101: Z=AB‾ XZ=A\overline B\,X.

Cost (total gate inputs, not counting input inverters): DAD_A: two 2-input ANDs plus a 2-input OR =6=6; DBD_B: none; ZZ: a 3-input AND =3=3. Network =9=9; two flip-flops (a PET D flip-flop costs about 1414 inputs) =28=28; total 3737.

One-hot (4 flip-flops Q1…Q4Q_1\dots Q_4)

Read directly from the table: S1S_1 is reached from S1,S2,S4S_1,S_2,S_4 with X=0X=0; S2S_2 from S1,S4S_1,S_4 with X=1X=1; S3S_3 from S2,S3S_2,S_3 with X=1X=1; S4S_4 from S3S_3 with X=0X=0. D1=X‾ (Q1+Q2+Q4), D2=X(Q1+Q4), D3=X(Q2+Q3), D4=X‾ Q3, Z=XQ4.D_1=\overline X\,(Q_1+Q_2+Q_4),\ D_2=X(Q_1+Q_4),\ D_3=X(Q_2+Q_3),\ D_4=\overline X\,Q_3,\ Z=XQ_4 . Network cost 5+4+4+2+2=175+4+4+2+2=17; four flip-flops =56=56; total ≈73\approx73. The reset must set only Q1Q_1 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 S1S_1: X=0,1,1,1,0,1,0,1,1,0,0X=0,1,1,1,0,1,0,1,1,0,0 gives states S1,S1,S2,S3,S3,S4,S2,S1,S2,S3,S4,S1S_1,S_1,S_2,S_3,S_3,S_4,S_2,S_1,S_2,S_3,S_4,S_1 and Z=1Z=1 only at input position 6 (the last bit of 11011101 in …0 1 1 1 0 1\dots0\,1\,1\,1\,0\,1). With input 0,1,1,0,1,1,0,1,0,1,1,0,10,1,1,0,1,1,0,1,0,1,1,0,1 the output is 11 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": R0R_0 (no prefix), R1R_1 (11), R2R_2 (1111), R3R_3 (110110), R4R_4 (11011101, output 1).

state (output) X=0X=0 X=1X=1
R0R_0 (0) R0R_0 R1R_1
R1R_1 (0) R0R_0 R2R_2
R2R_2 (0) R3R_3 R2R_2
R3R_3 (0) R0R_0 R4R_4
R4R_4 (1) R0R_0 R2R_2

From R4R_4 with X=1X=1 go to R2R_2: the tail of the received sequence (…1101 1\dots1101\,1) ends with 1111, the prefix recognised by R2R_2. 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 DD and outputs, minimise, circuit, verify. With D FFs next state = DD. Reset: synchronous (AND at the DD inputs) or asynchronous.
  • States: each summarises the useful history; reuse states while the description holds; aim for the minimum number.
  • Recogniser of 11011101 (Mealy, overlaps): S1S_1 none, S2S_2 (11), S3S_3 (1111), S4S_4 (110110). Table (next/output for X=0X=0, X=1X=1): S1(S1/0,S2/0)S_1(S_1/0,S_2/0), S2(S1/0,S3/0)S_2(S_1/0,S_3/0), S3(S4/0,S3/0)S_3(S_4/0,S_3/0), S4(S1/0,S2/1)S_4(S_1/0,S_2/1). Wrong inputs go back to the longest prefix still valid.
  • Encoding: n=⌈log⁡2m⌉n=\lceil\log_2m\rceil bits minimum. Gray S1..S4=00,01,11,10S_1..S_4=00,01,11,10: DA=BX+ABD_A=BX+AB, DB=XD_B=X, Z=AB‾XZ=A\overline BX, total cost 3737. One-hot 1000,0100,0010,00011000,0100,0010,0001: D1=X‾(Q1+Q2+Q4)D_1=\overline X(Q_1+Q_2+Q_4), D2=X(Q1+Q4)D_2=X(Q_1+Q_4), D3=X(Q2+Q3)D_3=X(Q_2+Q_3), D4=X‾Q3D_4=\overline XQ_3, Z=XQ4Z=XQ_4, cost about 7373; reset sets only Q1Q_1. Gray about half the cost; one-hot easier and more reliable.
  • Verification: X=0,1,1,1,0,1,0,1,1,0,0X=0,1,1,1,0,1,0,1,1,0,0 after reset covers all 8 state-input pairs; Z=1Z=1 only at position 6.
  • Moore: five states R0..R4R_0..R_4, R4R_4 outputs 1 and is reached after 11011101; output one cycle later than Mealy. From R4R_4 with X=1X=1 go to R2R_2.
  • 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.

Teoria collegata