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

Similar documents
CSE140L: Components and Design Techniques for Digital Systems Lab. Verilog HDL. Instructor: Mohsen Imani UC San Diego. Source: Eric Crabill, Xilinx

This Lecture. Some components (useful for the homework) Verilog HDL (will continue next lecture)

CSE140L: Components and Design Techniques for Digital Systems Lab. Verilog HDL. Instructor: Mohsen Imani UC San Diego. Source: Eric Crabill, Xilinx

Logic Circuits II ECE 2411 Thursday 4:45pm-7:20pm. Lecture 3

L5: Simple Sequential Circuits and Verilog

CSE140L: Components and Design Techniques for Digital Systems Lab

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

L5: Simple Sequential Circuits and Verilog

CSE140L: Components and Design

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

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

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

Modeling Synchronous Logic Circuits. Debdeep Mukhopadhyay IIT Madras

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

L5: Simple Sequential Circuits and Verilog

Modeling Sequential Circuits in Verilog

Sequential Circuit Design: Principle

L5: Simple Sequential Circuits and Verilog

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

Synthesizable Verilog

Sequential Circuit Design: Principle

Sequential Logic Design

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

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Department of Electrical Engineering and Computer Sciences

Quick Introduction to SystemVerilog: Sequental Logic

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

CSE 140L Final Exam. Prof. Tajana Simunic Rosing. Spring 2008

Lab 7 (All Sections) Prelab: Introduction to Verilog

Writing Circuit Descriptions 8

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

EECS150 - Digital Design Lecture 5 - Verilog Logic Synthesis

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

Verilog for Synthesis Ing. Pullini Antonio

EEL 4783: HDL in Digital System Design

EECS150 - Digital Design Lecture 20 - Finite State Machines Revisited

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

C A R L E T O N U N I V E R S I T Y. FINAL EXAMINATION April Duration: 3 Hours No. of Students: 108

Sequential Logic. Reminder: Lab #2 due Thursday Fall 2016 Lecture 4

5.14 Algorithmic State Machine (ASM) Charts

Lab 7 (Sections 300, 301 and 302) Prelab: Introduction to Verilog

Graduate Institute of Electronics Engineering, NTU Design of Datapath Controllers

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

EPC6055 Digital Integrated Circuits EXAM 1 Fall Semester 2013

Chapter 10. case studies in sequential logic design

General FSM design procedure

General FSM design procedure

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

General FSM design procedure

Sequential Logic. Use Explicit Port Declarations. Verilog Summary. Examples

The Verilog Language COMS W Prof. Stephen A. Edwards Fall 2002 Columbia University Department of Computer Science

ENSC E-123: HW D3: Counter Applications; Counter in Verilog

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

University of Toronto Faculty of Applied Science and Engineering Edward S. Rogers Sr. Department of Electrical and Computer Engineering

Course Topics - Outline

EECS 270 Verilog Reference: Sequential Logic

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

Chapter 5 Registers & Counters

Behavioral Modeling and Timing Constraints

Behavioral Modeling and Timing Constraints

CMPE 415 Verilog Case-Statement Based State Machines II

CSE 140L Final Exam. Prof. Tajana Simunic Rosing. Spring 2008

DIGITAL SYSTEM DESIGN

Verilog introduction. Embedded and Ambient Systems Lab

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

N-input EX-NOR gate. N-output inverter. N-input NOR gate

Verilog for High Performance

Digital Integrated Circuits

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

Digital Circuit Design and Language. Datapath Design. Chang, Ik Joon Kyunghee University

Date Performed: Marks Obtained: /10. Group Members (ID):. Experiment # 11. Introduction to Verilog II Sequential Circuits

CSCB58 - Lab 3. Prelab /3 Part I (in-lab) /2 Part II (in-lab) /2 TOTAL /8

Verilog Nonblocking Assignments with Delays - Myths & Mysteries

Computer Aided Design Basic Syntax Gate Level Modeling Behavioral Modeling. Verilog

ECEN 468 Advanced Logic Design

Lecture 15: System Modeling and Verilog

