SystemVerilog TestBench Example - ADDER - Verification Guide
SystemVerilog TestBench Example - ADDER - Verification Guide
Verification Guide
Table of Contents
1. ‘ADDER’ TestBench
We use cookies to ensureWithout Monitor,
that we give Agent
you the best and Scoreboard
experience on our website. If you continue to use this site we will assume that you are happy with it.
1.1. TestBench Architecture
Ok
1.2. Transaction Class
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 1/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
TestBench Architecture
Ok
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 2/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
Transaction Class
Fields required to generate the stimulus are declared in the transaction class.
Transaction class can also be used as a placeholder for the activity monitored by the monitor on DUT signals.
So, the first step is to declare the ‘Fields‘ in the transaction class.
Below are the steps to write the transaction class.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
class transaction; Ok
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 3/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
endclass
class transaction;
endclass
Ok
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 4/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
class transaction;
Generator Class
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
class generator;
Ok
------
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 5/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
endclass
class generator;
endclass
class generator;
//main task, generates(create and randomizes) the packets and puts into mailbox
task main();
trans = new();
if( !trans.randomize() ) $fatal("Gen:: trans randomization failed");
gen2driv.put(trans);
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
endtask
Ok
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 6/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
endclass
This involves,
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
Ok
Declaring the Mailbox and Event
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 7/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
Getting the Mailbox handle from the env class ( because the same mailbox will be shared across generator and
driver).
class generator;
//declaring mailbox
mailbox gen2driv;
//constructor
function new(mailbox gen2driv);
//getting the mailbox handle from env
this.gen2driv = gen2driv;
endfunction
//main task, generates(create and randomizes) the packets and puts into mailbox
task main();
trans = new();
if( !trans.randomize() ) $fatal("Gen:: trans randomization failed");
gen2driv.put(trans);
-> ended; //triggering indicatesthe end of generation
endtask
endclass
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
Ok
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 8/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
class generator;
//declaring mailbox
mailbox gen2driv;
//constructor
function new(mailbox gen2driv);
//getting the mailbox handle from env
this.gen2driv = gen2driv;
endfunction
//main task, generates(create and randomizes) the repeat_count number of transaction packets and puts into
mailbox
task main();
repeat(repeat_count) begin
trans = new();
if( !trans.randomize() ) $fatal("Gen:: trans randomization failed");
gen2driv.put(trans);
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
end
-> ended; //triggering indicatesthe end of generation Ok
endtask
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 9/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
endclass
5. Adding an event to indicate the completion of the generation process, the event will be triggered on the completion
of the Generation process.
class generator;
//declaring mailbox
mailbox gen2driv;
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
//constructor
function new(mailbox gen2driv);
//getting the mailbox handle from env
this.gen2driv = gen2driv;
endfunction
//main task, generates(create and randomizes) the repeat_count number of transaction packets and puts into
mailbox
task main();
repeat(repeat_count) begin
trans = new();
if( !trans.randomize() ) $fatal("Gen:: trans randomization failed");
gen2driv.put(trans);
end
-> ended; //triggering indicatesthe end of generation
endtask
endclass
Interface
Ok
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 11/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
endinterface
Driver Class
receive the stimulus generated from the generator and drive to DUT by assigning transaction class values to
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
interface signals.
Ok
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 12/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
class driver;
----
endclass
1. Declare interface and mailbox, Get the interface and mailbox handle through a constructor.
//constructor
function new(virtual intf vif,mailbox gen2driv);
//getting the interface
this.vif = vif;
//getting the mailbox handle from environment
this.gen2driv = gen2driv;
endfunction
2. Adding a reset task, which initializes the Interface signals to default values.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
Ok
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 13/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
3. Adding a drive task to drive the transaction packet to the interface signal.
We//drive the
use cookies transaction
to ensure items
that we give to best
you the interface
experiencesignals
on our website. If you continue to use this site we will assume that you are happy with it.
task drive;
Ok
transaction trans;
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 14/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
gen2driv.get(trans);
@(posedge vif.clk);
vif.valid <= 1;
vif.a <= trans.a;
vif.b <= trans.b;
@(posedge vif.clk);
vif.valid <= 0;
trans.c <= vif.c;
@(posedge vif.clk);
trans.display("[ Driver ]");
no_transactions++;
end
endtask
4. Adding a local variable to track the number of packets driven, and increment the variable in the drive task.
(This
will be useful to end the test-case/Simulation. i.e compare the
generated pkt’s and driven pkt’s if both are same then end the
simulation)
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 15/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
no_transactions++;
endtask
class driver;
//used to count the number of transactions
int no_transactions;
//constructor
function new(virtual intf vif,mailbox gen2driv);
//getting the interface
this.vif = vif;
//getting the mailbox handles from environment
this.gen2driv = gen2driv;
endfunction
wait(!vif.reset);
$display("[ DRIVER ] ----- Reset Ended -----");
endtask
endclass
Environment
Ok
Creates the mailbox, generator and driver shares the mailbox handle across the Generator and Driver.
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 17/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
class environment;
---
endclass
//mailbox handle's
mailbox gen2driv;
We//virtual interface
use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
virtual intf vif;
Ok
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 18/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
Mailbox
Generator
Driver
//constructor
function new(virtual intf vif);
//get the interface from test
this.vif = vif;
//creating the mailbox (Same handle will be shared across generator and driver)
gen2driv = new();
3. For
Webetter accessibility.
use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
Generator and Driver activity can be divided and controlled in three methods.
Ok
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 19/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
task pre_test();
driv.reset();
endtask
task test();
fork
gen.main();
driv.main();
join_any
endtask
task post_test();
wait(gen.ended.triggered);
wait(gen.repeat_count == driv.no_transactions);
endtask
task run;
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
pre_test();
test();
Ok
post_test();
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 20/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
$finish;
endtask
`include "transaction.sv"
`include "generator.sv"
`include "driver.sv"
class environment;
//mailbox handle's
mailbox gen2driv;
//virtual interface
virtual intf vif;
//constructor
function new(virtual intf vif);
//get the interface from test
this.vif = vif;
//creating the mailbox (Same handle will be shared across generator and driver)
gen2driv = new();
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
//creating generator and driver
Ok
gen = new(gen2driv);
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 21/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
driv = new(vif,gen2driv);
endfunction
//
task pre_test();
driv.reset();
endtask
task test();
fork
gen.main();
driv.main();
join_any
endtask
task post_test();
wait(gen.ended.triggered);
wait(gen.repeat_count == driv.no_transactions);
endtask
//run task
task run;
pre_test();
test();
post_test();
$finish;
endtask
endclass
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
Ok
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 22/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
Test
program test;
----
endprogram
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
Ok
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 23/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
initial begin
//creating environment
env = new(intf);
end
//calling run of env, it interns calls generator and driver main tasks.
env.run();
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
4. Complete Test Code,
Ok
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 24/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
`include "environment.sv"
program test(intf intf);
initial begin
//creating environment
env = new(intf);
//calling run of env, it interns calls generator and driver main tasks.
env.run();
end
endprogram
TestBench Top
This is the topmost file, which connects the DUT and TestBench.
TestBench top consists of DUT, Test and Interface instances.
The interface connects the DUT and TestBench.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
module tbench_top;
Ok
---
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 25/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
endmodule
//clock generation
always #5 clk = ~clk;
//reset Generation
initial begin
reset = 1;
#5 reset =0;
end
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
3. Create Design Instance and Connect Interface signals,
Ok
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 26/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
initial begin
$dumpfile("dump.vcd"); $dumpvars;
end
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
6. Complete testbench top code,
Ok
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 27/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
`include "interface.sv"
`include "random_test.sv"
module tbench_top;
//clock generation
always #5 clk = ~clk;
//reset Generation
initial begin
reset = 1;
#5 reset =0;
end
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 28/29
9/21/23, 3:41 PM SystemVerilog TestBench Example - ADDER - Verification Guide
.c(i_intf.c)
);
Click to execute on
❮ Previous
report this ad
Verification Guide / Proudly powered by WordPress
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
Ok
https://verificationguide.com/systemverilog-examples/systemverilog-testbench-example-adder-2/ 29/29