VERILOG 2: LANGUAGE BASICS

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

14:332:231 DIGITAL LOGIC DESIGN. Hardware Description Languages

Reset-able and Enable-able Registers

Contents. Appendix D Verilog Summary Page 1 of 16

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

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

A Brief Introduction to Verilog Hardware Definition Language (HDL)

Verilog HDL Introduction

ECE Digital System Design & Synthesis Exercise 1 - Logic Values, Data Types & Operators - With Answers

Lecture #2: Verilog HDL

Introduction. Purpose. Intended Audience. Conventions. Close

Verilog Module 1 Introduction and Combinational Logic

FPGA: FIELD PROGRAMMABLE GATE ARRAY Verilog: a hardware description language. Reference: [1]

Online Verilog Resources

Chapter 2 Using Hardware Description Language Verilog. Overview

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

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

Introduction to Verilog/System Verilog

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

Spring 2017 EE 3613: Computer Organization Chapter 5: Processor: Datapath & Control - 2 Verilog Tutorial

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

Digital Design with FPGAs. By Neeraj Kulkarni

Design Using Verilog

EECS150 - Digital Design Lecture 10 Logic Synthesis

Verilog. Verilog for Synthesis

Schematic design. Gate level design. 0 EDA (Electronic Design Assistance) 0 Classical design. 0 Computer based language

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

EEL 4783: HDL in Digital System Design

Verilog 1 - Fundamentals

Chapter 4 :: Topics. Introduction. SystemVerilog. Hardware description language (HDL): allows designer to specify logic function only.

271/469 Verilog Tutorial

ECE 353 Lab 3 (Verilog Design Approach)

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

Introduction to Digital VLSI Design מבוא לתכנון VLSI ספרתי

Verilog Design Principles

! ISP (circa 1977) - research project at CMU " Simulation, but no synthesis

ECE 353 Lab 4. Verilog Review. Professor Daniel Holcomb UMass Amherst Fall 2017

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

Verilog Dataflow Modeling

ECE 353 Lab 4. Verilog Review. Professor Daniel Holcomb With material by Professor Moritz and Kundu UMass Amherst Fall 2016

HDLs and SystemVerilog. Digital Computer Design

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

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

Hardware Description Languages: Verilog. Quick History of HDLs. Verilog/VHDL. Design Methodology. Verilog Introduction. Verilog.

Logic Synthesis. EECS150 - Digital Design Lecture 6 - Synthesis

Chapter 3: Dataflow Modeling

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

Combinational Logic II

EN2911X: Reconfigurable Computing Topic 02: Hardware Definition Languages

Chap 6 - Introduction to HDL (b)

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

Control in Digital Systems

Arithmetic Operators There are two types of operators: binary and unary Binary operators:

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

271/471 Verilog Tutorial

Introduction to VHDL #1

EECS150 - Digital Design Lecture 10 Logic Synthesis

Verilog 1 - Fundamentals

Hardware Description Languages: Verilog

ECEN 468 Advanced Logic Design

CSE140L: Components and Design Techniques for Digital Systems Lab

VERILOG: FLIP-FLOPS AND REGISTERS

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

Verilog Behavioral Modeling

CSE241 VLSI Digital Circuits Winter Recitation 1: RTL Coding in Verilog

Course Topics - Outline

Lab #1. Topics. 3. Introduction to Verilog 2/8/ Programmable logic. 2. Design Flow. 3. Verilog --- A Hardware Description Language

Introduction To Verilog Design. Chun-Hung Chou

EECS150 - Digital Design Lecture 4 - Verilog Introduction. Outline

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

Verilog Design Principles

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

Verilog. Reminder: Lab #1 due tonight! Fall 2008 Lecture 3

Hardware description languages

Advanced Digital Design Using FPGA. Dr. Shahrokh Abadi

Verilog Tutorial - Edited for CS141

Digital Design (VIMIAA01) Introduction to the Verilog HDL

Behavioral Modeling and Timing Constraints

Lecture 32: SystemVerilog

Course Topics - Outline

Chapter 4. Digital Design and Computer Architecture, 2 nd Edition. David Money Harris and Sarah L. Harris. Chapter 4 <1>

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

Programmable Logic Devices Verilog VII CMPE 415

Introduction to Verilog. Garrison W. Greenwood, Ph.D, P.E.

Chap 6 Introduction to HDL (d)

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

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

Hardware Description Language VHDL (1) Introduction

ELEC 326: Class project

EN2911X: Reconfigurable Computing Lecture 05: Verilog (2)

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

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

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

Chapter 4. Digital Design and Computer Architecture, 2 nd Edition. David Money Harris and Sarah L. Harris. Chapter 4 <1>

Synthesizable Verilog

ENGIN 241 Digital Systems with Lab

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

LANGUAGE CONSTRUCTS AND CONVENTIONS IN VERILOG