Logic Synthesis. EECS150 - Digital Design Lecture 6 - Synthesis

Verilog Coding Guideline

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

ENGR 3410: Lab #1 MIPS 32-bit Register File

Outline. EECS150 - Digital Design Lecture 5 - Verilog 2. Structural Model: 2-to1 mux. Structural Model - XOR. Verilog Basics Lots of Examples

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

Chap 6 Introduction to HDL (d)

Topics. Midterm Finish Chapter 7

Nonblocking Assignments in Verilog Synthesis; Coding Styles That Kill!

Timing and Verification

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

Department of Computer Science and Electrical Engineering. CMPE 415 Verilog Events Timing and Testbenches Prof. Ryan Robucci

Nanosistemų programavimo kalbos 5 paskaita. Sekvencinių schemų projektavimas

ECE 4514 Digital Design II. Spring Lecture 2: Hierarchical Design

In this lecture, we will go beyond the basic Verilog syntax and examine how flipflops and other clocked circuits are specified.

Digital Integrated Circuits

CMPE 415 Verilog Case-Statement Based State Machines II

EECS150 - Digital Design Lecture 10 Logic Synthesis

Synthesis of Combinational and Sequential Circuits with Verilog

Introduction to Digital Design with Verilog HDL

FSM and Efficient Synthesizable FSM Design using Verilog

Hardware description languages

Lecture 32: SystemVerilog

Sequential Logic Implementation. Mealy vs. Moore Machines. Specifying Outputs for a Mealy Machine. Specifying Outputs for a Moore Machine

Transcription:

CSE140L: Components and Design Techniques for Digital Systems Lab Verilog HDL Tajana Simunic Rosing Source: Eric Crabill, Xilinx 1

More complex behavioral model module life (n0, n1, n2, n3, n4, n5, n6, n7, self, out); input n0, n1, n2, n3, n4, n5, n6, n7, self; output out; reg out; reg [7:0] neighbors; reg [3:0] count; reg [3:0] i; assign neighbors = {n7, n6, n5, n4, n3, n2, n1, n0}; always @(neighbors or self) begin count = 0; for (i = 0; i < 8; i = i+1) count = count + neighbors[i]; out = (count == 3); out = out ((self == 1) & (count == 2)); end endmodule

Delay in Assignment Delayed assignment: Δt time units pass before the statement is executed and LHS assignment is made Intra-assignment delay: RHS is evaluated immediately but there is a delay of Δt before the result is placed in LHS If another procedure changes RHS signals during Δt, it does not affect the output Source: Peter Nyasulu 3

Delay Control Control the timing of assignments in procedural blocks by: Level triggered timing control. wait (!reset); wait (!reset) a = b; Simple delays. #10; #10 a = b; Edge triggered timing control. @(a or b); @(a or b) c = d; @(posedge clk); @(negedge clk) a = b;

Comparator example module Compare1 (Equal, Alarger, Blarger, A, B); input A, B; output Equal, Alarger, Blarger; assign #5 Equal = (A & B) (~A & ~B); assign #3 Alarger = (A & ~B); assign #3 Blarger = (~A & B); endmodule

Delay Control Generation of clock and resets in testbench: reg rst, clk; initial // this happens once at time zero begin rst = 1 b1; // starts off as asserted at time zero #100; // wait for 100 time units rst = 1 b0; // deassert the rst signal end always // this repeats forever begin clk = 1 b1; // starts off as high at time zero #25; // wait for half period clk = 1 b0; // clock goes low #25; // wait for half period end

System Tasks The $ sign denotes Verilog system tasks, there are a large number of these, most useful being: $display( The value of a is %b, a); Used in procedural blocks for text output. The %b is the value format (binary, in this case ) $finish; Used to finish the simulation. Use when your stimulus and response testing is done. $stop; Similar to $finish, but doesn t exit simulation.

Driving a simulation through a testbench module testbench (x, y); output t x, y; reg [1:0] cnt; 2-bit vector initial begin initial block executed cnt = 0; only once at start of simulation repeat (4) begin #10 cnt = cnt + 1; $display ("@ time=%d, x=%b, y=%b, cnt=%b", $time, x, y, cnt); end #10 $finish; print to a console end assign x = cnt[1]; assign y = cnt[0]; endmodule directive to stop simulation

