sv copy

别等时光非礼了梦想. 提交于 2020-02-22 14:42:35

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.
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

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!