2_Finite State Machines,Interfacing lectureA

download 2_Finite State Machines,Interfacing lectureA

of 14

Transcript of 2_Finite State Machines,Interfacing lectureA

  • 8/7/2019 2_Finite State Machines,Interfacing lectureA

    1/14

    CENG 563

    Real-Time and Embedded System Design

    Lecture 3-A

    Finite State Machines

    Asst. Prof. Tolga Ayav, Ph.D.

    Department of Computer Engineering

    zmir Institute of Technology

    1

  • 8/7/2019 2_Finite State Machines,Interfacing lectureA

    2/14

    Finite State Machines

    Finite state machines (FSMs) are powerful design elements used to implementalgorithms in hardware.

    Z is a set of states {z0, z1, ... , zl},

    X is a set of inputs {x0, x1, ..., xm},

    Y is a set of outputs {y0, y1, ..., yn},

    is a next-state function (i.e., transitions), mapping states and inputs

    to states, (Z x X Y) is an output function, mapping current states to outputs (Z Y),

    and

    z0 is an initial state.

    An FSM is a 6-tuple, , where:

    zmir Institute of Technology Real-Time and Embedded System Design

  • 8/7/2019 2_Finite State Machines,Interfacing lectureA

    3/14

    Moore and Mealy FSMs

    zmir Institute of Technology Real-Time and Embedded System Design

  • 8/7/2019 2_Finite State Machines,Interfacing lectureA

    4/14

    FSM Model: Moore

    zmir Institute of Technology Real-Time and Embedded System Design

    Outputs depends on states. : (Z Y)

  • 8/7/2019 2_Finite State Machines,Interfacing lectureA

    5/14

    FSM Model: Mealy

    Outputs depends on states and inputs. : (X x Z Y)

    zmir Institute of Technology Real-Time and Embedded System Design

  • 8/7/2019 2_Finite State Machines,Interfacing lectureA

    6/14

    FSMD (Finite-state machines withdatapaths)

    When using an FSM for embedded system design, the inputs andoutputs represent boolean data types, and the functions therefore representboolean functions with boolean operations. This model is sufficient for purelycontrol systems that do not input or output data. However, when we mustdeal with data, two new features would be helpful: more complex data types(such as integers or floating point numbers), and variables to store data.

    An FSMD is a 7-tuple, , where:

    Z is a set of states {z0, z1, ... , zl},

    X is a set of inputs {x0, x1, ..., xm},

    Y is a set of outputs {y0, y1, ..., yn},

    V is a set of variables {v0, v1, ..., vn},

    is a next-state function (i.e., transitions), mapping states and inputsto states, (Z x X Y)

    is an output function, mapping current states to outputs (Z Y),

    and

    z0 is an initial state.

    zmir Institute of Technology Real-Time and Embedded System Design

  • 8/7/2019 2_Finite State Machines,Interfacing lectureA

    7/14

    FSMD (Finite-state machines withdatapaths)

    Various data types are allowed (like the ones in a typicalprogramming language)

    During execution of the model, the complete system state consistsnot only of the current state, but also the values of all variables

    and includes arithmetic operations, not only booleanoperations.

    also includes variable updates.

    zmir Institute of Technology Real-Time and Embedded System Design

  • 8/7/2019 2_Finite State Machines,Interfacing lectureA

    8/14

    Describing a system as astate machine

    1. List all possible states, giving each a descriptive name.

    2. Declare all variables.3. For each state, list the possible transitions, with associatedconditions, to other states.

    4. For each state and/or transition, list the associated actions

    5. For each state, ensure that exiting transition conditions areexclusive (no two conditions could be true simultaneously) and

    complete (one of the conditions is true at any time).

    zmir Institute of Technology Real-Time and Embedded System Design

  • 8/7/2019 2_Finite State Machines,Interfacing lectureA

    9/14

    State Machine in C

    Design a Simple Seat Belt Controller:

    The controller's job is to turn on a buzzer if a person sits in a seat and does notfasten the seat belt within a fixed amount of time. This system has three inputsand one output.The inputs are a sensor for the seat to know when a person has sat down, a seatbelt sensor that tells when the belt is fastened, and a timer that goes off when the

    required time interval has elapsed. The output is the buzzer

    seat1: person sat down0: no person

    belt1: seat belt fastened0: not fastened

    timer1: expired0: not expired

    timer_on

    buzzer

    Seat belt

    controller

    zmir Institute of Technology Real-Time and Embedded System Design

  • 8/7/2019 2_Finite State Machines,Interfacing lectureA

    10/14

    State Diagram forthe Seat Belt Controller

    zmir Institute of Technology Real-Time and Embedded System Design

  • 8/7/2019 2_Finite State Machines,Interfacing lectureA

    11/14

    C Code:#define IDLE 0#define SEATED 1

    #define BELTED 2#define BUZZER 3

    void F S M ( ){ s wi t c h(state) { case IDLE:

    if( seat) {state=SEATED; timer_on=1; } break; case SEATED:

    if( belt) state=BELTED; else if( timer) state=BUZZER;

    break; case BELTED:

    if( ! seat) state=IDLE; else if( ! belt) state=SEATED;

    break; case BUZZER: if( belt) st ate=BELTED;

    else if( ! seat) state=IDLE; break; }

    }

    State diagram

    void m a i n ( ){

    while( 1 ) F SM( ) ;

    }

    zmir Institute of Technology Real-Time and Embedded System Design

  • 8/7/2019 2_Finite State Machines,Interfacing lectureA

    12/14

    Adding Hierarchy/Concurrency:HCFSM

    (a) three-state example without hierarchy, (b) same example with hierarchy,

    (c) concurrency.

    In the seatbelt controller example, assume that any state goes to a newly definedstate when a pust button is pressed. This requires many transitions in the diagram.

    Seatbelt controller, cruise controller etc. must be run in concurrency.

    zmir Institute of Technology Real-Time and Embedded System Design

  • 8/7/2019 2_Finite State Machines,Interfacing lectureA

    13/14

    Program State Machine (PSM)

    The program-state machine (PSM) model extends state machines to allow use ofsequential program code to define a states actions (including extensions for complex datatypes and variables), as well as including the hierarchy and concurrency extensions ofHCFSM.

    zmir Institute of Technology Real-Time and Embedded System Design

  • 8/7/2019 2_Finite State Machines,Interfacing lectureA

    14/14

    1. Construct an FSMD model of automobilecruise controller. Define the sets of FSMD (For

    example; V={SA, SD, G, K} etc.) and draw thediagram.

    2. Write a C program implementing this FSMD.

    3. Write a C program running this FSMD andthe seatbelt controller FSM concurrently.

    Assignment 2

    zmir Institute of Technology Real-Time and Embedded System Design