L02 Verilog
L02 Verilog
Rc
: <2
5:2
1>
2S
EL
assign pcinc = pc + 4;
RA
on 1>
XA
ILLP JT
dOr
2
1
0
A M
tru
cti
Ins emory
6>
Rb
: <1
5:1 0 1
RA
2 WD WERF for (i=0; i < 31; i = i+1) begin
D 0:1 ter WE
3 00 : <2 gis
L4 Ra Re ile RD
2
SE PC SE
L 1 F
PC RA
WA A
WWA
1
1 RD
X5P:21> 0 JT EL
+4 BS
+ : <2
Rc >) 1
0
Z 15:0 Wr
XT(<
S
C: R/W
)
0 WD
1
XT
(C ory
EL em
4*S AS ta M
+4+ Z
B Da
PC RD
IRQ U Ad
r
gic AL
Lo A
ol
ntr
Co L
SE UF
N
PC SEL AL
2
RA L
E
AS
EL
BS SEL
WD FN EL
+4 WDS
AL
U PC 2
Wr 01
RF
WE EL
S
WA
,…
t,irq
se Inpu
lk,re t [3 1
c ( :0] m
beta em_
e l data
odu ;
m
If (done) $finish; e
dul
o
ndm
e
Network
Memory Memory
Bank Bank
A B
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
module foo(clk,xi,yi,done);
input [15:0] xi,yi;
output done;
endmodule
c
A B
module adder( input [3:0] A, B,
output cout,
adder output [3:0] S );
cout S
FA fa0( ... );
FA fa1( ... );
FA fa2( ... );
FA fa3( ... );
FA FA FA FA endmodule
A B
module adder( input [3:0] A, B,
output cout,
adder output [3:0] S );
cout S
wire c0, c1, c2;
FA fa0( A[0], B[0], 0, c0, S[0] );
FA fa1( A[1], B[1], c0, c1, S[1] );
FA fa2( A[2], B[2], c1, c2, S[2] );
FA fa3( A[3], B[3], c2, cout, S[3] );
FA FA FA FA
endmodule
Carry Chain
A B
module adder( input [3:0] A, B,
output cout,
adder output [3:0] S );
cout S
wire c0, c1, c2;
FA fa0( .a(A[0]), .b(B[0]),
.cin(0), .cout(c0),
.sum(S[0] );
endmodule
4’b10_11
0 1 Underscores
X Z are ignored
Base format
(d,b,o,h)
Decimal number
representing size in bits
32’h8XXX_XXA3
6.884 – Spring 2005 02/04/05 L02 – Verilog 20
3 Common Abstraction Levels
Module is implemented by
Dataflow specifying how data flows
between registers
endmodule
endmodule
endmodule
An implicit continuous assignment combines
the net declaration with an assign statement
and thus is more succinct
endmodule
endmodule
reg out;
always @( a or b or c or d or sel )
begin
if ( sel == 0 ) An always block is a behavioral
out = a;
block which contains a list of
else if ( sel == 1 )
out = b expressions which are (usually)
else if ( sel == 2 ) evaluated sequentially
out = c
else if ( sel == 3 ) The code in an always block can
out = d be very abstract (similar to C
end code) – here we implement a mux
with an if/else statement
endmodule
reg out;
always @( a or b or c or d or sel )
begin
if ( sel == 0 )
out = a;
An always block can include a
else if ( sel == 1 )
out = b
else if ( sel == 2 ) sensitivity list – if any of these
out = c signals change then the always
else if ( sel == 3 ) block is executed
out = d
end
endmodule
reg out;
always @( a, b, c, d, sel )
begin
if ( sel == 0 )
out = a;
else if ( sel == 1 )
out = b In Verilog-2001 we can use a
else if ( sel == 2 ) comma instead of the or
out = c
else if ( sel == 3 )
out = d
end
endmodule
reg out;
always @( * )
begin
if ( sel == 0 )
out = a;
else if ( sel == 1 )
out = b In Verilog-2001 we can use the
else if ( sel == 2 ) @(*) construct which creates a
out = c sensitivity list for all signals
else if ( sel == 3 )
read in the always block
out = d
end
endmodule
reg out;
always @( * )
begin
case ( sel )
0 : out = a; Always blocks can contain case
1 : out = b; statements, for loops, while loops,
2 : out = c; even functions – they enable
3 : out = d; high-level behavioral modeling
endcase
end
endmodule
reg out;
What about this funny No! and whoever
always @( * ) reg statement? decided on the reg
begin Is this how you create syntax really
case ( sel ) a register in Verilog? messed things up!
0 : out = a;
1 : out = b;
2 : out = c;
3 : out = d;
endcase
end
endmodule
reg out;
In Verilog a reg is just a variable –
always @( * ) when you see reg think variable not
begin hardware register!
case ( sel )
0 : out = a; Any assignments in an always block
1 : out = b; must assign to a reg variable – the
reg variable may or may not actually
2 : out = c;
3 : out = d;
endcase represent a hardware register
end
If the always block assigns a value to
endmodule the reg variable for all possible
executions then the reg variable is
not actually a hardware register
endmodule
endmodule
endmodule
next_x D Q X next_x D Q X
clk clk
X X
next_x D Q Y next_x D Q D Q Y
clk clk clk
X Y
D Q X
clk
Behaviora
l
Gat Dataflow
e-L
eve
l
Gate-Level
endmodule
Model
Static Elaboration
Elaborated Model
Synthesis
Gate-Level
GCD Beta
while ( !done )
begin
if ( A < B )
begin
swap = A;
We write the general
A = B; algorithm in an always block
using a very C-like syntax
B = swap;
end
else if ( B != 0 )
A = A - B;
else
done = 1;
end
Y = A;
end
endmodule
Test Harness
reg [width-1:0] A_in, B_in;
wire [width-1:0] Y;
gcd_behavioral #( .width(width) )
gcd_unit( .A_in(A_in), .B_in(B_in), .Y(Y) );
initial
begin
zero? lt out
A_in
A
sub
B_in
B
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
reg [width-1:0] A, B;
assign Y = A;
// Datapath logic
wire [width-1:0] out = ( out_mux_sel ) ? B : A - B;
wire [width-1:0] A_next = ( A_mux_sel ) ? out : A_in;
wire [width-1:0] B_next = ( B_mux_sel ) ? A : B_in;
endmodule
// The running bit is one after go goes high and until done goes high
reg running = 0;
always @( posedge clock )
begin
if ( go ) running <= 1;
else if ( done ) running <= 0;
end
always @(*)
begin
if ( !running ) ctrl_sig = 6'b11_00x_0; // Latch in A and B values
else if ( A_lt_B ) ctrl_sig = 6'b11_111_0; // A <= B and B <= A
else if ( !B_zero ) ctrl_sig = 6'b10_1x0_0; // A <= A - B and B <= B
else ctrl_sig = 6'b00_xxx_1; // Done
end
endmodule
Test Inputs
Behavioral RTL
Model Model
Identical
Outputs?
PCSEL 4 3 2 1 0
PC 00
A Instruction
Memory
+4 D
XP
RA1 Register RA2
1
WD
Rc: <25:21> WA
WA
File
0
RD1 RD2 WE WERF
Z
JT
C: SXT(<15:0>)
PC+4+4*SXT(C)
IRQ Z
ASEL 1 0 1 0 BSEL
Control Logic
PCSEL
RA2SEL A B
ASEL ALU WD R/W Wr
ALUFN
BSEL
WDSEL Data Memory
ALUFN Adr RD
Wr
WERF
WASEL
PC+4
0 1 2 WDSEL
2 + WASEL
XP 1
RA1 Register RA2
WD
1
PC
Rc: <25:21> WA
WA
File
0
RD1 RD2 WE WERF
Z
JT
C: SXT(<15:0>)
PC+4+4*SXT(C)
IRQ Z
ASEL 1 0 1 0 BSEL
Control Logic
PCSEL
RA2SEL A B
ASEL ALU WD R/W Wr
ALUFN
BSEL
WDSEL Data Memory
ALUFN Adr RD
Wr
WERF
WASEL
PC+4 1
0 1 2 WDSEL
2
Main Datapath
6.884 – Spring 2005 02/04/05 L02 – Verilog 58
Take Away Points
Hardware description languages are an essential
part of modern digital design
– HDLs can provide an executable functional specification
– HDLs enable design space exploration early in design process
– HDLs encourage the development of automated tools
– HDLs help manage complexity inherent in modern designs