Lecture 5. Verilog HDL #1 Prof. Taeweon Suh Computer Science & Engineering Korea University COSE221, COMP211 Logic Design.

Slides:



Advertisements
Similar presentations
Verilog Overview. University of Jordan Computer Engineering Department CPE 439: Computer Design Lab.
Advertisements

Chapter 11 Verilog HDL Application-Specific Integrated Circuits Michael John Sebastian Smith Addison Wesley, 1997.
Verilog Intro: Part 1.
Combinational Logic with Verilog Materials taken from: Digital Design and Computer Architecture by David and Sarah Harris & The Essentials of Computer.
16/04/20151 Hardware Descriptive Languages these notes are taken from Mano’s book It can represent: Truth Table Boolean Expression Diagrams of gates and.
CSE 201 Computer Logic Design * * * * * * * Verilog Modeling
 HDLs – Verilog and Very High Speed Integrated Circuit (VHSIC) HDL  „ Widely used in logic design  „ Describe hardware  „ Document logic functions.
Copyright © 2007 Elsevier4- Chapter 4 :: Hardware Description Languages Digital Design and Computer Architecture David Money Harris and Sarah L. Harris.
ECE 353 Computer Systems Lab I Verilog Hardware Description Language.
Hardware Description Language HDL. 2 Hardware Description Language HDL  Describes circuits and systems in text. −As a software program.  Can be processed.
Computer Organization Lecture Set – 03 Introduction to Verilog Huei-Yung Lin.
University of Jordan Computer Engineering Department CPE 439: Computer Design Lab.
Digital System Design EEE344 Lecture 3 Introduction to Verilog HDL Prepared by: Engr. Qazi Zia, Assistant Professor EED, COMSATS Attock1.
Lecture 7 Verilog Additional references Structural constructs
Computer Architecture and Design – ECEN 350
COMP541 Combinational Logic - I
Verilog Basics Nattha Jindapetch November Agenda Logic design review Verilog HDL basics LABs.
Topics We are going to discuss the following topics for roughly 3 weeks from today Introduction to Hardware Description Language (HDL) Combinational Logic.
Programmable Logic Architecture Verilog HDL FPGA Design Jason Tseng Week 5.
Introduction to FPGA AVI SINGH. Prerequisites Digital Circuit Design - Logic Gates, FlipFlops, Counters, Mux-Demux Familiarity with a procedural programming.
ECE 2372 Modern Digital System Design
Lecture 4. Verilog HDL 1 (Combinational Logic Design)
1 COMP541 Combinational Logic - I Montek Singh Jan 11, 2012.
Lecture 6. Verilog HDL – Sequential Logic
Verilog Language Concepts
CS 3850 Lecture 3 The Verilog Language. 3.1 Lexical Conventions The lexical conventions are close to the programming language C++. Comments are designated.
1 COMP541 Verilog Primer Montek Singh Aug 28, 2015 (draft version  to be updated)
EEE2243 Digital System Design Chapter 3: Verilog HDL (Combinational) by Muhazam Mustapha, January 2011.
Module 1.2 Introduction to Verilog
COE 202 Introduction to Verilog Computer Engineering Department College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals.
Anurag Dwivedi. Basic Block - Gates Gates -> Flip Flops.
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
1 Hardware description languages: introduction intellectual property (IP) introduction to VHDL and Verilog entities and architectural bodies behavioral,
1 Verilog Digital System Design Z. Navabi, 2006 Verilog Language Concepts.
1 COMP541 Hierarchical Design & Verilog Montek Singh Aug 29, 2014.
Verilog A Hardware Description Language (HDL ) is a machine readable and human readable language for describing hardware. Verilog and VHDL are HDLs.
CEC 220 Digital Circuit Design Introduction to VHDL Wed, February 25 CEC 220 Digital Circuit Design Slide 1 of 19.
1 Arithmetic, ALUs Lecture 9 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ELEE 4303 Digital II Introduction to Verilog. ELEE 4303 Digital II Learning Objectives Get familiar with background of HDLs Basic concepts of Verilog.
CSCE 211: Digital Logic Design Chin-Tser Huang University of South Carolina.
Introduction to ASIC flow and Verilog HDL
Chapter 4 Computer System Architectures Chapter 4 Based on Digital Design and Computer Architecture, 2 nd Edition David Money Harris and Sarah L. Harris.
Introduction to Verilog
COE 202 Introduction to Verilog Computer Engineering Department College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals.
Verilog Intro: Part 1. Hardware Description Languages A Hardware Description Language (HDL) is a language used to describe a digital system, for example,
CEC 220 Digital Circuit Design Introduction to VHDL Wed, Oct 14 CEC 220 Digital Circuit Design Slide 1 of 19.
1 University of Jordan Computer Engineering Department CPE 439: Computer Design Lab.
Lecture 5. Verilog HDL #3 Prof. Taeweon Suh Computer Science & Engineering Korea University COSE221, COMP211 Logic Design.
1 Lecture 1: Verilog HDL Introduction. 2 What is Verilog HDL? Verilog Hardware Description Language(HDL)? –A high-level computer language can model, represent.
Exp#5 & 6 Introduction to Verilog COE203 Digital Logic Laboratory Dr. Ahmad Almulhem KFUPM Spring 2009.
Introduction to Verilog COE 202 Digital Logic Design Dr. Muhamed Mudawar King Fahd University of Petroleum and Minerals.
ECE 111 (Spring 2017) Professor Bill Lin
Hardware Description Languages: Verilog
Chapter 4 Digital Design and Computer Architecture: ARM® Edition
COMP541 More on Verilog; Debouncing switches
Discussion 2: More to discuss
Chapter 4 Digital Design and Computer Architecture, 2nd Edition
COMP211 Computer Logic Design
Chapter 4 Digital Design and Computer Architecture, 2nd Edition
Hardware Description Languages: Verilog
ECE 448 Lecture 3 Combinational-Circuit Building Blocks Data Flow Modeling of Combinational Logic ECE 448 – FPGA and ASIC Design with VHDL.
Behavioral Modeling in Verilog
Introduction to Verilog
COMP541 State Machines Montek Singh Feb 4, 2010.
Introduction to Digital System and Microprocessor Design
Chapter 4 Digital Design and Computer Architecture: ARM® Edition
COE 202 Introduction to Verilog
Lecture 7: Verilog Part II
Presentation transcript:

