Top Banner
Lecture Note on Verilog, Course #90132300, EE, NTU, C.H. Chao Basic Logic Design with Verilog TA: Chihhao Chao [email protected] Lecture note ver.1 by Chen-han Tsai ver.2 revised by Chih-hao Chao
46
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: verilog

Lecture Note on Verilog, Course #90132300, EE, NTU, C.H. Chao

Basic Logic Designwith Verilog

TA: Chihhao [email protected]

Lecture note ver.1 by Chen-han Tsaiver.2 revised by Chih-hao Chao

Page 2: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Outline

Introduction to HDL/ Verilog Gate Level ModelingBehavioral Level ModelingTest benchSummary and Notes

Page 3: verilog

Lecture Note on Verilog, Course #90132300, EE, NTU, C.H. Chao

Introduction to HDL/ Verilog

Page 4: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

What is HDL/VerilogWhy use HDL (Hardware Description Language)?

Design abstraction: HDL ←→ layout by humanHardware modelingReduce cost and time to design hardware

Verilog is one of the most popular HDLsVHDL (another popular HDL)

Key features of VerilogSupports various levels of abstraction

Behavior level Register transfer levelGate levelSwitch level

Simulate design functions

Page 5: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Hardware Design Flow

RTLEditor

LogicSynthesizer

RTLSimulation

Gate LevelSimulation

Place & RoutePost Gate

LevelSimulation

Chip

RTL Code

Gate Level Code

Physical Layout

Tape Out

DesignerLevel

High

Low

Cost

Low

High

Verilog

Page 6: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

An Example1-bit Multiplexer

in1

in2out

0

1

sel

if (sel==0) out = in1;else out = in2;

out = (sel’‧in1) + (sel‧in2)

sel in1 in2 out

0 0 0 0

0 0 1 0

0 1 0 1

0 1 1 1

1 0 0 0

1 0 1 1

1 1 0 0

1 1 1 1

to “select” output

Page 7: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Gate Level Description

a1

a2

in1

in2

sel

outo1

iv_sel a1_o

a2_on1

iv_sel

Gate Level: you see only netlist (gates and wires) in the code

Page 8: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Behavioral Level/RTL Description

RTL: you may see high level behavior in the code

Behavior: event-driven behavior description construct

always block assign

Page 9: verilog

Lecture Note on Verilog, Course #90132300, EE, NTU, C.H. Chao

Verilog HDL Syntax

Page 10: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

A Simple Verilog Code

declaration syntax

module name in/out port

port/wiredeclaration

kernel hardwaregate-connection/behavior

Page 11: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Module Basic building block in Verilog.Module1. Created by “declaration” (can’t be nested)2. Used by “instantiation“Interface is defined by portsMay contain instances of other modulesAll modules run concurrently

Page 12: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

InstancesA module provides a template from which you can create actual objects.When a module is invoked, Verilog creates a unique object from the template.Each object has its own name, variables, parameters and I/O interface.

Page 13: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Module Instantiation

Adder

Adder Adder

Adder_tree

instance example

Page 14: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Analogy: module ↔ class

obj_name.member_datains_name.member_signalMember

object.sub_object.member_data

instance.sub_instance.member_signal

Hierachy

c_Name obj_name;m_Name ins_name ( port connection list );

Instantiation

class c_Name {...};

module m_Name( IO list );...endmodule

Format

As module is to Verilog HDL, so class is to C++programming language.

Page 15: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Analogy: module ↔ class

assign and evaluate() is simulated/called at each Ti+1 = Ti + tresolution

Model AND gate with C++ Model AND gate with Verilog HDL

Page 16: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Port Connection

Connect module port by order listFA1 fa1(c_o, sum, a, b, c_i);

Not fully connectedFA1 fa3(c_o,, a, b, c_i);

Connect module port by name .PortName( NetName ) FA1 fa2(.A(a), .B(b), .CO(c_o),.CI(c_i), .S(sum));Recommended

Page 17: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Verilog Language Rule Case sensitive Identifiers:

Digits 0…9Underscore _Upper and lower case letters from the alphabet

