typedef struct {
bit [4:0] A;
bit [4:0] B;
bit [4:0] C;
} test_struct;
class test;
test_struct struct_a[$]; // 动态数组,用于存储 test_struct 类型的元素
// 对 struct_a 数组进行排序,按照 struct_a 中元素的 A 字段排序
function void sort_struct_array();
struct_a.sort() with (item.A);
endfunction
endclass
解释:
-
typedef struct
定义:
test_struct
是一个 struct
,包含三个 5 位的位域字段:A
、B
和 C
。- 位宽为
bit [4:0]
,即每个字段的值范围是 0 到 31(5 位二进制数)。
-
test_struct struct_a[$];
:
struct_a
是一个队列,类型为 test_struct
,表示可以动态存储多个 test_struct
类型的结构体实例。- 动态数组
$
表示数组的大小不固定,可以根据需求动态分配或扩展。
-
struct_a.sort() with (item.A);
:
sort()
是 SystemVerilog 提供的数组排序方法,它会对动态数组 struct_a
中的元素进行排序。with (item.A)
表示按 test_struct
的 A
字段来排序数组中的元素,item
代表数组中的每个元素。- 通过这种方式,
struct_a
将按照 A
字段的值从小到大进行排序。