DSDV Unit 4
DSDV Unit 4
1
Girija.S Dept. of ECE, Dr.Ambedkar Institute of Technology
HDLs – WHAT-WHY-HOW
§ WHAT is a HDL?
2
HDLs – WHAT-WHY-HOW
§ WHY were HDLs required?
§ The designers felt need for a flexible language that may help
the design process by giving a complete framework for design.
3
HDLs – WHAT-WHY-HOW
§ WHY were HDLs required? (contd..)
4
HDLs – WHAT-WHY-HOW
§ HOW are HDLs advantageous?
LSI
✓ Design process became complex
✓ Led to the invention of the CAD tools
✓ CAD – Computer Aided Design
✓ Designer’s used the circuit and logic
simulation tools to verify the
functionality.
Evolution of Computer Aided Design
VLSI
✓ With 10000 of transistors, impossible to verify on
a breadboard.
✓ Computer aided techniques became critical for
verification and design of VLSI circuits.
✓ Computer aided techniques which do automatic
route and placement is popular
✓ Logic simulators to verify the functionality has
come into existence.
Emergence of HDL’s
2. A behavioral description is
then created to analyze the
design in terms of functionality,
performance, and compliance to
standards, and other high-level
issues.
Behavioral descriptions are often
written with HDLs.
12
Girija S ECE Dr.AIT
Verilog HDL
Basic Concepts
Lexical Conventions
AVerilog program is a string of tokens
✓ Whitespace
✓ Comments
✓ Delimiters
✓ Numbers
✓ Strings
✓ Identifiers
✓ Keywords
Lexical Conventions
• Whitespace • Comments
• Blank space (\b) • Used for readability and
documentation
• Tab (\t)
• Just like in C:
• Newline (\n)
1 . / / single line comment
• Whitespace is ignored in Verilog 2. /* multi line
except comment
*/
• In strings
3. /* Nested
• When separating tokens comments
/* like this */ may not be
acceptable (depends on
Verilog compiler) */
<base> ‘b, ‘B, ‘d, ‘D, ‘o, ‘O, ‘h, ‘H. Default is ‘decimal’
Numbers example
module Verilog_number;
reg [7:0] Num;
wire status;
…
Num = 16; // 8’b0001_0000
Num = -8’d4; // two’s complement of 4
Num = ‘bx; // 8’bxxxx_xxxx
Num = ‘b0x; // 8’b0000_000x
Num = ‘b10x; // 8’b0000_010x
if (status == 1) // status == 32’h0001
Num = 8’b1010_0101;
if (status == 1’b1)
…
endmodule
DATATYPES
28
Girija S ECE Dr.AIT
Verilog HDL
Datatypes
✓ Value set and strengths
✓ Nets and Registers
✓ Vectors
✓ Integer, Real, and Time Register Data Types
✓ Arrays
✓ Memories
✓ Parameters
✓ Strings
Value Set
• Verilog concepts to model hardware circuits
• Value level
• Value strength
• Used to accurately model
• Signal contention
• MOS devices
• Dynamic MOS
• Other low-level details
Value Set
Value HW Condition Strength level Type
level
0 Logic zero, false supply Driving
strong Driving
1 Logic one, true
pull Driving
x Unknown
large Storage
z High imp., floating
weak Driving
medium Storage
small Storage
highz High Impedance
Nets
§ Represent the connections between hardware elements
Nets
• Used to represent connections between HW elements
• Values continuously driven on nets
• Keyword: wire
Default: One-bit values
unless declared as vectors
• Default value: z
For trireg, default is x
• Examples
• wire a;
• wire b, c;
• wire d=1’b0;
Registers
§ Correspond to variables in the C language.
§ Register data types always retain their value until another
§ value is placed on them.
Registers
• Registers represent data storage elements
• Retain value until next assignment
• NOTE: this is not a hardware register or flipflop
• Keyword: reg
Default value: x
• Example:
reg reset;
initial
begin
reset = 1’b1;
#100 reset=1’b0;
end
Vectors
• Net and register data types can be declared as vectors
(multiple bit widths)
• Vectors are multi bit values
Syntax:
• wire/reg [msb_index : lsb_index] data_id;
• Examplewire a;
wire [7:0] bus;
wire [31:0] busA, busB, busC;
reg clock;
reg [0:40] virtual_addr;
Vectors cont’d
• Consider
wire [7:0] bus;
wire [31:0] busA, busB, busC; reg
[0:40] virtual_addr;
• Access to bits or parts of a vector is possible:
busA[7]
bus[2:0] // three least-significant bits of bus
// bus[0:2] is illegal.
virtual_addr[0:1] /* two most-significant bits
* of virtual_addr*/
Vectors cont’d
Variable Vector Part Select
[<starting_bit>+: width>
part-select increments from starting bit
[<starting_bit>-: width>
part-select decrements from starting bit
Examples:
reg [255:0] data1;
reg [0:255] data2;
reg [0:7] byte;
byte= data1[31-:8];
//starting bit is 31 width=8 =>data[31:24]
byte= data1[24+:8];
//starting bit is 24 width=8 =>data[31:24]
byte= data2[31-:8];
//starting bit is 31 width=8 =>data[24:31]
byte= data2[24+:8];
//starting bit is 24 width=8 =>data[24:31]
Arrays
• Only one-dimensional arrayssupported
Allowed for reg, integer, time
Not allowed for real data type
• Syntax:
<data_type> <var_name>[start_idx : end_idx];
• Examples:
integer count[0:7];
reg bool[31:0];
time chk_point[1:100]; reg [4:0]
port_id[0:7];
integer matrix[4:0][4:0]; // illegal
count[5] chk_point[100]
port_id[3]
• Note the difference between vectors and arrays
Memories
• RAM, ROM, and register-files used many times in digital systems
• Memory = array of registers in Verilog
• Word = an element of the array
• Can be one or more bits
• Examples:
reg membit[0:1023];
reg [7:0] membyte[0:1023];
membyte[511]
• Note the difference (as in arrays):
reg membit[0:127];
reg [0:127] register
Parameters
• Similar to const in C
• But can be overridden for each module at compile-time
• Syntax:
parameter <const_id>=<value>;
• Gives flexibility
• Allows to customize the module
• Example:
parameter port_id=5;
parameter cache_line_width=256; parameter
bus_width=8;
wire [bus_width-1:0] bus;
Strings
• Strings are stored in reg variables.
• 8-bits required per character
• The string is stored from the least-significant part to the most-
significant part of the reg variable
• Example:
reg [8*18:1]
string_value;
initial
string_value = “Hello World!”;
• Escaped characters
• \n: newline \”: “
• \t: tab \\: \
• %%: % \ooo: character number in octal
✓ Modules
✓ Ports
✓ Hierarchical Names
❑ Elements are grouped into modules to provide the 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).
Modules
❑ Module instantiation is like creating actual objects (Instances) from the common
template (module definition).
Structure of module
endmodule
Structure of modules
module module_name (port_list);
Declarations:
input, output, wire, parameter…..
System Modeling:
describe the system in gate-level, data-flow, or
behavioral style…
endmodule
Program
module not_gate(in, out); // module name+ports
// comments: declaring port type
input in;
output out;
endmodule
Program
// Compute the logical AND and OR of inputs A and B.
AND_OR
A
andOut
TheAndGate
orOut
B
TheOrGate
Program
module D_FF(q,d,clk,reset);
output q; //port declaration
input d,clk,reset; // data type declaration reg q;
always @ (posedge reset or negedge clk)
if (reset)
q=1'b0;
else
q=d; endmodule
Ports
• Ports provide interface by which a module can
communicate with its environment.
Ports
Port Declaration
Port assignments
Modules contain functional descriptions and have input,
output, and inout (bidirectional ports) for interfaces
The following are true of module interfaces:
§ An input or inout port is a wire type within its module
§ An output port must be a wire if it is generated by a
sub module
§ An output port must be a wire if it is generated
declaratively.
An output port ust be
An output port must be
§ reg if it is assigned to procedurally.
§ A wire if it is assigned through continuous assignment
§ Bidirectional ports cannot be assigned to procedurally
Example :D -Flipflop
module DFF (q, d, clk, reset)
output q;
reg q;
input d, clk, reset;
.
.
.
endmodule
SR latch
module SR_latch (Q, Qbar, Sbar,
Rbar);
output Q, Qbar;
input Sbar, Rbar;
// instantiate lower -level module
nand n1(Q, Sbar, Qbar);
nand n2(Qbar, Rbar, Q);
endmodule
• Port consists of two units. One unit internal to module and another
external to module.
• The internal and external units are connected.
• There are rules governing port declarations when modules are
instantiated within other modules.
Dataflow Modeling
Dataflow modeling
► Gate level modeling works well for small
circuits
Dataflow modeling
In data flow modeling a circuit is designed in terms of
the data flow between registers
Continuous Assignments
Syntax:
continuous_assign ::= assign [ drive_strength ] [ delay ]
list_of_net_assignments ;
Characteristics
► Left hand side must always be a scalar, vector, net or
a concatenation of scalar and vector nets
► It can not be scalar or vector register
► Always active
► Evaluated as soon as a right hand side value
changes
► RHS canbe registers, nets, or function calls
► Delay is used to decide when to assign the
evaluated value to LHS
Continuous Statement
► Most basic statement in dataflow modeling
► Drives a value onto a net
► Nets: connections between hardware elements
§ Declared with wire
§ Default value z
§ Get output of drivers (no value = z)
§ Class net
► Wire, wand, wor, tri, triand, trior, trireg
§ Continuous assignment replaces gates
Continuous Statement
Examples :
//Continuous assign. out is a net. i1 and i2 are nets.
assign out = i1 & i2;
Delays
Delay values control the time between the change in a
right-hand-side operand and when the value is assigned to
the LHS.
There are 3 ways of specifying delays in continuous
assignment-
1. regular assignment delay,
2. implicit continuous assignment delay and
3. net declaration delay.
wire out ;
assign #10 out=in1 & in2;
wire # 10 out1;
assign out1= in1 & in2;
// here out will be delayed by 10
OR
wire out;
assign #10 out = in1 & in2;
► Parameter port_id = 5
Girija S ECE Dr.AIT
Verilog HDL
► Value set: 0, 1, x, z
Preferably use –ve numbers only for integers & real data
Arithmetic Operators
► Binary: * , / , + , - , %
§ Result is “x” if any of the two operands contains an x
► Unary operators:
+ (positive) or – (negative) sign
§ Higher precedence
Arithmetic Operators
Operator Operator Operation No of
type symbol performe operands
d
* Multiply Two
/ Divide Two
Arithmetic + Add Two
- Subtract Two
% Modulus Two
** Power(exp Two
onent)
Arithmetic Operators
A= 4’b0011 B= 4’b0100 //A&B are registers
D= 6 E= 4 F= 2 // D&E are integers
A*B = //mul A&B, o/p 4’b1100
D/E // Div D by E, o/p 1
A+B //Add A&B, o/p 4’b0111
B-A //Subtract A from B, o/p 4’b0001
F=E**F //F= 𝐸 F = 42 =16
Arithmetic Operators
If any operand has a value x, then the result of the
entire expression is x
i1= 4’b101x i2=4’b1011
i1+i2 = 4’bx
Modulus operator produces the remainder from division
of two numbers
13 % 3 //13/3 R=1
-7 % 2 //-7/2 R=-1
7 % -2 //7/-2 R=+1
Logical Operators
► Binary: &&, ||
► Unary: !
► Evaluates to a 1-bit (0, 1 or x)
§ Operand = 0 logical 0
§ Operand != 0 logical 1
§ Operand with x’s or z’s x se)
! Logical one
Logical negation
|| Logical or Two
//unknowns
A= 2‘b0X; B= 2’b10;
A && B //o/p is 2’bX(evaluates to X)
// expression
(a==2) && (b==3)
► & , ~& , | , ~| , ^ , ~^
► 1-bit result
// x = 4’b1010
&x // 1 & 0 & 1 & 0 => 1’b0
|x // 1 | 0 | 1 | 0 => 1’b1
^x // 1^ 0 ^ 1 ^ 0 => 1’b0
// used for a even or odd parity generation. Similarly
~& reduction nand,
~^ reduction xnor,
~| reduction nor
Relational Operators
Relational operators are
• greaterthan (>)
• lessthan (<)
• greaterthan or equal to (>=)
• lessthan or equal to (<=).
Relational Operators
Relational Operators
// A= 4, B= 3
//X= 4’b1010, Y=4’b1101, z=4’b1xxx
Equality Operators
Equality
=== Case Two 0,1
equality
!== Case Two 0,1
inequality
Equality Operators
Equality Operators
Case equality: unlike the ordinary ‘==’ both operands are compared bit
by bit including x & z. the result is ‘1’ if the operands match exactly,&
result is ‘0’ if the operands do not match exactly.
The o/p of case equality & case inequality is never ‘X’.
//A=4, B=3, X=4’b1010, Y=4’b1011
//Z= 4’b1xxz,M=4’b1xxz, N=4’b1xxx
A == B //o/p is ‘0’.
A != B //o/p is ‘1’
X == Z //o/p is ‘X’
Z ===M //o/p is ‘1’
Z===N //o/p is ‘0’
M! == N //o/p is ‘1’
Girija S ECE Dr.AIT
Verilog HDL
Shift Operators
► >> shift right
Shift Operators
Operator Operato Operation No of
type r symbol performed operands
Shift Operators
Regular shift operators shift a vector operands to the
right or the left by specified number of bits the operands
are the vector and the number of bits to shift.
When bits are shifted the vacant bit positions are filled
with zeros
// x = 4’b1100
Y = x >> 1 ; // y = 4’b0110
Y = x << 1 ; // y = 4’b1000
Y = x << 2 ; // y = 4’b0000
Concatenation Operators
► {{…}
Concatenation Operator
• It provides a mechanism to concatenate multiple
operands must be sized.
• Unsigned operands not allowed.
• Operands enclosed in {} and separated by ,
Replication Operator
The operator is {{}}
Repetitive concatenation of the same number can be
expressed by using a replication constant.
A replication constant tells number of time repetition is
to be done.
A=1’b1 ; b=2’b00 ; c=2’b11
Y= {4{a}} //y=4’b1111
Y= {4{a},2{b}} // y= 11110000
Conditional Operators
Conditional Operator
Conditions expiration? true expression : false expression;
► Used in data flow to model conditional
statements
► Example
§ assign addr_bus = drive_enable ? Addr_out : 32’bz;
§ assign out = control ? in1 : in0;
//xor
y=(a != b)? 1:0;
Operator Precedence
Operators Operator symbol Precedence
Unary +, - ,! , ~ highest
Mul,div,mod *,/ , %
Add,sub,shift +, -, << , >>
Relational < ,<= , > ,>=
Equality ==, !=, === ,!==
Reduction &, ~&, ^, ~^, |, ~|
Logical && , ||
Conditional ?! lowest
Girija S ECE Dr.AIT
Verilog HDL
Operator Precedence
Operator Precedence
A=11110000, B=01011101, C=00000000
3. ~|B = ~|(01011101)=1
Exercise
A=10010011 B=01101111
Exercise
A=4’b0110; b=4’b1001; c=4’b101x; d=4’b101x
C===D
C===D 1
B>>>2
B>>>2 0010
A**B A**B 69
Y=(A>B)? C :D Y=(A>B)? C :D 101x
C==D C==D x
A<=D A<=D 0
Y= Y = {a,b[1],c[0],d[2]} Y= {a,b[1],c[0],d[2]} 01100x0
//Dataflow Description
//Gatelevel Description
module full_adder_data(a,b,c, sum,carry);
module full_adder_gate(a,b,c, sum,carry);
input a,b,c; output sum,carry;
input a,b,c;
assign s= a^b^c;
output reg sum, carry;
assign carry= (a&b)(b&c)|(c&a);
wire x1,x2,x3,x4;
endmodule
xor g1 (x1,a,b);
xor g2(sum,x1,c);
//Behavioral Description and a1(x2,a,b);
module full_adder(a,b,c, sum,carry); and a2(x3, b, c);
input a, b, c; and a3(x4, c, a );
output reg sum, carry; or o1(carry, x2, x3, x4);
always@(c, a, b) endmodule
begin
sum = a ^ b ^ c;
carry= (a&b)|(a&c)|(b&c);
end
endmodule
wire [2:0]w;
gfulladder g1(s[0],w[0],a[0],b[0],c);
gfulladder g2(s[1],w[1],a[1],b[1],w[0]);
gfulladder g3(s[2],w[2],a[2],b[2],w[1]);
gfulladder g4(s[3],cout,a[3],b[3],w[2]);
endmodule
Half Subtractor
Difference = A XOR B
Borrow = A’B
endmodule
In Logic diagram of 2 to 4 line decoder. Here, 2 inputs are decoded into four outputs.
The two inverters provide the complement of the inputs, and each one of four AND gates
generates one of the minterms.
4:2 Encoder
//Testbench code for 4-2 Encoder
module encode_4_to_2( module top;
input d0,d1,d2,d3, wire a0,a1;
output a0,a1 reg d0,d1,d2,d3;
); Encoder_4_to_2(d0,d1,d2,d3, a0,a1);
wire x,y,z; initial
not g1(x,d2); begin
and g2(y,x,d1); // Initialize Inputs
or g3(a0,y,d3); d0 = 1;d1 = 0;d2 = 0;d3 = 0;
or g4(a1,d2,d3); #100;
endmodule // Add stimulus here
#100;d0 = 0;d1 = 1;d2 = 0;d3 = 0;
#100;d0 = 0;d1 = 0;d2 = 1;d3 = 0;
#100;d0 = 0;d1 = 0;d2 = 0;d3 = 1;
end
4:1 Multiplexer
4:1 Multiplexer
4:1 Multiplexer
Method2 : using conditional operator
// 4-to-1 multiplexer. Port list is taken exactly from the I/O
diagram.
module mux4_to_1 (out, i0, i1, i2, i3, s);
4:1 Multiplexer
Method3 : using nested ternary/conditional operator
// 4-to-1 multiplexer. Port list is taken exactly from the I/O
diagram.
module mux4_to_1 (out, i0, i1, i2, i3, s1, s0);
endmodule
// Set up variables
0] A, B;
reg C_IN;
wire [3:0] SUM;
wire C_OUT;
// Stimulate inputs
initial begin
A = 4'd0; B = 4'd0; C_IN = 1'b0;
#5 A = 4'd34'd4;
#5 A = 4'd2; B = 4'd5;
#5 A = 4'd9; B = 4'd9;
#5 A = 4'd10; B = 4'd15;
#5 A = 4'd10; B = 4'd5; C_IN = 1'b1;
end
endmodule
// Internal wires
wire p0,g0, p1,g1, p2,g2, p3,g3; wire c4, c3, c2, c1;
// Compute Sum
assign sum[0] = p0 ^ c_in,
sum[1] = p1 ^ c1,
sum[2] = p2 ^ c2,
sum[3] = p3 ^ c3;
endmodule