Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

36
Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University

Transcript of Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Page 1: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Flip-flops, Latches and State

Prof. Sirer

COMP 303

Koç University

Page 2: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Early Transistors

The first transistor, on a workbench at AT&T Bell Laboratories in 1947

Page 3: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Logic Gates

One can buy gates separately

ex. 74xxx series of integrated circuits

cost ~$1 per chip, mostly for packaging and testing

Cumbersome, but possible to build devices using gates put together manually

Page 4: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Integrated Circuits

Or one can manufacture a complete design using a custom mask

An Intel Pentium has approximately 125 million transistors

Page 5: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Special-Purpose Transistors

A photo-sensitive transistor can be used to detect the presence of light

Photo-sensitive material triggers the gate

Page 6: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

RecapWe have enough tools in our arsenal to start build interesting devices

Let’s build a Scantron device

Background: A vote is recorded on a piece of paper, by punching out a hole, there are at most 7 choices we will not worry about “hanging chads” or “invalids”

For now, let’s just display the numerical identifier to the ballot supervisor

we won’t do counting yet, just decoding we can use four photo-sensitive transistors to find out which

hole is punched out

Page 7: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Ballot Reading

All we want to do is go from a paper with a hole in it to a number the ballot supervisor can recordBallots The super-duper

COMP303 vote decodingmachine

Page 8: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Demultiplexors/Encoders

N sensors in a rowWe want to distinguish which sensor of the N sensors has firedWant to represent the firing sensor number in compact form

N might be large, I want to sell this device to Italy

Only one wire is on at any time

Silly to route N wires everywhere, better to encode in log N wires

a

bo0

1

c

d

2

3

4

o1

A 3-bit (7-to-3)encoder

(4 inputs shown)

o2

Page 9: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Number Representations

Decimal numbers are written in base 10

3 x 101 + 7 x 100 = 37

Can just as easily use other bases

Base 2 - “Binary” Base 8 - “Octal” Base 16 – “Hexadecimal”

Base conversion via repetitive division

Divide by base,write remainder,move left with quotient

Sanity check with 37 and 10

37101 100

Page 10: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Binary Representation

010010126 25 24 23 22 21 20

37 = 32 + 4 + 1

64 32 16 8 4 2 1

Page 11: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Hexadecimal Representation

37 decimal = (25)16

Convention Base 16 is written with a leading

0x 37 = 0x25

Need extra digits! 0, 1, 2, 3, 4, 5, 6, 7,

8, 9, A, B, C, D, E, F

Binary to hexadecimal is easy Divide into groups of 4, translate

groupwise into hex digits

25161 160

Page 12: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Encoder Truth Table

a

b

1

c

d

2

3

4

o1

A 3-bitencoderwith 4 inputsfor simplicity

a b c d o2 o1 o0

0 0 0 0 0 0 0

1 0 0 0 0 0 1

0 1 0 0 0 1 0

0 0 1 0 0 1 1

0 0 0 1 1 0 0

o0

o1

o2

o2 = abcd

o1 = abcd + abcd

o0 = abcd + abcd

Page 13: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Ballot Reading

Ok, we builtfirst half of the machine

Need to display the result

Ballots The super-duperCOMP303 vote decoding

machine

Page 14: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

7-Segment LED Decoder

4 inputs encoded in binary

8 outputs, each driving an independent, rectangular LED

Can display numbersJust a simple logic circuit

Write the truth table

Page 15: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

7-Segment LED Decoder

4 inputs encoded in binary

8 outputs, each driving an independent, rectangular LED

Can display numbers

01

00

Page 16: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

7-Segment LED Decoder

4 inputs encoded in binary

8 outputs, each driving an independent, rectangular LED

Can display numbers

01

10

Page 17: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

7-Segment Decoder Truth Table

i3 i2 i1 i0 o0 o1 o2 o3 o4 o5 o6

0 0 0 0 1 1 1 0 1 1 1

0 0 0 1 1 0 0 0 0 0 1

0 0 1 0 1 1 0 1 1 1 0

0 0 1 1 1 1 0 1 0 1 1

0 1 0 0 1 0 1 1 0 0 1

0 1 0 1 0 1 1 1 0 1 1

0 1 1 0 0 0 1 1 1 1 1

0 1 1 1 1 1 0 0 0 0 0

1 0 0 0 1 1 1 1 1 1 1

1 0 0 1 1 1 1 1 0 1 1

o0

o1

o2

o3

o4