Transcription:

VERILOG 2: LANGUAGE BASICS

Verilog module Modules are basic building blocks. These are two example module definitions which you should use: // Safer traditional method module abc (in1, in2, out); input in1; input in2; output out; <body of module> module in1 in2 abc or // Shorter, info not repeated module abc ( input in1, input in2, output out // no comma on last signal ); <body of module> module out Others exist, but use only one of these two forms in this class B. Baas 35

Verilog Comments and Values Comments Single line assign b = c; // A comment Multiple lines /* This module filters a series of images at a rate of f frames per second */ Values of a single wire or register (not buses) 0 and 1 // Binary zero and one x or X // Unknown value due to things such as uninitialized state or // two drivers driving the same net z or Z // High impedance, e.g., a node not driven by any circuit. // This is identical to the z state of a tri-state output driver. others // Don t worry about others B. Baas 36

Verilog Constants Can be specified in a number of formats; use only these four in this class: binary hexadecimal octal decimal Syntax: [size.in.bits] [first.letter.of.base.of.representation][value] Underscore characters ( _ ) are ignored and can greatly help readability Make sure to specify enough digits to cover the full range of the constant. Although Quartus will probably not complain, other CAD tools may do something you are not expecting especially with more complex number formats. Examples: Value in binary Comment 1 b0 0 1 b1 1 4 b0101 0101 5 h0b 01011 // two hex digits for 5 bits, range of [0, +31] 16 h3f09 0011111100001001 // four hex digits for 16 bits 12 b0000_1010_0101 000010100101 // underscores are ignored 8 d003 00000011 // three base 10 digits for 8 bits // which has range of [0, +255] B. Baas 37

Constants With parameter and `define There are two main methods to simplify constants by using readable text to represent a number parameter Local to a module Usage: parameter HALT = 4 b0101; if (inst == HALT) begin Definitely use this for state names in state machines in this class `define macro Global text macro substitution using a compiler directive Usage: `define HALT 4 b0101 if (inst == `HALT) begin // requires back tick grave accent Best when helpful to put all definitions in a global file; probably do not use in this class B. Baas 38

Verilog Operators Operators: bit-wise negation ~ AND & OR XOR ^ Shift a left by b bits a << b Shift a right by b bits a >> b Operators: logical (e.g., test for if-then-else) negation! AND && OR Basic arithmetic addition + subtraction multiplication * division / modulus % B. Baas 39

Verilog Operators Equality, inequalities, and relational operators all return a 1-bit true or false result equal == not equal!= less than < greater than > less than or equal <= Not to be confused with the non-blocking assignment which is used with flip-flops greater than or equal >= B. Baas 40

Verilog Operators Concatenation x = {a,b,c} Each input may be a wire or a reg The output may be a wire or a reg Example: if g, h, j, l, m are all 6 bits wide, then all = {g,h,j,k,m} is 30 bits wide g h Example: to replicate the sign bit of a 4-bit value a two times and assign it to b: reg [5:0] b; b = {a[3], a[3], a}; If a were 1010, then b would be 111010 j k m { } all B. Baas 41

* 3 Ways to Specify Hardware * There are three primary means to specify hardware circuits: 1) Instantiate another module 2) wires, assign statements 3) registers, always blocks Example instantiating modules inside a main module module abc (in1, in2, out); input in1; input in2; output out; assign... always... always... square_root sqr1 (clk, reset, in1, out1); square_root sqr2 (clk, reset, in2, out2); module module name module name instance names square_root sqr2 abc square_root sqr1 B. Baas 42

Declaring and Referencing Signals Single-bit wire and reg signals wire reset; reg start; Multiple-bit signals By convention, write [(MSB-1):0] Multiple-bit wire and reg signals wire [7:0] phase; // 8-bit signal reg [31:0] elevation; // 32-bit signal To reference part of a multi-bit signal phase[0] // LSB of phase elevation[7:0] // lowest byte B. Baas 43

Verilog Instantiation Syntax Ports of an instantiated module can be connected to signals referenced in the module s declaration assuming they are in the same order but this is dangerous so don t do it. Instead write out both the port name and the connected signal as shown below. module abc (in1, in2, out); input in1; input in2; output out;... module // Don t use this method! It works but typos can be difficult to catch abc instance1 (phase3, angle, magnitude3); // phase3 connected to in1, etc. // This is good. Ports are in the same order as in the module declaration abc instance2 (.in1 (phase1),.in2 (angle),.out (magnitude1) ); // no comma on last port // This is good. Ports are not in the same order as in the module declaration abc instance3 (.in2 (angle), // in2 comes before in1 here but everything.in1 (phase2), // still works ok.out (magnitude2) ); B. Baas 44