Blocking and Non-Blocking Assignments Blocking assignments (X=A) completes the assignment before continuing on to next statement Non-blocking assignments (X<=A) completes in zero time and doesn t change the value of the target until a blocking point (delay/wait) is encountered Example: copy vs. swap always @(posedge CLK) begin A = B; B = A; end always @(posedge CLK) begin A <= B; B <= A; end 9

Blocking vs. nonblocking with delay 10

Blocking Delay Unintended Behavior 11

Assign Delay Correct Behavior 12

Nonblocking delay expected behavior 13 Source: http://www.sunburst-design.com/papers/cummingshdlcon1999_behavioraldelays_rev1_1.pdf

Blocking vs. Nonblocking Assignment Guidelines Guidelines: use nonblocking assignments for sequential logic use blocking assignments for combinational logic in always blocks when modeling both sequential and combinational logic within the same always block, use nonblocking assignments. do not mix blocking and nonblocking assignments in the same always block Multiple nonblocking assignments to the same variable the last assignment wins! 14

CSE140L: Components and Design Techniques for Digital Systems Lab Flip Flops Tajana Simunic Rosing Source: Eric Crabill, Xilinx 15

D Flip-Flop p D flip-flop Clk D Dl latch Dl latch Dm Qm Ds Qs Q D/Dm Cm Clk Cm master Cs Qs servant Q Qm/Ds Cs Qs Flip-flop: Bit storage that stores on clock edge, not level Master-slave design: 16

Comparison of latches and flip-flops p D Q CLK positive edge-triggered flip-flop D CLK Qedge D Q G CLK Qlatch transparent (level-sensitive) latch 17

FF Types 18

Reset (set state to 0) R Flip-flop p features synchronous: Dnew = R' Dold (when next clock edge arrives) asynchronous: doesn't wait for clock Preset or set (set state to 1) S (or sometimes P) synchronous: Dnew = Dold + S (when next clock edge arrives) asynchronous: doesn't wait for clock Both reset and preset (set and reset dominant) Dnew =R' Dold +S (set-dominant) Dnew = R' Dold + R'S (reset-dominant) Selective input capability (input enable or load) LD or EN multiplexor at input: Dnew = LD' Q + LD Dold load may or may not override reset/set (usually R/S have priority) Complementary outputs Q and Q' 19

Flip-flop pin Verilog Use always block's sensitivity list to wait for clock edge module dff (clk, d, q); input clk, d; output q; reg q; always @(posedge clk) q = d; endmodule 20

What does this code do? module unknwn (clk, d, q); input clk, d; output q; reg q; always @(clk) q = d; endmodule 21

More Flip-flops p Synchronous/asynchronous reset/set single thread that waits for the clock three parallel threads only one of which waits for the clock Synchronous module dff (clk, s, r, d, q); input clk, s, r, d; output q; reg q; always @(posedge clk) if (r) q = 1'b0; else if (s) q = 1'b1; else q = d; endmodule Asynchronous module dff (clk, s, r, d, q); input clk, s, r, d; output q; reg q; always @(posedge r) q = 1'b0; always @(posedge s) q = 1'b1; always @(posedge clk) q = d; endmodule 22

Non-Ideal Flip-Flop p Behavior D C S D latch Q C D S u 1 2 clk D setup time u R Q R Q 3 4 7 clk Q 5 6 D Can t change flip-flop input too close to clock edge Setup time: time that D must be stable before edge Else, stable value not present at internal latch Hold time: time that D must be held stable after edge hold time Setup time violation Leads to oscillation! 23 Else, new value doesn t have time to loop around and stabilize in internal latch

Timing: Definitions data clock D Q D Q D T su 1.8 ns T h 0.5 ns T su 18 1.8 ns T h 05 0.5 ns Clk T w 33 3.3 ns T w 33 3.3 ns Q Setup time T pd 36 3.6 ns 1.1 ns T pd 3.6 ns 1.1 ns minimum time before the clocking event by which the input must be stable (Tsu) Hold time: minimum time after the clocking event until which the input must remain stable (Th) Propagation delay Amount of time for value to propagate from input to output (Tpd) 24