o5

o6

Exercise: find the error(s) in this truth table

Page 18: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

7-Segment Decoder Truth Table

i3 i2 i1 i0 o0 o1 o2 o3 o4 o5 o6

0 0 0 0 1 1 1 0 1 1 1

0 0 0 1 1 0 0 0 0 0 1

0 0 1 0 1 1 0 1 1 1 0

0 0 1 1 1 1 0 1 0 1 1

0 1 0 0 1 0 1 1 0 0 1

0 1 0 1 0 1 1 1 0 1 1

0 1 1 0 0 0 1 1 1 1 1

0 1 1 1 1 1 0 0 0 0 1

1 0 0 0 1 1 1 1 1 1 1

1 0 0 1 1 1 1 1 0 1 1

o0

o1

o2

o3

o4

o5

o6

Page 19: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Ballot Reading

Done!

Off to the patent office!

Ballots The super-duperCOMP303 vote decoding

machine

Page 20: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Keyboard

Let’s build a keyboard Lots of mechanical

switches Need to convert to a

compact form (binary)

We’ll use a special mechanical switch that, when pressed, connects two wires simultaneously

Page 21: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Keyboard

When a key is pressed, a 7-bit key identifier is computed

+

3-bitencoder(4 to 3)

4-bitencoder(16 to 4)

not all 16 wires are shown

Page 22: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Stateful Components

Everything we did until now is combinatorial logic Output is computed when inputs are present The system has no internal state Nothing computed in the present can depend

on what happened in the past!

Need a way to record dataNeed a way to build stateful circuitsNeed a state-holding device

Page 23: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Bistable Devices

In stable state, A = B

How do we change the state?

A B

A B

1

A B

10 0

A Simple Device

Page 24: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

SR Latch

Set-Reset (S-R) LatchQ: Stored value and its complement

S=1 and R=1 ?

S

RQ

Q

S R Q Q

0 0 Q Q

0 1 0 1

1 0 1 0

1 1 ? ?

Page 25: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

D Latch

Data Latch Easier to use than an SR latch No possibility of entering an undefined state

When D changes, Q changes … immediately

Need to control when the output changes

S

R

Q

QD

Page 26: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Clocks

Clocks help with modifying the contents of state-holding elements

A free running signal Generated by an oscillating crystal

Clock signal has a fixed cycle time (aka cycle period)

Clock frequency = 1/cycle time

clockperiod

clockhigh

clocklow

1

0

fallingedge

risingedge

Page 27: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Edge-triggering

Can design circuits to change on the rising or falling edge

Trigger on rising edge = positive edge-triggered

Trigger on falling edge = negative edge-triggered

Inputs must be stable just before the triggering edgeinput

clock

Page 28: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

First Attempt

How does the output behave?

S

R

D

clk

D

clk

Q

Q

Q

Q

D Q

Q

Page 29: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

First Attempt

How does the output behave?

S

R

D

clk

Q

Q

clk

D

Q

Page 30: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

First Attempt

How does the output behave?

Changes in D that occur when the clock is low are deferred until clock high

Changes when clock is high are registered immediately

S

R

D

clk

D

clk

Q

Q

Q

Q

D Q

Q

Page 31: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Master-Slave Flip-FlopOutputs change only on falling edgesData is captured on rising edges1/2 cycle delay but works out

perfectly – data for the next stage is ready 1/2 cycle ahead of time

D Q

Q

D Q

Q

clk

D

X

Q

X

Page 32: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Keyboard

When a key is pressed, a 7-bit key identifier is computedLet’s store this keycode The computer

may not be ready to read it right away

+

3-bitencoder(4 to 3)

4-bitencoder(16 to 4)

not all 16 wires are shown

Page 33: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Registers

A register is simply a set of master-slave flip-flops in parallel with a shared clock

D Q D Q

D Q D Q

D Q D Q

D Q D Q

clk

D0

D3

D1

D2

4 4

4-bitreg

Page 34: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

One-Shot

A simple device that, when triggered, produces a pulse of the specified width

in one shotX ms

out

in

out

X ms

Page 35: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Keyboard with Last Key Display

+

3-bitencoder(4 to 3)

4-bitencoder(16 to 4)

not all 16 wires are shown

4-bitreg

7seg

deco

7seg

deco

4-bitreg

one-shot1ms

Page 36: Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.

Summary

We can now build interesting devices with sensors Using combinatorial logic

We can also store data values In state-holding elements Coupled with clocks