fsms and synchronization. asynchronous inputs in sequential systems

44
FSMs and Synchroni zation

Post on 21-Dec-2015

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

FSMs and Synchronization

Page 2: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Asynchronous Inputs in Sequential Systems

Page 3: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Asynchronous Inputs in Sequential Systems

Page 4: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Asynchronous Inputs in Sequential Systems

Page 5: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Asynchronous Inputs in Sequential Systems

如此可避免發生如此可避免發生 II 、、 IIII 類時序不同步之問題類時序不同步之問題 但對於第三類觸發時間不確定確無法克服但對於第三類觸發時間不確定確無法克服??

Page 6: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Handling Metastability Preventing metastability turns out to be an i

mpossible problem High gain of digital devices makes it likely t

hat metastable conditions will resolve themselves quickly

Solution to metastability: allow time for signals to stabilize

Page 7: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Handling MetastabilityHow many registers are necessary? Depends on many design parameters(clock speed, device sp

eeds, …),one or maybe two synchronization registers is sufficient

Page 8: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Finite State Machines

Finite State Machines (FSMs) are a useful abstraction for sequential circuits with centralized “states” of operation

At each clock edge, combinational logic computes outputs and next state as a function of inputs and present state

Page 9: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Finite State Machines

Page 10: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Two Types of FSMs

Moore FSMs

輸出只與目前狀態有關 Mealy FSMs

輸出與目前狀態及輸入有關 Moore and Mealy FSMs are distinguis

hed by their output generation

Page 11: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Moore FSMs

Moore and Mealy FSMs are distinguished by their output generation

Page 12: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Mealy FSMs

Page 13: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

FSM Design Example: Level-to-Pulse

功能: A level-to-pulse converter produces a single-cycle pulse each time its input goes high.

In other words, it’s a synchronous rising edge detector.

應用例 : Buttons and switches pressed by humans for arbitrary periods of time Single-cycle enable signals for counters

Page 14: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

FSM Design Example: Level-to-Pulse

Page 15: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

State Transition Diagrams

如何設計如何設計 Level to Pulse FSM?Level to Pulse FSM?狀態轉移圖是一種非常有用之工具。狀態轉移圖是一種非常有用之工具。

Page 16: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

State Transition Diagrams

狀態 A狀態 B

轉移

觸發輸入 /輸出

轉移

觸發輸入 /輸出

狀態… .

Page 17: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

State Transition Diagrams

Page 18: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Logic Derivation for a Moore FSM

應用轉移應用轉移 寫出下一狀態、寫出下一狀態、輸出與輸入、目前輸出與輸入、目前狀態之真假值表狀態之真假值表

Page 19: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Logic Derivation for a Moore FSM

對應之組合邏輯應用對應之組合邏輯應用K-mapK-map 化簡化簡

Page 20: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Moore Level-to-Pulse Converter

Page 21: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Design of a Mealy Level-to-Pulse

Page 22: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Mealy Level-to-Pulse Converter

Page 23: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Moore/Mealy Trade-OffsRemember that the difference is in the output:

•Moore outputs are based on state only•Mealy outputs are based on state and input•Therefore, Mealy outputs generally occur one cycle earlier than a Moore:

Compared to a Moore FSM, a Mealy FSM might...

•Be more difficult to conceptualize and design• Have fewer states

Page 24: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

FSM Timing Requirements

Page 25: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

FSM Timing Requirements

Page 26: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Vending Machine

Page 27: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

What States are in the System?

Page 28: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

A Moore Vender

Page 29: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

State Reduction

Page 30: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Verilog for the Moore Vender

module mooreVender (N, D, Q, DC, DN, DD,clk, reset, state);

input N, D, Q, clk, reset;output DC, DN, DD;output [3:0] state;reg [3:0] state, next;

Page 31: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

parameter IDLE = 0;parameter GOT_5c = 1;parameter GOT_10c = 2;parameter GOT_15c = 3;parameter GOT_20c = 4;parameter GOT_25c = 5;parameter GOT_30c = 6;parameter GOT_35c = 7;parameter GOT_40c = 8;parameter GOT_45c = 9;parameter GOT_50c = 10;parameter RETURN_20c = 11;parameter RETURN_15c = 12;parameter RETURN_10c = 13;parameter RETURN_5c = 14

Page 32: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

always @ (posedge clk or negedge reset)if (!reset) state <= IDLE;

else state <= next;

Page 33: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

always @ (state or N or D or Q) begincase (state)

IDLE: if (Q) next = GOT_25c;else if (D) next = GOT_10c;else if (N) next = GOT_5c;else next = IDLE;

GOT_5c: if (Q) next = GOT_30c;else if (D) next = GOT_15c;else if (N) next = GOT_10c;else next = GOT_5c;

GOT_10c: if (Q) next = GOT_35c;else if (D) next = GOT_20c;else if (N) next = GOT_15c;else next = GOT_10c;

GOT_15c: if (Q) next = GOT_40c;else if (D) next = GOT_25c;else if (N) next = GOT_20c;else next = GOT_15c;

GOT_20c: if (Q) next = GOT_45c;else if (D) next = GOT_30c;else if (N) next = GOT_25c;else next = GOT_20c;

GOT_25c: if (Q) next = GOT_50c;else if (D) next = GOT_35c;else if (N) next = GOT_30c;else next = GOT_25c;

GOT_30c: next = IDLE;GOT_35c: next = RETURN_5c;GOT_40c: next = RETURN_10c;GOT_45c: next = RETURN_15c;GOT_50c: next = RETURN_20c;RETURN_20c: next = RETURN_10

