ECE 4514 Digital Design II. Spring Behavioral Modeling II: Conditionals and Loops

Similar documents
CAD for VLSI Design - I. Lecture 21 V. Kamakoti and Shankar Balachandran

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

EECS150 - Digital Design Lecture 10 Logic Synthesis

EECS150 - Digital Design Lecture 10 Logic Synthesis

Chap 6 Introduction to HDL (d)

Course Topics - Outline

a, b sum module add32 sum vector bus sum[31:0] sum[0] sum[31]. sum[7:0] sum sum overflow module add32_carry assign

ECEN 468 Advanced Digital System Design

Logic Synthesis. EECS150 - Digital Design Lecture 6 - Synthesis

Digital Design with FPGAs. By Neeraj Kulkarni

CSE140L: Components and Design Techniques for Digital Systems Lab

P-1/P-105. Samir Palnitkar. Prentice-Hall, Inc. INSTRUCTOR : CHING-LUNG SU.

Verilog Behavioral Modeling

CSE140L: Components and Design

EEL 4783: HDL in Digital System Design

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

ECE 4514 Digital Design II. Spring Lecture 7: Dataflow Modeling

Workshop on Digital Circuit Design in FPGA

Course Topics - Outline

Verilog. What is Verilog? VHDL vs. Verilog. Hardware description language: Two major languages. Many EDA tools support HDL-based design

Spiral 1 / Unit 4 Verilog HDL. Digital Circuit Design Steps. Digital Circuit Design OVERVIEW. Mark Redekopp. Description. Verification.

Veriolog Overview. CS/EE 3710 Fall 2010

Introduction to Digital Design with Verilog HDL

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

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

Digital System Design Verilog-Part III. Amir Masoud Gharehbaghi

In this lecture, we will focus on two very important digital building blocks: counters which can either count events or keep time information, and

ES611 FPGA Based System Design. Behavioral Model

Introduction to Verilog design. Design flow (from the book) Hierarchical Design. Lecture 2

Modeling Sequential Circuits in Verilog

ECEN 468 Advanced Logic Design

Introduction to Verilog design. Design flow (from the book)

CS6710 Tool Suite. Verilog is the Key Tool

Under-Graduate Project Logic Design with Behavioral Models

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

271/471 Verilog Tutorial

Synthesis of Combinational and Sequential Circuits with Verilog

Contents. Appendix D Verilog Summary Page 1 of 16

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

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

Verilog for Synthesis Ing. Pullini Antonio

Online Verilog Resources

ECEN 468 Advanced Digital System Design

Lab 7 (All Sections) Prelab: Introduction to Verilog

Department of Computer Science and Electrical Engineering. Intro to Verilog II

Introduction to Verilog and ModelSim. (Part 5 Sequential Logic)

Verilog introduction. Embedded and Ambient Systems Lab

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

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

EECS150 - Digital Design Lecture 4 - Verilog Introduction. Outline

Verilog Essentials Simulation & Synthesis

Lecture #2: Verilog HDL

CS6710 Tool Suite. Verilog is the Key Tool. Verilog as HDL (AHT) Verilog has a Split Personality. Quick Review. Synthesis

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

Why Should I Learn This Language? VLSI HDL. Verilog-2

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

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

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

Chapter 2 Using Hardware Description Language Verilog. Overview

EECS150 - Digital Design Lecture 5 - Verilog Logic Synthesis

Verilog Overview. The Verilog Hardware Description Language. Simulation of Digital Systems. Simulation of Digital Systems. Don Thomas, 1998, Page 1

EECS 427 Lecture 14: Verilog HDL Reading: Many handouts/references. EECS 427 W07 Lecture 14 1

Verilog Fundamentals. Shubham Singh. Junior Undergrad. Electrical Engineering

ECE 4514 Digital Design II. Spring Lecture 9: Review of Key Ideas, System Commands and Testbenches

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

Introduction. Why Use HDL? Simulation output. Explanation

