03 Digital Design Using Verilog
03 Digital Design Using Verilog
HỒ CHÍ MINH
ĐẠI HỌC BÁCH KHOA
NGÀNH KỸ THUẬT ĐIỆN TỬ
Chapter 3
Digital Design Using Verilog
PGS.TS. HOÀNG TRANG
Bộ môn Kỹ Thuật Điện Tử
hoangtrang@hcmut.edu.vn
1
Cell Base Design Flow Review
Support Tools
Design flow Output
(Languages)
Specifications <file>.docx/xls/ppt
NG Report file,
VCS/ModelSim,
RTL Verification wave form
etc(Verilog/ VHDL)
<file>.v (netlist),
Synthesis DC compiler <file>.sdf,
Reports
NG
Netlist Verification Formality Report file
Place&Route <file>.gds
ICC compiler
2
Cell Base Design Flow Review
Support Tools
Design flow Output
(Languages)
Specifications <file>.docx/xls/ppt
Place&Route <file>.gds
ICC compiler
3
Cell Base Design Flow Review
Design flow
Specifications
System Level
Design
NG
RTL Design
NG
RTL Verification Can simulate, Tools are not
but cannot enough excellent
Synthesis
synthesize to to understand
Netlist Verification Gate Level your writing
4
Agenda
Introduction
5
Introduction
Verilog, standardized as IEEE 1364, is a hardware
description language (HDL) used to model electronic systems.
It is most commonly used in the design and verification of
digital circuits at the register-transfer level of abstraction. It is
also used in the verification of analog circuits and mixed-signal
circuit.
6
Agenda
Introduction
7
Behavior level
Verilog Language
.................
SUCCESS
FAIL
8
Behavior level
Verilog Language
9
Behavior level
Verilog Language
• Red Green : 30 s
• Green Yellow : 27s
• Yellow Red :3s
10
Behavior level
• Red Green : 30 s
• Green Yellow : 27s
• Yellow Red :3s
Initial begin
while (1) begin
Red = 1; Yellow = 0; Green = 0;
# 30;
Red = 0; Yellow = 0; Green = 1;
# 27;
Red = 0; Yellow = 1; Green = 0;
#3;
end
end
11
Behavior level
• Red Green : 30 s
• Green Yellow : 27s
• Yellow Red :3s
Verificataion
12
Behavior level
• Red Green : 30 s
• Green Yellow : 27s
• Yellow Red :3s
Initial begin
while (1) begin
Can simulate, Red = 1; Yellow = 0; Green = 0;
# 30;
but cannot be Red = 0; Yellow = 0; Green = 1;
# 27;
synthesized to Red = 0; Yellow = 1; Green = 0;
Gate Level #3;
end
end
13
Cell Base Design Flow Review
Design flow
Specifications
System Level
Design
NG
RTL Design
NG
RTL Verification Can simulate, Tools are not
but cannot enough excellent
Synthesis
synthesize to to understand
Netlist Verification Gate Level your writing
14
Cell Base Design Flow Review
Synthesize Report
Netlist
Verilog 15
Writing Verilog code for what ?
Writing Verilog
Code for What ?
16
How to write Verilog code well ?
Verilog Basic
17
How to write Verilog code well ?
Verilog Basic
18
Synthesized Verilog Introduce Components
Components
Combination Sequential
components components
19
Synthesized Verilog Introduce
Components
Combination Sequential
components components
Comb1
DFF DFF
Comb 3
DFF
Comb 2
DFF
DFF
20
Synthesized Verilog Introduce Prototype
assign/always@ always@
Comb1
DFF DFF
Comb 3
DFF
Comb 2
DFF
DFF
21
Synthesized Verilog Introduce Thinking Flow
Specification
we have idea
22
Synthesized Verilog Introduce Thinking Flow
Specification
we have idea
23
How to write Verilog code well ?
Verilog Basic
24
Verilog Basic Fundamentals
B
A
C B
A
C
25
Verilog Basic Fundamentals
B assign A = (S == 1)?B:C;
A
C always@(S or B or C) begin
B if(S == 1) begin
A A <= B;
assign A = B&C; C end
else begin
always@(B or C) begin
A <= C;
A <= B&C;
end
end
end
27
Verilog Basic Fundamentals
28
Verilog Basic Fundamentals
always@(X) begin
X[2] X[1] X[0] Y[1:0]
end
0 0 0 01
0 0 1 10
0 1 0 11
0 1 1 00
-------- --------- -------- 00
29
Verilog Basic Fundamentals
always@(X) begin
X[2] X[1] X[0] Y[1:0]
case (X)
0 0 0 01 endcase
0 0 1 10 end
0 1 0 11
0 1 1 00
-------- --------- -------- 00
30
Verilog Basic Fundamentals
always@(X) begin
X[2] X[1] X[0] Y[1:0]
case (X)
0 0 0 01 3’b000: begin
0 0 1 10 end
3’b001: begin
0 1 0 11 end
0 1 1 00 3’b010: begin
-------- --------- -------- 00 end
default : begin
end
endcase
end
31
Verilog Basic Fundamentals
always@(X) begin
X[2] X[1] X[0] Y[1:0]
case (X)
0 0 0 01 3’b000: begin
0 0 1 10 Y <= 2’b01;
end
0 1 0 11 3’b000: begin
0 1 1 00 Y <= 2’b10;
-------- --------- -------- 00 end
3’b000: begin
Y <= 2’b11;
end
default : begin
Y <= 2’b00;
end
endcase
end
32
Verilog Basic Fundamentals
always@(X) begin
X[2] X[1] X[0] Y[1:0]
case (X)
0 0 0 01 3’b000: begin
0 0 1 10 Y <= 2’b01;
end
0 1 0 11 3’b001: begin
0 1 1 00 Y <= 2’b10;
-------- --------- -------- 00 end
3’b010: begin
Y <= 2’b11;
end
default : begin
Y <= 2’b00;
end
endcase
end
33
Verilog Basic Fundamentals
always@(X) begin
X[2] X[1] X[0] Y[1:0]
if (X == 3’b000) begin
0 0 0 01 Y <= 2’b01;
0 0 1 10 end
else if (X == 3’b001) begin
0 1 0 11 Y <= 2’b10;
0 1 1 00 end
-------- --------- -------- 00 else if (X == 3’b010) begin
Y <= 2’b11;
end
else begin
Y <= 2’b00;
end
end
34
Verilog Basic Fundamentals
example1
35
Verilog Basic Fundamentals
example1 R
S Input
Output
Q Internal Signal
A
CLK
36
Verilog Basic Fundamentals
example1 R
S Input
Output
Q Internal Signal
A
CLK
Should be “reg/wire”
data type
37
Verilog Basic Fundamentals
wire D;
assign D = (S==0)?A:Q;
CLK
always@(posedge CLK or negedge R) begin
if(R == 0) begin
Q <= 0;
end
else begin
Q <= D;
end
end
endmodule 38
Verilog Basic Fundamentals
assign D = (S==0)?A:Q;
CLK
always@(posedge CLK or negedge R) begin
if(R == 0) begin
Q <= 0;
end
else begin
Q <= D;
end
end
endmodule 39
Verilog Basic Fundamentals
example1
S R
Q[2:0]
A [2:0]
CLK
40
Verilog Basic Fundamentals
S R parameter DATA_WIDTH = 3;
input CLK;
input R;
input S;
input [DATA_WIDTH-1:0] A;
Q[2:0]
A [2:0] output [DATA_WIDTH-1:0] Q
reg [DATA_WIDTH-1:0] Q;
wire [2:0] D;
assign D = (S==0)?A:Q;
endmodule
41
Verilog Basic Fundamentals
S R parameter DATA_WIDTH = 3;
input CLK;
input R;
input S;
input [DATA_WIDTH-1:0] A;
Q[2:0]
A [2:0] output [DATA_WIDTH-1:0] Q
reg [DATA_WIDTH-1:0] Q;
wire [2:0] D;
assign D = (S==0)?A:Q;
endmodule
42
Verilog Basic Fundamentals
parameter DATA_WIDTH = 3;
output [DATA_WIDTH-1:0] Q
Input Declaration
reg [DATA_WIDTH-1:0] Q;
assign D = (S==0)?A:Q;
Internal signal
always@(posedge CLK or negedge R) begin
if(R == 0) begin
Combination logic Q <= 0;
end
Sequential logic else begin
Q <= D;
end
end
endmodule
43
Verilog Basic Fundamentals
parameter DATA_WIDTH = 3;
output [DATA_WIDTH-1:0] Q
Input Declaration
reg [DATA_WIDTH-1:0] Q;
assign D = (S==0)?A:Q;
Internal signal
always@(posedge CLK or negedge R) begin
if(R == 0) begin
Combination logic Q <= 0;
end
Sequential logic else begin
Q <= D;
end
end
endmodule
44
Module
Verilog Basic Declaration
45
Module
Verilog Basic Declaration
46
Module
Verilog Basic Declaration
47
Module
Verilog Basic Declaration
• Comment:
+ Line comment (//)
+ Block comment (/* */)
Should not use the block comment
• Use the underscore “_” to separate the name by meaning (not use at the end)
• Not use the same character with different lower & upper cases (“aaa” and “AAA”)
• Do not mix the upper case and lower case (Dmac_Enable, Uart_Clock)
• Do not use the name which is same with the keywords (vcc, vdd, clock, reset …)
• Use the upper cases for the parameter (parameter DATA_WIDTH = 32)
<NETTYPE>
wire, reg, wand, wor, supply0, supply1, tri, tri0, tri1, triand , trior, trireg
<CAPACITOR_SIZE>
small, medium, large
<STRENGTH0>
supply0, strong0, pull0, weak0, highz0
<STRENGTH1>
supply1, strong1, pull1, weak1, highz1
<CAPACITOR_SIZE>
small, medium, large
<GATETYPE>
and, nor, pullup, tran, buf, not, rcmos, tranif0, bufif0, notif0, rnmos, tranif1, bufif1,
notif1 rpmos, xnor, cmos, or, rtran, xor, nand, pmos rtranif0, nmos, pulldown, rtranif1
52
System
Verilog Basic Function/Variable
parameter DATA_WIDTH = 3;
output [DATA_WIDTH-1:0] Q
Input Declaration
reg [DATA_WIDTH-1:0] Q;
assign D = (S==0)?A:Q;
Internal signal
always@(posedge CLK or negedge R) begin
if(R == 0) begin
Combination logic Q <= 0;
end
Sequential logic else begin
Q <= D;
end
end
endmodule
54
Verilog Basic Parameter
55
Verilog Basic Parameter
8 9
The values of “WIDTH” are different in
two independent modules 57
Verilog Basic Parameter
module LEVER_A(….);
parameter WIDTH = 8;
…..
reg [WIDTH-1:0] level_a_temp;
……
LEVEL_B inst_01 (…);
module LEVER_B(….);
parameter WIDTH = 9;
…..
reg [WIDTH-1:0] level_b_temp;
59
Verilog Basic Parameter
60
Verilog Basic Parameter
8 8
If using same parameter, the same parameters can list
in the parameter file 61
Verilog Basic Parameter
62
Verilog Basic Parameter
63
Verilog Basic Parameter
64
Verilog Basic Fundamentals
parameter DATA_WIDTH = 3;
output [DATA_WIDTH-1:0] Q
Input Declaration
reg [DATA_WIDTH-1:0] Q;
assign D = (S==0)?A:Q;
Internal signal
always@(posedge CLK or negedge R) begin
if(R == 0) begin
Combination logic Q <= 0;
end
Sequential logic else begin
Q <= D;
end
end
endmodule
65
Verilog Basic Input Declaration
66
Verilog Basic Input Declaration
67
Verilog Basic Fundamentals
parameter DATA_WIDTH = 3;
output [DATA_WIDTH-1:0] Q
Input Declaration
reg [DATA_WIDTH-1:0] Q;
assign D = (S==0)?A:Q;
Internal signal
always@(posedge CLK or negedge R) begin
if(R == 0) begin
Combination logic Q <= 0;
end
Sequential logic else begin
Q <= D;
end
end
endmodule
68
Verilog Basic Input Declaration
69
Verilog Basic Fundamentals
parameter DATA_WIDTH = 3;
output [DATA_WIDTH-1:0] Q
Input Declaration
reg [DATA_WIDTH-1:0] Q;
assign D = (S==0)?A:Q;
Internal signal
always@(posedge CLK or negedge R) begin
if(R == 0) begin
Combination logic Q <= 0;
end
Sequential logic else begin
Q <= D;
end
end
endmodule
70
Verilog Basic Internal Signal
71
Verilog Basic Main Structures
parameter DATA_WIDTH = 3;
output [DATA_WIDTH-1:0] Q
Input Declaration
reg [DATA_WIDTH-1:0] Q;
assign D = (S==0)?A:Q;
Internal signal
always@(posedge CLK or negedge R) begin
if(R == 0) begin
Combination logic Q <= 0;
end
Sequential logic else begin
Q <= D;
end
end
endmodule
72
Verilog Basic Rules
Is Same?
73
Verilog Basic Number
Decimal number 35
8’d35 //10*3+5
Octal number 8’o043 //64*0+8*4+3
Hex number 8’h23 //16*2+3
Binary number 8’b0010_0011 //Should use
74
Verilog & Techniques Main Structures
Limitation of Hierarchies
75
Logic vs. Arithmetic
wire [2:0] a;
wire [2:0] b;
wire [2:0] c1; //Test logic
wire [2:0] c2; //test arithmetic
76
Logic vs. Arithmetic
wire [2:0] a;
wire [2:0] b;
wire [2:0] c1; //Test logic
wire [2:0] c2; //test arithmetic
77
Logic vs. Arithmetic
wire [2:0] a;
wire [1:0] b; // b is only two bits
wire [2:0] c1; //Test logic
wire [2:0] c2; //test arithmetic
79
Synchronous Reset vs. Asynchronous Reset
Asynchronous Synchronous
• Synchronous reset signal is similar to other signal as S,
D or Q
• Asynchronous reset signal is special signal as CLK Q7?
80
Synchronous Reset vs. Asynchronous Reset
Synchronous
Asynchronous
always@(posedge CLK or negedge PRESET) begin assign D = S & PRESET;
if(PRESET == 0) begin
Q <= 0; always@(posedge CLK) begin
end Q <= D;
else begin end
Q <= D;
end
end
81
Asynchronous Synchronous
always@(posedge CLK or negedge PRESET) begin
if(PRESET == 0) begin assign D = S & PRESET;
Q <= 0;
end always@(posedge CLK) begin
else begin Q <= D;
Q <= D; end
end
end
D <= S and PRESET;
process (CLK, PRESET) begin process (CLK) begin
if (PRESET = '0') then if (PRESET = '0') then
Q <= '0'; Q <= '0';
elsif (rising_edge(CLK)) then elsif (rising_edge(CLK)) then
Q <= D; Q <= D;
end if; end if;
end process; end process;
Blocking vs. Non-Bloking
Non-Blocking : Only apply to ‘always’ prototype (<=)
Blocking : Apply for both “assign” and “always” prototype(=)
83
Blocking vs. Non-Bloking
Non-Blocking : Only apply to ‘always’ prototype (<=)
Blocking : Apply for both “assign” and “always” prototype(<=)
always@(posedge CLK) begin
B <= A; // Non-Blocking assignment
C <= B;
D <= C;
end
84
Blocking vs. Non-Bloking
85
Use K-Map to optimize function Fundamentals
86
Use enough cases Fundamentals
always@(X) begin
X[2] X[1] X[0] Y[1:0]
if (X == 3’b000) begin
0 0 0 01 Y <= 2’b01;
0 0 1 10 end
else if (X == 3’b001) begin
0 1 0 11 Y <= 2’b10;
0 1 1 00 end
-------- --------- -------- 00 else if (X == 3’b010) begin
Y <= 2’b11;
end
else begin
Y <= 2’b00;
end
end
87
Use enough cases Fundamentals
always@(X) begin
X[2] X[1] X[0] Y[1:0]
case (X)
0 0 0 01 3’b000: begin
0 0 1 10 Y <= 2’b01;
end
0 1 0 11 3’b001: begin
0 1 1 00 Y <= 2’b10;
-------- --------- -------- 00 end
3’b010: begin
Y <= 2’b11;
end
default : begin
Y <= 2’b00;
end
endcase
end
‘default’ is for other cases to cover all cases of X[2:0]
88
Use enough cases Fundamentals
always@(X) begin
case (X)
3’b000: begin
Y <= 2’b01;
end
3’b001: begin
Y <= 2’b10;
assign Y[1] = (!X[2] && X[0]) | (!X[2] && X[1])
assign Y[0] = !X[2] && (!X[0])
VS. end
3’b010: begin
Y <= 2’b11;
end
default : begin
Y <= 2’b00;
end
endcase
end
89
Limitation of hierarchies Fundamentals
always@(X) begin
case (X) // Level 1 of case
3’b000: begin
case (Z) // Level 2 of case
3’b000: begin
Y <= 2’b01; // How many levels ?
4 space bar end
after ‘begin’ …………..
endcase
end Maximum three levels are accepted
………….. + If
default : begin + Case
Y <= 2’b00; + If & Case together
end
endcase
end
90
Use “always” Fundamentals
always( S or A or D) begin
if(S == 1’b0) begin
D = A;
end
else
D = B;
end
end
Top level
module second level
third level
module
module
92
Connect modules Fundamentals
endmodule
module com (A, B, S, O);
input A;
input B;
input S;
output O;
wire O;
assign O = (S==1’b0)?A:B;
endmodule
93
Connect modules Fundamentals
module com
module seq module top (CLK, R, A, B, S, Q);
input CLK;
X input R;
input A;
input B;
input S;
output Q;
wire Q;
endmodule
94
Connect modules Fundamentals
input CLK;
X input R;
input A;
input B;
input S;
output Q;
wire Q;
endmodule
95
Connect modules Fundamentals
input CLK;
X input R;
input A; Second level
input B; Top level
input S;
output Q;
wire Q;
endmodule
96
Connect modules Fundamentals
input CLK;
X input R;
input A;
input B;
input S;
endmodule
97
Connect modules Fundamentals
output Q; output Q;
wire Q; wire Q;
wire X; wire X;
// Call module com named ‘com_01’ // Call module com named ‘com_01’
com com_01 ( .A(A), .B(B), .S(S), .O(X) ); com com_01 ( A, B, S, X);
// Call module seq named : ‘seq_01’ // Call module seq named : ‘seq_01’
seq seq_01 ( .CLK(CLK), .R(R), .D(X), .Q(Q) ); seq seq_01 (CLK, R, X, Q);
endmodule endmodule
98
Gate Level Netlist
Verilog Language
99
Gate Level Netlist
Verilog: Primitives Format
100
Q&A
101