8. state machines (also called finite state machines (fsm), automaton)

53
Slide 1 8. State Machines (also called Finite State Machines (FSM), Automaton)

Upload: meris

Post on 02-Feb-2016

44 views

Category:

Documents


0 download

DESCRIPTION

8. State Machines (also called Finite State Machines (FSM), Automaton). Finite-State Machines (FSMs). Why do we need them? Problem 1: make an 8-bit shift register. Implement the following sequence: 8’b00000001 //Shift left 8’b00000010 //Shift Left … 8’b10000000 //Shift right - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 1

8. State Machines(also called Finite State Machines

(FSM), Automaton)

Page 2: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 2

• Why do we need them?

• Problem 1: make an 8-bit shift register. Implement the following sequence:8’b00000001 //Shift left8’b00000010 //Shift Left…8’b10000000 //Shift right8’b01000000 //Shift right…8’b00000001..and from the beginning again…• Solution (?) Bidirectional shift register with a comparator…always @ (posedge CLK)if (Reset || Q_reg == 8’b00000000) Q_reg <= 8’b00000001;else if (LnR) Q_reg <= {Q_reg[6:0], 1’b0};else Q_reg <= {1’b0, Q_reg [7:1]};

assign LnR = (Q_reg == 8’b10000000) ? 1’b0:1’b1;

Finite-State Machines (FSMs)

CLK

SR8_RL

Q[7:0]

8

Reset

LnR

CLK

Reset

=

8'b10000000

Page 3: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 3

•Solution (?) Bidirectional shift register with a comparator…always @ (posedge CLK)if (Reset || Q_reg == 8’b00000000) Q_reg <= 8’b00000001;else if (LnR) Q_reg <= {Q_reg[6:0], 1’b0};else Q_reg <= {1’b0, Q_reg [7:1]};

assign LnR = (Q_reg == 8’b10000000) ? 1’b0:1’b1;• Will it work? What will be the real sequence?8’b000000018’b00000010…8’b100000008’b01000000 //AND NOW?8’b10000000 //Because LnR becomes 1

//again!8’b01000000 // and 0 again!8’b10000000 //and so on…• What are we missing?

Finite-State Machines (FSMs)

CLK

SR8_RL

Q[7:0]

8

Reset

LnR

CLK

Reset

=

8'b10000000

Page 4: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 4

•What are we missing?

• KEEP IN MIND whether the register shifts to left or right

• Toggle LnR at both endpoint conditions

reg QFF;

always @ (posedge CLK)

if (Reset) QFF = 1’b0;