Verilog Instantiation Example In this example, two copies of the module abc are instantiated in the higher-level module. As described in a later slide, only wires can connect to the outputs of modules. wire phase; // must be a wire wire mag1; // must be a wire // a, b, c may be wires, regs, // or inputs of the module abc proc1 (.in1 (a),.in2 (b),.out (phase) ); abc proc2 (.in1 (phase),.in2 (c),.out (mag1) ); a b in1 out in2 proc1 (abc) module abc (in1, in2, out); input in1; input in2; output out;... module phase c in1 out in2 proc2 (abc) mag1 B. Baas 45

Describing Hardware module definition As previously stated, there are three main ways to describe hardware circuits which produce a signal, electrical node, word, (whatever you like to call it) inside a module definition: Instantiate a module which has wires connected to its outputs The assign command which defines a wire The always command which defines a reg All of these must be declared at the module definition level not inside each other (e.g., a module instance can not be declared inside an always block) module module_name (port_name_list); module instance assign statement always block wire wire reg B. Baas module 46

Module Inputs and Outputs There are three main possible inputs to a module instance: A wire A reg An input into the module (technically still a wire) The output of a module instance is always a wire, at least for this class This is perhaps the most tricky case wire reg input module definition module module_name (port_name_list); module module instance wire B. Baas 47

All of these signal types may be used as outputs in a module definition: wire reg Module Outputs Another possibility which is typically uncommon is for an input to pass directly to a module output port assign statement always block module instance module definition module module_name (port_name_list); wire reg wire wire wire wire input output B. Baas 48 module wire

1) wire, assign Picture always active hardwired logic For now, declare all wires wire out; // a and b can be wires or regs or module inputs a b out B. Baas 49

1) wire, assign Example: wire out; assign out = a & b; a b & out B. Baas 50

1) wire, assign Example: multibit operands wire [3:0] c, d; // c and d are both 4 bits wire [4:0] sum; // sum is 5 bits so no overflow assign sum = {c[3],c} + {d[3],d}; // sign ext inputs // for 2 s complement c d + sum B. Baas 51

2) reg, always Picture a much more general way of assigning wires or signals or buses if/then/else and case statements are permitted You could, but don t use for loops in hardware blocks (use in testing blocks is ok) Sequential execution statements execute in order to specify a circuit Syntax: always @(sensitivity list) begin statements Operation: statements are executed when any signal in sensitivity list changes B. Baas 52

2) reg, always Including all inputs in the sensitivity list can be tedious and prone to errors especially as the number of statements in the always block grows always @(sensitivity list) begin statements Verilog 1364-2001 allows the use of the always @(*) or always @* construct which tells the simulator to include all inputs in the sensitivity list automatically. This can be very handy but is not supported by all modern CAD tools. Ok to use for this class If you discover any issues, email the instructor and your TA B. Baas 53

2) reg, always Example: there is no difference whatsoever in this AND gate from the AND gate built using assign reg out; always @(a or b) begin out = a & b; a b & out B. Baas 54

2) reg, always There is no assign keyword in always blocks! (Why is it so tempting to type it here?) reg out; always @(a or b) begin out = a & b; reg out2; always @(a or b) begin assign out2 = a ^ b; B. Baas 55

If-Then-Else Statement The general syntax is as follows: if (condition) statement else statement Or, taking advantage of the fact that a begin block acts as a single statement: if (condition) begin statement; statement;... else begin statement; statement;... B. Baas 56

If-Then-Else Statement Nesting an if block within another yields else-if blocks: if (condition1) begin statement; statement;... else if (condition2) begin statement; statement;... else begin statement; statement;... B. Baas 57

assign statement inputs In the same way, there are three main possible inputs to an assign statement: A wire A reg An input into the module Example: input a; wire b; reg c; wire x; assign x = a & b c; wire reg input module definition module module_name (port_name_list); module assign statement wire B. Baas 58

always block inputs In the same way, there are three main possible inputs to an always block: A wire A reg An input into the module (technically still a wire) Example: input a; wire b; reg c; reg x; always @(*) begin x = a & b c; wire reg input module definition module module_name (port_name_list); module always block reg B. Baas 59

Special Block Style: initial This block executes only once at the beginning of the simulation. It is the normal way to write testbench code. initial begin... Example: circuit that generates a reset signal at the beginning of a simulation For our usage, initial blocks are used in only two cases 1) Test bench code 2) Hardware code only to specify the contents of a ROM memory B. Baas 60

Special Block Style: always begin This block executes repeatedly; it begins another execution cycle as soon as it finishes. Therefore it must contain some delay. This is a good construct for a clock oscillator. always begin... Example: clock signal generator Can view as an always @(sensitivity list) construct where the sensitivity list is always activated immediately Verilog suitable for always blocks is also suitable for initial blocks B. Baas 61

Example: 2:1 Multiplexer Example #1 reg out; always @(a or b or s) begin if (s == 1'b0) begin out = a; else begin out = b; a b 0 1 out s B. Baas 62

