Sequential Logic Examples z Finite State Machine Concept

  • Slides: 28
Download presentation
Sequential Logic Examples z Finite State Machine Concept y FSMs are the decision making

Sequential Logic Examples z Finite State Machine Concept y FSMs are the decision making logic of digital designs y Partitioning designs into datapath and control elements y When inputs are sampled and outputs asserted z Basic Design Approach: 4 -step Design Process z Implementation Examples and Case Studies y Finite-string pattern recognizer y Complex counter y Traffic light controller y Door combination lock CS 150 - Spring 2001 - Sequential Logic Examples - 1

General FSM Design Procedure z (1) Determine inputs and outputs z (2) Determine possible

General FSM Design Procedure z (1) Determine inputs and outputs z (2) Determine possible states of machine y – State minimization z (3) Encode states and outputs into a binary code y – State assignment or state encoding y – Output encoding y – Possibly input encoding (if under our control) z (4) Realize logic to implement functions for states and outputs y – Combinational logic implementation and optimization y – Choices in steps 2 and 3 have large effect on resulting logic CS 150 - Spring 2001 - Sequential Logic Examples - 2

Finite String Pattern Recognizer (Step 1) z Finite String Pattern Recognizer y One input

Finite String Pattern Recognizer (Step 1) z Finite String Pattern Recognizer y One input (X) and one output (Z) y Output is asserted whenever the input sequence … 010… has been observed, as long as the sequence 100 has never been seen z Step 1: Understanding the Problem Statement y Sample input/output behavior: X: 0 0 1 0 1 0 … Z: 0 0 0 1 0 1 0 0 0 … X: 1 1 0 1 0 … Z: 0 0 0 0 1 0 0 0 … CS 150 - Spring 2001 - Sequential Logic Examples - 3

Finite String Pattern Recognizer (Step 2) z Step 2: Draw State Diagram y For

Finite String Pattern Recognizer (Step 2) z Step 2: Draw State Diagram y For the strings that must be recognized, i. e. , 010 and 100 y Moore implementation reset 0 S 0 [0] 1 S 1 [0] S 4 [0] 1 0 S 2 [0] S 5 [0] 0 0 S 3 [1] S 6 [0] CS 150 - Spring 2001 - Sequential Logic Examples - 4 0 or 1

Finite String Pattern Recognizer (Step 2, cont’d) z Exit conditions from state S 3:

Finite String Pattern Recognizer (Step 2, cont’d) z Exit conditions from state S 3: have recognized … 010 y If next input is 0 then have … 0100 =. . . 100 (state S 6) y If next input is 1 then have … 0101 = … 01 (state S 2) reset Exit conditions from S 1: recognizes strings of form … 0 (no 1 seen); loop back to S 1 if input is 0 Exit conditions from S 4: recognizes strings of form … 1 (no 0 seen); loop back to S 4 if input is 1 0 0 S 1 [0]. . . 0 1 S 2. . . 01 [0] 0 1 S 3. . . 010 [1] CS 150 - Spring 2001 - Sequential Logic Examples - 5 S 0 [0] 1 S 4. . . 1 [0] 0 1 S 5 [0] 0 S 6. . . 100 [0] 0 or 1

Finite String Pattern Recognizer (Step 2, cont’d) z S 2 and S 5 still

Finite String Pattern Recognizer (Step 2, cont’d) z S 2 and S 5 still have incomplete transitions y S 2 = … 01; If next input is 1, then string could be prefix of (01)1(00) S 4 handles just this case y S 5 = … 10; If next input is 1, then string could be prefix of (10)1(0) S 2 handles just this case z Reuse states as much as possible y Look for same meaning y State minimization leads to smaller number of bits to represent states z Once all states have complete set of transitions we have final state diagram 0 reset 0 S 0 [0] S 1 [0]. . . 0 1 1 S 2. . . 01 [0] 0 1 S 3. . . 010 [1] CS 150 - Spring 2001 - Sequential Logic Examples - 6 1 S 4. . . 1 [0] 0 1 1 S 5 [0]. . . 10 0 S 6. . . 100 [0] 0 or 1

