Professional Documents
Culture Documents
Testbench 2014
Testbench 2014
TESTBENCH
TESTBENCH
TECHBENCH
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
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
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
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
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
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
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;
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);
10