sv copy
1. 使用new操作符复制一个对象
1 class Header; 2 int id; 3 function new (int id); 4 this.id = id; 5 endfunction 6 7 function showId(); 8 $display ("id=0x%0d", id); 9 endfunction 10 endclass 11 12 class Packet; 13 int addr; 14 int data; 15 Header hdr; 16 17 function new (int addr, int data, int id); 18 hdr = new (id); 19 this.addr = addr; 20 this.data = data; 21 endfunction 22 23 function display (string name); 24 $display ("[%s] addr=0x%0h data=0x%0h id=%0d", name, addr, data, hdr.id); 25 endfunction 26 endclass 27 28 module tb; 29 Packet p1, p2; 30 initial begin 31 // Create a new pkt object called p1 32 p1 = new (32'hface_cafe, 32'h1234_5678, 26); 33 p1.display ("p1"); 34 35 // Shallow copy p1 into p2; p2 is a new object with contents in p1 36 p2 = new p1; 37 p2.display ("p2"); 38 39 // Now let's change the addr and id in p1 40 p1.addr = 32'habcd_ef12; 41 p1.data = 32'h5a5a_5a5a; 42 p1.hdr.id = 17; 43 p1.display ("p1"); 44 45 // Print p2 and see that hdr.id points to the hdr in p1, while 46 // addr and data remain unchanged.Shallow copy
47 p2.display ("p2"); 48 end 49 endmodule
这是一种简易复制(shallow copy),原对象的值被盲目copy到对象中,如果类A1中包含一个指向另一个子类B的句柄,那么复制类A2中只是复制了子类B的句柄,A1和A2中的B对象指向的是同一个对象。
2. 定义copy函数,实现深拷贝
1 class Header; 2 int id; 3 function new (int id); 4 this.id = id; 5 endfunction 6 7 function showId(); 8 $display ("id=0x%0d", id); 9 endfunction 10 endclass 11 12 class Packet; 13 int addr; 14 int data; 15 Header hdr; 16 17 function new (int addr, int data, int id); 18 hdr = new (id); 19 this.addr = addr; 20 this.data = data; 21 endfunction 22 23 function copy (Packet p); 24 this.addr = p.addr; 25 this.data = p.data; 26 this.hdr.id = p.hdr.id; 27 endfunction 28 29 function display (string name); 30 $display ("[%s] addr=0x%0h data=0x%0h id=%0d", name, addr, data, hdr.id); 31 endfunction 32 endclass 33 34 module tb; 35 Packet p1, p2; 36 initial begin 37 p1 = new (32'hface_cafe, 32'h1234_5678, 32'h1a); 38 p1.display ("p1"); 39 40 p2 = new (1,2,3); // give some values 41 p2.copy (p1); 42 p2.display ("p2"); 43 44 // Now let's change the addr and id in p1 45 p1.addr = 32'habcd_ef12; 46 p1.data = 32'h5a5a_5a5a; 47 p1.hdr.id = 32'h11; 48 p1.display ("p1"); 49 50 // Now let's print p2 - you'll see the changes made to hdr id 51 // but not addr 52 p2.display ("p2"); 53 end 54 endmodule
3. copy 函数定义
1 // Code your testbench here 2 // or browse Examples 3 class base; 4 int p1; 5 virtual function void copy(base orig); 6 this.p1 = orig.p1; 7 endfunction 8 virtual function void display(); 9 $display("base:p1 = %d",this.p1); 10 endfunction 11 endclass 12 class ex_base extends base; 13 int p2; 14 virtual function void copy(base orig); 15 super.copy(orig); 16 this.p2 = p2; 17 endfunction 18 virtual function void display(); 19 $display("ex_base:p1 = %d, p2 = %d", this.p1, this.p2); 20 endfunction 21 endclass 22 23 module testbench(); 24 base b1,b2; 25 ex_base eb1, eb2; 26 initial begin 27 eb1 = new; eb2 = new(); 28 eb2.p2 = 5; 29 b1 = eb1; b2 = eb2; 30 b1.copy(b2); // p2 is not copied 31 b1.display(); 32 b2.display(); 33 eb1.copy(eb2); // p2 is copied 34 eb1.display(); 35 eb2.display(); 36 end 37 endmodule
4. do_copy
virtual function void do_copy( uvm_object rhs ); //rhs 基类句柄
jelly_bean_transaction that;//that 子类句柄
if ( ! $cast( that, rhs ) ) begin
`uvm_error( get_name(), "rhs is not a jelly_bean_transaction" )
return;
end
super.do_copy( rhs );
this.flavor = that.flavor;//前面通过$cast 进行向下类型转换,that 句柄成功的指向了rhs句柄指向的对象,然后进行赋值,达到copy的目的
this.color = that.color;
this.sugar_free = that.sugar_free;
this.sour = that.sour;
this.taste = that.taste;
endfunction: do_copy
5. cast 的使用
将一个基类句柄tr 赋值给一个扩展类句柄ex_tr 并不总是合法的,当基类句柄tr指向的确实是扩展类ex_tr 的对象时,则是允许的。$cast 子程序会检查句柄所指向的对象的类型,而不仅仅是句柄类型。
$cast(ex_tr, tr) ; 检查tr句柄指向的对象的类型并拷贝句柄。(如果成功ex_tr 就指向tr 所指向的对象)
$cast(ex_tr, tr.clone()) ; 检查tr句柄指向的对象的类型并拷贝对象本身。
本文来自博客园,作者:hematologist,转载请注明原文链接:https://www.cnblogs.com/littleMa/p/12345263.html
posted on 2020-02-22 14:14 hematologist 阅读(839) 评论(0) 编辑 收藏 举报