Terminate statement/declaration with semicolon “;”Comments:

Single line: // it’s a single line comment exampleMulti-line: /* when the comment exceeds single line,

multiline comment is necessary*/

Page 18: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Register and NetRegisters

Keyword : reg, integer, time, realEvent-driven modelingStorage element (modeling sequential circuit)Assignment in “always” block

NetsKeyword : wire, wand, wor, tri

triand, trior, supply0, supply1Doesn’t store value, just a connectioninput, output, inout are default “wire”Can’t appear in “always” block assignment

Page 19: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Four-valued LogicVerilog’s nets and registers hold four-valued data

0 represent a logic zero or false condition1 represent a logic zero or false conditionz

Output of an undriven tri-state driver –high-impedance valueModels case where nothing is setting a wire’s value

xModels when the simulator can’t decide the value –uninitialized or unknown logic value

Initial state of registersWhen a wire is being driven to 0 and 1 simultaneouslyOutput of a gate with z inputs

Page 20: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Logic SystemFour values: 0, 1, x or X, z or Z // Not case sensitive here

The logic value x denotes an unknown (ambiguous) valueThe logic value z denotes a high impedance

Primitives have built-in logicSimulators describe 4-value logic (see Appendix A in text)

ab

y

0 1

x

a

b

y

x

xx

z

z z z zx x x x

0 1 X Z0 0 0 0 01 0 1 X XX 0 X X XZ 0 X X X

Page 21: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Number RepresentationFormat: <size>’<base_format><number>

<size> - decimal specification of number of bitsdefault is unsized and machine-dependent but at least 32 bits

<base format> - ' followed by arithmetic base of number

<d> <D> - decimal - default if no <base_format> given<h> <H> - hexadecimal<o> <O> - octal<b> <B> - binary

<number> - value given in base of <base_format>_ can be used for reading clarityx, z is automatically extented

Page 22: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Number RepresentationExamples:

6’b010_111 gives 0101118’b0110 gives 000001104’bx01 gives xx0116’H3AB gives 000000111010101124 gives 0…00110005’O36 gives 1111016’Hx gives xxxxxxxxxxxxxxxx8’hz gives zzzzzzzz

Page 23: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Value and Number Expressions : Examples

659 // unsized decimal‘h 837ff // unsized hexadecimal‘o7460 // unsized octal4af // illegal syntax4’b1001 // 4-bit binary5’D 3 // 5-bit decimal3’b01x // 3-bit number with

unknown LSB12’hx // 12-bit unknown8’d -6 // illegal syntax-8’d 6 // phrase as - (8’d6)

// underline usage27_195_00016’b0001_0101_0001_111132’h12ab_f001

// X and Z is sign-extended

reg [11:0] a;initialbegin

a = ‘hx; // yields xxxa = ‘h3x; // yields 03xa = ‘h0x; // yields 00x

end

Page 24: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Net Concatenations : An Easy Way to Group Nets

Representations Meanings{b[3:0],c[2:0]} {b[3] ,b[2] ,b[1] ,b[0], c[2] ,c[1] ,c[0]}{a,b[3:0],w,3’b101} {a,b[3] ,b[2] ,b[1] ,b[0],w,1’b1,1’b0,1’b1}{4{w}} {w,w,w,w}{b,{3{a,b}}} {b,a,b,a,b,a,b}

3‘o7

Module AModule B

Module C

Page 25: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

(excerpts from CIC training course: Verilog_9807.pdf)

Page 26: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

(excerpts from CIC training course: Verilog_9807.pdf)

all bits are 0 →logic false

Page 27: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Compiler Directives `define

`define RAM_SIZE 16Defining a name and gives a constant value to it.

`include`include adder.vIncluding the entire contents of other verilog source file.

`timescale`timescale 100ns/1nsSetting the reference time unit and time precision of your simulation.

Page 28: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

System Tasks$monitor

$monitor ($time,"%d %d %d",address,sinout,cosout);Displays the values of the argument list whenever any of the arguments change except $time.

$display$display ("%d %d %d",address,sinout,cosout);Prints out the current values of the signals in the argument list