Clock skew D-FF original state: IN = 0, Q0 = 1, Q1 = 1 due to skew, next state becomes: Q0 = 0, Q1 = 0, and not Q0 = 0, Q1 = 1 100 In Q0 Q1 CLK0 CLK1 CLK1 is a delayed version of CLK0 The problem correct behavior assumes next state of all storage elements determined by all storage elements at the same time this is difficult in high-performance systems because time for clock to arrive at flip-flop is comparable to delays through logic effect of skew on cascaded flip-flops: 25

Metastability clk ai D setup time violation ai Q synchronizer metastable state Violating setup/hold time can lead to a metastable state Metastable state: Any flip-flop state other than a stable 1 or 0 Eventually settles to one or other, but we don t know which h Fix: for internal circuits make sure to observe setup time; not possible for external inputs (e.g. button press) Partial solution Insert synchronizer flip-flop for asynchronous input flip-flop w very small setup/hold time, but doesn t completely prevent metastability a 26

Metastability Probability of flip-flop being metastable is low very low very very low incredibly low ai synchronizers One flip-flop p doesn t completely solve problem Add more synchronizer flip-flops to decrease the probability of metastability Can t solve completely just decrease the likelihood of failure 27

Metastability and asynchronous inputs Clocked synchronous circuits inputs, state, and outputs sampled or changed in relation to a common reference signal (called the clock) e.g., master/slave, edge-triggered Asynchronous circuits inputs, state, and outputs sampled or changed independently of a common reference signal (glitches/hazards a major concern) e.g., R-S latch Asynchronous inputs to synchronous circuits inputs can change at any time, will not meet setup/hold times dangerous, synchronous inputs are greatly preferred cannot be avoided (e.g., reset signal, memory wait, user input) 28

Synchronization failure logic 1 logic 0 logic 1 logic 0 small, but non-zero probability that the FF output will get stuck in an in-between state oscilloscope traces demonstrating synchronizer failure and eventual decay to steady state Occurs when FF input changes close to clock edge the FF may enter a metastable state neither a logic 0 nor 1 it may stay in this state an indefinite amount of time this is not likely in practice but has some probability 29

Dealing with synchronization failure Reduce the probability of failure: (1) slow down the system clock (2) use fastest possible logic technology in the synchronizer (3) cascade two synchronizers asynchronous input D Q D Q synchronized input Clk synchronous system (1) slow down the system clock: this gives the synchronizer more time to decay into a steady state; synchronizer failure becomes a big problem for very high speed systems (2) use fastest possible logic technology in the synchronizer this makes for a very sharp "peak" upon which to balance (3) cascade two synchronizers this effectively synchronizes twice (both would have to fail) 30

Handling asynchronous inputs Async Input Clocked Synchronous System D Q Q0 Async Input D Q Synchronizer D Q Q0 Clock Clock D Q Q1 D Q Q1 Clock Clock In Q0 Q1 CLK In is asynchronous and fans out to D0 and D1 one FF catches the signal, one does not inconsistent state may be reached! 31

Glitching Glitch: Temporary values on outputs that appear soon after input changes, before stable new output values Designer must determine whether glitching outputs may pose a problem If so, may consider adding flip-flops to outputs Delays output by one clock cycle, but may be OK 32

CSE140L: Components and Design Techniques for Digital Systems Lab Counters and FSMs Tajana Simunic Rosing Source: Eric Crabill, Xilinx 33

Mobius Counter in Verilog initial begin A = 1 b0; B = 1 b0; C = 1 b0; D = 1 b0; end always @(posedge clk) begin A <= ~D; B <= A; C <= B; D <= C; end 34

Light Game FSM Tug of War game 7 LEDs, 2 push buttons (L, R) RESET R LED (6) LED (5) LED (4) L L R L R R R LED (3) LED (2) LED (1) LED (0) L L 35

