Module 4 DSDV NOTES
Module 4 DSDV NOTES
Module-4
Introduction to Verilog: Structure of Verilog module, Operators, Data Types, Styles of Description. (Section
1.1 to 1.6.2, 1.6.4 (only Verilog), 2 of Text 3)
Verilog Data flow description: Highlights of Data flow description, Structure of Data flow description.
(Section 2.1 to 2.2 (only Verilog) of Text 3)
Dept of ECE,SSSE 1
Digital System Design Using Verilog BEC302
The Boolean function of the output of the adder is obtained from the truth table. The Boolean function of the
output is generated using minterms (where the output has a value of 1) or maxterms (where the output has a
value of 0). The Boolean function using minterms in the sum of products (SOP) form is
After minimization (aa = 0 and bb =0), the SOP and the POS yield identical Boolean functions. Figure 1.1a
shows the logic symbol of the half adder. Figure 1.1b shows the logic diagram of the half adder.
The name of the module in Listing 1.2 is a user-selected Half_adder. Verilog is case sensitive. Half_adder,
half_adder, and half_addEr are all different names. The name of the module should start with an alphabetical
Dept of ECE,SSSE 2
Digital System Design Using Verilog BEC302
letter and can include the special character underscore (_). The declaration of the module starts with the
predefined word module followed by the user-selected name. The names of the inputs and outputs (they are
called input and output ports) follow the same guidelines as the module’s name. They are written inside
parentheses separated by a comma. The parenthesis is followed by a semicolon. In Listing 1.2, a, b, S, and C
are the names of the inputs and outputs. The order of writing the input and output ports inside the
parentheses is irrelevant. We could have written the module statement as: module half_adder (S, C, a, b);
The semicolon (;) is a line separator. Carriage return here does not indicate a new statement, the semicolon
does. Following the module statement, the input and output port modes are declared. For example, the
statement input a; declares signal a as an input port. The type of the input and output port signals need not be
declared. The order of writing the inputs and outputs and their declaration is irrelevant. For example, the
inputs and outputs in Listing 1.2 can be written as:
module half_adder (a,b, S, C);
output S;
output C;
input a;
input b;
Also, more than one input or output could be entered on the same line by using a comma (,) to separate each
input or output as:
module half_adder (a,b, S, C);
output S, C;
input a, b;
Statements 1 and 2 in Listing 1.2 are signal assignment statements . In statement 1, the symbol ^ represents
an EXCULSIVE-OR operation; this symbol is called a logical operator . So, statement 1 describes the
relationship between S, a, and b as S = a xor b. In statement 2, the symbol & represents an AND logic; the
symbol is called a logical operator. So, statement 2 describes the relationship between C, a, and b as C = a
and
b. Accordingly, Listing 1.2 simulates a half adder. The double slash (//) is a comment command where a
comment can be entered. If the comment takes more than one line, a double slash or pair (/.............../) can
be
used. The module is concluded by the predefined word endmodule. Leaving blank lines is allowed in the
module; also, spaces between two words or at the beginning of the line are allowed.
Dept of ECE,SSSE 3
Digital System Design Using Verilog BEC302
identify behavioral, structural, or switch-level descriptions. Boolean function of S and C have been
implemented to describe the half adder; Listing 1.2 (Verilog).
2.Behavioral Description
A behavioral description models the system as to how the outputs behave with the inputs; usually, a
flowchart is used to show this behavior. In the half adder, the S output can be described as “1” if the inputs a
and b are not equal, otherwise S = “0,” . The output C can be described as acquiring a value of “1” only if
each input (a and b) is “1.” The HDL behavioral description is the one where the module (Verilog) contains
the predefined word always or initial . Behavioral description is usually used when the Boolean function or
the digital logic of the system is hard to obtain. Listing 1.3 shows a behavioural description of the output S
3.Structural Description
Structural description models the system as components or gates. This description is identified by the
presence of the gates construct such as and, or, and not in the module . For the half adder, Figure 1.1b is
used to write the structural code. Listing 1.4 shows a structural description for the half adder.
Verilog Description
module Half_adder1(a,b,S,C);
input a, b;
Dept of ECE,SSSE 4
Digital System Design Using Verilog BEC302
output S,C;
and a1(C,a,b);
//The above statement is AND gate
xor x1(S,a,b);
//The above statement is EXCLUSIVE-OR gate
endmodule
4.Switch-Level Description
The switch-level description is the lowest level of description. The system is described using switches or
transistors. Some of the Verilog predefined words used in the switch level description are nmos, pmos,
cmos, tranif0, tran, and tranif1. VHDL does not have built-in switch-level primitives, but a construct
package can be built to include such primitives.
Verilog Description
module invert(y,a);
input a;
output y;
supply1 vdd;
supply0 gnd;
pmos p1(y, vdd, a);
nmos n1(y, gnd, a);
/*The above two statement are using the two primitives pmos and nmos*/
endmodule
5.Mixed-Type Description
Mixed-type or mixed-style descriptions are those that use more than one type or style of the above-
mentioned descriptions. In fact, most of the descriptions of moderate to large systems are mixed. Some parts
of the system may be described using one type and others using other types of description.
6.Mixed-Language Description
The mixed-language description is a newly added tool to HDL description. The user now can write a module
in one language (VHDL or the other language). Listing 1.6 illustrates the mixed-language description. In this
Listing, inside Verilog module Full_Adder1, the VHDL entity HA is instantiated (imported). The
information given in that entity is now visible to the Verilog module.
Dept of ECE,SSSE 5
Digital System Design Using Verilog BEC302
Ports
A simple definition of ports can be stated as a communication means between the system to be described
and the environment.
Verilog ports can take one of the following three modes:
input: The port is only an input port. In any assignment statement, the port should appear only on
the right-hand side of the statement (i.e., the port is read).
output: The port is an output port. In contrast to VHDL, the Verilog output port can appear in either
side of the assignment statement.
inout: The port can be used as both an input and output. The inout port represents a bidirectional bus.
1.3 Operators
HDL has an extensive list of operators. Operators perform a wide variety of functions. These functions can be
classified as:
Logical such as AND, OR, and XOR.
Relational to express the relation between objects.These operators include equality, inequality, less
than, less than or equal, greater than, and greater than or equal.
Arithmetic such as addition, subtraction, multiplication, and division.
Shift to move the bits of an object in a certain direction, such as right or left
1.Logical Operators
These operators perform logical operations, such as AND, OR, NAND,NOR, NOT, and XOR. The operation
can be on two operands or on a single operand. The operand can be single or multiple bits.
Dept of ECE,SSSE 6
Digital System Design Using Verilog BEC302
Other types of logical operators include the Boolean logical operators. These operators operate on two
operands, and the result is in Boolean: 0 (false) or 1 (true). For example, consider the statement Z = X && Y
where && is the Boolean logical AND operator. If X = 1011 and Y = 0001, then Z = 1. If X = 1010 and Y =
0101, then Z = 0. Table 1.4 shows the Boolean logical operators.
The third type of logical operator is the reduction operator. Reduction operators operate on a single operand.
The result is in Boolean. For example, in the statement Y = &X, where & is the reduction AND operator,
and assuming X = 1010, then Y = (1 & 0 & 1 & 0) = 0. Table 1.5 shows the reduction logic operators.
Dept of ECE,SSSE 7
Digital System Design Using Verilog BEC302
2.Relational Operators
Relational operators are implemented to compare the values of two objects. The result returned by these
operators is in Boolean: false (0) or true (1).
For the equality operator (==) and inequality operator (!=), the result can be of type unknown (x) if any of
the operands include “don’t care,” “unknown (x),” or “high impedance z.” The following are examples of a
Verilog relational operators:
if (A == B) .…….
If the value of A or B contains one or more “don’t care” or z bits, the value of the expression is unknown.
Otherwise, if A is equal to B, the value of the expression is true (1). If A is not equal to B, the value of the
expression is false (0).
if (A === B)…..
This is a bit-by-bit comparison. A or B can include x or high impedance Z; the result is true (1) if all bits of
A match that of B. Otherwise, the result is false (0).
For the conditional operator “?” the format is:
3.Arithmetic Operators
Arithmetic operators can perform a wide variety of operations, such as addition, subtraction, multiplication,
and division.
Verilog Arithmetic Operators
For most operations, only one type of operation is expected for each operator. An example of an arithmetic
Verilog operator is the addition operator (+); the statement Y = (A + B) calculates the value of Y as the sum
of A and B. Table 1.9 shows the Verilog arithmetic operators.
Dept of ECE,SSSE 8
Digital System Design Using Verilog BEC302
Dept of ECE,SSSE 9
Digital System Design Using Verilog BEC302
1.Nets
Nets are declared by the predefined word wire. Nets have values that change continuously by the circuits
that are driving them. Verilog supports four values for nets, as shown in Table 1.13.
2.Register
Register, in contrast to nets, stores values until they are updated. Register, as its name suggests, represents
data-storage elements. Register is declared by the predefined word reg. Verilog supports four values for
register, as shown in Table 1.14.
Dept of ECE,SSSE 10
Digital System Design Using Verilog BEC302
3.Vectors
Vectors are multiple bits. A register or a net can be declared as a vector. Vectors are declared by brackets [ ].
Examples of vectors are:
wire [3:0] a = 4’b1010;
reg [7:0] total = 8’d12;
The first statement declares a net a. It has four bits, and its initial value is 1010 (b stands for bit). The second
statement declares a register total. Its size is eight bits, and its value is decimal 12 (d stands for decimal).
4.Integers
Integers are declared by the predefined word integer. An example of integer declaration is:
integer no_bits;
The above statement declares no_bits as an integer.
Real
Real (floating-point) numbers are declared with the predefined word real. Examples of real values are 2.4,
56.3, and 5e12. The value 5e12 is equal to 5 × 1012. The following statement declares the register weight as
real:
real weight;
5.Parameter
Parameter represents a global constant. It is declared by the predefined word parameter. The following is an
example of implementing parameters:
module compr_genr (X, Y, xgty, xlty, xeqy);
parameter N = 3;
input [N:0] X, Y;
output xgty, xlty, xeqy;
wire [N:0] sum, Yb;
To change the size of the inputs x and y, the size of the nets sum, and the size of net Yb to eight bits, the
value of N is changed to seven as: parameter N = 7
6.Arrays
Verilog, in contrast to VHDL, does not have a predefined word for array. Registers and integers can be
written as arrays. Consider the following statements:
parameter N = 4;
parameter M = 3;
reg signed [M:0] carry [0:N];
The above statements declare an array by the name carry. The array carry has five elements, and each
element is four bits. The four bits are in two’s complement form. For example, if the value of a certain
element is 1001, then it is equivalent to decimal –7. Arrays can be multidimensional.
Dept of ECE,SSSE 11
Digital System Design Using Verilog BEC302
Dept of ECE,SSSE 12
Digital System Design Using Verilog BEC302
Referring to Listing 2.1, the input and output signals are declared in the module as ports. In HDL, a signal
has to be declared before it can be used (although in Verilog, it is not necessarily needed if the signal is a
single bit). Accordingly, signals s1 and s2 have to be declared using the predefined word wire:
wire s1, s2;
By default, all ports in Verilog are assumed to be wires. The value of the wire is continuously changing with
changes in the device that is deriving it. For example, s1 is the output of the AND gate in Figure 2.1, and s1
is continuously updated as a or b changes. A signal-assignment statement is used to assign a value to a
signal. The left-hand side of the statement should be declared as a signal. The righthand side can be a signal,
a variable, or a constant. The operator for signal assignment is <= in VHDL or the predefined word assign in
Verilog. In Listing 2.1, statements 1, 2, and 3 are signal-assignment statements. Statements 1–3 need an
event to occur on its right-hand side to start execution. If no event occurred on any statement, this statement
would not be executed. An event is a change in the value of a signal or variable such as a change from 0 to 1
(from low to high) or from 1 to 0 (from high to low). The statement that receives an event first will be
executed first regardless of the order of its placement in the HDL code. If more than one statement has an
event at the same time, all of these statements will be executed concurrently (i.e., simultaneously).
Accordingly, statement 3, for example, could have been written before statement 1 in Listing 2.1, and the
order of execution would not be affected. The signal-assignment statement is executed in two phases:
calculation and assignment. If an event occurs on the right-hand side of a statement, then this side is
calculated at the time of the event; after calculation, the value obtained from the calculation is assigned to
the left-hand side, taking into consideration any timing information given in the statement. Consider Listing
2.1 and Figure 2.2. At T0, an event has occurred in signal a and signal b (both signals changed their value
from 0 to 1, which is an event). Accordingly, an event occurred in statement 1; the value of (a and b) is
calculated as (1 and 1 = 1).
Because no delay time is specified, the value 1 is assigned immediately to s1, changing s1 from 0 to 1.
Changing the value of s1 from 0 to 1 constitutes an event in s1 and in statement 3, which is executed as a
result of the event in its right-hand side. The right-hand side of statement 3 is calculated at T0 as (s1 [1] or
s2
[0] = 1). The value of 1 is assigned to y; all at T0 because no delay time is specified. At T1, there is event on
signals a (1 to 0), c (0 to 1), and d (0 to 1). Statements 1 and 2 will be executed concurrently because an
event occurred on their right-hand side. The right-hand side of statement 1 and 2 is calculated at T1 as (0
and 1 = 0) and (1 and 1 = 1); the value of 0 is assigned to s1, and the value of 1 is assigned to s2 at T1.
Changing the value of s1 and s2 constitutes an event on s1 and s2, which selects statement 3 for execution at
T1; statement
Dept of ECE,SSSE 13
Digital System Design Using Verilog BEC302
Dept of ECE,SSSE 14
Digital System Design Using Verilog BEC302
endmodule
Figure 2.3 shows the simulation waveform of Listing 2.2. Table 2.1 shows analysis of the waveform
according to Listing 2.2. At T0, an event occurred on signal a and signal b (both changed from 0 to 1). This
event will invoke execution of statement 1. The right-hand side (R.H.S) of statement1 is calculated at T0 as
(1 and 1 = 1). However, this value of 1 will not assigned to s1 at T0; rather, it will be assigned at T0 + 10 ns
= T1. The rest of Table 2.1 could be understood by following the same analysis that has been done above at
T0.
Dept of ECE,SSSE 15
Digital System Design Using Verilog BEC302
From Table 2.1, the worst total delay time between the input and the output of Figure 2.1, as expected, is 20
ns. It is to be noted that if a signal assignment statement did not specify a delay time, the assignment to its
left-hand side would occur after the default infinitesimally small delay time of D (delta) seconds. This
infinitesimally small time cannot be detected on the screen, and the delay time will look as if it is zero. In the
following several examples, data-flow descriptions are introduced.
FIGURE 2.4 K-maps for the minterms (m) for the Sum and Carryout.
The Boolean function of the Sum and Carryout can be obtained from K-maps as shown in Figure 2.4.
Sum = a bc + a b c + ab c + abc (2.3)
Carryout = ab + ac + bc (2.4)
The symbol diagram of the full adder is shown in Figure 2.5a. The logic diagram of a full adder based on
Equations 2.3 and 2.4 is shown in Figure 2.5b.
The full adder can be built from several existing logic components such as two half adders and multiplexers.
Building a full adder from two half adders is based on the following analysis. The full adder adds a plus b
plus c = carryout sum. If the addition is performed in two steps: a plus b = C1 S, and c plus S = C2 sum
(sum is the
Dept of ECE,SSSE 16
Digital System Design Using Verilog BEC302
sum of the three bits). C1 and C2 cannot have a value of 1 at the same time. If C1 has value of 1, then C2
has to be 0 and vice versa. For example, to add 1 plus 1 plus 1, divide the addition in two halves; the first
half is 1 plus 1 = 10, and the second half is 0 plus 1 = 1. The carryout will be (C1 or C2); in this example, it
is 1 and the sum = 1. Figure 2.6 shows the logic diagram
Listing 2.3 shows the HDL code for the full adder as shown in Figure 2.5. The code assumes no delay time.
The parenthesis in the code, as in C language, gives the highest priority to the expression within the
parenthesis and makes the code more readable.
LISTING 2.3 HDL Code of the Full Adder From Figure 2.5
Verilog description
module fulladder(a, b, c);
output Sum, Carryout;
input a, b, c;
assign Sum = (~ a & ~ b & c)|( ~ a & b & ~c)|( a & ~b & ~c)|( a & b & c) ;
assign Carryout = (a & b) | (a & c) | (b & c);
endmodule
Dept of ECE,SSSE 17
Digital System Design Using Verilog BEC302
Compare the Diff in Table 2.3 and the Sum in Table 2.2; they are identical, so the Boolean function of the
Diff is the same as the sum in Equation 2.3. For the Borrow, draw the K-map as shown in Figure 2.8.
If the enable (Gbar) is high (1), the output is low (0) regardless of the input. When Gbar is low (0), the
output is A if SEL is low (0), or the output is B if SEL is high (1). From Table 2.4, the Boolean function of
the output Y is: Y = (S1 and A and SEL) or (S1 and B and SEL); S1 is the invert of Gbar.
Figure 2.9a shows the logic symbol, and Figure 2.9b shows the gatelevel structure of the multiplexer.
Dept of ECE,SSSE 18
Digital System Design Using Verilog BEC302
Listing 2.4a shows the HDL code. To generate the code, follow Figure 2.9b. Propagation delay time for all
gates is assumed to be 7 ns. Because this is a data-flow description, the order in which the statements are
written in the code is irrelevant. For example, statement st6 could have been written at the very beginning
instead of statement st1. The logical operators in VHDL and (Verilog) implemented in this Listing are: OR
(|), AND (&), and NOT (~).
Verilog Description
module mux2x1 (A, B, SEL, Gbar, Y);
input A, B, SEL, Gbar;
output Y;
wire S1, S2, S3, S4, S5;
time dly = 7;
/* Assume 7 time units delay for all and, or, not operations. The delay here is expressed in simulation screen
units. */
assign # dly Y = S4 | S5; //st1
assign #dly S4 = A & S2 & S1; //st2
assign #dly S5 = B & S3 & S1; //st3
assign #dly S2 = ~ SEL; //st4
assign #dly S3 = ~ S2; //st5
assign #dly S1 = ~ Gbar; //st6
endmodule
Dept of ECE,SSSE 19
Digital System Design Using Verilog BEC302
LISTING 2.4b HDL Code of a 2x1 Multiplexer Using Verilog Conditional (?)
Dept of ECE,SSSE 20
Digital System Design Using Verilog BEC302
Listing 2.4 shows the HDL code of the decoder. Figure 2.12 shows the simulation waveform of the decoder.
Verilog description
module decoder2x4( a, b, D);
input a,b;
output [3:0]D;
assign D[0] = ~a & ~ b;
assign D[1] = a & ~ b;
assign D[2] = ~a & b;
Dept of ECE,SSSE 21
Digital System Design Using Verilog BEC302
Dept of ECE,SSSE 22