Lecture 5. Verilog HDL #1 Prof. Taeweon Suh Computer Science & Engineering Korea University COSE221, COMP211 Logic Design

Korea Univ Topics We are going to discuss the following topics for roughly 3 weeks from today  Introduction to Hardware Description Language (HDL)  Combinational Logic Design with HDL  Synchronous Sequential Logic Design with HDL Finite State Machine (FSM) Design  Testbenches 2

Korea Univ Introduction In old days (~ early 1990s), hardware engineers used to draw schematic of digital logic (combinational and sequential logics), based on Boolean equations But, it is not virtually possible to draw schematic as the hardware complexity increases As the hardware complexity increases, there has been a necessity of designing hardware in a more efficient way 3

Korea Univ Examples 4 Core i7  Number of transistors in Core i7 is roughly 1 billion  Assuming that the gate count is based on 2-input NAND gate, (which is composed of 4 transistors), do you want to draw 250 million gates by hand? Absolutely NOT! Midterm Exam  Even a simple FSM design problem in the midterm exam took you more than 30 minutes  Even worse, many of you got your answer wrong in the exam!

Korea Univ Introduction Hardware description language (HDL)  Allows hardware designers to specify logic function using language So, hardware designer only needs to specify the target functionality (such as Boolean equations and FSM) with language  Then, a computer-aided design (CAD) tool produces the optimized digital circuit with logic gates Nowadays, most commercial designs are built using HDLs 5 module example( input a, b, c, output y); assign y = ~a & ~b & ~c | a & ~b & ~c | a & ~b & c; endmodule HDL-based Design CAD Tool Optimized Gates

Korea Univ HDLs Two leading HDLs  VHDL Developed in 1981 by the Department of Defense Became an IEEE standard (1076) in 1987  Verilog-HDL Developed in 1984 by Gateway Design Automation Became an IEEE standard (1364) in 1995 We are going to use Verilog-HDL in this class  The book on the right is a good reference (but not required to purchase) 6 IEEE: Institute of Electrical and Electronics Engineers is a professional society responsible for many computing standards including WiFi (802.11), Ethernet (802.3) etc

Korea Univ Hardware Design with HDL 3 steps to design hardware with HDL 1. Hardware design with HDL Describe target hardware with HDL  When describing circuits using an HDL, it’s critical to think of the digital logic the code would produce 2.Simulation Validate the design  Inputs are applied to the design  Outputs checked for correctness  Millions of dollars saved by debugging in simulation instead of hardware 3.Synthesis Transforms HDL code into a netlist, describing the hardware  Netlist is a text file describing a list of logic gates and the wires connecting them 7

