0% found this document useful (0 votes)
50 views51 pages

Verilog Coding Guideline: Author: Trumen

The document provides an introduction and guidelines for Verilog coding, covering topics such as Verilog syntax, data types, operators, and how to represent combinational and sequential logic in Verilog. It explains the difference between blocking and non-blocking statements in Verilog and how sensitivity lists work. The document also discusses different data types, value sets, number formats, and various operators that can be used in Verilog.

Uploaded by

NK NK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views51 pages

Verilog Coding Guideline: Author: Trumen

The document provides an introduction and guidelines for Verilog coding, covering topics such as Verilog syntax, data types, operators, and how to represent combinational and sequential logic in Verilog. It explains the difference between blocking and non-blocking statements in Verilog and how sensitivity lists work. The document also discusses different data types, value sets, number formats, and various operators that can be used in Verilog.

Uploaded by

NK NK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Verilog Coding

Guideline
數位電路實驗
TA: 吳柏辰

Author: Trumen
Outline

• Introduction to Verilog HDL


• Verilog Syntax
• Combinational and Sequential Logics
• Module Hierarchy
• Write your design
• Finite State Machine

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

• Other hardware description language


• VHDL
5
Represent a Circuit (1/2)

• 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.

• Nonblocking Statements "<="


• Nonblocking statements allow you to schedule
assignments without blocking the procedural flow.

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

wire assign longdata = areg + 88;

