Docsity

Understanding Level-Sensitive D Latches, D Flip-Flops, and FSMs in Digital Design, Study notes of Computer Science

Toggle sidebar
bg1
1
Digital Design
Copyright © 2006
Frank Vahid
Digital Design
Chapter 3:
Sequential Logic Design -- Controllers
Slides to accompany the textbook Digital Design, First Edition,
by Frank Vahid, John Wiley and Sons Publishers, 2007.
http://www.ddvahid.com
Modified by Jay Brockman, University of Notre Dame, 2007
Copyright © 2007 Frank Vahid
Instructors of courses requiring Vahid's Digital Design textbook (published by John Wiley and Sons) havepermission to modify and use these slides for customary course-related activities,
subject to keeping this copyright notice in place and unmodified. These slides may be posted as unanimatedpdf versions on publicly-accessible course websites.. PowerPoint source (or pdf
with animations) may not be posted to publicly-accessible websites, but may be posted for students on internal protected sites or distributed directly to students by other electronic means.
Instructors may make printouts of the slides available to students for a reasonable photocopying charge, without incurring royalties. Any other use requires explicit permission. Instructors
may obtain PowerPoint source or obtain special use permissions from Wiley –see http://www.ddvahid.com for information.
2
Digital Design
Copyright © 2006
Frank Vahid
Introduction
• Sequential circuit
– Output depends not just on present inputs (as in
combinational circuit), but on past sequence of inputs
• Stores bits, also known as having “state”
– Simple example: a circuit that counts up in binary
• In this chapter, we will:
– Design a new building block, a flip-flop, that stores
one bit
– Combine that block to build multi-bit storage – a
register
– Describe the sequential behavior using a finite state
machine
– Convert a finite state machine to a controller –a
sequential circuit having a register and combinational
logic
3.1
si
z
e
ansis
Combinational
digital circuit
1
a
b
1F
0
1
a
b?F
0
Must know
sequence of
past inputs to
know output
Sequential
digital circuit
Note: Slides with animation are denoted with a small red "a" near the animated items
3
Digital Design
Copyright © 2006
Frank Vahid
Example Needing Bit Storage
• Flight attendant call button
– Press call: light turns on
•Stays on after button released
– Press cancel: light turns off
– Logic gate circuit to implement this?
Q
Call
Cancel
Doesn’t work. Q=1 when Call=1, but
doesn’t stay 1 when Call returns to 0
Need some form of “feedback” in the circuit
a
a
3.2
Bit
Storage
Blue light
Call
button
Cancel
button
1. Call button pressed – light turns on
Bit
Storage
Blue light
Call
button
Cancel
button
2. Call button released – light stays on
Bit
Storage
Blue light
Call
button
Cancel
button
3. Cancel button pressed – light turns off
Types of Storage Elements
set
reset
out data_in data_out
write
set-reset latch
(SR latch) data latch
(D latch)
Jay Brockman, 2007
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Understanding Level-Sensitive D Latches, D Flip-Flops, and FSMs in Digital Design and more Study notes Computer Science in PDF only on Docsity!

Digital DesignCopyright © 2006Frank Vahid

Digital Design

Chapter 3:

Sequential Logic Design -- Controllers

Slides to accompany the textbook

Digital Design

, First Edition,

by Frank Vahid, John Wiley and Sons Publishers, 2007.

http://www.ddvahid.com

Modified by Jay Brockman, University of Notre Dame, 2007

Copyright © 2007

Frank Vahid

Instructors of courses requiring Vahid's Digital Design textbook (published by John Wiley and Sons) have permission to modify and use these slides for customary course-related activities,subject to keeping this copyright notice in place and unmodified. These slides may be posted as unanimated pdf versions on publicly-accessible course websites.. PowerPoint source (or pdfwith animations) may

not

be posted to publicly-accessible websites, but may be posted for students on internal protected sites or distributed directly to students by other electronic means.

Instructors may make printouts of the slides available to students for a reasonable photocopying charge, without incurring royalties. Any other use requires explicit permission. Instructorsmay obtain PowerPoint source or obtain special use permissions from Wiley – see http://www.ddvahid.com for information.

Digital DesignCopyright © 2006Frank Vahid

Introduction

Sequential circuit

Output depends not just on present inputs (as incombinational circuit), but on past sequence of inputs