$finish$finishTerminate the simulation

Page 29: verilog

Lecture Note on Verilog, Course #90132300, EE, NTU, C.H. Chao

Gate Level Modeling

Gate Level ModelingCase Study

Page 30: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Gate Level ModelingSteps

Develope the boolean function of outputDraw the circuit with logic gates/primitivesConnect gates/primitives with net (usually wire)

HDL: Hardware Description LanguageFigure out architecture first, then write code.

Page 31: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Primitives

Primitives are modules ready to be instancedSmallest modeling block for simulatorVerilog build-in primitive gate

and, or, not, buf, xor, nand, nor, xnorprim_name inst_name( output, in0, in1,.... );

User defined primitive (UDP)building block defined by designer

Page 32: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Case Study1-bit Full Adder

A B

CiCo

S

FullAdder

Page 33: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Case Study1-bit Full Adder

co = (a•b) + (b•ci) + (ci•a);

Page 34: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Case Study1-bit Full Adder

sum = a b ci

Page 35: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Case Study1-bit Full Adder

Full Adder ConnectionInstance ins_c from FA_coInstance ins_s from FA_sum

abc

sum

abbcca

co

carry out connection

sum connection

full adder

Page 36: verilog

Lecture Note on Verilog, Course #90132300, EE, NTU, C.H. Chao

RT-Level & Behavioral Level Modeling

RT-Level & Behavioral Level ModelingCase Study

Page 37: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

RT-Level & Behavioral Level Modeling

High level descriptionUser friendlyConcise codeFaster simulation speed ( event driven )

Widely used for some common operations+,-,*&,|,~

Two main formatsalways block ( for behavior level )assign ( for RT level )

Page 38: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Case Study1-bit Full Adder

A B

CiCo

S

FullAdder

{Co,S} = A + B + Ci

Page 39: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Case Study1-bit Full Adder

RT-level modeling of combinational circuitDescribe boolean function with operators and use continuous assignment assign

Page 40: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Case Study1-bit Full Adder

Behavior-level modeling of combinational circuit:

Use event-driven construct: always blockEvent: @( sensitive_list )

Page 41: verilog

Lecture Note on Verilog, Course #90132300, EE, NTU, C.H. Chao

Test bench

Page 42: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Test Methodology Systematically verify the functionality of a model. Simulation: (1) detect syntax violations in

source code (2) simulate behavior (3) monitor results Design

Top Module

input ports

output ports

Test bench

data_oEqual?

answer_o

data_i

Page 43: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Verilog Simulator

Page 44: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

Testbench for Full Addermodule t_full_add();reg a, b, cin; // for stimulus waveformswire sum, c_out;full_add M1 (sum, c_out, a, b, cin); //DUTinitial #200 $finish; // Stopwatchinitial begin // Stimulus patterns#10 a = 0; b = 0; cin = 0; // Statements execute in sequence#10 a = 0; b = 1; cin = 0; #10 a = 1; b = 0; cin = 0; #10 a = 1; b = 1; cin = 0; #10 a = 0; b = 0; cin = 1; #10 a = 0; b = 1; cin = 1; #10 a = 1; b = 0; cin = 1; #10 a = 1; b = 1; cin = 1;endendmodule

Page 45: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

SummaryDesign module

Gate-level or RT-levelReal hardware

Instance of modules exist all the timeEach module has architecture figure

Plot architecture figures before you write verilog codes

Test benchFeed input data and compare output values versus timeUsually behavior levelNot real hardware, just like C/C++

Page 46: verilog

Lecture Note on Verilog, Course #901 32300, EE, NTU C.H. Chao, 11/18/2005

NoteVerilog is a platform

Support hardware design (design module)Also support C/C++ like coding (test bench)

How to write verilog wellKnow basic concepts and syntaxGet a good reference (a person or some code files)Form a good coding habit

Naming rule, comments, format partition (assign or always block)

HardwareCombinational circuits (today’s topic)

畫圖(architecture), then 連連看(coding)Sequential circuits (we won’t model them in this course)

register: element to store data