Verilog Coding Guideline: Author: Trumen
Verilog Coding Guideline: Author: Trumen
Verilog Coding Guideline: Author: Trumen
Guideline
數位電路實驗
TA: 吳柏辰
Author: Trumen
Outline
2
Introduction to Verilog HDL
3
What is Verilog Doing…
Wafer Chip Logic Module
PE0 PE1
+
CIS
Feature
MUX
Processor
PE2 DFF
PE3
RISC
-
PE5
PE4
Registers
PE6 PE7
Abs
Memory
Verilog
Backend
EDA Tools
Standard Cell
4
Layout
Verilog HDL
• Verilog HDL
• Hardware Description Language
• Programming language
• Describes a hardware design
• In software
• max_abcd = max( max(a,b), max(c,d) );
a max_ab
max
b
max max_abcd
c
max
d max_cd
• In verilog ??
6
Represent a Circuit (2/2)
a max_ab
max
b
max max_abcd
c
max
d max_cd
wire [3:0] a, b, c, d;
reg [3:0] max_ab; max_cd; data declaration
reg [3:0] max_abcd;
always@(*) begin
max_ab = (a > b)? a: b; logic behavior
max_cd = (c > d)? c: d;
max_abcd = (max_ab > max_cd)? max_ab: max_cd;
end 7
Verilog Syntax
8
Blocking and Nonblocking
Statements (1/2)
• Blocking Statements "="
• A blocking statement must be executed before the
execution of the statements that follow it in a
sequential block.
9
Blocking and Nonblocking
Statements (2/2)
module block_nonblock();
reg a, b, c, d, e, f;
// Blocking assignments
initial begin
a = #10 1'b1; // The simulator assigns 1 to a at time 10
b = #20 1'b0; // The simulator assigns 0 to b at time 30
c = #40 1'b1; // The simulator assigns 1 to c at time 70
end
// Nonblocking assignments
initial begin
d <= #10 1'b1; // The simulator assigns 1 to d at time 10
e <= #20 1'b0; // The simulator assigns 0 to e at time 20
f <= #40 1'b1; // The simulator assigns 1 to f at time 40
end
endmodule
10
Data Types (1/3)
• wire
• Used as inputs and outputs within an actual module
declaration.
• Must be driven by something, and cannot store a
value without being driven.
• Cannot be used as the left-hand side of an = or <=
sign in an always@ block.
• The only legal type on the left-hand side of an
assign statement.
• Only be used to model combinational logic.
11
Data Types (2/3)
• reg
• Can be connected to the input port (but not output
port) of a module instantiation.
• Can be used as outputs (but not input) within an
actual module declaration.
• The only legal type on the left-hand side of an
always@ (or initial) block = or <= sign.
• Can be used to create registers when used in
conjunction with always@(posedge Clock) blocks.
• Can be used to create both combinational and
sequential logic.
12
Data Types (3/3)
data declaration
wire [15:0] longdata; // 16-bit wire
wire shortvalue; // 1-bit wire
reg [3:0] areg; //4-bit reg
always@(*) begin
if(shortvalue == 1'b1)
areg = shortvalue + 3;
reg
else
areg = shortvalue + 7;
end 13
logic behavior
Value Set
0 z
logic low high-impedance
x
1 unknown
logic high 14
Numbers
16
Operators (1/6)
• Arithmetic operators
• +, -, *, /, %
• Bitwise operators
• Perform the operation one bit of a operand and its
equivalent bit on the other operand to calculate one
bit for the result
• ~, &, |, ^, ~^
17
20
Operators (5/6)
• Concatenation operator
• {}
• Join bits from two or more
a b c d
expressions together
• Very convenient y
• Multiple layers y = {a[1:0], b[2], c[4,3], d[7:5]}
• {{},{}…}
a
• Replication operator
• {n{}} y
y = {{4{a[3]}},a} 21
Operators (6/6)
22
Combinational and
Sequential Logics
23
Two Types of Logics (1/2)
• Combinational Logics
• data-in → data-out
• instant response
• fixed arrangement
a max_ab
max
b
max max_abcd
c
max
d max_cd 24
Two Types of Logics (2/2)
• Sequential Logics
• always and only update at clock edges
• posedge / negedge
reg [3:0] a, b; // declaration
Sequential Logic
Sequential Logic
(Register)
(Register)
(Register)
Combinational Combinational
Logic Logic
clk
always @(posedge clk) begin assign c = b [2:0];
if(~rst_n) begin assign d = c & 3'b101;
a <= 3'd0;
end always@(a or d) begin
else begin sum = a + d;
a <= next_a; ......
29
end end
Sequential Circuit (1/3)
• Synchronous reset
always @(posedge clk) begin
if(~rst_n) begin
a <= 8'd0;
end
else begin
clk
a <= next_a;
end rst_n
next_ 8'h01 8'h5a
a 30
Sequential Circuit (2/3)
• Asynchronous reset
always @(posedge clk or negedge rst_n) begin
if(~rst_n) begin
a <= 8'd0;
end
else begin
clk
a <= next_a;
end rst_n
next_ 8'h01 8'h5a
a 31
Sequential Circuit (3/3)
33
What is a Module?
MUX
DFF
• Easier to reuse / maintain
-
Abs
34
a
b max D Q maxab
A Module rst_
nclk
rst_n
clk
combinational logics 35
Connection Between Modules
(1/2)
a
D Q maxabcd
max
b
clk max D Q
clk
c D Q
max
d clk
36
Connection Between Modules
(2/2)
maxcd
38
endmodule
Write Your Design
39
Use Parameters (1/2)
41
Finite State Machine
42
Finite State Machine (1/2)
43
Finite State Machine (2/2)
44
Elements of FSM (1/2)
46
Moore Machine
Next Current
Input State State Output
X Next-state (NS) Memory (CS) Output X
Logic Element Logic
(NL) (ME) (OL)
47
Mealy Machine
Next Current
Input State State Output
X Next-state (NS) Memory (CS) Output X
Logic Element Logic
(NL) (ME) (OL)
48
Modeling FSM in Verilog
• Sequential circuits
• Memory elements of current state (CS)
• Combinational circuits
• Next-state logic (NL)
• Output logic (OL)
49
The End.
Any question?
Reference
1. "Verilog_Coding_Guideline_Basic" by
members of DSP/IC Design Lab
2. http://inst.eecs.berkeley.edu/~cs150/Docume
nts/Nets.pdf by Chris Fletcher, UC Berkeley
3. CIC training course: "Verilog_9807.pdf"
4. http://www.asic-world.com/
51