ECE 4514 Digital Design II. Spring Lecture 15: FSM-based Control

Similar documents
FSM Components. FSM Description. HDL Coding Methods. Chapter 7: HDL Coding Techniques

EE178 Lecture Verilog FSM Examples. Eric Crabill SJSU / Xilinx Fall 2007

CMPE 415 Verilog Case-Statement Based State Machines II

ECE 2300 Digital Logic & Computer Organization. More Finite State Machines

EECS150 - Digital Design Lecture 5 - Verilog Logic Synthesis

Verilog Sequential Logic. Verilog for Synthesis Rev C (module 3 and 4)

Quick Introduction to SystemVerilog: Sequental Logic

ECE 551: Digital System *

EEL 4783: HDL in Digital System Design

CSE140L: Components and Design Techniques for Digital Systems Lab

Laboratory Exercise 3 Davide Rossi DEI University of Bologna AA

FSM and Efficient Synthesizable FSM Design using Verilog

CSE140L: Components and Design

CMPE 415 Verilog Case-Statement Based State Machines II

Designing Safe Verilog State Machines with Synplify

Graduate Institute of Electronics Engineering, NTU. Lecturer: Chihhao Chao Date:

ECE 4514 Digital Design II. Spring Lecture 13: Logic Synthesis

Synthesis vs. Compilation Descriptions mapped to hardware Verilog design patterns for best synthesis. Spring 2007 Lec #8 -- HW Synthesis 1

EECS150 - Digital Design Lecture 20 - Finite State Machines Revisited

EECS150 - Digital Design Lecture 10 Logic Synthesis

Logic Synthesis. EECS150 - Digital Design Lecture 6 - Synthesis

EECS150 - Digital Design Lecture 10 Logic Synthesis

Blocking(=) vs Nonblocking (<=) Assignment. Lecture 3: Modeling Sequential Logic in Verilog HDL. Procedural assignments

Verilog for High Performance

In the previous lecture, we examined how to analyse a FSM using state table, state diagram and waveforms. In this lecture we will learn how to design

In the previous lecture, we examined how to analyse a FSM using state table, state diagram and waveforms. In this lecture we will learn how to design

Finite State Machines (FSM) Description in VHDL. Review and Synthesis

Lecture 3. Behavioral Modeling Sequential Circuits. Registers Counters Finite State Machines

RTL Design (Using ASM/SM Chart)

VHDL for Synthesis. Course Description. Course Duration. Goals

Outline. EECS Components and Design Techniques for Digital Systems. Lec 11 Putting it all together Where are we now?

Modeling of Finite State Machines. Debdeep Mukhopadhyay

EECS150 - Digital Design Lecture 4 - Verilog Introduction. Outline

General FSM design procedure

Graduate Institute of Electronics Engineering, NTU Design of Datapath Controllers

ECEN 468 Advanced Logic Design

CSE140L: Components and Design Techniques for Digital Systems Lab. FSMs. Instructor: Mohsen Imani. Slides from Tajana Simunic Rosing

Verilog for Synthesis Ing. Pullini Antonio

Laboratory Finite State Machines and Serial Communication

Digital Design with FPGAs. By Neeraj Kulkarni

Synthesis of Combinational and Sequential Circuits with Verilog

Last Lecture. Talked about combinational logic always statements. e.g., module ex2(input logic a, b, c, output logic f); logic t; // internal signal

ECE 2300 Digital Logic & Computer Organization. More Verilog Finite State Machines

ECE 2300 Digital Logic & Computer Organization. More Verilog Finite State Machines

Topics. Midterm Finish Chapter 7

FINITE STATE MACHINES (FSM) DESCRIPTION IN VHDL. Cristian Sisterna UNSJ

Sequential Logic Design

Finite State Machines

FPGA Design Challenge :Techkriti 14 Digital Design using Verilog Part 1

Homework deadline extended to next friday

General FSM design procedure

Laboratory Exercise 7

FPGA for Software Engineers

Writing Circuit Descriptions 8

ECE 551 Digital System Design and Synthesis. Instructor: Kewal K. Saluja. Midterm Exam

Creating Safe State Machines

