3.module 2 24.1.25
3.module 2 24.1.25
(5 hours)
2
3
Introduction
Digital IC’s are classified as
• Small Scale Integration Circuit (SSI).
• Medium Scale Integration.(MSI)
• Large Scale Integration.(LSI)
• Very Large Scale Integration(VLSI)
• Ultra Large Scale Integration.(ULSI)
• 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.
4
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.
• Different HDLs are available different purpose such as Analog system design, digital
system design, and PCB design.
• Hardware description languages such as Verilog HDL and VHDL became popular.
• Purpose of VHDL and Verilog HDL are same but they differ in their syntax and the
levels of circuit descriptions.
• VERILOG HDL – VERIfiable LOGic HDL: Programming language used to
describe digital circuits and systems..
• File Extension .v
6
Difference Between HDL and Programming
Language
HDL Software/programming language
•Allows both sequential and concurrent •Can only handle sequential instructions
executions.
• Successful VHDL programmer needs •Successfully written with pure logical or
thorough working knowledge of the algorithmic thinking
hardware circuits
•Memory and other logic elements are • Don't care about resource usage in C.
limited in a FPGA.
7
VERILOG HDL VHDL
• VERIfiable LOGarithmic HDL • Very high speed integrated circuit HDL
• Developed based on C-language • Developed based on ADA-language
• Case sensitive • No case sensitive
• It allows switch-level modelling • switch-level modelling is not possible
• Very simple data types • Complex data types
• User defined data types are not allowed • User defined data types are allowed
• Unary reduction operators are available • Unary reduction operators not available
• Multi-dimensional array not supported • Multi-dimensional array supported
• Code is simplified using procedure • Code is simplified using task
• Design composed of modules • Design composed of entities
• All signals are initialized to “unknown” state • All signals are initialized to “zero” state
8
Simulators for 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:
• [Link]
• [Link]
• [Link]
9
Simulation Vs Synthesis
In Verilog, simulation is the process of testing a system on a computer, while synthesis is the process of
converting the code into a hardware circuit
Simulation
•Tests a system on a computer before it's turned into hardware
•Helps identify potential issues and understand the system's behavior
•Can be faster than synthesis for small designs
•Can be used to debug a design by tracing every wire in the design
Synthesis
•Converts Verilog code into a hardware circuit
•Optimizes the design for performance by considering timing constraints
•Creates a circuit netlist, which is a set of logic gates and wiring
Why simulation and synthesis are important
•Simulation and synthesis work together to ensure that digital circuits are functional, perform well, and
are successfully realized
•Simulation can help identify issues before the hardware is created
•Synthesis can help optimize the design for performance
10
Design Process
11
Design Methodologies
There are 2 types of design methodologies:
1. Top-down design methodology
2. Bottom-up design methodology
✓ Top-down design : First define the top-level block and identify the sub-blocks necessary
to build the top-level block.
✓ Bottom-up design : First identify the building blocks that are available to us then build
bigger cells, using these building blocks.
12
Top Down Design
13
Bottom Up Design
14
Verilog Program- Format
Sum = A ⊕ B
Carry =𝑨𝑩
1. Module
2. Port Declaration
3. Port directions
4. Output logic
A. Gate level modelling - Uses various gate primitives, logic diagram
must be known
B. Data flow modelling - Uses assignment statement and operators,
logical expression must be known
C. Behavioural modelling - Uses procedural & conditional statements,
circuit behaviour described as Truth table is known
D. Switch level modelling - Uses various transistor level primitives,
CMOS logic diagram must be known 15
AND GATE -Design
Behavioral Model
Structural/Gate Level Data flow Model module andgate (y,a,b);
module andgate (y,a,b); module andgate (y,a,b); input a,b;
input a,b; input a,b; output y;
output y; output y; reg y;
and (y,a,b); assign y = a & b; always @ (a or b)
endmodule endmodule begin
if (a == 1 && b == 1)
ex: and A1(out,in1,in2); // if one y = in1 & in2 & in3; //and gate
‘AND’ gate is using A1 is not needed. If we use
y = 1;
y = in1 | in2; // or gate
multiple ‘AND’ gates identifiers are needed. else
Name of the gate id user defined (user only
assigns some specifi name).-- Don’t maintain
y = ~in1 // not gate y = 0;
the same name more than one time
y = ~ (in1 & in2) // Nand gate end
or A2(out,in1,in2);
not A3(out,in1); y = ~(in1 | in2); // Nor gate endmodule
xor A4(out,in1,in2);
y = in1 ^ in2 // xor gate
nand A4(out,in1,in2);
nor (out,in1,in2); y = ~(in1 ^ in2); //Xnor gate
16
xnor A5(out,in1,in2);
//TESTBENCH
module andgate_tb;
reg a,b;
Structural/Gate Level wire y;
andgate dut(y,a,b);
module andgate(y,a,b);
initial begin
input a,b;
a=0; b=0; //This is input a=0,b=0
output y;
#10;
and (y,a,b);
a=0; b=1; //This is input a=0,b=1
endmodule
#10;
a=1; b=0; //This is input a=1,b=0
#10;
a=1; b=1; //This is input a=1,b=1
#10;
end
initial begin
$monitor(“a=%b, b=%b, y=%b",a,b,y);
//if any changes in variables include inside monitor, it takes effect
// $monitor("name=%d",variable_name)
end
endmodule
17
Half adder-Design
Behavioral Model
Structural/Gate Level Data flow Model module half_adder (sum,carry, a,b);
module half_adder (sum,carry, a,b); output sum,carry;
module half_adder (sum,carry, a,b);
output sum,carry; input a,b;
output sum,carry;
input a,b; input a,b; reg sum,carry;
xor(sum,a,b); assign sum=a ^ b; always @ (a or b)
and(carry,a,b); assign carry =a&b; begin
endmodule endmodule if (a == 0 && b == 0)
sum = 0; carry =0;
elseif (a == 0 && b == 1)
sum = 1; carry =0;
elseif (a == 1 && b == 0)
sum = 1; carry =0;
elseif (a == 1 && b == 1)
Sum = A ⊕ B sum = 0; carry =1;
Carry =𝑨𝑩 end
endmodule
18
Half adder-Design // Half_adder_tb
module half_adder _tb;
reg a;
reg b;
wire sum;
wire carry;
half_adder dut (sum,carry, a,b);
initial begin
a=1'b0; b=1'b0; // This is input a=0,b=0
#10 a=1'b0; b=1'b1; // This is input a=0,b=1
#10 a=1'b1; b=1'b0; // This is input a=1,b=0
#10 a=1'b1; b=1'b1; // This is input a=1,b=1
#10 $stop;
end
initial begin
$monitor($time, "a=%b, b=%b, sum=%b,carry=%b", a,b,sum,carry);
end
endmodule
19
Verilog Program- Format : Module
• 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.
20
Verilog Program-Format
• Example:
21
A sample Verilog HDL Program
• Structure of Verilog Program:
22
PORTS
Verilog Type of Port
Keyword
input Input Port
output Output Port
inout Bidirectional Port
26
Lexical Conventions- Keywords
• Keywords are predefined, special identifiers that define the language
constructs.
• All key words defined in lower case.
• Some of them are
• input, output, inout
• reg, wire, task, tri, time,
• pmos, nmos, and, or, not, wor, xnor,
• begin, parameter, buf, edge, join, medium, wait, use
• if, else, case, while, always, initial, posedge, negedge, assign
• end, endtask, endcase, endfunction, default, endtable, endmodule.
27
Lexical Conventions-Identifier
• Identifiers are name given to objects so that they can be referenced in the
design.
• Identifiers are case sensitive
• Mymodule ≠ mymodule Identifier
• Formed from {[A-Z], [a-z], [0-9], _, $}, but .. .. can’t begin with $ or [0-9].
• Identifiers cannot be Verilog keywords or system functions.
• Examples:
• andgatepr →valid
• ANDGATEPR →valid module andgate (y,a,b);
input a,b;
• and_gate →valid output y;
and (y,a,b);
• and →invalid ( ‘and is a key word) endmodule
• 3and_gate →invalid (in starting number is not allowed)
• $and_gate →invalid (in starting $ symbol is not allowed
• _and_gate →valid 28
Lexical Conventions-Comments
• Two types of comments possible.
• Single Line Comment (\\).
• Example:
• A= x^y; // this is single line comment
• Multiple Line Comment (/*……*/).
• Example:
• /* this is example of multiline comment
A= x^y;
C=a+b; */
• Nesting of Comments not possible.
29
Value Levels // Half_adder_tb
module
reg a;
reg b;
wire sum;
wire carry;
half_adder dut (sum,carry, a,b);
initial begin
a=1'b0; b=1'b0; // This is input a=0,b=0
#10 a=1'b0; b=1'b1; // This is input a=0,b=1
#10 a=1'b1; b=1'b0; // This is input a=1,b=0
#10 a=1'b1; b=1'b1; // This is input a=1,b=1
#10 $stop;
end
initial begin
$monitor($time, "a=%b, b=%b, sum=%b,carry=%b",
a,b,sum,carry);
end
endmodule
30
Logical (0,1,x,z) Operation
Verilog Number
Format:
<number of bit><base><number>
<base> ‘b, ‘B, ‘d, ‘D, ‘o, ‘O, ‘h, ‘H. Default is ‘d
25
31
Representation of Number in Verilog
This field signifies the value of the number. For binary numbers, the characters 0, 1, x, z can be used to
form the value.
For octal numbers, the numerals 0 to 7, x, z can be used to form the value.
For decimal numbers, all the numerals, x, z can be used to form the value.
For hex numbers, all the numerals, a, b, c, d, e, f, x, z can be used to form the numbers.
This combination - the single quote character followed by b, o, d, or h - specifies the base of the
number. The character signifies binary, octal, decimal, or hexadecimal base. If this field is absent,
the number is taken as a decimal one.
If present, the decimal number in this field signifies the bit width of the number. If absent, the width
is assigned a default value by the compiler.
This field (optional) is for the sign bit. It is allowed only with the decimal numbers. If absent, the
number is taken as positive. For a number with a negative sign, the number is represented in 2's
complement form.
32
Lexical Conventions-Numbers
• Number Specification allowed are
• Sized numbers
• Unsized numbers
• Unknown and high-impedance values
• Negative numbers
• Underscore character and question marks used to improve readability.
• Not allowed as the first character.
• Examples:
• 12’b1111_0000_1010
• – ‘?’is the same as ‘z’ (only regarding numbers)
• 4’b10?? // the same as 4’b10zz
33
Lexical Conventions-Sized Numbers
Examples:
34
Lexical Conventions-Unsized Numbers
• Size of Number is not specified. Default size is at least 32 (depends on
Verilog compiler).
• Default base is decimal.
• Examples:
• Negative numbers can be used with negative sign (-) before size.
• Examples:
• -6’d35
• 6’d-35 // illegal
35
Lexical Conventions-Unsized Numbers
X or Z values:
• Unknown value: lowercase x or Uppercase X
• 4 bits in hex, 3 bits in octal, 1 bit in binary.
• High-impedance value: lowercase z or Uppercase Z
• 4 bits in hex, 3 bits in octal, 1 bit in binary.
• Examples:
• 12’h1a3x
• 4’hX
➢ Extending the most-significant part
37
Data Types
Data types are mainly used to declare identifiers/variables
• The Data types used in Verilog are
• Value Set
• Nets
• Registers
• Vectors
• Integer, Real, and Time Registers
• Arrays
• Memories
• Parameters
• Strings
38
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.
• Nets are declared primarily with the keyword wire.
• Nets are one-bit values by default unless they are declared explicitly as vectors.
• The terms wire and net are often used interchangeably.
• 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.
40
wire
41
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.
• The default value for a reg data type is x.
• Unlike a net, a register does not need a driver.
• Verilog registers do not need a clock as hardware
registers do.
• Values of registers can be changed anytime in a
simulation by assigning a new value to the
register.
• Register data types are commonly declared by the
keyword reg. 43
Data Types-Vectors
• Examples: wire b; //scalar net variable
wire[7:0] databus; //8-bit data bus
• Represent buses
• wire[3:0] busA;
• reg [1:4] busB;
• reg [1:0] busC;
• Left number is always MSB.
• Vector assignment by position also possible.
• busB[1] = busA[3];
• busB[2] = busA[2];
• busB[3] = busA[1];
• busB[4] = busA[0];
46
Data Types-Time
▪ Used to Store the simulation time.
▪ Keyword – time.
50
Data Types-Usage restrictions
Data Flow and Structural Modeling
• Can use only wire data type.
• Cannot use reg data type.
Behavioral Modeling
• Can use only reg data type (within initial and always constructs).
• Cannot use wire data type. (within initial and always constructs).
55
OPERATORS
➢ Operators are used in expressions to produce values from operands.
56
Operators
Many types of operators are supported by Verilog HDL. They are
• Arithmetic Operators.
• Logical Operators.
• Relational Operators.
• Equality Operators.
• Bitwise Operators.
• Reduction Operators.
• Shift Operators.
• Concatenation Operators.
• Conditional Operators.
57
58
OPERATORS
ARITHMETIC OPERATORS
➢ Arithmetic operators: Unary:(+, -), Binary: (+, -, *, /, %)
59
59
OPERATORS
BITWISE OPERATORS
➢ This operator compares each bit in one operand to its equivalent bit in
the other operand to calculate one bit for the result
➢ If operands are of unequal, the shorter operand is zero-filled in MSB
➢ Values X or Z will be treated as unknown value (X)
➢ If a = 3’b101, b=3’b110 and c=3’b01X, d=3’b1z0
60
60
61
OPERATORS
REDUCTION OPERATORS
➢ Unary reduction operators perform a bit-wise operation on a single
multi-bit operand to produce a single bit result.
➢ If there are the characters z and x the result can be a known value.
➢ If a = 5’b10101, b = 4’b0011, c = 3’bz00 and d = 3’bx011
62
First do the XOR operation then NOT
62
OPERATORS
LOGICAL OPERATORS
➢ The operators logical AND (&&) and logical OR (||) compare operands
and results a 1-bit scalar Boolean value
➢ The unary logical negation operator (!) converts 0 to 1 and 1 to 0
➢ Always result is ONE bit value: 0, 1 or x
➢ An ambiguous truth value (x or z) remains as x
➢ If a = 3’b010 and b = 3’b000
a &&b = 1’b0
a ||b = 1’b1
63
63
OPERATORS
RELATIONAL OPERATORS
➢ Result is one bit value: 0, 1 or x
➢ If a = 3’b010, b = 3’b100, c = 3’b111, d = 3’b01z and e = 3’b01X.
64
64
OPERATORS
EQUALITY OPERATORS
➢ Compare operands bit for bit, with zero filling if the two operands are
of unequal bit-length.
➢ logical equality (==), logical inequality(!=) returns 0, 1, or x
➢ case equality (===), case inequality (!==) returns 0 or 1
65
65
OPERATORS
➢ If a = 3’b010, b = 3’b100, c = 3’b111, d = 3’b01z and e = 3’b01X
a = = e ; o/p = 1’bx
a == = e ; o/p = 1’b0
66
66
OPERATORS
SHIFT OPERATORS
➢ Regular shift operators: Left shift (<<), Right shift (>>) perform left and
right shifts on their operand by the number of bit positions specified
➢ Both shift operators fill the vacated bit positions with zeroes
➢ The arithmetic shift operators (<<< and >>>) work the same as
regular shift operators on unsigned numbers. 67
67
OPERATORS
SHIFT OPERATORS
➢ For signed numbers,
➢ Arithmetic shift left is same as regular shift left operator
➢ Arithmetic shift right, MSB (sign bit) copied as well as shifted right
➢ If a = 4’b1010 and b = 4’b10X0
68
68
OPERATORS
CONCATINATION OPERATOR
➢ A concatenation is the joining together of bits resulting from two or more expressions
➢ The concatenation is expressed using the brace characters { and }, with commas
separating the expressions within
➢ Syntax: concatenation ::= {expression, {expression }}
➢ Unsized constant numbers are not allowed in concatenations
69
69
•Example:
reg a;
reg [2:0] b, c;
a = 1’b 1;
b = 3’b 010;
c = 3’b 101;
catx = {a, b, c}; // catx = 1_010_101
caty = {b, 2’b11, a}; // caty = 010_11_1
catz = {b, 1}; // WRONG !!
70
Replication Operator
❑ Replication Operator {{ }}.
❑ A replication constant specifies how many times to replicate the number inside
the bracket ({}).
71
OPERATORS
CONDITIONAL OPERATOR
➢ Examples:
Y = (sel) ? A : B; // If sel=1 then Y=A, else Y=B
Out = sel[1] ? (sel[0] ? in3 : in2) : (sel[0] ? in1 : in0); // Nested conditional operator
72
72
OPERATORS
OPERATORS PRECEDENCE
73
BEHAVIORAL
STRUCTURAL /
GATE LEVEL
SWITCH
75
75
STRUCTURAL / GATE-LEVEL
MODELLING
76
76
STRUCTURAL MODELLING
➢ At gate level modelling, the circuit is described in terms of gates.
77
STRUCTURAL MODELLING
79
79
STRUCTURAL MODELLING
80
80
Structural Modelling
81
Structural Modelling
82
DATA FLOW MODELLING
83
83
DATA FLOW MODELLING
➢ Dataflow level description of a digital circuit is at higher level; it makes
the circuit description more compact than design through gate primitives.
84
DATA FLOW MODELLING
AND GATE EXAMPLE
86
87
Dataflow Modelling-Examples
88
BEHAVIOURAL
MODELLING
89
module andgate(y,a,b);
input a,b;
module andgate (y,a,b); output y;
input a,b; reg y;
output y; always @(a or b)
reg y; begin
always @ (a or b) case ({a, b})
begin 2'b00: out = 0;
if (a == 1 && b == 1) 2'b01: out = 0;
y = 1; 2'b10: out = 0;
else 2'b11: out = 1;
y = 0; endcase
end end
endmodule endmodule
92
module half_adder (sum,carry, a,b);
Behavioral Model – half Adder output sum,carry;
input a,b;
reg sum,carry;
always @ (a or b)
module half_adder (sum,carry, a,b); begin
output sum,carry; case ({a, b})
input a,b; 2'b00: begin
reg sum,carry; sum = 1'b0;
carry = 1'b0;
always @ (a or b) end
begin 2’b01: begin
if (a == 0 && b == 0) sum = 1’b1;
sum = 0; carry =0; carry = 1'b0;
end
elseif (a == 0 && b == 1) 2’b10: begin
sum = 1; carry =0; sum = 1’b1;
elseif (a == 1 && b == 0) carry = 1'b0;
sum = 1; carry =0; end
elseif (a == 1 && b == 1) 2’b11: begin
sum = 1'b0;
sum = 0; carry =1; carry = 1’b1;
end end
endmodule endcase
end
endmodule 93
BEHAVIOURAL MODELLING
PROCEDURAL STATEMENT
➢ The assignment statement within an initial statement or always statement
is called procedural statement.
96
BEHAVIOURAL MODELLING
PROCEDURAL STATEMENT – BLOCKING ASSIGNMENT
➢ A blocking assignment statements are executed in the order they are
specified in a sequential block.
➢ The execution of next statement begins only after the completion of the
present blocking assignments.
➢ A blocking assignment will not block the execution of the next statement
in a parallel block.
➢ This type of assignment is majorly used in combinational circuit
modelling.
97
BEHAVIOURAL MODELLING
BLOCK STATEMENT – SEQUENTIAL BLOCK
Example-1: Example-2:
//Sequential block without delay // Sequential blocks with delay
reg X, Y; reg x, y;
• #10 : delay of 10 simulation time units.
reg [1:0] z, w; reg [1:0] z , w; • The #10 delay depends on the timescale
directive at the top of Verilog file.
initial initial • If timescale = 1ns , #10 means 10 ns
begin begin
x = l'b0; x = l'bo; //completes at simulation time 0
y = l‘b1; #5 y = l'bl; //completes at simulation time 5
z = {x, y}; #10 z = {x, y};//completes at simulation time 15
w = {y, x}; #20 w = {y, x); //completes at simulation time 35
end end 98
The #10 delay depends on the timescale directive at the top of your Verilog file.
BEHAVIOURAL MODELLING
NON BLOCK STATEMENT – SEQUENTIAL BLOCK
Example-1: Example-2:
//Sequential block without delay // Sequential blocks with delay
reg X, Y; reg x, y;
• #10 : delay of 10 simulation time units.
reg [1:0] z, w; reg [1:0] z , w; • The #10 delay depends on the timescale
directive at the top of Verilog file.
initial initial • If timescale = 1ns , #10 means 10 ns
begin begin
x <= l'b0; x <= l'bo; //completes at simulation time 0
y <= l‘b1; #5 y <= l'bl; //completes at simulation time 5
z <= {x, y}; #10 z <= {x, y};//completes at simulation time 10
w <= {y, x}; #20 w<= {y, x); //completes at simulation time 20
end end 99
initial begin
a = 1; // At t=0 in Verilog, if a module contains multiple initial blocks, all initial
#5 a = 2; // At t=5 blocks execute concurrently and begin execution at the same
end
simulation time (t=0ns). Each initial block runs independently,
initial begin and any delays (#) in one block do not affect the execution of
b = 1; // At t=0
#10 b = 3; // At t=10 the other blocks.
end
initial begin
c = 0; // At t=0
#15 c = 5; // At t=15
end
endmodule
TEST BENCH
102
TEST BENCH
➢ After modelling our digital circuit using either one of the modelling, we
need to apply appropriate stimulus to the design to test it.
➢ This can be done by writing another Verilog code called Test Bench and
it is used to simulate your design without physical hardware.
105
Test Bench -Half adder-Design
// Half_adder_tb
module half_adder _tb;
reg a;
reg b;
wire sum;
wire carry;
half_adder dut (sum,carry, a,b);
initial begin
a=1'b0; b=1'b0; // This is input a=0,b=0
#10 a=1'b0; b=1'b1; // This is input a=0,b=1
#10 a=1'b1; b=1'b0; // This is input a=1,b=0
#10 a=1'b1; b=1'b1; // This is input a=1,b=1
#10 $stop;
end
initial begin
$monitor($time, "a=%b, b=%b, sum=%b,carry=%b", a,b,sum,carry);
end
endmodule
106
module test_bench();
reg A, B, C;
wire SUM, CARRY;
integer i;
FullAdder dut (SUM, CARRY, A, B, C);
initial begin
for (i = 0; i < 8; i = i + 1)
begin
{A, B, C} = i; // Assign binary value of i to inputs
#10; // Wait for 10 time units
end
end
// Monitor and display outputs
initial begin
$monitor($time, " A = %b, B = %b, C = %b, SUM = %b,
CARRY = %b", A, B, C, SUM, CARRY);
#60 $finish; // End simulation after 60 time units
end
endmodule
107