1 Lecture 3: Modeling Sequential Logic in Verilog HDL.

Slides:



Advertisements
Similar presentations
FSM and Efficient Synthesizable FSM Design using Verilog
Advertisements

//HDL Example 8-2 // //RTL description of design example (Fig.8-9) module Example_RTL (S,CLK,Clr,E,F,A);
Counters Discussion D8.3.
Synchronous Sequential Logic
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.
Verilog. 2 Behavioral Description initial:  is executed once at the beginning. always:  is repeated until the end of simulation.
1 COMP541 Sequential Circuits Montek Singh Sep 17, 2014.
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.
OUTLINE Introduction Basics of the Verilog Language Gate-level modeling Data-flow modeling Behavioral modeling Task and function.
FSMs 1 Sequential logic implementation  Sequential circuits  primitive sequential elements  combinational logic  Models for representing sequential.
Verilog Sequential Circuits Ibrahim Korpeoglu. Verilog can be used to describe storage elements and sequential circuits as well. So far continuous assignment.
ENEE 408C Lab Capstone Project: Digital System Design Fall 2005 Sequential Circuit Design.
ELEN 468 Advanced Logic Design
VII - Finite State Machines © Copyright 2004, Gaetano Borriello and Randy H. Katz 1 Finite State Machines Sequential circuits  primitive sequential elements.
Advanced Verilog EECS 270 v10/23/06.
ECE 331 – Digital Systems Design Introduction to Sequential Logic Circuits (aka. Finite State Machines) and FSM Analysis (Lecture #19)
Verilog Intro: Part 2. Procedural Blocks There are two types of procedural blocks in Verilog. – initial for single-pass behavior : initial blocks execute.
Today’s Lecture Process model –initial & always statements Assignments –Continuous & procedural assignments Timing Control System tasks.
Lecture 17 General finite state machine (FSM) design
Overview Logistics Last lecture Today HW5 due today
Sequential Logic in Verilog
VII - Finite State Machines © Copyright 2004, Gaetano Borriello and Randy H. Katz 1 Finite State Machines (FSM) Sequential circuits  primitive sequential.
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.
1 CSE370, Lecture 19 Lecture 19 u Logistics n Lab 8 this week to be done in pairs íFind a partner before your lab period íOtherwise you will have to wait.
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.
1 CSE-308 Digital System Design (DSD) N-W.F.P. University of Engineering & Technology, Peshawar.
1 COMP541 Sequential Circuits Montek Singh Feb 1, 2012.
Verilog for Synthesis Ing. Pullini Antonio
Anurag Dwivedi. Basic Block - Gates Gates -> Flip Flops.
Lab 7 : Overview State Machine : Moore Model Concept in designing a Vending Machine.
3/4/20031 ECE 551: Digital System Design * & Synthesis Lecture Set 3 3.1: Verilog - User-Defined Primitives (UDPs) (In separate file) 3.2: Verilog – Operators,
Behavioral Modelling - 1. Verilog Behavioral Modelling Behavioral Models represent functionality of the digital hardware. It describes how the circuit.
Finite State Machine (FSM) Nattha Jindapetch December 2008.
Verilog A Hardware Description Language (HDL ) is a machine readable and human readable language for describing hardware. Verilog and VHDL are HDLs.
M.Mohajjel. Structured Procedures Two basic structured procedure statements always initial All behavioral statements appear only inside these blocks Each.
Introduction to ASIC flow and Verilog HDL
M.Mohajjel. Objectives Learn How to write synthesizable Verilog code Common mistakes and how to avoid them What is synthesized for what we code Digital.
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.
OUTLINE Introduction Basics of the Verilog Language Gate-level modeling Data-flow modeling Behavioral modeling Task and function.
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU 99-1 Under-Graduate Project Design of Datapath Controllers Speaker: Shao-Wei Feng Adviser:
Spring 2009W. Rhett DavisNC State UniversityECE 406Slide 1 ECE 406 – Design of Complex Digital Systems Lecture 9: State Machines & Reset Behavior Spring.
SYEN 3330 Digital SystemsJung H. Kim Chapter SYEN 3330 Digital Systems Chapters 4 – Part4: Verilog – Part 2.
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 #
Overview Logistics Last lecture Today HW5 due today
Verilog Tutorial Fall
Supplement on Verilog FF circuit examples
Figure 8.1. The general form of a sequential circuit.
Chapter #6: Sequential Logic Design
© Copyright 2004, Gaetano Borriello and Randy H. Katz
HDL for Sequential Circuits
Supplement on Verilog Sequential circuit examples: FSM
CSE 370 – Winter Sequential Logic-2 - 1
FSM MODELING MOORE FSM MELAY FSM. Introduction to DIGITAL CIRCUITS MODELING & VERIFICATION using VERILOG [Part-2]
332:437 Lecture 8 Verilog and Finite State Machines
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
Lecture 22 Logistics Last lecture Today HW7 is due on Friday
332:437 Lecture 8 Verilog and Finite State Machines
Lecture 22 Logistics Last lecture Today HW7 is due on Friday
Presentation transcript:

1 Lecture 3: Modeling Sequential Logic in Verilog HDL

2 Blocking(=) vs Nonblocking (<=) Assignment (b) begin a = b; c = a; end clk) begin a <= b; c <= a; end final value: a = b =c final value: a = b c = old a

