68
Assembly Language and Computer Organization Richard A. Goodrum

Intro to Verilog

Embed Size (px)

DESCRIPTION

Verilog starter info

Citation preview

Page 1: Intro to Verilog

Assembly Language and Computer Organization

Richard A. Goodrum

Page 2: Intro to Verilog
Page 3: Intro to Verilog

SourcesComputer Organization and Design, Fourth

Edition, Patterson and HennessyVerilog Hardware Description Language,

The, Fifth Edition, Thomas and Moorbyhttp://www.asic-world.com/verilog/

syntax.html

Page 4: Intro to Verilog

White SpaceBlank spaces Tabs Carriage returns New-line Form-feeds

Page 5: Intro to Verilog

CommentsC and C++ Style Comments

From // to the end-of lineMulti-line comments beginning with /* and

ending with */

Page 6: Intro to Verilog

Case SensitivityVerilog is case sensitive

“Code” is not the same as “code”All Verilog keywords are lower case.

Page 7: Intro to Verilog

IdentifiersBegin with alphabetic or underscore symbolsMay contain alphanumeric, underscore, or $

Page 8: Intro to Verilog

Escaped IdentifiersBegin with backslashEnd with white space

Page 9: Intro to Verilog

NumbersIntegersReal (Floating Point)

Page 10: Intro to Verilog
Page 11: Intro to Verilog

Data TypesTo connect ports requires a “wire”

wire [31:0] xyzzy; // is a 32-bit wirewire [3:0] plugh; is a 4-bit wire

To store values requires a “reg”reg [5:0] omega; // is a single 6-bit registerreg [31:0] delta[0:31]; // is 32 32-bit registers

otherwise known as a memory

Page 12: Intro to Verilog

Data Types in VerilogData values

01x - unknownz – high impedience

Page 13: Intro to Verilog

Data Representationw’rnw is the field width, an unsigned decimal value

specifying the number of bits in the value.r is the radix

h – hexd – decimalo – octalb – binary

n is the number represented in the appropriate radix

Page 14: Intro to Verilog

Data Examples6’d51 is the decimal value 51 stored as a 6-

bit quantity3’b010 is the 3-bit quantity 010 binary = 2

decimal8’o67 is the 8-bit value 0011_0111 binary =

55 decimal12’h31 is the 12-bit value 0000_0011_0001

binary = 49 decimal

Page 15: Intro to Verilog

Data Concatenation{r1{b1}[,ri{bi}]}

The braces are dropped after evaluationThe brackets represent optional, repeatable

fields.rj – an optional repeat count for the bit fieldbj – bit field representation of a value

bj Data Representation

Example{3{1’b1},2’b0,3’b111} = 8’b1110_0111 = 8’he7

Page 16: Intro to Verilog

Exercise – Verilog ValuesWhich of the following define exactly the

same values?8’b111100008’hF08’d240{{4{1’b1}},{4{1’b0}}}{4’b1,4’b0}

Page 17: Intro to Verilog

Answer – Verilog ValuesAll of them

Page 18: Intro to Verilog
Page 19: Intro to Verilog

Operators in Verilog Assignment Operators

=, <= Arithmetic Operators

+, -, *, /, % Logical Operators

&&, ||, ! Bit-wise

~, &, |, ^, ~^, ^~ Comparison Operators

==, ===, !=, !==, >, <, <=, >= Shift Operators

<<, >> Conditional Operator

?: Unary Reduction Operators

&, ~&, |, ~|, ^, ~^, ^~ Concatenation Operator

{}

Page 20: Intro to Verilog

Concatenation Operator{r1{b1}[,ri{bi}]}

The braces are dropped after evaluationThe brackets represent optional, repeatable

fields.rj – an optional repeat count for the bit fieldbj – bit field representation of a value or

variablebj

Data RepresentationBit Selection

Variable[high bit:low bit]

Page 21: Intro to Verilog
Page 22: Intro to Verilog

Structure of a Verilog ModelModel hierarchy consists of:

ModulesPorts

Page 23: Intro to Verilog

Components of a ModuleParametersNetsRegistersPrimitives and InstancesContinuous AssignmentsProcedural BlocksTask/Function definitions

Page 24: Intro to Verilog
Page 25: Intro to Verilog

General Structuremodule <module_name> (<port_list>); module contentendmodule

Page 26: Intro to Verilog

Top Level Structuremodule name; module contentendmodule

Page 27: Intro to Verilog

Module Invocation• Module may invoke other modules:

– module <module_name_1> (<port_list_1>);– .– . – <module_name_2>

<instance_name> (<port_list_2>);

– .– .– endmodule

Page 28: Intro to Verilog