ECE 574: Modeling and Synthesis of Digital Systems using Verilog and VHDL. Fall 2017 Final Exam (6.00 to 8.30pm) Verilog SOLUTIONS

Chapter 10. case studies in sequential logic design

Digital Integrated Circuits

Topics. Midterm Finish Chapter 7

Control in Digital Systems

Last Lecture: Divide by 3 FSM

Finite-State Machine (FSM) Design

Mealy and Moore examples

Verilog introduction. Embedded and Ambient Systems Lab

Bulletproofing FSM Verification Automated Approach to Detect Corner Case Issues in an FSM Design

Lecture 15: System Modeling and Verilog

HDL Design Cube. Synthesis and VHDL

Parallel versus serial execution

Finite State Machines

State Machine Descriptions

EECS150 - Digital Design Lecture 7 - Computer Aided Design (CAD) - Part II (Logic Simulation) Finite State Machine Review

Lecture 12 VHDL Synthesis

EN164: Design of Computing Systems Lecture 07: Lab Foundations / Verilog 3

Digital Integrated Circuits

EECS 270 Verilog Reference: Sequential Logic

ECE 2300 Digital Logic & Computer Organization. More Sequential Logic Verilog

Chapter 9: Sequential Logic Modules

Lecture #2: Verilog HDL

Synthesizable Verilog

An easy to read reference is:

Techniques for Digital Systems Lab. Verilog HDL. Tajana Simunic Rosing. Source: Eric Crabill, Xilinx

EECS150 - Digital Design Lecture 6 - Logic Simulation

ARM 64-bit Register File

Course Topics - Outline

EPC6055 Digital Integrated Circuits EXAM 1 Fall Semester 2013

Recommended Design Techniques for ECE241 Project Franjo Plavec Department of Electrical and Computer Engineering University of Toronto

Synthesis of Language Constructs. 5/10/04 & 5/13/04 Hardware Description Languages and Synthesis

Register Transfer Level

Asynchronous FIFO Design

EN164: Design of Computing Systems Lecture 06: Lab Foundations / Verilog 2

VHDL Modeling Behavior from Synthesis Perspective -Part B - EL 310 Erkay Savaş Sabancı University

EE 231 Fall EE 231 Homework 8 Due October 20, 2010

Abstraction of State Elements. Sequential Logic Implementation. Forms of Sequential Logic. Finite State Machine Representations

Registers and finite state machines

Register Transfer Level in Verilog: Part I

University of Hawaii EE 361L. Getting Started with Spartan 3E Digilent Basys2 Board. Lab 4.1

Sequential Circuits. inputs Comb FFs. Outputs. Comb CLK. Sequential logic examples. ! Another way to understand setup/hold/propagation time

Verilog Module 1 Introduction and Combinational Logic

Advanced Digital Design with the Verilog HDL

Transcription:

ECE 4514 Digital Design II Lecture 15: FSM-based Control A Design Lecture

Overview Finite State Machines Verilog Mapping: one, two, three always blocks State Encoding User-defined or tool-defined State encoding techniques: one-hot, user-defined, gray Synthesis Issues Default state assignment Safe Implementations Next-state logic: RAM or LUT FSM-based control of Datapath (gcd)

Finite State Machine Template Sequential Machine defined by Set of Inputs and Outputs Set of States Initial State (reset function) State Transitions inputs next-state function state register output function outputs (in case of Mealy Machine)

Design Process Designer Creates State Transition Graph Extract State Transition Table Choose State Encoding Create Next State Logic

Design Process Designer Creates State Transition Graph Extract State Transition Table Choose State Encoding Create Next State Logic full-structural Verilog Model (design all gates)

Design Process Designer Creates State Transition Graph Extract State Transition Table Choose State Encoding Create Next State Logic Structural Verilog Model Behavioral Verilog Model (with explicit state encoding)

Design Process Designer Creates State Transition Graph We will focus on and Extract State Transition Table Choose State Encoding Create Next State Logic Structural Verilog Model Behavioral Verilog Model (with explicit state encoding) Behavioral Verilog Model (with automatic state assignment)

