Digital
Digital
www.jntuworld.com
www.jntuworldupdates.org
assign pcinc = pc + 4;
ter
gis
Re ile
1
F
RA
AA
WW
1
RD
JT
1
X5P:21> 0
: <2
Rc
>)
Z 15:0
T(<
X
S
C:
0
)
1
(C
XT
EL
4*S
AS
+4+
Z
PC
IRQ
gic
A
Lo
ol
ntr
Co
L
N
SE
UF
PC SEL
AL
2
RA L
E
AS
EL
BS SEL
WD FN
U
AL
Wr
RF
WE EL
S
WA
2
RA
2
RD
WERF
WD
WE
EL
BS
Wr
B
U
AL
+4
PC
2
01
R/W
WD
ory
em
ta M
Da
RD
r
Ad
EL
WDS
c
l
k
,re
se
t
,
irq
,
If (done) $finish;
www.specworld.in
+4
6>
0:1
: <2
Ra
L
SE
WA
en
dm
od
ul
PC
m
o
du
l
00
e
b
eta
;
data
em_
1>
5:2
: <2
EL
Rc
2S
RA
:0] m
1>
5:1 0
: <1
Rb
t [3 1
PC
L4
SE
on
cti
tru
Ins emory
M
D
Inpu
ILLP JT
r
dO
XA
www.smartzworld.com
02/04/05
L02 Verilog 1
www.jntuworld.com
www.jntuworldupdates.org
www.specworld.in
6.884 Spring 2005
2
02/04/05
www.smartzworld.com
L02 Verilog 2
www.jntuworld.com
www.jntuworldupdates.org
3
02/04/05
www.smartzworld.com
L02 Verilog 3
www.specworld.in
6.884 Spring 2005
4
02/04/05
www.jntuworld.com
www.jntuworldupdates.org
www.smartzworld.com
L02 Verilog 4
www.specworld.in
6.884 Spring 2005
5
02/04/05
www.jntuworld.com
www.jntuworldupdates.org
www.smartzworld.com
L02 Verilog 5
www.jntuworld.com
Advantages of HDLs
www.jntuworldupdates.org
www.specworld.in
6.884 Spring 2005
6
Figure by MIT OCW.
02/04/05
www.smartzworld.com
L02 Verilog 6
www.specworld.in
6.884 Spring 2005
7
02/04/05
www.jntuworld.com
Advantages of HDLs
www.jntuworldupdates.org
www.smartzworld.com
L02 Verilog 7
www.specworld.in
6.884 Spring 2005
8
02/04/05
www.jntuworld.com
Advantages of HDLs
www.jntuworldupdates.org
www.smartzworld.com
L02 Verilog 8
Processor
B
www.jntuworld.com
Advantages of HDLs
www.jntuworldupdates.org
Processor
C
Network
Memory
Bank
A
www.specworld.in
6.884 Spring 2005
Memory
Bank
B
9
02/04/05
www.smartzworld.com
L02 Verilog 9
www.jntuworld.com
Advantages of HDLs
www.jntuworldupdates.org
www.specworld.in
6.884 Spring 2005
10
02/04/05
www.smartzworld.com
L02 Verilog 10
VHDL
Verilog
www.specworld.in
6.884 Spring 2005
11
02/04/05
www.jntuworld.com
www.jntuworldupdates.org
www.smartzworld.com
L02 Verilog 11
Advantages
www.jntuworld.com
www.jntuworldupdates.org
Disadvantages
Some comma gotchas which catch beginners everytime
C syntax can cause beginners to assume C semantics
Easy to create very ugly code, good and consistent
coding style is essential
www.specworld.in
6.884 Spring 2005
12
02/04/05
www.smartzworld.com
L02 Verilog 12
An HDL is NOT a
www.jntuworld.com
www.jntuworldupdates.org
www.specworld.in
6.884 Spring 2005
13
02/04/05
www.smartzworld.com
L02 Verilog 13
www.jntuworld.com
www.jntuworldupdates.org
adder
cout
// HDL modeling of
// adder functionality
sum
endmodule
Don't forget the semicolon!
www.specworld.in
6.884 Spring 2005
14
02/04/05
www.smartzworld.com
L02 Verilog 14
www.jntuworld.com
www.jntuworldupdates.org
adder
cout
www.specworld.in
6.884 Spring 2005
sum
endmodule
15
02/04/05
www.smartzworld.com
L02 Verilog 15
www.jntuworld.com
www.jntuworldupdates.org
b
cin
cout
FA
endmodule
www.specworld.in
6.884 Spring 2005
16
02/04/05
www.smartzworld.com
L02 Verilog 16
www.jntuworld.com
www.jntuworldupdates.org
adder
cout
FA
www.specworld.in
6.884 Spring 2005
FA
FA
FA
FA
FA
FA
FA
fa0(
fa1(
fa2(
fa3(
...
...
...
...
);
);
);
);
endmodule
17
02/04/05
www.smartzworld.com
L02 Verilog 17
www.jntuworld.com
www.jntuworldupdates.org
adder
cout
FA
FA
FA
c0, S[0] );
c1, S[1] );
c2, S[2] );
cout, S[3] );
FA
endmodule
www.specworld.in
0,
c0,
c1,
c2,
18
02/04/05
Carry Chain
www.smartzworld.com
L02 Verilog 18
www.jntuworld.com
www.jntuworldupdates.org
adder
cout
FA
www.specworld.in
6.884 Spring 2005
FA
FA
www.smartzworld.com
L02 Verilog 19
Data Values
Numeric Literals
www.jntuworld.com
Verilog Basics
www.jntuworldupdates.org
4b10_11
0 1
X Z
Underscores
are ignored
Base format
(d,b,o,h)
Decimal number
representing size in bits
32h8XXX_XXA3
www.specworld.in
6.884 Spring 2005
20
02/04/05
www.smartzworld.com
L02 Verilog 20
Behavioral
Dataflow
Module is implemented by
specifying how data flows
between registers
Gate-Level
www.specworld.in
6.884 Spring 2005
21
02/04/05
www.jntuworld.com
www.jntuworldupdates.org
www.smartzworld.com
L02 Verilog 21
Behavioral
Dataflow
The process of
automatically generating a
gate-level model from
either a dataflow or a
behavioral model is called
Gate-Level
www.specworld.in
6.884 Spring 2005
www.jntuworld.com
www.jntuworldupdates.org
Logic Synthesis
22
02/04/05
www.smartzworld.com
L02 Verilog 22
n3;
sel[1]
sel_b[1]
sel[1]
sel_b[1]
);
);
);
);
www.jntuworld.com
www.jntuworldupdates.org
sel[0]
sel[1]
out
d
b
23
02/04/05
www.smartzworld.com
L02 Verilog 23
www.jntuworld.com
www.jntuworldupdates.org
endmodule
24
02/04/05
www.smartzworld.com
L02 Verilog 24
www.jntuworld.com
www.jntuworldupdates.org
www.specworld.in
6.884 Spring 2005
25
02/04/05
www.smartzworld.com
L02 Verilog 25
sel
sel
sel
sel
==
==
==
==
0
1
2
3
)
)
)
)
?
?
?
?
a
b
c
d
:
:
:
: 1bx;
www.jntuworld.com
www.jntuworldupdates.org
endmodule
// Simple four bit adder
module adder( input [3:0] op1, op2,
output [3:0] sum );
assign sum = op1 + op2;
endmodule
www.specworld.in
6.884 Spring 2005
26
02/04/05
www.smartzworld.com
L02 Verilog 26
www.jntuworld.com
www.jntuworldupdates.org
www.specworld.in
6.884 Spring 2005
Arithmetic:
Logical:
Relational:
Equality:
Bitwise:
Reduction:
Shift:
Concatenation:
Conditional:
+ - * / % **
! && ||
> < >= <=
== != === !===
~ & | ^ ^~
& ~& | ~| ^ ^~
{ }
?:
27
02/04/05
Avoid these
operators since
they usually
synthesize poorly
www.smartzworld.com
L02 Verilog 27
www.jntuworld.com
www.jntuworldupdates.org
www.specworld.in
6.884 Spring 2005
Arithmetic:
Logical:
Relational:
Equality:
Bitwise:
Reduction:
Shift:
Concatenation:
Conditional:
+ - * / % **
! && ||
?:
28
02/04/05
assign signal[3:0]
= { a, b, 2b00 }
www.smartzworld.com
L02 Verilog 28
www.jntuworld.com
www.jntuworldupdates.org
reg out;
always @( a or b or c or d or sel )
begin
if ( sel == 0 )
evaluated sequentially
else if ( sel == 2 )
out = c
The code in an always block can
else if ( sel == 3 )
end
code) here we implement a mux
endmodule
www.specworld.in
6.884 Spring 2005
29
02/04/05
www.smartzworld.com
L02 Verilog 29
www.jntuworld.com
www.jntuworldupdates.org
reg out;
always @( a or b or c or d or sel )
begin
if ( sel == 0 )
out = a;
else if ( sel == 1 )
An always block can include a
out = b
sensitivity list if any of these
else if ( sel == 2 )
else if ( sel == 3 )
block is executed
out = d
end
endmodule
www.specworld.in
6.884 Spring 2005
30
02/04/05
www.smartzworld.com
L02 Verilog 30
www.jntuworld.com
www.jntuworldupdates.org
reg out;
always @( a, b,
begin
if ( sel == 0
out = a;
else if ( sel
out = b
else if ( sel
out = c
else if ( sel
out = d
end
c, d, sel )
)
== 1 )
== 2 )
== 3 )
endmodule
www.specworld.in
6.884 Spring 2005
31
02/04/05
www.smartzworld.com
L02 Verilog 31
www.jntuworld.com
www.jntuworldupdates.org
begin
if ( sel == 0
out = a;
else if ( sel
out = b
else if ( sel
out = c
else if ( sel
out = d
end
)
== 1 )
== 2 )
== 3 )
endmodule
www.specworld.in
6.884 Spring 2005
32
02/04/05
www.smartzworld.com
L02 Verilog 32
www.jntuworld.com
www.jntuworldupdates.org
reg out;
always @( * )
begin
if ( sel == 0
out = a;
else if ( sel
out = b
else if ( sel
out = c
else if ( sel
out = d
end
)
== 1 )
== 2 )
== 3 )
endmodule
www.specworld.in
6.884 Spring 2005
33
02/04/05
www.smartzworld.com
L02 Verilog 33
www.jntuworld.com
www.jntuworldupdates.org
reg out;
always @( * )
begin
case ( sel )
0 : out = a;
1 : out = b;
2 : out = c;
3 : out = d;
endcase
end
endmodule
www.specworld.in
6.884 Spring 2005
34
02/04/05
www.smartzworld.com
L02 Verilog 34
end
www.jntuworld.com
www.jntuworldupdates.org
endmodule
www.specworld.in
6.884 Spring 2005
35
02/04/05
www.smartzworld.com
L02 Verilog 35
www.specworld.in
6.884 Spring 2005
www.jntuworld.com
www.jntuworldupdates.org
www.smartzworld.com
L02 Verilog 36
www.jntuworld.com
www.jntuworldupdates.org
reg out;
always @( * )
begin
case ( sel )
0 : out = a;
1 : out = b;
2 : out = c;
3 : out = d;
endcase
end
endmodule
www.specworld.in
6.884 Spring 2005
37
02/04/05
www.smartzworld.com
Figure by MIT
OCW.
L02 Verilog 37
www.jntuworld.com
www.jntuworldupdates.org
reg out;
always @( * )
begin
case ( sel )
0 : out = a;
1 : out = b;
2 : out = c;
3 : out = d;
endcase
end
endmodule
www.specworld.in
6.884 Spring 2005
38
02/04/05
Figure by www.smartzworld.com
MIT OCW.
L02 Verilog 38
www.jntuworld.com
www.jntuworldupdates.org
reg out;
always @( * )
begin
case ( sel )
default : out = 1bx;
0 : out = a;
1 : out = b;
2 : out = c;
3 : out = d;
endcase
end
endmodule
www.specworld.in
6.884 Spring 2005
39
02/04/05
www.smartzworld.com
Figure by MIT
OCW.
L02 Verilog 39
clk
next_x
clk
clk
www.specworld.in
6.884 Spring 2005
clk
www.jntuworld.com
www.jntuworldupdates.org
40
02/04/05
clk
www.smartzworld.com
L02 Verilog 40
www.jntuworld.com
www.jntuworldupdates.org
clk
clk
www.specworld.in
6.884 Spring 2005
41
02/04/05
www.smartzworld.com
L02 Verilog 41
www.jntuworld.com
www.jntuworldupdates.org
Behaviora
l
Dataflow
G at
e-L
eve
l
www.specworld.in
6.884 Spring 2005
42
02/04/05
www.smartzworld.com
L02 Verilog 42
Behavioral
Dataflow
Gate-Level
www.specworld.in
6.884 Spring 2005
www.jntuworld.com
www.jntuworldupdates.org
43
02/04/05
www.smartzworld.com
L02 Verilog 43
www.jntuworld.com
www.jntuworldupdates.org
Behavioral
Dataflow
Gate-Level
www.specworld.in
6.884 Spring 2005
44
02/04/05
www.smartzworld.com
L02 Verilog 44
www.jntuworld.com
www.jntuworldupdates.org
...
endmodule
// Specify parameters at instantiation time
mux4 #( .width(32) )
alu_mux( .a(op1), .b(bypass), .c(32b0), .d(32b1),
.sel(alu_mux_sel), .out(alu_mux_out) );
45
02/04/05
www.smartzworld.com
L02 Verilog 45
www.jntuworld.com
www.jntuworldupdates.org
carry[i], carry[i+1] );
end
endgenerate
endmodule
www.specworld.in
6.884 Spring 2005
46
02/04/05
www.smartzworld.com
L02 Verilog 46
www.jntuworld.com
Static Elaboration
www.jntuworldupdates.org
Model
Static Elaboration
Elaborated Model
Synthesis
Gate-Level
www.specworld.in
6.884 Spring 2005
47
02/04/05
www.smartzworld.com
L02 Verilog 47
GCD
www.specworld.in
6.884 Spring 2005
www.jntuworld.com
Larger Examples
www.jntuworldupdates.org
Beta
48
02/04/05
www.smartzworld.com
L02 Verilog 48
www.jntuworld.com
www.jntuworldupdates.org
integer
done;
begin
done = 0;
A = A_in; B = B_in;
while ( !done )
begin
if ( A < B )
begin
swap = A;
A = B;
B = swap;
end
else if ( B != 0 )
A = A - B;
else
done = 1;
end
Y = A;
end
endmodule
www.specworld.in
6.884 Spring 2005
49
02/04/05
www.smartzworld.com
L02 Verilog 49
GCD Behavioral
Test Harness
module gcd_test;
parameter width = 16;
reg [width-1:0] A_in, B_in;
wire [width-1:0] Y;
www.jntuworld.com
www.jntuworldupdates.org
gcd_behavioral #( .width(width) )
initial
begin
B_in = 15;
endmodule
www.specworld.in
6.884 Spring 2005
50
02/04/05
www.smartzworld.com
L02 Verilog 50
go
done
Control Unit
zero?
lt
www.jntuworld.com
www.jntuworldupdates.org
out
A_in
sub
B_in
Design
Design Strategy
Strategy
Partition
Partition into
into control
control and
and datapath
datapath
Keep
Keep all
all functional
functional code
code in
in the
the leaf
leaf modules
modules
www.specworld.in
6.884 Spring 2005
51
02/04/05
www.smartzworld.com
L02 Verilog 51
www.jntuworld.com
www.jntuworldupdates.org
reg [width-1:0] A, B;
assign Y = A;
// Datapath logic
wire [width-1:0] out
= ( out_mux_sel ) ? B : A - B;
if ( A_en )
A <= A_next;
if ( B_en )
B <= B_next;
end
Edge-triggered
flip-flops with
enables
A mix of dataflow
and behavioral
endmodule
www.specworld.in
6.884 Spring 2005
52
02/04/05
www.smartzworld.com
L02 Verilog 52
www.jntuworld.com
www.jntuworldupdates.org
// The running bit is one after go goes high and until done goes high
reg running = 0;
begin
if ( go )
running <= 1;
end
always
begin
if (
else
else
else
end
@(*)
!running )
if ( A_lt_B )
if ( !B_zero )
ctrl_sig
ctrl_sig
ctrl_sig
ctrl_sig
=
=
=
=
6'b11_00x_0;
6'b11_111_0;
6'b10_1x0_0;
6'b00_xxx_1;
//
//
//
//
endmodule
www.specworld.in
6.884 Spring 2005
53
02/04/05
www.smartzworld.com
L02 Verilog 53
www.jntuworld.com
GCD Testing
www.jntuworldupdates.org
Test Inputs
Behavioral
Model
RTL
Model
Identical
Outputs?
www.specworld.in
6.884 Spring 2005
54
02/04/05
www.smartzworld.com
L02 Verilog 54
Beta Redux
ILL
XAdr OP JT
PCSEL
PC
www.jntuworld.com
www.jntuworldupdates.org
00
+4
Instruction
Memory
Ra: <20:16>
RA2SEL
WASEL
XP
Rc: <25:21>
RA1
WA
WA
RD1
PC+4+4*SXT(C)
IRQ
Rc: <25:21>
Rb: <15:11>
Register
File
RA2
WD
RD2
WE
WERF
JT
C: SXT(<15:0>)
Z
ASEL
BSEL
Control Logic
PCSEL
RA2SEL
ASEL
ALU
ALUFN
WD
BSEL
WDSEL
ALUFN
Wr
WERF
WASEL
Wr
Data Memory
Adr
RD
PC+4
0 1 2
www.specworld.in
R/W
55
02/04/05
WDSEL
www.smartzworld.com
L02 Verilog 55
www.jntuworld.com
www.jntuworldupdates.org
Think about physical partition since wires that cross boundaries can take
lots of area and blocks have to fit into the floorplan without wasteful
gaps.
www.specworld.in
6.884 Spring 2005
56
02/04/05
www.smartzworld.com
L02 Verilog 56
details so we could
www.specworld.in
6.884 Spring 2005
57
02/04/05
www.jntuworld.com
www.jntuworldupdates.org
www.smartzworld.com
L02 Verilog 57
PCSEL
JT
2
1
PC
00
Instruction
Memory
D
+4
Ra: <20:16>
www.jntuworld.com
www.jntuworldupdates.org
RA2SEL
WASEL
XP
PC
Rc: <25:21>
RA1
WA
WA
RD1
PC+4+4*SXT(C)
IRQ
Rc: <25:21>
Rb: <15:11>
Register
File
RA2
WD
RD2
WE
1
WERF
JT
C: SXT(<15:0>)
Z
ASEL
BSEL
Control Logic
PCSEL
RA2SEL
ASEL
ALU
ALUFN
WD
BSEL
WDSEL
ALUFN
Wr
WERF
Main Datapath
Wr
Data Memory
Adr
WASEL
www.specworld.in
R/W
PC+4
0 1 2
58
02/04/05
RD
WDSEL
www.smartzworld.com
L02 Verilog 58
www.jntuworld.com
www.jntuworldupdates.org
59
02/04/05
www.smartzworld.com
L02 Verilog 59
www.jntuworld.com
Laboratory 1
www.jntuworldupdates.org
% cp r /mit/6.884/examples/gcd .
% cat gcd/README
www.specworld.in
6.884 Spring 2005
60
02/04/05
www.smartzworld.com
L02 Verilog 60