Digital Circuit Design Lab: Part-B
Digital Circuit Design Lab: Part-B
Part- B
Part B: To design and simulate using
CAD tools
Software - Xilinx
Language - Verilog
What is Verilog HDL?
Why using Hardware Description Language?
Design abstraction: HDL ←→ layout by human
Hardware modeling
Reduce cost and time to design hardware
Two Popular HDLs
VHDL
Verilog
What is Verilog HDL?
Key features of Verilog
Supports various levels of abstraction
Behavior level
Register transfer level or dataflow
Gate level or structural level
Switch level or transistor level
Simulate design functions
Basic Limitation of Verilog
5
Verilog Value Set
0 represents low logic level or false condition
6
Numbers in Verilog (i)
<size>’<radix> <value>
No
Noofof Binary
Binary
bbororBB Consecutive
bits Consecutivechars
chars
bits Octal
Octal
ooor
orOO 0-f,
0-f,x,x,zz
Decimal
Decimal
ddor
orDD
Hexadecimal
Hexadecimal hhororHH
7
Numbers in Verilog (ii)
12’o 07_24
8
Number Representation
Examples:
6’b010_111gives 010111
8’b0110 gives 00000110
4’bx01 gives xx01
16’H3AB gives 0000001110101011
24 gives 0…0011000
5’O36 gives 11110
16’Hx gives xxxxxxxxxxxxxxxx
8’hz gives zzzzzzzz
Data Type: Register
Register
Keyword: reg, integer, time, real
Storage element (modeling sequential circuit)
Assignment in “always” block (LHS of expressions)
Data Type: Net
Net
Keyword: wire, wand, wor, tri, triand, trior, supply0, supply1
Doesn’t store value, just a connection
Input, output and inout ports are default “wire”
Vectors
Represent buses
wire [3:0] busA;
reg [1:4] busB;
reg [1:0] busC;
Left number is MS bit
Slice management
busC[1]
busC = busA[2:1]; = busA[2];
busC[0] = busA[1];
12
Bitwise Operators (i)
& bitwise AND
| bitwise OR
~ bitwise NOT
^ bitwise XOR
~^ or ^~ bitwise XNOR
13
Bitwise Operators (ii)
c = ~a; c = a & b;
a = 4’b1010;
b = 4’b1100;
c = a ^ b;
a = 4’b1010;
b = 2’b11;
14
Shift Operators
>> shift right
<< shift left
15
Concatenation Operator
{op1, op2, ..} concatenates op1, op2, .. to single number
Operands must be sized !!
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 !!
Replication ..
catr = {4{a}, b, 2{c}}; // catr = 1111_010_101101
16
Relational Operators
17
Equality Operators
== logical equality
Return 0, 1 or x
!= logical inequality
=== case equality
Return 0 or 1
!== case inequality
18
Conditional Operator
cond_expr ? true_expr : false_expr
A
1
Y
B Y = (sel)? A : B;
0
sel
19
Hierarchical Design
Top
TopLevel
Level E.g.
Module
Module
Full
FullAdder
Adder
Sub-Module
Sub-Module Sub-Module
Sub-Module
11 22
Half
HalfAdder
Adder Half
HalfAdder
Adder
Basic
BasicModule
Module Basic
BasicModule
Module Basic
BasicModule
Module
11 22 33
20
Module
f .. // declarations
inN outM .. // description of f (maybe
.. // sequential)
endmodule
21
Example: Half Adder
A S assign S = A ^ B;
Half assign C = A & B;
Half
B Adder
Adder C
endmodule
22
Example: Full Adder
in1 A S I1 A S sum
Half
Half Half
Half
Adder
Adder11 Adder
in2 B C I2 B Adder C I3
ha1
ha1 ha2
ha2 cout
cin
module full_adder(sum, cout, in1, in2, cin);
output sum, cout;
input in1, in2, cin;
endmodule
23
Hierarchical Names
ha2.A
cin
24
Structural Model (Gate Level)
Built-in gate primitives:
and, nand, nor, or, xor, xnor, buf, not, bufif0,
bufif1, notif0, notif1
Usage:
nand (out, in1, in2); 2-input NAND without delay
and #2 (out, in1, in2, in3); 3-input AND with 2 t.u. delay
not #1 N1(out, in); NOT with 1 t.u. delay and instance name
xor X1(out, in1, in2); 2-input XOR with instance name
Write them inside module, outside procedures
25
Example: Half Adder,
2nd Implementation
endmodule
26
Behavioral Model - Procedures (i)
Procedures = sections of code that we know they
execute sequentially
Procedural statements = statements inside a procedure
(they execute sequentially)
e.g. another 2-to-1 mux implem:
begin
if (sel == 0)
Execution Y = B;
Flow else Procedural
Proceduralassignments:
assignments:
Y = A; YYmust
mustbe
bereg
reg!!!!
end
27
Behavioral Model - Procedures (ii)
28
“Initial” Blocks
Start execution at sim time zero and finish when their
last statement executes
module nothing;
initial
$display(“I’m first”); Will
Willbe bedisplayed
displayed
atatsim
simtime
time00
initial begin
#50;
$display(“Really?”); Will
Willbe bedisplayed
displayed
atatsim
simtime
time5050
end
endmodule
29
“Always” Blocks
Start execution at sim time zero and continue until sim
finishes
30
Events (i)
@
always @(signal1 or signal2 or ..) begin
..
end execution triggers
execution triggersevery
every
time
timeany
anysignal
signalchanges
changes
always @(posedge clk) begin
.. execution
executiontriggers
triggersevery
every
end time
timeclk
clkchanges
changes
from
from00to
to11
always @(negedge clk) begin
.. execution
executiontriggers
triggersevery
every
end time
timeclk
clkchanges
changes
from
from11to
to00
31
Examples
3rd half adder implem
module half_adder(S, C, A, B);
output S, C;
input A, B;
reg S,C;
wire A, B;
endmodule
32
Procedural Statements: if
E.g. 4-to-1 mux:
module mux4_1(out, in, sel);
output out;
if (expr1) input [3:0] in;
true_stmt1; input [1:0] sel;
reg out;
else if (expr2) wire [3:0] in;
wire [1:0] sel;
true_stmt2;
.. always @(in or sel)
if (sel == 0)
else out = in[0];
def_stmt; else if (sel == 1)
out = in[1];
else if (sel == 2)
out = in[2];
else
out = in[3];
endmodule
33
Procedural Statements: case
34
Test Methodology
Stimulus
Hardware Design
Testbench
(Design Under Test)
Response
35