3 Use non-blocking for flip flop inference posedge/negedge require nonblocking (posedge clock) begin b <= a; //swap a and b a <= b; end If using blocking for flipflop inference, we may not get what you want. clock) begin b = a; a = b; // b=a, a=a end

4 Procedural assignments Blocking assignment = Regular assignment inside procedural block Assignment takes place immediately LHS must be a register Nonblocking assignment <= Compute RHS Assignment takes place at end of block LHS must be a register

5 Execution order at Q1 (in any order) Evaluate RHS of all non-blocking assignments Evaluate RHS and change LHS of all blocking assignments (execution of all blocking assignments is in order) Evaluate RHS and change LHS of all continuous assignments Evaluate inputs and change outputs of all primitives at Q2 Change LHS of all non-blocking assignments at the same time All non-blocking assignments are evaluated using the values the variables have when your design entered the always block.

6 Non-blocking assignment is also known as an RTL assignment if used in an always block triggered by a clock edge all flip- flops change together clk) begin B = A; C = B; D = C; end //what does this circuit do? B=A, C=A, D=A clk) begin B <= A; C <= B; D <= C; end //What does this circuit do? a shift register

7 Finite State Machine Review A finite state machine has a state register that holds current state some combinational logic that calculates the next state and outputs based on the current state and/or the current inputs A finite state machine is a feedback system which updates on each clock.

8 Moore machine: output based on current state state feedback inputs outputsreg combinational logic for next state logic for outputs Mealy machine: based on current state and inputs inputsoutputs state feedback reg combinational logic for next state logic for outputs

9 Comparison of Mealy and Moore machines Mealy machines tend to have less states –different outputs on arcs (n 2 ) rather than states (n) Moore machines are safer to use –outputs change at clock edge (always one cycle later) –in Mealy machines, input change can cause output change as soon as logic is done – a big problem when two machines are interconnected – asynchronous feedback may occur if one isn’t careful Mealy machines react faster to inputs –react in same cycle – don't need to wait for clock –in Moore machines, more logic may be necessary to decode state into outputs – more gate delays after clock edge

10 Finite State Machine Design Determine inputs/output Determine machine types (Mealy vs. Moore) Determine machine flow (state diagram) State assignment Implementation

11 one-hot encoding State Q 3 Q 2 Q 1 Q 0 A B C D Output Z 2 Z 1 Z 0 A Moore Machine Using D Flip Flops Using D flip flops next state logic D 0 = Q 1 x’+ Q 3 x + Q 0 x D 1 = Q 2 x’ D 2 = Q 0 x’+ Q 1 x D 3 = Q 2 x + Q 3 x’ input x Output Logic Z 2 =Q 2 ; Z 1 =Q 2 ’ ; Z 0 =Q 2 +Q 0 Write a complete structural Verilog model of this simple Moore machine.

