Can somebody post a simple code of cloning an object and storing in fifo/queue?

I have seen in many threads that data is getting overwritten because object is not cloned before storing in fifo/queue. I really would like to see a sample code it.

In reply to rag123:

I have seen in many threads that data is getting overwritten because object is not cloned before storing in fifo/queue. I really would like to see a sample code it.

Hi,

You can find the below snippet of code. The reason for which the data is being overwritten could be because, the object that is being cloned might not have been created for multiple times. You might be sampling the data and pushing it into the same object every time.


// Example compiled by Putta Satish
`include "uvm_macros.svh"
import uvm_pkg::*;
 
class my_trans extends uvm_sequence_item;
 
  rand int unsigned addr;
  rand int unsigned data;

  constraint valid_range {addr inside {[1:200]}; data inside {[1:100]};}
 
  `uvm_object_utils(my_trans)	
 
  function new(string name = "my_trans");
    super.new(name);
  endfunction

  function void do_copy(uvm_object rhs);
    my_trans rhs_;
 
    if(!$cast(rhs_, rhs)) begin
      uvm_report_error("do_copy:", "Cast failed");
      return;
    end
    super.do_copy(rhs); // Chain the copy with parent classes
    addr = rhs_.addr;
    data = rhs_.data;
  endfunction: do_copy
  
  function string convert2string();
    string s;
 
    s = super.convert2string();
    // Note the use of \t (tab) and \n (newline) to format the data in columns
    $sformat(s, "%s\n addr \t%0h\n data \t%0h\n",
      s, addr, data);
    return s;
  endfunction: convert2string

  function void do_print(uvm_printer printer);
    printer.m_string = convert2string();   
  endfunction: do_print
endclass

module top;
 
  my_trans trans_h;
  my_trans trans_clone_h;
  my_trans trans_Q[$];	

  initial
    begin
      trans_h = my_trans::type_id::create("trans_h");
      repeat (10) begin
        if (!trans_h.randomize())
          `uvm_fatal("TOP", "Unable to randomize my_trans");

        `uvm_info("TOP", trans_h.sprint(), UVM_NONE);
 
        if(!$cast(trans_clone_h, trans_h.clone()))
          `uvm_fatal("TOP", "Cloning failed");
        trans_Q.push_back(trans_clone_h);
      end
      
      `uvm_info("TOP","\n Pushing is done into the Queue \n", UVM_NONE)

      foreach(trans_Q[i])
        begin
          `uvm_info("TOP", trans_Q[i].sprint(), UVM_NONE);
        end
    end
endmodule

Putta Satish

Thanks Satish !!