- 1 of 18 - The Verilog Hardware Description Language - A Behavioural View Overview. The Behavioural Model

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

Abstraction. Levels of abstraction in a computer system

Hardware Description Languages. Veriolog Overview. CS/EE 3710 Fall Verilog. Verilog Origins. Quick Review (2001 syntax) Quick Review HDL

Introduction to Verilog

Verilog Overview. The Verilog Hardware Description Language. Simulation of Digital Systems. Simulation of Digital Systems. Don Thomas, 1998, Page 1

Digital Integrated Circuits

Verilog Overview. The Verilog Hardware Description Language. Simulation of Digital Systems. Simulation of Digital Systems. Don Thomas, 1998, Page 1

Homework deadline extended to next friday

Register Transfer Level in Verilog: Part I

ECE 545 Lecture 12. Datapath vs. Controller. Structure of a Typical Digital System Data Inputs. Required reading. Design of Controllers

Abstraction. ENGR Michael S. McCorquodale, Peter M. Chen

Verilog HDL. A Guide to Digital Design and Synthesis. Samir Palnitkar. SunSoft Press A Prentice Hall Title

Department of Computer Science and Electrical Engineering. CMPE 415 Synthesis. Prof. Ryan Robucci

Lecture 32: SystemVerilog

271/469 Verilog Tutorial

Digital Design (VIMIAA01) Introduction to the Verilog HDL

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

Introduction to Verilog and ModelSim. (Part 6 State Machines)

VERILOG. Deepjyoti Borah, Diwahar Jawahar

The Verilog Hardware Description Language Testing the Design Overview

A Verilog Primer. An Overview of Verilog for Digital Design and Simulation

2. compile it for simulation: iverilog foo.v. 3. simulate system: vvp a.out > testresult.txt. 4. see what happened: vi testresult.

HDL Introduction and Reuse

FSM and Efficient Synthesizable FSM Design using Verilog

The Verilog Hardware Description Language

Course Topics - Outline

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

Programmable Logic Devices Verilog VII CMPE 415

Midterm Exam Thursday, October 24, :00--2:15PM (75 minutes)

A Tutorial Introduction 1

structure syntax different levels of abstraction

Here is a list of lecture objectives. They are provided for you to reflect on what you are supposed to learn, rather than an introduction to this

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Department of Electrical Engineering and Computer Sciences

Transcription:

ECE 4514 Digital Design II Behavioral Modeling II: Conditionals and Loops A Language Lecture

Behavioral Modeling so far Procedural statements (lecture 2 and on) Always and Initial constructs Sequential block statements Procedural Assignments Blocking assignments (lecture 2 and on) Non-blocking assignments (lecture 10) Event control statements (lecture 5) @ wait #NNN

Today Conditional statements if-then-else case Loop statement Parallel block statements, named block statements Plus a few useful structural techniques Generate construct Palnitkar: 7.4-7.10 IEEE 1364: 9.4-9.8, 12.4

Today Conditional statements if-then-else case Loop statement Parallel block statements, named block statements Plus a few useful structural techniques Generate construct Palnitkar: 7.4-7.10 IEEE 1364: 9.4-9.8, 12.4

Conditional statement reg [7:0] a, b, max; always @(posedge clk) begin max = b; if (a > b) max = a; True if non-zero False if zero, X or Z always @(posedge clk) begin if (a > b) max = a; else max = b;

Conditional statement reg [7:0] a, b, max; always @(posedge clk) begin if (a > b) begin max = a;... other statement.. Use begin.. to include multiple statements

Nested if-then-else Else associates with the innermost if reg [7:0] a, b, c, max; always @(posedge clk) begin if (b > a) if (b > c) max = b; else max = c; else max = a; Note that indentation does not imply association. Verilog is context-free. How to change this association?

Nested if-then-else Use begin-- to change association reg [7:0] a, b, c, max; always @(posedge clk) begin if (b > a) begin if (b > c) max = b; else max = a;