Light Game FSM Verilog module Light_Game (LEDS, LPB, RPB, CLK, RESET); input LPB ; input RPB ; combinational logic input CLK ; wire L, R; input RESET; assign L = ~left && LPB; output [6:0] LEDS ; assign R = ~right && RPB; assign LEDS = position; reg [6:0] position; reg left; reg right; sequential logic always @(posedge CLK) begin left <= LPB; right <= RPB; if (RESET) position <= 7'b0001000; else if ((position == 7'b0000001) (position == 7'b1000000)) ; else if (L) position <= position << 1; else if (R) position <= position >> 1; end 36 endmodule

Finite string pattern recognizer Output a 1 when 010 appears in a bit string, and stop when 100 appears 0 reset S1 [0] 0 S0 [0] 1...0...1 1 1 0 S4 [0] S2 1 S5...01 [0] [0]...10 0 1 0 S3...010 [1]...100 S6 [0] 1 0 or 1 37

Finite string gpattern recognizer in Verilog Verilog description including state assignment module string (clk, X, rst, Q0, Q1, Q2, Z); input clk, X, rst; output Q0, Q1, Q2, Z; parameter S0 = [0,0,0]; //reset state parameter S1 = [0,0,1]; //strings ending in...0 parameter S2 = [0,1,0]; //strings ending in...01 parameter S3 = [0,1,1]; //strings ending in...010 parameter S4 = [1,0,0]; //strings ending in...1 parameter S5 = [1,0,1]; //strings ending in...10 parameter S6 = [1,1,0]; //strings ending in...100 reg state[0:2]; assign Q0 = state[0]; assign Q1 = state[1]; assign Q2 = state[2]; assign Z = (state == S3); always @(posedge clk) begin if (rst) state = S0; else case (state) S0: if (X) state = S4 else state = S1; S1: if (X) state = S2 else state = S1; S2: if (X) state = S4 else state = S3; S3: if (X) state = S2 else state = S6; S4: if (X) state = S4 else state = S5; S5: if (X) state = S2 else state = S6; S6: state = S6; default: begin $display ( invalid state reached ); state = 3 bxxx; end endcase end endmodule 38

Complex counter A synchronous 3-bit counter has a mode control M when M = 0, the counter counts up in the binary sequence when M = 1, the counter advances through the Gray code sequence Binary: 000, 001, 010, 011, 100, 101, 110, 111 Gray: 000, 001, 011, 010, 110, 111, 101, 100 0 reset 0 0 0 0 0 0 0 S0 [000] S1 [001] S2 [010] S3 [011] S4 [100] S5 [101] S6 [110] 1 1 1 1 1 1 1 1 S7 [111] 39

Complex counter in Verilog module string (clk, M, rst, Z0, Z1, Z2); input clk, X, rst; output Z0, Z1, Z2; parameter S0 = [0,0,0]; parameter S1 = [0,0,1]; parameter S2 = [0,1,0];, parameter S3 = [0,1,1]; parameter S4 = [1,0,0]; parameter S5 = [1,0,1]; parameter S6 = [1,1,0]; parameter S7 = [1,1,1]; reg state[0:2]; assign Z0 = state[0]; assign Z1 = state[1]; assign Z2 = state[2]; always @(posedge clk) begin if rst state = S0; else case (state) S0: state = S1; S1: if (M) state = S3 else state = S2; S2: if (M) state = S6 else state = S3; S3: if (M) state = S2 else state = S4; S4: if (M) state = S0 else state = S5; S5: if (M) state = S4 else state = S6; S6: if (M) state = S7 else state = S7; S7: if (M) state = S5 else state = S0; endcase end endmodule 40

HDLs vs. PLs Program structure instantiation of multiple components of the same type specify interconnections ti between modules via schematic hierarchy of modules Assignment continuous assignment (logic always computes) propagation delay (computation takes time) timing of signals is important (when does computation have its effect) Data structures size explicitly spelled out - no dynamic structures no pointers Parallelism hardware is naturally parallel (must support multiple threads) assignments can occur in parallel (not just sequentially)