Example FSM Design Create an FSM that turns the first '1' of a string of consecutive '1' into a '0'. Each clock cycle, a new input is provided Input 1 0 1 0 0 0 1 1 1 0 0 1 1 1 1 1 1 1 Output 0 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1 1

Example FSM Design How many states? Input Output 1 0 1 0 0 0 1 1 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1 1

Example FSM Design How many states? Input Output 1 0 1 0 0 0 1 1 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1 1 current state x ( = I have not seen a '1') 3 states 1 ( = I have seen a single '1') 1 ( = I have seen more then a single '1')

Example FSM Design How many states? Input Output 1 0 1 0 0 0 1 1 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1 1 S0 S1 S2

Example FSM Design How many states? Input Output 1 0 1 0 0 0 1 1 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1 1 S0 0 input 0 0 S1 1 output S0 -> 0 S1 -> 0 S2 -> 1 1 S2 1

Verilog Mapping of FSM - Generic Ideas Use 'parameter' to express state encoding parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10; This is a symbolic encoding, not necessarily the encoding that will be used by the synthesis tool Use standard conventions for coding logic Use non-blocking <= for registers, specify correct reset and edge-triggered behavior Use blocking = for combinational logic, make sure sensitivity lists are complete

Example FSM Design - Verilog Mapping I module fsm(q, i, clk, rst); input i, clk, rst; output q; reg q; reg [1:0] state; parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10; 0 S0 1 0 endmodule always @(posedge clk or posedge rst) if (rst) begin state <= s0; q <= 1'b0; end else begin case (state) s0: if (i == 1'b1) begin state <= s1; q <= 1'b0; end else begin state <= s0; q <= 1'b0; end s1:.. s2:.. endcase end 0 S1 S2 output S0 -> 0 S1 -> 0 S2 -> 1 1 1

Example FSM Design - Verilog Mapping I One single always block Reset and update in same block Output will always have a register (not always desired..) inputs next-state function state register output function outputs