Multiway decisions: if-else-if Frequently used as a case-decoding statement reg [7:0] a, b, c, max; always @(posedge clk) begin if (a < 10) begin //... when a < 10 else if (a < 20) begin //... when a >= 10 and a < 20 else if (a < 30) begin //.. when a >= 20 and a < 30 else begin //.. when a >= 30

Application: Next-state logic for FSM Design reg a; reg [1:0] state; parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10; always @(posedge clk) begin if (state == s0) if (a) state <= s1; else state <= s2; else if (state == s1) if (a) state <= s2; else state <= s1; else if (state == s2) state <= s2; else state <= s0; S0 ~a a S2 a S1 ~a

Application: Next-state logic for FSM Design reg a; reg [1:0] state; parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10; always @(posedge clk) begin if (state == s0) if (a) state <= s1; else state <= s2; else if (state == s1) if (a) state <= s2; else state <= s1; else if (state == s2) state <= s2; else state <= s0; S0 ~a a S2 a S1 ~a By matching up if-else conditions, you can be sure that the set of state transitions is logically complete.

Case statement reg a; reg [1:0] state; parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10; always @(posedge clk) case (state) s0: if (a) state <= s1; else state <= s2; s1: if (a) state <= s2; else state <= s1; s2: state <= s2; default state <= s0; case S0 ~a a S2 a S1 ~a

Case statement reg a; reg [1:0] state; parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10; always @(posedge clk) case (state) s0: if (a) state <= s1; else state <= s2; s1: if (a) state <= s2; else state <= s1; s2: state <= s2; default: state <= s0; case Two differences with if-then-else 1. if-then-else conditions can be more general. Each if-then-else leg can test a different expression 2. case statement performs exact matching*, including x and z If-then-else always returns false when matching x or z * cfr case-equality operator ===

Case statement reg a; reg [1:0] state; parameter s0 = 2'b00, s1 = 2'b01, s2 = 2'b10; always @(posedge clk) case (state) s0: if (a) state <= s1; else state <= s2; s1: if (a) state <= s2; else state <= s1; s2: state <= s2; default: state <= s0; case Default is optional. However, if no case label matches, then no entry of case will execute.

Another example reg a, b; always @(posedge clk) case ({a,b}) 2'b00: // will match a = 0, b = 0 2'bx0: // a = x, b = 0 2'b10: // a = 1, b = 1 2'b01, 2'b0x: // a = 0, b = 1 or x 2'b00: // a = 0, b = 0 default: // all other cases case Case labels are tested in sequence Do you see anything special?

Case statement with don't care casez allows to express don't care with 'z' or '?' reg [7:0] instr; always @(posedge clk) casez (instr) 7'b1???????: // arithmetic 7'b01??????: // load-reg 7'b00??????: // store-reg case Example instruction format 1 arithmetic 0 1 load-reg 0 0 store-reg OR: casez (instr) 7'b1zzzzzzz: // arithmetic 7'b01zzzzzz: // load-reg 7'b00zzzzzz: // store-reg case

Case statement with don't care casex allows to express don't care with 'x' or 'z' However, please use casez for all practical applications involving don't cares