Korea Univ CAD tools for Simulation 8 There are renowned CAD companies that provide HDL simulators  Cadence  Synopsys  Mentor Graphics We are going to use ModelSim Altera Starter Edition for simulation index.htmlhttp:// index.html

Korea Univ CAD tools for Synthesis The same companies (Cadence, Synopsys, and Mentor Graphics) provide synthesis tools, too  They are extremely expensive to purchase though We are going to use a synthesis tool from Altera  Altera Quartus-II Web Edition (free) Synthesis, place & route, and download to FPGA 9

Korea Univ Verilog Modules Verilog Modul e  A block of hardware with inputs and outputs Examples: AND gate, multiplexer, priority encoder etc  A Verilog module begins with the module name and a list of the inputs and outputs  assign statement is used to describe combinational logic  ~ indicates NOT  & indicates AND  | indicates OR 10 module example(input a, b, c, output y); assign y = ~a & ~b & ~c | a & ~b & ~c | a & ~b & c; endmodule

Korea Univ Synthesis Transforms HDL code into a netlist, that is, collection of gates and their connections 11 module example(input a, b, c, output y); assign y = ~a & ~b & ~c | a & ~b & ~c | a & ~b & c; endmodule

Korea Univ Digital Design w/ Verilog HDL Combinational Logic  Continuous assignment statement It is used to describe simple combinational logic assign  always statement It is used to describe complex combinational logic Synchronous Sequential Logic  FSM is composed of flip-flops and combinational logics  Flip-flops are described with always statement clk) clk) 12

Korea Univ Verilog Syntax Verilog is case sensitive.  So, reset and Reset are NOT the same signal. Verilog does not allow you to start signal or module names with numbers  For example, 2mux is NOT a valid name Verilog ignores whitespace such as spaces, tabs and line breaks  Proper indentation and use of blank lines are helpful to make your design readable Comments come in single-line and multi-line varieties like C- language  // : single line comment  /* */ : multiline comment 13

Korea Univ Continuous Assignment Statement Statements with assign keyword  Examples: assign y = ~(a & b); // NAND gate assign y = a ^ b; // XOR gate It is used to describe combinational logic Anytime the inputs on the right side of the “=“ changes in a statement, the output on the left side is recomputed assign statement should not be used inside the always statement 14

Korea Univ Bitwise Operators Bitwise operators perform a bit-wise operation on two operands  Take each bit in one operand and perform the operation with the corresponding bit in the other operand 15 module gates(input [3:0] a, b, output [3:0] y1, y2, y3, y4, y5); /* Five different two-input logic gates acting on 4 bit busses */ assign y1 = a & b; // AND assign y2 = a | b; // OR assign y3 = a ^ b; // XOR assign y4 = ~(a & b); // NAND assign y5 = ~(a | b); // NOR endmodule

Korea Univ Wait! What is Bus? 16 Bus is a medium that transfers data between computer components In hardware design, a collection of bits is called bus  Example: A[3:0]: 4-bit bus (composed of A[3], A[2], A[1], A[0]) A[5:0]: 6-bit bus CPU North Bridge South Bridge A[31:0] Address Bus D[63:0] Data Bus Main Memory

Korea Univ Bus Representation Why uses a[3:0] to represent a 4-bit bus?  How about a[0:3]?  How about a[1:4] or a[4:1]? In digital world, we always count from 0  So, it would be nice to start the bus count from 0  If you use a[0:3], a[0] indicates MSB a[3] indicates LSB  If you use a[3:0], a[3] indicates MSB a[0] indicates LSB We are going to follow this convention in this course 17

Korea Univ Reduction Operators Reduction operations are unary  Unary operation involves only one operand, whereas binary operation involves two operands They perform a bit-wise operation on a single operand to produce a single bit result As you might expect, | (or), & (and), ^ (xor), ~& (nand), ~| (nor), and ~^ (xnor) reduction operators are available 18 module and8(input [7:0] a, output y); assign y = &a; // &a is much easier to write than // assign y = a[7] & a[6] & a[5] & a[4] & // a[3] & a[2] & a[1] & a[0]; endmodule