Example: 2:1 Multiplexer Example #1 Normally always include begin and statements even though they are not needed when there is only one statement in the particular block. Text struck out below could be taken out but always add it anyway in this class. reg out; always @(a or b or s) begin if (s == 1'b0) begin out = a; else begin out = b; a b 0 1 s out B. Baas 63

Example: 2:1 Multiplexer Example #2 May be clearer in some cases, e.g., s==1 b0 sets off auto airbag reg out; always @(a or b or s) begin out = b; if (s == 1'b0) begin out = a; a b 0 1 s out B. Baas 64

Example: 2:1 Multiplexer Example #3 May be clearer in some cases reg out; always @(a or b or s) begin out = a; if (s == 1'b1) begin out = b; a b 0 1 out s B. Baas 65

Example: 2:1 Multiplexer Example #4 Simpler but less clear way of writing if/then/else called "inline if" or "conditional operator" which is also found in some programming languages reg out; always @(a or b or s) begin out = s? b : a; a b 0 1 out s B. Baas 66

Example: 2:1 Multiplexer Example #5 The inline conditional operator can also be used to define wires wire out; assign out = s? b : a; a b 0 1 s out B. Baas 67

Case Statement The general syntax is as follows: case_expression normally a multi-bit bus of wire or reg value i targets normally 0, 1, or a wildcard character (for casez and casex) statement 1) An arbitrary-length block of verilog code beginning with begin and ing with begin a = b + c;... 2) A single verilog statement case (case_expression) value1: statement value2: statement value3: statement... valuen: statement default: statement case If multiple value i targets match the case_expression, only the first one that matches is taken B. Baas 68

Case Statement: default The default case is optional It may be beneficial to set the output to a special value such as x even if you expect the default case will never be reached For example: default: begin out = 4 bxxxx; For a state machine, setting unused values to x makes them don t care states which should allow the synthesis tool to simplify logic Setting unused input values to an easilyrecognizable value (such as x s) could make mistakes easier to spot during debugging Setting the output to x may cause warnings with some CAD tools case (wire or reg) value1: statement value2: statement value3: statement... valuen: statement default: statement case B. Baas 69

casez and casex case Normal case statement casez casex Allows use of wildcard? character for don t cares in the target values casez(in) 4 b1???: out = r; 4 b01??: out = s; 4 b0000: out = t; default: out = 4 bxxxx; case Do not use it for this class. It can use z or x logic Recommation: probably never use it for hardware B. Baas 70

Example: 4:1 Multiplexer Example: 4:1 multiplexer reg out; // must be a reg to be set in an always block! always @(a or b or c or d or s1 or s0) begin case ({s1,s0}) // concatenate two select signals 2 b00: begin out = a; 2 b01: begin a out = b; 2 b10: begin b out = c; 2 b11: begin c out = d; d default: begin // does nothing out = 1 bx; case // of always block 00 01 10 11 s1,s0 out B. Baas 71

Example: 4:1 Mux with zero on Example #1 two inputs reg out; // must be a reg to be set in an always block! always @(c or d or s1 or s0) begin case ({s1,s0}) 2 b00: begin out = c; 2 b01: begin out = 1 b0; 2 b10: begin out = 1 b0; 2 b11: begin out = d; default: begin out = 1 b0; // zero case // of always block 0 d s1,s0 B. Baas 72 c 0 00 01 10 11 out

Example: 4:1 Mux with zero on two inputs Example #2 Here the case s default section is used reg out; // must be a reg to be set in an always block! always @(c or d or s1 or s0) begin case ({s1,s0}) 2 b00: begin out = c; 2 b11: begin out = d; default: begin out = 1 b0; case // of always block c 0 0 d 00 01 10 11 s1,s0 out B. Baas 73

Example: 4:1 Mux with zero on two inputs Example #3 Here out is set to a default value before the case block reg out; // must be a reg to be set in an always block! always @(c or d or s1 or s0) begin out = 1 b0; // set out to a default value case ({s1,s0}) 2 b00: begin out = c; 2 b11: begin out = d; case // of always block c 0 0 d 00 01 10 11 s1,s0 out B. Baas 74

Example: 4:1 Mux with zero on two inputs Example #4 Here if statements are used. Clearly there are many solutions. reg out; // must be a reg to be set in an always block! always @(c or d or s1 or s0) begin out = 1 b0; // set default if ({s1,s0} == 2 b00) begin out = c; if (s1==1 b1 && s0==1 b1) begin out = d; // of always block c 0 0 d 00 01 10 11 out s1,s0 B. Baas 75

Concurrency All circuits operate indepently and concurrently Different from most programming paradigms This is natural if we remember hardware verilog describes real circuit hardware 101100 010101 001001 B. Baas 76