ECE448 Lecture6 FSM

Embed Size (px)

Citation preview

  • 8/11/2019 ECE448 Lecture6 FSM

    1/77

    George Mason University

    Finite State Machines

    State Diagrams,State Tables,

    Algorithmic State Machine (ASM) Charts,and VHDL Code

    ECE 448

    Lecture 6

  • 8/11/2019 ECE448 Lecture6 FSM

    2/77

    2

    Required reading

    P. Chu, FPGA Prototyping by VHDL ExamplesCh apt er 5, FSM

  • 8/11/2019 ECE448 Lecture6 FSM

    3/77

    3

    Recommended reading

    S. Brown and Z. Vranesic, Fundamentals of Digital Logic with VHDL Design

    Chapter 8 , Sync hro no us Sequ ent ia l Ci rcui t s

    Sect io n s 8.1-8.5Sect ion 8 .10, A lgo ri th m ic State Machin e (A SM)

    Charts

  • 8/11/2019 ECE448 Lecture6 FSM

    4/77

    4

    Datapathvs.

    Controller

  • 8/11/2019 ECE448 Lecture6 FSM

    5/77

    5

    Structure of a Typical Digital System

    Datapath(ExecutionUnit)

    Controller(ControlUnit)

    Data Inputs

    Data Outputs

    Control & Status Inputs

    Control & Status Outputs

    ControlSignals

    Status

    Signals

  • 8/11/2019 ECE448 Lecture6 FSM

    6/77

    6

    Datapath (Execution Unit) Manipulates and processes data Performs arithmetic and logic operations,

    shifting/rotating, and other data-processingtasks

    Is composed of registers, multiplexers, adders,decoders, comparators, ALUs, gates, etc.

    Provides all necessary resources and

    interconnects among them to perform specifiedtask Interprets control signals from the Controller

    and generates status signals for the Controller

  • 8/11/2019 ECE448 Lecture6 FSM

    7/777

    Controller (Control Unit)

    Controls data movement in the Datapath byswitching multiplexers and enabling or disablingresources

    Example: enable signals for registersExample: select signals for muxes

    Provides signals to activate various processingtasks in the Datapath

    Determines the sequence of operationsperformed by the Datapath Follows Some Program or Schedule

  • 8/11/2019 ECE448 Lecture6 FSM

    8/778

    Programmable vs. Non-Programmable Controller

    Controller can be programmable or non-programmable Programmable

    Has a program counter which points to next instruction Instructions are held in a RAM or ROM

    Microprocessor is an example of programmablecontroller Non-Programmable

    Once designed, implements the same functionality

    Another term is a hardwired state machine, orhardwired FSM, or hardwired instructions

    In this course we will be focusing on non-programmable controllers.

  • 8/11/2019 ECE448 Lecture6 FSM

    9/779

    Finite State Machines Controllers can be described as Finite State

    Machines (FSMs) Finite State Machines can be represented using

    State Diagrams and State Tables - suitable

    for simple controllers with a relatively fewinputs and outputs Algorithmic State Machine (ASM) Charts -

    suitable for complex controllers with a largenumber of inputs and outputs

    All of these descriptions can be easily translatedto the corresponding synthesizable VHDL code

  • 8/11/2019 ECE448 Lecture6 FSM

    10/7710

    Labs in This Class

    Datapath

    (ExecutionUnit)

    Controller

    (ControlUnit)

    Data Inputs

    Data Outputs

    Control & Status Inputs

    Control & Status Outputs

    ControlSignals

    StatusSignals

    Lab 2: Combinational DatapathLab 3: Sequential Datapath

    Lab 4: Primarily the Controller

    Labs 5-7 will include both Datapath and Controller

  • 8/11/2019 ECE448 Lecture6 FSM

    11/7711

    Hardware Design with RTL VHDL

    Pseudocode

    Datapath Controller

    Blockdiagram

    Blockdiagram

    State diagramor ASM chart

    VHDL code VHDL code VHDL code

    Interface

  • 8/11/2019 ECE448 Lecture6 FSM

    12/77

    Steps of the Design Process1. Text description2. Interface3. Pseudocode4. Block diagram of the Datapath5. Interface divided into Datapath and Controller6. ASM chart of the Controller

    7. RTL VHDL code of the Datapath, Controller, andTop-Level Unit

    8. Testbench for the Datapath, Controller, and Top-LevelUnit

    9. Functional simulation and debugging10. Synthesis and post-synthesis simulation11. Implementation and timing simulation12. Experimental testing using FPGA board

    12

  • 8/11/2019 ECE448 Lecture6 FSM

    13/77

    1. Text description

    2. Interface3. Pseudocode4. Block diagram of the Datapath5. Interface divided into Datapath and Controller6. ASM chart of the Controller7. RTL VHDL code of the Datapath, Controller , and

    Top-level Unit8. Testbench for the Datapath, Controller, and Top-Level

    Unit9. Functional simulation and debugging10. Synthesis and post-synthesis simulation11. Implementation and timing simulation12. Experimental testing using FPGA board

    Steps of the Design ProcessIntroduced in Class Today

    13

  • 8/11/2019 ECE448 Lecture6 FSM

    14/7714

    Finite State MachinesRefresher

  • 8/11/2019 ECE448 Lecture6 FSM

    15/7715

    Finite State Machines (FSMs) An FSM is used to model a system that transits

    among a finite number of internal states. Thetransitions depend on the current state and externalinput.

    The main application of an FSM is to act as thecontroller of a medium to large digital system

    Design of FSMs involves Defining states

    Defining next state and output functions Optimization / minimization

    Manual optimization/minimization is practical forsmall FSMs only

  • 8/11/2019 ECE448 Lecture6 FSM

    16/7716

    Moore FSM

    Output is a Function of the Present State Only

    Present Stateregister

    Next Statefunction

    Outputfunction

    Inputs

    Present StateNext State

    Outputs

    clockreset

  • 8/11/2019 ECE448 Lecture6 FSM

    17/7717

    Mealy FSM Output is a Function of the Present State and the

    Inputs

    Next Statefunction

    Outputfunction

    Inputs

    Present StateNext State

    Outputs

    Present Stateregister

    clockreset

  • 8/11/2019 ECE448 Lecture6 FSM

    18/7718

    State Diagrams

  • 8/11/2019 ECE448 Lecture6 FSM

    19/7719

    Moore Machine

    state 1 /output 1

    state 2 /output 2

    transitioncondition 1

    transitioncondition 2

  • 8/11/2019 ECE448 Lecture6 FSM

    20/7720

    Mealy Machine

    state 1 state 2

    transition condition 1 /output 1

    transition condition 2 /output 2

  • 8/11/2019 ECE448 Lecture6 FSM

    21/7721

    Moore FSM - Example 1

    Moore FSM that Recognizes Sequence 10

    S0 / 0 S1 / 0 S2 / 1

    00

    0

    1

    11

    reset

    Meaningof states:

    S0: Noelementsof thesequenceobserved

    S1: 1 observed

    S2: 10 observed

  • 8/11/2019 ECE448 Lecture6 FSM

    22/7722

    Mealy FSM - Example 1

    Mealy FSM that Recognizes Sequence 10

    S0 S1

    0 / 0 1 / 0 1 / 0

    0 / 1reset

    Meaningof states:

    S0: Noelementsof thesequenceobserved

    S1: 1 observed

  • 8/11/2019 ECE448 Lecture6 FSM

    23/77

    23

    Moore & Mealy FSMs Example 1

    clock

    input

    Moore

    Mealy

    0 1 0 0 0

    S0 S0 S1 S2 S0 S0

    S0 S0 S1 S0 S0 S0

    state

    output

    state

    output

  • 8/11/2019 ECE448 Lecture6 FSM

    24/77

    24

    Moore vs. Mealy FSM (1)

    Moore and Mealy FSMs Can BeFunctionally Equivalent Equivalent Mealy FSM can be derived from

    Moore FSM and vice versa Mealy FSM Has Richer Description and

    Usually Requires Smaller Number of States

    Smaller circuit area

  • 8/11/2019 ECE448 Lecture6 FSM

    25/77

    25

    Moore vs. Mealy FSM (2)

    Mealy FSM Computes Outputs as soon asInputs Change Mealy FSM responds one clock cycle sooner

    than equivalent Moore FSM Moore FSM Has No Combinational Path

    Between Inputs and Outputs

    Moore FSM is less likely to affect the criticalpath of the entire circuit

  • 8/11/2019 ECE448 Lecture6 FSM

    26/77

    26

    Moore vs. Mealy FSM (3)

    Types of control signal Edge sensitive

    E.g., enable signal of counter

    Both can be used but Mealy is faster

    Level sensitive E.g., write enable signal of SRAM Moore is preferred

  • 8/11/2019 ECE448 Lecture6 FSM

    27/77

    27

    Which Way to Go?

    Safer.Less likely to affect

    the critical path.

    Mealy FSM Moore FSM

    Lower Area

    Responds one clockcycle earlier

    Fewer states

  • 8/11/2019 ECE448 Lecture6 FSM

    28/77

    28

    Finite State Machinesin VHDL

  • 8/11/2019 ECE448 Lecture6 FSM

    29/77

    29

    FSMs in VHDL Finite State Machines Can Be Easily

    Described With Processes Synthesis Tools Understand FSM

    Description if Certain Rules Are Followed State transitions should be described in a

    process sensitive to clock and asynchronousreset signals only

    Output function described using rules forcombinational logic, i.e. as concurrentstatements or a process with all inputs in thesensitivity list

  • 8/11/2019 ECE448 Lecture6 FSM

    30/77

  • 8/11/2019 ECE448 Lecture6 FSM

    31/77

    31

    Mealy FSM

    Next Statefunction

    Outputfunction

    Inputs

    Present StateNext State

    Outputs

    Present StateRegister

    clockreset

    process(clock, reset)

    concurrentstatements

  • 8/11/2019 ECE448 Lecture6 FSM

    32/77

    32

    Moore FSM - Example 1

    Moore FSM that Recognizes Sequence 10

    S0 / 0 S1 / 0 S2 / 1

    0

    0

    0

    1

    11

    reset

  • 8/11/2019 ECE448 Lecture6 FSM

    33/77

    33

    Moore FSM in VHDL (1)

    TYPE state IS (S0, S1, S2);SIGNAL Moore_state: state;

    U_Moore: PROCESS (clock, reset)BEGIN

    IF(reset = 1) THEN Moore_state IF input = 1 THEN

    Moore_state

  • 8/11/2019 ECE448 Lecture6 FSM

    34/77

    34

    Moore FSM in VHDL (2)

    WHEN S1 =>IF input = 0 THEN

    Moore_state

  • 8/11/2019 ECE448 Lecture6 FSM

    35/77

    35

    Mealy FSM - Example 1

    Mealy FSM that Recognizes Sequence 10

    S0 S1

    0 / 0 1 / 0 1 / 0

    0 / 1reset

  • 8/11/2019 ECE448 Lecture6 FSM

    36/77

    36

    Mealy FSM in VHDL (1)

    TYPE state IS (S0, S1);SIGNAL Mealy_state: state;

    U_Mealy: PROCESS(clock, reset)BEGIN

    IF(reset = 1) THEN Mealy_state IF input = 1 THEN

    Mealy_state

  • 8/11/2019 ECE448 Lecture6 FSM

    37/77

    37

    Mealy FSM in VHDL (2)

    WHEN S1 =>IF input = 0 THEN

    Mealy_state

  • 8/11/2019 ECE448 Lecture6 FSM

    38/77

    38

    Algorithmic State Machine (ASM)Charts

  • 8/11/2019 ECE448 Lecture6 FSM

    39/77

    39

    Algorithmic State Machine

    Algorithmic State Machine representation of a Finite State Machine

    suitable for FSMs with a larger number ofinputs and outputs compared to FSMsexpressed using state diagrams and state

    tables.

  • 8/11/2019 ECE448 Lecture6 FSM

    40/77

    40

    Elements used in ASM charts (1)

    Output signals or actions

    (Moore type)

    State name

    Conditionexpression

    0 (False) 1 (True)

    Conditional outputs

    or actions (Mealy type)

    (a) State box (b) Decision box

    (c) Conditional output box

  • 8/11/2019 ECE448 Lecture6 FSM

    41/77

    41

    State Box State box represents a state. Equivalent to a node in a state diagram or

    a row in a state table. Contains register transfer actions or

    output signals

    Moore-type outputs are listed inside ofthe box. It is customary to write only the name of

    the signal that has to be asserted in thegiven state, e.g., z instead of z

  • 8/11/2019 ECE448 Lecture6 FSM

    42/77

    42

    Decision Box

    Decision box indicates that agiven condition is tobe tested and theexit path is to bechosen accordingly.The condition

    expression mayinclude one or moreinputs to the FSM.

    Conditionexpression

    0 (False) 1 (True)

  • 8/11/2019 ECE448 Lecture6 FSM

    43/77

    43

    Conditional Output Box

    Conditionaloutput box

    Denotes outputsignals that are of

    the Mealy type. The condition that

    determineswhether suchoutputs aregenerated isspecified in thedecision box.

    Conditional outputsor actions (Mealy type)

    ASMs representing simple FSMs

  • 8/11/2019 ECE448 Lecture6 FSM

    44/77

    44

    ASMs representing simple FSMs

    Algorithmic state machines can model bothMealy and Moore Finite State Machines

    They can also model machines that are of

    the mixed type

  • 8/11/2019 ECE448 Lecture6 FSM

    45/77

    45

    Moore FSM Example 2: State diagram

    C z 1=

    Reset

    B z 0= A z 0= w 0=

    w 1=

    w 1=

    w 0=

    w 0= w 1=

  • 8/11/2019 ECE448 Lecture6 FSM

    46/77

    46

    Present Next state Output state w = 0 w = 1 z

    A A B 0B A C 0C A C 1

    Moore FSM Example 2: State table

  • 8/11/2019 ECE448 Lecture6 FSM

    47/77

    47

    w

    w

    w0 1

    0

    1

    0

    1

    A

    B

    C

    z

    Reset

    w

    w

    w0 1

    0

    1

    0

    1

    A

    B

    C

    z

    Reset

    ASM Chart for Moore FSM Example 2

  • 8/11/2019 ECE448 Lecture6 FSM

    48/77

    48

    USE ieee.std_logic_1164.all ;

    ENTITY simple ISPORT ( clock : IN STD_LOGIC ;

    resetn : IN STD_LOGIC ;w : IN STD_LOGIC ;z : OUT STD_LOGIC ) ;

    END simple ;

    ARCHITECTURE Behavior OF simple ISTYPE State_type IS (A, B, C) ;SIGNAL y : State_type ;

    BEGINPROCESS ( resetn, clock )BEGIN

    IF resetn = '0' THENy

  • 8/11/2019 ECE448 Lecture6 FSM

    49/77

    E l 2 VHDL d (3)

  • 8/11/2019 ECE448 Lecture6 FSM

    50/77

    50

    Example 2: VHDL code (3)

    END IF ;END PROCESS ;

    z

  • 8/11/2019 ECE448 Lecture6 FSM

    51/77

    51

    A

    w 0= z 0=

    w 1= z 1= Bw 0= z 0=

    Reset

    w 1= z 0=

    Mealy FSM Example 3: State diagram

  • 8/11/2019 ECE448 Lecture6 FSM

    52/77

    52

    ASM Chart for Mealy FSM Example 3

    w

    w

    0 1

    0

    1

    A

    B

    Reset

    z

    E l 3 VHDL d (1)

  • 8/11/2019 ECE448 Lecture6 FSM

    53/77

    53

    LIBRARY ieee ;USE ieee.std_logic_1164.all ;

    ENTITY Mealy ISPORT ( clock : IN STD_LOGIC ;

    resetn : IN STD_LOGIC ;w : IN STD_LOGIC ;

    z : OUT STD_LOGIC ) ;END Mealy ;

    ARCHITECTURE Behavior OF Mealy ISTYPE State_type IS (A, B) ;SIGNAL y : State_type ;

    BEGINPROCESS ( resetn, clock )BEGIN

    IF resetn = '0' THENy

  • 8/11/2019 ECE448 Lecture6 FSM

    54/77

    54

    Example 3: VHDL code (2)

    CASE y ISWHEN A =>

    IF w = '0' THENy

  • 8/11/2019 ECE448 Lecture6 FSM

    55/77

    55

    Example 3: VHDL code (3)

    END IF ;END PROCESS ;

    z

  • 8/11/2019 ECE448 Lecture6 FSM

    56/77

    56

    Control Unit Example: Arbiter (1)

    Arbiter

    reset

    r1

    r2

    r3

    g1

    g2

    g3

    clock

    Control Unit Example: Arbiter (2)

  • 8/11/2019 ECE448 Lecture6 FSM

    57/77

    57

    Idle

    000

    1--

    Reset

    gnt1 g1 1=

    -1-

    gnt2 g2 1=

    --1

    gnt3 g3 1=

    0-- 1--

    01--0-

    001--0

    Control Unit Example: Arbiter (2)

    Control Unit Example: Arbiter (3)

  • 8/11/2019 ECE448 Lecture6 FSM

    58/77

    58

    Control Unit Example: Arbiter (3)

    r 1r 2

    r 1r 2 r 3

    Idle

    Reset

    gnt1 g 1 1=

    gnt2 g 2 1=

    gnt3 g 3 1=

    r 1r 1

    r 1

    r 2

    r 3

    r 2

    r 3

    r 1r 2 r 3

    r 1r 2

    r 1r 2 r 3

    Idle

    Reset

    gnt1 g 1 1=

    gnt2 g 2 1=

    gnt3 g 3 1=

    r 1r 1

    r 1

    r 2

    r 3

    r 2

    r 3

    r 1r 2 r 3

    ASM Ch f C l U i E l 4

  • 8/11/2019 ECE448 Lecture6 FSM

    59/77

    59

    ASM Chart for Control Unit - Example 4

    r 1

    r 30 1

    1

    Idle

    Reset

    r 2

    r 1

    r 3

    r 2

    gnt1

    gnt2

    gnt3

    1

    1

    1

    0

    0

    0

    g 1

    g 2

    g 3

    0

    0

    1

    r 1

    r 30 1

    1

    Idle

    Reset

    r 2

    r 1

    r 3

    r 2

    gnt1

    gnt2

    gnt3

    1

    1

    1

    0

    0

    0

    g 1

    g 2

    g 3

    0

    0

    1

    E l 4 VHDL d (1)

  • 8/11/2019 ECE448 Lecture6 FSM

    60/77

    60

    Example 4: VHDL code (1)

    LIBRARY ieee;USE ieee.std_logic_1164.all;

    ENTITY arbiter ISPORT ( Clock, Resetn : IN STD_LOGIC ;

    r : IN STD_LOGIC_VECTOR(1 TO 3) ;g : OUT STD_LOGIC_VECTOR(1 TO 3) ) ;

    END arbiter ;

    ARCHITECTURE Behavior OF arbiter IS

    TYPE State_type IS (Idle, gnt1, gnt2, gnt3) ;SIGNAL y : State_type ;

    E l 4 VHDL d (2)

  • 8/11/2019 ECE448 Lecture6 FSM

    61/77

    61

    Example 4: VHDL code (2)BEGIN

    PROCESS ( Resetn, Clock )

    BEGINIF Resetn = '0' THEN y

  • 8/11/2019 ECE448 Lecture6 FSM

    62/77

    62

    Example 4: VHDL code (3)

    WHEN gnt3 =>IF r(3) = '1' THEN y

  • 8/11/2019 ECE448 Lecture6 FSM

    63/77

    63

    ASM Summary by Prof. Chu

    ASM (algorithmic state machine) chart Flowchart-like diagram Provides the same info as a state diagram More descriptive, better for complex description ASM block

    One state box One or more optional decision boxes:

    with T (1) or F (0) exit path One or more conditional output boxes:

    for Mealy output

  • 8/11/2019 ECE448 Lecture6 FSM

    64/77

  • 8/11/2019 ECE448 Lecture6 FSM

    65/77

    I t ASM Ch t

  • 8/11/2019 ECE448 Lecture6 FSM

    66/77

    66

    Incorrect ASM Charts

    Based on RTL Hardware Design by P. Chu

    G li d FSM

  • 8/11/2019 ECE448 Lecture6 FSM

    67/77

    67

    Generalized FSM

    Based on RTL Hardware Design by P. Chu

  • 8/11/2019 ECE448 Lecture6 FSM

    68/77

    68

    Alternative Coding Stylesby Dr. Chu

    (to be used with caution)

  • 8/11/2019 ECE448 Lecture6 FSM

    69/77

    VHDL Description of a FSM

    Follow the basic block diagram Code the next-state/output logic according

    to the state diagram/ASM chart Use enumerate data type for states

    69

  • 8/11/2019 ECE448 Lecture6 FSM

    70/77

    70

  • 8/11/2019 ECE448 Lecture6 FSM

    71/77

    71

  • 8/11/2019 ECE448 Lecture6 FSM

    72/77

    72

  • 8/11/2019 ECE448 Lecture6 FSM

    73/77

    73

  • 8/11/2019 ECE448 Lecture6 FSM

    74/77

    74

    C bi / l i h

  • 8/11/2019 ECE448 Lecture6 FSM

    75/77

    Combine next-state/output logic together

    75

  • 8/11/2019 ECE448 Lecture6 FSM

    76/77

    76

  • 8/11/2019 ECE448 Lecture6 FSM

    77/77