12 module DFF(q, d, clk, reset, preset); input d, clk, reset, preset; output q; reg q; clk or negedge reset or negedge preset) begin if (~reset) q <= 0; else if (~preset) q <= 1; else q <= d; end endmodule

13 module clock (clk); output clk; reg clk; initial #1 clk=0; always begin #10 clk = ~clk; end endmodule

14 module OneHotNS(D, Q, x); input x; input [3:0] Q; output [3:0]D; reg[3:0]D; or x) begin D[3] = (Q[2] & x) | (Q[3] & ~x); D[2] =(Q[0] & ~x) | (Q[1] & x); D[1]= Q[2] & ~x; D[0] = (Q[1] & ~x) | (Q[0] & x) | (Q[3] & x); end endmodule

15 module OneHotOutput(Z, Q); input [3:0] Q; output [2:0] Z; reg [2:0] Z; (Q) begin Z[2] = Q[2]; Z[1] = ~Q[2]; Z[0] = Q[2] | Q[0]; end endmodule

16 module OneHotFSM(Z, x, clk, reset); input x, clk, reset; output [2:0] Z; wire [3:0] D, Q; DFF OneHotD3(Q[3], D[3], clk, reset, 1’b1), OneHotD2(Q[2], D[2], clk, reset, 1’b1), OneHotD1(Q[1], D[1], clk, reset, 1’b1), OneHotD0(Q[0], D[0], clk, 1’b1, reset); OneHotNSNextStateUnit(D, Q, x); OneHotOutputOutputUnit(Z, Q); endmodule

17 module testbench (Z, x, clk, reset); input [2:0] Z; inputclk; outputx, reset; regx, reset; reg[11:0]data; integer i; initial begin $monitor ($time,, “Z = %b, x = %b, clock= %b”, Z, x, clk); reset = 0; data = 12’b ; // x = (from L to R) #1 reset = 1; for (i = 0; i <= 11; i = i + 1) begin x = data[i]; #20; end #1 $finish; end endmodule

18 module system wireclk, reset, x; wire [2:0] Z; testbench testunit (Z, x, clk, reset); clock clockunit (clk); OneHotFSMFSMunit(Z, x, clk, reset); endmodule

19 0 Z = 011, x = x, clock= x 1 Z = 011, x = 1, clock= 0 10 Z = 011, x = 1, clock= 1 20 Z = 011, x = 1, clock= 0 30 Z = 011, x = 1, clock= 1 40 Z = 011, x = 1, clock= 0 41 Z = 011, x = 0, clock= 0 50 Z = 101, x = 0, clock= 1 60 Z = 101, x = 0, clock= 0 61 Z = 101, x = 1, clock= 0 70 Z = 010, x = 1, clock= 1 80 Z = 010, x = 1, clock= 0 81 Z = 010, x = 0, clock= 0 90 Z = 010, x = 0, clock= Z = 010, x = 0, clock= Z = 010, x = 0, clock= Z = 010, x = 0, clock= 0

Z = 010, x = 1, clock= Z = 011, x = 1, clock= Z = 011, x = 1, clock= Z = 011, x = 0, clock= Z = 101, x = 0, clock= Z = 101, x = 0, clock= Z = 010, x = 0, clock= Z = 010, x = 0, clock= Z = 010, x = 1, clock= Z = 101, x = 1, clock= Z = 101, x = 1, clock= Z = 101, x = 0, clock= Z = 010, x = 0, clock= Z = 010, x = 0, clock= Z = 011, x = 0, clock= Z = 011, x = 0, clock= 0

21 module sfsm (clk, reset, in, out); input clk, reset, in; output out; parameter zero = 2’b00; parameter one1 = 2’b01; parameter two1s = 2’b10; reg out; reg [2:1] state;// state variables reg [2:1] next_state; clk) if (reset) state <= zero; else state <= next_state; Another finite state machine given the state transition diagram zero [0] one1 [0] two1s [1]

