Professional Documents
Culture Documents
Verilog HDL
Edited by Chu Yu
http://ece.niu.edu.tw/~chu/
(2007/2/26)
<##>
1
Edited by Chu Yu
Verilog HDL
<##>
2
Edited by Chu Yu
Verilog HDL
Verilog HDL
<##>
3
Edited by Chu Yu
Verilog HDL
Verilog HDL
<##>
4
Edited by Chu Yu
Verilog HDL
Verilog
Programming LanguageHDL
V.S. Verilog HDL
Programming Language
if (a>b) sub a, b
{ compiler assembler Computer
(CPU)
}
(C code) (asm code)
Verilog HDL
if (a>b)
begin
I0
I1
end out
synthesizer In
M
(Verilog code)
a
b a>b
(logic circuit)
(cell library) <##>
5
Edited by Chu Yu
Verilog HDL
Verilog HDL
<##>
6
Edited by Chu Yu
Verilog HDL
Et t
An event Et at time t
Schedules another event t+1
at time t + 2
t+2
U0
U1
A hs
B Sum
U2
U3
1 U4
3 1 hc1
2 3 1
2 3
2 Co
hc0
Cin
<##>
9
Edited by Chu Yu
Verilog HDL
<##>
10
Edited by Chu Yu
Verilog HDL
Behavioral Modeling
RTL Modeling
<##>
11
Edited by Chu Yu
Verilog HDL
RTL Model
(Verilog HDL)
Logic Synthesis
(Synopsys)
GDS II
<##>
12
Edited by Chu Yu
Verilog HDL
Verilog-HDL Simulators
VCS (Synopsys)
Platform
Windows NT/XP, SUN Solaris (UNIX), Linux.
Modelsim (Mentor)
Platform
Windows NT/XP, SUN Solaris (UNIX), Linux.
NC-Verilog (Cadence)
Platform
Windows NT/XP, SUN Solaris (UNIX), Linux.
Verilog-XL (Cadence)
Platform
SUN Solaris (UNIX).
Other Simulators
MAX+PLUS II, Quartus II (Altera)
Active HDL (Aldec), Silos (Silvaco),
<##>
13
Edited by Chu Yu
Verilog HDL
<##>
14
Edited by Chu Yu
Verilog HDL
Example of Adder
z A Full Adder
module name
a
xor u0(sum, a, b, cin); w1
U4
carry
cin U2
and u1(w0, a, b);
and u2(w1, b, cin); b
and u3(w2, cin, b); cin U3 w2
<##>
15
Edited by Chu Yu
Verilog HDL
<##>
16
Edited by Chu Yu
Verilog HDL
Identifiers of Verilog
<##>
17
Edited by Chu Yu
Verilog HDL
Escaped Identifiers
<##>
18
Edited by Chu Yu
Verilog HDL
Case Sensitivity
Example:
module inv(out, in); module Inv(out, in);
endmodule endmodule
<##>
19
Edited by Chu Yu
Verilog HDL
z Verilog Module
Modules are basic building blocks in hierarchy.
Every module description starts with module
name(output_ports, input_ports), and ends with
endmodule.
z Module Ports
Module ports are equivalent to the pins in hardware.
Declare ports to be input, output, or inout (bidirectional)
in the module description.
<##>
20
Edited by Chu Yu
Verilog HDL
<##>
21
Edited by Chu Yu
Verilog HDL
Registers
z More Examples
reg mem1[127:0]; //128-bit memory with 1-bit wide
reg mem2[63:0];
reg [7:0] mem3[127:0]; //128-bit memory with 8-bit wide
M
mem2=0; // illegal syntax
mem2[5] = mem1[125];
mem2[10:8] = mem1[120:118];
mem3[11]=0; //8-bit zero value
<##>
22
Edited by Chu Yu
Verilog HDL
<##>
23
Edited by Chu Yu
Verilog HDL
Example of Nets
z Example I
<##>
24
Edited by Chu Yu
Verilog HDL
Example of Nets
z Example II
<##>
25
Edited by Chu Yu
Verilog HDL
<##>
26
Edited by Chu Yu
Verilog HDL
<##>
27
Edited by Chu Yu
Verilog HDL
ctl
ctl
pctl
nctl
<##>
28
Edited by Chu Yu
Verilog HDL
<##>
29
Edited by Chu Yu
Verilog HDL
<##>
30
Edited by Chu Yu
Verilog HDL
<##>
31
Edited by Chu Yu
Verilog HDL
<##>
32
Edited by Chu Yu
Verilog HDL
Operators
Unary Operator
assign a = ~b;
Binary Operator
assign a = b&c;
Ternary Operator
assign out = sel ? a: b; //2-to-1 multiplexer
Comments
One Line Comment
// this is an example of one line comment
Multiple Line Comment
/* this is an example of
multiple line comment */
Error Comment Remarks
/* Error comment remark */ */
<##>
33
Edited by Chu Yu
Verilog HDL
Literal Numbers
z Examples
12 32-bit decimal
8'd45 8-bit decimal
10'hF1 10-bit hex (left-extended with zero)
1'B1 1-bit binary
32'bz 32-bit Z
6'b001_010 6-bit binary with underscore for readability.
<##>
36
Edited by Chu Yu
Verilog HDL
Block Statement
<##>
37
Edited by Chu Yu
Verilog HDL
z Examples
always @(posedge clk) posedge clk?
n
begin q d
y
#5 q=d;
#1 qb=~d; #5 q=d qb ~d
end
#1 qb=~d t=0 t=5 t=6
<##>
38
Edited by Chu Yu
Verilog HDL
wait(en1|en2) read=1;
#5 read=0; read=1 t=0 t=n t=n+5
end
#5 read=0
set
n
always wait(set) set=1? clk
begin
@(negedge clk);
n q
#3 q=1; negedge clk?
Syntax of Verilog
<##>
40
Edited by Chu Yu
Verilog HDL
z Looping Controls
forever loop while loop
example example
forever #100 clk=~clk; while(val[index]==1'b0)
always #100 clk=~clk; index=index-1;
repeat loop for loop
example example
repeat(mem_depth) for(index=0;index<size;
begin index=index+1)
mem[address]=0; if(val[index]==1'bx)
address=address+1; $display(''found an x'');
end
<##>
41
Edited by Chu Yu
Verilog HDL
Continuous Assignment
z Continuous assignment provide a means to abstractly
model combinational hardware driving values onto nets.
An alternate version of the 1-bit full adder is shown blow:
module FA(Cout, Sum, a, b, Cin);
output Cout, Sum;
input a, b, Cin;
assign Sum = a ^ b ^ Cin,
Cout = (a & b) | (b & Cin) | (a & Cin);
endmodule
<##>
43
Edited by Chu Yu
Verilog HDL
Procedural Assignments
<##>
45
Edited by Chu Yu
Verilog HDL
t = 11
a 1
t=0 t=10
$display a
<##>
48
Edited by Chu Yu
Verilog HDL
t = 11
a 1
t=0 t=10
$display a
<##>
49
Edited by Chu Yu
Verilog HDL
Hierarchical Design
<##>
50
Edited by Chu Yu
Verilog HDL
<##>
51
Edited by Chu Yu
Verilog HDL
<##>
52
Edited by Chu Yu
Verilog HDL
<##>
54
Edited by Chu Yu
Verilog HDL
`timescale 1ns/10ps 0 ps
module m1();
1 ps
`timescale 100ns/1ns 2 ps
module m2();
3 ps
`timescale 1ps/1ps 4 ps
module m3();
<##>
55
Edited by Chu Yu
Verilog HDL
Compiler Directives
<##>
56
Edited by Chu Yu
Verilog HDL
Compiler Directives
<##>
57
Edited by Chu Yu
Verilog HDL
Compiler Directives
<##>
58
Edited by Chu Yu
Verilog HDL
Parameters
z Parameters are constants rather than variables.
z Typically parameters are used to specify delays and
width of variable.
Example:
module varmux(out, I0, I1, sel);
parameter width=2, delay=1;
output [width-1:0] out;
input [width-1:0] I0, I1;
input sel;
assign #delay out=sel? I1:I0;
endmodule
<##>
59
Edited by Chu Yu
Verilog HDL
<##>
60
Edited by Chu Yu
Verilog HDL
z $<identifier>
Sign$ denotes Verilog system tasks and functions.
A number of system tasks and functions are available to
perform different operations such as
- Finding the current simulation time ($time).
- Displaying/monitoring the values of signals ($display,
$monitor).
- Stopping the simulation ($stop).
- Finishing the simulatioon ($finish).
<##>
61
Edited by Chu Yu
Verilog HDL
Examples of Verilog-HDL
z 2-to-1 Multiplexer
module mux(out, I0, I1, sel);
output [3:0] out;
input [3:0] I0, I1; 4
input sel; I0 0 4
4 Out
reg [3:0] out; I1 1
always @(I0 or I1 or sel)
sel
if (sel==1b1) out=I1;
else out=I0;
endmodule
//The output value will be changed when one of I0, I1, and sel input signals is
changing.
<##>
62
Edited by Chu Yu
Verilog HDL
Example of Verilog-HDL
z 2-to-1 Multiplexer
endmodule
<##>
63
Edited by Chu Yu
Verilog HDL
Example of Verilog-HDL
z 4-to-1 Multiplexer
module mux(out, I3, I2, I1, I0, sel);
output [3:0] out;
input [3:0] I3, I2, I1, I0; 4
input [1:0] sel; I0 0
4
reg [3:0] out; I1 1 4
4 Out
always @(I3 or I2 or I1 or I0 or sel) I2 2
case (sel) 4
I3 3
2b00: out=I0;
2b01: out=I1; 2
2b10: out=I2; sel
2b11: out=I3;
default: out=4bx;
endcase
endmodule
<##>
64
Edited by Chu Yu
Verilog HDL
Example of Verilog-HDL
z 4-to-1 Multiplexer
module mux(out, I3, I2, I1, I0, sel);
output [3:0] out; 4
input [3:0] I3, I2, I1, I0; I0 0
4
input [1:0] sel; I1 1 4
4 Out
assign out = (sel==2b00)?I0: I2 2
(sel==2b01)?I1: I3 4
3
(sel==2b10)?I2:
(sel==2b11)?I3: 2
sel
4bx;
endmodule
<##>
65
Edited by Chu Yu
Verilog HDL
Example of Verilog-HDL
z 3-to-1 Multiplexer
module mux(out, I3, I1, I0, sel);
output [3:0] out;
input [3:0] I3, I1, I0;
input [1:0] sel;
reg [3:0] out;
always @(I3 or I1 or I0 or sel)
case (sel) //synopsys full_case -> If condition 2b10 is impossible
2b00: out=I0; to appear.
2b01: out=I1;
2b11: out=I3;
endcase
endmodule // Require latches to synthesis the priority encoder circuit
if the remark synopsys full_case is not assigned.
<##>
66
Edited by Chu Yu
Verilog HDL
Example of Verilog-HDL
z Compound Logic
module complogic1(F, x, y, z);
output F;
input x, y, z;
assign F = (x&~y)|(y|z);
endmodule x yp
xy
y F
module complogic2(F, x, y, z); yz
output F; z
input x, y, z;
not u1(yp, y);
and u2(xy, x, yp);
or u3(yz, y, z);
or u4(F, xy, yz);
endmodule <##>
67
Edited by Chu Yu
Verilog HDL
Example of Verilog-HDL
z casex I
module top(R1, R2, R3, I2, I1, I0);
output R1, R2, R3;
input I2, I1, I0;
reg R1, R2, R3;
always @(I2 or I1 or I0)
casex ({I2, I1, I0})
3b1??: {R1, R2, R3}=3b100;
3b?1?: {R1, R2, R3}=3b010; // ? = {0, 1}
3b??1: {R1, R2, R3}=3b001; // ? != {x, z}
endcase
endmodule
<##>
68
Edited by Chu Yu
Verilog HDL
Example of Verilog-HDL
z casex II
module top(R1, R2, R3, I2, I1, I0);
output R1, R2, R3;
input I2, I1, I0;
reg R1, R2, R3;
always @(I2 or I1 or I0)
casex (1b1)
I0: {R1, R2, R3}=3b100;
I1: {R1, R2, R3}=3b010;
I2: {R1, R2, R3}=3b001;
endcase
endmodule
<##>
69
Edited by Chu Yu
Verilog HDL
Example of Verilog-HDL
z casez
module top(R1, R2, R3, I2, I1, I0);
output R1, R2, R3;
input I3, I1, I0;
reg R1, R2, R3;
always @(I2 or I1 or I0)
casez ({I2, I1, I0})
3b1??: {R1, R2, R3}=3b100;
3b?1?: {R1, R2, R3}=3b010; // ? = {0, 1, x, z}
3b??1: {R1, R2, R3}=3b001;
endcase
endmodule
<##>
70
Edited by Chu Yu
Verilog HDL
Example of Verilog-HDL
z Comparator
<##>
71
Edited by Chu Yu
Verilog HDL
Hierarchical Modules
<##>
72
Edited by Chu Yu
Verilog HDL
Example of Verilog-HDL
z Resource Sharing
<##>
73
Edited by Chu Yu
Verilog HDL
Example of Verilog-HDL
z 1-bit latch
Latch
assign q=(rst==0)?0:
enable
(enable==1)?d:q;
endmodule rst
<##>
74
Edited by Chu Yu
Verilog HDL
Example of Verilog-HDL
<##>
75
Edited by Chu Yu
Verilog HDL
Example of Verilog-HDL
<##>
76
Edited by Chu Yu
Verilog HDL
Example of Verilog-HDL
<##>
77
Edited by Chu Yu
Verilog HDL
Example of Verilog-HDL
<##>
78
Edited by Chu Yu
Verilog HDL
z Mealy Machine (Finite State Machine)
module mealy(out, in, rst, clk);
output out; In/Out 0/0 0/0
input in;
input clk, rst;
reg out; 1/0 s0 s1 s2 s3
0/0 1/0 0/0
reg [1:0] state;
parameter s0=2d0, s1=2d1, s2=2d2, s3=2d3; 1/0 1/1
always @(posedge clk or negedge rst)
if (rst==0) begin state=s0; out=0; end
else begin
case (state)
s0: if (in==0) begin out=0; state=s1; end
else begin out=0; state=s0; end
s1: if (in==0) begin out=0; state=s1; end
else begin out=0; state=s2; end
s2: if (in==0) begin out=0; state=s3; end
else begin out=0; state=s0; end
s3: if (in==0) begin out=0; state=s1; end
else begin out=1; state=s2; end
default: state=s0;
endcase
end
endmodule <##>
79
Edited by Chu Yu
Verilog HDL
z Moore Machine
module moore(out, in, rst, clk); 0 0
output out;
input in; 1 s0/0 s1/0 s2/0 s3/1
0 1 0
input clk, rst;
reg out; 1 1
reg [1:0] state;
parameter s0=2d0, s1=2d1, s2=2d2, s3=2d3;
always @(posedge clk or negedge rst)
if (rst==0) begin state=s0; out=0; end
else begin
case (state)
s0: begin out=0; if (in==0) state=s1; else state=s0; end
s1: begin out=0; if (in==0) state=s1; else state=s2; end
s2: begin out=0; if (in==0) state=s3; else state=s0; end
s3: begin out=1; if (in==0) state=s1; else state=s2; end
default: state=s0;
endcase
end
endmodule
<##>
80
Edited by Chu Yu
Verilog HDL
z RAM
module RAM(out, in, addr, RW, CS);
output [7:0] out;
input [7:0] in;
input [3:0] addr; in out
input RW,CS;
reg [7:0] out; addr RAM
reg [7:0] DATA[15:0];
RW
always @(negedge CS) CS
begin
if (RW==1'b0) //READ
out=DATA[addr];
else
if (RW==1'b1) //WRITE
DATA[addr]=in;
else
out=8'bz;
end
endmodule
<##>
81
Edited by Chu Yu
Verilog HDL
z ROM
module ROM_Qe(out, addr, CS);
output [15:0] out;
input [3:0] addr;
input CS; addr out
reg [15:0] out;
reg [15:0] ROM[15:0]; RAM
always @(negedge CS)
begin CS
ROM[0]=16'h5601; ROM[1]=16'h3401;
ROM[2]=16'h1801; ROM[3]=16'h0ac1;
ROM[4]=16'h0521; ROM[5]=16'h0221;
ROM[6]=16'h5601; ROM[7]=16'h5401;
ROM[8]=16'h4801; ROM[9]=16'h3801;
ROM[10]=16'h3001; ROM[11]=16'h2401;
ROM[12]=16'h1c01; ROM[13]=16'h1601;
ROM[14]=16'h5601; ROM[15]=16'h5401;
out=ROM[addr];
end
endmodule
<##>
82
Edited by Chu Yu
Verilog HDL
z I/O read/write
<##>
83
Edited by Chu Yu
Verilog HDL
<##>
84
Edited by Chu Yu
Verilog HDL
0 Logic 0
1 Logic 1
x Unknown
? Iteration of 0, 1, and x input field
b Iterayion of 0 and 1 input field
- No change output field
(vw) Change of value from v to w
* Same as (??) Any value change on input
r Same as (01) Rising edge on input
f Same as (10) Falling edge on input
p Iteration of (01), (0x), and (x1) Positive edge including x
n Iteration of (10), (1x), and (x0) Negative edge including x
<##>
85
Edited by Chu Yu
Verilog HDL
UDP Definition
<##>
86
Edited by Chu Yu
Verilog HDL
primitive latch(q,clock,data);
output q;
reg q;
input clock,data;
table
// clock data : state_output : next_state The '?' is used to represent don't
0 1 : ? : 1; care condition in either inputs or
0 0 : ? : 0; current state.
1 ? : ? : -;
The '-' in the output field indicates
endtable
'no change'.
endprimitive
<##>
87
Edited by Chu Yu
Verilog HDL
<##>
88
Edited by Chu Yu
Verilog HDL
<##>
89
Edited by Chu Yu
Verilog HDL
<##>
90
Edited by Chu Yu
Verilog HDL
<##>
91
Edited by Chu Yu
Verilog HDL
Pull
Strong0
Strong0
Supply1
Supply1
Large1
<##>
92
Edited by Chu Yu
Verilog HDL
<##>
93
Edited by Chu Yu
Verilog HDL
Specify Blocks
a
sum
b
Full Adder carry
cin
<##>
94
Edited by Chu Yu
Verilog HDL
specify
specparam
InCap$d = 0.024, Tsetup$d_cp = 0.41, Thold$d_cp = 0.2;
(clk => q) = (0.390, 0.390);
$setup(d, posedge clk, Tsetup$d_cp, notifier);
$hold(posedge clk, d, Thold$d_cp, notifier); }perform timing check
endspecify
endmodule
<##>
95
Edited by Chu Yu
Verilog HDL
<##>
96
Edited by Chu Yu
Verilog HDL
Test Fixture
a Response
Stimulus sum
b Generation
and Full Adder carry and
Control cin
Verification
z Test the full adders Verilog model by applying test patterns and
observing its output responses.
Stimulus and control: Changes on device inputs, simulation finish
time, ... etc.
Device under test: Behavior, gate, or switch level modules.
Response generation and verification: Which signals to
save/display, verification of generated response.
<##>
97
Edited by Chu Yu
Verilog HDL
Verilog Simulation
Verilog Parser
0.00 ns in = 0 out = x
16.00 ns in = 0 out = 1
100.00 ns in = 1 out = 1
<##>
98
Edited by Chu Yu
Verilog HDL
z Monitoring Commands
Text Format Output
$monitor($time,''a=%d, b=%b,...\n'',a,b);
Graphic Output
$gr_waves(''<signal_label>'',<signal>, ...);
$SimWave: $shm_open(<file_name>), $shm_probe( )
<##>
100
Edited by Chu Yu
Verilog HDL
<##>
101
Edited by Chu Yu
Verilog HDL
SimWave
SimWave Window
<##>
103
Edited by Chu Yu
Verilog HDL
Trouble Shooting
<##>
104