cpsc 121: models of computationcs121/2014w2/slides/sequential... · format similar to that of...

34
CPSC 121: Models of Computation Module 8: Sequential Circuits

Upload: vohanh

Post on 03-Feb-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121: Models of Computation

Module 8: Sequential Circuits

Page 2: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 2

Module 8: Sequential Circuits

By the start of class, you should be able to

Trace the operation of a DFA (deterministic finite-state automaton) represented as a diagram on an input, and indicate whether the DFA accepts or rejects the input.

Deduce the language accepted by a simple DFA after working through multiple example inputs.

Page 3: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 3

Module 8: Sequential Circuits

Quiz 8 feedback:

Very well done.

Many fine answers to the push-button light question. We will revisit this problem soon.

Page 4: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 4

1. Course Introduction

???

?

?

? ??

?

???

?

?

?

?

?

CPSC 121: the BIG questions:

1. How can we build a computer that is able to execute a user-defined program?

a) Computers execute instructions one at a time.

b) They need to remember values, unlike the circuits you designed in labs 1, 2, 3 and 4.

c) That is, a computer is a very large and very complicated sequential circuit.

Page 5: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 5

Module 8: Sequential Circuits

By the end of this module, you should be able to:

Translate a DFA into a sequential circuit that implements the DFA.

Explain how and why each part of the resulting circuit works.

Page 6: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 6

Module 8: Sequential Circuits

Announcements:

Pre-class quiz #9 is due Thursday March 12th at 19:00.

Textbook sections:

Epp, 4th edition: 5.1 to 5.4

Epp, 3rd edition: 4.1 to 4.4

Rosen, 6th edition: 4.1, 4.2

Rosen, 7th edition: 5.1, 5.2

Assignment #4 is due Monday March 16th at 17:00.

Page 7: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 7

Module 8: Sequential Circuits

Announcements (continued):

In-class test #2:

Wednesday March 18th, 2015

Format similar to that of in-class test #1.

You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper.

No textbook, calculator or other electronic equipment is allowed.

Page 8: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 8

Module 8: Sequential Circuits

Announcements (continued):

Pre-class quiz #10 is tentatively due Tuesday March 31st at 19:00.

Textbook sections:

Epp, 4th edition: 6.1, 7.1

Epp, 3rd edition: 5.1, 6.1

Rosen, 6th edition: 2.1, 2.3 up to the top of page 136.

Rosen, 7th edition: 2.1, 2.3 down to the bottom of page 141.

Page 9: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 9

Module 8: Sequential Circuits

Module Summary

Latches, toggles and flip-flops.

Branch prediction.

Other problems and exercises.

Page 10: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 10

Module 8: Sequential Circuits

There are two types of Finite-State Automata:

Those whose output is determined solely by the final state (Moore machines).

Used to match a string to a pattern.

Input validation.Searching text for contents.Lexical Analysis: the first step in a compiler or an interpreter.

(define (fun x) (if (<= x 0) 1 (* x (fun (- x 1)))))

( define ( fun x ) ( if ( <= x 0 ) 1 ( * x ( fun ( - x 1 ) ) ) ) )

Page 11: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 11

Module 8: Sequential Circuits

Those that produce output every time the state changes (Mealy machines).

Examples:

Simple ciphersTraffic lights controller.Predicting branching in machine-language programs

A circuit that implements a finite state machine of either type needs to remember the current state:

It needs memory.

Page 12: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 12

Module 8: Sequential Circuits

Recall the latch from lab #5:

When en is low, the MUX retains its current value.

When en is high, it changes its value to d instead.

Page 13: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 13

Module 8: Sequential Circuits

Problem:

Design a circuit that changes state every time a button is pushed.

??

Page 14: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 14

Module 8: Sequential Circuits

What signal does the button generate?

low

high

Page 15: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 15

Module 8: Sequential Circuits

Complete the circuit...

Circuit to calculatethe next state

Page 16: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 16

Module 8: Sequential Circuits

What is wrong with our solution?

a) We should have used XOR instead of NOT.

b) We designed an effective random bit generator.

c) The delay introduced by the NOT gate is too long.

d) There is some other problem with the circuit.

e) Nothing is wrong.

Page 17: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 17

Module 8: Sequential Circuits

This toll booth has a similar problem.

From MIT 6.004, Fall 2002

Page 18: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 18

Module 8: Sequential Circuits

Instead use this:

From MIT 6.004, Fall 2002