Stores bits, also known as having “state”

Simple example: a circuit that counts up in binary

In this chapter, we will:

Design a new building block, a

flip-flop

, that stores

one bit

Combine that block to build multi-bit storage – a register

Describe the sequential behavior using a

finite state

machine

Convert a finite state machine to a

controller

  • a

sequential circuit having a register and combinationallogic

si

ez

ansis

Combinational

digital circuit

a b

F

a b

F

Must know sequence of

past inputs to

know output

Sequential

digital circuit

Note: Slides with animation are denoted with a small red "a" near the animated items

Digital DesignCopyright © 2006Frank Vahid

Example Needing Bit Storage

Flight attendant call button

– Press call: light turns on

Stays on

after button released

– Press cancel: light turns off– Logic gate circuit to implement this?

Q

Call

Cancel

Doesn’t work. Q=1 when Call=1, butdoesn’t stay 1 when Call returns to 0

Need some form of “feedback” in the circuit

a

a

Bit

Storage

Blue light

Call

button Cancel

button

1. Call button pressed – light turns on

Bit

Storage

Blue light

Call

button Cancel

button

2. Call button released – light

stays on

Bit

Storage

Blue light

Call

button Cancel

button

3. Cancel button pressed – light turns off

Types of Storage Elements

set reset

out

data_in

data_out

write

set-reset latch

(SR latch)

data latch

(D latch)

Jay Brockman, 2007

Feedback-Based Storage

How do you get the data in and out?

Jay Brockman, 2007

MUX Based Latches

Q

D

1 0 clk

Positive Latch

Q

D

0 1 clk

Negative Latch

Q = !clk & Q

| clk & D

Q = clk & Q

| !clk & D

feedback

transparent when the

clock is low

transparent when the

clock is high

feedback

Change the stored value by cutting the feedback loop

[Adapted from Mary Jane Irwin and Vijay Narananan, adaptation of Rabaey’s

Digital Integrated Circuits

, ©2002, J. Rabaey et al.]

SR Latch

disallowed

0

0

1

1

reset

1

0

1

0

set

0

1

0

1

memory

!Q

Q

0

0

!Q

Q

R

S

S R

!Q Q

[Adapted from Mary Jane Irwin and Vijay Narananan, adaptation of Rabaey’s

Digital Integrated Circuits

, ©2002, J. Rabaey et al.]

Digital DesignCopyright © 2006Frank Vahid

Example Using SR Latch for Bit Storage

SR latch can serve as bitstorage in previous exampleof flight-attendant call button

  • Call=1 : sets Q to 1

Q stays 1 even after Call=

  • Cancel=1 : resets Q to 0

But, there’s a problem...

S R

Q

Call

but t on

Blue light

Cancelbut t on

Bit

Storage

Blue lig

ht

Call

but ton

Cancel but ton

Digital DesignCopyright © 2006Frank Vahid

Problem with Level-Sensitive D Latch

D latch still has problem (as does SR latch)

  • When C=1, through how many latches will a signal travel?– Depends on for how long C=

Clk_A -- signal may travel through multiple latches

Clk_B -- signal may travel through fewer latches

  • Hard to pick C that is just the right length

Can we design bit storage that only stores a value on the rising edge ofa clock signal?

D

Q

D

Q

D

Q

D4 C

C

C

C

Q

Y

Clk Clk_A

Clk_B

Clk

rising edges

Digital DesignCopyright © 2006Frank Vahid

D Flip-Flop

Flip-flop

: Bit storage that stores on clock edge, not level

One design -- master-servant

Two latches, output of first goes to input of second, masterlatch has inverted clock signal

So master loaded when C=0, then servant when C=

When C changes from 0 to 1, master disabled, servantloaded with value that was at D just before C changed -- i.e.,value at D during rising edge of C

Clk

D/Dm

Qm/Ds

Cm

Cs

Qs

Clk

rising edges

Note:Hundreds ofdifferent flip-flop designsexist

D latch master

D latch

servant

D

Dm

Ds Cs

Qm

Qs’ Qs

Q

Q’

Cm

Clk

D flip-flop

Master Slave Based ET Flipflop

Q

M

D

1 0

Q

0 1

Slave

Master

Q

M

D

clk 1 0

Q

clk

0 1