Korea Univ Examples 19 & 4’b1001 = & 4’bx111 = ~& 4’b1001 = ~& 4’bx001 = | 4’b1001 = ~| 4’bx001 = ^ 4’b1001 = ~^ 4’b1101 = ^ 4’b10x1 = 0 x x

Korea Univ Conditional Assignment The conditional operator ? : chooses between a second and third expression, based on a first expression  The first expression is the condition If the condition is 1, the operator chooses the second expression If the condition is 0, the operator chooses the third expression  Therefore, it is a ternary operator because it takes 3 inputs  It looks the same as the C-language and Java, right? 20 module mux2(input [3:0] d0, d1, input s, output [3:0] y); assign y = s ? d1 : d0; // if s is 1, y = d1 // if s is 0, y = d0 endmodule What kind of hardware do you think this would generate?

Korea Univ Internal Variables It is often convenient to break a complex design into intermediate designs The keyword wire is used to represent internal variable whose value is defined by an assign statement For example, in the schematic below, you can declare p and g as wires 21

Korea Univ Internal Variables Example 22 module fulladder(input a, b, cin, output s, cout); wire p, g; // internal nodes assign p = a ^ b; assign g = a & b; assign s = p ^ cin; assign cout = g | (p & cin); endmodule

Korea Univ Logical & Arithmetic Shifts 23 Logical shift Arithmetic shift

Korea Univ Logical & Arithmetic Shifts Logical shift ( > )  Every bit in the operand is simply moved by a given number of bit positions, and the vacant bit-positions are filled in with zeros Arithmetic shift ( >> )  Like logical shift, every bit in the operand is moved by a given number of bit positions  Instead of being filled with all 0s, when shifting to the right, the leftmost bit (usually the sign bit in signed integer representations) is replicated to fill in all the vacant positions This is sign extension  Arithmetic shifts can be useful as efficient ways of performing multiplication or division of signed integers by powers of two a <<< 2 is equivalent to a x 4 ? a >>> 2 is equivalent to a/4 ?  Take floor value if the result is not an integer. The floor value of X (or  X  ) is the greatest integer number less than or equal to X  Examples:  5/2  = 2,  -3/2  = -2 24

Korea Univ Operator Precedence The operator precedence for Verilog is much like you would expect in other programming languages  In particular, AND has precedence over OR  You may use parentheses if the operation order is not clear 25 ~ NOT *, /, %mult, div, mod +, -add,sub >logical shift >>arithmetic shift, >=comparison ==, !=equal, not equal &, ~&AND, NAND ^, ~^XOR, XNOR |, ~|OR, XOR ?: ternary operator Highest Lowest

Korea Univ Number Representation In Verilog, you can specify base and size of numbers  Format: N’Bvalue N : size (number of bits) B : base (b: binary, d: decimal, o: octal, h: hexadecimal) When writing a number, specify both base and size 26 Number# BitsBase Decimal Equivalent Stored 3’b1013binary5101 8’b118binary ’b1010_10118binary ’d63decimal6110 6’o426octal ’hAB8hexadecimal

Korea Univ Replication Operator Replication operator is used to replicate a group of bits  For instance, if you have a 1-bit variable and you want to replicate it 3 times to get a 3-bit variable, you can use the replication operator 27 wire [2:0] y; assign y = {3{b[0]}}; // the above statement produces: // y = b[0] b[0] b[0]

Korea Univ Concatenation Operator Concatenation operator {, } combines (concatenates) the bits of 2 or more operands 28 wire [11:0] y; assign y = {a[2:1], {3{b[0]}}, a[0], 6’b100_010}; // the above statement produces: // y = a[2] a[1] b[0] b[0] b[0] a[0] // underscores (_) are used for formatting only to make it easier to read. Verilog ignores them.

Korea Univ Tri-state Buffer 29 An implementation of tri-state buffer What happens to Y if E is 0? Output ( Y ) is effectively floating ( Z ) A Y E

Korea Univ Usage of Tri-state Buffer It is used to implement bus  Only one device should drive the bus  What happens if 2 devices drive the bus simultaneously? For example: Video drives the bus to 1, and Timer drives to 0  The result is x (unknown), indicating contention 30 CPU VideoEthernetTimer Shared bus

Korea Univ Tristate buffer and Floating output (Z) 31 Synthesis: Verilog: module tristate(input [3:0] a, input en, output [3:0] y); assign y = en ? a : 4'bz; endmodule

Korea Univ Verilog Module Description Two general styles of describing module functionality  Behavioral modeling Express the module’s functionality descriptively  Structural modeling Describe the module’s functionality from combination of simpler modules 32

