Module-4 DSD 2022 Scheme
Module-4 DSD 2022 Scheme
MODULE-4
INTRODUCTION TO VERILOG
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 1
Module-4 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
// declaration:
input , output, reg, wire, parameter,
inout;
functions, tasks;
// statements or body
Initial statement
always statement
module instantiation
continuous assignment
endmodule
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 2
Module-4 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
Verilog ports
input: the port is only an input port. In any assignment statement, the port should appear only
on the right hand side of the assignment statement.(i.e., port is read.)
output: the port is an output port. The Verilog output port can appear on 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.
Operators
HDL has an extensive list of operators. Operator performs a wide variety of functions.
Functions classified
1. Logical operators such as and, or, nand, nor, xor, xnor and not
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 3
Module-4 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
2. Relational operators: to express the relation between objects. The operators include =,
/=, <, <=, >and >=.
3. Arithmetic operators: such as +, -, * and division.
4. Shifts operators: To move the bits of an object in a certain direction such as
right or left.
Logical operators
These operator performs Logical operations, such as and, or, nand, nor, xor, xnor, and not.
The operation can be on two operands or on a single operand. The operand can be single bit
or multiple bits.
Verilog logical operators
Verilog logical operator can be classified as Bitwise, Boolean logical and reduction
logical operators.
Bitwise Logical Operators
The bitwise operators operate on the corresponding bits of two operands.
Verilog operator
Equivalent logic Operand type Result type
(bitwise)
| Bit Bit
^ Bit Bit
~^ Bit Bit
~ Bit Bit
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 4
Module-4 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
Example: z= x & y, if x=1011 and y=1010 are 4-bit signals then z=1010 is logical and
operation of x and y.
Boolean Logical Operators
Boolean logical operators operate on the two operands. The result is Boolean true (1) or false
(0). These are shown in table 1.2
Table 1.2 Boolean operators
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 5
Module-4 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
Example: if (A==B), if the values of A or B contains one or more don’t care or Z bits. The
value of the expression is unknown.
If A is equal to B, then result of the expression (A==B) is true (1).
If A is not equal to B, then result of the expression (A==B) is false (0).
Verilog Arithmetic operators
Arithmetic operators can perform a wide variety of operation, such as addition, subtraction,
multiplication and division.
Table 1.5 Verilog arithmetic operator
Operators Description A or B type Y type
Addition A numeric
+ Numeric
A+B B numeric
Subtraction A numeric
- Numeric
A-B B numeric
Multiplication A numeric
* Numeric
A?B B numeric
division A numeric
/ Numeric
A/B B numeric
A numeric, not real
Modulus
% B numeric, Numeric, not real
A%B
not real
Exponent A numeric
** Numeric
A**B B numeric
Concatenation A numeric, or array
{,} Same as A
{A,B} B numeric, or array
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 6
Module-4 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
Data types
The data or operands used in the language must have several types to match the need for
describing the hardware.
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 7
Module-4 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
Parameters:
It represents global constants. Declared by the predefined word “parameter”
Eg. Module comp_genr (x, y, xgty, xlty, xeqy);
Parameter N=3;
input [n:0] x,y;
output xgty, xlty, xeqy;
Wire [N:0] sum, xb;
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 8
Module-4 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
Arrays: there is no predefined word “array”. Registers and integers can be used as arrays.
Parameter N=4;
Parameter M=3;
reg signed [M: 0] carry [0:N]
reg [M: 0] b [0: N];
integer sum [0: N];
The above statement declares an array by the name sum. It has 5 elements, and each element is
an integer type. Array carry has 5 elements, and each elements is 4bits. They are in 2’S
complement formThe array b has 5 elements, each element is 4 bits. The value of each bit
can be 0, 1, X or Z;
Styles (Types) of Description
1.Data Flow Description
Data flow describes how the system’s signals flow from the inputs to the outputs. Usually,
the description is done by writing the Boolean function of the outputs. The data-flow
statements are concurrent; their execution is controlled by events.
Example: Refer Lab Record
2. Behavioral Descriptions
A behavioral description models the system as to how the outputs be have with the inputs.
HDL behavioral description is the one where the Verilog module contains the predefined
word always or initial.
Example: Refer Lab Record & Class Notes
3. Structural Description
Structural description models the system as gates. This description is identified by the
presence of the keyword gates construct such as and, or, and not in the module.
Example: Refer Lab Record & Class Notes
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.
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 9
Module-4 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
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.
module halfadder (a,b,sum,carry);
input a,b;
output sum,carry;
reg carry;
assign sum=a^b;
always@(a,b)
begin
carry=a&b;
end
endmodule
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 Verilog) and invoke or import a construct (entity
or module) written in the other language.
module halfadder (a,b,sum,carry);
input a,b;
output sum,carry;
assign sum=a^b;
assign carry=a&b;
endmodule
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 10
Module-4 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity halfadder is
port(a,b: in bit;
sum,carry: out bit);
end halfadder;
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 11
Module-4 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 12
Module-4 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
written as:
y = s1 + s2; where s1=ab and s2 =cd
The Boolean function of y could be written as:
Y = ab + cd
Verilog description
module andor (a,b,c,d, y );
input a,b,c,d;
output y;
wire s1, s2;
assign s1 = a & b; //statement 1.
assign s2 = c & d; //statement 2.
assign y = s1 | s2; //statement 3.
Endmodule
constant. The operator for signal assignment is the predefined word assign in Verilog.
The signal-assignment statement is executed in two phases: calculation and assignment.
Calculation: If an event occurs on the right-hand side of a statement, then this side is
calculated at the time of the event.
Assignment: 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. For
example
assign #10 y = s1 | s2;
Constant Declaration and Assignment Statements
A constant in HDL is treated as it is in C language; its value is constant within the segment of
the program where it is visible. A constant in Verilog can be declared by its type such as time
or integer
time period; // Verilog
To assign a value to a constant, use the assignment operator = in Verilog
period = 100; // Verilog
In the above Verilog statement, there are no explicit units of time; 100 means 100 simulation
screen time units. If the simulation screen time units are defined as nanoseconds (ns), for
example, then 100 will mean 100 nanoseconds. The declaration and assignment can be
combined in one statement as:
time period = 100 //Verilog
Assigning a Delay Time to the Signal-Assignment Statement
To assign a delay time to a signal-assignment statement, the # symbol is used in Verilog. For
example, the following statement assigns a 10 ns delay time to signal S1:
assign #10 S1 = a & b // Verilog
EXAMPLE- 2x1 MULTIPLEXER WITH ACTIVE LOW ENABLE
A 2x1 multiplexer is a combinational circuit; it has two one-bit inputs, a one-bit select line,
and a one-bit output. Additional control signals may be added, such as enable. The output of
the basic multiplexer depends on the level of the select line. If the select is high (1), the
output is equal to one of the two inputs. If the select is low (0), the output is equal to the
other input. A truth table for a 2x1 multiplexer with active low enable is shown in Table 1.9.
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 14
Module-4 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
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
1.9, the Boolean function of the output Y is:
Y = (S1 and A and ̅̅̅̅̅ ) or (S1 and B and SEL);S1 is the invert of Gbar
Figure 1.3a shows the logic symbol, and Figure 1.3b shows the gatelevel structure of the
multiplexer
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 15
Module-4 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 16
Module-4 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 17