Example FSM Design - Verilog Mapping II reg q; reg [1:0] state; parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10; S0 0 always @(posedge clk or posedge rst) if (rst) state <= s0; else case (state) s0: if (i == 1'b1) state <= s1; else state <= s0; s1:.. endcase always @(state) q = 1'b0; // default assignment case (state) s0: q = 1'b0; s1: q = 1'b0; s2: q = 1'b1; endcase endmodule 0 0 S1 S2 1 output S0 -> 0 S1 -> 0 S2 -> 1 1 1

Example FSM Design - Verilog Mapping II Two always block Reset and update still in same block Output can be combinational inputs next-state function state register output function outputs

Example FSM Design - Verilog Mapping III reg q; reg [1:0] state, next_state; parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10; S0 0 always @(posedge clk or posedge rst) if (rst) state <= s0; else state <= next_state; always @(state or i) begin next_state = s0; case (state) s0: if (i == 1'b1) next_state = s1; else next_state = s0; s1:.. endcase end always @(state) // output encoding endmodule 0 0 S1 S2 1 output S0 -> 0 S1 -> 0 S2 -> 1 1 1

Example FSM Design - Verilog Mapping III Three always block Reset and update in different blocks Output can be combinational inputs next-state function state register output function outputs

One, two, three always block 1 always block 2 always block 3 always block Combinational Output Reset/Update separate from next-state Logic

Synthesis of FSM Output of XST on Example 1 (single always block): ========================================================================= * HDL Synthesis * ========================================================================= Performing bidirectional port resolution... Synthesizing Unit <myfsm>. Related source file is "myfsm.v". Found finite state machine <FSM_0> for signal <state>. ----------------------------------------------------------------------- States 3 Transitions 6 Inputs 1 Outputs 3 Clock clk (rising_edge) Reset rst (positive) Reset type asynchronous Reset State 00 Encoding automatic Implementation LUT ----------------------------------------------------------------------- Found 1-bit register for signal <q>. Summary: inferred 1 Finite State Machine(s). inferred 1 D-type flip-flop(s). Unit <myfsm> synthesized. Synthesis tool treats FSM as a separate entity

RTL Schematic State Machine Output Encoding Output Flipflop

Synthesis of FSM ========================================================================= * Advanced HDL Synthesis * ========================================================================= Analyzing FSM <FSM_0> for best encoding. Optimizing FSM <state> on signal <state[1:2]> with gray encoding. ------------------- State Encoding ------------------- 00 00 01 01 10 11 ------------------- This encoding is used in actual implementation This state was chosen in parameter entry This encoding style was chosen by the tool (encoding is selected to be 'automatic') parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10;

Gray Encoding? A binary code in which subsequent words differ by only a single bit 1-bit gray code 0 1 2-bit gray code 00 01 11 10 mirror 3-bit gray code 000 001 011 010 110 111 101 100 mirror Gray Codes require minimal logic transitions to go from one state to the next. (= mininal power consumption, minimal risk for glitches & hazards)

What other types of encoding are common? One-hot: Select one bit for each state Each state transition flips only two bits Good for FPGA (with lots of flip-flops) Not so efficient with large number of states or large number of state transitions, because state-decoding becomes too big 16 flip-flops 4 flip-flops next-state encoding one-hot encoding with 16 states next-state encoding normal encoding with 16 states

What other types of encoding are common? Sequential: Encode successive states in sequence Simplifies next-state logic if there are long sequences 1 2 3 4 5... User-defined: Keeps the same encoding as specified by the designer parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10; Synthesis tool tries to select a good encoding automatically This choice is only a heuristic!

Can we enforce the encoding? What if we insist that the encoding should be User-defined: parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10; Solution 1: Change encoding option in the synthesis tool Select User Encoding

Can we enforce the encoding? What if we insist that the encoding should be User-defined: parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10; Solution 2: Provide pragma in the Verilog Code (This is also called a synthesis constraint) (* fsm_encoding = user *) module fsm(q, i, clk, rst); input i, clk, rst; output q; reg q; reg [1:0] state; parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10;... endmodule

Example 1 with one-hot encoding (* fsm_encoding = one-hot *) module fsm(q, i, clk, rst); input i, clk, rst; output q; reg q; reg [1:0] state; parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10;... endmodule ========================================================================= * Advanced HDL Synthesis * ========================================================================= Optimizing FSM <state> on signal <state[1:3]> with one-hot encoding. ------------------- State Encoding ------------------- 00 001 01 010 10 100 -------------------

Example 1 with one-hot encoding

Another way of writing one-hot state encoding reg q; reg [2:0] state, next_state; parameter s0 = 3'd0, s1 = 3'd1, s2 = 3'd2; always @(posedge clk or posedge rst) if (rst) state <= 3'd1; else state <= next_state; always @(state or i) begin next_state = 3'd0; case (1'b1) state[s0]: if (i == 1'b0) next_state[s1] = 1'd1; else next_state[s0] = 1'd1; state[s1]:.. endcase end index into state register next-state selection This example is not recognized as a finite state machine by XST, so that the user-specified encoding will be used.

Default State Assignment reg q; reg [1:0] state, next_state; parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10; S0 0 always @(posedge clk or posedge rst) if (rst) state <= s0; else state <= next_state; always @(state or i) begin next_state = s0; case (state) s0: if (i == 1'b0) next_state = s1; else next_state = s0; s1:.. endcase end always @(state) // output encoding endmodule '11' 0 0 S1 S2 1 1 Default next-state assignment may deal with improper initialization and faults 1

Safe Implementation A 'Safe FSM' is an FSM that will convert each invalid state automatically to a recovery state (usually the reset state) S0 0 recovery state 0 S1 1 '11' illegal state 1 S2 1

Default (unsafe) implementation module fsm(q, i, clk, rst); input i, clk, rst; output q; reg q; reg [1:0] state; parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10; 0 S0 1 0 endmodule always @(posedge clk or posedge rst) if (rst) begin state <= s0; q <= 1'b0; end else begin case (state) s0: if (i == 1'b0) begin state <= s1; q <= 1'b0; end else begin state <= s0; q <= 1'b0; end end s1:.. s2:.. endcase 0 Cannot 'escape' from state '11' S1 S2 1 output S0 -> 0 S1 -> 0 S2 -> 1 1

Default (unsafe) implementation state flip-flop state flip-flop

Default (unsafe) implementation s0 s1 s2 0 1 1 1 00 10 11 0 0 0 01 1 Illegal state '01' will drive 's2' and 's0' output simultaneouly high (This may cause errors in output encoding block)

Safe Implementation Specified with tool option or Verilog code pragma

Safe Implementation next-state logic next-state logic state flip-flop state flip-flop

Safe Implementation f2 = (f1.f2 +!f1.f2 +!f1.!f2). (in1)

Safe Implementation f1 = (f1.f2 +!f1.f2 +!f1.!f2). (in.f2)

Safe Implementation f1 = (f1.f2 +!f1.f2 +!f1.!f2). (in.f2) f2 = (f1.f2 +!f1.f2 +!f1.!f2). (in1) f1. f2 will always be one of {00, 01, 11}. State 10 cannot appear (will be immediately converted into 00) Safe Implementations are used when proper, precisely defined operation is more important than cost (area, performance) Related concept: fault tolerance. Design of digital architectures that can withstand the presence of faults Usually implemented using redundancy (replicating functions in time or space).

RAM-based next-state logic In FPGA, on-chip synchronous BRAM can be used for next-state logic and state register Selected as a synthesis option; default is LUT-based FSM

Mixing Datapath and FSM Example - Greatest Common Divisor ld m n GCD done gcd GCD: while (m!= n) { if (m > n) m = m - n; else n = n - m; } output = m;

Mixing Datapath and FSM Example - Greatest Common Divisor ld m n GCD idle done!done run!ld ld ld, done current_state done gcd ld m n m n done gcd

Mixing Datapath and FSM Example - Greatest Common Divisor ld done next-state state ld, done state ld m n state equations m n done gcd

GCD I/O and State module gcd(q, done, m, n, ld, clk, rst); output [9:0] q; reg [9:0] q; output done; reg done; input [9:0] m, n; input ld; input clk; input rst; ld done ld, done next-state state state reg state; reg [9:0] reg_m, reg_n; reg reg_ld; parameter swait = 1'b0, srun = 1'b1; ld m n state equations m n done gcd

GCD Single-case statement design module gcd(q, done, m, n, ld, clk, rst);... always @(posedge clk) begin state <= swait; // default reg assignment (essential for control-related reg_ld <= ld; // state, optional for datapath-related state) done <= 1'b0; case (state) swait: begin if (reg_ld) state <= srun; else state <= swait; reg_m <= m; // read new input reg_n <= n; end srun: begin if (done) state <= swait; else state <= srun; reg_m <= (reg_m > reg_n)? reg_m - reg_n : reg_m; // GCD iteration reg_n <= (reg_m > reg_n)? reg_n : reg_n - reg_m; q <= reg_m; done <= (reg_m == reg_n); end endcase end

GCD Single-case statement design module gcd(q, done, m, n, ld, clk, rst);... always @(posedge clk) begin state <= swait; // default reg assignment (essential for control-related reg_ld <= ld; // state, optional for datapath-related state) done <= 1'b0; case (state) swait: begin if (reg_ld) state <= srun; else state <= swait; reg_m <= m; // read new input reg_n <= n; end Each state specifies state transitions + datapath operations srun: begin if (done) state <= swait; else state <= srun; reg_m <= (reg_m > reg_n)? reg_m - reg_n : reg_m; // GCD iteration reg_n <= (reg_m > reg_n)? reg_n : reg_n - reg_m; q <= reg_m; done <= (reg_m == reg_n); end endcase end

Synthesis Advanced HDL Synthesis Report Macro Statistics # Adders/Subtractors : 2 10-bit subtractor : 2 # Registers : 33 Flip-Flops : 33 # Comparators : 2 10-bit comparator equal : 1 10-bit comparator greater : 1

GCD Single-case statement design start GCD(34, 8) done GCD = 2

Summary Finite State Machines Verilog Mapping: one, two, three always blocks State Encoding User-defined or tool-defined State encoding techniques: one-hot, user-defined, gray Synthesis Issues Default state assignment Safe Implementations Next-state logic: RAM or LUT FSM-based control of Datapath (gcd)