Professional Documents
Culture Documents
Verilog-HDL Programming
Verilog-HDL Programming
Verilog-HDL Programming
Giới thiệu
Mạch tổ hợp
Xử lý tuần tự
1
Giới thiệu
Chuẩn hóa: IEEE 1364 năm 1995, cuối cùng IEEE 1364-
2001
2
Qui định
3
Từ khóa
always endmodule large reg
and endprimitive macromodule release
tranif0
assign endspecify nand repeat
tranif1
attribute endtable negedge rnmos
tri
begin endtask nmos rpmos
tri0
buf event nor rtran
tri1
bufif0 for not rtranif0
triand
bufif1 force notif0 rtranif1
trior
case forever notif1 scalared
trireg
casex fork or signed
unsigned
casez function output small
vectored
cmos highz0 parameter specify
wait
deassign highz1 pmos specparam
wand
default if posedge strength
weak0
defparam ifnone primitive strong0
weak1
disable initial pull0 strong1
while
edge inout pull1 supply0
wire
else input pulldown supply1
wor
end integer pullup table
xnor
endattribute join rcmos task
xor
endcase medium real time
endfunction module realtime tran
4
1. Những khái niệm cơ bản
Cấu trúc cơ bản của mạch thiết kế
Khai báo
5
Cấu trúc cơ bản của Verilog-HDL
Endmodule Endmodule
6
Khai báo
7
Mô tả mạch thiết kế
Mô tả tín hiệu xử lý
assign SUM = A ^ B;
assign CARRY = A & B;
A SUM
Endmodule
B CARRY
input HALFADD output
8
Chú thích
Input A, B;
Output SUM, CARRY;
Ví dụ:
/* Đây là phần mô tả
mạch thiết kế */
Assign SUM = A ^ B;
Assign CARRY = A & B;
Endmodule
9
Biểu diễn giá trị logic
10
Biểu diễn giá trị hệ cơ số
11
Biểu diễn giá trị hệ cơ số (cont.)
Ví dụ:
12
Khai báo tín hiệu
13
Lược bỏ khai báo Module DFF (CLK, D, Q);
Input CLK, D;
Output Q;
Wire CLK, D; // Có thể lược bỏ
Reg Q;
Verilog-HDL cho phép
lược bỏ khai báo Always @ (posedge CLK)
begin
Q <= D;
end
VD1: Endmodule
Lược bỏ khai báo Net
Tín hiệu khai báo cho Port Module RSFF(SB, RB, Q);
mặc định là kiểu Net và có Input SB, RB;
thể lược bỏ Output Q;
Wire tmp; // Có thể lược bỏ
Reg Q;
VD2:
Nand na1 (Q, SB, tmp);
Những kết nối 1 bit bên trong Nand na2 (tmp, Q, RB);
Module: có thể lược bỏ, Tuy
nhiên, khó cho việc Debug Endmodule
14
Tín hiệu nhiều bit
Assign c[7:4] = a;
Assign c[3:0] = b;
Assign d = bus_a[3:0];
15
Phương pháp thiết kế
Sub-Block1 Sub-Block2
Leaf-cell Leaf-cell
Top-Level
Bottom-up design
Macro-cell1 Macro-cell2
Leaf-cell Leaf-cell
16
Tích hợp Module (Module Instance)
17
Example of Module Instance
Module DFF4 (CLK, D, Q);
Input CLK;
Input [3:0] D;
Output [3:0] Q; Module DFF (CLK, D, Q);
// Kết nối theo tên Port Input CLK, D;
Output Q;
DFF DFF2 (.CLK(CLK), .D(D[2]), .Q(Q[2]));
Reg Q;
DFF DFF3 (.D(D[3]), .CLK(CLK),.Q(Q[3]));
DFF DFF1 (.CLK(CLK), .D(D[1]), .Q(Q[1]));
Always @ (posedge CLK)
DFF DFF0 (.CLK(CLK), .D(D[0]), .Q(Q[0]));
begin
Q <= D;
Kết nối theo thứ tự Port end
// DFF DFF2 (CLK, D[2], Q[2]); Endmodule
// DFF DFF3 (CLK, D[3], Q[3]);
// DFF DFF1 (CLK, D[1], Q[1]);
// DFF DFF0 (CLK, D[0], Q[0]);
Endmodule
18
Ví dụ thiết kế Full Adder
Module HALFADD (A, B, SUM, CARRY);
Input A, B;
Module fulladd (Ain, Bin, Cin, Sout, Cout); Output SUM, CARRY;
Input Ain, Bin, Cin;
Assign SUM = A ^ B;
Output Sout, Cout; Assign CARRY = A & B;
Wire u0_S, u0_Co, u1_Co; Endmodule
19
Toán tử (operators)
Usage Description
Arithmetic Operators
+ m+n Add n to m
- m-n Subtract n from m
- -m Negate m (2's complement)
* m*n Multiply m by n
/ m/n Divide m by n
% m%n Modulus of m / n
20
Toán tử (operators) cont.
Bitwise Operators
~ ~m Invert each bit of m
& m&n AND each bit of m with each bit of n
| m|n OR each bit of m with each bit of n
^ m^n Exclusive OR each bit of m with n
~^ m ~^ n
Exclusive NOR each bit of m with n
^~ m ^~ n
Reduction Operators
& &m AND all bits in m together (1-bit result)
~& ~&m NAND all bits in m together (1-bit result)
| |m OR all bits in m together (1-bit result)
~| ~|m NOR all bits in m together (1-bit result)
^ ^m Exclusive OR all bits in m (1-bit result)
~^ ~^m
Exclusive NOR all bits in m (1-bit result)
^~ ^~m
21
Toán tử (operators) cont.
Logical Operators
! !m Is m not true? (1-bit True/False result)
&& m && n Are both m and n true? (1-bit True/False result)
|| m || n Are either m or n true? (1-bit True/False result)
Equality Operators (compares logic values of 0 and 1
== m == n Is m equal to n? (1-bit True/False result)
!= m != n Is m not equal to n? (1-bit True/False result)
Identity Operators (compares logic values of 0, 1, X and Z
=== m === n Is m identical to n? (1-bit True/False results)
!== m !== n Is m not identical to n? (1-bit True/False result)
22
Toán tử (operators) cont.
Relational Operators
< m<n Is m less than n? (1-bit True/False result)
> m>n Is m greater than n? (1-bit True/False result)
<= m <= n Is m less than or equal to n? (True/False result)
23
Phép tính Bit
~ & | ^ ~^
(NOT) (AND) (OR) (XOR) (Ex-NOR)
A1
Ví dụ: wire A2, A1, A0, B2, B1, B0; A2
assign A2 = A1 & A0; A0
24
Phép dịch Bit
<< >>
(Left shift) (Right shift)
wire [7:0] a, b; 1 0 0 1 0 1 0 1
assign a = b << 1;
1 0 0 1 0 1 0 1 0
Output
wire [7:0] a, b; 1 0 0 1 0 1 0 1
assign a = b >> 2;
0 0 1 0 0 1 0 1 0 1
Output
25
Phép tính rút gọn
& ~& | ~| ^ ~^
(NOT) (AND) (OR) (NOR) (XOR) (Ex-NOR)
assign all = cnt[5] & cnt[4] & cnt[3] & cnt[2] & cnt[1] & cnt[0];
assign parity = cnt[5] ^ cnt[4] ^ cnt[3] ^ cnt[2] ^ cnt[1] ^ cnt[0];
26
Phép tính liên kết
{}
27
Phép tính so sánh
=== và !== là phép so sánh đồng nhất, sử dụng cho các tín
hiệu có trạng thái x, z
28
Phép tính Logic
! && ||
Các phép logic cho kết quả True, False (phủ định, AND, OR)
29
Phép tính số học
+ - * / %
Riêng các phép “/” và “%” không thể tổng hợp (synthesize),
chỉ có thể dùng đến mức mô phỏng
30
Phép tính lựa chọn
? :
wire [7:0] a, b, c;
assign a = (sel == 1’b1)? b: c;
TRUE FALSE
31
Trật tự ưu tiên
Operator Precedence
highest precedence
!~+-
*/ %
+-
<< >>
< <= > >=
== != === !==
& ~& ^ ~^
| ~|
&&
||
?:
lowest precedence
32
Bài tập
33
2. Mạch tổ hợp
34
Mạch tổ hợp (Combinational Circuit)
35
Phát biểu assign
input [1:0] in0, in1; input sel;
output [1:0] out;
Bộ chọn (Selector):
assign out = (sel == 1’b1)? in1: in0;
Bộ đệm 3 trạng thái (three-state buffer):
output [7:0] data; wire sel;
sel
data wire [7:0] dataout;
dataout
assign data = (sel)? dataout: 8’hz;
36
Phát biểu always (1)
Module Architecture(…);
Các khối always được xử Input …;
lý song song Output …; Xử lý đồng thời
// concurrent statements
Always @(…)
Begin
Các phát biểu nằm trong // sequential statements
always được xử lý tuần tự End
Xử lý đồng thời
// concurrent statements
Always @(…)
Begin
// sequential statements
End
Endmodule
37
Phát biểu always (2)
38
Phát biểu always (3)
39
Phát biểu always (4)
40
Phát biểu if
Cú pháp:
If (condition1) begin if (sel == 2’h0)
processing statements; out = in[0];
else if (sel == 2’h1)
end else if (condition2) begin out = in[1];
processing statements; else if (sel == 2’h2)
end else begin out = in[2];
else
processing statements; out = in[3];
end
Điều kiện đầu tiên có độ ưu tiên cao nhất
Trường hợp chỉ có 1 phát biểu begin end
có thể lược bỏ
41
Phát biểu case, casex
42
Phát biểu function
Module DEC2TO4 (in, out);
Cú pháp: Input [1:0] in;
Có thể nhiều đầu vào Output [3:0] out;
function […] func_name;
input port_name; Function [3:0] dec;
Input [1:0] in;
begin Begin
processing statements; case (in)
0: dec = 4’h1;
end 1: dec = 4’h2;
2: dec = 4’h4;
3: dec = 4’h8;
Có thể kết hợp sử dụng với
Có thể gọi nhiều lần
endcase
End
if, case, casex Endfunction
43
3. Xử lý tuần tự
Sử dụng always
Reset đồng bộ và không đồng bộ
Bài tập
44
Xử lý tuần tự
45
DFF với Reset đồng bộ
Module SYNC_FF(CLK, RST, D, Q);
Input CLK, RST, D; CLK
Output Q;
Reg Q; RST
46
DFF với Reset không đồng bộ
Module SYNC_FF(CLK, RST, D, Q);
Input CLK, RST, D; CLK
Output Q;
Reg Q; RST
Endmodule
47
Bài tập lớn M1 M0
A
Y
B
ALU Cout
1. Thiết kế bộ xử lý logic Cin
số học (Arithmetic
Logic Unit - ALU) OE
48