Constant-case Search for a pattern '10' in a word, starting from the lsb reg [4:0] thebits; always @(posedge clk) case (2'b10) constant as a case selector thebits[1:0]: // it's on the lsb position thebits[2:1]: // it's on the lsb+1 position thebits[3:2]: // it's on the lsb+2 position case

Today Conditional statements if-then-else case Loop statement Parallel block statements, named block statements Plus a few useful structural techniques Generate construct Palnitkar: 7.4-7.10 IEEE 1364: 9.4-9.8, 12.4

Four Different loop statements forever: does what it says repeat: repeat a fixed number of times while: for: conditionally repeat a number of times initialize variable; test loop condition; run body; loop counter update

Forever Note that there is no break statement.. always @(posedge clk) forever // OMG! How do I get out? Can be terminated by creating a named block and a disable statement

Repeat Sample expression at start. If non-zero, do this N number of times. If zero, exit immediately module clock_divider(clk, clkdiv); input clk; output clkdiv; reg clkdiv; initial clkdiv <= 0; always begin repeat(5) @(posedge clk) ; clkdiv <= ~clkdiv; module

While module countones(num, in); input [7:0] in; output [7:0] num; reg [7:0] num; reg [7:0] wrk; initial begin num = 0; wrk = in; while (wrk) begin if (wrk[0]) num = num + 1; wrk = wrk >> 1; module

For module countones(num, in); input [7:0] in; output [7:0] num; reg [7:0] num, a; reg [7:0] wrk; initial begin num = 0; wrk = in; for (a=0; a < 8; a = a + 1) if (wrk[a]) num = num + 1; module

Procedural Timing Controls @, wait, #: we discussed these in Lecture 5. What follows are some further details Timing of a procedure is affected with event controls or with delay controls @ is an event control # is a delay control wait is the combination of a while loop and an event control

Event control posedge (P) and negedge (N) From To 0 1 X Z 0 P P P 1 N N N X Z N N P P Events are combined with or or, @(posedge a or b or c) @(a, posedge b, negedge c)

Implicit Event Expression List Automatically select RHS of all expressions in a block always @(a, b, c, e) begin b = a + 1; c = b / 2; d = b + c + a - e; always @* same as @(a, b, c, e) begin b = a + 1; c = b / 2; d = b + c + a - e;

Implicit event lists are useful What RHS are included here? always @* begin tmp1 = a & b; tmp2 = c & d; always @* begin @(n) a = b; always @* begin y = 8'hff; y[a] =!en;

Implicit event lists are useful What RHS are included here? always @* begin tmp1 = a & b; tmp2 = c & d; always @* begin @(n) a = b; always @* begin y = 8'hff; y[a] =!en; @(a, b, c, d) @(b) @(a, en)

Implicit event lists are useful What RHS are included here? assume CAPITAL variables are constants always @* begin next = 4'b0; case (1'b1) state[idle]: if (go) next[read] = 1'b1; else next[idle] = 1'b1; state[read]: next[dly ] = 1'b1; state[dly ]: if (!ws) next[done] = 1'b1; else next[read] = 1'b1; state[done]: next[idle] = 1'b1; case

Implicit event lists are useful What RHS are included here? assume CAPITAL variables are constants @(go, ws, state) always @* begin next = 4'b0; case (1'b1) state[idle]: if (go) next[read] = 1'b1; else next[idle] = 1'b1; state[read]: next[dly ] = 1'b1; state[dly ]: if (!ws) next[done] = 1'b1; else next[read] = 1'b1; state[done]: next[idle] = 1'b1; case

Today Conditional statements if-then-else case Loop statement Parallel block statements, named block statements Plus a few useful structural techniques Generate construct Palnitkar: 7.4-7.10 IEEE 1364: 9.4-9.8, 12.4

Parallel Block and Sequential Block Sequential Block: begin.. initial begin #50 clk = 0; #30 clk = 1; rst = 0; #50 clk = 0; #50 rst = 1; @ 50 @ 80 @ 130 clk = 0 clk = 1 rst = 0 clk = 0 @ 180 rst = 1

Parallel Block and Sequential Block Parallel Block: fork.. join initial fork #50 clk = 0; #80 clk = 1; #80 rst = 0; #130 clk = 0; #180 rst = 1; join @ 50 @ 80 @ 130 clk = 0 clk = 1 rst = 0 clk = 0 @ 180 rst = 1

Parallel Block and Sequential Block Mixed parallel/sequential initial fork #50 clk = 0; #80 begin clk = 1; rst = 0; #130 clk = 0; #180 rst = 1; join @ 50 @ 80 @ 130 clk = 0 clk = 1 rst = 0 clk = 0 @ 180 rst = 1

Parallel Block and Sequential Block Can have a different start/stop behavior A B initial begin @(A) $display("a"); @(B) $display("b"); initial fork @(A) $display("a"); @(B) $display("b"); join Will these two behave differently for the given stimuli?

