Chapter 3: Verilog HDL
Đỗ Duy Tân, Ph.D
Email: tandd@hcmute.edu.vn
https://sites.google.com/site/tandduy/
Department of Computer and Communication Engineering
Faculty of Electrical and Electronics Engineering, HCMUTE
Original slides composed by Dr. Truong Ngoc Son – Modified by Dr. Do Duy Tan
Department of Computer and Communication Engineering
2 IC Design Companies in Vietnam
Outline
• Verilog?
• Module
• Instances
• User Identifiers
• Numbers in Verilog
• Signal and Nets in Verilog
• Registers
• Vectors and Arrays
• Logical vs Bitwise Operators
• Conditional Operator
• Continuous Assignments
• Structural Model (Gate Level)
• Behavioral Model – Procedures: Initial vs Always
• Procedural Statements: If – case – for
• System Tasks
3 Verilog HDL Basics
• EXAMPLES
What is Verilog
• Hardware Description Language (HDL)
• Hardware description languages such as Verilog
HDL and VHDL became popular
• Developed in 1983
• Standard: IEEE 1364, Dec 1995
4 Verilog HDL Basics
Design flow
5
Verilog HDL Basics
Popularity of Verilog
Verilog HDL is a general-purpose hardware
description language that is easy tolearn and easy to
use
Verilog HDL allows different levels of abstraction to
be mixed in the same model: gates, RTL, or behavioral
code
Most popular logic synthesis tools support Verilog
HDL
All fabrication vendors provide Verilog HDL libraries
for postlogic synthesis simulation
6 Verilog HDL Basics
Abstraction Levels in Verilog
Behavioral
RTL
Gate
7 Verilog HDL Basics
Design Methodologies
Top-down design methodology: we define the top-
level block and identify the sub-blocks necessary to
build the top-level block
8 Verilog HDL Basics
Design Methodologies
Bottom-up design methodology: we first identify the
building blocks that are available to us. We build
bigger cells, using these building blocks
9 Verilog HDL Basics
Main Language Concepts (i)
• Concurrency
• Structure
10 Verilog HDL Basics
4 bit counter
11 Verilog HDL Basics
4 bit counter, top-down design
12 Verilog HDL Basics
Outline
• Verilog?
• Module
• Instances
• User Identifiers
• Numbers in Verilog
• Signal and Nets in Verilog
• Registers
• Vectors and Arrays
• Logical vs Bitwise Operators
• Conditional Operator
• Continuous Assignments
• Structural Model (Gate Level)
• Behavioral Model – Procedures: Initial vs Always
• Procedural Statements: If – case – for
• System Tasks
13 Verilog HDL Basics
Module
A module is the basic building block in Verilog.
A module can be an element or a collection of lower-
level design blocks
A module provides the necessary functionality to the
higher-level block through its port interface (inputs
and outputs)
14 Verilog HDL Basics
Module
module <module_name> (<module_terminal_list>);
...
<module internals>
...
endmodule
15 Verilog HDL Basics
Module
Specifically, the T-flipflop could be defined as a module
as follows:
module T_FF (clock, reset, q);
clock q
input clock;
reset
input reset;
T_FF
// input clock, reset;//clock, reset 1 bit
//input [2:0] clock, reset;//clock 3 bit, reset 3 bit
// input [1:0] clock; ;//clock 2 bit
output q;//q là 1 bit
.
16 Verilog HDL Basics
<functionality of T-flipflop>
Outline
• Verilog?
• Module
• Instances
• User Identifiers
• Numbers in Verilog
• Signal and Nets in Verilog
• Registers
• Vectors and Arrays
• Logical vs Bitwise Operators
• Conditional Operator
• Continuous Assignments
• Structural Model (Gate Level)
• Behavioral Model – Procedures: Initial vs Always
• Procedural Statements: If – case – for
• System Tasks
17 Verilog HDL Basics
Instances
module ripple_carry_counter(clk, reset, q);
q[0] q[1] q[2] q[3]
output [3:0] q;
input clk, reset; clk clock q clock q clock q clock q
reset reset reset reset
tff0 tff1 tff2 tff3
reset
ripple_carry_counter
T_FF tff0(.clock(clk), .reset(reset), .q(q[0]));
T_FF tff1(.clock(q[0]), .reset(reset), .q(q[1]));
T_FF tff2(.clock(q[1]), .reset(reset), .q(q[2]));
T_FF tff3(.clock(q[2]), .reset(reset), .q(q[3]));
endmodule
T_FF tff0(clk, reset, q[0]);//Đúng
T_FF tff0(reset, clk, q[0]);//SAI
T_FF tff0(.reset(reset), .clock(clk), .q(q[0]));
Instances
module T_FF(q, clk, reset);
output q;
input clk, reset;
wire d;
D_FF dff0(q, d, clk, reset);
not n1(d, q);
endmodule
19 Verilog HDL Basics
20 Verilog HDL Basics
Outline
• Verilog?
• Module
• Instances
• User Identifiers
• Numbers in Verilog
• Signal and Nets in Verilog
• Registers
• Vectors and Arrays
• Logical vs Bitwise Operators
• Conditional Operator
• Continuous Assignments
• Structural Model (Gate Level)
• Behavioral Model – Procedures: Initial vs Always
• Procedural Statements: If – case – for
• System Tasks
21 Verilog HDL Basics
User Identifiers
• Formed from {[A-Z], [a-z], [0-9], _, $}, but ..
• .. can’t begin with $ or [0-9] or -
– myidentifier □v
– m_y_identifier □v
– 3my_identifier □
– $my_identifier □
– _myidentifier$ □v
– -myidentifier$ □
• Identifiers are case sensitive
– myid Myid
22 Verilog HDL Basics
Comments
• // The rest of the line is a comment
• /* Multiple line
comment */
• /* Nesting /* comments */ do NOT work */
23 Verilog HDL Basics
Verilog Value Set
• 0 represents low logic level or false condition
• 1 represents high logic level or true condition
• x represents unknown logic level
• z represents high impedance logic level
24 Verilog HDL Basics
Outline
• Verilog?
• Module
• Instances
• User Identifiers
• Numbers in Verilog
• Signal and Nets in Verilog
• Registers
• Vectors and Arrays
• Logical vs Bitwise Operators
• Conditional Operator
• Continuous Assignments
• Structural Model (Gate Level)
• Behavioral Model – Procedures: Initial vs Always
• Procedural Statements: If – case – for
• System Tasks
25 Verilog HDL Basics
Numbers in Verilog (i)
<size>’<radix> <value>
No of Binary b or B Consecutive chars
bits Octal o or O 0-f, x, z
Decimal d or D
Hexadecimal h or H
– 8’h a5 = 1010|0101
– 8’b 10100101 = 1010|0101
– 12’o 3zx7 = 011|zzz|xxx|111
26 Verilog HDL Basics
Numbers in Verilog (ii)
• You can insert “_” for readability
– 12’b 000_111_010_100
– 12’b 0001_1101_0100 Represent the same number
– 12’o 07_24
• Bit extension
– MS bit = 0, x or z extend this
• 4’b x1 = 4’b xx_x1
– MS bit = 1 zero extension Mở rộng bit
• 4’b 1x = 4’b 00_1x
27 Verilog HDL Basics
Numbers in Verilog (iii)
• If size is ommitted it
– is inferred from the value or
– takes the simulation specific number of bits or
– takes the machine specific number of bits
• If radix is ommitted too .. decimal is assumed
– 15 = <size>’d 15
28 Verilog HDL Basics
Parameters in Verilog (iii)
• A parameter associates an identifier name with a
constant. Let the Verilog code include the following
declarations:
parameter n = 4;
parameter S0 = 2’b00, S1 = 2’b01, S2 = 2’b10, S3
= 2’b11;
29 Verilog HDL Basics
Outline
• Verilog?
• Module
• Instances
• User Identifiers
• Numbers in Verilog
• Signal and Nets in Verilog
• Registers
• Vectors and Arrays
• Logical vs Bitwise Operators
• Conditional Operator
• Continuous Assignments
• Structural Model (Gate Level)
• Behavioral Model – Procedures: Initial vs Always
• Procedural Statements: If – case – for
• System Tasks
30 Verilog HDL Basics
Signal in Verilog code
Tín hiệu Dây dẫn vật lý Biến không lưu trữ
• Signal = physical wire = variable without storing
• In Verilog, a signal in a circuit is represented as a net
or a variable with a specific type. The term net is
derived from the electrical jargon, where it refers to
the interconnection of two or more points in a circuit.
A net or variable declaration has the form
type [range] signal_name{signal_name};
31 Verilog HDL Basics
Nets (i)
• Can be thought as hardware wires driven by logic
• Equal z when unconnected
• Various types of nets
– wire
– wand (wired-AND)
– wor (wired-OR)
– tri (tri-state)
• In following examples: Y is evaluated,
automatically, every time A or B changes
32 Verilog HDL Basics
Nets (ii)
A wire A, B, Y; // declaration signal
Y
B assign Y = A & B;
Y được đánh giá, một cách tự
động, mỗi khi A hoặc B thay đổi
dr
tri Y; // declaration
A Y
assign Y = (dr) ? A : z;
33 Verilog HDL Basics
Outline
• Verilog?
• Module
• Instances
• User Identifiers
• Numbers in Verilog
• Signal and Nets in Verilog
• Registers
• Vectors and Arrays
• Logical vs Bitwise Operators
• Conditional Operator
• Continuous Assignments
• Structural Model (Gate Level)
• Behavioral Model – Procedures: Initial vs Always
• Procedural Statements: If – case – for
• System Tasks
34 Verilog HDL Basics
Registers
• Variables that store values Biến lưu trữ giá trị
• Do not represent real hardware but ..
• .. real hardware can be implemented with registers
• Only one type: reg
reg A, C; // A: 1 bit, C: 1 bit
//reg [1:0] A;//A: 2 bit
// assignments are always done inside a procedure
A = 1;
C = A; // C gets the logical value 1
A = 0; // C is still 1
Giá trị được cập nhật một
C = 0; // C is now 0 cách rõ ràng
• Register values are updated
35
explicitly!! Verilog HDL Basics
Outline
• Verilog?
• Module
• Instances
• User Identifiers
• Numbers in Verilog
• Signal and Nets in Verilog
• Registers
• Vectors and Arrays
• Logical vs Bitwise Operators
• Conditional Operator
• Continuous Assignments
• Structural Model (Gate Level)
• Behavioral Model – Procedures: Initial vs Always
• Procedural Statements: If – case – for
• System Tasks
36 Verilog HDL Basics
[ MSB : LSB ] Vectors
• Represent buses
wire [3:0] busA;
reg [1:4] busB;
reg [1:0] busC;
• Left number is MS bit
• Slice management
busC[1] = busA[2];
busC = busA[2:1];
busC[0] = busA[1];
• Vector assignment (by position!!)
busB[1] = busA[3];
busB[2] = busA[2];
busB = busA;
busB[3] = busA[1];
busB[4] = busA[0];
37 Verilog HDL Basics
Integer & Real Data Types
• Declaration
integer i, k;
real r;
• Use as registers (inside procedures)
i = 1; // assignments occur inside procedure
r = 2.9;
k = r; // k is rounded to 3
• Integers are not initialized!! +khởi
Các số nguyên không được
tạo
• Reals are initialized to 0.0 + Các số thực được khởi tạo
với giá trị 0.0
38 Verilog HDL Basics
Time Data Type
• Special data type for simulation time measuring
• Declaration Dữ liệu để đo thời gian mô phỏng
time my_time;
• Use inside procedure
Lấy thời gian SIM hiện tại
my_time = $time; // get current sim time
• Simulation runs at simulation time, not real time
39 Verilog HDL Basics
Arrays (i)
• Syntax
integer count[1:5]; // 5 integers
reg var [-15:16]; // 32 1-bit regs, var[-15] var[16]
reg [1:0] mem1D;//2 bit
reg [7:0] mem [0:1023]; // 1024 8-bit regs (bus 8)
//mem[0]: 8-bit register mem[0] = row[0]
//mem[0][5:0]: first 6-bit of register mem[0]
//reg array [7:0] = '{0,0,0,0,0,0,0,1};//8 1-bit regs
0 8-bit
• Accessing array elements 1 8-bit
… ……
– Entire element: mem[1] = 8’b 10101010;
– Element subfield (needs temp storage): 1023 8-bit
reg [7:0] temp; Trường con của phần
mem (2D)
temp = mem[10]; tử (cần
40 lưu trữ tạm Verilog HDL Basics
var[6] = temp[2]; thời)
Arrays (ii)
• Limitation: Cannot access array subfield or entire
array at once Hạn chế: Không thể truy cập trường con của
mảng hoặc toàn bộ mảng cùng một lúc.
var[2:9] = ???; // WRONG!!
var = ???; // WRONG!!
• No multi-dimentional arrays Không có mảng đa chiều
reg var[1:10] [1:100]; // WRONG!!
• Arrays don’t work for the Real data type
real r[1:10]; // WRONG !!
Mảng không hoạt động cho kiểu dữ liệu thực.
41 Verilog HDL Basics
Strings
• Implemented with regs:
reg [8*13:1] string_val; // can hold up to 13 chars
..
string_val = “Hello Verilog”;
string_val = “hello”; // MS Bytes are filled with 0
string_val = “I am overflowed”; // “I ” is truncated
>>”am overflowed”
• Escaped chars:
– \n newline
Khi khai báo không đủ
– \t tab thì sẽ cắt trọng số cao đi
– %% %
– \\ \
– \“ “
42 Verilog HDL Basics
Outline
• Verilog?
• Module
• Instances
• User Identifiers
• Numbers in Verilog
• Signal and Nets in Verilog
• Registers
• Vectors and Arrays
• Logical vs Bitwise Operators
• Conditional Operator
• Continuous Assignments
• Structural Model (Gate Level)
• Behavioral Model – Procedures: Initial vs Always
• Procedural Statements: If – case – for
• System Tasks
43 Verilog HDL Basics
Logical Operators
• && logical AND
• || logical OR
• ! logical NOT
• Operands evaluated to ONE bit value: 0, 1 or x
• Result is ONE bit value: 0, 1 or x
A = 6; A && B 1 && 0 0
B = 0; A || !B 1 || 1 1
C = x; C || B x || 0 x but C&&B=0
44 Verilog HDL Basics
Bitwise Operators (i)
• & bitwise AND
• | bitwise OR
• ~ bitwise NOT
• ^ bitwise XOR
• ~^ or ^~ bitwise XNOR
• Operation on bit by bit basis
45 Verilog HDL Basics
Bitwise Operators (ii)
c = ~a; c = a & b;
• a = 4’b1010;
b = 4’b1100;
c = a ^ b;
• a = 4’b1010;
b = 2’b11;
46 Verilog HDL Basics
Reduction Operators
• & AND
• | OR
• ^ XOR
• ~& NAND
• ~| NOR
• ~^ or ^~ XNOR
• One multi-bit operand One single-bit result
a = 4’b1001;
..
c = |a; // c = 1|0|0|1 = 1
47 Verilog HDL Basics
Shift Operators
• >> shift right
• << shift left
• Result is same size as first operand, always zero filled
a = 4’b1010;
...
d = a >> 2; // d = 0010
c = a << 1; // c = 0100
48 Verilog HDL Basics
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
49 Verilog HDL Basics
Relational Operators
• > greater than
• < less than
• >= greater or equal than
• <= less or equal than
• Result is one bit value: 0, 1 or x
1 > 0 1
’b1x1 <= 0 x
10 < z x
50 Verilog HDL Basics
Equality Operators
• == logical equality
Return 0, 1 or x
• != logical inequality
• === case equality
Return 0 or 1
• !== case inequality
(x's are compared, and the result is 1)
– 4’b 1001 == 4’b 1101 0
– 4’b 1z0x == 4’b 1z0x x
– 4’b 1z0x === 4’b 1z0x 1
– 4’b 1z0x !== 4’b 1z0x
51 0 Verilog HDL Basics
Outline
• Verilog?
• Module
• Instances
• User Identifiers
• Numbers in Verilog
• Signal and Nets in Verilog
• Registers
• Vectors and Arrays
• Logical vs Bitwise Operators
• Conditional Operator
• Continuous Assignments
• Structural Model (Gate Level)
• Behavioral Model – Procedures: Initial vs Always
• Procedural Statements: If – case – for
• System Tasks
52 Verilog HDL Basics
Conditional Operator
• cond_expr ? true_expr : false_expr
• Like a 2-to-1 mux ..
A true(1)
1
Y
Y = (sel==1’b1)? A : B;
B 0
sel false(0)
sel Y
1 A
53 Verilog HDL Basics
0 B
Conditional Operator
• Like a 4-to-1 mux .. A
B sel Y
Y
C 00 A
01 B
D
10 C
11 D
sel(1:0)
true true true
Y = (sel==2’b00)? A :((sel==2’b01)? B:((sel==2’b10)? C : D));
false false false
Arithmetic Operators (i)
• +, -, *, /, %
• If any operand is x the result is x
Các thanh ghi có thể được gán giá trị
• Negative registers: âm nhưng được coi là không có dấu.
– regs can be assigned negative but are treated as unsigned
reg [15:0] regA;
..
regA = -4’d12; // stored as 216-12 = 65524
regA/3 evaluates to 21861
55 Verilog HDL Basics
Arithmetic Operators (ii)
• Negative integers:
– can be assigned negative values
– different treatment depending on base specification or not
reg [15:0] regA;
integer intA;
..
intA = -12/3; // evaluates to -4 (no base spec)
intA = -’d12/3; // evaluates to 1431655761 (base spec)
56 Verilog HDL Basics
Operator Precedence
Use parentheses to
enforce your
priority
57 Verilog HDL Basics
Hierarchical Design
Top Level
E.g.
Module
Full Adder
Sub-Module Sub-Module
1 2
Half Adder Half Adder
Basic Module Basic Module Basic Module
1 2 3
58 Verilog HDL Basics
Module
module my_module(out1, .., inN);
out1 output out1, .., outM;
in1 my_module
out2 input in1, .., inN;
in2
f .. // declarations
inN outM .. // description of f (maybe
.. // sequential)
endmodule
Everything you write in Verilog must be inside a module
exception: compiler directives
59 Verilog HDL Basics
Example: Half Adder
A S module half_adder(S, C, A, B);
Half output S, C;
B Adder C input A, B;
//wire S, C, A, B;
A
S assign S = A ^ B;//xor
B assign C = A & B;//and
C
endmodule
60 Verilog HDL Basics
Example: Full Adder
in1 A Half S I1 A Half S sum
Adder 1 Adder
in2 B C I2 B C I3
ha1 ha2 cout
cin
module full_adder(sum, cout, in1, in2, cin);
output sum, cout;
input in1, in2, cin;
//wire sum, cout, in1, in2, cin;
Module wire I1, I2, I3; Instance
name name
half_adder ha1(.A(in1), .B(in2), .S(I1), .C(I2));
half_adder ha2(.A(I1), .B(cin), .S(sum), .C(I3));
assign cout = I2 || I3;
endmodule … ha1(.A(in1), .B(in2), .S(I1), .C(I2));
61 Verilog HDL Basics
Hierarchical Names
ha2.A
in1 A Half S I1 A Half S sum
Adder 1 Adder
in2 B C I2 B C I3
ha1 ha2 cout
cin
Remember to use instance names,
not module names
62 Verilog HDL Basics
Port Assignments
module
• Inputs reg or net net
module
• Outputs reg or net net
module
net net
• Inouts
63 Verilog HDL Basics
Outline
• Verilog?
• Module
• Instances
• User Identifiers
• Numbers in Verilog
• Signal and Nets in Verilog
• Registers
• Vectors and Arrays
• Logical vs Bitwise Operators
• Conditional Operator
• Continuous Assignments
• Structural Model (Gate Level)
• Behavioral Model – Procedures: Initial vs Always
• Procedural Statements: If – case – for
• System Tasks
64 Verilog HDL Basics
Continuous Assignments
a closer look
• Syntax:
assign #del <id> = <expr>;
optional net type !!
• Where to write them: Nơi để viết:
– inside a module – bên trong một mô-đun
– bên ngoài quy trình
– outside procedures
• Properties: Tính chất:
– tất cả đều thực thi song song
– they all execute in parallel – độc lập với thứ tự
– are order independent – luôn luôn hoạt động
– are continuously active
65 Verilog HDL Basics
Outline
• Verilog?
• Module
• Instances
• User Identifiers
• Numbers in Verilog
• Signal and Nets in Verilog
• Registers
• Vectors and Arrays
• Logical vs Bitwise Operators
• Conditional Operator
• Continuous Assignments
• Structural Model (Gate Level)
• Behavioral Model – Procedures: Initial vs Always
• Procedural Statements: If – case – for
• System Tasks
66 Verilog HDL Basics
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
67 Verilog HDL Basics
Example: Half Adder,
2nd Implementation
module half_adder(S, C, A, B);
A output S, C;
S
input A, B;
B
C wire S, C, A, B;
xor #2 (S, A, B);
//assign #2 S = A ^ B;//xor
Assuming: and #1 (C, A, B);
• XOR: 2 t.u. delay
• AND: 1 t.u. delay endmodule
68 Verilog HDL Basics
Outline
• Verilog?
• Module
• Instances
• User Identifiers
• Numbers in Verilog
• Signal and Nets in Verilog
• Registers
• Vectors and Arrays
• Logical vs Bitwise Operators
• Conditional Operator
• Continuous Assignments
• Structural Model (Gate Level)
• Behavioral Model – Procedures: Initial vs Always
• Procedural Statements: If – case – for
• System Tasks
69 Verilog HDL Basics
Behavioral Model - Procedures (i)
• Procedures = sections of code that we know they execute
sequentially
• Procedural statements = statements inside a procedure
(they execute sequentially)
A
• e.g. another 2-to-1 mux implem: 1
Y
sel Y
begin 1 A
B 0
if (sel == 0) 0 B
sel Y = (sel)? A : B;
Y = B;
Execution else
Flow Procedural assignments:
Y = A;
Y must be reg !!
end
70 Verilog HDL Basics
Các quy trình thực thi song song
Behavioral Model - Procedures (ii)
• Modules can contain any number of procedures
• Procedures execute in parallel (in respect to each
other) and ..
• .. can be expressed in two types of blocks:
– initial they execute only once
– always they execute for ever (until simulation finishes)
71 Verilog HDL Basics
“Initial” Blocks
• Start execution at simulation time zero and finish
when their last statement executes
module nothing;
initial
$display(“I’m first”); Will be displayed
at sim time 0
initial
begin
#50; Will be displayed
$display(“Really?”); at sim time 50
end
endmodule
72 Verilog HDL Basics
“Always” Blocks
• Start execution at sim time zero and continue until
sim finishes
always
begin
if (sel == 0)
Y = B;
else
Y = A;
end
end
73 Verilog HDL Basics
Events (i)
• @
always @(signal1 or signal2 or ..)
begin
..
end
execution triggers every
always @(A,B,sel)
begin
time any signal changes
if (sel == 0)
Y = B;
else
Y = A; execution triggers every
end
end time clk changes
from 0 to 1
always @(posedge clk)
begin
.. execution triggers every
end
time clk changes
from 1 to 0
always @(negedge clk)
begin
.. 74 Verilog HDL Basics
end
Examples
• 3rd half adder implem • Behavioral edge-triggered
module half_adder(S, C, A, D-FlipFlop implem
B); module dff(Q, D, Clk);
output S, C; output Q;
input A, B; input D, Clk;
Clk Q
//wire A, B; //wire D, Clk; D
reg S,C;//result variable reg Q;//result variable
always @(A or B) always @(posedge Clk)
begin
Q = D;
S = A ^ B;
C = A & B;
endmodule
end
75 Verilog HDL Basics
endmodule
Chờ cho đk ctr1 trong ngoặc đúng thì
thực hiện câu lệnh trong wait ....
Events (ii)
• wait (expr)
always
begin execution loops every
wait (ctrl) time ctrl = 1 (level
#10 cnt = cnt + 1; sensitive timing control)
#10 cnt2 = cnt2 + 2;
end
• e.g. Level triggered DFF ?
76 Verilog HDL Basics
Example
always @(reset or posedge clk)
begin
reset if (reset)
a begin
Y Y = 0;
b
W = 0;
end
else // reset = 0
c W begin
Y = a & b;
clk
W = ~c;
end
end
77 Verilog HDL Basics
Timing (i)
d
initial
begin
c
#5 c = 1;
#5 b = 0; b
#5 d = c;
end 0 5 10 15
Time
Each assignment is
blocked by its previous one
Tham khảo thêm:
Video Hướng dẫn các bước làm BT TKVM (Lý thuyết)
Video Hướng dẫn sử dụng phần mềm Xilinx-ISE lập78trình RTL và Testbench Verilog HDL Basics
Các câu lệnh trong fork - join
cùng thực hiện 1 lúc
Timing (ii)
d
initial begin
fork c
#5 c = 1;
#5 b = 0; b
#5 d = c;
join 0 5 10 15
end Time
Assignments are
not blocked here
79 Verilog HDL Basics
Outline
• Verilog?
• Module
• Instances
• User Identifiers
• Numbers in Verilog
• Signal and Nets in Verilog
• Registers
• Vectors and Arrays
• Logical vs Bitwise Operators
• Conditional Operator
• Continuous Assignments
• Structural Model (Gate Level)
• Behavioral Model – Procedures: Initial vs Always
• Procedural Statements: If – case – for
• System Tasks
80 Verilog HDL Basics
Procedural Statements: if
E.g. 4-to-1 mux:
module mux4_1(in, sel, out);
output out;
if (expr1) input [3:0] in;
true_stmt1; input [1:0] sel;
//wire [3:0] in;
else if (expr2) //wire [1:0] sel;
reg out;
true_stmt2;
.. always @(in or sel)
if (sel == 0)
else out = in[0];
def_stmt; else if (sel == 1)
in[0] out = in[1];
else if (sel == 2)
out = in[2];
out else
in[3]
out = in[3];
endmodule
81 Verilog HDL Basics
sel[1:0]
Procedural Statements: case
E.g. 4-to-1 mux:
module mux4_1(out, in, sel);
case (expr) output out;
input [3:0] in;
input [1:0] sel;
item_1: statement1;
reg out;
item_2: statement2; //wire [3:0] in;
.. //wire [1:0] sel;
default: def_statement; always @(in or sel)
case (sel)
0: out = in[0];
endcase 1: out = in[1];
2: out = in[2];
3: out = in[3];
endcase
endmodule
82 Verilog HDL Basics
Procedural Statements: for
for (init_assignment; cond; step_assignment)
stmt;
E.g.
module count(Y, start);
output [3:0] Y;
input start;
reg [3:0] Y;
wire start;
integer i;
initial
Y = 0;
always @(posedge start)
for (i = 0; i < 3; i = i + 1)
#10 Y = Y + 1;
endmodule
83 Verilog HDL Basics
Procedural Statements: while
E.g.
module count(Y, start);
output [3:0] Y;
input start;
reg [3:0] Y;
wire start;
integer i;
while (expr) stmt;
initial
Y = 0;
always @(posedge start) begin
i = 0;
while (i < 3) begin
#10 Y = Y + 1;
i = i + 1;
end
end
endmodule
84 Verilog HDL Basics
Procedural Statements: repeat
repeat(n) --> chỉ định câu
lệnh lặp lại n lần E.g.
module count(Y, start);
output [3:0] Y;
input start;
repeat (times) stmt; reg [3:0] Y;
wire start;
initial
Can be either an Y = 0;
integer or a variable
always @(posedge start)
repeat (4) #10 Y = Y + 1;
endmodule
85 Verilog HDL Basics
Procedural Statements: forever
Tclk = 20 time units Typical example:
clock generation in test modules
10 module test;
reg clk;
// clock generation
10 Tclk = 20 time units
initial begin
forever stmt; clk = 0;
forever #10 clk = ~clk;
end
Executes until sim dff IC1(clk, D, Q);
finishes //other_module2 o2(.., clk, ..);
initial begin
D = 1’b0;//testcase 1
#100;
D = 1’b1;//testcase 2
end
endmodule
86 Verilog HDL Basics
Testing Your Modules
Input output
Testbench Verilog Module
87 Verilog HDL Basics
Testing Your Modules
module top_test;
wire [1:0] t_out; // Top’s signals
reg [3:0] t_in;
reg clk;
top inst(t_out, t_in, clk); // Top’s instance
initial begin // Generate clock
clk = 0;
forever #10 clk = ~clk;
end
initial begin // Generate remaining inputs
$monitor($time, " %b -> %b", t_in, t_out);
#5 t_in = 4'b0101;
#20 t_in = 4'b1110;
#20 t_in[0] = 1;
#300 $finish;
end
endmodule
88 Verilog HDL Basics
Mixed Model
Code that contains various both structure and behavioral styles
module simple(Y, c, clk, res);
output Y;
input c, clk, res;
reg Y;
wire c, clk, res;
res wire n;
c n Y not(n, c); // gate-level
clk
always @(res or posedge clk)
if (res)
Y = 0;
else
Y = n;
endmodule
89
Outline
• Verilog?
• Module
• Instances
• User Identifiers
• Numbers in Verilog
• Signal and Nets in Verilog
• Registers
• Vectors and Arrays
• Logical vs Bitwise Operators
• Conditional Operator
• Continuous Assignments
• Structural Model (Gate Level)
• Behavioral Model – Procedures: Initial vs Always
• Procedural Statements: If – case – for
• System Tasks
90 Verilog HDL Basics
System Tasks
Always written inside procedures
• $display(“..”, arg2, arg3, ..); much like printf(), displays formatted string
in std output when encountered
• $monitor(“..”, arg2, arg3, ..); like $display(), but .. displays string each
time any of arg2, arg3, .. Changes
• $stop; suspends sim when encountered
• $finish; finishes sim when encountered
• $fopen(“filename”); returns file descriptor (integer); then, you can use
$fdisplay(fd, “..”, arg2, arg3, ..); or $fmonitor(fd, “..”, arg2, arg3, ..); to write
to file
• $fclose(fd); closes file
• $random(seed); returns random integer; give her an integer as a seed
91 Verilog HDL Basics
$display & $monitor string format
92 Verilog HDL Basics
Compiler Directives
• `include “filename” inserts contents of file into current file; write it
anywhere in code ..
• `define <text1> <text2> text1 substitutes text2;
– e.g. `define BUS reg [31:0] in declaration part: `BUS data;
• `timescale <time unit>/<precision>
– e.g. `timescale 10ns/1ns later: #5 a = b;
Đơn vị TG/ Độ chính xác 50ns
93 Verilog HDL Basics
Parameters
in[3:0] p_in[3:0]
out[2:0]
wu
A. Implelementation
without parameters
wd
clk
module dff4bit(Q, D, clk); module dff2bit(Q, D, clk);
output [3:0] Q; output [1:0] Q;
input [3:0] D; input [1:0] D;
input clk; input clk;
reg [3:0] Q; reg [1:0] Q;
wire [3:0] D; wire [1:0] D;
wire clk; wire clk;
always @(posedge clk) always @(posedge clk)
Q = D; Q = D;
endmodule endmodule
94 Verilog HDL Basics
Parameters
(ii)
module top(out, in, clk);
output [1:0] out;
A. Implelementation input [3:0] in;
input clk;
without parameters (cont.)
wire [1:0] out;
wire [3:0] in;
wire clk;
wire [3:0] p_in; // internal nets
wire wu, wd;
assign wu = p_in[3] & p_in[2];
assign wd = p_in[1] & p_in[0];
dff4bit instA(p_in, in, clk);
dff2bit instB(out, {wu, wd}, clk);
// notice the concatenation!!
endmodule
95 Verilog HDL Basics
Parameters
(iii)
module top(out, in, clk);
B. Implelementation output [1:0] out;
with parameters input [3:0] in;
input clk;
wire [1:0] out;
module dff(Q, D, clk); wire [3:0] in;
wire clk;
parameter WIDTH = 4;
output [WIDTH-1:0] Q; wire [3:0] p_in;
input [WIDTH-1:0] D; wire wu, wd;
input clk;
assign wu = p_in[3] & p_in[2];
reg [WIDTH-1:0] Q; assign wd = p_in[1] & p_in[0];
wire [WIDTH-1:0] D;
wire clk; dff instA(p_in, in, clk);
// WIDTH = 4, from declaration
always @(posedge clk) dff instB(out, {wu, wd}, clk);
Q = D; defparam instB.WIDTH = 2;
// We changed WIDTH for instB only
endmodule
endmodule
96 Verilog HDL Basics
Outline
• Verilog?
• Module
• Instances
• User Identifiers
• Numbers in Verilog
• Signal and Nets in Verilog
• Registers
• Vectors and Arrays
• Logical vs Bitwise Operators
• Conditional Operator
• Continuous Assignments
• Structural Model (Gate Level)
• Behavioral Model – Procedures: Initial vs Always
• Procedural Statements: If – case – for
• System Tasks
97 Verilog HDL Basics
• EXAMPLES
Example Codes
• Yêu cầu:
– Đọc hiểu 1 đoạn CODE MẪU
– Làm lại các bước thiết kế (xem Video HD các bước
làm 1 BT TKVM trên LMS): làm trên giấy + chụp hình
hoặc viết file WORD submit Assignment x
• Thiết kế SĐK (~sơ đồ nguyên lý)
• Bản chân trị/bản sự thật
• Vẽ lưu đồ giải thuật
• Code lập trình (đã có, ko cần viết lại)
– Làm simulation (xem Video HD sử dụng Xilinx ISE
để lập trình RTL và simulation) OPTIONAL
Encoder - Using if-else Statement
• http://www.asic-
world.com/examples/verilog/encoder.html#Encod
er_-_Using_if-else_Statement
99 Verilog HDL Basics
Encoder - Using case Statement
• http://www.asic-
world.com/examples/verilog/encoder.html#Encod
er_-_Using_if-else_Statement
100 Verilog HDL Basics
Pri-Encoder - Using if-else Statement
• http://www.asic-
world.com/examples/verilog/pri_encoder.html#Pri
ority_Encoders
101 Verilog HDL Basics
Encoder - Using assign Statement
• http://www.asic-
world.com/examples/verilog/pri_encoder.html#Pri
ority_Encoders
102 Verilog HDL Basics
Decoder - Using case Statement
• http://www.asic-
world.com/examples/verilog/decoder.html#Decod
er_-_Using_case_Statement
103 Verilog HDL Basics
Mux : Using assign Statement
104 Verilog HDL Basics
Mux : Using if Statement
105 Verilog HDL Basics
Mux : Using case Statement
106 Verilog HDL Basics
in[3:0]
data_out
sel[1:0] MUX_4-to-1
en
HL
(high or low logic)
module MUX_4-to-1(in, sel, en, HL, data_out);
input [3:0] in;
input [1:0] sel;
input en, HL;
output data_out;
assign data_out = (HL==1’b1 && en)? in[sel]: ((HL==1’b0 && en)? ~in[sel]:
1’b0);
endmodule 107
in[3:0]
data_out[1:0]
en Encoder_4-to-2
HL
module Encoder_4-to-2(in, en, HL, data_out);
input [3:0] in; input en, HL;
output [1:0] data_out;
reg [1:0] temp;
always @(in, en, HL)
begin
if (en)
if (in==4’b0001) temp = 2’b01;
…
else temp = 2’b00;
end
assign data_out = (HL==1’b1)? temp: ~temp;
endmodule
in[2:0]
data_out[3:0]
en Decoder_2-to-4
HL
module Decoder_2-to-4(in, en, HL, data_out);
input [1:0] in; input en, HL;
output [3:0] data_out;
reg [1:0] temp;
endmodule