Slave

Master

clk

Q

M

D Q

clock

D FF

Q

D

clk = 0 transparent

hold

clk = 0

→

1

hold

transparent

[Adapted from Mary Jane Irwin and Vijay Narananan, adaptation of Rabaey’s

Digital Integrated Circuits

, ©2002, J. Rabaey et al.]

Digital DesignCopyright © 2006Frank Vahid

D Flip-Flop

D

Q’

Q

Q’

D

Q

Symbol for rising-edge

triggered D flip-flop

Symbol for falling-edge

triggered D flip-flop

Clk

rising edges

Clk

falling edges

Internal design: Justinvert servant clockrather than master

The trianglemeans clockinput, edgetriggered

Digital DesignCopyright © 2006Frank Vahid

D Flip-Flop

Solves problem of not knowing through how many latches a signaltravels when C=

In figure below, signal travels through exactly one flip-flop, for Clk_A orClk_B

Why? Because on rising edge of Clk, all four flip-flops are loadedsimultaneously -- then all four no longer pay attention to their input, until thenext rising edge. Doesn’t matter how long Clk is 1.

Two latches insideeach flip-flop

D

Q

D

Q

D

Q

D

Q

Y

Clk

Clk_A

Clk_B

Tw ol atches

insideeach

flip-flop

Digital DesignCopyright © 2006Frank Vahid

D Latch vs. D Flip-Flop

Latch is level-sensitive: Stores D when C=

Flip-flop is edge triggered: Stores D when C changes from0 to 1

  • Saying “level-sensitive latch,” or “edge-triggered flip-flop,” is

redundant

  • Two types of flip-flops -- rising or falling edge triggered.

Comparing behavior of latch and flip-flop:

Clk

D

Q (D latch)

Q (D flip-flop)

Digital DesignCopyright © 2006Frank Vahid

Flight-Attendant Call Button Using D Flip-Flop

D flip-flop will store bit

Inputs are Call, Cancel, and present valueof D flip-flop, Q

Truth table shown below

Preserve value: ifQ=0, make D=0; ifQ=1, make D=1Cancel -- makeD=0Call -- make D=1Let’s give priorityto Call -- makeD=

Circuit derived from truth table,using Chapter 2 combinational

logic design process

Call

but tonCancelbut ton

Flight

attendant

call-button

system

Bluelight

D

Q’

Q

Clk

Call

but ton

Cancel but ton

Bluelight

Call

Cancel

Q

Digital DesignCopyright © 2006Frank Vahid

Basic Register

Typically, we store multi-bit items

  • e.g., storing a 4-bit binary number

Register

: multiple flip-flops sharing clock signal

  • From this point, we’ll use registers for bit storage

No need to think of latches or flip-flops

But now you know what’s inside a register

D

Q

D

Q

D

Q

D

Q

I

I

Q

Q

Q

Q

I

I

clk

4-bit register

I

I

I1 I

Q3 Q2 Q1 Q

reg(4)

Digital DesignCopyright © 2006Frank Vahid

Describing Behavior of Sequential Circuit: FSM

Finite-State Machine (FSM)

  • A way to describe desired

behavior of sequential circuit

Akin to Boolean equations forcombinational behavior

  • List states, and transitions

among states

Example: Make x changetoggle (0 to 1, or 1 to 0) everyclock cycle

Two states: “Off” (x=0), and“On” (x=1)

Transition from Off to On, orOn to Off, on rising clock edge

Arrow with no starting statepoints to initial state (whencircuit first starts)

Outputs: x

On

Off x=

x=

clk

^

clk

^

Off

On

Off

On

Off

On

Off

On

cycle 1

Off

Off

On

On

cycle 2

cycle 3

cycle 4

clk

state

x

Outputs:

Digital DesignCopyright © 2006Frank Vahid

FSM Example: 0,1,1,1,repeat

Want 0, 1, 1, 1, 0, 1, 1, 1, ...

  • Each value for one clock cycle

Can describe as FSM

  • Four states– Transition on rising clock

edge to next state

Off

Off

On

On1 On

On

On

On

Off

clk

x

State

Outputs:

Outputs: x

On

Off

On

On

clk

^

clk

^

clk

^

x=

x=

x=

x=

clk

^

Digital DesignCopyright © 2006Frank Vahid

