Professional Documents
Culture Documents
Verilog Tutorial PDF
Verilog Tutorial PDF
Verilog
Verilog
Verilog .
.
. - .
Verilog :
: .
) : (Dataflow
.
) : (Behavioral
.
: .
Module body
endmodule
D0
D1
D2
D3
B
En
--
Verilog . .
Instantiation
Instance . -
.
Verilog
:
Top-Level Cicuit
dcd0
d0
d1
Circuit
d2
d3
D0
D1
A
B
D2
D3
En
a
b
Circuit
en
;module Top
;wire a, b, en
;wire d0, d1, d2, d3
><Other Components
;) decoder dcd0 ( a, b, en, d0, d1, d2, d3
><Other Components
endmodule
--
. Comparator
Comp0 .
.
Comp0
A_GT_B
a_gt_b
A_EQ_B
a_eq_b
A_LT_B
a_lt_b
gt
GT
eq
EQ
lt
LT
Verilog C .
Verilog .
.
Verilog : ) (\t) tab (\b ) .(\n
Verilog
.
Verilog
. .
a = b && c; // This is a single line comment
/* This is a multiple line
comment */
Verilog :
: Verilog
. > <size><radix><number <size> .
<radix> .
d D b B o O h H
. .
"_" .
This is a 8 bit binary number
This is a 12 bit hex number
This is a 12 bit decimal number
This is a 4 bit negative decimal number, that stored
as 2s complement of 13
//
//
//
//
//
8b1010_1110
12hab6
16D255
-4d13
: > <radix><number .
.
// This is a 32 bit decimal number
// This is a 32 bit hex number
// This is a 32 bit octal number
1234
h62
o255
Verilog
12h13x
??4b10
.
27_13_45
8d-6
5D3
12hx
4AF
h83F
659
.
.
. _ $
. .
--
Verilog
. - - .
Verilog
Value Level
0
1
x
z
--
Degree
strongest
Weakest
Type
Driving
Driving
Driving
Storage
Driving
Storage
Storage
High Impedance
Strength Level
supply
strong
pull
large
weak
medium
small
highz
--
--
Net
.
net net .
Verilog wire z.
wire a; // Declare net a
wire b=1b0
// Net b is fixed to logic value 0 at declaration
z x
MOS z .
.
.
.
Verilog
--
Register
.
. register net register . Verilog
reg x.
// Declare a variable that can be hold its value
--
;reg reset
Vector
reg wire .
:
><vector_type> [ MSB : LSB ] <vector_name
// Declare a bus that has 32 bit width
// virtual address 41 bits wide
--
:
. reg reg
.
integer
.
;integer counter
:
. integer .
// Declare a real variable called delta
;real delta
: Verilog
. time .
.
// Define a time variable sim_time
;time sim_time
Verilog
;]integer count[0:7
Verilog .
n.
Verilog .
// 1K x 1bit memory
// 1K x 8bit memory
;]reg membit[0:1023
;]reg [7:0] membyte[0:1023
parameter .
// Define a constant port_id
;parameter port_id = 5
reg . reg
. .
// Define a variable with 18 bytes
task
Verilog task
... . task > $<task_name .
Verilog
: $display
;) $display( format, p1, p2, ..., pn pi
. format
.
- .
Display
Display variable in decimal
Display variable in binary
Display string
Display variable in hex
Display ASCII character
Display strength
Display variable in octal
Display in current time format
Display real number in scientific format
Display real number in decimal format
Display real number in scientific or decimal format, whichever is
shorter
Format
%d or %D
%b or %B
%s or %S
%h or %H
%c or %C
%v or %V
%o or %O
%t or %T
%e or %E
%f or %F
%g or %G
--
: $monitor
;) $monitor( format, p1, p2, ..., pn pi
. $display
$monitor
. $monitor
$monitor $monitor . task
$monitoron $monitoroff
.
: $stop
interactive . . $finish
.
Verilog
`define .
`include Verilog .
// Used as `WORD_SIZE in the code
// Include the file header.v
`define WORD_SIZE 32
`include header.v
. - .
.
.
Declaration
--
) (
. -
.
Verilog
:
fulladd4
sum
b
cin
cout
--
. - .
Type of port
Input Prt
Output Prt
Bidirectional Prt
Verilog Keyword
input
output
inout
--
wire
reg
.
- .
;) module fulladd4( sum, cout, a, b, cin
;output [3:0] sum
;output cout
;input [3:0] a, b
;input cin
> < Module Body
endmodule
:
net
reg net .
reg net
net .
Verilog
net
reg net .
.
Verilog :
- :
.
.
- : :
).port_name(external_signal_name
. .
//-- First Method -------------------------------------------;) fulladd4 fa0( s, co, x, y, ci
fulladd4 fa0( s,
;) , x, y, ci
//-- Second Method ------------------------------------------;) )fulladd4 fa0( .sum(s), .cout(co), .cin(ci), .a(x), .b(y
;) )fulladd4 fa0( .sum(s), .cin(ci), .a(x), .b(y
- .
net
inout
net
net
reg or net
output
reg or net
net
input
module
--
. Verilog
.
-
--
and/or
and/or - .
Verilog
Comparator . Comparator4
. im8 im0 .
a[3:0]
b[3:0]
a[0]
b[0]
gt
a_gt_b
a_eq_b
gt
eq
a_lt_b
eq
lt
im0
a[1]
b[1]
im1
a_gt_b
a_eq_b
gt
im2
eq
lt
a_lt_b
im3
a[2]
b[2]
im4
a_gt_b
a_eq_b
gt
im5
eq
lt
Comp0
a_lt_b
im6
a[3]
b[3]
im7
a_gt_b
a_gt_b
a_eq_b
a_eq_b
gt
im8
eq
lt
Comp1
a_lt_b
a_lt_b
lt
Comp2
Comp3
and
or
xor
0
1
0
0
0
1
0
x
0
x
0
1
0
1
1
1
x
1
x
1
0
1
0
1
1
0
x
x
x
x
x
z
0
0
x
x
x
x
x
x
x
z
x
x
1
1
x
x
x
x
x
z
x
x
x
x
x
x
x
x
nand
nor
xnor
0
1
1
1
1
0
1
x
1
x
0
1
1
0
0
0
x
0
x
0
0
1
1
0
0
1
x
x
x
x
x
z
1
1
x
x
x
x
x
x
x
z
x
x
0
0
x
x
x
x
x
z
x
x
x
x
x
x
x
x
and/or --
//
//
//
//
//
//
.
.
) ) (
.( Verilog
Verilog
--
buf/not
buf/not - .
ctrl
ctrl
z
L
x
L
1
0
0
z
bufif1
0
z
L
x
L
1
z
0
0
bufif0
0
out
0
buf
0
H
x
H
x
1
x
z
z
1
x
H
x
H
x
z
z
1
x
1
x
1
x
1
x
notif1
notif0
out
not
H
L
H
L
1
0
z
z
0
1
H
L
H
L
z
z
1
0
0
1
1
0
0
1
x
x
x
x
x
x
z
z
x
z
x
x
x
x
z
z
x
x
x
z
x
x
x
z
ctrl
ctrl
-- buf/not
not, buf .
.
.
L 0 z H 1 z.
net
Wiring .
.
: .
: Verilog - .
: .
: Verilog - .
nand D Clk
. D, Clk q, q_bar
.
Verilog
i0
y0
s1n
s0n
i1
y1
i2
y2
i3
y3
out
s1
s0
(
(
(
(
y0,
y1,
y2,
y3,
i0,
i1,
i2,
i3,
s1n,
s1n,
s1 ,
s1 ,
s0n
s0
s1n
s0
);
);
);
);
Verilog --
im2
gt
im3
im0
a_gt_b
im4
im1
im7
im8
lt
im9
a_lt_b
// Gate instantiation
not ( im0, b );
not ( im1, a );
nand ( im2, a, gt );
nand ( im3, gt, im0 );
nand ( im4, a, im0 );
nand ( im5, a, b, eq );
nand ( im6, im0, eq, im1 );
nand ( im7, im1, b );
nand ( im8, im1, lt );
nand ( im9, lt, b );
nand ( a_gt_b, im2, im3, im4 );
nand ( a_eq_b, im5, im6 );
nand ( a_lt_b, im7, im8, im9 );
endmodule
Verilog --
. .
Verilog .
. #
Verilog
--
TFall
TRise
1
1,x,z
0,x,z
0
// Rise=5, Fall=5, Turn-Off=5
// Rise=4, Fall=6, Turn-Off=4
// Rise=4, Fall=5, Turn-Off=6
--
Min/Typ/Max
Verilog
. Min : Typ : Max .
. +maxdelay
.
T=Turn-Off Delay
F=Fall Dealy
// R=Rise Dealy
= 5, T
= 6, T
= 7, T
i1, i2
Verilog
4
5
6
;) i1, i2
= T
= T
= T
o1,
. Verilog
.
Continuous Assignment
net .
:
;assign <signal_strength> <delay> assignment_lists
.
net
.
net .
Verilog
:
;wire out
;assign out = i1 & i2
// are same as
;wire out = i1 & i2
. assignment .
--
assign net .
.
Inertial
. - .
i0
<5
>5
i1
;assign #5 o1 = i1 & i2
endmodule
o1
-- Inertial
--
--
net
net .
net .
.
Verilog
:
wire #5 out;
assign out = i1 & i2;
. -
. - Verilog
Operator
Type
Arithmetic
Logical
Relational
Equality
Bitwise
Reduction
Shift
Reduction
Conditional
Operator
Symbol
Operation Performed
Number of
Operands
*
/
+
%
!
&&
||
>
<
>=
<=
==
!=
===
!==
~
&
|
^
~^ or ^~
&
~&
|
~|
^
~^ or ^~
>>
<<
{}
{ {} }
?:
multiply
division
add
subtract
modulus
logical negation
logical and
logical or
greater than
less than
greater than or equal
less than or equal
equality
inequality
case equality
case inequality
bitwise negation
bitwise and
bitwise or
bitwise xor
bitwise xnor
reduction and
reduction nand
reduction or
reduction nor
reduction xor
reduction xnor
right shift
left shift
concatenation
replication
conditional
2
2
2
2
2
1
2
2
2
2
2
2
2
2
2
2
1
2
2
2
2
1
1
1
1
1
1
2
2
any number
any number
3
Verilog --
Verilog
:
.
x x .
% .
.
;A = 4b0011
;B = 4b0100
;D = 6
;E = 4
A*B
// Evaluated to 4b1100
D/E
// Evaluated to 1
A+B
// Evaluated to 4b0111
B-A
// Evaluated to 4b0001
//--------------------------------------------------------;in1 = 4b101x
;in2 = 4b1010
;sum = in1 + in2
// Evaluated to 4x
//---------------------------------------------------------7 % 2 // Evaluated to -1, take sign of the first operand
7 % -2 // Evaluated to +1, take sign of the first operand
:
0 : 1 x .
0 1 x
.
;= 0
) Evaluated to 0 ( False
) Evaluated to 1 ( True
) Evaluated to 0 ( False
B
//
//
//
;A = 3
A && B
A || B
!A
//--------------------------------------------------------;A = 2bx0
;B = 2b10
) A && B // Evaluated to x ( Unknown
:
Verilog
.
reg net reg net
.
Verilog
;1b1
;B = 2b00
;D = 2b10
;C = 3b110
;} { B, C
// Result y is 4b0010
;} { A, B ,C, D, 3b001
// Result y is 11b1_00_10_110_001
;} ]{ A, b[0], C[1
// Result y is 3b101
=
=
=
=
A
y
y
y
:
}{
.
;D = 2b10
// Result y is 4b1111
// Result y is 8b11110000
// Result y is 10b1111000010
;B = 2b00
;A = 1b1
;} }y = { 4{A
;} }y = { 4{A}, 2{B
;} y = { 4{A}, 2{B}, C
:
:
;<condition> ? true_exp : false_exp
condition x
true_exp, false_exp
x
.
:
a==b 0 1 x . x z
x.
a!=b 0 1 x . x z
x.
a===b 0 1 a . b .
a!==b 0 1 a . b .
Verilog
;N = 4b1xxx
0
1
1
0
1
logical
logical
x
logical
logical
logical
;A = 3
;B = 3
;X = 4b1010
;Y = 4b1101
;Z = 4b1xzz
;M = 4b1xzz
is
is
is
is
is
is
Result
Result
Result
Result
Result
Result
//
//
//
//
//
//
== B
!= B
== Z
=== M
=== N
!== M
A
A
X
Z
Z
M
) (reduction :
.
;x = 4b1010
&x
|x
^x
.
: .
: .
Verilog
. :
// Dataflow model of a cascadable 1-bit comparator
module Comparator ( a_gt_b, a_eq_b, a_lt_b, a, b, gt, eq, lt );
output a_gt_b, a_eq_b, a_lt_b;
input a, b, gt, eq, lt;
// Specify the boolean function of a 1-bit
assign a_gt_b = ( a & gt ) | ( ~b & gt ) |
assign a_eq_b = ( a & b & eq ) | ( ~a & ~b
assign a_lt_b = ( ~a & lt ) | ( b & lt ) |
comparator
( a & ~b );
& eq );
( ~a & b );
endmodule
. .
a3
a2
a1
a0
E
c2
a* 3
c1
a* 2
c0
a *1
C-1=0
a* 0
Verilog
--
initial
initial 0 .
initial begin end .
initial 0
.
.
;module stimulus
;reg a, b, x, y, m
initial
;m = 1b0
initial
begin
;#10 x = 1b0
;#10 y = 1b1
end
initial
begin
;#5 a = 1b1
;#10 b = 1b0
end
initial
;#50 $finish
endmodule
Statement executed
;m = 1b0
;a = 1b1
;x = 1b0
;b = 1b0
;y = 1b1
;$finish
Time
0
5
10
30
35
50
//
//
//
//
//
//
//
Verilog
--
always
always 0
) ( .
.
;module clock_gen
;reg clk
initial
;clk = 1b0
) // Toggle clk every half_cycle ( Period = 20
always
;#10 clk = ~clk
initial
;#1000 $finish
endmodule
Procedural Assignment
reg .
.
:
reg
Bit-Select
Part-Select
--
Blocking Assignment
. = .
initial
begin
// These statements are executed at time 0 sequentially
;x = 0
;y = 1
;z = 1
;count = 0
;reg_a = 16h0000; reg_b = reg_a
// This statement is executed at time 15
;#15 reg_a[2] = 1b1
Verilog
:
// These statements are executed at time 25 sequentially
;} #10 reg_b[15:13] = { x, y, z
;count = count + 1
end
--
Nonblocking Assignment
. =< .
Nonblocking
.
Nonblocking
.
initial
begin
;x = 0
;y = 1
;z = 1
;count = 0
;reg_a = 16h0000; reg_b = reg_a
// This statement is scheduled to execute after 15 time units
;reg_a[2] <= #15 1b1
// This statement is scheduled to execute after 10 time units
;} reg_b[15:13] <= #10 { x, y, z
// This statement is scheduled to execute without any delay
;count <= count + 1
end
Verilog .
Verilog :
--
. .
Verilog
: .
.
initial
begin
;x = 0
;#10 y = 1
;#(4:5:6) q=0
end
: assignment
.
.
;temp = x + z
--
initial
begin
;x = 0
;z = 0
;y = #5 x + z
end
// Is equivalent to
initial
begin
;x = 0
;z = 0
;#5 y = temp
end
reg net .
.
: @ .
. 0 1, x , z; x 1; z 1
1 0, x, z; x 0; z 0.
//
//
//
//
//
;@(clock) q = d
;@(posedge clock) q = d
;@(negedge clock) q = d
;q = @(posedge clock) d
Verilog :
. event
> - .
Verilog
:
;event rec_data
)always @(posedge clock
begin
;if( last_data_packet ) ->rec_data
end
)always @(rec_data
;} ]data_buf = { data[0] , data[1], data[2], data[3
:
. or
. Sensitivity List .
)always @(posedge clock or reset
begin
;if( reset ) q = 0
;else q = d
end
--
Verilog
wait .
always
;wait( count_enable ) #20 count = count + 1
case
) if( expr
true_st
else
;false_st
)
;st1
;st2
;stn
;def_st
case ( expr
case 1 :
case 2 :
...
case n :
defult :
endcase
Verilog
expr ) .( x, 1, 0
.
casex z, x ) (dont care
.
casez z ) (dont care
.
) while( expr
;st
//--------------------------------------) for( init; end_cond; chang_control_var
;st
//--------------------------------------) repeat( number_of_iteration
;st
//--------------------------------------forever
;st
Task - .
Task
--
Verilog
Task
task task .
Function
.
task 0 .
0 .
task .
task
.
task .
-- Task
;module operation
;reg [15:0] A, B
;reg [15:0] AB_AND, AB_OR, AB_XOR
always @(A or B) //whenever A or B changes in value
;)bitwise_oper(AB_AND, AB_OR, AB_XOR, A, B
Function
--
.
;function calc_parity
;input [31:0] address
begin
// set the output value appropriately. Use the implicit
// internal register calc_parity.
Verilog
always . if else
.
: .
.
: D-FF reset .
// Behavioral model of a d-ff with synchronous reset
;) module d_ff ( d, clk, rst, q
;input d, clk, rst
;output q
;reg q
) always @( posedge clk
begin
)if (rst
;q = 1b0
else
;q = d
end
endmodule
: D-FF reset .
Verilog
:
// Behavioral model of a d-ff with asynchronous reset
module d_ff ( d, clk, rst, q );
input d, clk, rst;
output q;
reg q;
always @( posedge clk or posedge rst)
begin
if (rst)
q = 1b0;
else
q = d;
end
endmodule
. :(mealy )
1/0
0/0
init
got1
0/0
1/0
0/1
got11
1/0
Verilog
:
// Mealy state machine
`define init
`define got1
`define got11
2'd0
2'd1
2'd2
)
= x ? `got1 : `init;
= x ? `got11 : `init;
= x ? `got11 : `init;
. :
// Mealy state machine (Hufmann Model)
`define init
`define got1
`define got11
2'd0
2'd1
2'd2
Verilog
. :(moore )
init
got1
0
0
0
0/0
got110
got11
init
got1
got11
got110
2'd0
2'd1
2'd2
2'd3
x
x
x
x
?
?
?
?
`got1
`got11
`got11
`got1
:
:
:
:
`init;
`init;
`got110;
`init;
. :
// Moore state machine (Hufmann Model)
`define
`define
`define
`define
init
got1
got11
got110
2'd0
2'd1
2'd2
2'd3
Verilog
:
always @( present_state or
begin
case ( present_state )
`init
: next_state
`got1
: next_state
`got11 : next_state
`got110 : next_state
endcase
end
x )
=
=
=
=
x
x
x
x
?
?
?
?
`got1
`got11
`got11
`got1
:
:
:
:
`init;
`init;
`got110;
`init;
:
. Handshake .
.
.
module producer( dataOut, prodReady, consReady )
output [7:0] dataOut;
output prodReady;
reg [7:0] dataOut;
reg prodReady;
input consReady;
always
begin
prodReady = 0;
// Indicate nothing to transfer
forever
begin
// ... Prduce data and put into temp
// Wait for consumer ready
wait( consReady ) dataOut = $random;
// Indicate ready to transfer
prodReady = 1;
// Finish handshake
wait( !consReady ) prodReady = 0;
end
end
endmodule
module consumer( dataIn, prodReady, consReady )
input [7:0] dataIn;
input prodReady;
output consReady;
reg consReady;
reg [7:0] dataInCopy;
Verilog
:
always
begin
consReady = 1;
// Indicate nothing to transfer
forever
begin
wait( prodReady ) dataInCopy = dataIn;
// Indicate value consumed
consReady = 0;
// ... munch on data
// Complete handshake
wait( !prodReady ) consReady = 1;
end
end
endmodule
// Top-level Producer-Consumer process
module ProducerConsumer;
wire [7:0] data;
wire pReady, cReady;
producer p( data, pReady, cReady );
consumer c( data, pReady, cReady );
endmodule
clk
)
.(
Producer Writes
Consumer Reads
Verilog
.
MOS, CMOS
. -
Verilog
:
Data
Out
Data
Out
Ncontrol
Control
nmos
0
1
x
z
0
z
z
z
z
1
0
1
x
z
Control
x
L
H
x
z
z
L
H
x
z
nmos
0
1
x
z
0
0
1
x
z
1
z
z
z
z
x
L
H
x
z
Data
z
L
H
x
z
Out
Pcontrol
MOS --
nmos n1 ( out, data, control );
pmos ( out, data, control );
cmos c1 ( out, data, ncontrol, pcontrol );
. -
control
inout1
inout2
tran
inout1
control
inout2
tranif0
inout1
inout2
tranif1
MOS --
tran t1 ( inout1, inout2 );
tranif0 ( inout1, inout2, control );
tranif1 ( inout1, inout2, control );
. ( Vss, Vdd )
. Vss, Vdd supply0, supply1
supply1 Vdd;
supply0 Vss;
assign a = Vdd;
// Connect a to Vdd
Verilog
. -
. r .
// Resistive nmos,pmos switches
// Resistive cmos switch
// Resistive tran,tranif0,tranif1 switches
Output Strength
pull
pull
weak
medium
medium
small
small
high
rnmos, rpmos
rcmos
rtran, rtranif0, rtranif1
Input Strength
supply
strong
pull
weak
large
medium
small
high
--
: .
i0
out
;) not ( sbar, s
;) cmos ( out, i0, sbar, s
;) cmos ( out, i1, s, sbar
endmodule
i1
sbar