Uvm transaction class related

UVM Transaction class has two variable rand int a; rand int b; and both variables are depends on x and y respectively so when x =1 (is high) then a should be print and when y=1(is high) then b should be print. so how we will print and where we will print can you please give me suggestions.

If you are referring specifically to uvm_object::print(), we recommend overriding do_print() or even using convert2string() and formatting your output exactly the way you want it. UVM’s built-in printer facility can give you quick output, but eventually you’ll need to learn how do do it yourself anyway. See

@dave_59 I referred the uvm_object ::print method and I coded also but I didn’t get the output can you please help me?

`include "uvm_macros.svh"
import uvm_pkg::*;

class transaction extends uvm_object;
  rand int a;
  rand int b;
  
  bit x;
  bit y;

  `uvm_object_utils_begin(transaction)
  `uvm_field_int(a, UVM_DEFAULT)
  `uvm_field_int(b, UVM_DEFAULT)
  `uvm_object_utils_end

  virtual function void do_print(uvm_printer printer);
    super.do_print(printer);
    if(x==1) begin
    printer.print_field("a",this.a,32,UVM_DEC);
    end
    if(y==1) begin
    printer.print_field("b",this.b,32,UVM_DEC);
    end
  endfunction
  
  function new(string name = "transaction");
    super.new(name);
  endfunction
  
 
endclass

class base_test extends uvm_test;
  `uvm_component_utils(base_test)
  function new(string name = "base_test", uvm_component parent=null);
    super.new(name, parent);
  endfunction
  
  function void build_phase(uvm_phase phase);
    transaction trans = transaction::type_id::create("trans");
    trans.randomize();
    trans.print();
    `uvm_info(get_type_name(), $sformatf("Contents: \n%s", trans.sprint()), UVM_LOW)
   
  endfunction
endclass

module tb;
  initial run_test("base_test");
endmodule

OUTPUT:-

UVM_INFO @ 0: reporter [RNTST] Running test base_test...
------------------------------------
Name   Type         Size  Value     
------------------------------------
trans  transaction  -     @349      
  a    integral     32    'h1f135537
  b    integral     32    'he6195fe3
------------------------------------
UVM_INFO testbench.sv(102) @ 0: uvm_test_top [base_test] Contents: 
------------------------------------
Name   Type         Size  Value     
------------------------------------
trans  transaction  -     @349      
  a    integral     32    'h1f135537
  b    integral     32    'he6195fe3
------------------------------------

do_print append to the output of the field automation macros. If you are going to use print/sprint and the field automation macros and add the do_print(), then you need to do one of

  • remove the uvm_field_ macros, recommended
  • use convert2string instead of print/sprint.
  • change the UVM_DEFAULT to UVM_NOPRINT

@dave_59 Thanks for contributing, I got the output.

`include “uvm_macros.svh”
import uvm_pkg::*;

class transaction extends uvm_object;
rand int a;
rand int b;

bit x=1;
bit y=0;

uvm_object_utils_begin(transaction) uvm_field_int(a, UVM_NOPRINT)
uvm_field_int(b, UVM_NOPRINT) uvm_object_utils_end

virtual function void do_print(uvm_printer printer);
super.do_print(printer);
if(x==1) begin
printer.print_field(“a”,this.a,32,UVM_DEC);
end
if(y==1) begin
printer.print_field(“b”,this.b,32,UVM_DEC);
end
endfunction

function new(string name = “transaction”);
super.new(name);
endfunction

endclass

class base_test extends uvm_test;
`uvm_component_utils(base_test)
function new(string name = “base_test”, uvm_component parent=null);
super.new(name, parent);
endfunction

function void build_phase(uvm_phase phase);
transaction trans = transaction::type_id::create(“trans”);
trans.randomize();
trans.print();
`uvm_info(get_type_name(), $sformatf(“Contents: \n%s”, trans.sprint()), UVM_LOW)

endfunction
endclass

module tb;
initial run_test(“base_test”);
endmodule

Output:-

UVM_INFO @ 0: reporter [RNTST] Running test base_test…
-------------------------------------
**Name Type Size Value **
-------------------------------------
trans transaction - @349 **
** a integral 32 'd521360695

-------------------------------------
**UVM_INFO testbench.sv(43) @ 0: uvm_test_top [base_test] Contents: **
-------------------------------------
**Name Type Size Value **
-------------------------------------
trans transaction - @349 **
** a integral 32 'd521360695