Extend FSM to Three-Cycles High Laser Timer

Four states

Wait in “Off” state while b is0 (b’)

When b is 1 (and risingclock edge), transition toOn

  • Sets x=1– On next two clock edges,

transition to On2, then On3,which also set x=

So x=1 for three cycles afterbutton pressed

Off

Off

On

Off

Off

Off

On2 On

Off

clk

State

Outputs:

Inputs:

x

b

On

On

On

Off

clk

^

clk

^

x=

x=

x=0 x=

clk

^

b’*clk

^

b*clk

^

Inputs: b; Outputs: x

Digital DesignCopyright © 2006Frank Vahid

FSM Simplification: Rising Clock Edges Implicit

Showing rising clock on everytransition: cluttered

  • Make implicit -- assume every

edge has rising clock, even if notshown

  • What if we wanted a transition

without

a rising edge

We don’t consider suchasynchronous FSMs -- lesscommon, and advanced topic

Only consider

synchronous

FSMs -- rising edge on

every

transition

Note: Transition with no associated condition thustransistions to next state on next clock cycle

On

On

On

Off

x=

x=

x=0 x=

b’

b

Inputs: b; Outputs: x

On

On

On

Off

x=

x=

x=0 x=

b’

clk

^

clk

^

^

clk

*clk

^

*clk

^

b

Inputs: b; Outputs: x

a

Digital DesignCopyright © 2006Frank Vahid

FSM Definition

FSM consists of

  • Set of states

Ex: {Off, On1, On2, On3}

  • Set of inputs, set of outputs

Ex: Inputs: {b}, Outputs: {x}

  • Initial state

Ex: “Off”

  • Set of transitions

Describes next states

Ex: Has 5 transitions

  • Set of actions

Sets outputs while in states

Ex: x=0, x=1, x=1, and x=

Inputs: b; Outputs: x

On

On

On

Off

x=

x=

x=0 x=

b’

b

We often draw FSM graphically,

known as

state diagram

Can also use table (state table), or

textual languages

typo

Digital DesignCopyright © 2006Frank Vahid

FSM Example: Secure Car Key

Many new car keys includetiny computer chip

  • When car starts, car’s

computer (under engine hood)requests identifier from key

  • Key transmits identifier

If not, computer shuts off car

FSM

  • Wait until computer requests