Korea Univ Behavioral Modeling Example Behavioral modeling  Express the module’s functionality descriptively 33 module example(input a, b, c, output y); assign y = ~a & ~b & ~c | a & ~b & ~c | a & ~b & c; endmodule

Korea Univ Structural Modeling Example 34 Structural modeling  Describe the module’s functionality from combination of simpler modules module myinv(input a, output y); assign y = ~a ; endmodule module myand3(input a, b, c, output y); assign y = a & b & c; endmodule module myor3(input a, b, c, output y); assign y = a | b | c; endmodule // Behavioral model module example(input a, b, c, output y); assign y = ~a & ~b & ~c | a & ~b & ~c | a & ~b & c; endmodule module example_structure (input a, b, c, output y); wire inv_a, inv_b, inv_c; wire and3_0, and3_1, and3_2; myinvinva (.a (a),.y (inv_a)); myinvinvb (.a (b),.y (inv_b)); myinvinvc (.a (c),.y (inv_c)); myand3 and3_y0 (.a (inv_a),.b (inv_b),.c (inv_c),.y (and3_0)); myand3 and3_y1 (.a (a),.b (inv_b),.c (inv_c),.y (and3_1)); myand3 and3_y2 (.a (a),.b (inv_b),.c (c),.y (and3_2)); myor3 or3_y (.a (and3_0),.b (and3_1),.c (and3_2),.y (y)); endmodule

Korea Univ Simulation 35 module example(input a, b, c, output y); assign y = ~a & ~b & ~c | a & ~b & ~c | a & ~b & c; endmodule

Korea Univ Delays timescale directive is used to indicate the value of time unit  The statement is of the form `timescale unit/precision Example: `timescale 1ns/1ps means that time unit is 1ns and simulation has 1ps precision In Verilog, a # symbol is used to indicate the number of time units of delay 36 `timescale 1ns/1ps module example(input a, b, c, output y); wire ab, bb, cb, n1, n2, n3; assign #1 {ab, bb, cb} = ~{a, b, c}; assign #2 n1 = ab & bb & cb; assign #2 n2 = a & bb & cb; assign #2 n3 = a & bb & c; assign #4 y = n1 | n2 | n3; endmodule

Korea Univ Delays 37 module example(input a, b, c, output y); wire ab, bb, cb, n1, n2, n3; assign #1 {ab, bb, cb} = ~{a, b, c}; assign #2 n1 = ab & bb & cb; assign #2 n2 = a & bb & cb; assign #2 n3 = a & bb & c; assign #4 y = n1 | n2 | n3; endmodule

Korea Univ Testbenches Testbench is an HDL code written to test another HDL module, the device under test (dut)  It is Not synthesizeable Types of testbenches  Simple testbench  Self-checking testbench  Self-checking testbench with testvectors We’ll cover this later 38

Korea Univ Simple Testbench Signals in initial statement should be declared as reg (we’ll cover this later) 39 `timescale 1ns/1ps module testbench1(); reg aa, bb, cc; wire yy; // instantiate device under test sillyfunction dut(.a (aa),.b (bb),.c (cc),.y (yy)); // apply inputs one at a time initial begin aa = 0; bb = 0; cc = 0; #10; cc = 1; #10; bb = 1; cc = 0; #10; cc = 1; #10; aa = 1; bb = 0; cc = 0; #10; cc = 1; #10; bb = 1; cc = 0; #10; cc = 1; #10; end endmodule `timescale 1ns/1ps module sillyfunction(input a, b, c, output y); assign #3 y = ~b & ~c | a & ~b; endmodule y = bc + ab

Korea Univ Self-checking Testbench 40 `timescale 1ns/1ps module testbench2(); reg aa, bb, cc; wire yy; // instantiate device under test sillyfunction dut(.a (aa),.b (bb),.c (cc),.y (yy)); // apply inputs one at a time // checking results initial begin aa = 0; bb = 0; cc = 0; #10; if (yy !== 1) $display("000 failed."); cc = 1; #10; if (yy !== 0) $display("001 failed."); bb = 1; cc = 0; #10; if (yy !== 0) $display("010 failed."); cc = 1; #10; if (yy !== 0) $display("011 failed."); end endmodule `timescale 1ns/1ps module sillyfunction(input a, b, c, output y); assign #3 y = ~b & ~c | a & ~b; endmodule y = bc + ab