c;RETURN_15c: next = RETURN_5c;RETURN_10c: next = IDLE;RETURN_5c: next = IDLE;default: next = IDLE;

endcaseend

Page 34: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

assign DC = (state == GOT_30c || state == GOT_35c ||state == GOT_40c || state == GOT_45c ||state == GOT_50c);

assign DN = (state == RETURN_5c);assign DD = (state == RETURN_20c || state ==

RETURN_15c || state == RETURN_10c);

endmodule

Page 35: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Simulation of Moore Vender

Page 36: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Coding Alternative: Two Blocksalways @ (state or N or D or Q) beginDC = 0; DD = 0; DN = 0; // defaultscase (state)

IDLE: if (Q) next = GOT_25c;else if (D) next = GOT_10c;else if (N) next = GOT_5c;else next = IDLE;

GOT_5c: if (Q) next = GOT_30c;else if (D) next = GOT_15c;else if (N) next = GOT_10c;else next = GOT_5c;

GOT_10c: if (Q) next = GOT_35c;else if (D) next = GOT_20c;else if (N) next = GOT_15c;else next = GOT_10c;

GOT_15c: if (Q) next = GOT_40c;else if (D) next = GOT_25c;else if (N) next = GOT_20c;else next = GOT_15c;

GOT_20c: if (Q) next = GOT_45c;else if (D) next = GOT_30c;else if (N) next = GOT_25c;else next = GOT_20c;

GOT_25c: if (Q) next = GOT_50c;else if (D) next = GOT_35c;else if (N) next = GOT_30c;else next = GOT_25c;

GOT_30c: beginDC = 1; next = IDLE;end

GOT_35c: beginDC = 1; next = RETURN_5c;end

GOT_40c: beginDC = 1; next = RETURN_10c;end

GOT_45c: beginDC = 1; next = RETURN_15c;end

GOT_50c: beginDC = 1; next = RETURN_20c;end

RETURN_20c: beginDD = 1; next = RETURN_10c;end

RETURN_15c: beginDD = 1; next = RETURN_5c;end

RETURN_10c: beginDD = 1; next = IDLE;end

RETURN_5c: beginDN = 1; next = IDLE;end

default: next = IDLE;endcaseend

Page 37: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

FSM Output Glitching FSM state bits may not transition at precisely the same time Combinational logic for outputs may contain hazards Result: your FSM outputs may glitch!

Page 38: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

If the soda dispenser is glitch-sensitive, your customers can get a 20-cent soda!

Page 39: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Registered FSM Outputs are Glitch-Free

reg DC,DN,DD;// Sequential always block for state a

ssignmentalways @ (posedge clk or negedge r

eset) beginif (!reset) state <= IDLE;else if (clk) state <= next;

DC <= (next == GOT_30c || next == GOT_35c ||next == GOT_40c || next == GOT_45c ||next == GOT_50c);

DN <= (next == RETURN_5c);DD <= (next == RETURN_20c || next =

= RETURN_15c ||next == RETURN_10c);

ENDEND

Page 40: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Mealy Vender (covered in Recitation)

Page 41: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Verilog for Mealy FSmodule mealyVender (N, D, Q, DC, DN, DD, clk, reset, state);input N, D, Q, clk, reset;output DC, DN, DD;reg DC, DN, DD;output [3:0] state;reg [3:0] state, next;parameter IDLE = 0;parameter GOT_5c = 1;parameter GOT_10c = 2;parameter GOT_15c = 3;parameter GOT_20c = 4;parameter GOT_25c = 5;parameter RETURN_20c = 6;parameter RETURN_15c = 7;parameter RETURN_10c = 8;parameter RETURN_5c = 9;// Sequential always block for state assignmentalways @ (posedge clk or negedge reset)

if (!reset) state <= IDLE;else state <= next;

Page 42: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

always @ (state or N or D or Q) beginDC = 0; DN = 0; DD = 0; // defaults

case (state)IDLE: if (Q) next = GOT_25c;

else if (D) next = GOT_10c;else if (N) next = GOT_5c;else next = IDLE;

GOT_5c: if (Q) beginDC = 1; next = IDLE;endelse if (D) next = GOT_15c;else if (N) next = GOT_10c;else next = GOT_5c;

GOT_10c: if (Q) beginDC = 1; next = RETURN_5c;endelse if (D) next = GOT_20c;else if (N) next = GOT_15c;else next = GOT_10c;

GOT_15c: if (Q) beginDC = 1; next = RETURN_10c;endelse if (D) next = GOT_25c;else if (N) next = GOT_20c;else next = GOT_15c;

GOT_20c: if (Q) beginDC = 1; next = RETURN_15c;endelse if (D) beginDC = 1; next = IDLE;endelse if (N) next = GOT_25c;else next = GOT_20c;

GOT_25c: if (Q) beginDC = 1; next = RETURN_20c;endelse if (D) beginDC = 1; next = RETURN_5c;endelse if (N) beginDC = 1; next = IDLE;endelse next = GOT_25c;RETURN_20c: begin

DD = 1; next = RETURN_10c;end

RETURN_15c: beginDD = 1; next = RETURN_5c;end

RETURN_10c: beginDD = 1; next = IDLE;end

RETURN_5c: beginDN = 1; next = IDLE;end

default: next = IDLE;endcase

endendmodule

Page 43: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Simulation of Mealy Vender

Page 44: FSMs and Synchronization. Asynchronous Inputs in Sequential Systems

Summary Synchronize all asynchronous inputs

Use two back to back registers Two types of Finite State Machines introduc

ed Moore – Outputs are a function of current state Mealy – outputs a function of current state and i

nput A standard template can be used for coding

FSMs Register outputs of combinational logic for

critical control signals