CPSC 121: Models of Computation Unit 8: Sequ ential Circuits Based on slides by Patrice Be lleville and Steve Wolfman
Pre-Class Learning Goals By the start of class, you s hould 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 h multiple l example inputs. 2
Quiz 8 feedback: Over all: Issues : Push-button light question: We will revisit this problem soon. 3
In-Class Learning G oals By the end of this unit, 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. 4
?? Related to CPSC 12 21 Bib Questions? How can we build a c omputer that is able to execute a user-defined program? Computers execute instr ructions one at a time. They need to rememberr values, unlike the circuits you designed in labs 1, 2, 3 and 4.? NOW: We are learning to build a new kind of circuits with memory that will be the key new feature we need to build full-blown computers!? 5?
Unit Outline Latches, toggles and flip-flops. DFA Example : Branch prediction. Implementing DFAs Other problems and exercises. 6
Finite-State Automa ta 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. o Input validation. o Searching text for contents. o 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 ) ) ) ) ) 7
Finite-State Automa ta Those that produce output t every time the state changes (Mealy machines). Examples: o Simple ciphers o Traffic lights controller. o 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. 8
Are Combinational Circuits Enough? Problem: Design a circuit that changes state every time a button is pushed.? 9
DFA for Push-ButtonSwitch? pressed light off light on pressed This Deterministic Finite Automaton (DFA) isn t really about accepting/rejecting; its current state is the state of the light. 10
Departures from Com mbinational Circuits MEMORY: We need to remember the light s state. EVENTS: We need to act on a button push rather than in response to an input value. 11
How Do We Remem mber? We want a circuit that: Sometimes remembers its current state. Other times loads a new state and remembers it. Sounds like a choice. What circuit element do we have for modelling choices? 12
Mux Memory Howdoweuseamux we to store a bit of memory? We can choose to remember on a control value of 0 and to load a new state ona1 1. new data??? 0 1 output control We use 0 and 1 because that s how MUXes are usually labelled. 13
Mux Memory Howdoweuseamux we to store a bit of memory? We can choose to remember on a control value of 0 and to load a new state ona1 1. old output (q ) new data (d) 0 1 output (q) control (g) We use 0 and 1 because that s how MUXes are usually labelled. 14
D Latches We call a "mux-memory" ad-latch ( recall from lab #5) When g is low, the latch retains its current value. When g is high, it changes its value to d instead. g 15
D-Latch AD-latch looks like new dataa (D) control (G) D G Q output (Q) Why does the D Latch hav ve two inputs and one output when the mux inside has THREE inputs and one output? A. The D Latch is broken as is; it should have three inputs. B. A circuit can always ignore one of its inputs. C. One of the inputs is always true. D. One of the inputs is always false. E. None of these (but the D Latch is not broken as is). 16
Push-Button Switch What signal does the butto on generate? high low 17
Push-Button Switch Complete the circuit... 18
Push-Button Switch 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 d by th he NOT gate is too long. D. There is some other problem with the circuit. E. Nothing is wrong. 19
A Timing Problem This toll booth has a simila ar problem. What is wrong with this booth? P.S. Call this a bar, not a gate, or we'll tie ourselves in (k)nots. From MIT 6.004, Fall 2002 20
A Timing Solution Instead use this: From MIT 6.004, Fall 2002 21
D- Flip-Flops The circuit version of this im mproved tollbooth is called a D-flip-flop: When control (G, Clock ) goes from 0 (low) to 1 (high), the flip-flop loads a new value from D. Otherwise, it maintains its current value. 22
Push-Button Switch: Solution And we get the following im mproved circuit for our button and light problem: 23
Push-Button Switch: Solution Using a D- flip-flop 24
Why Abstract? Logisim (and real circuits) behave very similarly: D flip-flops, T flip-flops, J-K flip-flops, and S-R flip-flops. have lots of flip-flops that all They have slightly different implementations and one could imagine i brilliant new designs that t are radically different inside. Abstraction ti allows us to build a good design at a high-h the level without worrying about details. 25
Unit Outline Latches, toggles and flip-flops. DFA Example : Branch prediction. Implementing DFAs Other problems and exercises. 26
Computer Instruction ns How do computers really execute programs? Programs written in a high-level language (Racket, Java) are translated into machine language. g A machine-language program is a sequence of very simple instructions. o Each instruction is a sequence of 0s and 1s. o Each instruction also has a human-readable version Humans don't like looking at long sequences of 0s and 1s. The human-readable e version is not actually part of the program. 27
Computer Instruction ns Example (modified to make 1. sum 0 2. is n = 0? 3. if true go to 7 4. sum sum + n 5. n n 1 6. goto 2 it easier to understand): 7.. Some instructions like instruction 3 (called branch instructions) may tell the computer that the next instruction to execute is not the next in the sequence (4), but elsewhere (7). 28
Computer Instruction ns To speed things up, a mod dern computer starts executing an instruction before the previous one is finished. This means that when it is if true go to 7 it does not yet know if the condition is true, and hence does not know if the next in nstruction is sum sum + n or instruction number 7. executing 29
Branch Prediction: Simple Guess So we want to be able to predict the outcome of a branch instruction. If we guess wrong, then we was done. will ignore some of the work that To pre-execute a branch, the computer guesses which instruction comes next. Here s one reasonable guess: If the last branch was taken (like going to (7) from (3)), take the next. If it was not taken (like go oing to (4) from (3)), don t take the next. 30
Branch Prediction: Simple Guess Here s the corresponding DFA. (Instead of accept/reject, we care about the current state.) taken yes taken not taken not taken no taken not taken yes no the last branch was taken the last branch was not taken we predict the next branch will be taken we predict the next branch will be not taken 31
Branch Prediction: Using Confidence We now add the confidence we gain from the previous prediction : If we guessed right our confidence will be high; otherwise it will be low. Therefore, we will keep track of two pieces of info: 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. For instance: If the current prediction is to branch and we are confident then we'll predict to branch again, etc. 32
DFA Example: Bran ch Prediction How many states will the Finite State Automaton have now? A. 2 B. 4 C. 8 D. Another value less than 8. E. Another value larger than 8. 33
DFA Example: Bran ch Prediction 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 34
DFA Example: Bran ch Prediction Hence we get the following DFA: not taken yes? taken not taken NO! taken not taken no? not taken taken YES! taken Note: NO! no branch with high confidence no? no branch with low confidence YES! branch with high confidence yes? branch with low confidence 35
Unit Outline Latches, toggles and flip-flops. DFA Example : Branch prediction. Implementing DFAs Other problems and exercises. 36
Abstract Template fo or a DFA Circuit Each time the clock ticks next. move from one state to the clock input store current state compute next state 37
Template for a DFA Circuit Each time the clock ticks next. move from one state to the D CLK Q Combinational circuit to calculate next state/output input Each of these lines (except the clock) may carry multiple bits; the D flip-flop may be several flip-flops to store several bits. 38
Implementing DFAs HowdoweturnaDFAinto we o 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 outp ut, 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. 39
Implementing DFAs The circuit will look like the following: Next State circuits Compute Output 40
Implementing DFA for Branch Prediction Now let us assign numbers to states and inputs. Inputs: not taken = 0 taken = 1 Transition Table: Current State input New state 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 1 1 1 1 1 0 0 0 0 1 0 1 1 1 1 1 0 1 0 1 1 1 1 1 not taken NO! 0 taken not taken not taken no? 1 p yes? 2 not taken taken YES! 3 taken taken 41
Implementing DFA for Branch Prediction Here is the basic circuit: 42
Unit Outline Latches, toggles and flip-flops. DFA Example : Branch prediction. Implementing DFAs Other problems and exercises. 43
Exercises 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. o 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. 44
Exercises Real numbers (continued) Then design a circuit that turns a LED on if the input is a valid real number, and off otherwise. o Hint: Logisim has a keyboard component you can use. o 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. 45
Quiz #9 Due Date: Check Announc cements. Reading for the Quiz Textbook sections: Epp, 4th edition: 5.1 to 5.4 Epp, 3rd edition: 4.1 to 4.44 Rosen, 6th edition: 4.1, 4.2 Rosen, 7th edition: 5.1, 5.2 46