ID (a=1)

  • Transmit ID (in this case,

K

K

K

K

r=

r=

r=

r=

Wait

r=

Inputs: a; Outputs: r

a’

a

Digital DesignCopyright © 2006Frank Vahid

FSM Example: Secure Car Key (cont.)

Nice feature of FSM

  • Can evaluate output behavior

for different input sequence

  • Timing diagrams show states

and output values for differentinput waveforms

K

K

K

K

r=

r=

r=

r=

Wait

r=

Inputs: a; Outputs: r

a’

a

Wait

Wait

K

K

K

K

Wait Wait

clk

Inputs

Outputs

State

a

r

clk

Inputs

a

K

Wait

Wait

K

K

K

K

Wait

Output

State

r

Q: Determine states and r value forgiven input waveform:

a

Digital DesignCopyright © 2006Frank Vahid

FSM Example: Code Detector

Unlock door (u=1) only whenbuttons pressed in sequence:

start, then red, blue, green, red

Input from each button:

s, r, g, b

Also, output

a

indicates that

some colored button pressed

FSM

Wait for start (s=1) in “Wait”

Once started (“Start”)

If see red, go to “Red1”

Then, if see blue, go to “Blue”

Then, if see green, go to“Green”

Then, if see red, go to “Red2”

In that state, open the door(u=1)

Wrong button at any step, returnto “Wait”, without opening door

StartRed

Green

Blue

s r

g b a

Door

lock

u

Code

detector

Q: Can you trick this FSM to open the door,without knowing the code?

A: Yes, hold all buttons simultaneously

a

a

a

Wait

Start

Red

Red

Green

Blue

s’

a’

ar’

ab’

ag’

ar’

a’

ab

ag

ar

a’

a’

u=

u=

ar

u=

s

u=

u=

u=

Inputs: s,r,g,b,a;

Outputs: u

Digital DesignCopyright © 2006Frank Vahid

Controller Design: Laser Timer Example (cont)

Step 4: Create state table

x=

x=

x=

x=

b

b’

On

On

Off

On

Inputs: b; Outputs: x

Combinational

logic

State register

s

s

n1n

x

b clk

FSM

inputs

FSM

outputs

a

Digital DesignCopyright © 2006Frank Vahid

Controller Design: Laser Timer Example (cont)

Step 5: Implementcombinational logic

Combinational

logic

State register

s

s

n1 n

x

b clk

FSM

inputs

FSM

outputs

a

x = s1 + s

(note from the table that x=1 if s1 = 1 or s0 = 1)

n1 = s1’s0b’ + s1’s0b + s1s0’b’ + s1s0’bn1 = s1’s0 + s1s0’n0 = s1’s0’b + s1s0’b’ + s1s0’bn0 = s1’s0’b + s1s0’

Digital DesignCopyright © 2006Frank Vahid

Controller Design: Laser Timer Example (cont)

Step 5: Implementcombinational logic (cont)

a

x = s1 + s0n1 = s1’s0 + s1s0’n0 = s1’s0’b + s1s0’

Combinational

logic

State register

s

s

n1n

x

b clk

FSM

inputs

FSM

outputs

n1 n

s

s

clk

Combinational Logic

State register

b

FSMoutputs

FSMinputs

x

Digital DesignCopyright © 2006Frank Vahid

Understanding the Controller’s Behavior

s

s

b

x

n1 n

x=

x=

x=

b

01

10

11

On

On

Off

On

00

0

0

0 00 0

b’

0

0 0

0

0

x=

0

00

clk

clk

Inputs:

Outputs:

0 1

1

0

b 1

0 01 0

s

s

b

x n1 n

x=

x=

x=

b’

01

10

11

On

On

Off

On

clk

b x

00

0

0

x=

0

00

state=

state=

s

s

b

x n1 n

x=

x=

x=

x=

b

b’

01

00

10

11

On

On

Off

On

1

1 1 0

011 00 0

clk

0

1

0

1

state=

a

Digital DesignCopyright © 2006Frank Vahid

Example: Seq. Circuit to FSM

(Reverse Engineering)

clk

State register

y

z

FSMoutputs

FSMinputs

n1 n

s

s

x

What does thiscircuit do?

Work backwards

y=s1’z = s1s0’n1=(s1 xor s0)xn0=(s1’*s0’)x

a

Pick any state names you want

A D

B C

states

Outputs:y, z

A D

B

yz=

yz=

yz=

yz=

C

stateswithoutputs

A D

B

yz=

yz=

yz=

yz=

C

Inputs: x; Outputs:y, z

x’ x’

x’

x

x

x

states withoutputs andtransitions

Digital DesignCopyright © 2006Frank Vahid

Controller Example: Sequence Generator

Want generate sequence 0001, 0011, 1100, 1000, (repeat)

Each value for one clock cycle

Common, e.g., to create pattern in 4 lights, or control magnets of a “stepper motor”

00

01

11 10

A

B

D

wxyz=

wxyz=

wxyz=

wxyz=

C

Inputs: none; Outputs: w,x,y,z

Step 3: Encode states

Step 4: Create state table

clk

State register

wx y

z

FSMoutputs

n

s

s

n

Step 5: Create combinational circuit

w = s1x = s1s0’y = s1’s0z = s1’n1 = s1 xor s0n0 = s0’

a

Step 1: Create FSM

A B

D

wxyz=

wxyz=

wxyz=

wxyz=

C

Inputs: none; Outputs: w,x,y,z

Step 2: Create architecture

Combinational

logic

n

s

s

n

clk

State register

wxy

z

Digital DesignCopyright © 2006Frank Vahid

Controller Example: Secure Car Key

(from earlier example)

K

K

K

K

r=

r=

r=

r=

Wait

r=

Inputs: a; Outputs: r

a’

a

Step 1

FSM

Combinational

logic

s

s

s

n

r

a

n1n

clk

State register

FSM

inputs

outputs

Step 2

a’

a

r=

r=

r=

r=

r=

Inputs: a; Outputs: r

Step 3

Step 4

a

We’ll omit Step 5

Digital DesignCopyright © 2006Frank Vahid

Controller Example:

Button Press Synchronizer

Want simple sequential circuit that converts button press tosingle cycle duration, regardless of length of time thatbutton actually pressed

  • We assumed such an ideal button press signal in earlier example,

like the button in the laser timer controller

cycle

cycle

cycle

cycle

clk

Inputs:

Outputs:

bi

bo

Button press

synchronizer

controller

bi

bo

Digital DesignCopyright © 2006Frank Vahid

Simplifying Notations

FSMs

  • Assume unassigned output

implicitly assigned 0

Sequential circuits

  • Assume unconnected clock

inputs connected to sameexternal clock

a=0b=1c=

a=0b=0c=

b=

c=

clk

a a

a

Digital DesignCopyright © 2006Frank Vahid

More on Flip-Flops and Controllers

Other flip-flop types

  • SR flip-flop: like SR latch, but edge triggered– JK flip-flop: like SR (S

Æ

J, R

Æ

K)

But when JK=11, toggles

1

Æ

0, 0

Æ

1

  • T flip-flop: JK with inputs tied together

Toggles on every rising clock edge

  • Previously utilized to minimize logic outside flip-flop

Today, minimizing logic to such extent is not as important

D flip-flops are thus by far the most common

Digital DesignCopyright © 2006Frank Vahid

Non-Ideal Flip-Flop Behavior

Can’t change flip-flop input too close to clockedge

Setup time: time that D must be stable

before

edge

Else, stable value not present at internal latch

Hold time: time that D must be held stable

after

edge

Else, new value doesn’t have time to loop aroundand stabilize in internal latch

clk

D

clk

D

setup time

hold time

S R

D C

u

D latch

Q’ Q

1

2

3

4

5

6

7

C DS u R Q’

Q

Setup time violation

Leads to oscillation!

Digital DesignCopyright © 2006Frank Vahid

Metastability

Violating setup/hold time can lead to badsituation known as

metastable

state

Metastable state: Any flip-flop state otherthan stable 1 or 0

Eventually settles to one or other, but wedon’t know which

For internal circuits, we can make sureobserve setup time

But what if input comes from external(asynchronous) source, e.g., buttonpress?

Partial solution

Insert synchronizer flip-flop forasynchronous input

Special flip-flop with very small setup/holdtime

Doesn’t completely prevent metastability

clk

D Q

setup time

violation

metastable

state

ai ai

synchronizer

a

Digital DesignCopyright © 2006Frank Vahid

Metastability

One flip-flop doesn’t completely solve problem

How about adding more synchronizer flip-flops?

Helps, but just decreases probability of metastability

So how solve completely?

Can’t! May be unsettling to new designers. But we just can’t guarantee adesign that won’t ever be metastable. We can just minimize the mean timebetween failure (MTBF) -- a number often given along with a circuit

ai

synchronizers

low

very

low

veryvery

low

incredibly

low

Probability of flip-flop being metastable is…

Digital DesignCopyright © 2006Frank Vahid

Flip-Flop Set and Reset Inputs

Some flip-flops haveadditional inputs

  • Synchronous reset: clears Q to 0

on next clock edge

  • Synchronous set: sets Q to 1 on

next clock edge

  • Asynchronous reset: clear Q to 0

immediately (not dependent onclock edge)

Example timing diagram shown

  • Asynchronous set: set Q to 1

immediately

D

Q’

Q

R

Q’

AR

D

Q

Q’

AR AS

D

Q

cycle 1

cycle 2

cycle 3

cycle 4

clk

D

AR

Q

Digital DesignCopyright © 2006Frank Vahid

Initial State of a Controller

All our FSMs had initial state

  • But our sequential circuit designs

did not

  • Can accomplish using flip-flops

with reset/set inputs

Shown circuit initializes flip-flops to 01

  • Designer must ensure reset input

is 1 during power up of circuit

By electronic circuit design

Inputs: x; Outputs: b

On

On

On

Off

x=

x=

x=0 x=

b’

b

D

Q’

Q’

Q

R

S

D

Q

State register

clk

reset

s

s

n1 n

b

x

Combinational

logic

Digital DesignCopyright © 2006Frank Vahid

Glitching

Glitch: Temporary values on outputs that appear soon afterinput changes, before stable new output values

Designer must determine whether glitching outputs maypose a problem

  • If so, may consider adding flip-flops to outputs

Delays output by one clock cycle, but may be OK