You are on page 1of 10

9/25/2015

TESTBENCH

TESTBENCH

TECHBENCH

DUT Inputs Outputs


DUT
Stimulus Monitor
DUT
Design/Device Under Test

1
9/25/2015

TESTBENCH
1. Thiết lập tập mô phỏng (vector) cho DUT.
2. Có thể quan sát hay phân tích ngõ ra của DUT.
3. Phải kết nối ngõ vào/ngõ ra của DUT với
testbench.

Ví dụ `timescale 1ns/1ps
module counter_tb ();
reg clear, clock;
module counter (clear, clock, state); wire [3:0] state;
input clear; counter test (.clear(clear), .clock(clock), .state(state));
input clock; initial begin
output [3:0] state; clock = 1'b0;
reg [3:0]state; clear = 1'b1;
end
wire [3:0]next_state; always #5 clock = ~clock;
initial begin
assign next_state = state + 1'b1; #10 clear = 1'b0; //disable clear
#100 clear = 1'b1; // clear when state = 10
always @ (posedge clock) begin #10 clear = 1'b0; //disable clear
if (clear) begin #1000 $finish;
state <= 4'b0; end
end // create dump file
else begin initial begin
state <= next_state; $vcdplusfile ("lab1_wave.vpd");
end $vcdpluson ;
end end
endmodule endmodule

2
9/25/2015

Cấu trúc của TestBench sử dụng verilog


1. Định nghĩa các tham số (Parameter)
2. Xác định timescale
3. Các trạng thái (Statement)
5. Input reg của DUT
6. Output wire của DUT
8. Điều kiện khởi đầu
9. Các vector test
10. Debug output
11. Sử dụng bộ nhớ trong testbench
12. Các sự kiện trong verilog

1) Định nghĩa các tham số


Định nghĩa tham số giúp dễ dàng kiểm soát và hiểu
trong thiết kế testbench hay cho phép thay đổi
testbench nhanh chơn. Một số tham số cần định
nghĩa:
• Clock period
• finish time
• control words
• data widths
• Delay

3
9/25/2015

Ví dụ `timescale 1ns/1ps
module counter_tb ();
reg clear, clock;
module counter (clear, clock, state); wire [3:0] state;
input clear; counter test (.clear(clear), .clock(clock), .state(state));
input clock; initial begin
output [3:0] state; clock = 1'b0;
reg [3:0]state; clear = 1'b1;
end
wire [3:0]next_state; always #5 clock = ~clock;
initial begin
assign next_state = state + 1'b1; #10 clear = 1'b0; //disable clear
#100 clear = 1'b1; // clear when state = 10
always @ (posedge clock) begin #10 clear = 1'b0; //disable clear
if (clear) begin #1000 $finish;
state <= 4'b0; end
end // create dump file
else begin initial begin
state <= next_state; $vcdplusfile ("lab1_wave.vpd");
end $vcdpluson ;
end end
endmodule endmodule

3) Xác định timescale

4
9/25/2015

Ví dụ `timescale 1ns/1ps
module counter_tb ();
reg clear, clock;
module counter (clear, clock, state); wire [3:0] state;
input clear; counter test (.clear(clear), .clock(clock), .state(state));
input clock; initial begin
output [3:0] state; clock = 1'b0;
reg [3:0]state; clear = 1'b1;
end
wire [3:0]next_state; always #5 clock = ~clock;
initial begin
assign next_state = state + 1'b1; #10 clear = 1'b0; //disable clear
#100 clear = 1'b1; // clear when state = 10
always @ (posedge clock) begin #10 clear = 1'b0; //disable clear
if (clear) begin #1000 $finish;
state <= 4'b0; end
end // create dump file
else begin initial begin
state <= next_state; $vcdplusfile ("lab1_wave.vpd");
end $vcdpluson ;
end end
endmodule endmodule

5) Input reg của DUT


Để gán dữ liệu cho ngõ vào của DUT nên
testbench phải sử dụng các thanh ghi (reg), gọi là
input reg

5
9/25/2015

Ví dụ `timescale 1ns/1ps
module counter_tb ();
reg clear, clock;
module counter (clear, clock, state); wire [3:0] state;
input clear; counter test (.clear(clear), .clock(clock), .state(state));
input clock; initial begin
output [3:0] state; clock = 1'b0;
reg [3:0]state; clear = 1'b1;
end
wire [3:0]next_state; always #5 clock = ~clock;
initial begin
assign next_state = state + 1'b1; #10 clear = 1'b0; //disable clear
#100 clear = 1'b1; // clear when state = 10
always @ (posedge clock) begin #10 clear = 1'b0; //disable clear
if (clear) begin #1000 $finish;
state <= 4'b0; end
end // create dump file
else begin initial begin
state <= next_state; $vcdplusfile ("lab1_wave.vpd");
end $vcdpluson ;
end end
endmodule endmodule

6) Output wire của DUT


Do ngõ ra chỉ cần quan sát nên testbench chỉ cần
sử dụng “wire” ở ngõ ra.

6
9/25/2015

