25
Finite State Machines CS 3410 Computer System Organization & Programming [K. Bala, A. Bracy, E. Sirer, and H. Weatherspoon]

Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Finite State Machines

CS 3410Computer System Organization & Programming

[K. Bala, A. Bracy, E. Sirer, and H. Weatherspoon]

Page 2: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Stateful ComponentsCombinational logic• Output computed directly from inputs• System has no internal state• Nothing depends on the past!

Need:• to record data• to build stateful circuits• a state-holding device

Enter: Sequential Logic & Finite State Machines2

Inputs Combinationalcircuit

OutputsN M

Page 3: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Finite State MachinesAn electronic machine which has◦external inputs◦externally visible outputs◦ internal state

Output and next state depend on◦inputs◦current state

3

Page 4: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Automata ModelFinite State Machine

◦ inputs from external world◦ outputs to external world◦ internal state◦ combinational logic

4

Next State

Current State

Input

OutputRe

gist

ers

Comb.Logic

Page 5: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

FSM Example

5

Legend

state

input/outputstartstate

A B

0/01/0 0/0

1/1

Input: 1 or 0Output: 1 or 0States: A or B

What input pattern is the FSM “looking for”?

[Mealy Machine]

state

Page 6: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Mealy MachineGeneral Case: Mealy Machine

Outputs and next state depend on bothcurrent state and input

6

Next State

Current State

Input

OutputRe

gist

ers

Comb.Logic

Page 7: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Moore MachineOutputs depend only on current state

7

Next State

Current State

Input

OutputRe

gist

ers Comb.

Logic

Comb.Logic

A0

B0

0

1

0

1Legend

stateout

input

stateout

C1

1

0

startstate

Page 8: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

xkcd

8

Page 9: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Why FSMs?They help us reason about complex behavior.

9https://learnyousomeerlang.com/finite-state-machines

barks wags tail

sits

gets petted

waits

gets pettedsees squirrel

Clicker Question: What kind of machine is this?(A) Mealy(B) Moore(C) Neither

doesn’t give a crap about you

any event ever!

Page 10: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Other FSMs

10

Tennis

https://www.c-sharpcorner.com/article/understanding-state-design-pattern-by-implementing-finite-state/

http://blog.mikemccandless.com/2014/08/scoring-tennis-using-finite-state.html

Page 11: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Add two infinite input bit streams◦streams sent with least-significant-bit (lsb) first

Clicker Question: How many states are needed to represent this FSM? (a) 0 (d) 3(b) 1 (e) 4(c) 2

Activity: Build a Circuit for a Serial Adder

11

…10110

…01111…00101

Page 12: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Strategy for Building an FSM

(1) Draw a state diagram(2) Write output and next-state tables(3) Encode states, inputs, and outputs as bits(4) Determine logic equations for next state

and outputs(5) Draw the circuit

12

Page 13: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Step 1: State Diagram

13

…10110…01111

…00101

S0

States:Inputs: a and b (drawn as 2-bit input ab)Output: z

Legend

state

input/outputstartstate

state

Page 14: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Strategy for Building an FSM

(1) Draw a state diagram(2) Write output and next-state tables(3) Encode states, inputs, and outputs as bits(4) Determine logic equations for next state

and outputs(5) Draw the circuit

15

Page 15: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Step 2: Output & Next State Tables

16

a bCurrState

sz

Next State

s’

Page 16: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Strategy for Building an FSM

(1) Draw a state diagram(2) Write output and next-state tables(3) Encode states, inputs, and outputs as bits(4) Determine logic equations for next state

and outputs(5) Draw the circuit

18

Page 17: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Step 3: Create Bit Encoding

19

Encode states as bitsS0 = S1 = 2 states à 1-bit enough

a bCurrState

sz Next

State s’

Copy from previous

Make a binary encoding instead of

names

(1-hot also an option)

Page 18: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Strategy for Building an FSM

(1) Draw a state diagram(2) Write output and next-state tables(3) Encode states, inputs, and outputs as bits(4) Determine logic equations for next state

and outputs(5) Draw the circuit

21

Page 19: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Step 4: Create Logic Equations

22

Determine logic equations for next state and outputs

s’ =

z =

Page 20: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Strategy for Building an FSM

(1) Draw a state diagram(2) Write output and next-state tables(3) Encode states, inputs, and outputs as bits(4) Determine logic equations for next state

and outputs(5) Draw the circuit: Simplify first!

24

Page 21: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

Step 5: Draw the Circuit

25

Current State

ab

D Q sz

s'

s'

Next State

Output

Page 22: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

FSMs in a Processor?•multi-cycle (non-pipelined) processor

27

Page 23: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

FSMs in a Processor?•multi-cycle (non-pipelined) processor • handling cache misses, branch mispredictions, interrupts• tracking the state of data in your cache (cache coherency)

28

Page 24: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

29

Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary numbers, entered into the FSM one digit at a time, beginning with the most significant digit. The output, Z, should be the larger of the two numbers. Example: A = 1000 and B = 0101, then Z = 1000 (the value of A).

Draw the state transition diagram (states & arrows) that expresses this FSM. Use the notation AB for inputs (10 means A = 1 and B = 0).

(1) Draw a state diagram(2) Write output and next-state tables(3) Encode states, inputs, and outputs as bits(4) Determine logic equations for next state and outputs(5) Draw the circuit

Page 25: Finite State Machines - Cornell University€¦ · 29 Consider a finite state machine that takes two inputs, A and B, and generates a single output, Z. Inputs are unsigned binary

30

Clicker Question: How many states are needed to represent this FSM? (a) 0 (d) 3(b) 1 (e) 4(c) 2