UVM Object Pack/Unpack

2023-11-05
UVM

UVM macro有將class裡面的變數打包(pack)成bit或byte stream、解壓(unpack)bit或byte stream填入class的機制。在處理 SPI、I2C 和 RS-232等串行形式的通訊協定時很有用。 有三個主要得打包(pack)和解壓(unpack)的function。

Packing主要有三個function:
packpack_bytespack_ints,pack將此物件以位元連接(bitwise-concatenate)將變數打包到bits、bytes或ints的array裡面。

Unpacking主要有三個function:
unpackunpack_bytesunpack_ints,unpack從bits、bytes或ints的array把值取出放入object。unpack function的return值是從array中實際unapck的bit的數量。

另外也有do_pack和do_unpack可以客製化自己的pack/unpack function。

下面的例子是將object裡面的變數分別用packpack_bytespack_ints把資料打包到bits、bytes、ints這三個array中,接下來是用obj2.unpack將bits array的資料填入obj2、用obj2.unpack_bytes將bytes array的資料填入obj2、用obj2.unpack_ints將ints array的資料填入obj2。val1/val2/val3是實際上unpack的bit數量

Example

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import uvm_pkg::*;
`include "uvm_macros.svh"

class Packet extends uvm_object;
bit [3:0] addr;
bit [3:0] wdata;
bit [3:0] rdata;
bit wr;

`uvm_object_utils_begin(Packet)
`uvm_field_int(addr, UVM_DEFAULT)
`uvm_field_int(wdata, UVM_DEFAULT)
`uvm_field_int(rdata, UVM_DEFAULT)
`uvm_field_int(wr, UVM_DEFAULT)
`uvm_object_utils_end

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

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

bit bits[];
byte unsigned bytes[];
int unsigned ints[];

int val1, val2, val3;

function void build_phase(uvm_phase phase);
Packet pkt = Packet::type_id::create("pkt");
Packet pkt2 = Packet::type_id::create("pkt2");
pkt.addr = 4'hA;
pkt.wdata = 4'hB;
pkt.rdata = 4'hC;
pkt.wr = 1'b1;
pkt.print();

pkt.pack(bits);
pkt.pack_bytes(bytes);
pkt.pack_ints(ints);

`uvm_info(get_type_name(), $sformatf("bits=%p", bits), UVM_LOW)
`uvm_info(get_type_name(), $sformatf("bytes=%p", bytes), UVM_LOW)
`uvm_info(get_type_name(), $sformatf("ints=%p", ints), UVM_LOW)

val1 = pkt2.unpack(bits);
`uvm_info(get_type_name(), $sformatf("Actual number of bits unpacked from the given array. unpacked val1=0x%0h", val1), UVM_LOW)
pkt2.print();

val2 = pkt2.unpack_bytes(bytes);
`uvm_info(get_type_name(), $sformatf("Actual number of bits unpacked from the given array. unpacked val2=0x%0h", val2), UVM_LOW)
pkt2.print();

val3 = pkt2.unpack_ints(ints);
`uvm_info(get_type_name(), $sformatf("Actual number of bits unpacked from the given array. unpacked val3=0x%0h", val3), UVM_LOW)
pkt2.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# start
# UVM_INFO @ 0: reporter [RNTST] Running test base_test...
# ------------------------------
# Name Type Size Value
# ------------------------------
# pkt Packet - @473
# addr integral 4 'ha
# wdata integral 4 'hb
# rdata integral 4 'hc
# wr integral 1 'h1
# ------------------------------
# UVM_INFO .\uvm_object_pack_unpack2.sv(47) @ 0: uvm_test_top [base_test] bits='{1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1}
# UVM_INFO .\uvm_object_pack_unpack2.sv(48) @ 0: uvm_test_top [base_test] bytes='{171, 200}
# UVM_INFO .\uvm_object_pack_unpack2.sv(49) @ 0: uvm_test_top [base_test] ints='{2882011136}
# UVM_INFO .\uvm_object_pack_unpack2.sv(52) @ 0: uvm_test_top [base_test] Actual number of bits unpacked from the given array. unpacked val1=0xd
# ------------------------------
# Name Type Size Value
# ------------------------------
# pkt2 Packet - @474
# addr integral 4 'ha
# wdata integral 4 'hb
# rdata integral 4 'hc
# wr integral 1 'h1
# ------------------------------
# UVM_INFO .\uvm_object_pack_unpack2.sv(56) @ 0: uvm_test_top [base_test] Actual number of bits unpacked from the given array. unpacked val2=0xd
# ------------------------------
# Name Type Size Value
# ------------------------------
# pkt2 Packet - @474
# addr integral 4 'ha
# wdata integral 4 'hb
# rdata integral 4 'hc
# wr integral 1 'h1
# ------------------------------
# UVM_INFO .\uvm_object_pack_unpack2.sv(60) @ 0: uvm_test_top [base_test] Actual number of bits unpacked from the given array. unpacked val3=0xd
# ------------------------------
# Name Type Size Value
# ------------------------------
# pkt2 Packet - @474
# addr integral 4 'ha
# wdata integral 4 'hb
# rdata integral 4 'hc
# wr integral 1 'h1
# ------------------------------
#

因為pkt資料並沒有變,只是用不同的pack方式把資料打包放入不同的array,pkt2也是用相對應的unpack方式把資料解開放入pkt2對應的變數內,所以pkt2的print結果都是相同的,且val1/val2/val3的值都是0xD(十進位是13),就是addr(4bits)+wdata(4bits)+rdata(4bits)+wr(1bit)的總和。

Using do_pack/do_unpack

和print/copy一樣,pack/unpack一樣可以有do_pack/do_unpack客製化自己的pack/unpack,目前還沒打算練習這個。