else if ((Q == 8’h10) && (QFF == 1’b0) QFF <= 1’b1;

else if ((Q == 8’h01) && (QFF == 1’b1) QFF <= 1’b0;

assign LnR = ~QFF;

• QFF depends on its previous value the current value i.e. ON THE PREVIOUS STATE

• In fact QFF implements a STATE MACHINE WITH TWO STATES

Finite-State Machines (FSMs)

CLK

SR8_RL

Q[7:0]

8

Reset

LnR

CLK

Reset

=

8'b10000000

=8'b00000001

CLK

QFF

QD

CE

CLK R

Reset

Page 5: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 5

• In fact QFF implements a STATE MACHINE WITH TWO STATES (we can call it hidden state machine)• Look to the block schematic below!

• The comparators will determinethe NEXT STATE of QFF, DEPENDINGon the CURRENT STATE• QFF holds THE CURRENT STATE• There is useless to have a state machinewithout an OUTPUT (LnR, which is ~QFF)

Finite-State Machines (FSMs)

CLK

SR8_RL

Q[7:0]

8

Reset

LnR

CLK

Reset

=

8'b10000000

=8'b00000001

CLK

QFF

QD

CE

CLK R

Reset

ShL

LnR = 1'b1

ShR

LnR = 1'b0

Q==8'h10

Reset

Q==8'h01

Page 6: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 6

• Problem 2: make a 4-bit counter, that counts in the following sequence:1. Count up to 122. Count down to 53. Count up to 7 and hold counting until Reset becomes active• In other words, imagine it as a task:

1. Go up to the 12th floor to take some papers from an office2. Then go down to the 5th floor to leave the papers to be signed3. Afterwards go up to the 7th floor and wait for…

• Cannot be done by simply pressing the elevator buttons 12, 5 and 7! • We have here four STATES to keep in mind:

• Counting up to 12• Counting down to 5• Counting up to 7• Hold counting at 7

• Think about a two-bit counter that holds the current state

Finite-State Machines (FSMs)

Page 7: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 7

•Think about a two-bit counter that holds the current statereg [1:0] cntst;always @ (posedge CLK)if (Reset) cntst <= 2’b00;else if ( ((cnt == 4’hC) && (cntst == 2’b00)) || //are the conditions OK?

((cnt == 4’h5) && (cntst == 2’b01)) || //Check them ((cnt == 4’h7) && (cntst == 2’b10)) ) //using simulation!cntst<=cntst+ 2’b01;

//correct conditions are: 4’hB, 4’h6, 4’h6 !!!//NOT EVEN THE PREVIOUS PROBLEM/SOLUTION IS CORRECT!!! GO BACK //AFTER FINISHING THIS ONE !always @ (cntst) begincase (cntst)2’b00: begin

CE = 1’b1; UnD = 1’b1; end

2’b01: begin CE = 1’b1; UnD = 1’b0;end

…2’b11: CE = 1’b0;

Finite-State Machines (FSMs)

Page 8: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 8

•Think about a two-bit counter that holds the current state

• Therefore our states are called 2’b00, 2’b01, 2’b10, 2’b11

• Why not organize the state machine?

• Describe states and transitions by using STATE MACHINE DIAGRAMS

• Give suggestive names to states

• Use a typical coding style

• Note: In the HLD7 state the UnD = 1’b1 output is not relevant (CE is anyway 0)

Finite-State Machines (FSMs)

CU12

CE=1'b1, UnD=1'b1

CD5

CE = 1'b1, UnD = 1'b0

CU7

CE = 1'b1, UnD = 1'b1

HLD7

CE = 1'b0UnD = 1'b1

cnt ==4'hb

cnt == 4'h6

cnt == 4'h6

Reset

Page 9: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 9

• Many digital systems can be looked as FSMs

• Example: Assembly code sequence for HC08 (Freescale, former Motorola)

EE00 lda $0010 ; Load accumulator A from address…

EE02 beq #EE0D ; If A=0, jump to address EE0D

EE04 inca ; A<-A+1

EE05 beq #EE10 ; If A was FF, then jump to address …

EE07 cmp #$02 ; Compare with 2

EE09 beq #EE15 ; If A = 2, then jump to address…

EE0B bra #EE00 ; Jump back to address…

Finite-State Machines (FSMs)

Page 10: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 10

• Many digital systems can be looked as FSMs

• Example: Assembly code sequence for HC08 (Freescale, former Motorola)

The state machine diagram implemented by the code sequence:

Finite-State Machines (FSMs)

@ELSE

A=1

A=255A=0

RESET

INSTR_FROM_EE15

INSTR_FROM_EE10

INSTR_FROM_EE0D

IDLE

Page 11: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 11

• IN FACT : CPU is a PROGRAMMABLE STATE MACHINE

• States are held by the INSTRUCTION POINTER (or PROGRAM COUNTER) (for HC08: 16 bits)

• Outputs: set by the instructions

• Transitions: depending on the microprocessor flags

• Keeping into account the considerations above, a part of the state diagram is – see next slide

Finite-State Machines (FSMs)

Page 12: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 12Keeping into account the considerations above, a part of the state diagram is:

Z=0

Z=1

1

A=0 A\=0

1

A=0 A\=0

1

RESET

EE15

INSTRUCTION

EE09PC<=PC+(Z=1) AND 12 + (Z=0)

EE10INSTRUCTION

EE07Z<='1' when A=2 ELSE

'0'

EE0D

INSTRUCTION

EE05PC<=PC+(A=0) AND 11 + (A\=0) AND

EE04A<=A+1

PC<=PC+1

EE02PC<=PC+(A=0) AND 11 + (A\=0)

EE00PC=PC+2

Page 13: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 13

• Notes:• The state machine diagram above can be described in many ways, depending on the level of abstractization

• Each state (instruction) represents in fact a colloection of states/operations for the CPU:

• Fetch

• Instruction decode

• Execute

•Some of the operations above can be implemented in Pipeline!

Finite-State Machines (FSMs)

Page 14: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 14

• For State machines implemented by CPUs:

• Low execution speed: some of the instructions need several clock periods, at a clock speed of 4..20MHZ

• But flexible – easy to change the algorithm

• For state machines implemented on FPGAs or ASICs:

• Very fast (A decision can be taken in a single clock period, at a clock frequency of 50..200MHz

• But rigid – changing the algorithm to be implemented may need to redesign the state machine

• Therefore: design the state machine to be as readable as possible!

Finite-State Machines (FSMs)

Page 15: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 15

• SINCRONE:

• Structura Generala(Ieșirile nu sunt atribuite):

În funcție de modul de atribuire a ieșirilor, se deosebesc trei tipuri de automate secvențiale (conform Heinkel):

• Medvedev

• Moore

• Mealy

Logica Stare

>

D QIN

CLK

RESET

RESET – Sincron sau asincron

Automate Secvenţiale (Finite-State Machines, FSM, Automaton)

COMBINAȚIONAL SECVENȚIAL

Page 16: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 16

• MEDVEDEV

Logica Stare

>

D QIN

CLK

RESET

RESET – Sincron sau asincron

OUT

• Iesirile reprezinta de fapt STĂRILE automatului secvential (vectorul de stari)

• Exemplu:

RESET

A XOR B ='1' A='0' AND B='0'

A='1' AND B='1'

SFARSITY<='0';Z<='1'

INCEPUTY<='0'; Z<='0'

CONTINUARE

Y<=‘1';Z<=‘1'

Automate Secvenţiale (Finite-State Machines, FSM, Automaton)

Page 17: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 17

• MEDVEDEV• Ieșirile reprezintă de fapt STĂRILE automatului secvențial• Exemplu: Un Procesparameter INCEPUT =2’b00;parameter CONTINUARE = 2’b11;parameter SFARSIT = 2’b01;reg [1:0] STATE;

always @ (posedge CLK or A or B or STATE or posedge RESET) if (RESET) STATE<=INCEPUT; else begin case (STATE) INCEPUT: if (!(A | B)) STATE<=CONTINUARE;

else STATE<=INCEPUT; CONTINUARE : if (A & B) STATE<=SFARSIT;

else STATE<=CONTINUARE; SFARSIT: if (A ^ B) STATE<=INCEPUT;

else STATE<=SFARSIT; default: STATE<=INCEPUT; endcase; end

assign Y<=STATE[1]; assign Z<=STATE[0];end

RESET

A XOR B ='1' A='0' AND B='0'

A='1' AND B='1'

SFARSIT

Y<='0';Z<='1'

INCEPUT

Y<='0'; Z<='0'

CONTINUARE

Y<=‘1';Z<=‘1'

Automate Secvenţiale (Finite-State Machines, FSM, Automaton)

Page 18: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 18

• MEDVEDEV• Ieșirile reprezintă de fapt STĂRILE automatului secvențial

•Rezultatul simulării Post-Translate: Y, Z: reprezintă starea -> Automat MEDVEDEV

RESET

A XOR B ='1' A='0' AND B='0'

A='1' AND B='1'

SFARSIT

Y<='0';Z<='1'

INCEPUT

Y<='0'; Z<='0'

CONTINUARE

Y<=‘1';Z<=‘1'

Automate Secvenţiale (Finite-State Machines, FSM, Automaton)

Page 19: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 19

• MEDVEDEV• Ieșirile reprezintă de fapt STARILE automatului secvential• Exemplele cu un proces:

• greu de urmărit, greu de depanat• greu de atribuit semnale la “intrarea în stare”

• Exemplu cu Doua Procese:• Folosim semnalul intermediar NEXTSTATE - de la ieșirea blocului combinațional

RESET

A XOR B ='1' A='0' AND B='0'

A='1' AND B='1'

SFARSIT

Y<='0';Z<='1'

INCEPUT

Y<='0'; Z<='0'

CONTINUARE

Y<=‘1';Z<=‘1'

Automate Secvenţiale (Finite-State Machines, FSM, Automaton)

Page 20: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 20

• MEDVEDEV•Folosim semnalul intermediar NEXTSTATE - de la iesirea blocului combinațional

reg [1:0] STATE, NEXTSTATE:=INCEPUT;always @ (posedge CLK or posedge RESET) if (RESET) STATE<=INCEPUT; else STATE <= NEXTSTATE;

always @ (A or B or STATE) begin case (STATE) INCEPUT: if (!(A | B)) NEXTSTATE<=CONTINUARE;

else NEXTSTATE<=INCEPUT; CONTINUARE : if (A & B) NEXTSTATE<=SFARSIT;

else NEXTSTATE<=CONTINUARE; SFARSIT: if (A ^ B) NEXTSTATE<=INCEPUT;

else NEXTSTATE<=SFARSIT; default: NEXTSTATE<=INCEPUT; endcase; end

assign Y<=STATE[1]; assign Z<=STATE[0];end

RESET

A XOR B ='1' A='0' AND B='0'

A='1' AND B='1'

SFARSIT

Y<='0';Z<='1'

INCEPUT

Y<='0'; Z<='0'

CONTINUARE

Y<=‘1';Z<=‘1'

Automate Secvenţiale (Finite-State Machines, FSM, Automaton)

Logica Stare

>

D QIN

CLK

RESET

Page 21: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 21

• MEDVEDEV Rezultatul simularii Post-translate: acelasi

RESET

A XOR B ='1' A='0' AND B='0'

A='1' AND B='1'

SFARSIT

Y<='0';Z<='1'

INCEPUT

Y<='0'; Z<='0'

CONTINUARE

Y<=‘1';Z<=‘1'

Automate Secvenţiale (Finite-State Machines, FSM, Automaton)

Page 22: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 22

• (Heinkel): Una sau doua procese?• Observații:

• Moore, Mealy: pot fi două sau trei (sau mai multe!) procese

• Chiar si la Medvedev: pot exista mai multe procese !

• (Exemplu: Se trece din CONTINUARE in SFARSIT daca A AND B =‘1’ timp de 10 perioade de ceas)

• In acest caz: numărător separat, (recomandabil) numărând în proces separat:always @ (posedge CLK) begin

if ((STATE!=CONTINUARE) | (!(A & B)) cnt<=0;

else cnt<=cnt+1;

end always @ (A or B or STATE) begin case (STATE) … CONTINUARE : if (cnt==10) NEXTSTATE<=SFARSIT;

else NEXTSTATE<=CONTINUARE; SFARSIT: …

RESET

A XOR B ='1' A='0' AND B='0'

A='1' AND B='1'

SFARSITY<='0';Z<='1'

INCEPUTY<='0'; Z<='0'

CONTINUAREY<=‘1';Z<=‘1'

Automate Secvenţiale (Finite-State Machines, FSM, Automaton)

Page 23: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 23

• (Heinkel): Una sau doua procese?• Întrebarea relevantă este: Înregistrarea stării şi tranziţiile: ÎN PROCES COMUN SAU în PROCESE SEPARATE? (una sau doua procese?)

• Un singur proces, justificate de: • Starea automatului secvențial: SE SCHIMBĂ INTR-UN SINGUR PROCES• Reprezentarea grafică a automatului secvențial: Echivalent cu un singur proces• Ieșirile pot fi atribuite concomitent cu stările• Dar în cazul folosirii unui numărător? vezi exemplul precedent

• Doua procese, justificate de:• Structura automatului secvenţial: Circuitele combinaţionale ≠ Circuitele secventiale -> două procese• Simulare: Doua procese permit acces la semnalul NEXTSTATE (starea urmatoare), rezulta: MAI USOR DE DEPANAT• Sinteză: oferă acces la NEXTSTATE – semnalele pot fi modificate la “intrarea” în stare• Sinteză: Doua procese pot conduce la circuite mai mici, deci, la rezultate de sinteză mai bune (Heinkel)

Automate Secvenţiale

Page 24: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 24

• MEDVEDEV• Avantaje:

• Foarte rapide (ieșirile nu parcurg logică suplimentară)

• Ieşiri – de la ieşirea unui bistabil (Q), deci sunt ÎNREGISTRATE, Rezultă că se evită apariţia de semnale hazardate (spike, glitch)

• Dezavantaje:

• Codificarea stărilor: trebuie făcut manual, necesita efort suplimentar in scrierea codului HDL

• Rigide – greu de modificat automatul secvenţial (adăugat sau șters stări etc)

RESET

A XOR B ='1' A='0' AND B='0'

A='1' AND B='1'

SFARSITY<='0';Z<='1'

INCEPUTY<='0'; Z<='0'

CONTINUAREY<=‘1';Z<=‘1'

Automate Secvenţiale

Page 25: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 25

• MOORE

Automate Secvenţiale (Finite-State Machines, FSM, Automaton)

• Ieșirile reprezintă de fapt FUNCŢII LOGICE ale STĂRILOR (vectorului de stari)

• Exemplu: Acelaşi automat secvenţial

Logica Stare

>

D QIN

CLK

RESET

RESET – Sincron sau asincron

STATELogica Iesire

OUT

RESET

A XOR B ='1' A='0' AND B='0'

A='1' AND B='1'

SFARSITY<='0';Z<='1'

INCEPUTY<='0'; Z<='0'

CONTINUAREY<=‘1';Z<=‘1'

COMBINAȚIONAL

SECVENȚIAL

COMBINAȚIONAL

Page 26: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 26

• MOORE• Iesirile reprezintă de fapt FUNCŢII LOGICE ale STĂRILOR (vectorului de stari)parameter INCEPUT =1;parameter CONTINUARE = 2;parameter SFARSIT = 3;reg [1:0] StC, StN;always @ (posedge CLK or posedge RESET) if (RESET) StC<=INCEPUT; else StC <= NEXTSTATE;always @ (A or B or StC) begin StN <= StC; //Introducem tranzitia implicita//Asta e posibil la oricare implementare de automat secvential! case (StC) INCEPUT: if (!(A | B)) StC<=CONTINUARE; CONTINUARE : if (A & B) StC<=SFARSIT; SFARSIT: if (A ^ B) StC<=INCEPUT; default: StC<=INCEPUT; endcase;end

assign Y<= (StC == CONTINUARE) ? 1:0; assign Z<= ((StC == CONTINUARE) || (StC == SFARSIT)) ? 1:0;end

RESET

A XOR B ='1' A='0' AND B='0'

A='1' AND B='1'

SFARSITY<='0';Z<='1'

INCEPUTY<='0'; Z<='0'

CONTINUAREY<=‘1';Z<=‘1'

Automate Secvenţiale

Page 27: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 27

• Din nou: Una sau doua procese?• Observație: ISE, Language Templates, Coding Examples: Automat secvențial de tip MOORE cu un singur proces:

always@(posedge <clock>) if (<reset>) begin state <= <state1>; <outputs> <= <initial_values>; end else (* PARALLEL_CASE, FULL_CASE *) case (state) <state1> : begin if (<condition>) state <= <next_state>; else if (<condition>) state <= <next_state>; else state <= <next_state>; <outputs> <= <values>; end <state2> : begin if (<condition>) state <= <next_state>; else if (<condition>) state <= <next_state>; else state <= <next_state>; <outputs> <= <values>; end

Automate Secvenţiale

Page 28: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 28

• Din nou: Una sau doua procese?• Observație: ISE, Language Templates, Coding Examples: Automat secvențial de tip MOORE cu un singur proces:

Exemplu

always @ (posedge CLK) if (Reset) begin state <= INCEPUT; Y <= 0; Z <= 0; end else (* PARALLEL_CASE, FULL_CASE *) case (state) INCEPUT : begin if (A|B) state <= CONTINUARE; else state <= INCEPUT; Y <=1; Z <=0; end …• Codul e scris corect, ieșirile sunt ÎNREGISTRATE• Dar în cazul multor ieșiri sau în cazul folosirii unui contor?

Automate Secvenţiale

Page 29: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 29

• MOORE

• Ieşirile reprezinta de fapt FUNCTII LOGICE ale STĂRILOR (vectorului de stări)

Rezultatul simularii:

Semnalele de ieşire se schimbă sincron cu starea, rezultă automat secvenţial MOORE

Logica Stare

>

D QIN

CLK

RESET

RESET – Sincron sau asincron

STATE

Logica Iesire

OUT

RESET

A XOR B ='1' A='0' AND B='0'

A='1' AND B='1'

SFARSITY<='0';Z<='1'

INCEPUTY<='0'; Z<='0'

CONTINUAREY<=‘1';Z<=‘1'

Automate Secvenţiale

Page 30: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 30

• MOORE• Avantaje:

• Asigură viteză de lucru mare

• Operaţie stabilă

• Dezavantaje:

• Nu este ferit de hazard combinațional! Pot aparea valori parazite in semnalele de ieşire, de exemplu: logică SAU, logică XOR

• Însă: valorile parazite în general sunt mult mai scurte decât perioada de tact

RESET

A XOR B ='1' A='0' AND B='0'

A='1' AND B='1'

SFARSITY<='0';Z<='1'

INCEPUTY<='0'; Z<='0'

CONTINUAREY<=‘1';Z<=‘1'

Automate Secvenţiale

Page 31: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 31

• MEALY

Automate Secvenţiale (Finite-State Machines, FSM, Automaton)

• Ieșirile sunt FUNCŢII LOGICE ale STĂRILOR (vectorului de stări) şi ALE INTRĂRILOR

• Exemplu: Acelaşi automat secvenţial

Logica Iesire

Logica Stare

Out

>

D QIN

CLK

RESET

Proces Secvential

Proces Combinational

RESET

A XOR B ='1' A='0' AND B='0'

A='1' AND B='1'

SFARSITY<='0';Z<='1'

INCEPUTY<='0'; Z<='0'

CONTINUAREY<=‘1';Z<=‘1'

Page 32: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 32• MEALY• Ieșirile sunt FUNCTII LOGICE ale STARILOR (vectorului de stări) și ALE INTRĂRILOR• Exemplu: (aceeași descriere de stări ca si la MOORE)parameter INCEPUT =1;parameter CONTINUARE = 2;parameter SFARSIT = 3;reg [1:0] StC, StN;always @ (posedge CLK or posedge RESET) if (RESET) StC<=INCEPUT; else StC <= NEXTSTATE;always @ (A or B or StC) begin StN <= StC; //Introducem tranzitia implicita//Asta e posibil la oricare implementare de automat secvential! case (StC) INCEPUT: if (!(A | B)) StC<=CONTINUARE; CONTINUARE : if (A & B) StC<=SFARSIT; SFARSIT: if (A ^ B) StC<=INCEPUT; default: StC<=INCEPUT; endcase;endassign Y = ((StC==CONTINUARE) && (!(A | B))) || ((StC==SFARSIT) && (!(A & B))) ? 1:0;assign Z = ((StC==INCEPUT) && (A & B)) || (StC==CONTINUARE) || ((StC==SFARSIT && (A & B)) ? 1:0;end

RESET

A XOR B ='1' A='0' AND B='0'

A='1' AND B='1'

SFARSITY<='0';Z<='1'

INCEPUTY<='0'; Z<='0'

CONTINUAREY<=‘1';Z<=‘1'

Automate Secvenţiale

Page 33: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 33

• MEALY• Ieșirile sunt FUNCȚII LOGICE ale STĂRILOR (vectorului de stări) și ALE INTRARILOR

Rezultatul simulării: Y, Z se schimbă la cu intrările si cu starea, rezultă automat secvential MEALY

RESET

A XOR B ='1' A='0' AND B='0'

A='1' AND B='1'

SFARSITY<='0';Z<='1'

INCEPUTY<='0'; Z<='0'

CONTINUAREY<=‘1';Z<=‘1'

Automate Secvenţiale

Page 34: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 34

• MEALY

• Iesirile sunt FUNCTII LOGICE ale STARILOR (vectorului de stari) si ALE INTRARILOR•ATENTIE: PARAZIȚI (SPIKE, GLITCH) ÎN SEMNALELE DE IESIRE!• SPIKE: SEMNAL ACTIV PE O DURATĂ MAI SCURTĂ DECÂT PERIOADA DE TACT – POATE GENERA DISFUNCŢIUNI ÎN SISTEMUL DIGITAL

Automate Secvenţiale

Page 35: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 35

• MEALY• Avantaje:

• Cel mai flexibil (usor de modificat)• Dezavantaje:

• Posibilitatea apariției paraziților in semnalele de ieșire• Pot exista căi cu timpi de propagare lungi (reduce perioada minima a tactului• PERICOLUL APARIŢIEI BUCLELOR COMBINAŢIONALE!!!

Logica Iesire

Logica Stare

Out1

>

D QIN1

CLK

RESET

Proces Secvential

Proces Combinational

Logica Iesire

Logica Stare

Out2

<

DQIN2

CLK

RESET

Proces Secvential

Proces Combinational

Automate Secvenţiale

Page 36: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 36

• MEALY

• Pentru Evitarea PARAZITILOR SI A BUCLELOR COMBINATIONALE: IESIRI INREGISTRATE

• Doua Variante:

Logica Iesire

Logica Stare

Out

>

D QIN

CLK

RESET

Proces Secvential

Proces Combinational

>

D Q

Logica Iesire

Logica Stare

Out

>

D QIN

CLK

RESET

Proces Secvential

Proces Combinational

>

D Q

Automate Secvenţiale

Page 37: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 37

• MEALY• Pentru Evitarea PARAZITILOR SI A BUCLELOR COMBINATIONALE: IESIRI INREGISTRATE

• Prima varianta://semnale interne Y_INT, Z_INTwire Y_INT, Z_INT; …..assign Y_INT = ((StC==CONTINUARE) && (!(A | B))) || ((StC==SFARSIT) && (!(A & B))) ? 1:0;assign Z_INT = ((StC==INCEPUT) && (A & B)) || (StC==CONTINUARE) || ((StC==SFARSIT && (A & B)) ? 1:0;

always @ (posedge CLK) beginY<=Y_INT;Z<= Z_INT;end

Rezultatul simularii: Ieșirile – întârziate cu un tact, pot rezulta valori nedorite/nepresupuse

Logica Iesire

Logica Stare

Out

>

D QIN

CLK

RESET

Proces Secvential

Proces Combinational

>

D Q

Automate Secvenţiale

Page 38: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 38

• MEALY• Pentru Evitarea PARAZITILOR SI A BUCLELOR COMBINATIONALE: IESIRI INREGISTRATE

• A doua varianta: ATRIBUIRE PE StNwire Y_INT, Z_INT; …..assign Y_INT = ((StN==CONTINUARE) && (!(A | B))) || ((StN==SFARSIT) && (!(A & B))) ? 1:0;assign Z_INT = ((StN==INCEPUT) && (A & B)) || (StN==CONTINUARE) || ((StN==SFARSIT && (A & B)) ? 1:0;

always @ (posedge CLK) beginY<=Y_INT;Z<= Z_INT;end

Rezultatul simularii: Ieșirile – Sincrone cu tactul si cu starea, fără paraziți

Logica Iesire

Logica Stare

Out

>

D QIN

CLK

RESET

Proces Secvential

Proces Combinational

>

D Q

Automate Secvenţiale

Page 39: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 39

• MEALY• Pentru Evitarea PARAZITILOR SI A BUCLELOR COMBINATIONALE: IESIRI INREGISTRATE

• A doua varianta: ATRIBUIRE PE StN

Rezultatul simularii: Ieșirile – Sincrone cu tactul si cu starea, fără paraziți

• Dezavantaje: • Ieșirea circuitului combinațional, E ÎNCĂRCATĂ, care determină timpi de întârzieri mai mari

Logica Iesire

Logica Stare

Out

>

D QIN

CLK

RESET

Proces Secvential

Proces Combinational

>

D Q

Automate Secvenţiale

Page 40: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 40

•MEDVEDEV MODIFICAT

• Ieșirile reprezintă Semnale din Vectorul Stărilor• însă stările sunt codificate după conveniența proiectantului

•Exemplu://Prima coloană reprezintă stările, cea de-a doua coloană reprezintă ieșirileparameter INCEPUT = {2’b00, 2’b00};parameter CONTINUARE = {2’b01, 2’b11};parameter SFARSIT = {2’b11, 2’b01};reg [3:0] StC, StN = INCEPUT;

assign Y<=StC [1]; assign Z<=StC [0];

always @ (posedge CLK or posedge RESET) if (RESET) StC<=INCEPUT; else StC <= NEXTSTATE;

always @ * begin StN <= StC; case (StC) INCEPUT: if (!(A | B)) StC<=CONTINUARE; CONTINUARE : if (A & B) StC<=SFARSIT; SFARSIT: if (A ^ B) StC<=INCEPUT; default: StC<=INCEPUT; endcase;end

Automate Secvenţiale

RESET

A XOR B ='1' A='0' AND B='0'

A='1' AND B='1'

SFARSITY<='0';Z<='1'

INCEPUTY<='0'; Z<='0'

CONTINUAREY<=‘1';Z<=‘1'

Page 41: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 41

•MEDVEDEV MODIFICAT

• Ieșirile reprezintă Semnale din Vectorul Stărilor• însă stările sunt codificate după conveniența proiectantului

• Avantaje:• Ieșirile sunt înregistrate• Automatul secvențial este rapid și posedă toate avantajele automatelor secvențiale Medvedev

•Dezavantaje:• Automatul este totuși rigid și necesită o specificare foarte precisă• Semnale suplimentare față de stări:

• Automatul secvențial în implementare ocupă mai mulți regiștri decât în cazul unei implementări Medvedev

Automate Secvenţiale

RESET

A XOR B ='1' A='0' AND B='0'

A='1' AND B='1'

SFARSITY<='0';Z<='1'

INCEPUTY<='0'; Z<='0'

CONTINUAREY<=‘1';Z<=‘1'

Page 42: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 42

• Moduri de codificare a stărilor:

• Diverse moduri:• Binar: 000, 001, 010, 011, …. – compact, cele mai putine

bistabile rezultate

• One-Hot: 000, 001, 010, 100,… - rapid, rezulta logica minima

• Gray: 000, 001, 011, 010, …. – un singur bit se schimba: probabilitate foarte mica de hazard

• Johnson: 000, 001, 011, 111, 110, … - e atât rapid cât și cu probabilitate de hazard foarte mică

• Cel mai convenient mod de codificare: prin tipuri de enumerare, iar codificarea sa fie lasata pentru sintetizator

• În meniu: Synthesize, options, HDL options, FSM encoding style: se poate alege tipul de codificare, inclusiv USER şi NONE!

• În cod: (* FSM_ENCODING=“<CODING_STYLE>“ *)

• Întotdeauna ÎNAINTEA definirii semnalului de stare!!!

Automate Secvenţiale

Page 43: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 43

• Moduri de codificare a stărilor:

• În cod: (* FSM_ENCODING=“<CODING_STYLE>“ *)

• Întotdeauna ÎNAINTEA definirii semnalului de stare!!!

• Exemplu:(* FSM_ENCODING=“ONE_HOT“ *) reg [4:0] StC, StN;

• Coding STYLE (Encoding Algorithm)• Auto (default)• One-Hot• Compact• Sequential• Gray• Johnson• Speed1• User

• Coding STYLE poate fi specificat și în cadrul fișierului de constrângeri .xcf

Automate Secvenţiale

Page 44: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 44

• Observație: Synthesis ReportSynthesizing Unit <State_machine_1>.Related source file is "State_machine_1.v". Found finite state machine <FSM_0> for signal <StC>. ----------------------------------------------------------------------- | States | 4 | | Transitions | 7 | | Inputs | 2 | | Outputs | 3 | | Clock | CLK (rising_edge) | | Reset | RESET (positive) | | Reset type | synchronous | | Reset State | 0000 | | Encoding | automatic | | Implementation | LUT | ----------------------------------------------------------------------- Summary: inferred 1 Finite State Machine(s). Unit <State_machine_1> synthesized.

Automate Secvenţiale

Page 45: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 45

• Observație: Synthesis Report

============================================================== * Advanced HDL Synthesis * ==============================================================Analyzing FSM <FSM_0> for best encoding.Optimizing FSM <StC/FSM> on signal <StC[1:2]> with gray encoding. ------------------- State | Encoding ------------------- 0000 | 00 0101 | 01 1010 | 11 1111 | 10 -------------------

Automate Secvenţiale

Page 46: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 46

• Opțiuni de Sinteză legate de Automate Secvențiale

• Automatic Finite State Machine (FSM) Extraction (FSM_EXTRACT)• Yes sau No

• Finite State Machine (FSM) Style (FSM_STYLE)• LUT sau BRAM

• Finite State Machine (FSM) Encoding Algorithm (FSM_ENCODING)• Binary, One-hot etc

• Enumerated Encoding (ENUM_ENCODING)• Numai in cazul VHDL

• Safe Implementation (SAFE_IMPLEMENTATION)• Yes sau No

• Safe Recovery State (SAFE_RECOVERY_STATE)• (* safe_recovery_state = "<value>" *)• Exemplu: (* safe_recovery_state = “Shiftl0" *)

Automate Secvenţiale

Page 47: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 47

•Safe Implementation:•Automatele secventiale: datorita unor perturbatii, pot ajunge in stari nedefinite (eronate), lucru care trebuie evitat, de exemplu prin resetare

•Moduri de codificare a starilor: Evitarea blocarii in Starile Eronate

• Cel mai convenient mod de codificare: prin tipuri de enumerare, iar codificarea sa fie lasata pentru sintetizator

• Exemplu: case (StC)

INCEPUT: ……CONTINUARE: ….SFARSIT: …..default: => StN<=Inceput;// daca codificarea: Binar sau GRAY, rezulta: pe doi biti// Rezulta numarul maxim de stari: 4 // In cazul aparitiei starii nedorite, default reseteaza // automatul secvential ? Default tine doar de implementarea case

endcase //Doar daca (* SAFE_IMPLEMENTATION = YES *)

Automate Secvenţiale

Page 48: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 48

• Moduri de codificare a starilor:

• Alta Solutie:Introducerea de stari eronate manual, exemplu:• Aceasta Solutie poate fi si simulata!

Parameter Eronat = 2’b10case (StC)

INCEPUT: ……CONTINUARE: ….SFARSIT: …..ERONAT: …..

default: StN<=Inceput Dezavantaje:• In cazul codificarii binare sau Gray pe N biti: ca sa se acopere toate starile, ar fi necesar un numar de stari eronate de 2N-Numar_Stari_Corecte!• In cazul codificarii One-Hot: Prin introducera de stari suplimentare, creste numarul de biti, implicit numarul de stari eronate posibile. Rezulta: NU SE POT ACOPERI STARILE ERONATE!• Solutie: Pentru automat secvential stabil, metoda recomandata:• Codificare manuala, de tip Medvedev sau Medvedev modificat

Automate Secvenţiale

Page 49: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 49

• Moduri de codificare a starilor: Cat conteaza codificarea in viteza circuitului? Cat conteaza tipul de numarator folosit?• Exemplul 1: Numarator in cod BCD, pe 8 biti, descris in Behavioral (8 bistabile, 256 stari): (SPARTAN 2E XC2S200E)always @ (posedge CLK)if (CE) if (Q==255) Q<=0; else Q<=Q+1;Synthesis Report : Minimum period: 6.795ns (Maximum Frequency: 147.167MHz)

•Exemplul 2: Numarator in cod JOHNSON, pe 8 biti, descris in Behavioral (8 bistabile, 16 stari): (SPARTAN 2E XC2S200E)always @ (posedge CLK)if (CE) Q [7:0] <= {Q[6:0], ~Q[0]};

Synthesis Report : Minimum period: 4.254ns (Maximum Frequency: 235.073MHz)

Automate Secvenţiale

Page 50: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 50

• Cat conteaza tipul de codificare folosit?• Exemplul 3: Numarator in cod BINAR, pe 8 biti, descris in Behavioral (8 bistabile, 256 stari, cu insumare)always @ (posedge CLK)if (CE) Q<=Q+1;

Synthesis Report : Minimum period: 4.845ns (Maximum Frequency: 206.398MHz)-Mai rapid decat Exemplul 1, insa mai incet decat Exemplul 2-Din ce cauză?

Automate Secvenţiale

Page 51: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 51

• Cat conteaza tipul codificare folosit?• Exemplul 4: Numarator in cod JOHNSON, pe 8 biti, PRIMITIVA XILINX (componenta CJ8CE):

•Synthesis Report: Minimum period: 4.254ns (Maximum Frequency: 235.073MHz)

Automate Secvenţiale

Page 52: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 52

• Moduri de codificare a starilor: Cât contează codificarea în viteza circuitului? Cât contează tipul de numarator folosit?

• Automate secventiale: • Contează modul în care sunt concepute tranzitiile (timpul de propagare prin logica combinationala care determina urmatoarea stare)

• Codificarea One-HOT, Johnson: mai rapide

• Numaratoare

• În cod binar, descris cu behavioral: Fmax= 147.167MHz

• În cod Johnson, descris ori cu behavioral, ori prin folosirea unei primitive XILINX: Fmax= 235.073MHz

• În cod binar, descris simplificat: Fmax= 206.398MHz

• În cod binar, folosind primitiva CB8CE: Fmax= 117.233MHz

• Observatie: toate sintezele au fost efectuate pe Spartan IIE XC2S200E-PQ208-6. Pe alte componente, timpii de propagare sunt diferiti (mai mari sau mai mici)

Automate Secvenţiale

Page 53: 8. State Machines (also called Finite State Machines (FSM), Automaton)

Slide 53

• Concluzii:• Timpii de propagare sunt determinaţi în principal de circuitele combinaţionale

• Circuite combinaţionale mai complexe şi mai multe rezultă în timpi de propagare mai mari

• Codificare binară, Gray: Mai multă logică combinaţională, frecvenţă de lucru mai redusă

• Gray: Doar un singur bit se schimba la o singura tranziție! Pericol de hazard mai redus

• Codificare One-Hot, Johnson: mai puţine circuite combinaţionale, frecvenţă de lucru mărită

Automate Secvenţiale