22 or state) // generate Next State case (state) zero: begin //last input was a zero if (in) next_state = one1; else next_state = zero; end one1: begin // we've seen one 1 if (in) next_state = two1s; else next_state = zero; end two1s:// we've seen at least 2 ones begin if (in) next_state = two1s; else next_state = zero; end endcase zero [0] one1 [0] two1s [1] case (state) zero: out = 0; one1: out = 0; two1s: out = 1; endcase endmodule

23 An always block for a combinational function. A module can have more than one of these always blocks. Partition a design into several modules Tools optimize each module separately Combining several modules into one module, tools optimize this single module. case (state) zero: out = 0; one1: out = 0; two1s: out = 1; endcase

24 A Mealy Machine Binary encoding state A = 00; state B = 01; state C=10; state D = 11

25 module mealyFSM (out, x, clk, reset); input x, clk, reset; output [1:0] out; reg [1:0] out, curstate, nextstate; parameter [1:0] A=0, B=1, C=2, D=3; // state transition clk or negedge reset) begin $display(“ ***********************”); if (~reset) begin curstate <= 0; $display (“state 0 “); end else begin curstate <= nextstate; $display (“state %d’, nextstate); end

26 // next state logic and output logic combinational or x) begin case (curstate) A: begin out = 3; nextstate = D; end B: begin out = ( x ==1)? 2 : 0; nextstate = ( x == 1) ? A : D; end C: begin out = (x == 1)? 1 : 0; nextstate = (x == 1) ? B : D; end D: begin out = ( x == 1) ? 1 : 0; nextstate = ( x== 1) ? C : D; end endcase end endmodule

27 module clock(clk); output clk; reg clk; initial clk = 0; always #10 clk = ~clk; endmodule

28 module testbench (out, x, clk, reset); output x, reset; input [1:0] out; input clk; reg x, reset; reg[11:0] data; integer I; initial begin $monitor ($time,, “clk = %b, x = %b, out = %d, clk, x, out); reset = 0; // set state to A data = 12’b ; #1 reset = 1; for ( i = 11; i >= 0; i = i -1) begin x = data[i]; #20; end $finish; end endmodule

29 module system; wire [1:0] out; wire x, clk, reset; mealyFSMM1(out, x, clk, reset); clocktimer(clk); testbenchtest(out, x, clk, reset); endmodule

30 state 0 0 clk = 0, x = x, out = 3 1 clk = 0, x = 1, out = 3 *********************** state 3 10 clk = 1, x = 1, out = 1 20 clk = 0, x = 1, out = 1 21 clk = 0, x = 0, out = 0 *********************** state 3 30 clk = 1, x = 0, out = 0 40 clk = 0, x = 0, out = 0 *********************** state 3 50 clk = 1, x = 0, out = 0 60 clk = 0, x = 0, out = 0 61 clk = 0, x = 1, out = 1 *********************** state 2 70 clk = 1, x = 1, out = 1 80 clk = 0, x = 1, out = 1 81 clk = 0, x = 0, out = 0 *********************** copied from Silos output window

31 state 3 90 clk = 1, x = 0, out = clk = 0, x = 0, out = clk = 0, x = 1, out = 1 *********************** state clk = 1, x = 1, out = clk = 0, x = 1, out = 1 *********************** state clk = 1, x = 1, out = clk = 0, x = 1, out = clk = 0, x = 0, out = 0 *********************** state clk = 1, x = 0, out = clk = 0, x = 0, out = clk = 0, x = 1, out = 1 *********************** state clk = 1, x = 1, out = clk = 0, x = 1, out = 1 *********************** state clk = 1, x = 1, out = clk = 0, x = 1, out = 2 *********************** state clk = 1, x = 1, out = clk = 0, x = 1, out = 3 *********************** state clk = 1, x = 1, out = clk = 0, x = 1, out = 1