Gen + DRV + Dut
Gen + DRV + Dut
module add
(
input [3:0] a,b,
output reg [4:0] sum,
input clk
);
always@(posedge clk)
begin
sum <= a + b;
end
endmodule
class transaction;
randc bit [3:0] a;
randc bit [3:0] b;
bit [4:0] sum;
//we are making a copy of transaction class instead of sending original transaction data
function transaction copy(); // creating same handler for class transaction and
function transaction cpy. Since it returns some
value, we didnt use any void
copy = new();
copy.a = this.a; //copying data members from class transaction to the
transaction copy() function.(deepcopy)
copy.b = this.b;
copy.sum = this.sum;
endfunction
endclass
/*GENERATOR CLASS -- Generator takes inputs (a,b) from transaction via mailbox
and sends inputs (a,b) to DRIVER */
class generator;
transaction trans; // trans is object which will store memory(packet) of
transaction data
mailbox #(transaction) mbx; // now putting the data of trasaction class in the
mailbox
event done; // indicating that certain stimulus operation is done
and will quit the loop
task run();
for(int i=0; i<20; i++) begin
assert(trans.randomize()) else $display(" randomization failed");
class driver;
virtual add_if aif; // to connect driver to the interface we use -- "virtual" keyword
mailbox #(transaction) mbx;
transaction storage; // To store the transaction data which we receieve from that
class -- another handler of transaction class
event next;
// This allows the driver class to interact with the same mailbox that is used by other
parts of the design (like generator), enabling communication between them
task run();
forever begin // since we get values ocntinuosly
mbx.get(storage); // the data will be getting through mailbox from generator
@(posedge aif.clk);
aif.a <= storage.a; // the data in the storage is being sen to DUT (interface)
aif.b <= storage.b;
$display("[DRV]: Interface Trigger");
storage.display();
end
endtask
endclass
// TESTBENCH TOP
module tb;
add_if aif(); // connecting Testbench top to
generator gen;
driver drv;
mailbox #(transaction) mbx;
event done;
initial begin
aif.clk <= 0;
end
always #10aif.clk <= ~aif.clk;
initial begin
mbx = new(); // The mailbox will hold transaction objects (like a postbox for
storing letters or packages). holds the data
gen = new(mbx); // The mbx (mailbox) is passed to the generator, so the generator
knows where to put the transactions it generates.
drv = new(mbx);
drv.aif = aif;
done = gen.done; // To connect this done to the above which is delared in
generator
end
initial begin
fork
gen.run();
drv.run();
join
wait(done.triggered);
$finish(); // exits the simulation
end
endmodule