P.S. Call this a “bar”, not a “gate”, or we'll tie ourselves in (k)nots.

Page 19: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 19

Module 8: Sequential Circuits

The circuit version of this improved tollbooth is called a flip-flop:

Page 20: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 20

Module 8: Sequential Circuits

And we get the following improved circuit for our button and light problem:

Page 21: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 21

Module 8: Sequential Circuits

Module Summary

Latches, toggles and flip-flops.

Branch prediction.

Other problems and exercises.

Page 22: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 22

Module 8: Sequential Circuits

How do computers really execute programs?

Programs written in a high-level language (Racket, Java) are translated into machine language.

A machine-language program is a sequence of very simple instructions.

Each instruction is a sequence of 0s and 1s.

Each instruction also has a human-readable version

Humans don't like looking at long sequences of 0s and 1s.The human-readable version is not actually part of the program.

Page 23: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 23

Module 8: Sequential Circuits

Example (modified to make it easier to understand):

(1) sum ← 0

(2) is n = 0?

(3) if true go to 7

(4) sum ← sum + n

(5) n ← n – 1

(6) goto 2

Some instructions like instruction 3 may tell the computer that the next instruction to execute is not the next in the sequence (4), but elsewhere (7).

Page 24: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 24

Module 8: Sequential Circuits

To speed things up, a modern computer starts executing an instruction before the previous one is finished.

This means that when it is executing

if true go to 7

it does not yet know if the condition is true, and hence does not know if the next instruction is

sum ← sum + n

or instruction number 7.

Page 25: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 25

Module 8: Sequential Circuits

So we want to be able to predict the outcome.

If we guess wrong, then we will ignore some of the work that was done.

We will keep track of two pieces of information:

What we will predict (F = not branch, T = branch).

How confident we are that we are correct (F = not very, T = very).

once we know if the branch was taken, we update this information.

Page 26: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 26

Module 8: Sequential Circuits

How many states will the Finite State Automaton have?

a) 2

b) 4

c) 8

d) Another value less than 8.

e) Another value larger than 8.

Page 27: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 27

Module 8: Sequential Circuits

Let us fill out a truth table that describes the behaviour we want of the automaton.

Current State Taken? Next State

Pred Conf? Pred Conf?

F F F F T

F F T T T

F T F F T

F T T F F

T F F F T

T F T T T

T T F T F

T T T T T

Page 28: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 28

Module 8: Sequential Circuits

Hence we get the following DFA:

Page 29: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 29

Module 8: Sequential Circuits

How do we turn a DFA into a circuit?

Number the states, starting with 0, and figure out how many bits you need to store the state number.

Number the inputs, starting with 0, and figure out how many bits you need to represent the input.

Layout enough D flip-flops to store the state (one per bit).

For each state, build a combinational circuit that computes the next state (and the output, if needed) given the input.

Send all those into multiplexers, and use the current state as the control signal (so you only keep the correct one).

Store the next state back into the D flip-flops.

Page 30: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 30

Module 8: Sequential Circuits

The circuit will look like the following:

Compute Output

Next State circuits

Page 31: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 31

Module 8: Sequential Circuits

Now let us complete the implementation using Logisim...

Page 32: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 32

Module 8: Sequential Circuits

Module Summary

Latches, toggles and flip-flops.

Branch prediction.

Other problems and exercises.

Page 33: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 33

Module 8: Sequential Circuits

Real numbers:

We can write numbers in decimal using the format

(-)? d+ (.d+)?

where the ( )? mean that the part in parentheses is optional, and d+ stands for “1 or more digits”.

Design a DFA that will accept input strings that are valid real numbers using this format.

You can use else as a label on an edge instead of listing every character that does not appear on another edge leaving from a state.

Page 34: CPSC 121: Models of Computationcs121/2014W2/slides/Sequential... · Format similar to that of in-class test #1. You can bring one 8.5 x 11in (21.59 x 27.94cm) two sided sheet of paper

CPSC 121 – 2014W T2 34

Module 8: Sequential Circuits

Real numbers (continued)

Then design a circuit that turns a LED on if the input is a valid real number, and off otherwise.

Hint: Logisim has a keyboard component you can use.

Hint: my DFA for this problem has 6 states.

Design a DFA for a vending machine that sells one of three items (lemon juice, whiteboard markers, and corn flour) for 35¢ each. It should accept 5¢, 10¢ and 25¢ coins, and does not need to return change.