Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 1 Verilog for Digital Design Chapter 3: Sequential Logic Design.

Slides:



Advertisements
Similar presentations
Simulation executable (simv)
Advertisements

Verilog Overview. University of Jordan Computer Engineering Department CPE 439: Computer Design Lab.
ECE 551 Digital Design And Synthesis
Verilog Intro: Part 1.
Hardware Description Language (HDL)
Verilog. 2 Behavioral Description initial:  is executed once at the beginning. always:  is repeated until the end of simulation.
ECE 551 Digital System Design & Synthesis Lecture 08 The Synthesis Process Constraints and Design Rules High-Level Synthesis Options.
2/9/20031 ECE 551: Digital System Design & Synthesis Lecture Set 4 4.1: Verilog – Procedural Assignments &Scheduling Semantics 4.2: Verilog – More Behavioral.
Digital Design - Sequential Logic Design Chapter 3 - Sequential Logic Design.
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.
Circuits require memory to store intermediate data
Verilog - 1 Writing Hardware Programs in Abstract Verilog  Abstract Verilog is a language with special semantics  Allows fine-grained parallelism to.
Verilog Sequential Circuits Ibrahim Korpeoglu. Verilog can be used to describe storage elements and sequential circuits as well. So far continuous assignment.
Why Behavioral Wait statement Signal Timing Examples of Behavioral Descriptions –ROM.
ELEN 468 Lecture 161 ELEN 468 Advanced Logic Design Lecture 16 Synthesis of Language Construct II.
Basic Register Typically, we store multi-bit items
ENEE 408C Lab Capstone Project: Digital System Design Fall 2005 Sequential Circuit Design.
ELEN 468 Advanced Logic Design
ENGIN112 L25: State Reduction and Assignment October 31, 2003 ENGIN 112 Intro to Electrical and Computer Engineering Lecture 25 State Reduction and Assignment.
Today’s Lecture Process model –initial & always statements Assignments –Continuous & procedural assignments Timing Control System tasks.
Overview Logistics Last lecture Today HW5 due today
Advanced FPGA Based System Design Lecture-9 & 10 VHDL Sequential Code By: Dr Imtiaz Hussain 1.
Verilog for Digital Design
Sequential Logic in Verilog
ECE 2372 Modern Digital System Design
CS1Q Computer Systems Lecture 11 Simon Gay. Lecture 11CS1Q Computer Systems - Simon Gay2 The D FlipFlop A 1-bit register is called a D flipflop. When.
Sequential Logic Materials taken from: Digital Design and Computer Architecture by David and Sarah Harris & The Essentials of Computer Organization and.
ECE 551 Digital System Design & Synthesis Fall 2011 Midterm Exam Overview.
VHDL Introduction. V- VHSIC Very High Speed Integrated Circuit H- Hardware D- Description L- Language.
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 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
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.
1 CSE-308 Digital System Design (DSD) N-W.F.P. University of Engineering & Technology, Peshawar.
Introduction to State Machine
1 COMP541 Sequential Circuits Montek Singh Feb 1, 2012.
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,
1 Hardware description languages: introduction intellectual property (IP) introduction to VHDL and Verilog entities and architectural bodies behavioral,
Behavioral Modelling - 1. Verilog Behavioral Modelling Behavioral Models represent functionality of the digital hardware. It describes how the circuit.
1 Verilog Digital System Design Z. Navabi, 2006 Verilog Language Concepts.
ECE 274 Digital Logic RTL Design using Verilog Verilog for Digital Design Ch. 5.
Finite State Machine (FSM) Nattha Jindapetch December 2008.
VHDL Discussion Finite State Machines
Verilog A Hardware Description Language (HDL ) is a machine readable and human readable language for describing hardware. Verilog and VHDL are HDLs.
VHDL Discussion Finite State Machines IAY 0600 Digital Systems Design Alexander Sudnitson Tallinn University of Technology 1.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.
04/26/20031 ECE 551: Digital System Design & Synthesis Lecture Set : Introduction to VHDL 12.2: VHDL versus Verilog (Separate File)
Digital System Design using VHDL
03/31/031 ECE 551: Digital System Design & Synthesis Lecture Set 8 8.1: Miscellaneous Synthesis (In separate file) 8.2: Sequential Synthesis.
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:
Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 1 Verilog for Digital Design Chapter 5: RTL Design.
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.
Structural Description
Overview Logistics Last lecture Today HW5 due today
Hardware Description Languages: Verilog
‘if-else’ & ‘case’ Statements
Hardware Description Languages: Verilog
FSM MODELING MOORE FSM MELAY FSM. Introduction to DIGITAL CIRCUITS MODELING & VERIFICATION using VERILOG [Part-2]
Introduction to Sequential Circuits
332:437 Lecture 8 Verilog and Finite State Machines
Verilog for Digital Design
Dr. Tassadaq Hussain Introduction to Verilog – Part-3 Expressing Sequential Circuits and FSM.
The Verilog Hardware Description Language
EGR 2131 Unit 12 Synchronous Sequential Circuits
332:437 Lecture 8 Verilog and Finite State Machines
Presentation transcript:

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 1 Verilog for Digital Design Chapter 3: Sequential Logic Design

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 2 Register Behavior

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 3 Register Behavior Sequential circuits have storage Register: most common storage component –N-bit register stores N bits –Structure may consist of connected flip-flops I3I2I1I0 Q3Q2Q1Q0 reg(4) Rst I 2 I 3 Q2Q3Q1Q0 I 1 I 0 Clk 4-bit register D Q R D Q R D Q R D Q R Rst

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 4 Register Behavior Vectors Typically just describe register behaviorally –Declare output Q as reg variable to achieve storage Uses vector types –Collection of bits More convenient than declaring separate bits like I3, I2, I1, I0 –Vector's bits are numbered Options: [0:3], [1:4], etc. [3:0] –Most-significant bit is on left –Assign with binary constant (more on next slide) `timescale 1 ns/1 ns module Reg4(I, Q, Clk, Rst); input [3:0] I; output [3:0] Q; reg [3:0] Q; input Clk, Rst; Clk) begin if (Rst == 1 ) Q <= 4'b0000; else Q <= I; end endmodule vldd_ch3_Reg4.v I3I2I1I0 Q3Q2Q1Q0 reg(4) Rst I3I2I1I0 module Reg4(I3,I2,I1,I0,Q3,...); input I3, I2, I1, I0; module Reg4(I, Q,...); input [3:0] I; I: I[3]I[2]I[1]I[0]

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 5 Register Behavior Constants Binary constant –4'b0000 4: size, in number of bits 'b: binary base 0000: binary value Other constant bases possible –d: decimal base, o: octal base, h: hexadecimal base –12'hFA2 'h: hexadecimal base 12: 3 hex digits require 12 bits FA2: hex value –Size is always in bits, and optional 'hFA2 is OK –For decimal constant, size and 'd optional 8'd255 or just 255 In previous uses like “A <= 1;” 1 and 0 are actually decimal numbers. ‘b1 and ‘b0 would explicitly represent bits Underscores may be inserted into value for readability –12'b1111_1010_0010 –8_000_000 `timescale 1 ns/1 ns module Reg4(I, Q, Clk, Rst); input [3:0] I; output [3:0] Q; reg [3:0] Q; input Clk, Rst; Clk) begin if (Rst == 1 ) Q <= 4'b0000; else Q <= I; end endmodule vldd_ch3_Reg4.v I3I2I1I0 Q3Q2Q1Q0 reg(4) Rst

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 6 `timescale 1 ns/1 ns module Reg4(I, Q, Clk, Rst); input [3:0] I; output [3:0] Q; reg [3:0] Q; input Clk, Rst; Clk) begin if (Rst == 1 ) Q <= 4'b0000; else Q <= I; end endmodule Register Behavior Procedure's event control involves Clk input –Not the I input. Thus, synchronous –"posedge Clk" Event is not just any change on Clk, but specifically change from 0 to 1 (positive edge) negedge also possible Process has synchronous reset –Resets output Q only on rising edge of Clk Process writes output Q –Q declared as reg variable, thus stores value too vldd_ch3_Reg4.v I3I2I1I0 Q3Q2Q1Q0 reg(4) Rst

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 7 Register Behavior Testbench reg/wire declarations and module instantiation similar to previous testbenches Module uses two procedures –One generates 20 ns clock 0 for 10 ns, 1 for 10 ns Note: always procedure repeats –Other provides values for inputs Rst and I (i.e., vectors) initial procedure executes just once, does not repeat (more on next slide) vldd_ch3_Reg4TB.v `timescale 1 ns/1 ns module Testbench(); reg [3:0] I_s; reg Clk_s, Rst_s; wire [3:0] Q_s; Reg4 CompToTest(I_s, Q_s, Clk_s, Rst_s); // Clock Procedure always begin Clk_s <= 0; #10; Clk_s <= 1; #10; end // Note: Procedure repeats // Vector Procedure initial begin Rst_s <= 1; I_s <= Clk_s); #5 Rst_s <= 0; I_s <= Clk_s); #5 Rst_s <= 0; I_s <= Clk_s); #5 Rst_s <= 0; I_s <= 4'b1111; end endmodule

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 8 Register Behavior Testbench Variables/nets can be shared between procedures –Only one procedure should write to variable Variable can be read by many procedures Clock procedure writes to Clk_s Vector procedures reads Clk_s Event control Clk_s)" –May be prepended to statement to synchronize execution with event occurrence Statement may be just ";" as in example In previous examples, the “statement” was a sequential block (begin-end) –Test vectors thus don't include the clock's period hard coded Care taken to change input values away from clock edges vldd_ch3_Reg4TB.v `timescale 1 ns/1 ns module Testbench(); reg [3:0] I_s; reg Clk_s, Rst_s; wire [3:0] Q_s; Reg4 CompToTest(I_s, Q_s, Clk_s, Rst_s); // Clock Procedure always begin Clk_s <= 0; #10; Clk_s <= 1; #10; end // Note: Procedure repeats // Vector Procedure initial begin Rst_s <= 1; I_s <= Clk_s); #5 Rst_s <= 0; I_s <= Clk_s); #5 Rst_s <= 0; I_s <= Clk_s); #5 Rst_s <= 0; I_s <= 4'b1111; end endmodule

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 9 Register Behavior Testbench Simulation results –Note that Q_s updated only on rising clock edges –Note Q_s thus unknown until first clock edge Q_s is reset to “0000” on first clock edge vldd_ch3_Reg4TB.v... Clk) begin if (Rst == 1 ) Q <= 4'b0000; else Q <= I; end... vldd_ch3_Reg4.v... // Vector Procedure initial begin Rst_s <= 1; I_s <= Clk_s); #5 Rst_s <= 0; I_s <= Clk_s); #5 Rst_s <= 0; I_s <= Clk_s); #5 Rst_s <= 0; I_s <= 4'b1111; end I_s Q_s time (ns) Clk_s xxxx Rst_s 6080 Remember that Q_s is connected to Q, and I_s to I, in the testbench Initial value of a bit is the unknown value x

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 10 Common Pitfalls Using "always" instead of "initial" procedure –Causes repeated procedure execution Not including any delay control or event control in an always procedure –May cause infinite loop in the simulator Simulator executes those statements over and over, never executing statements of another procedure Simulation time can never advance –Symptom – Simulator appears to just hang, generating no waveforms // Vector Procedure always begin Rst_s <= 1; I_s <= Clk_s); #5 Rst_s <= 0; I_s <= 4'b1111; end // Vector Procedure always begin Rst_s <= 1; I_s <= 4'b0000; end time (ns) I_s Q_s Clk_s Rst_s 6080

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 11 Common Pitfalls Not initializing all module inputs –May cause undefined outputs –Or simulator may initialize to default value. Switching simulators may cause design to fail. –Tip: Immediately initialize all module inputs when first writing procedure // Vector Procedure always begin Rst_s <= 1; I_s <= Clk_s); #5 Rst_s <= 0; I_s <= 4'b1111; end

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 12 Common Pitfalls Forgetting to explicitly declare as a wire an indentifier used in a port connection –e.g., Q_s –Verilog implicitly declares identifier as a net of the default net type, typically a one-bit wire Intended as shortcut to save typing for large circuits May not give warning message during compilation Works fine if a one-bit wire was desired But may be mismatch – in this example, the wire should have been four bits, not one bit Unexpected simulation results –Always explicitly declare wires Best to avoid use of Verilog's implicit declaration shortcut `timescale 1 ns/1 ns module Testbench(); reg [3:0] I_s; reg Clk_s, Rst_s; wire [3:0] Q_s; Reg4 CompToTest(I_s, Q_s, Clk_s, Rst_s);...

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 13 Finite-State Machines (FSMs)—Sequential Behavior

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 14 Finite-State Machines (FSMs)—Sequential Behavior Finite-state machine (FSM) is a common model of sequential behavior –Example: If B=1, hold X=1 for 3 clock cycles Note: Transitions implicitly ANDed with rising clock edge –Implementation model has two parts: State register Combinational logic –HDL model will reflect those two parts Inputs: B; Outputs: X On2On1On3 Off X=1 X=0 B B Combinational logic State register State X B Clk FSM outputs FSM inputs FSM outputs StateNext

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 15 Finite-State Machines (FSMs)—Sequential Behavior Modules with Multiple Procedures and Shared Variables Code will be explained on following slides `timescale 1 ns/1 ns module LaserTimer(B, X, Clk, Rst); input B; output reg X; input Clk, Rst; parameter S_Off = 0, S_On1 = 1, S_On2 = 2, S_On3 = 3; reg [1:0] State, StateNext; // CombLogic B) begin case (State) S_Off: begin X <= 0; if (B == 0) StateNext <= S_Off; else StateNext <= S_On1; end... S_On1: begin X <= 1; StateNext <= S_On2; end S_On2: begin X <= 1; StateNext <= S_On3; end S_On3: begin X <= 1; StateNext <= S_Off; end endcase end // StateReg Clk) begin if (Rst == 1 ) State <= S_Off; else State <= StateNext; end endmodule vldd_ch3_LaserTimerBeh.v Inputs: B; Outputs: X On2On1On3 Off X=1 X=0 B B Combinational logic State register State X B Clk FSM inputs FSM outputs StateNext

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 16 Finite-State Machines (FSMs)—Sequential Behavior Modules has two procedures –One procedure for combinational logic –One procedure for state register –But it's still a behavioral description `timescale 1 ns/1 ns module LaserTimer(B, X, Clk, Rst); input B; output reg X; input Clk, Rst; parameter S_Off = 0, S_On1 = 1, S_On2 = 2, S_On3 = 3; reg [1:0] State, StateNext; // CombLogic B) begin... end // StateReg Clk) begin... end endmodule Combinational logic State register State X B Clk FSM inputs FSM outputs StateNext vldd_ch3_LaserTimerBeh.v

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 17 `timescale 1 ns/1 ns module LaserTimer(B, X, Clk, Rst); input B; output reg X; input Clk, Rst; parameter S_Off = 0, S_On1 = 1, S_On2 = 2, S_On3 = 3; reg [1:0] State, StateNext; // CombLogic B) begin... end // StateReg Clk) begin... end endmodule Finite-State Machines (FSMs)—Sequential Behavior Parameters parameter declaration –Not a variable or net, but rather a constant –A constant is a value that must be initialized, and that cannot be changed within the module’s definition –Four parameters defined S_Off, S_On1, S_On2, S_On3 Correspond to FSM’s states –Should be initialized to unique values vldd_ch3_LaserTimerBeh.v

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 18 Finite-State Machines (FSMs)—Sequential Behavior Module declares two reg variables –State, StateNext –Each is 2-bit vector (need two bits to represent four unique state values 0 to 3) –Variables are shared between CombLogic and StateReg procedures CombLogic procedure –Event control sensitive to State and input B –Will output StateNext and X StateReg procedure –Sensitive to Clk input –Will output State, which it stores Combinational logic State register State X B Clk FSM inputs FSM outputs StateNext `timescale 1 ns/1 ns module LaserTimer(B, X, Clk, Rst); input B; output reg X; input Clk, Rst; parameter S_Off = 0, S_On1 = 1, S_On2 = 2, S_On3 = 3; reg [1:0] State, StateNext; // CombLogic B) begin... end // StateReg Clk) begin... end endmodule vldd_ch3_LaserTimerBeh.v

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 19 Finite-State Machines (FSMs)—Sequential Behavior Procedures with Case Statements Procedure may use case statement –Preferred over if-else-if when just one expression determines which statement to execute –case (expression) Execute statement whose case item expression value matches case expression –case item expression : statement –statement is commonly a begin-end block, as in example –First case item expression that matches executes; remaining case items ignored –If no item matches, nothing executes –Last item may be "default : statement" Statement executes if none of the previous items matched // CombLogic B) begin case (State) S_Off: begin X <= 0; if (B == 0) StateNext <= S_Off; else StateNext <= S_On1; end S_On1: begin X <= 1; StateNext <= S_On2; end S_On2: begin X <= 1; StateNext <= S_On3; end S_On3: begin X <= 1; StateNext <= S_Off; end endcase end vldd_ch3_LaserTimerBeh.v

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 20 Finite-State Machines (FSMs)—Sequential Behavior Procedures with Case Statements FSM’s CombLogic procedure –Case statement describes states –case (State) Executes corresponding statement (often a begin-end block) based on State's current value –A state's statements consist of Actions of the state Setting of next state (transitions) Ex: State is S_On1 –Executes statements for state On1, jumps to endcase reg [1:0] State, StateNext; // CombLogic B) begin case (State) S_Off: begin X <= 0; if (B == 0) StateNext <= S_Off; else StateNext <= S_On1; end S_On1: begin X <= 1; StateNext <= S_On2; end S_On2: begin X <= 1; StateNext <= S_On3; end S_On3: begin X <= 1; StateNext <= S_Off; end endcase end Suppose State is S_On1 Inputs: X; Outputs: B On2On1On3 Off X=1 X=0 B' B vldd_ch3_LaserTimerBeh.v

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 21 Finite-State Machines (FSMs)—Sequential Behavior FSM StateReg Procedure –Similar to 4-bit register Register for State is 2-bit vector reg variable –Procedure has synchronous reset Resets State to FSM’s initial state, S_Off... parameter S_Off = 0, S_On1 = 1, S_On2 = 2, S_On3 = 3; reg [1:0] State, StateNext;... // StateReg Clk) begin if (Rst == 1 ) State <= S_Off; else State <= StateNext; end... vldd_ch3_LaserTimerBeh.v

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 22 Finite-State Machines (FSMs)—Sequential Behavior Modules with Multiple Procedures and Shared Variables Code should now be clear `timescale 1 ns/1 ns module LaserTimer(B, X, Clk, Rst); input B; output reg X; input Clk, Rst; parameter S_Off = 0, S_On1 = 1, S_On2 = 2, S_On3 = 3; reg [1:0] State, StateNext; // CombLogic B) begin case (State) S_Off: begin X <= 0; if (B == 0) StateNext <= S_Off; else StateNext <= S_On1; end... S_On1: begin X <= 1; StateNext <= S_On2; end S_On2: begin X <= 1; StateNext <= S_On3; end S_On3: begin X <= 1; StateNext <= S_Off; end endcase end // StateReg Clk) begin if (Rst == 1 ) State <= S_Off; else State <= StateNext; end endmodule vldd_ch3_LaserTimerBeh.v Inputs: B; Outputs: X On2On1On3 Off X=1 X=0 B B Combinational logic State register State X B Clk FSM inputs FSM outputs StateNext

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 23 Finite-State Machines (FSMs)—Sequential Behavior Self-Checking Testbenches FSM testbench –First part of file (variable/net declarations, module instantiations) similar to before –Vector Procedure Resets FSM Sets FSM's input values (“test vectors”) Waits for specific clock cycles –We observe the resulting waveforms to determine if FSM behaves correctly... // Clock Procedure always begin Clk_s <= 0; #10; Clk_s <= 1; #10; end // Note: Procedure repeats // Vector Procedure initial begin Rst_s <= 1; B_s <= Clk_s); #5 Rst_s <= Clk_s); #5 B_s <= Clk_s); #5 B_s <= Clk_s); end endmodule vldd_ch3_LaserTimerTB.v B_s X_s time (ns) Clk_s Rst_s

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 24 Finite-State Machines (FSMs)—Sequential Behavior Self-Checking Testbenches vldd_ch3_LaserTimerTBDisplay.v // Vector Procedure initial begin Rst_s <= 1; B_s <= Clk_s); #5 if (X_s != 0) $display("%t: Reset failed", $time); Rst_s <= Clk_s); #5 B_s <= Clk_s); #5 B_s <= 0; if (X_s != 1) $display("%t: First X=1 failed", Clk_s); #5 if (X_s != 1) $display("%t: Second X=1 failed", Clk_s); #5 if (X_s != 1) $display("%t: Third X=1 failed", Clk_s); #5 if (X_s != 0) $display("%t: Final X=0 failed", $time); end B_s X_s time (ns) Clk_s Rst_s Reading waveforms is error-prone Create self-checking testbench –Use if statements to check for expected values If a check fails, print error message Ex: if X_s fell to 0 one cycle too early, simulation might output: –95: Third X=1 failed

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 25 Finite-State Machines (FSMs)—Sequential Behavior $display System Procedure vldd_ch3_LaserTimerTBDisplay.v // Vector Procedure initial begin Rst_s <= 1; B_s <= Clk_s); #5 if (X_s != 0) $display("%t: Reset failed", $time); Rst_s <= Clk_s); #5 B_s <= Clk_s); #5 B_s <= 0; if (X_s != 1) $display("%t: First X=1 failed", Clk_s); #5 if (X_s != 1) $display("%t: Second X=1 failed", Clk_s); #5 if (X_s != 1) $display("%t: Third X=1 failed", Clk_s); #5 if (X_s != 0) $display("%t: Final X=0 failed", $time); end $display – built-in Verilog system procedure for printing information to display during simulation – A system procedure interacts with the simulator and/or host computer system To write to a display, read a file, get the current simulation time, etc. Starts with $ to distinguish from regular procedures String argument is printed literally... –$display("Hello") will print "Hello" –Automatically adds newline character...except when special sequences appear – %t: Display a time expression –Time expression must be next argument $time – Built-in system procedure that returns the current simulation time –95: Third X=1 failed

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 26 Top-Down Design – FSMs to Controller Structure

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 27 Top-Down Design – FSMs to Controller Structure Recall from Chapter 2 –Top-down design Capture behavior, and simulate Capture structure (circuit), simulate again Gets behavior right first, unfettered by complexity of creating structure Capture behavior: FSM Capture structure: Controller –Create architecture (state register and combinational logic) –Encode states –Create stable table (describes combinational logic) –Implement combinational logic Capture behavior Simulate W_s P_s S_s K_s Capture structure Simulate W_s P_s S_s K_s Should be the same Combinational logic State register S1S0 N1 N0 X B Clk FSM outputs FSM inputs FSM outputs X=1 X=0 B B' On2On1 Off On3 Inputs: B; Outputs: X LaserTimer example

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 28 Top-Down Design – FSMs to Controller Structure Recall from Chapter 2 –Top-down design Capture behavior, and simulate Capture structure (circuit), simulate again Gets behavior right first, unfettered by complexity of creating structure Capture behavior: FSM Capture structure: Controller –Create architecture (state register and combinational logic) –Encode states –Create stable table (describes combinational logic) –Implement combinational logic X=1 X=0 B B' On2On1 Off On3 Inputs: B; Outputs: X FSM outputs

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 29 Top-Down Design – FSMs to Controller Structure Recall from Chapter 2 –Top-down design Capture behavior, and simulate Capture structure (circuit), simulate again Gets behavior right first, unfettered by complexity of creating structure Capture behavior: FSM Capture structure: Controller –Create architecture (state register and combinational logic) –Encode states –Create stable table (describes combinational logic) –Implement combinational logic X=1 X=0 B B' On2On1 Off On3 Inputs: B; Outputs: X FSM outputs X = S1 + S0 N1 = S1’S0 + S1S0’ N0 = S1’S0’B + S1S0’

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 30 Controller Structure Structural description Test with LaserTimerTB –Same results FSM outputs X = S1 + S0 N1 = S1’S0 + S1S0’ N0 = S1’S0’B + S1S0’ `timescale 1 ns/1 ns module LaserTimer(B, X, Clk, Rst); input B; output reg X; input Clk, Rst; parameter S_Off = 2'b00; reg [1:0] State, StateNext; // State encodings: // S_Off 00, S_On1 01, S_On2 10, S_On3 11 // CombLogic B) begin X <= State[1] | State[0]; StateNext[1] <= (~State[1] & State[0]) | (State[1] & ~State[0]); StateNext[0] <= (~State[1] & ~State[0] & B) | (State[1] & ~State[0]); end // StateReg Clk) begin if (Rst == 1 ) State <= S_Off; else State <= StateNext; end endmodule Combinational logic State register S1S0 N1 N0 X B Clk FSM outputs FSM inputs FSM outputs vldd_ch3_LaserTimerStruct.v B_s X_s time (ns) Clk_s Rst_s

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 31 Controller Structure Initial state is S_Off –Encoded as "00" –State register set to S_Off during FSM reset Note that CombLogic uses equations, not case statement –Actually CombLogic is still behavioral –Do top-down design again, this time on CombLogic, to get structure FSM outputs vldd_ch3_LaserTimerStruct.v `timescale 1 ns/1 ns module LaserTimer(B, X, Clk, Rst); input B; output reg X; input Clk, Rst; parameter S_Off = 2'b00; reg [1:0] State, StateNext; // State encodings: // S_Off 00, S_On1 01, S_On2 10, S_On3 11 // CombLogic B) begin X <= State[1] | State[0]; StateNext[1] <= (~State[1] & State[0]) | (State[1] & ~State[0]); StateNext[0] <= (~State[1] & ~State[0] & B) | (State[1] & ~State[0]); end // StateReg Clk) begin if (Rst == 1 ) State <= S_Off; else State <= StateNext; end endmodule

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 32 Common Pitfall: Not Assigning Every Output in Every State FSM outputs should be combinational function of current state (for Moore FSM) Not assigning output in given state means previous value is remembered –Output has memory –Behavior is not an FSM Solution 1 –Be sure to assign every output in every state Solution 2 –Assign default values before case statement –Later assignment in state overwrites default // CombLogic B) begin X <= 0; case (State) S_Off: begin X <= 0; if (B == 0) StateNext <= S_Off; else StateNext <= S_On1; end S_On1: begin X <= 1; StateNext <= S_On2; end S_On2: begin X <= 1; StateNext <= S_On3; end S_On3: begin X <= 1; StateNext <= S_Off; end endcase end Could delete this without changing behavior (but probably clearer to keep it)

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 33 Common Pitfall: Not Assigning Every Output in Every State Solution 2 –Assign default values before case statement –Later assignment in state overwrites default –Helps clarify which actions are important in which state –Corresponds directly to the common simplifying FSM diagram notation of implicitly setting unassigned outputs to 0 TS A=0 B=1 C=0 A=0 B=0 C=1 TS B=1C=1 case State S: begin A <= 0; B <= 1; C <= 0; end T: begin A <= 0; B <= 0; C <= 1; end endcase A <= 0; B <= 0; C <= 0; case State S: begin B <= 1; end T: begin C <= 1; end endcase

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 34 More Simulation Concepts

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 35 The Simulation Cycle Instructive to consider how an HDL simulator works –HDL simulation is complex; we'll introduce simplified form Consider example SimEx1 –Three reg variables – Q, Clk, S –Three procedures – P1, P2, P3 Simulator's job: Determine values for nets and variables over time –Repeatedly executes and suspends procedures Note: Actually considers more objects, known collectively as processes, but we'll keep matters simple here to get just the basic idea of simulation –Maintains a simulation time Time `timescale 1 ns/1 ns module SimEx1(Q); output reg Q; reg Clk, S; // P1 always begin Clk <= 0; #10; Clk <= 1; #10; end // P2 begin Q <= ~S; end // P3 initial (posedge Clk); S <= (posedge Clk); S <= 0; end endmodule vldd_ch3_SimEx1.v

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 36 The Simulation Cycle Start of simulation –Simulation time Time is 0 –Bit variables/nets initialized to the unknown value x –Execute each procedure In any order, until stops at a delay or event control `timescale 1 ns/1 ns module SimEx1(Q); output reg Q; reg Clk, S; // P1 always begin Clk <= 0; #10; Clk <= 1; #10; end // P2 begin Q <= ~S; end // P3 initial (posedge Clk); S <= (posedge Clk); S <= 0; end endmodule vldd_ch3_SimEx1.v Q Clk S Variables x x x Start x 0 x 0 Time (ns): P1 P2 P3 Clk <= 0, then stop. Activate when Time is 0+10=10 ns. No actions, then stop. Activate when S changes. No actions, then stop. Activate when Clk changes to 1 Procedures We'll use arrow to show where a procedure stops

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 37 The Simulation Cycle Simulation cycle –Set time to next time at which a procedure activates (note: could be same as current time) In this case, time = 10 ns (P1 activates) –Execute active procedures (in any order) until stops vldd_ch3_SimEx1.v Q Clk S Variables x x x Start x 0 x 0 Time (ns): x 1 x 10 P1 P2 P3 Activate when Time is 10 ns. Activate when S changes. Activate when Clk changes to 1. Procedures Clk <= 1, stop, activate when Time=10+10=20 ns. `timescale 1 ns/1 ns module SimEx1(Q); output reg Q; reg Clk, S; // P1 always begin Clk <= 0; #10; Clk <= 1; #10; end // P2 begin Q <= ~S; end // P3 initial (posedge Clk); S <= (posedge Clk); S <= 0; end endmodule

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 38 The Simulation Cycle Simulation cycle –Set time to next time at which a procedure activates Still 10 ns; Clk just changed to 1 (P3 activates) –Execute active procedures (in any order) until stops vldd_ch3_SimEx1.v Q Clk S Variables x x x Start x 0 x 0 x 1 x 10 x 1 1 Time (ns): P1 P2 P3 Activate when Time is 20 ns. Activate when S changes. Activate when Clk changes to 1 Procedures S <= 1, stop, activate when Clk changes to 1 again `timescale 1 ns/1 ns module SimEx1(Q); output reg Q; reg Clk, S; // P1 always begin Clk <= 0; #10; Clk <= 1; #10; end // P2 begin Q <= ~S; end // P3 initial (posedge Clk); S <= (posedge Clk); S <= 0; end endmodule

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 39 The Simulation Cycle Simulation cycle –Set time to next time at which a procedure activates Still 10 ns; S just changed (P2 activates) –Execute active procedures until stops vldd_ch3_SimEx1.v Q Clk S Variables x x x Start x 0 x 0 x 1 x 10 x Time (ns): P1 P2 P3 Activate when Time is 20 ns. Activate when S changes. Activate when change on Clk to 1. Procedures Q <= 0 (~S), stop, activate when S changes. `timescale 1 ns/1 ns module SimEx1(Q); output reg Q; reg Clk, S; // P1 always begin Clk <= 0; #10; Clk <= 1; #10; end // P2 begin Q <= ~S; end // P3 initial (posedge Clk); S <= (posedge Clk); S <= 0; end endmodule

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 40 The Simulation Cycle Simulation cycle –Set time to next time at which a procedure activates In this case, set Time = 20 ns (P1 activates) –Execute active procedures until stops vldd_ch3_SimEx1.v Q Clk S Variables x x x Init x 0 x 0 x 1 x 10 x Time (ns): P1 P2 P3 Activate when Time is 20 ns. Activate when S changes. Activate when change on Clk to 1. Procedures Clk <= 0, stop, activate when T=20+10=30ns `timescale 1 ns/1 ns module SimEx1(Q); output reg Q; reg Clk, S; // P1 always begin Clk <= 0; #10; Clk <= 1; #10; end // P2 begin Q <= ~S; end // P3 initial (posedge Clk); S <= (posedge Clk); S <= 0; end endmodule

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 41 The Simulation Cycle Simulation ends when user-specified time is reached Variable/net values translate to waveforms vldd_ch3_SimEx1.v Q Clk S Variables x x x Init x 0 x 0 x 1 x 10 x Time (ns): Q Clk S Variables Time (ns) x x `timescale 1 ns/1 ns module SimEx1(Q); output reg Q; reg Clk, S; // P1 always begin Clk <= 0; #10; Clk <= 1; #10; end // P2 begin Q <= ~S; end // P3 initial (posedge Clk); S <= (posedge Clk); S <= 0; end endmodule

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 42 Variable Updates Assignment using "<=" ("non blocking assignment") doesn't change variable's value immediately –Instead, schedules a change of value by placing an event on an event queue –Scheduled changes occur at end of simulation cycle Important implications –Procedure execution order in a simulation cycle doesn't matter Assume procedures 1 and 2 are both active –Proc1 schedules B to be 1, but does not change the present value of B. B is still 0. –Proc2 schedules A to be 0 (the present value of B). –At end of simulation cycle, B is updated to 1 and A to 0 –Order of assignments to different variables in a procedure doesn't matter Assume C was 0. Scheduled values will be C=1 and D=0, for either Proc3a or Proc3b. –Later assignment in procedure effectively overwrites earlier assignment E will be updated with 0, but then by 1; so E is 1 at the end of the simulation cycle. Simulation cycle (revised) –Set time to next time at which a procedure resumes –Execute active procedures –Update variables with schedule values Assume B is 0. Proc1: B <= ~B; Proc2: A <= B; A will be 0, not 1. Proc3a: C <= ~C; D <= C; Proc3b: D <= C; C <= ~C; Same Proc4: E <= 0;... E <= 1; Recall FSM output assignment example, in which default assignments were added before the case statement.

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 43 Resets

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 44 Resets Reset – Behavior of a register when a reset input is asserted Good practice dictates having defined reset behavior for every register Reset behavior should always have priority over normal register behavior Reset behavior –Usually clears register to 0s –May initialize to other value e.g., state register of a controller may be initialized to encoding of initial state of FSM Reset usually asserted externally at start of sequential circuit operation, but also to restart due to failure, user request, or other reason `timescale 1 ns/1 ns module Reg4(I, Q, Clk, Rst); input [3:0] I; output [3:0] Q; reg [3:0] Q; input Clk, Rst; Clk) begin if (Rst == 1 ) Q <= 4'b0000; else Q <= I; end endmodule vldd_ch3_Reg4.v I3I2I1I0 Q3Q2Q1Q0 reg(4) Rst

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 45 Rst Clk Q I xxxx Synchronous Reset Previous examples used synchronous resets –Rst input only considered during rising clock `timescale 1 ns/1 ns module Reg4(I, Q, Clk, Rst); input [3:0] I; output [3:0] Q; reg [3:0] Q; input Clk, Rst; Clk) begin if (Rst == 1 ) Q <= 4'b0000; else Q <= I; end endmodule vldd_ch3_Reg4.v I3I2I1I0 Q3Q2Q1Q0 reg(4) Rst Rst=1 has no effect until rising clock

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 46 Rst Clk Q I xxxx Asynchronous Reset Can also use asynchronous reset –Rst input considered independently from clock Add "posedge Rst" to sensitivity list `timescale 1 ns/1 ns module Reg4(I, Q, Clk, Rst); input [3:0] I; output [3:0] Q; reg [3:0] Q; input Clk, Rst; Clk, posedge Rst) begin if (Rst == 1 ) Q <= 4'b0000; else Q <= I; end endmodule vldd_ch3_Reg4AsyRst.v I3I2I1I0 Q3Q2Q1Q0 reg(4) Rst Clk Q I Asynchronous reset Synchronous reset Rst=1 has almost immediate effect Rst=1 has no effect until next rising clock

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 47 Asynchronous Reset Could have used asynchronous reset for FSM state register too... // StateReg Clk, posedge Rst) begin if (Rst == 1 ) State <= S_Off; else State <= StateNext; end... vldd_ch3_LaserTimerBehAsyRst.v... // StateReg Clk) begin if (Rst == 1 ) State <= S_Off; else State <= StateNext; end... Synchronous Asynchronous

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 48 Synchronous versus Asynchronous Resets Which is better – synchronous or asynchronous reset? –Hotly debated in design community Each has pros and cons –e.g., asynchronous can still reset even if clock is not functioning, synchronous avoids timing analysis problems sometimes accompanying asynchronous designs We won’t try to settle the debate here –What’s important is to be consistent throughout a design All registers should have defined reset behavior that takes priority over normal register behavior That behavior should all be synchronous reset or all be asynchronous reset –We will use synchronous resets in all of our remaining examples

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 49 Describing Safe FSMs

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 50 Describing Safe FSMs Safe FSM – If enters illegal state, transitions to a legal state Example –Suppose example has only three states –Two-bit encoding has illegal state encoding "11" Also known as "unreachable" state Not possible to enter that state under normal FSM operation But actually possible to enter that state due to circuit error – e.g., electrical noise that causes state register bit to switch X=1 X=0 B B' On2On1 Off Inputs: B; Outputs: X

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 51 Describing Safe FSMs Safe FSM – If enters illegal state, transitions to a legal state Example –Suppose example has only three states –Two-bit encoding has illegal state encoding "11" –Safe implementation Transition to appropriate legal state Even though that undefined state appears to be unreachable Thus, FSM recovers from the error X=1 X=0 B B' On2On1 Off Inputs: B; Outputs: X X=0 11

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 52 Describing Safe FSMs in VHDL Unsafe FSM description –Only describes legal states, ignores illegal states Some synthesis tools support "safe" option during synthesis –Automatically creates safe FSM from an unsafe FSM description... reg [1:0] State, StateNext; B) begin case (State) S_Off: begin X <= 0; if (B == 0) StateNext <= S_Off; else StateNext <= S_On1; end S_On1: begin X <= 1; StateNext <= S_On2; end S_On2: begin X <= 1; StateNext <= S_Off; end endcase end...

Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 53 Describing Safe FSMs in VHDL Explicitly describing a safe FSM –Include case item(s) to describe illegal states –Can use "default" case item Executes if State equals anything other than S_Off, S_On1, or S_On2 Note: Use of default is wise regardless of number of states –Even if number is power of two, because state encoding may use more than minimum number of bits e.g., one-hot encoding has many more illegal states than legal states Note: If synthesis tool support "safe" option, use it –Otherwise, tool may automatically optimize away unreachable states to improve performance and size, but making state machine unsafe... reg [1:0] State, StateNext; B) begin case (State) S_Off: begin X <= 0; if (B == 0) StateNext <= S_Off; else StateNext <= S_On1; end S_On1: begin X <= 1; StateNext <= S_On2; end S_On2: begin X <= 1; StateNext <= S_Off; end default: begin X <= 0; StateNext <= S_Off; end endcase end... vldd_ch3_LaserTimerBehSafe.v