DSD Module 2
DSD Module 2
(5 hours)
• Introduction
• Hardware Description Languages
• Introduction to Verilog HDL
• Lexical Conventions
• Data types
• Ports and Modules
Content
• Operators
• Abstraction Levels.
• Gate Level Modelling,
• Data Flow Modelling,
• Behavioural level Modelling
• Test Bench.
Introduction
Digital IC’s are classified as
• Small Scale Integration Circuit (SSI).
• Medium Scale Integration.
• Large Scale Integration.
• Very Large Scale Integration (more than 1, 00, 000 transistors).
• Ultra Large Scale Integration.
• Smaller circuits can be verified using simple Breadboard connections.
• Highly impossible with LSI and higher integrated circuits.
• As the design becomes more and more difficult Computer Aided Design (CAD)
are evolved for the design and testing of Digital circuits.
• So CAD tools are more popular.
Introduction
• For a long time, programming languages such as FORTRAN, Pascal, and C were
being used to describe computer programs that were sequential in nature.
• Similarly, in the digital design field, designers felt the need for a standard
language to describe digital circuits. Thus, Hardware Description Languages
(HDLs) came into existence.
• Hardware description languages such as Verilog HDL and VHDL became
popular.
• Verilog HDL originated in 1983 at Gateway Design Automation. Later, VHDL
was developed under contract from DARPA.
• Both Verilog and VHDL simulators to simulate large digital circuits quickly
gained acceptance from designers.
Why HDL ???
Fast design and better verification are possible, using HDL.
Complex digital systems need more time for simulation, verification, synthesis, and debugging., these tasks are done
simultaneously using HDL, which will save time. The sequential execution of statements is possible in HDL.
The different parameters, such as power, delay, and area, can be extracted using HDL.
Using HDL, the designer can make the necessary engineering trade-offs and can develop the design in a better and
efficient way.
The testing of large circuits was not possible because the large circuits contain lots of gates, and the response of every
gate could not be checked. HDL has evolved the testing process easy and feasible.
The designer can develop HDL codes on hardware using PLDs, which saves time as well as cost.
The Top-down design and hierarchical design method allow the design time, design cost, and design errors to be
reduced.
HDL provides the timing information and allows the design to be described in the gate level and register transfer level.
HDL allows reusability of resources. Design reuse is the process of migrating high-quality intellectual property (IP)
from one ASIC design to another. Designs can be reused for variable-width implementations using unconstrained
arrays.
Using HDL, the designers can create tools that manipulate the design for synthesis, verification, and optimization, etc.
Introduction to Verilog HDL
• VERILOG HDL – VERIfiable LOGic HDL: Programming language used to
describe digital circuits and systems.
• HDL is a language used to describe digital system.
• The goal of an HDL is to describe and model digital systems faithfully and
accurately.
• Different HDLs are available different purpose such as Analog system design,
digital system design, and PCB design.
• The two main HDLs are Verilog and VHDL for digital system design.
• There are other, limited capability, languages such as ABEL, CUPL, and
PALASM that are tailored specifically for PALs and CPLDs.
Introduction to Verilog HDL
• Verilog Simulators:
• Commercial Simulators:
• Xilinx ISE
• Active HDL • Open Source Simulators:
• Model Sim • Verilator
• Quartus-II • Icarus Verilog
• MP Sim • GPL cver
• VeriWell
• Online Simulators:
• iverilog.com
• EDAPlayground.com
• easyeda.com
Verilog Program-Format
• All components except module, module name, and endmodule are optional and
can be mixed and matched as per design needs.
• Verilog allows multiple modules to be defined in a single file.
• The modules can be defined in any order in the file.
Module Name,
Port List, Port Declarations (if ports present)
Parameters (optional)
endmodule statement
Module-Introduction
• Verilog uses the concept called Module.
• A module is the basic building block in Verilog.
• A module can be an element or a collection of lower-level design blocks.
• Typically, elements are grouped into modules to provide common functionality
that is used at many places in the design.
• A module provides the necessary functionality to the higher-level block
through its port interface (inputs and outputs), but hides the internal
implementation.
• This allows the designer to modify module internals without affecting the rest of
the design.
• Each module must have a name.
• Modules are designated with a key word “Module”.
Module-Introduction
• A module in Verilog consists of distinct parts.
• A module definition always begins with the keyword module and must
completed with the keyword endmodule.
• The module name, port list, port declarations, and optional parameters must come
first in a module definition.
• Port list and port declarations are present only if the module has any ports to
interact with the external environment.
• The five components within a module are - variable declarations, dataflow
statements, instantiation of lower modules, behavioral blocks, and tasks or
functions.
• These components can be in any order and at any place in the module definition.
Module Instances
• A module provides a template from which you can create actual objects.
• When a module is invoked, Verilog creates a unique object from the template.
• Each object has its own name, variables, parameters and I/ O interface.
• The process of creating objects from a module template is called
instantiation, and the objects are called instances.
Module-Format
• Example:
• net is not a keyword but represents a class of data types such as wire, wand, wor,
tri, triand, trior, trireg, etc.
• The wire declaration is used most frequently.
reg reset; / / declare a variable reset that can hold its value
initial / / this construct will be discussed later
begin
reset = 1’b1; //initialize reset to 1 to reset the digital circuit.
#100 reset = 1’b0; // after 100 time units reset is deasserted.
end
Data Types-Vectors
• Nets or reg data types can be declared as vectors (multiple bit widths).
• If bit width is not specified, the default is scalar (l-bit).
• Vectors can be declared at [high#:low#] or [low# :high#], but the left number in
the squared brackets is always the most significant bit of the vector.
2. Dataflow level:
• At this level the module is designed by specifying the data flow.
• The designer is aware of how data flows between hardware registers and
how the data is processed in the design.
Abstraction levels
3. Gate level:
• 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.
3. 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 implementation
details.
Switch Level Modeling
• Switch level modeling is the lowest level of abstraction provided by Verilog. It consists of
PMOS and NMOS switches.
• A module can be implemented in terms of transistors, switches, storage nodes, and the
interconnections between them.
• In Verilog, HDL transistors are known as Switches that can either conduct or open.
• Design at this level requires knowledge of switch-level implementation details.
• Format for PMOS, NMOS and CMOS are:
nmos (out, data, ncontrol);
pmos (out, data, pcontrol);
cmos (out, data, ncontrol, pcontrol);
module my_nor(Y,A,B);
output Y;
input A,B;
wire C;
supply1 pwr;
supply0 gnd ;
pmos (C, pwr, A);
pmos (Y,C,B);
nmos (Y, gnd, A);
nmos (Y, gnd, B);
NOR gate using CMOS Logic
endmodule
Structural Modelling
• At gate level, the circuit is described in terms of gates. (e.g., and,
nand…)
• Hardware design at this level is intuitive for a user with a basic knowledge
of digital logic design.
• It is possible to see a one-to-one correspondence between the logic circuit
diagram and the Verilog description.
• Actually, the lowest level of abstraction is switch- (transistor-) level
modeling.
• However, with designs getting very complex, very few hardware designers
work at switch level.
• So most of the digital design is now done at gate level or higher levels of
abstraction.
Structural Modelling
• All logic circuits can be designed by using basic 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.
• Logic gates can be used in design using gate instantiation.
Structural Modelling
• The Syntax is
Gate_type [instance_name] (term1, term2,……………..termn);
• Example
• Implicit net declaration – if a signal name of the left hand side of the
continuous assignment statement is not declared the Verilog simulator
assign an implicit net declaration for the net
Dataflow Modelling
• Regular assignment delay – the assignment takes effect with the time delay
of 2 time steps. If the values of ‘a’ and ‘b’ changes then ‘c’ wait for two
time steps to compute the result.
x1
Structural Dataflow Behavioral
Modeling Modeling Modeling
a1
module ha(S,C,A,B); module ha(S,C,A,B); module ha(S,C,A,B);
output S,C; output S,C; output S,C;
input A,B; input A,B; input A,B;
xor x1(S,A,B); assign S=A^B; reg S,C;
and a1(C,A,B); assign C=A&B; always @(A,B)
endmodule endmodule begin
S=A^B;
C=A&B;
end
Endmodule
Testbench
Simulation Result
Test Bench
• We need to apply appropriate stimulus to the design to test it.
• Stimulus is nothing but the application of various permutations and
combinations of inputs at various points of time and, looking for correct results
produced by the design.
• This can be done by writing another Verilog code called Test Bench.
• Test bench used to simulate your design without physical hardware.
• This is written as a separate file, different from the design file(s).
• The biggest benefit of this is that you can actually inspect every signal that is in
your design.
Test Bench - Structure
module test_module_name;
//declare local reg and wire identifiers.
//instantiate the design module under test
//generate stimulus (inputs to design module) using initial and always.
// display output response.(display on screen or print)
endmodule
Test Bench - Example
Test Bench-Example
module HalfTB(); //the test bench doesn’t have any port
reg p,q;
wire sum,carry;
Half H1(.a(p),.b(q),.s(sum),.c(carry)); //Module instantiation
initial
begin
p=1’b0; q=1’b0;
#2 p=1’b0; q=1’b1;
#2 p=1’b1; q=1’b0;
#2 p=1’b1; q=1’b1;
#2 $stop;
end
endmodule
Verilog Program for Full Adder using Half Adder
// Test bench module:
// Main Module module FullTB();
module Full(A,B,Cin,Sum,Carry); reg a,b,cin;
input A,B,Cin; wire s, cout;
output Sum, Carry; Full f1(.A(a),.B(b),.Cin(cin),.Sum(s),.Carry(cout));
wire s0,c0,c1; initial
Half H1(A,B,s0,c0); begin
Half H2(s0,Cin,s,c1); a=1’b0; b=1’b0;cin=1’b0;
or o1(cout,c0,c1); #2 a=1’b0; b=1’b1; cin=1’b0;
endmodule #2 a=1’b1; b=1’b0; cin=1’b0;
#2 a=1’b1; b=1’b1; cin=1’b1;
#2 $stop;
end
endmodule
Behavioral Code of Half Subtractor
Test bench module:
module HalfSubTB(); //the test bench doesn’t have any port
Main module:
reg p,q;
module HalfSub(a,b,d,bo);
wire diff,borrow;
input a,b;
HalfSub H1(p,q, diff,borrow); //Module instantiation; here H1 is the instance name
output reg d,bo;
initial
always @(*)
begin
begin
p=1’b0; q=1’b0;
d=a ^ b;
#2 p=1’b0; q=1’b1;
bo=(~a) & b;
#2 p=1’b1; q=1’b0;
end
#2 p=1’b1; q=1’b1;
endmodule
#2 $stop;
end
endmodule
Verilog Program for MUX
// Main Module (STRUCTURAL)
// Main Module (DATAFLOW) // Main Module (DATAFLOW) module mux21(Y,A,B,S);
module mux21(Y,A,B,S); module mux21(Y,A,B,S); output Y;
input A, B, S;
output Y; output Y;
wire SA, SB,N;
input A, B, S; input A, B, S; and g1(SB, S, B);
assign Y = (S & ~B) | (~S & A); assign Y = (S == 1'b0) ? A : not g2(N, S);
endmodule (S == 1'b1) ? B : 1'bx ; and g3(SA, A, N);
endmodule or g4(Y,SA, SB);
endmodule
B SB
g1
S
g2 N g4
A
g3
SA
// Main Module (BEHAVIOURAL)
module mux21(Y,A,B,S); // Main Module (BEHAVIOURAL)
output Y; module mux21(Y,A,B,S);
input A, B, S; output Y;
always @(*) input A, B, S;
begin always @(*)
if (S == 0) begin
begin Y = A; if (S == 0)
end begin Y = A;
else end
begin Y = B; else
B
end begin Y = B;
end end
endmodule end
endmodule
A
Verilog Program for MUX // Testbench Module
module mux21TB();
// Main Module reg sel, a, b;
module mux21(Y,A,B,S); wire y;
output Y; mux21 m1 (.S(sel), .A(a), .B(b), .Y(y));
input A, B, S; initial
assign Y = (S & ~B) | (~S & A);
begin
endmodule
$monitor("S=%b, A=%b, B=%b, Y=%b", S, A, B, Y);
B
// Test case 1: S=0, A=1, B=0
S = 0; A = 1; B = 0; #10;
// Test case 2: S=1, A=0, B=1
S = 1; A = 0; B = 1; #10;
A
// Test case 3: S=0, A=0, B=1
S = 0; A = 0; B = 1; #10;
// Test case 4: S=1, A=1, B=0
S = 1; A = 1; B = 0; #10;
$finish;
end
endmodule