Digital Design and Synthesis: Fall 09
Digital Design and Synthesis: Fall 09
Fall 09
Simulator Mechanics
Testbench Basics (stimulus generation)
Dataflow Verilog
Administrative Matters
Readings
Digital Simulation
0
0
0
1
0
1
1
1
1
0
0
0
1
0
1
1
Event-Driven Simulation
Simulation
1
1
1
1
0
0
0
1
0
1
1
Use a testbench
DUT
(Response)
Testbench
Stimulus
Inputs
Inputs
Outputs
DUT
(Response)
Testbench
7
Simulation Example
a[3:0]
b[3:0]
4
4
4
adder4bit
(DUT)
sum[3:0]
c_out
c_in
Use a consistent
naming convention
for your test
benches:
I usually add _tb
to the end of the
unit name
a[3:0]
b[3:0]
4
4
4
adder4bit
(DUT)
sum[3:0]
c_out
c_in
adder4bit_tb
8
Testbench Requirements
$monitor
Formatting string
%h, %H
%d, %D
%o, %O
%b, %B
%t
hex
decimal
octal
binary
time
$monitor(%t: %b %h %h %h %b\n,
$time, c_out, sum, a, b, c_in);
Output Example
module adder4bit_tb;
reg[8:0] stim;
wire[3:0] S;
wire C4;
// instantiate DUT
adder4bit(.sum(S), .c_out(C4), .a(stim[8:5]), .b(stim[4:1]), .c(stim[0]));
initial $monitor(%t A:%h B:%h ci:%b Sum:%h co:%b\n,$time,
stim[8:5],stim[4:1],stim[0],C4,S);
// stimulus generation
initial begin
stim = 9'b0000_0000_0;
#10 stim = 9'b1111_0000_1;
#10 stim = 9'b0000_1111_1;
#10 stim = 9'b1111_0001_0;
#10 stim = 9'b0001_1111_0;
#10 $stop;
end
endmodule
//
//
//
//
//
//
at
at
at
at
at
at
0 ns
10 ns
20 ns
30 ns
40 ns
50 ns stops simulation
13
Exhaustive Testing
1100 => 1101 => 1110 => 1111 => 0000 => 0001
Example: DUT
16
Example: Testbench
module Comp_4_tb();
wire
A_gt_B, A_lt_B, A_eq_B;
reg
[4:0] A, B;
// DUT
a[2:0]
b[2:0]
3
3
3
adder3bit
(DUT)
sum[2:0]
c_out
c_in
18
Combinational Testbench
module comb(output d, e, input a, b, c);
and(d, a, b);
nor(e, a, b, c);
endmodule
module comb_tb();
wire
d, e;
reg [3:0] abc;
comb CMD(.d(d),.e(e), .a(abc[2]), .b(abc[1]), .c(abc[0])); // DUT
initial $monitor(%t a: %b b: %b c: %b d: %b e: %b, $time,
abc[2], abc[1], abc[0], d, e);
initial #2000 $finish;
// end simulation, quit program
// exhaustive test of valid inputs
initial begin
for (abc = 0; abc < 8; abc = abc + 1) #5;
end // initial
endmodule
19
Generating Clocks
Wrong way:
Right way:
LESS TYPING
Easier to read, harder to make mistake
initial begin
#5 clk = 0;
#5 clk = 1;
#5 clk = 0;
(repeat hundreds of times)
end
initial clk = 0;
always @(clk) clk = #5 ~clk;
initial begin
clk = 0;
forever #5 clk = ~clk;
end
20
FSM Testing
Initially reset the counter and then test all states, but
do not test reset in each state.
22
// reset system
// first positive edge
// release reset
// traverse states
24
Force/Release In Testbenches
25
Force/Release Example
assign y = a & b;
assign z = y | c;
initial begin
a = 0; b = 0; c = 0;
#5 a = 0; b = 1; c = 0;
#5 force y = 1;
#5 b = 0;
#5 release y;
#5 $stop;
end
T
0
5
10
15
20
a
0
0
0
0
0
b
0
1
1
0
0
c
0
0
0
0
0
y
0
0
1
1
0
z
0
0
1
1
0
26
Initially reset the counter and then test all states, then
test reset in each state.
27
initial begin
clk = 0; forever #5 clk = ~clk; // What is the clock period?
end
initial begin
rst = 1; #10 rst = 0;
#90 rst = 1; #10 force GC.ns = 3'b001; #10 release GC.ns;
#10 force GC.ns = 3'b011; #10 release GC.ns;
#10 force GC.ns = 3'b010; #10 release GC.ns;
end // initial
endmodule
28
# 215
# 225
# 235
# 245
out: 101
out: 000
out: 100
out: 000
rst: 1
rst: 1
rst: 1
rst: 1
29
Dataflow Verilog
Where:
& Where:
Net_assignment ::= net_lvalue = expression
Simplest form:
// out is a net, a & b are also nets
assign out = a & b; // and gate functionality
Using vectors
wire [15:0] result, src1, src2; // 3 16-bit wide vectors
assign result = src1 ^ src2; // 16-bit wide XOR
Vector concatenation
Vector concatenation
Example 2
module add_concatenate(out, a, b, c, d);
input [7:0] a;
input [4:0] b;
input [1:0] c;
input d;
output [7:0] out;
add8bit(.sum(out), .cout(), .a(a), .b({b,c,d}), .cin());
endmodule
src2[31:0]
c_in
c_out sum[31:0]
35
Operators: Arithmetic
Operators
Applies bit-by-bit!
Reduction Operators
Reduction AND
assign all_ones = &accumulator;
Reduction OR
assign not_zero = |accuumulator;
Reduction XOR
assign parity = ^data_out;
38
39
Oh my god,
Ive created a
monster
Synopsys
40
Conditional Operator
This is a favorite!
The functionality of a 2:1 Mux
assign out = conditional_expr ? true_expr : false_expr;
Examples:
// a 2:1 mux
assign out = select ? in0 : in1;
false_expr
true_expr
// tri-state bus
assign src1 = rf2src1 ? Mem[addr1] : 16hzzzz;
0
1
out
cond_expr
41
add 3'b000
and 3'b001
xor 3'b010
shft_l 3'b011
shft_r 3'b100
Personal choice
You are welcome to use it when appropriate (I never do)
43
It acts as a latch.
data_in
en
q_out
q_out
enable
Is the synthesizer smart
enough to see this as a latch
and pick a latch element
from the standard cell
library?
data_in
enable
Or is it what you code is what you
get? A combinational feedback
loop. Still acts like a latch. Do
you feel comfortable with this?
45
Operator Precedence
46