Multiple Modules• module foo;• bar boo (port1, port2);• bar hoo (port1, port2);• endmodule

• module bar (port1, port2);• ...• endmodule

Page 29: Intro to Verilog
Page 30: Intro to Verilog

Declaring Port Directionsmodule foo ( a, b, c ); input a; output b; inout c; …endmodule

Page 31: Intro to Verilog

Declaring Port Directionsmodule foo ( input a, output b, inout c ); …endmodule

Page 32: Intro to Verilog

Verilog PortsPorts equate to wiresType Data Direction

input parent->child output child->parent inout child<->parent

Page 33: Intro to Verilog

Instantiating Portsmodule top; wire x, y, z; feefi fofum( x, y, z ); …endmodule

Page 34: Intro to Verilog

Assign Statements

Page 35: Intro to Verilog

Combinational Logicmodule half_adder ( A, B, Sum, Carry ) input A, B; // two 1-bit inputs; output Sum, Carry; // two 1-bit outputs assign Sum = A ^ B; // Sum is A xor B assign Carry = A & B; // Carry is A and

Bendmodule

Page 36: Intro to Verilog

Common ProblemCreating sequential logic which imply the

existence of a latch or register

Page 37: Intro to Verilog

Tips and TechniquesPlace all combinational logic in a continuous

assignment or an always blockMake sure that all the signals used as inputs

appear in the sensitivity list of an always block

Ensure that every path through an always block assigns a value to the exact same set of bits

Page 38: Intro to Verilog

ExerciseAssuming all values are initially zero, what

are the values of A and B after executing this Verilog code inside an always block?C = 1;A <= C;B = C;

Page 39: Intro to Verilog

AnswerA = 0B = 1

Page 40: Intro to Verilog

Sensitivity Lists

Page 41: Intro to Verilog

Representing Complex Combinatorial Logic in VerilogSensitivity list

always @(list of signals that cause reevaluation) begin Verilog statements including assignments and other control statementsend

Blocking assignmentsStatements using the = assignment operator

Non-blocking assignmentsStatements using the <= assignment operator

Page 42: Intro to Verilog

Sensitivity Listalways @( list of signals that cause

reevaluation) begin Verilog Statements end

Page 43: Intro to Verilog
Page 44: Intro to Verilog

4 to 1 Multiplexor• module Mult4to1(Out , Sel, In1, In2, In3, In4 );• input [31:0] In1, In2, In3, In4;• input [1:0] Sel; // Selector signal• output [31:0] Out;• always @(Sel, In1, In2, In3, In4 )• case (Sel) // a 4 -> 1 multiplexor• 0: Out <= In1;• 1: Out <= In2;• 2: Out <= In3;• default: Out <= In4;• endcase• endmodule

Page 45: Intro to Verilog
Page 46: Intro to Verilog

Basic MIPS ALU• module MIPSALU (ALUOut, Zero, ALUctl, A, B );• input [3:0] ALUctl;• input [31:0] A, B;• output reg [31:0] ALUOut;• output Zero;• assign Zero = (ALUOut==0); // Zero is 1 if ALUOut is 0• always• @( ALUctl, A, B )• case (ALUOut)• 0: ALUOut <= A & B;• 1: ALUOut <= A | B;• 2: ALUOut <= A + B;• 6: ALUOut <= A - B;• 7: ALUOut <= A < B ? 1 : 0;• 12: ALUOut <= ~(A | B); // nor• default: ALUOut <= 0;• endcase• endmodule

Page 47: Intro to Verilog
Page 48: Intro to Verilog

One Bit Adder

a b

cico

z

+

a b ci co z

0 0 0 0 0

0 0 1 0 1

0 1 0 0 1

0 1 1 1 0

1 0 0 0 1

1 0 1 1 0

1 1 0 1 0

1 1 1 1 1

Page 49: Intro to Verilog

One Bit Adder – Gate Level module OneBitAdder( input a, input b, input ci, output co, output z ); wire na, nb, nci, x0, x1, x2, y0, y1, y2, y3; not #1 ng1( na, a ), ng2( nb, b ), ng3( nci, ci ); and #1 ag1( x0, a, b ), ag2( x1, b, ci ), ag3( x2, a, ci ),

ag4( y0, a, nb, nci ), ag5( y1, na, b, nci ), ag6( y2, na, nb, ci ), ag6( y3, a, b, ci );

or #1 og1( co, x0, x1, x2 ), og2( z, y0, y1, y2, y3 ); endmodule

Page 50: Intro to Verilog
Page 51: Intro to Verilog

ClocksClock PeriodCycle TimeEdge-triggered

