UVM Ojbect Copy/Clone

2023-11-01
UVM

Using copy method

範例大致跟uvm_object print差不多,這邊這個範例多新增一個Packet class,裡面有一個addr變數使用``uvm_field_int` macro註冊addr變數,並且Object這個class裡面有Packet的instance,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
typedef enum {FALSE, TRUE} e_bool;

class Packet extends uvm_object;
bit[31:0] addr;

`uvm_object_utils_begin(Packet)
`uvm_field_int(addr, UVM_DEFAULT)
`uvm_object_utils_end

function new(string name = "Packet");
super.new(name);
addr = $random;
endfunction
endclass

class Object extends uvm_object;
e_bool bool;
bit[3:0] bit_var;
int int_var;
string str;
Packet pkt;

`uvm_object_utils_begin(Object)
`uvm_field_enum(e_bool, bool, UVM_DEFAULT)
`uvm_field_int(bit_var, UVM_DEFAULT)
`uvm_field_int(int_var, UVM_DEFAULT)
`uvm_field_string(str, UVM_DEFAULT)
`uvm_field_object(pkt, UVM_DEFAULT)
`uvm_object_utils_end

function new(string name = "Object");
super.new(name);
str = name;
bit_var = $random;
int_var= $random;
pkt = Packet::type_id::create("pkt");
endfunction
endclass

接著在base_test裡面用產生兩個Object的instance,obj1obj2,先把obj1obj2的內容print出來之後,再使用copy function把obj1的內容copy到obj2,之後再把obj2的內容再print一次

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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);
Object obj1 = Object::type_id::create("obj1");
Object obj2 = Object::type_id::create("obj2");
obj1.print();
obj2.print();
obj2.copy(obj1);
`uvm_info("base test", "After copy", UVM_LOW)
obj2.print();
endfunction
endclass

module tb;
initial begin
$display("start");
run_test("base_test");
end
endmodule

可以看到一開始obj1和obj2的內容不同,在copy之後,obj2所有變數的值都和obj1一樣

Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# start
# UVM_INFO @ 0: reporter [RNTST] Running test base_test...
# -------------------------------------
# Name Type Size Value
# -------------------------------------
# obj1 Object - @473
# bool e_bool 32 FALSE
# bit_var integral 4 'h4
# int_var integral 32 'hc0895e81
# str string 6 Object
# pkt Packet - @474
# addr integral 32 'h8484d609
# -------------------------------------
# -------------------------------------
# Name Type Size Value
# -------------------------------------
# obj2 Object - @475
# bool e_bool 32 FALSE
# bit_var integral 4 'h3
# int_var integral 32 'h6b97b0d
# str string 6 Object
# pkt Packet - @476
# addr integral 32 'h46df998d
# -------------------------------------
# UVM_INFO .\uvm_object_copy_clone.sv(97) @ 0: uvm_test_top [base test] After copy
# -------------------------------------
# Name Type Size Value
# -------------------------------------
# obj2 Object - @475
# bool e_bool 32 FALSE
# bit_var integral 4 'h4
# int_var integral 32 'hc0895e81
# str string 6 Object
# pkt Packet - @477
# addr integral 32 'h8484d609
# -------------------------------------
#

Using do_copy

uvm copy也是可以給使用者做客製化,如果想要客製化自己的copy的話,要自己實作do_copy這個virtual function,我就沒有詳細練習了。

Using clone method

clone和copy基本上做的事情一樣,不過clone會return一個跟copy過來內容一樣的object,所以可以省去create object的步驟,例如上面的例子原本要create object2的instance,如果使用clone的話就可以省略create步驟,範例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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);
Object obj1 = Object::type_id::create("obj1");
Object obj2;
// Object obj2 = Object::type_id::create("obj2");
obj1.print();
//obj2.print();
//obj2.copy(obj1);
$cast(obj2, obj1.clone());
`uvm_info("base test", "After copy", UVM_LOW)
obj2.print();
endfunction
endclass

module tb;
initial begin
$display("start");
run_test("base_test");
end
endmodule

Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# start
# UVM_INFO @ 0: reporter [RNTST] Running test base_test...
# -------------------------------------
# Name Type Size Value
# -------------------------------------
# obj1 Object - @473
# bool e_bool 32 FALSE
# bit_var integral 4 'h4
# int_var integral 32 'hc0895e81
# str string 6 Object
# pkt Packet - @474
# addr integral 32 'h8484d609
# -------------------------------------
# UVM_INFO .\uvm_object_copy_clone.sv(59) @ 0: uvm_test_top [base test] After copy
# -------------------------------------
# Name Type Size Value
# -------------------------------------
# obj1 Object - @475
# bool e_bool 32 FALSE
# bit_var integral 4 'h4
# int_var integral 32 'hc0895e81
# str string 6 Object
# pkt Packet - @477
# addr integral 32 'h8484d609
# -------------------------------------