Verilog Tutorial1
Verilog Tutorial1
Verilog Tutorial1
1
VERILOG HDL
Module
Describes the functionality of the design
States the input and output ports
Example: A Computer
Functionality: Perform user defined computations
I/O Ports: Keyboard, Mouse, Monitor, Printer
2
Module
3
Lexical Conventions
Comments
// Single line comment
/* Another single line comment */
/* Begins multi-line (block) comment
All text within is ignored
Line below ends multi-line comment
*/
Number
decimal, hex, octal, binary
unsized decimal form
size base form
include underlines, +,-
String
" Enclose between quotes on a single line"
4
Lexical Conventions (cont.)
Identifier
A ... Z
a ... z
0 ... 9
Underscore
5
Description Styles
Structural: Logic is described in terms of Verilog gate
primitives
Example:
not n1(sel_n, sel);
and a1(sel_b, b, sel_b);
and a2(sel_a, a, sel);
or o1(out, sel_b, sel_a);
b sel_b
sel n1
sel_n
a1
o1 out
a
a2 sel_a
6
Description Styles (cont.)
Example:
assign out = (sel & a) | (~sel & b);
sel_b
sel sel_n
out
sel_a
7
Description Styles (cont.)
Example:
if (select == 0) begin
out = b;
end
else if (select == 1) begin
out = a; a Black Box
out
end b 2x1 MUX
sel
8
Structural Modeling
Execution: Concurrent
Format (Primitive Gates):
and G2(Carry, A, B);
First parameter (Carry) – Output
Other Inputs (A, B) - Inputs
9
Dataflow Modeling
10
Dataflow Modeling (cont.)
Timescale
`timescale 1ns/100ps
1 Time unit = 1 ns
Time precision is 100ps (0.1 ns)
10.512ns is interpreted as 10.5ns
11
Dataflow Modeling (cont.)
Example:
`timescale 1ns/100ps
module HalfAdder (A, B, Sum, Carry);
input A, B;
output Sum, Carry;
assign #3 Sum = A ^ B;
assign #6 Carry = A & B;
endmodule
12
Dataflow Modeling (cont.)
13
Behavioral Modeling
Example:
module mux_2x1(a, b, sel, out);
input a, a, sel;
output out;
always @(a or b or sel)
begin Sensitivity List
if (sel == 1)
out = a;
else out = b;
end
endmodule
14
Behavioral Modeling (cont.)
When is it executed?
Occurrence of an event in the sensitivity list
Event: Change in the logical value
Inter-Assignment Delay
Example:
Sum = A ^ B;
#2 Carry = A & B;
Delayed execution
Intra-Assignment Delay
Example:
Sum = A ^ B;
Carry = #2 A & B;
Delayed assignment
16
Procedural Constructs
17
Event Control
Event Control
Edge Triggered Event Control
Level Triggered Event Control
18
Loop Statements
Loop Statements
Repeat
While
For
Repeat Loop
Example:
repeat (Count)
sum = sum + 5;
If condition is a x or z it is treated as 0
19
Loop Statements (cont.)
While Loop
Example:
while (Count < 10) begin
sum = sum + 5;
Count = Count +1;
end
If condition is a x or z it is treated as 0
For Loop
Example:
for (Count = 0; Count < 10; Count = Count + 1) begin
sum = sum + 5;
end
20
Conditional Statements
if Statement
Format:
if (condition)
procedural_statement
else if (condition)
procedural_statement
else
procedural_statement
Example:
if (Clk)
Q = 0;
else
Q = D;
21
Conditional Statements (cont.)
Case Statement
Example 1:
case (X)
2’b00: Y = A + B;
2’b01: Y = A – B;
2’b10: Y = A / B;
endcase
Example 2:
case (3’b101 << 2)
3’b100: A = B + C;
4’b0100: A = B – C;
5’b10100: A = B / C; //This statement is executed
endcase
22
Conditional Statements (cont.)
Example:
casez (X)
2’b1z: A = B + C;
2’b11: A = B / C;
endcase
23
Data Types
Default Values
Net Types : z
Register Type : x
24
Data Types
Example
wire Reset; // A 1-bit wire
wire [6:0] Clear; // A 7-bit wire
Example
reg [ 3: 0 ] cla; // A 4-bit register
reg cla; // A 1-bit register
25
Restrictions on Data Types
Behavioral Modeling
Can use only reg data type (within initial and always
constructs)
Cannot use wire data type
26
Memories
An array of registers
reg [ msb : lsb ] memory1 [ upper : lower ];
Example
reg [ 0 : 3 ] mem [ 0 : 63 ];
// An array of 64 4-bit registers
reg mem [ 0 : 4 ];
// An array of 5 1-bit registers
27
Compiler Directives
`define – (Similar to #define in C) used to define global
parameter
Example:
`define BUS_WIDTH 16
reg [ `BUS_WIDTH - 1 : 0 ] System_Bus;
28
Compiler Directives (cont.)
Example
`include “./fulladder.v”
29
System Tasks
Display tasks
$display : Displays the entire list at the time when
statement is encountered
$monitor : Whenever there is a change in any argument,
displays the entire list at end of time step
Time
$time: gives the simulation
30
Type of Port Connections
Connection by Position
parent_mod
31
Type of Port Connections (cont.)
Connection by Name
parent_mod
32
Empty Port Connections
If an input port of an instantiated module is empty, the
port is set to a value of z (high impedance).
33
Test Bench
`timescale 1ns/100ps
module Top;
reg PA, PB;
wire PSum, PCarry;
Test Bench
Apply Inputs
HalfAdder G1(PA, PB, PSum, PCarry);
34
Test Bench - Generating Stimulus
initial begin
Clock = 0;
#50 Clock = 1;
#30 Clock = 0;
#20 Clock = 1;
end
35
Test Bench - Generating Clock
Clock
A Simple Solution:
wire Clock;
assign #10 Clock = ~ Clock
Caution:
Initial value of Clock (wire data type) = z
~z = x and ~x = x
36
Test Bench - Generating Clock (cont.)
Initialize the Clock signal
initial begin
Clock = 0;
end
Caution: Clock is of data type wire, cannot be used in an initial
statement
Solution:
reg Clock;
…
initial begin
Clock = 0;
end forever loop can
… also be used to
always begin generate clock
#10 Clock = ~ Clock;
end
37