Notes - Lecture Unit I
Notes - Lecture Unit I
Verilog is an HDL (Hardware Description Language). The latest stable version of Verilog
is IEEE 1364-2005. Verilog is a case sensitive language which only uses lowercase. It
supports simulation. In other words, it is possible to create a model of a function and
simulate it before building the real system. The base language of Verilog is C. Therefore,
a programmer who is familiar with C can learn Verilog quickly.
Module is the basic building block in Verilog. It provides information about input and
output ports and hides the internal implementation details. The syntax of module is as
follows. Every Verilog program starts with the keyword “module” and ends with the
keyword “endmodule”.
<program logic>
endmodule
What is VHDL
• Non C type.
• In VHDL User defined data types are used.
• In VHDL there is no case sensitive
• More complex
Verilog Operators
2 Arithmetic operators
Five arithmetic operators (+, - * / %)
a=3'b101, b= 3'b100
Y=a+b, a*b, a-b
3. Relational operators
It compares two values and returns either 1 or 0
a= 3'b010, b=3'b100
a> b=1'b0 (false) (is a greater than b)
a <b =1'b1 (true) (is a less than b)
a>= b =1'b0 (is a greater or equal to)
Language Rules
Primitives are predefined logic gates and functional blocks, low-level building
blocks used for gate-level modeling.
n-Input n-Output, 3-state
and buf
nand not
or bufif0
nor bufif1
xor notif0
xnor notif0
keywords are reserved words that defined the language's syntax and semantics.
•config •bufif0
•always •bufif1
•assign •case
•Begin •casex
•buf •casez
Design Encapsulation
endmodule
Encapsulation makes the model available for instantiation in
other modules
Data Types
realtime
Data Types
Net Data Type : These are used to connect logic gates. It doesn't store
any value on its own.
register Data Type: It is used to represent a variable and can store data between
assignments. So it is used to model hardware registers(constructed from flip- flop)
to store 1 or more bits. Its default value is X and unsigned integer.
Number of bits it can store depends on the system architecture.
Eg: reg a; // store 1 bit // so 1 bit Flip flop
reg [3:0] a ; // store 4 bits // 4 no. of 1 bit flip flop
Integer: It is signed integer variable of 32 bit wide. It's a general purpose register
data type for manipulating quantities.
Its default width is host machine word size(at least 32 bit)
Generally used for counting purpose.
Eg: 0, 4, -43, 53 etc.
real: It stores signed floating point value and rounded off to nearest interger when
assigned to an integer.
Its default value= 0.0
Eg: 5.6, 47.8
time: Its an Unsigned integer variable and 64 bit wide. It is used to store
simulation time during debugging.
Eg : time sim_time; //it stores simulation time in ns.
Data Types
module strings();
reg [8*28:0]string;
// declare a register variable of 28 byte
initial
begin
string="hello verilog world";
$display("%s\n",string);
end
endmodule
module module
name ports
primar primar
y y
inputs output module AOI_str (y_out, x_in1, x_in2, x_in3, x_in4,
AOI_str x_in5);
output y_out; port modes
input x_in1, x_in2, x_in3, x_in4,
x_in y x_in5; internal
1 1 wire y1, wires
y_ou
y2; establish
x_in t connectivit
nor (y_out, y1, y2);
2 and (y1, x_in1, x_in2); y
y
and (y2, x_in3, x_in4,
x_in 2 instantiat
x_in5);
endmodule
3 ed
primitives
x_in
4
x_in
5
Nested Modules
Model complex structural detail by instantiating modules
within modules
Add_full_0_dela
(a b)
c_in y
a. su c_in su
m m
w w3 (a b) c_in
Add_half_0_dela
a a. sum 1 y c_ou
Add_half_0_dela (ab t
b y b. c_out (a + b) c_in +
)w
a ab
b. c_out 2
b
actual name
Add_half_0_delay ( .b (b),
M1 .c_out
(w2),
.a (a),
formal .sum (w1)
name );
Vectors in Verilog
Examples
wire x,y; //single bit variable
wire [15:0] data; //16-bit data
wire [0:31] sum; //MSB is sum[0], LSB is
sum[31]
reg [7:0] d1,d2,d3; //3 buses of 8-bit width
reg [1:40] bus; //vector register, 40-bit bus
Input [3:0]a; // 4 bits a[3], a[2], a[1],a[0]
Example: 16-bit ripple carry Adder
Add_rca_16_0_delay
1
6
c_ou sum[15:
t 0]
4 4 4 4 4 4 4 4
Add_rca_4 Add_rca_4 Add_rca_4 Add_rca_4
_0_delay _0_delay _0_delay _0_delay
c_ou
t M M M M
4 c_in1 3 c_in 2 c_in 1
2 8 4
Add_rca_16_0_dela
y sum[15:1 sum[11: sum[7: sum[3:
2] 8] 4] 0]
a b c_in a b
Add_half_0_delay
Add_half_0_dela
y
Add_full_0_delay Add_half_0_dela
y
c_out su c_out
m sum
Verilog Model: 16-bit ripple carry Adder
Add_rca_1
6
M1 M2 M3 M4
Add_rca_4 Add_rca_4 Add_rca_4 Add_rca_4
M1 M2 M3 M4
Add_ful Add_ful Add_ful Add_ful
l l l l
M M M M4
1 2 3
Add_hal Add_hal norf20 invf10
f f 1 1
xo nan no xo nan no
r d t r d t
STRUCTURAL CONNECTIVITY
Wires in Verilog establish connectivity between primitives and/or modules
Data type: nets (Example: wire)
The logic value of a wire (net) is determined dynamically during simulation
by what is connected to the wire.
MODELING TIP
MODELING TIP
An undeclared identifier is treated by default as a
wire.
Structural Model: 2-bit Comparator
Compare two 2-bit binary words:
+ A1 A0 B0'
w
A1 6 w
1 A_gt_B
B1
w2
A0 A_lt_B
w
B 7
w3
0
w
4
A_eq_B
w
5
Verilog (Structural) Model:
(w
6,
A1)
;
not
(w
Example: 4-bit Comparator
Comp_4_str
M1
A3
A1 Comp_2_str
B3 g
B1 t
A3 A2
A0 lt A_gt_B
B3 B2
B0 eq
A2 A_gt_B
M0
B2 A_lt_B
A1
A1 Comp_2_str
A1 Comp_4_str B1 g A_lt_B
A_eq_B B1 t
B1 A0
A0 lt A_eq_B
A0 B0
B0 eq
B0
Verilog Model:
module Comp_4_str (A_gt_B, A_lt_B, A_eq_B, A3, A2, A1, A0, B3, B2, B1,
output A_gt_B, A_lt_B, A_eq_B;
B0);
input A3, A2, A1, A0, B3, B2, B1,
wire B0;
w1, w0;
Comp_2_str M1 (A_gt_B_M1, A_lt_B_M1, A_eq_B_M1, A3, A2, B3,
B2); Comp_2_str M0 (A_gt_B_M0, A_lt_B_M0, A_eq_B_M0, A1,
A0, B1, B0);
or (A_gt_B, A_gt_B_M1, w1);
and (w1, A_eq_B_M1, A_gt_B_M0);
and (A_eq_B, A_eq_B_M1,
or A_eq_B_M0); (A_lt_B, A_lt_B_M1,
and w0);
endmodulew0, A_eq_B_M1, A_lt_B_M0);
Simulation
Results:
Logic System
MODELING TIP
a y
b
x z
a 0 1
b x x z x z x
z z
y x x x
Net Data type
Net Data type
Note: If one of the input is Z means open circuit its acts as Buffer
Test Methodology
Design_Unit_Test_Bench
(DUTB)
Stimulus
Generato
r
Unit_Und
er_Test
(UUT)
S
E
T
D
Q
CLR
Q
Respons
e
Monitor
Example: Testbench
A testbench is a piece of code that generates input signals for the design you want to test.
module
wire
t_Add_half(); sum,
c_out; // Storage containers for stimulus
reg a, b; waveforms
Add_half_0_delay M1 (sum, c_out, a, b);
//UUT
initial begin // Time Out
#100 //
$finish; end Stopwatch
In Verilog, a “behavior” is a group of statements that set values for variables during
simulation — just like hardware would.
The initial keyword starts a block of code that runs once when the simulation begins
(tsim = 0).
The statements inside begin ... end are called procedural statements — they run one
by one, in order.
# delay control operator temporarily suspends execution of a behavior.
A testbench is a piece of code that generates input signals for the design
you want to test (called the UUT, or Unit Under Test), watches what
happens, and controls how the test runs.
In the example t_Add_half_0, the inputs are set by the testbench using
variables called reg. This means the values for a and b are controlled by the
testbench code.
Simulation Results for
add_half:
Event-Driven Simulation
Gate propagation delay specifies the time between an input change and
the resulting output change
In Verilog, by default, gates react instantly (delay = 0). But you can add a
delay to make the simulation more realistic.
Transport delay describes the time-of-flight of a signal transition
Verilog uses an inertial delay model for gates and transport delay for nets
Inertial delay suppresses short pulses (width less than the propdelay value)
Example: Propagation Delay
output Add_full
module
sum,
(sum, c_out, a, b,
c_in);
input c_out; a,
wire b, c_in;
w1, w2,
Add_hal w3;
M1 (w1, w2, a, b);
f M2 (sum, w3, w1,
Add_hal c_in); #1 M3 (c_out,
f or w2, w3);
endmodu
le
module Add_half (sum, c_out, a,
output
b); sum, c_out;
input a, b;
• Transport delay models pure delay: it passes every input change to the
output, exactly delayed by the specified amount.
• Even very short input pulses always appear at the output — just shifted
in time.
• Used to model ideal wires or transmission lines where all changes
propagate, even glitches.
• Not a built-in Verilog keyword, but this is the behavior you get when you
describe delays with continuous assignments, like:
• assign #5 y = a; // transport delay: output y copies input a with a 5 time
unit delay.
Inertial Delay
Not
0 1 0 0 0 : 1;
0 1 0 0 1 : 1;
0 1 0 1 0 : 1;
0 1 0 1 1 : 1;
0 1 1 0 0 : 1;
0 1 1 0 1 : 1;
0 1 1 1 0 : 1; An AOI Circuit diagram
0 1 1 1 1 : 0;
10 000:
1;
10 001:
1;
10 010:
1;
10 011:
1;
10 100:
1;
10 101:
1;
10 110:
1;
10 111:
0;
11 000:
0;
11 001:
0;
11 010:
0;
11 011:
0;
11 100:
0;
11 101:
0;
Example: UDP
selec
t
0
mux_pri mux_ou
1 m t
// select a b :
mux_out
0 0 0 : 0; // Order of table columns = port order of
inputs
0 0 1 : 0 ; // One output, multiple inputs, no inout
0 0 x : 0 ; // Only 0, 1, x on input and output
0 1 0 : 1 ; // A z input in simulation is treated as x
0 1 1 : 1 ; // by the simulator
0 1 x : 1 ; // Last column is the output
// select b :
a mux_out
1 0 0 : 0;
1 1 0 : 0;
1 x 0 : 0;
1 0 1 : 1;
1 1 1 : 1;
1 x 1 : 1;
x 0 0 : 0; // Reduces pessimism
x 1 1 : 1;
table
// Shorthand notation:
// ? represents iteration of the table entry over the
values 0,1,x.
// i.e., don't care on the input
// select a b : mux_out
// 0 0 ? : 0 ; // ? = 0, 1, x shorthand
notation.
// 0 1 ? : 1;
// 1 ? 0 : 0;
// 1 ? 1 : 1;
// ? 0 0 : 0;
// ?
endtable 1 1 : 1;
UDPs for Sequential Logic
In sequential UDP, the current input and the current output value
determine the next output value. The sequential UDP provides an efficient
interface for sequential circuits (i.e. flip-flops and latches) modeling.
The output has to be declared as reg in sequential UDP
Output is viewed as next state
Syntax
<input1> <input2> <input3> ... <inputN> : <current_state> :
<next_state>;
Example: Transparent Latch
When enabled, the output follows the input, making it "transparent." When disabled,
the output "latches" or holds its previous value.
data
q_out
preset
j q_out
J-K Functionality:
- preset and clear override
k clock
clk - no change if j = 0, k = 0
clear
- drive to 1 if j = 1, k = 0
- drive to 0 if j = 0, k = 1
- toggle if j = 1, k = 1
primitive jk_prim (q_out, clk, j, k, preset,
output q_out;
clear);
input clk, j, k, preset,
reg clear; q_out;
table
// j and k cases
// clk j pre clr state q_out/next_state
k
b * ? ? : ? : -;
? ? ? : ? : -;
b
// Reduced ?
*
pessimism.
p 0 0 1 1 : ? : -;
p 0 ? 1 ? : 0 -;
:
p ? 0 ? 1 : 1 -;
:
(?0) ? ? ? ? : ? : -;
(1x) 0 0 1 1 : ? : -;
(1x) 0 ? 1 ? : 0 -;
:
(1x) ? 0 ? 1 : 1 -;
:
x * 0 ? 1 : 1 -;
endtable :
x 0
endprimitive * 1 ? : 0 -;
:
Note: * denotes
any transition,
and is equivalent
to (??)