always@(*) begin
if(shortvalue == 1'b1)
areg = shortvalue + 3;
reg
else
areg = shortvalue + 7;
end 13
logic behavior
Value Set

• 0 - logic zero, or false condition


• 1 - logic one, or true condition
• z - high-impedance state
• x - unknown logic value – unrealistic value in design

0 z
logic low high-impedance

x
1 unknown
logic high 14
Numbers

• binary('b), decimal('d), hexadecimal('h),


octal('o)
• Format
• <number>: reg data = 127;
• '<base><number>: reg data = 'd127;
• <width>'<base><number> → complete format
659 // decimal
'o7460 // octal
4'b1001 // 4-bit binary
3'b01x // 3-bit with unknown
reg data =
16'hz // 16-bit high impedance
-8'd6 // two’s complement of 6 15

8'd-6 // illegal syntax


4af // illegal (requires 'h)
Case-Sensitive Control

• Suspends subsequent statement execution


until any of the specified list of events occurs
• Support event driven simulation
• Sensitivity list
• always@(… or … or … or…)
• always@(a or b or c )
• always@(*)
• verilog 2001, automatic list generation

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

Arithmetic operators Bitwise operators


Operators (2/6)

• Unary reduction operators


• Perform the operation on each bit of the operand
and get a one-bit result &4’b11100
• &, ~&, |, ~|, ^, ~^ &4’b11111
Unary reduction AND
|4’b00000
|4’b00011
Unary reduction OR
^4’b11110
^4’b11101
Unary reduction operators Unary reduction XOR 18
Operators (3/6)

• Logical operators operate with logic values


• Equality operators
• Logical equality
• ==, !=
• Case equality
• ===, !==
• Logical negation
• !
• Logical
• &&, ||
example !4’b0100  0 19
Logical operators
!4’b0000  1
!4’b00z0  x
Operators (4/6)

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)

• Shift operators  Operator precedence


• <<, >>
• Relational operators
if(a <= b) d = 0;
• <, <=, >=, > else d = 1;
• Conditional operator
• ?: d = (a<=b) ? 0 : 1

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 1 cycle


• always and only update at
clock edges
• posedge / negedge
• memory
a D Q
a_d1 D Q
a_d2 D Q
a_d3

clk clk clk


clk
25
Case-Sensitive Control (1/2)

• register in sequential changes only at


clock edges
always@(posedge clk) begin always@(negedge clk) begin
…… ……
end end

• with reset signal


always @(posedge clk or negedge rst_n) begin
......
end 26
Case-Sensitive Control (2/2)

• Sequential Logics
• always and only update at clock edges
• posedge / negedge
reg [3:0] a, b; // declaration

always @(posedge clk or negedge rst_n) begin


if(rst_n ==1'b0) begin //registers
a <= 4'd0;
b <= 4'd0;
end
else begin
a <= next_a;
b <= next_b;
end 27
The Use of Sequential Circuits?

• Temporal storage (memory)


• Split long computation lines
• timing issue
• divide circuits into independent stages
• work at the same time !!
• Combinational logics handle the computation
• Sequential logics store inter-stage temporal
data
28
Sequential and Combinational
Logics
Sequential Logic

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 XX 8’h00 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 XX 8’h00 8’h01 8’h5a

a 31
Sequential Circuit (3/3)

• Easy to synthesize, just another synchronous input to the


Pros
Synchronous design
reset • Require a free-running clock, especially at power-up, for
Cons
reset to occur
• Does not require a free-running clock
Pros • Uses separate input on FF, so it does not affect FF data
timing
Asynchronous • Harder to implement, usually a tree of buffers is inserted
reset at P&R
Cons • Makes static timing analysis and cycle-based simulation
Prefer more difficult, and can make the automatic insertion of
test structures more difficult
32
Module Hierarchy

33
What is a Module?

• Group circuits into meaningful building blocks


• Combine highly-related circuits Module
• Leave simple i/o interface +

MUX
DFF
• Easier to reuse / maintain
-

Abs

34
a
b max D Q maxab

A Module rst_
nclk
rst_n
clk

module maxab(clk,rst_n,a,b,maxab); always@(posedge clk or negedge


rst_n) begin
input clk;
if (rst_n == 1'b0) begin
input rst_n;
module port maxab <= 4'd0;
input [3:0] a;
input [3:0] b; definition end
else begin sequential logics
output [3:0] maxab;
maxab <= next_maxab;
end
reg [3:0] maxab; wire, reg
end
wire [3:0] next_maxab; declaration

endmodule ending your module!


assign next_maxab = (a>b)? a: b;

combinational logics 35
Connection Between Modules
(1/2)

• Where you "cut" your design.

a
D Q maxabcd
max
b
clk max D Q
clk
c D Q
max
d clk
36
Connection Between Modules
(2/2)

• Where you "cut" your design.


maxab
a
D Q maxabcd
max
b
clk max D Q
maxcd clk
c D Q
max
d clk
37
Module Instantiation

• Build a module by smaller modules


module maxabcd(clk,rst_n,a,b,c,d,maxabcd);
maxab
input clk; a
max D Q maxabcd
input rst_n; clk
b
input [3:0] a, b, c, d; max D Q
clk
output [3:0] maxabcd;
c
max D Q
wire [3:0] maxab, maxcd; d clk

maxcd

maxab m1(.clk(clk), .rst_n(rst_n), .a(a), .b(b), .maxab(maxab));


maxab m2(.clk(clk), .rst_n(rst_n), .a(c), .b(d), .maxab(maxcd));
maxab m3(.clk(clk), .rst_n(rst_n), .a(maxab), .b(maxcd), .maxab(maxabcd));

38
endmodule
Write Your Design

39
Use Parameters (1/2)

• Build a module by smaller modules


`define INST_WakeUp 0
`define INST_GoToSleep 1
`define BUS_WIDTH 64
input [`BUS_WIDTH-1:0] databus;
case (instruction)
`INST_WakeUp:

`INST_GoToSleep:

endcase 40
Use Parameters (2/2)

• Use parameter for reusable modules


parameter [4:0] FA_BitSize = 8;
reg [FA_BitSize-1:0] = datain;

• Use localparam for inner-module variables


localparam FSM_StateSize = 5;
localparam [FSM_StateSize-1:0] FSM_Idle = 5'd0;

41
Finite State Machine

42
Finite State Machine (1/2)

• Synchronous (i.e. clocked) finite state


machines (FSMs) have widespread application
in digital systems
• Controllers in computational units and processors.
• Synchronous FSMs are characterized by
• A finite number of states
• Clock-driven state transitions.

43
Finite State Machine (2/2)

44
Elements of FSM (1/2)

• Memory elements (ME)


• Memorize current state (CS)
• Usually consist of FF or latch
• N-bit FF have 2N possible states
• Next-state logic (NL)
• Combinational logic
• Produce next state
• Based on current state (CS) and input (X) 45
Elements of FSM (2/2)

• Output logic (OL)


• Combinational logic
• Produce outputs (Z)
• Based on current state (Moore)
• Based on current state and input (Mealy)

46
Moore Machine

• Output is function of CS only


• Not function of inputs

Next Current
Input State State Output
X Next-state (NS) Memory (CS) Output X
Logic Element Logic
(NL) (ME) (OL)

47
Mealy Machine

• Output is function of both


• Input and current state

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

You might also like