Finite String Pattern Recognizer (Step 3) z Verilog description including state assignment (or state

Finite String Pattern Recognizer (Step 3) z Verilog description including state assignment (or state encoding) module string (clk, X, rst, Q 0, Q 1, Q 2, Z); input clk, X, rst; output Q 0, Q 1, Q 2, Z; reg state[0: 2]; ‘define S 0 = [0, 0, 0]; ‘define S 1 = [0, 0, 1]; ‘define S 2 = [0, 1, 0]; ‘define S 3 = [0, 1, 1]; ‘define S 4 = [1, 0, 0]; ‘define S 5 = [1, 0, 1]; ‘define S 6 = [1, 1, 0]; assign //reset state //strings ending //strings ending Q 0 = state[0]; Q 1 = state[1]; Q 2 = state[2]; Z = (state == ‘S 3); in. . . 010 in. . . 100 always @(posedge clk) begin if rst state = ‘S 0; else case (state) ‘S 0: if (X) state = ‘S 4 else state = ‘S 1; ‘S 1: if (X) state = ‘S 2 else state = ‘S 1; ‘S 2: if (X) state = ‘S 4 else state = ‘S 3; ‘S 3: if (X) state = ‘S 2 else state = ‘S 6; ‘S 4: if (X) state = ‘S 4 else state = ‘S 5; ‘S 5: if (X) state = ‘S 2 else state = ‘S 6; ‘S 6: state = ‘S 6; default: begin $display (“invalid state reached”); state = 3’bxxx; endcase endmodule CS 150 - Spring 2001 - Sequential Logic Examples - 7

Finite String Pattern Recognizer z Review of Process y Understanding problem x. Write down

Finite String Pattern Recognizer z Review of Process y Understanding problem x. Write down sample inputs and outputs to understand specification y Derive a state diagram x. Write down sequences of states and transitions for sequences to be recognized y Minimize number of states x. Add missing transitions; reuse states as much as possible y State assignment or encoding x. Encode states with unique patterns y Simulate realization x. Verify I/O behavior of your state diagram to ensure it matches specification CS 150 - Spring 2001 - Sequential Logic Examples - 8

Complex Counter z Synchronous 3 -bit counter has a mode control M y When

Complex Counter z Synchronous 3 -bit counter has a mode control M y When M = 0, the counter counts up in the binary sequence y When M = 1, the counter advances through the Gray code sequence binary: 000, 001, 010, 011, 100, 101, 110, 111 Gray: 000, 001, 010, 111, 100 z Valid I/O behavior (partial) Mode Input M 0 0 1 1 1 0 0 Current State 000 001 010 111 101 110 Next State 001 010 111 101 110 111 CS 150 - Spring 2001 - Sequential Logic Examples - 9

Complex Counter (State Diagram) z Deriving State Diagram y One state for each output

Complex Counter (State Diagram) z Deriving State Diagram y One state for each output combination y Add appropriate arcs for the mode control 0 reset S 0 0 [000] S 1 0 [001] 1 S 2 0 [010] 1 1 1 S 3 0 [011] S 4 0 [100] S 5 0 [101] S 6 0 [110] 1 1 1 CS 150 - Spring 2001 - Sequential Logic Examples - 10 1 S 7 [111]

Complex Counter (State Encoding) z Verilog description including state encoding module string (clk, M,

Complex Counter (State Encoding) z Verilog description including state encoding module string (clk, M, rst, Z 0, Z 1, Z 2); input clk, X, rst; output Z 0, Z 1, Z 2; reg state[0: 2]; ‘define S 0 = [0, 0, 0]; ‘define S 1 = [0, 0, 1]; ‘define S 2 = [0, 1, 0]; ‘define S 3 = [0, 1, 1]; ‘define S 4 = [1, 0, 0]; ‘define S 5 = [1, 0, 1]; ‘define S 6 = [1, 1, 0]; ‘define S 7 = [1, 1, 1]; assign Z 0 = state[0]; assign Z 1 = state[1]; assign Z 2 = state[2]; always @(posedge clk) begin if rst state = ‘S 0; else case (state) ‘S 0: state = ‘S 1; ‘S 1: if (M) state = ‘S 3 ‘S 2: if (M) state = ‘S 6 ‘S 3: if (M) state = ‘S 2 ‘S 4: if (M) state = ‘S 0 ‘S 5: if (M) state = ‘S 4 ‘S 5: if (M) state = ‘S 7 ‘S 5: if (M) state = ‘S 5 endcase endmodule CS 150 - Spring 2001 - Sequential Logic Examples - 11 else else state state = = = = ‘S 2; ‘S 3; ‘S 4; ‘S 5; ‘S 6; ‘S 7; ‘S 0;

Traffic Light Controller as Two Communicating FSMs z Without Separate Timer TS' y S

Traffic Light Controller as Two Communicating FSMs z Without Separate Timer TS' y S 0 would require 7 states S 1 y S 1 would require 3 states TS/ST y S 2 would require 7 states y S 3 would require 3 states y S 1 and S 3 have simple transformation y S 0 and S 2 would require many more arcs S 1 a S 1 b S 1 c x. C could change in any of seven states z By Factoring Out Timer y Greatly reduce number of states x 4 instead of 20 y Counter only requires seven or eight states x 12 total instead of 20 CS 150 - Spring 2001 - Sequential Logic Examples - 12 traffic light controller ST TS TL timer –/ST

Communicating Finite State Machines z One machine's output is another machine's input X FSM

Communicating Finite State Machines z One machine's output is another machine's input X FSM 1 Y CLK FSM 2 FSM 1 A A B C D D X Y==0 A [1] Y==0 X==0 C [0] X==0 FSM 2 Y X==1 Y==1 D [1] B [0] X==1 X==0 machines advance in lock step initial inputs/outputs: X = 0, Y = 0 CS 150 - Spring 2001 - Sequential Logic Examples - 13

Datapath and Control z Digital hardware systems = data-path + control y Datapath: registers,

Datapath and Control z Digital hardware systems = data-path + control y Datapath: registers, counters, combinational functional units (e. g. , ALU), communication (e. g. , busses) y Control: FSM generating sequences of control signals that instructs datapath what to do next "puppeteer who pulls the strings" control status info and inputs state data-path control signal outputs "puppet" CS 150 - Spring 2001 - Sequential Logic Examples - 14

Digital Combinational Lock z Door Combination Lock: y Punch in 3 values in sequence

Digital Combinational Lock z Door Combination Lock: y Punch in 3 values in sequence and the door opens; if there is an error the lock must be reset; once the door opens the lock must be reset y Inputs: sequence of input values, reset y Outputs: door open/close y Memory: must remember combination or always have it available y Open questions: how do you set the internal combination? x. Stored in registers (how loaded? ) x. Hardwired via switches set by user CS 150 - Spring 2001 - Sequential Logic Examples - 15

Implementation in Software integer combination_lock ( ) { integer v 1, v 2, v

Implementation in Software integer combination_lock ( ) { integer v 1, v 2, v 3; integer error = 0; static integer c[3] = 3, 4, 2; while (!new_value( )); v 1 = read_value( ); if (v 1 != c[1]) then error = 1; while (!new_value( )); v 2 = read_value( ); if (v 2 != c[2]) then error = 1; while (!new_value( )); v 3 = read_value( ); if (v 2 != c[3]) then error = 1; if (error == 1) then return(0); else return (1); } CS 150 - Spring 2001 - Sequential Logic Examples - 16

Determining Details of the Specification z How many bits per input value? z How

Determining Details of the Specification z How many bits per input value? z How many values in sequence? z How do we know a new input value is entered? z What are the states and state transitions of the system? new value reset clock open/closed CS 150 - Spring 2001 - Sequential Logic Examples - 17

Digital Combination Lock State Diagram z States: 5 states y Represent point in execution

Digital Combination Lock State Diagram z States: 5 states y Represent point in execution of machine y Each state has outputs z Transitions: 6 from state to state, 5 self transitions, 1 global y Changes of state occur when clock says its ok y Based on value of inputs ERR z Inputs: reset, new, results of comparisons z Output: open/closed C 1!=value & new S 1 reset closed C 1==value & new not new S 2 closed C 2!=value & new S 3 C 2==value & new not new CS 150 - Spring 2001 - Sequential Logic Examples - 18 closed C 3!=value & new C 3==value & new not new OPEN open

Datapath and Control Structure z Datapath y Storage registers for combination values y Multiplexer

Datapath and Control Structure z Datapath y Storage registers for combination values y Multiplexer y Comparator z Control y Finite-state machine controller y Control for data-path (which value to compare) C 1 4 C 2 4 C 3 4 multiplexer new mux control 4 value 4 comparator reset controller clock equal CS 150 - Spring 2001 - Sequential Logic Examples - 19 open/closed

State Table for Combination Lock z Finite-State Machine y Refine state diagram to take

State Table for Combination Lock z Finite-State Machine y Refine state diagram to take internal structure into account y State table ready for encoding reset 1 0 0 0. . . new – 0 1 1 equal – – 0 1 state – S 1 S 1 next state S 1 ERR S 2 mux C 1 – C 2 open/closed closed 1 1 S 3 OPEN – open CS 150 - Spring 2001 - Sequential Logic Examples - 20

Encodings for Combination Lock z Encode state table y State can be: S 1,

Encodings for Combination Lock z Encode state table y State can be: S 1, S 2, S 3, OPEN, or ERR x Needs at least 3 bits to encode: 000, 001, 010, 011, 100 x And as many as 5: 00001, 00010, 00100, 01000, 10000 x Choose 4 bits: 0001, 0010, 0100, 1000, 0000 y Output mux can be: C 1, C 2, or C 3 x Needs 2 to 3 bits to encode x Choose 3 bits: 001, 010, 100 mux control equal x Needs 1 or 2 bits to encode x Choose 1 bit: 1, 0 new – 0 1 1 equal – – 0 1 state – 0001 next state 0001 0000 0010 mux 001 – 010 1 1 0100 1000 – reset controller y Output open/closed can be: open or closed reset 1 0 0 0. . . new clock open/closed 0 0 mux is identical to last 3 bits of state 0 open/closed is identical to first bit of state 0 therefore, we do not even need to implement FFs to hold state, just use outputs 1 CS 150 - Spring 2001 - Sequential Logic Examples - 21

Datapath Implementation for Combination Lock z Multiplexer y Easy to implement as combinational logic

Datapath Implementation for Combination Lock z Multiplexer y Easy to implement as combinational logic when few inputs y Logic can easily get too big for most PLDs value C 1 i C 2 i C 3 i mux control C 1 4 C 2 4 C 3 4 multiplexer 4 value 4 comparator mux control equal CS 150 - Spring 2001 - Sequential Logic Examples - 22

Datapath Implementation (cont’d) z Tri-State Logic y Utilize a third output state: “no connection”

Datapath Implementation (cont’d) z Tri-State Logic y Utilize a third output state: “no connection” or “float” y Connect outputs together as long as only one is “enabled” value C 1 i C 2 i C 3 i y Open-collector gates can only output 0, not 1 x. Can be used to implement logical AND with only wires + C 1 4 C 2 4 C 3 4 multiplexer 4 value 4 comparator oc mux control equal mux control tri-state driver (can disconnect from output) open-collector connection (zero whenever one connection is zero, one otherwise – wired AND) CS 150 - Spring 2001 - Sequential Logic Examples - 23

Tri-State Gates z Third value y Logic values: “ 0”, “ 1” y Don't

Tri-State Gates z Third value y Logic values: “ 0”, “ 1” y Don't care: “X” (must be 0 or 1 in real circuit!) y Third value or state: “Z” — high impedance, infinite R, no connection z Tri-state gates OE y Additional input – output enable (OE) In Out y Output values are 0, 1, and Z y When OE is high, the gate functions normally y When OE is low, the gate is disconnected from wire at output y Allows more than one gate to be connected to the same output wire x As long as only one has its output enabled at any one time (otherwise, sparks could fly) non-inverting tri-state buffer In X 0 1 OE 0 1 1 Out Z 0 1 In OE Out CS 150 - Spring 2001 - Sequential Logic Examples - 24 100

Tri-State and Multiplexing z When Using Tri-State Logic y (1) Never more than one

Tri-State and Multiplexing z When Using Tri-State Logic y (1) Never more than one "driver" for a wire at any one time (pulling high and low at same time can severely damage circuits) y (2) Only use value on wire when its being driven (using a floating value may cause failures) z Using Tri-State Gates to Implement an Economical Multiplexer Input 0 F OE Input 1 OE when Select is high Input 1 is connected to F when Select is low Input 0 is connected to F this is essentially a 2: 1 mux Select CS 150 - Spring 2001 - Sequential Logic Examples - 25

Open-Collector Gates and Wired-AND z Open collector: another way to connect gate outputs to

Open-Collector Gates and Wired-AND z Open collector: another way to connect gate outputs to same wire y Gate only has the ability to pull its output low y Cannot actively drive wire high (default – pulled high through resistor) z Wired-AND can be implemented with open collector logic y If A and B are "1", output is actively pulled low y If C and D are "1", output is actively pulled low y If one gate output is low and the other high, then low wins y If both outputs are "1", the wire value "floats", pulled high by resistor x Low to high transition usually slower than if gate pulling high y Hence, the two NAND functions are ANDed together open-collector NAND gates with ouputs wired together using "wired-AND" to form (AB)'(CD)' CS 150 - Spring 2001 - Sequential Logic Examples - 26

Digital Combination Lock (New Datapath) z Decrease number of inputs z Remove 3 code

Digital Combination Lock (New Datapath) z Decrease number of inputs z Remove 3 code digits as inputs y Use code registers y Make them loadable from value y Need 3 load signal inputs (net gain in input (4*3)– 3=9) x. Could be done with 2 signals and decoder (ld 1, ld 2, ld 3, load none) ld 1 ld 2 C 1 4 ld 3 C 2 4 C 3 4 multiplexer mux control 4 value 4 comparator CS 150 - Spring 2001 - Sequential Logic Examples - 27 equal

Section Summary z FSM Design y Understanding the problem y Generating state diagram y

Section Summary z FSM Design y Understanding the problem y Generating state diagram y Implementation using synthesis tools y Iteration on design/specification to improve qualities of mapping y Communicating state machines z Four case studies y Understand I/O behavior y Draw diagrams y Enumerate states for the "goal" y Expand with error conditions y Reuse states whenever possible CS 150 - Spring 2001 - Sequential Logic Examples - 28