Ví dụ `timescale 1ns/1ps
module counter_tb ();
reg clear, clock;
module counter (clear, clock, state); wire [3:0] state;
input clear; counter test (.clear(clear), .clock(clock), .state(state));
input clock; initial begin
output [3:0] state; clock = 1'b0;
reg [3:0]state; clear = 1'b1;
end
wire [3:0]next_state; always #5 clock = ~clock;
initial begin
assign next_state = state + 1'b1; #10 clear = 1'b0; //disable clear
#100 clear = 1'b1; // clear when state = 10
always @ (posedge clock) begin #10 clear = 1'b0; //disable clear
if (clear) begin #1000 $finish;
state <= 4'b0; end
end // create dump file
else begin initial begin
state <= next_state; $vcdplusfile ("lab1_wave.vpd");
end $vcdpluson ;
end end
endmodule endmodule

8) Điều kiện khởi đầu


Cần khởi tạo các điều kiện khởi đầu như giá trị
các thanh ghi, clock…
Chú ý: nên có FINISHTIME $finish vào khối
initial.

7
9/25/2015

Ví dụ `timescale 1ns/1ps
module counter_tb ();
reg clear, clock;
module counter (clear, clock, state); wire [3:0] state;
input clear; counter test (.clear(clear), .clock(clock), .state(state));
input clock; initial begin
output [3:0] state; clock = 1'b0;
reg [3:0]state; clear = 1'b1;
end
wire [3:0]next_state; always #5 clock = ~clock;
initial begin
assign next_state = state + 1'b1; #10 clear = 1'b0; //disable clear
#100 clear = 1'b1; // clear when state = 10
always @ (posedge clock) begin #10 clear = 1'b0; //disable clear
if (clear) begin #1000 $finish;
state <= 4'b0; end
end // create dump file
else begin initial begin
state <= next_state; $vcdplusfile ("lab1_wave.vpd");
end $vcdpluson ;
end end
endmodule endmodule

9) Các vector test

Phân chia thời gian cho cho các vector mô


phỏng ở ngõ vào và sử dụng các thanh ghi để
giữ các giá trị này

8
9/25/2015

Ví dụ `timescale 1ns/1ps
module counter_tb ();
reg clear, clock;
module counter (clear, clock, state); wire [3:0] state;
input clear; counter test (.clear(clear), .clock(clock), .state(state));
input clock; initial begin
output [3:0] state; clock = 1'b0;
reg [3:0]state; clear = 1'b1;
end
wire [3:0]next_state; always #5 clock = ~clock;
initial begin
assign next_state = state + 1'b1; #10 clear = 1'b0; //disable clear
#100 clear = 1'b1; // clear when state = 10
always @ (posedge clock) begin #10 clear = 1'b0; //disable clear
if (clear) begin #1000 $finish;
state <= 4'b0; end
end // create dump file
else begin initial begin
state <= next_state; $vcdplusfile ("lab1_wave.vpd");
end $vcdpluson ;
end end
endmodule endmodule

Ví dụ
module graycounter (out, resetn, clock, enable);
input resetn, enable, clock;
output [1:0] out;
reg [1:0] out;

always @ (posedge clock or negedge resetn)


begin
if (~resetn) begin out = 2'b0; end
else begin
if (enable) begin
case (out)
2'b00: begin out = 2'b01; end
2'b01: begin out = 2'b11; end
2'b11: begin out = 2'b10; end
default: begin out = 2'b00; end
endcase
end //if (enable)
end //else if (~resetn)
end //always
endmodule

9
9/25/2015

`timescale 1ns/1ps
module tb_graycounter ();
reg resetn; reg enable; reg clock; wire [1:0] out;
parameter t = 1; // 1/2 cycle clock
parameter t1 = 2 * t; // 1 cycle clock
parameter t2 = 4 * t; // 2 cycle clock
Ví dụ
parameter ts = 1.5 * t; // time start
parameter t3 = 10 * t;
parameter t4 = 3 * t3 + 1;
graycounter U1 (.out(out), .resetn(resetn), .clock(clock), .enable(enable));
initial begin
clock = 1'b0; enable = 1'b0; resetn = 1'b1;
end
always #t clock = ~clock;
initial begin
#ts;
#t resetn = 1'b0; //reset
#t resetn = 1'b1; //non reset
#t2 enable = 1'b1;
#300 $finish;
end
// check simulation result
initial begin
#t3 if (out == 2'b01) begin $display ("time = %1t ---- check counter out value ---- TRUE", $time); end
else begin $display ("time = %1t -- out = %d -- check counter out value ---- FALSE", $time, out); end
#t3 if (out == 2'b11) begin $display ("time = %1t ---- check counter out value ---- TRUE", $time); end
else begin $display ("time = %1t -- out = %d -- check counter out value ---- FALSE", $time, out); end
#t3 if (out == 2'b10) begin $display ("time = %1t ---- check counter out value ---- TRUE", $time); end
else begin $display ("time = %1t -- out = %d -- check counter out value ---- FALSE", $time, out); end
#t3 if (out == 2'b00) begin $display ("time = %1t ---- check counter out value ---- TRUE", $time); end
else begin $display ("time = %1t -- out = %d -- check counter out value ---- FALSE", $time, out); end
end
initial begin
$vcdplusfile ("/users/andong/Desktop/ASIC_LECTURES/lab/primtime_lab/solution/lab_1/cpusv.vpd");
$vcdpluson;
end
endmodule

Ví dụ
module dlatch_reset ( data , en , reset , q);

input data, en, reset ;


output q
reg q;

always @ ( en or reset or data)


if (~reset) begin
q <= 1'b0;
end
else if (en) begin
q <= data;
end
endmodule

10

You might also like