Verilog
Verilog
Concepts
Learning Objectives:
• Understand top-down and bottom-up design methodologies
for digital design.
• Explain differences between modules and module instances
in Verilog.
• Describe four levels of abstraction? behavioral, data flow,
gate level, and switch level? to represent the same module.
• Describe components required for the simulation of a digital
design.
•Define a stimulus block and a design block. Explain two
methods of applying stimulus.(Not being covered new!)
Design Methodologies
Ex. of T-FF:
module T_FF (q, clock, reset);
.
.
<functionality of T-flipflop>
.
.
endmodule
Levels of Abstraction:
1. Behavioral or algorithmic level
• This is the highest level of abstraction provided by Verilog HDL.
• A module can be implemented in terms of the desired design
algorithm without concern for the hardware implementation
details.
• Designing at this level is very similar to C programming.
2. Dataflow level
• At this level, the module is designed by specifying the data
flow.
• Here the designer is aware of how data flows between
hardware registers and how the data is processed in the
design.
3. Gate level (Structural)
• The module is implemented in terms of logic
gates and interconnections between these gates.
• Design at this level is similar to describing a
design in terms of a gate-level logic diagram.
4. Switch level
• This is the lowest level of abstraction
provided by Verilog.
• A module can be implemented in terms of
switches, storage nodes, and the interconnections
between them.
• Design at this level requires knowledge of
switch-level(transistor) implementation details.
Flexibility of Abstraction Levels
• the higher the level of abstraction, the more flexible and
technology independent the design.
• As one goes lower toward switch-level design, the design
becomes technology-dependent and inflexible.
Ex:
The analogy with C programming and assembly language
programming.
• It is easier to program in a higher-level language
such as C. The program can be easily ported to any machine.
• However, if you design at the assembly level, the program is
specific for that machine and cannot be easily ported
Instances and Instantiation
Module Instantiation
// Define the top-level module called ripple carry
// counter. It instantiates 4 T-flipflops.
module ripple_carry_counter(q, clk, reset);
output [3:0] q;
input clk, reset;
//Four instances of the module T_FF are created. Each has a unique
//name.Each instance is passed a set of signals. Notice, that
//each instance is a copy of the module T_FF.
T_FF tff0(q[0],clk, reset);
T_FF tff1(q[1],q[0], reset);
T_FF tff2(q[2],q[1], reset);
T_FF tff3(q[3],q[2], reset);
endmodule
// Define the module T_FF. It instantiates a D-flipflop.
//module D-flipflop is defined elsewhere in the design.
module T_FF(q, clk, reset);
//Declarations to be explained later
output q;
input clk, reset;
wire d;
D_FF dff0(q, d, clk, reset); // Instantiate D_FF. Call it dff0.
not n1(d, q); // not gate is a Verilog primitive.
endmodule
Illegal Module Nesting
• One module definition cannot contain another
module definition within the module and
endmodule statements.
• Instead, a module definition can incorporate
copies of other modules by instantiating
them.
• It is important not to confuse module
definitions and instances of a module
Example of Illegal Module Nesting
1. Sized numbers
• Sized numbers are represented as
<size> '<base format> <number>.
• <size> is written only in decimal and specifies the number
of bits in the number.
• <base format> can be decimal ('d or 'D), hexadecimal ('h or
'H), binary ('b or 'B) and octal ('o or 'O).
• The <number> is specified as consecutive digits from 0, 1,
2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f.
• Uppercase letters are legal for number specification.
Lexical Conventions
Examples of Sized numbers:
1. 4'b1111
// This is a 4-bit binary number
2. 12'habc
// This is a 12-bit hexadecimal number
3. 16'd255
// This is a 16-bit decimal number.
Lexical Conventions
2. Unsized numbers
• Numbers that are specified without a <base format>
specification are decimal numbers by default.
• Numbers that are written without a <size> specification
have a default number of bits that is simulator- and
machine-specific (must be at least 32).
Examples of unsized numbers:
1. 23456 // This is a 32-bit decimal number by default
2. 'hc3 // This is a 32-bit hexadecimal number
3. 'o21 // This is a 32-bit octal number
Lexical Conventions
X or Z values:
• Verilog has two symbols for unknown and high
impedance values along with 0 and 1.
• These values are very important for modeling
real circuits.
• An unknown value is denoted by an X.
• A high impedance value is denoted by Z.
Lexical Conventions
Examples for X and Z
• 12'h13x // This is a 12-bit hex number; 4 least significant
bits unknown
• 6'hx // This is a 6-bit hex number
• 32'bz // This is a 32-bit high impedance number
Lexical Conventions
Negative numbers
• Negative numbers can be specified by putting
a minus sign before the size for a constant
number.
• Size constants are always positive. It is illegal
to have a minus sign between <base format>
and <number>.
• An optional signed specifier can be added for
signed arithmetic.
Lexical Conventions
Examples of Negative numbers:
• -6'd3 // 8-bit negative number stored as 2's
complement of 3
• -6'sd3 // note the sign specifier, which is optional
• 4'd-2 // Illegal specification
Lexical Conventions
Underscore characters and question marks
• An underscore character "_" is allowed
anywhere in a number except the first
character.
• Underscore characters are allowed only to
improve readability of numbers and are
ignored by Verilog.
Lexical Conventions
• A question mark "?" is the Verilog HDL
alternative for z in the context of numbers.
Examples:
• 12'b1111_0000_1010 // Use of underline
characters for readability
• 4'b10?? // Equivalent of a 4'b10zz
Lexical Conventions
Strings
• A string is a sequence of characters that are enclosed
by double quotes.
• The restriction on a string is that it must be contained
on a single line, that is, without a carriage return. It
cannot be on multiple lines.
• Strings are treated as a sequence of one-byte ASCII
values.
Examples:
• "Hello Verilog World" // is a string
• "a / b" // is a string
Lexical Conventions
Identifiers and Keywords
• Keywords are special identifiers reserved to
define the language constructs. Keywords are
in lowercase.
• Identifiers are names given to objects so that
they can be referenced in the design.
• Identifiers are made up of alphanumeric
(Alphabets and Numbers) characters, the
underscore ( _ ), or the dollar sign ( $ ).
Lexical Conventions
• Identifiers are case sensitive.
• Identifiers start with an alphabetic character or
an underscore.
• They cannot start with a digit or a $ sign (The $
sign as the first character is reserved for
system tasks).
Examples:
• reg value; // reg is a keyword; value is an identifier
• input clk; // input is a keyword, clk is an identifier
Lexical Conventions
Escaped Identifiers
• Escaped identifiers begin with the backslash ( \ ) character
and end with whitespace(space, tab, or newline).
• All characters between backslash and whitespace are
processed literally.
• Any printable ASCII character can be included in escaped
identifiers.
• Neither the backslash nor the terminating whitespace is
considered to be a part of the identifier.
Examples:
• \a+b-c
• \**my_name**
Data Types
Value Set
• Verilog supports four values and eight strengths
to model the functionality of real hardware.
• In addition to logic values, strength levels are often used to
resolve conflicts between drivers of different strengths in
digital circuits.
• Value levels 0 and 1 can have the strength levels listed in Table .
Data Types
Strength Levels:
• If two signals of unequal strengths are driven on a
wire, the stronger signal prevails.
Ex: If two signals of strength strong1 and weak0
contend, the result is resolved as a strong1.
• If two signals of equal strengths are driven on a
wire, the result is unknown.
Ex: If two signals of strength strong1 and strong0
conflict, the result is an X.
Data Types
• Strength levels are particularly useful for
accurate modeling of signal contention, MOS
devices, dynamic MOS, and other low-level
devices.
• Only trireg nets can have storage strengths
large, medium, and small.
Data Types
Nets
• Nets represent connections between hardware
elements.
• Just as in real circuits, nets have values
continuously driven on them by the outputs of
devices that they are connected to.
•Net ‘a’ is connected to the output of
‘and’ gate g1.
•Net ‘a’ will continuously assume the
value computed at the output of gate g1,
which is computed as b & c.
Data Types
Nets (Contd…)
• Net is not a keyword but represents a class of
data types such as wire, wand,wor, tri, triand,
trior, trireg, etc.
• Nets are declared primarily with the keyword
wire.
• Nets are one-bit values by default, unless they
are declared explicitly as vectors.
• The default value of a net is z (except the trireg
net, which defaults to x).
• Nets get the output value of their drivers. If a net
has no driver, it gets the value z.
Data Types
Nets (Contd…)
Examples:
• wire a; // Declare net a
• wire b; // Declare net b
• Wire a,b; // declare two nets a and b in the same line
• wire d = 1'b0; /* Net d is fixed to logic value 0 at
declaration.*/
Data Types
Registers
• Registers represent data storage elements.
• Registers retain value until another value is
placed onto them.
• In Verilog, the term register merely means a
variable that can hold a value and not a
hardware register.
Data Types
Example of data types:
• reg reset; // declare a variable reset that can hold its value
Usage:
$display(p1, p2, p3,....., pn);
m1 (SR Latch)
n2
Design Hierarchy for SR Latch Simulation
• Stimulus, the top-level module is not instantiated
anywhere, it is called the root module.
• The identifiers defined in this module are q, qbar,
set, and reset.
• The root module instantiates m1, which is a
module of type SR_latch.
• The module m1 instantiates nand gates n1 and n2.
• Q, Qbar, S, and R are port signals in instance m1.
• Hierarchical name referencing assigns a unique
name to each identifier.
• To assign hierarchical names, use the module
name for root module and instance names for
all module instances below the root module.
Examples of Hierarchical Names:
stimulus stimulus.q
stimulus.qbar stimulus.set
stimulus.reset stimulus.m1
stimulus.m1.Q stimulus.m1.Qbar
stimulus.m1.S stimulus.m1.R
stimulus.n1 stimulus.n2
• Each identifier in the design is uniquely specified
by its hierarchical path name.
• To display the level of hierarchy, use the special
character %m in the $display task.
//Display the hierarchical name of instance p1
instantiated under the highest-level module called
top. No argument is required. This is a useful
feature
• $display("This string is displayed from %m level of
hierarchy");
• -- This string is displayed from top.p1 level of
hierarchy
Chapter 5. Gate-Level Modeling
Learning Objectives
• Identify logic gate primitives provided in Verilog.
• Understand instantiation of gates, gate symbols, and
truth tables for and/or and buf/not type gates.
• Understand how to construct a Verilog description from
the logic diagram of the circuit.
• Describe rise, fall, and turn-off delays in the gate-level
design.
• Explain min, max, and typ delays in the gate-level
design.
Gate Types
• A logic circuit can be designed by use of logic
gates.
• Verilog supports basic logic gates as predefined
primitives.
• These primitives are instantiated like modules
except that they are predefined in Verilog and do
not need a module definition.
• All logic circuits can be designed by using basic
gates.
• There are two classes of basic gates: and/or gates
and buf/not gates.
And/Or Gates
• And/or gates have one scalar output and multiple
scalar inputs.
• The first terminal in the list of gate terminals is an
output and the other terminals are inputs.
• The output of a gate is evaluated as soon as one
of the inputs changes.
• The and/or gates available in Verilog are
and or xor
nand nor xnor
Basic Gates
• These gates are instantiated to build logic circuits in
Verilog.
• For all instances, OUT is connected to the output out,
and IN1 and IN2 are connected to the two inputs i1 and
i2 of the gate primitives.
• Note that the instance name does not need to be
specified for primitives.
• This lets the designer instantiate hundreds of gates
without giving them a name.
• More than two inputs can be specified in a gate
instantiation.
• Gates with more than two inputs are instantiated by
simply adding more input ports in the gate instantiation.
• Verilog automatically instantiates the appropriate gate.
Gate Instantiation of And/Or Gates
wire OUT, IN1, IN2;
// basic gate instantiations.
and a1(OUT, IN1, IN2);
nand na1(OUT, IN1, IN2);
or or1(OUT, IN1, IN2);
nor nor1(OUT, IN1, IN2);
xor x1(OUT, IN1, IN2);
xnor nx1(OUT, IN1, IN2);
// More than two inputs; 3 input nand gate
nand na1_3inp(OUT, IN1, IN2, IN3);
// gate instantiation without instance name
and (OUT, IN1, IN2); // legal gate instantiation
Truth Tables for And/Or
Buf/Not Gates
• Buf/not gates have one scalar input and one
or more scalar outputs.
• The last terminal in the port list is connected
to the input.
• Other terminals are connected to the outputs.
• Two basic buf/not gate primitives are provided
in Verilog.
buf not
Notice that these gates can have multiple outputs
but exactly one input
• turn-off = min(rise,fall)
// Three delays
// if +mindelays, rise= 2 fall= 3 turn-off = 4
// if +typdelays, rise= 3 fall= 4 turn-off = 5
// if +maxdelays, rise= 4 fall= 5 turn-off = 6
1. When signals in1 and in2 go high at time 20, out goes to a high
10 time units later (time = 30).
2. When in1 goes low at 60, out changes to low at 70.
3. However, in1 changes to high at 80, but it goes down to low
before 10 time units have elapsed.
4. Hence, at the time of recomputation, 10 units after time 80,
in1 is 0. Thus, out gets the value 0. A pulse of width less than
the specified assignment delay is not propagated to the output.
2.Implicit Continuous Assignment Delay:
• An equivalent method is to use an implicit
continuous assignment to specify both a delay
and an assignment on the net.
Example:
//implicit continuous assignment delay
wire #10 out = in1 & in2;
• The declaration above has the same effect as
defining a wire out and declaring a continuous
assignment on out as shown below.
wire out;
assign #10 out = in1 & in2;
3.Net Declaration Delay:
• A delay can be specified on a net when it is declared
without putting a continuous assignment on the net.
• If a delay is specified on a net out, then any value change
applied to the net out is delayed accordingly.
//Net Delays
wire # 10 out;
assign out = in1 & in2;
//The above statement has the same effect as the following.
wire out;
assign #10 out = in1 & in2;
Expressions, Operators, and Operands
Expressions
• Expressions are constructs that combine
operators and operands to produce a result.
Examples of expressions:
1. a ^ b
2. addr1[20:17] + addr2[20:17]
3. in1 | in2
Operands:
• Operands can be constants, integers, real numbers, nets,
registers, times, bit-select (one bit of vector net or a
vector register), part-select (selected bits of the vector
net or register vector), and memories or function calls
Examples:
1. integer count, final_count;
final_count = count + 1;
//count is an integer operand
2. real a, b, c;
c = a - b;
//a and b are real operands
3. reg [15:0] reg1, reg2;
reg [3:0] reg_out;
reg_out = reg1[3:0] ^ reg2[3:0];
//reg1[3:0] and reg2[3:0]
//are part-select register operands
4. reg ret_value;
ret_value = calculate_parity(A,
B);//calculate_parity is a
//function type operand
Operators:
• Operators act on the operands to produce
desired results
Examples:
1. d1 && d2 // && is an operator on operands d1 and d2
2. !a[0] // ! is an operator on operand a[0]
3. B >> 1 // >> is an operator on operands B and 1
Operator Types:
Arithmetic Operators
• There are two types of arithmetic operators:
binary and unary.
Binary operators
• Binary arithmetic operators are multiply (*),
divide (/), add (+), subtract (-), power (**), and
modulus (%).
• Binary operators take two operands.
Examples:
A = 4'b0011; B = 4'b0100; // A and B are register
vectors
D = 6; E = 4; F=2// D and E are integers
1. A * B // Multiply A and B. Evaluates to 4'b1100
2. D / E // Divide D by E. Evaluates to 1. Truncates any
fractional part.
3. A + B // Add A and B. Evaluates to 4'b0111
4. B - A // Subtract A from B. Evaluates to 4'b0001
5. F = E ** F; //E to the power F, yields 16
• If any operand bit has a value x, then the
result of the entire expression is x.
• This seems intuitive because if an operand
value is not known precisely, the result should
be an unknown.
Example:
in1 = 4'b101x;
in2 = 4'b1010;
sum = in1 + in2; // sum will be evaluated to the value 4'bx
• Modulus operators produce the remainder
from the division of two numbers.
1. 13 % 3 // Evaluates to 1
2. 16 % 4 // Evaluates to 0
3. -7 % 2 // Evaluates to -1, takes sign of the first operand
4. 7 % -2 // Evaluates to +1, takes sign of the first operand
Unary operators:
• The operators + and - can also work as unary operators.
• They are used to specify the positive or negative sign of
the operand.
1. -4 // Negative 4
2. +5 // Positive 5
• Negative numbers are represented as 2's complement
internally in Verilog.
• It is advisable to use negative numbers only if the type
integer or real in expressions.
• Designers should avoid negative numbers of the type
<sss> '<base> <nnn> in expressions because they are
• converted to unsigned 2's complement numbers and
hence yield unexpected results.
1. //Advisable to use integer or real numbers
-10 / 5// Evaluates to -2
initial
m = 1'b0; //single statement; does not need to be
grouped
initial
begin
#5 a = 1'b1; //multiple statements; need to be grouped
#25 b = 1'b0;
end
initial
begin
#10 x = 1'b0;
#25 y = 1'b1;
end
Initial
#50 $finish;
endmodule
• The three initial statements start to execute in parallel
at time 0.
• If a delay #<delay> is seen before a statement, the
statement is executed <delay> time units after the
current simulation time.
Time statement executed
0 m = 1'b0;
5 a = 1'b1;
10 x = 1'b0;
30 b = 1'b0;
35 y = 1'b1;
50 $finish;
//Type 1 statements
if(!lock) buffer = data; //if lock=0 assign data to buffer
if(enable) out = in;//if enable=1 assign in to out
//Type 2 statements
if (number_queued < MAX_Q_DEPTH)
begin
data_queue = data;
number_queued = number_queued + 1;
end
else
$display("Queue Full. Try again");
//Type 3 statements
//Execute statements based on ALU control signal.
if (alu_control == 0)
y = x + z;
else if(alu_control == 1)
y = x - z;
else if(alu_control == 2)
y = x * z;
else
$display("Invalid ALU control signal");
Multiway Branching