Named Block Give a parallel or sequential block a name initial fork : stimuli #50 clk = 0; #80 begin clk = 1; rst = 0; #130 clk = 0; #180 rst = 1; join Named blocks can be disabled (terminated) initial begin : runthis forever begin #10 a = a + 1; if (a > 30) disable runthis;

Today Conditional statements if-then-else, nested-if, multiway decisions case Loop statement Parallel block statements, named block statements Plus a few useful structural techniques Generate construct Palnitkar: 7.4-7.10 IEEE 1364: 9.4-9.8, 12.4

Generate Whenever you instantiate modules, you interconnect and wire them statically. The behavior of the modules is also static. Parameters are useful but simple, e.g. simple expansion of bus interconnection, creation of gate arrays, and so on. The Generate statement provides more flexibility for design creation. It enables to expand your design using for loops (generate) using if-then-else (generate-conditional) using case (generate-case).

Example Assume you have 2 types of adders, A and B. For wordlengths smaller than 12 bit, adder type A is quicker. For wordlengths above 12 bit, adder type B is quicker. Can we create a single adder module that will choose the right module for us? module #(parameter width = 10) myadder; begin output [0:width-1] q; input [0:width-1] a, b; if (width > 12) instantiate code for adder B else instantiate code for adder A This is meta code. It does not make part of simulation model. But it does help to create the simulation model

Example: Generate Pipeline length data_in data_out clk rst width module #(parameter width = 8, length = 8) pipeline(data_out, data_in, clk, rst); output [0:width-1] data_out; input [0:width-1] data_in; input clk, rst; reg [0:width-1] pipestage [0:length-1]; wire [0:width-1] d_in [0:length-1]; // generate_pipe goes here.. module

Example: Generate Pipeline module #(parameter width = 8, length = 8) pipeline(data_out, data_in, clk, rst); output [0:width-1] data_out; input [0:width-1] data_in; input clk, rst; reg [0:width-1] pipestage [0:length-1]; wire [0:width-1] d_in [0:length-1]; assign d_in[0] = data_in; assign data_out = pipestage[length-1]; generate genvar k; for (k=0; k<=length-1; k = k + 1) begin : W if (k > 0) assign d_in[k] = pipestage[k-1]; always @(posedge clk or negedge rst) if (~rst) pipestage[k] = 0; else pipestage[k] = d_in[k]; generate module

Example: Generate Pipeline module #(parameter width = 8, length = 8) pipeline(data_out, data_in, clk, rst); output [0:width-1] data_out; input [0:width-1] data_in; input clk, rst; reg [0:width-1] pipestage [0:length-1]; wire [0:width-1] d_in [0:length-1]; assign d_in[0] = data_in; assign data_out = pipestage[length-1]; generate genvar k; for (k=0; k<=length-1; k = k + 1) begin : W generate loop generate conditional generate module metacode if (k > 0) assign d_in[k] = pipestage[k-1]; always @(posedge clk or negedge rst) if (~rst) pipestage[k] = 0; else pipestage[k] = d_in[k]; expanded code

Generate code and real code Meta Code: generate for (init; condition; incr) begin : name // real code generate generate if (condition) // real code generate always blocks initial blocks assign statements modules primitives generate case (N) 1: // real code case generate

Generate loop creates hierarchical names generate genvar k; for (k=0; k<=l-1; k = k + 1) begin : thegate xor mygate(a[k], b[k], c[k]) generate this generates L gates First one is called thegate[0].mygate Second one is called thegate[1].mygate and so on

Summary Behavioral modeling can express fairly sophisticated constructs if-then-else, nested-if, multiway-if case, casex, casez, constant-case forever, repeat, while, for implicit event lists parallel and sequential blocks, named blocks Many (but not all) of these constructs are synthesizable and create hardware Worthwhile to learn how they work Verilog-2001 enables generate - parametric generation of code when a module is instantiated