Verilog Semidesign
Verilog Semidesign
VERILOG
INTRODUCTION
Semi Design
find your way in #VLSI with us
History and Evolution of
Verilog
Gateway Design Automation introduced Verilog
1984 (Prabhu Goel and Phil Moorby)
Verilog can be used to model a digital circuit at Behaviour, Data Flow, Gate, Switch
and Structural level.
Examples
// This is a single line comment
Examples
• module
• always
• wire
• reg
Semi Design
find your way in #VLSI with us
Number Specifications
There are two types of number specifications
Sized Numbers
Unsized Numbers
Size Numbers
Syntax : <size>’ <base> <number>
size: decimal value specifying number of bits to represent number.
base: base represents the format of the number. It can be
decimal (d or D) hexadecimal (h or H)
octal (o or O) binary (b or B)
Unsized Numbers
Numbers are written without size specification.
Default number of bits depends upon the machine and simulator,
must be at least 32.
Decimal is the default base format if not specified.
Negative Numbers
To represent a negative number, add minus sign(“-”) before the size
specification.
Negative numbers are stored in 2’s Complement.
Semi Design
find your way in #VLSI with us
Number Specifications
Examples
156 // Decimal number
‘hab3 // Hexadecimal number
‘o54 // Octal Number
5’b10110 // 5-bit binary number
12’h7f3 // 12-bit hexadecimal number
-4’d4 // 2’s complement of 4
-3’b010 // 2’s complement of 2
9’o342 // 9-bit octal number
8’h-af // Invalid syntax
Semi Design
find your way in #VLSI with us
Number Specifications
X or Z values
X is used to represent unknown values.
Z is used to represent high impedance.
If the MSB bit of the number is x or z, then x or z is padded respectively to fill
the remaining most significant bits.
If the MSB bit of the number is 1 or 0, then 0 is padded to fill the remaining most
significant bits.
_ or ?
_ is allowed anywhere in a number except at first character.
_ are used to improve readability. Ignored by the compiler.
? is an alternative for z in verilog.
Semi Design
find your way in #VLSI with us
Number Specifications
Examples
12’h14x // 12 bit hex number with 4 LSB bits X
11‘oz32 // 11 bit octal number with 5 MSB bits Z
5’b110 // 5 bit binary number with 2 MSB bits 0
16’b1011_0100_1100_1010
// 16 bit binary number with _ to improve readability.
Modules are used to provide a common functionality that can be used in others
design units.
Module
endmodule // no semicolon
Semi Design
find your way in #VLSI with us
Module
Example: Vector Port
Registers
Vectors
Integer
Real
Time
Arrays
Parameters
Strings
Semi Design
find your way in #VLSI with us
Data Types
Value Set
Verilog supports four basic values to model functionality of ahardware.
Data Types
Nets
Nets are used to represent connection between elements.
A net should be continuously driven by some hardware.
If there is no driver, the value of a net is Z.
wire is most commonly use net type.
Other net data types are wand, wor, tri etc.
Data Types
Vectors
Vector is used to represent a group of bits.
Nets or reg data types can be declared as vectors.
Examples: wire temp1; //Scalar net
wire [0:7] temp2; //8 bit vector net
reg [3:0] temp3; //4 bit vector reg
Selecting part of a Vector
Data Types
Integer
A type of register used to store integer values.
The default size depends upon host-machine but should be at least 32 bits.
It is used to store signed quantities.
Examples:
integer int1, int2; // signed int1, int2
integer [3:0] int3; // Invalid syntax size not allowed
Semi Design
find your way in #VLSI with us
Data Types
Time
A type of register used to store simulation time.
The size depends upon implementation but should be at least 64 bits.
$time is system function which returns current simulation time.
Examples:
time a, b; //to be used inside initial or
a=$time; //always statement
Semi Design
find your way in #VLSI with us
Data Types
Real
A type of register used to store real values.
Values can be stored in decimal or scientific notation.
Examples:
real a, b; //Assigned inside initial or always block
a=3.14; //Decimal
b=5e12; //Scientific (5 x (10^12))
Semi Design
find your way in #VLSI with us
Data Types
Arrays
Arrays can be used to store same type of data together.
Data Types
Parameters
Constants are defined with keyword parameter.
Parameter value for each module instance can be overridden at compile time.
Data Types
Strings
reg data type is used to store string data.
Each character requires 8 bits for its storage.
If reg size is more than size of string, zeros are padded on left of the string.
If reg size is less, the leftmost bits of string are truncated.
Module Instance
A module can be used as building block in other modules.
A0 A Sum
S
temp
A1 A S B Adder Carry
1 Adder D temp2 D
A2 B C
A3 C
my_design
Semi Design
find your way in #VLSI with us
Positional Port Connection
module my_design (A0, A1, A2, A3, Sum, Carry); input A0, A1, A2, A3 ;
output Sum, Carry; wire temp1, temp2;
module my_design(A0, A1, A2, A3, Sum, Carry); input A0, A1, A2, A3 ;
output Sum, Carry; wire temp1, temp2;
// module adder (A, B, C, S, D);
adder u0 (.A(A1), .B(A2), .C(A3), .S(temp1), .D(temp2)); //.formal ( Actual )
adder u1 (.A(A0) , .B(temp1), .C(temp2), .S(Sum), .D(Carry));
Since they are predefined in Verilog, they don’t require module definition.
There are three class of basic logic gates that verilog provides
and/or gates
buf/not gates
bufif/notif gates
Semi Design
find your way in #VLSI with us
Primitives
and/or gates
They have one scalar output and multiple scalar inputs.
The first port is output, other ports are input.
The following and/or gates are available in verilog
buf not
Semi Design
find your way in #VLSI with us
Primitives
bufif/notif gates
These are buf/not gates with control input.
The last port is control input, middle port is input and first port is output.
The following bufif/notif gates are available in verilog
bufif1 bufif0 notif1 notif0
Examples:
and a1(out, ip1, ip2);
xor x1(out,ip1,ip2);
or o1(out, ip1, ip2, ip3);
Semi Design
find your way in #VLSI with us
Primitives
More Examples:
buf (out1, out2, ip); not n1 (out1, ip); bufif1 b1(out, ip, ctrl); notif0 n2 (out, ip , ctrl);
// Alternative solution
and u [0:3] (out, ip1, ip2);
Semi Design
find your way in #VLSI with us
Operators
Operators in verilog can be classified as following:
On basis of functionality
Arithmetic Reduction
Logical Shift
Relational Concatenation
Equality Replication
Bitwise Conditional
Semi Design
find your way in #VLSI with us
Arithmetic Operators
Arithmetic operators are binary operators.
Verilog provides following arithmetic operations
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
% (Modulus)
** (Exponent)
A – B; // evaluates to 4’b0101
B – A; // evaluates to 4’b1011
A * B; // evaluates to 8’b00011010
D + E; // evaluates to 4’b1000
E / D; // evaluates to 1, i.e. truncates fractional part
E ** F; // evaluates to 25
E % G; // evaluates to 0 result takes sign of 1st operand
E % D; // evaluates to 2
Semi Design
find your way in #VLSI with us
Logical Operators
Logical operators are binary operators.
Verilog provides following logical operations
! (Logical not)
&& (Logical and)
|| (Logical or)
Relational Operators
Relational operators are binary operators.
Verilog provides following equality operations
> (greater than)
!= (inequality)
A && D; // Returns 1
A || E; // Returns 1
(D==4’b1100) && (A==4’b1011); // Returns 0
F >= A; // Returns 1
D > B; // Returns X
B==C; // Returns X
D==E; // Returns 0
A!=F; // Returns 1
B===C; // Returns 1
Semi Design
find your way in #VLSI with us
Bitwise Operators
Verilog provides following bitwise operations
~ (bitwise negation)
& (bitwise and)
| (bitwise or)
^ (bitwise xor)
^~ or ~^ (bitwise xnor)
| (reduction or)
~| (reduction nor)
^ (reduction xor)
^~ or ~^ (reduction xnor)
Arithmetic shift works with signed data only. Arithmetic right shift pads
MSB bits.
Arithmetic left shift works same as logical left shift.
Shift by 1’bx or 1’bz result in X.
Semi Design
find your way in #VLSI with us
Examples
reg [3:0] A=4’b0101, B=4’b1011, C=4’b1101;
reg signed [5:0] D =6’b101101;
Concatenation Operators
Concatenation operators can accept any number of operands.
This operation provides mechanism to append multiple operands.
Examples:
A=2’b01; // reg or wire data
B=3’b110;
C=2’b10;
{A, C}; //Result is 4’b0110
{B, A, C}; // Result is 7’b1100110
{A, 4’b0011, B[2], C}; // Result is 9’b010011110
Semi Design
find your way in #VLSI with us
Replication Operators
Replication operator are used to replicate an operand specified number of times.
{3 { A } }; //Result is 6’b010101
{B , {2{A}}, {2{3’B101}} }; // Result is 11’b1_01_01_101_101
Semi Design
find your way in #VLSI with us
Conditional Operators
Conditional operators are ternary operators.
Example:
assign y1 = sel ? a : b;
assign y2 = (sel==2) ? in1 : in0 ;
assign y3 = sel1 ? (sel2? a : b) : (sel2? c : d); //Nested
Semi Design
find your way in #VLSI with us
Test Bench
A A0
B A1
C A2
D
my_design
A3
E Sum
F Carry
Semi Design
find your way in #VLSI with us
Test Bench
module tb ;
reg A, B, C, D;
wire E, F;
my_design U0(A, B, C, D, E, F); //Module Instance
VERILOG LOOPS
AND
DELAYS
Semi Design
find your way in #VLSI with us
Loops
Verilog provides four type of loop statements
while
for
repeat
forever
while
while loop executes until the while expression is not true.
repeat
Repeat loop executes the loop a fixed number of times
forever
forever is an infinite loop that executes without any condition
Block Statements
Sequential Blocks:
o begin and end keywords are used to group statements.
o Statements are executed in the order they are specified.
Semi Design
find your way in #VLSI with us
Block Statements
Parallel Blocks:
o fork and join keywords are used to group statements.
o Ordering of statements is controlled by delay or event control
assigned to each statement.
Block Statements
• Example: Sequential Block
initial
// Sequential block
begin
a=5; // a=5 occurs at time 0
#3 b=7; // b=7 occurs at time 3
#5 c=4; // c=4 occurs at time 8
#8 d=10; // d=10 occurs at time 16
#2 e=9; // e=9 occurs at time 18
end
Semi Design
find your way in #VLSI with us
Block Statements
• Example: Parallel Block
initial
fork // Parallel block
a=5; // a=5 occurs at time 0
#3 b=7; // e=9 occurs at time 2
#5 c=4; // b=7 occurs at time 3
#8 d=10; // c=4 occurs at time 5
#2 e=9; // d=10 occurs at time 8
join
Semi Design
find your way in #VLSI with us
Nested Block Statements
Example1
integer a=0, b=0, c=0, d=0, e=0;
initial //sequential Block
begin a=5; // a=5 occurs at time 0
#5 b=7; // b=7 occurs at time 5
#6 c=4; // c=4 occurs at time 11
fork #1 a=9; // a=9 occurs at time 12
#4 d=3; // d=3 occurs at time 15
#5 c=8; // c=8 occurs at time 16
join //Parallel Block
#8 d=10; // d=10 occurs at time 24
#2 e=9; // e=9 occurs at time 26
end
Semi Design
find your way in #VLSI with us Nested Block Statements
Example2
integer a=0, b=0, c=0, d=0, e=0;
initial
fork //Parallel Block
a=2; // a=2 occurs at time 0
#6 b=7; // a=9 occurs at time 1
#3 c=4; // e=9 occurs at time 2
begin //Sequential Block // c=4 occurs at time 3
#1 a=9; // d=3 occurs at time 5
#4 d=3; // b=7 occurs at time 6
#5 c=1; end // d=10 occurs at time 9
#9 d=10; // c=1 occurs at time 10
#2 e=9;
join
Semi Design
find your way in #VLSI with us
Named Block
Verilog allows giving names to block statements. These block are
called named blocks.
initial initial
begin begin
b=2; a=3;
forever #5 a=7;
begin : block1 #4 a=2;
#1 count = count + 1;
end
end
Semi Design
find your way in #VLSI with us
Named Block
Example4:
These delays are inertial in nature and are used to model a digital circuit.
Semi Design
find your way in #VLSI with us
First delay is performed and then register is updated with the value
at current time.
Any event on the sensitivity list is ignored till the all the delays
are executed.
b 5 7 8 1 9
0 2 3 6 7 9 12 15 16 19 21 22
Semi Design
find your way in #VLSI with us
Regular Delay Control
Example2:module delay(input signed [31:0] a, output
integer b);
always @ (a)
#3 b<=a;
endmodule
a 2 5 2 7 8 1 6 9
b 5 7 8 1 9
0 2 3 6 7 9 12 15 16 19 21 22
Semi Design
find your way in #VLSI with us
Current values are read and are assigned to the register after specified
delay.
Any event on the sensitivity list is ignored till the all the delays are
executed for blocking statement.
Blocking and Non-Blocking does not show same behavior with this type
of delay.
Semi Design
find your way in #VLSI with us
b 2 8 1 6
0 2 3 6 7 12 15 16 19 21 22
Semi Design
find your way in #VLSI with us
b 2 5 2 7 8 1 6 9
0 2 3 6 7 12 16 19 21
Semi Design
find your way in #VLSI with us
Event OR control
Semi Design
find your way in #VLSI with us
Event OR Control
In may be a requirement that transition on one or more variable may
trigger execution of statement or block of statements.
integer count=0;
always
wait (Enable)
#5 count= count + 1;
Enable is monitored continuously, if it is 0 then statement is not
executed. If it is 1 then count is incremented by 1 after 5 time units.
Semi Design
find your way in #VLSI with us
Conditional Statement
Conditional statements are used to perform some task based on
certain condition.
Conditional Statement
• Syntax2: True or False
if (<Expression>) begin true_statements; end
else
begin false_statements; end
Conditional Statement
Syntax3: Nested if-else
Conditional Statement
always @ (a, b)
begin
if (a==b)
c=0;
else
c=1;
end
Semi Design
find your way in #VLSI with us
Multiway Branching
Verilog provides case statement which tests whether
expression matches one of the multiple alternatives.
Syntax: case (<expression>)
alternative1 : begin statements1; end
alternative2 : begin statements2; end
alternative3 : begin statements3; end
……
default : begin default statements; end
endcase
Multiway Branching
Statements corresponding to first alternative that matches the
expression are executed.
Multiway Branching
Example1: Multiplexer
always @ (*) //all RHS variable covered in sensitivity list
case(sel)
2’b00: out=a;
2’b01: out=b;
2’b10: out=c;
2’b11: out=d;
default: $display(“Invalid selection”);
endcase
Semi Design
find your way in #VLSI with us
Multiway Branching
Example2
always @ (*) //all RHS variable covered in
sensitivity list
case(sel)
2’b00: out=a;
2’b01: out=b;
default: $display(“Invalid selection”);
2’b01: out=c;
2’b11: out=d;
endcase
Semi Design
find your way in #VLSI with us
Multiway Branching
Example3 always @ (*)
case(sel)
2’b00: out=a; 2’b01: out=b;
2’b10: out=c; 2’b11: out=d;
2’b0x, 2’bx0, 2’b1x, 2’bx1, 2’bzx, 2’bxz,
2’bxx:
begin out=0; $display(“sel contains X”); end
2’b0z, 2’bz0, 2’b1z, 2’bz1, 2’bzz:
begin out=0; $display(“sel contains Z”); end
default: $display(“unspecified input”);
endcase
Semi Design
find your way in #VLSI with us
casez
Example: casez
always @ (*) //all RHS variable covered in sensitivity list
casez(sel)
2’b0?: out=a;
2’b0x: out=b;
2’b10: out=c;
2’b11: out=d;
default: out=e;
endcase
Semi Design
find your way in #VLSI with us
casex
Example: casex
always @ (*) //all RHS variable covered in sensitivity list
casex(sel)
2’bx0: out=a;
2’b00: out=b;
2’b1?: out=c;
2’b11: out=d;
default: out=e;
endcase