Rising EdgeFalling Edge

Level triggeredAssertedDeasserted

Page 52: Intro to Verilog

Specifying Sequential Logic in VerilogSpecifying a clock

reg clock; // clock is a registeralways #1 clock = 1; #1 clock = 0;

Using a clockreg [31:0] A;wire [31:0] b;always @(posedge clock) A <= b;

Page 53: Intro to Verilog

Clock Module DiscussionA clock signal stays high for some length of

time, HI,Then, it transitions to low and stays there for

some length of time, LO.It is appropriate to use parameters to define

these

HILO

Page 54: Intro to Verilog

Clock Modulemodule Clock( clock ); #( parameter LO = 10, HI = 10 ) output reg clock;

initial clock = 0;

always begin #LO clock = ~clock; #HI clock = ~clock; end

endmodule

Page 55: Intro to Verilog

Clock Testbenchmodule testClock; wire clock;

Clock c0( clock );

initial begin $monitor( $time,,”clock = %b”,

clock ); #100 $finish; end

endmodule

Page 56: Intro to Verilog

Compiling and Executing

Page 57: Intro to Verilog
Page 58: Intro to Verilog

Verilog TutorialChapter 6 - Advanced Features

System Tasks and Functions $readmem

Page 59: Intro to Verilog

Reading File DataVerilog supports the following system tasks for

reading files into a model:$readmemx( “file”, <mem>, <<start><,<end>>?>?);

Underscore characters, _, are ignoredEnd of line comments, //, are ignoredPlaces the contents of each value into a different

memory location.Values are white space delimited@hh…h may be used, in the file, to specify the

location of subsequent data.

Page 60: Intro to Verilog

Reading File Datawhere

x – isb – binary formatted datah – hexidecimal formatted data

mem – the Verilog Identifier being referenced start – starting address within mem end – ending address within mem

Page 61: Intro to Verilog

$readmemh example module read;

reg [31:0] m[0:31]; reg [5:0] i;

initial begin $readmemh( "memory.dat", m ); end

always for( i=0; i<32; i=i+1 ) begin if( m[i] ) $display( "m[%d] = %x", i, m[i] ); else $finish; end

endmodule

Page 62: Intro to Verilog

memory.data0123_4567 // comments are ignored89ab_cdef // underscores are ignored@4 dead beef@2 feed babe

Page 63: Intro to Verilog

Compile and Execute

Page 64: Intro to Verilog
Page 65: Intro to Verilog

Defining the MIPS ALU in Verilog – Behavioral Definition module MIPSALU (ALUctl, A, B, ALUOut, Zero); input [3:0] ALUctl; input [31:0] A,B; output reg [31:0] ALUOut; output Zero; assign Zero = (ALUOut==0); //Zero is true if ALUOut is 0 always @(ALUctl, A, B) begin //reevaluate if these change case (ALUctl) 0: ALUOut <= A & B; 1: ALUOut <= A | B; 2: ALUOut <= A + B; 6: ALUOut <= A - B; 7: ALUOut <= A < B ? 1 : 0; 12: ALUOut <= ~(A | B); // result is nor default: ALUOut <= 0; endcase end endmodule

Page 66: Intro to Verilog

Defining the MIPS ALU in Verilog – Control Definition module ALUControl (FuncCode, ALUCtl); input [5:0] FuncCode; output [3:0] reg ALUCtl; always case (FuncCode) 32: ALUCtl <=2; // add 34: ALUCtl <=6; //subtract 36: ALUCtl <=0; // and 37: ALUCtl <=1; // or 39: ALUCtl <=12; // nor 42: ALUCtl <=7; // slt default: ALUCtl <=15; // should not happen endcase endmodule

Page 67: Intro to Verilog

Specifying Sequential Logic in VerilogSpecifying a clock

reg clock; // clock is a registeralways#1 clock = 1; #1 clock = 0;

Using a clockreg [31:0] A;wire [31:0] b;always @(posedge clock) A <= b;

Page 68: Intro to Verilog

MIPS Register File:Rising Edge Triggered

module registerfile (Read1,Read2,WriteReg,WriteData,RegWrite,Data1,Data2,clock);

input [5:0] Read1,Read2,WriteReg; // the register numbers to read or write input [31:0] WriteData; // data to write input RegWrite, // the write control clock; // the clock to trigger write output [31:0] Data1, Data2; // the register values read reg [31:0] RF [31:0]; // 32 registers each 32 bits long

assign Data1 = RF[Read1]; assign Data2 = RF[Read2];

always begin // write the register with new value if Regwrite is high @(posedge clock) if (RegWrite) RF[WriteReg] <= WriteData; end

endmodule