Question insv- class copy method

Status
Not open for further replies.

otis

Member level 3
Joined
Sep 21, 2010
Messages
60
Helped
4
Reputation
8
Reaction score
3
Trophy points
1,288
Visit site
Activity points
1,711
Hi,


I have a question in the copy method. I hope this is the correct forum

Here is the code


Code:
package test_pkg; 

  typedef enum bit {rd, wr} typ_dir; 
  typedef logic [15:0] typ_data; 
  typedef logic [15:0] typ_addr; 
  
  typedef class stream; 

class packet; 
   static int next_ID; 
   const int ID; 

  rand typ_data data; 
   rand typ_addr addr; 
   rand typ_dir dir; 

  function new(); 
    ID = next_ID++; 
  endfunction 

  function packet copy(); 
    packet clone; 
     clone.dir  = dir; 
     clone.addr = addr; 
     clone.data = data; 
     return clone; 
  endfunction 
  
  function bit compare(packet other); 
    if (other.dir !== dir) return 0; 
    if (other.addr !== addr) return 0; 
    if (other.data !== data) return 0; 
   return 1; 
  endfunction 
  
  function string psprint(); 
    string kind; 
    kind = (dir==rd) ? "Read" : "Write";; 
    $sformat (psprint, "%s packet %0d:  a=%h, D=%h...id=%h",kind,data,addr,dir,ID ); 
  endfunction 
    
endclass 

class stream; 
  string name; 
   packet history [$]; 

  function packet get_trans(bit print = 1'b0); 
    packet t; 
     t = new(); 
     void'(t.randomize()); 
     history.push_back(t); 
    if (print) 
      $display ("Stream %s generated %s\n", name, t.psprint()); 
     return t; 
   endfunction : get_trans 
    
  function new(string name = "default"); 
    this.name = name; 
  endfunction 
    
  endclass 

endpackage 

module top; 

  import test_pkg::*; 

  stream one = new (); 

  packet t, t1, t2; 

  initial begin 

    
  repeat (10) 
   begin 
      t = one.get_trans(1); 
      #1000; 
      $display ("%t.....",$time); 
   end 
    for (int i=0; i<one.history.size(); i++) 
      $display( "%d: %s", i, one.history[i].psprint() ); 

    t = one.get_trans(); 
    t1 = t.one.copy();          
    t2 = one.get_trans(); 
  end 

endmodule


I am getting error (questasim) at t1 = t.one.copy(); .
** FatalSIGSEGV) Bad handle or reference
#Fatal error in Function test_pkg/packet::copy at .......

What is wrong with the copy class.

Thanks a lot!
 

Your copy method declares a variable called clone, but it is never constructed. You really should separate the construction and copying into to separate methods.
You should have a virtual method called clone() that does the construction and then calls a non-virtual copy method. Now your code calls clone() instead of copy().

Code:
virtual function packet clone();
  clone = new();
  clone.copy(this);
endfunction
function void copy(packet rhs);
     this.dir  = rhs.dir; 
     this.addr = rhs.addr; 
     this.data = rhs.data; 
endfunction
It is better to do it this way if you ever plan to extend packet into another class.
Code:
class packet2 extends packet;
int id;
virtual function packet2 clone();
  clone = new();
  clone.copy(this);
endfunction
function void copy(packet rhs);
     super.copy(rhs); // copies everything in the base class
     this.id= rhs.id; // copy the extended properties. 
endfunction 
endclass
 
Reactions: otis

    otis

    Points: 2
    Helpful Answer Positive Rating
Hi Dave,

Thanks for your reply. It helped me.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…