Finite State Machine (FSM) Nattha Jindapetch December 2008.

Slides:



Advertisements
Similar presentations
VERILOG: Synthesis - Combinational Logic Combination logic function can be expressed as: logic_output(t) = f(logic_inputs(t)) Rules Avoid technology dependent.
Advertisements

FSM and Efficient Synthesizable FSM Design using Verilog
Combinational Logic.
Table 7.1 Verilog Operators.
//HDL Example 5-1 // //Description of D latch (See Fig.5-6) module D_latch (Q,D,control); output Q; input.
FSM Revisit Synchronous sequential circuit can be drawn like below  These are called FSMs  Super-important in digital circuit design FSM is composed.
TOPIC : Finite State Machine(FSM) and Flow Tables UNIT 1 : Modeling Module 1.4 : Modeling Sequential circuits.
Give qualifications of instructors: DAP
Slide 1 7. Verilog: Combinational always statements. VHDL: Combinational Processes: To avoid (I.E. DO NOT What in your HDL code?) Cases that generate Synthesis.
CSE Spring Verilog for Sequential Systems - 1 Today: Verilog and Sequential Logic zFlip-flops yrepresentation of clocks - timing of state.
FSM examples.
Spring 20067W. Rhett Davis with minor modifications by Dean Brock ECE 406 at UNASlide 1 ECE 406 Design of Complex Digital Systems Lecture 10: 9: State.
CS 151 Digital Systems Design Lecture 37 Register Transfer Level
Digital System Design by Verilog University of Maryland ENEE408C.
Verilog Sequential Circuits Ibrahim Korpeoglu. Verilog can be used to describe storage elements and sequential circuits as well. So far continuous assignment.
Dr. Turki F. Al-Somani VHDL synthesis and simulation – Part 3 Microcomputer Systems Design (Embedded Systems)
ELEN 468 Lecture 161 ELEN 468 Advanced Logic Design Lecture 16 Synthesis of Language Construct II.
Prof. John Nestor ECE Department Lafayette College Easton, Pennsylvania ECE Senior Design I Lecture 4 - Verilog 2 (Sequential.
ENEE 408C Lab Capstone Project: Digital System Design Fall 2005 Sequential Circuit Design.
ELEN 468 Advanced Logic Design
Advanced Verilog EECS 270 v10/23/06.
1 COMP541 State Machines – 2 Registers and Counters Montek Singh Feb 8, 2007.
Overview Logistics Last lecture Today HW5 due today
Sequential Logic in Verilog
ECE 551 Digital System Design & Synthesis Fall 2011 Midterm Exam Overview.
Chapter 11: System Design Methodology Digital System Designs and Practices Using Verilog HDL and 2008, John Wiley11-1 Ders 8: FSM Gerçekleme ve.
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU 99-1 Under-Graduate Project: RTL Coding Style Speaker: 蔡朝陽 Date: 2010/10/26.
ECE 551 Digital Design And Synthesis
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU Under-Graduate Project: RTL Coding Style Speaker: MingChun-Hsiao Date: 2012/11/27.
Lab7-1 Lab 6: FSM Description Separate combinational and memory circuits –State memory uses FFs –Others are combinational circuits.
EEE2243 Digital System Design Chapter 4: Verilog HDL (Sequential) by Muhazam Mustapha, January 2011.
Slide 1 6. VHDL/Verilog Behavioral Description. Slide 2 Verilog for Synthesis: Behavioral description Instead of instantiating components, describe them.
ECE/CS 352 Digital System Fundamentals© 2001 C. Kime 1 ECE/CS 352 Digital Systems Fundamentals Spring 2001 Chapters 3 and 4: Verilog – Part 2 Charles R.
Register Transfer Level & Design with ASM
Verilog for Synthesis Ing. Pullini Antonio
Charles Kime & Thomas Kaminski © 2004 Pearson Education, Inc. Terms of Use (Hyperlinks are active in View Show mode) Terms of Use Verilog Part 3 – Chapter.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.
Introduction to ASIC flow and Verilog HDL
1 COMP541 State Machines – 2 Registers and Counters Montek Singh Feb 11, 2010.
1 COMP541 State Machines - II Montek Singh Feb 13, 2012.
Chapter 11: System Design Methodology Digital System Designs and Practices Using Verilog HDL and 2008, John Wiley11-1 Chapter 11: System Design.
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU 99-1 Under-Graduate Project Design of Datapath Controllers Speaker: Shao-Wei Feng Adviser:
SYEN 3330 Digital SystemsJung H. Kim Chapter SYEN 3330 Digital Systems Chapters 4 – Part4: Verilog – Part 2.
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
1 Modeling of Finite State Machines Debdeep Mukhopadhyay Associate Professor Dept of Computer Science and Engineering NYU Shanghai and IIT Kharagpur.
EMT 351/4 DIGITAL IC DESIGN Verilog Behavioral Modeling  Finite State Machine -Moore & Mealy Machine -State Encoding Techniques.
Pusat Pengajian Kejuruteraan Mikroelektronik EMT 351/4 DIGITAL IC DESIGN Verilog Behavioural Modeling (Part 4) Week #
Lecture 5. Verilog HDL #3 Prof. Taeweon Suh Computer Science & Engineering Korea University COSE221, COMP211 Logic Design.
1 Lecture 3: Modeling Sequential Logic in Verilog HDL.
Figure Implementation of an FSM in a CPLD..
Exp#7 Finite State Machine Design in Verilog COE203 Digital Logic Laboratory Dr. Ahmad Almulhem KFUPM Spring 2009.
Overview Logistics Last lecture Today HW5 due today
Verilog Tutorial Fall
Figure 8.1. The general form of a sequential circuit.
Last Lecture Talked about combinational logic always statements. e.g.,
© Copyright 2004, Gaetano Borriello and Randy H. Katz
Introduction Introduction to VHDL Entities Signals Data & Scalar Types
Supplement on Verilog Sequential circuit examples: FSM
Buttons and Debouncing Finite State Machine
COMP541 State Machines Montek Singh Feb 4, 2010.
FSM MODELING MOORE FSM MELAY FSM. Introduction to DIGITAL CIRCUITS MODELING & VERIFICATION using VERILOG [Part-2]
COE 202 Introduction to Verilog
Supplement on Verilog Sequential circuit examples: FSM
Dr. Tassadaq Hussain Introduction to Verilog – Part-3 Expressing Sequential Circuits and FSM.
The Verilog Hardware Description Language
Lecture 4: Continuation of SystemVerilog
Dr. Tassadaq Hussain Introduction to Verilog – Part-4 Expressing FSM in Verilog (contd) and FSM State Encoding Dr. Tassadaq Hussain.
CSE 370 – Winter Sequential Logic-2 - 1
Lecture 7: Verilog Part II
Presentation transcript:

Finite State Machine (FSM) Nattha Jindapetch December 2008

Agenda Finite State Machine (FSM) Synthesis techniques Lab5

Finite State Machines Basically a FSM consists of Control logic is used to decide the next state of the FSM sequential is used to store the current state of the FSM output logic is a mixture of both comb. and seq. logic Two types Moore machine O = f(Q) Q’= f(Q,I) Mealy machine O = f(Q,I) Q’= f(Q,I)

Example 1 State diagram State table

Example 1 State diagram module jk_counter(count, clock); input clock; output reg [2:0] count; parameter [2:0] A = 3'b000, B = 3'b100, C = 3'b111, D = 3'b010, E = 3'b011; (posedge clock) case (count) A: count <= B; B: count <= C; C: count <= D; D: count <= E; E: count <= A; default: count <= A; endcase endmodule

Ex2: Moore Machines   Check input steam  If in = 1 two consecutive periods, then out=1 State Diagram S0 “ 0 ” S1 S2 “ 1 ” Symbol S0 “ out ” in

The meaning of State Diagram   For example, at state S0  At state S0 the circuit output out = 0  At the rising-edge of clk, if in = 1, the state will change to S1, otherwise S0 0 S0 “ 0 ” S1 S2 “ 1 ”

Ex2: Moore Machines module Moore1 (clk, IN, OUT); input clk, IN; output OUT; reg [1:0] State; parameter [1:0] s0=2’b00, s1=2’b01, s2=2’b11; (posedge clock) case (State) s0: begin OUT <= 0; if (IN) State <= s1; else State <= s0; end s1: begin OUT <= 0; if (IN) State <= s2; else State <= s0; end s2: begin OUT <= 1; if (!IN) State <= s0; else State <= s2; end default: begin OUT <= 0; if (IN) State <= s1; else State <= s0; endcase endmodule

Ex3: Mealy Machines State Diagram S0S1 0/00/01/0 1/1 Symbol S0 in/ out   Check input steam  If in = 1 two consecutive periods, then out=1

 For example, at state S1  At S1, the output OUT = IN (OUT=1 if IN=1 and OUT=0 if IN=0)  At the rising-edge of clk, if IN = 0 the state will change to S0, otherwise S1 0/ 0 1/ 1 S0S1 0/0 1/0 The meaning of State Diagram

Ex3: Mealy Machines module Mealy1 (clk, IN, OUT); input clk, IN; output OUT; reg State; parameter s0=1’b0, s1=1’b1; (posedge clock) case (State) s0: begin OUT <= 0; if (IN) State <= s1; else State <= s0; end s1: begin OUT <= IN; if (IN) State <= s2; else State <= s0; end default: begin OUT <= 0; if (IN) State <= s1; else State <= s0; end endcase endmodule

Ex4: Arbiter  First come first serve

Ex4_1: Arbiter Code Using A Function for Control Logic (1/3) module fsm_using_function (clock, reset, req_0, req_1, gnt_0, gnt_1); input clock,reset,req_0,req_1; output gnt_0,gnt_1; wire clock,reset,req_0,req_1; reg gnt_0,gnt_1; parameter SIZE = 3 ; parameter IDLE =3'b001, GNT0 = 3'b010,GNT1 = 3'b100; reg [SIZE-1:0] state; wire [SIZE-1:0] next_state; assign next_state = fsm_function(state, req_0, req_1); // Function for Control Logic function [SIZE-1:0] fsm_function; input [SIZE-1:0] state ; input req_0 ; input req_1 ; case (state) IDLE : if (req_0 == 1'b1) begin fsm_function = GNT0; end else if (req_1 == 1'b1) begin fsm_function= GNT1; end else begin fsm_function = IDLE; end

Ex4_1: Arbiter Code Using A Function for Control Logic (2/3) GNT0 : if (req_0 == 1'b1) begin fsm_function = GNT0; end else begin fsm_function = IDLE; end GNT1 : if (req_1 == 1'b1) begin fsm_function = GNT1; end else begin fsm_function = IDLE; end default : fsm_function = IDLE; endcase endfunction // Seq Logic (posedge clock) begin : FSM_SEQ if (reset == 1'b1) begin state <= #1 IDLE; end else begin state <= #1 next_state; end

Ex4_1: Arbiter Code Using A Function for Control Logic (3/3) // Output Logic (posedge clock) begin : OUTPUT_LOGIC if (reset == 1'b1) begin gnt_0 <= #1 1'b0; gnt_1 <= #1 1'b0; end else begin case (state) IDLE : begin gnt_0 <= #1 1'b0; gnt_1 <= #1 1'b0; end GNT0 : begin gnt_0 <= #1 1'b1; gnt_1 <= #1 1'b0; end GNT1 : begin gnt_0 <= #1 1'b0; gnt_1 <= #1 1'b1; end default : begin gnt_0 <= #1 1'b0; gnt_1 <= #1 1'b0; end endcase end end // End Of Block OUTPUT_LOGIC endmodule // End of Module arbiter

Ex4_2: Arbiter Code Using Two Always Blocks (1/2) module fsm_using_always (clock, reset, req_0, req_1, gnt_0, gnt_1); input clock,reset,req_0,req_1; output gnt_0,gnt_1; wire clock,reset,req_0,req_1; reg gnt_0,gnt_1; parameter SIZE = 3 ; parameter IDLE = 3'b001,GNT0 = 3'b010,GNT1 = 3'b100 ; reg [SIZE-1:0] state; reg [SIZE-1:0] next_state; (state or req_0 or req_1) begin : FSM_CONTROL next_state = 3'b000; case (state) IDLE : if (req_0 == 1'b1) next_state = GNT0; else if (req_1 == 1'b1) next_state= GNT1; else next_state = IDLE; GNT0 : if (req_0 == 1'b1) next_state = GNT0; else next_state = IDLE; GNT1 : if (req_1 == 1'b1) next_state = GNT1; else next_state = IDLE; default : next_state = IDLE; endcase end

Ex4_2: Arbiter Code Using Two Always Blocks (2/2) // Seq Logic- (posedge clock) begin : FSM_SEQ if (reset == 1'b1) state <= #1 IDLE; else state <= #1 next_state; end // Output Logic (posedge clock) begin : OUTPUT_LOGIC if (reset == 1'b1) begin gnt_0 <= #1 1'b0; gnt_1 <= #1 1'b0; end else begin case (state) IDLE : begin gnt_0 <= #1 1'b0; gnt_1 <= #1 1'b0; end GNT0 : begin gnt_0 <= #1 1'b1; gnt_1 <= #1 1'b0; end GNT1 : begin gnt_0 <= #1 1'b0; gnt_1 <= #1 1'b1; end default : begin gnt_0 <= #1 1'b0; gnt_1 <= #1 1'b0; end endcase end end // End Of Block OUTPUT_LOGIC endmodule // End of Module arbiter

(posedge clock) begin : FSM if (reset == 1'b1) begin state <= #1 IDLE; gnt_0 <= 0; gnt_1 <= 0; end else case (state) IDLE : if (req_0 == 1'b1) begin state <= #1 GNT0; gnt_0 <= 1; end else if (req_1 == 1'b1) begin gnt_1 <= 1; state <= #1 GNT1; end else state <= #1 IDLE; module fsm_using_always (clock, reset, req_0, req_1, gnt_0, gnt_1); input clock,reset,req_0,req_1; output gnt_0,gnt_1; wire clock,reset,req_0,req_1; reg gnt_0,gnt_1; parameter SIZE = 3 ; parameter IDLE = 3'b001,GNT0 = 3'b010,GNT1 = 3'b100 ; reg [SIZE-1:0] state; reg [SIZE-1:0] next_state; Ex4_3: Arbiter Code Using Single Always For Sequential, Control Logic And Output Logic (1/2)

GNT0 : if (req_0 == 1'b1) begin state <= #1 GNT0; end else begin gnt_0 <= 0; state <= #1 IDLE; end GNT1 : if (req_1 == 1'b1) begin state <= #1 GNT1; end else begin gnt_1 <= 0; state <= #1 IDLE; end default : state <= #1 IDLE; endcase end endmodule // End of Module arbiter Ex4_3: Arbiter Code Using Single Always For Sequential, Control Logic And Output Logic (2/2)

Synthesis Techniques

Using Hierarchy leads to greater design readability, reuse, and debug Hierarchical Design Top-Level ControlDatapathMemory FSM1FSM2ROMRAM ALUCountersRegs

Benefits of Using Hierarchy Design readability Easier to understand the design functionality and data flow Easier to debug Easy to reuse parts of a design

Coding Tips Synchronous reset — better system control but depend on the circuit behavior Asynchronous Reset CLOCK or posedge RESET) if (RESET) Q = 0; else Q = D_IN; Asynchronous Reset CLOCK or posedge RESET) if (RESET) Q = 0; else Q = D_IN; Synchronous Reset CLOCK) if (RESET) Q = 0; else Q = D_IN; Synchronous Reset CLOCK) if (RESET) Q = 0; else Q = D_IN;

Coding Tips Order and group arithmetic and logical functions and operators A <= (B + C) + (D + E); is better than A <= B + C + D + E;

Basic Performance Tips Simple coding yields better performance Avoid high-level loop constructs Synthesis tools may not produce optimal results Avoid nested if-then-else statements Most tools implement these in parallel; however, multiple nested if-then-else statements can result in priority encoded logic Use case statements for large decoding Rather than if-then-else

State Machine Encoding Use enumerated types to define state vectors Most synthesis tools have commands to extract and re-encode state machines described in this way Use one-hot encoding for high-performance state machines Uses more registers, but simplifies next-state logic Experiment to discover how your synthesis tool chooses the default encoding scheme

Lab5: FSM for display scan Four 7-segment Digits share the same data bus

Lab5: FSM for display scan State3 D=0111 sel=11 State0 D=1110 sel=00 State1 D=1101 sel=01 State2 D=1011 sel=10 Reset