SystemVerilog中除了数组、队列和关联数组等数据结构,这些数据结构还可以嵌套。
module top;
typedef int Qint[$];
// dynamic array of queues
Qint DynamicQ[ ]; // same as int DynamicQ[ ][$];
// queue of queues
Qint QueueQ[$]; // same as int QueueQ[$][$];
// associative array of queues
Qint AssociativeQ[string]; // same as
//int AssociativeQ[string][$];
initial begin
// Dynamic array of 2 queues
DynamicQ = new[2]; //Create dynamic array of size 2
(queues)
// initialize queue 0 with three entries
DynamicQ[0] = {1,2,3};
// Push onto queue 1
DynamicQ[1].push_back(1);
$display("DynamicQ = %p", DynamicQ);
//push/initialize queue of 3 queues
QueueQ[0].push_front(7);
QueueQ[1].push_back(6);
QueueQ[2].push_back(1);
$display("QueueQ = %p", QueueQ);
// Associative array of queues
AssociativeQ["one"].push_back(5);
AssociativeQ["two"] = {5,6,7,8};
$display("AssociativeQ = %p", AssociativeQ);
end
endmodule : top
仿真log:
DynamicQ = '{'{1, 2, 3} , '{1} }
QueueQ = '{'{7} , '{6} , '{1} }
AssociativeQ = '{"one":'{5} , "two":'{5, 6, 7, 8} }
在上面的例子中,我们定义了三种不同类型的数据结构。队列动态数组、队列队列和队列关联数组:
// dynamic array of queues Qint DynamicQ[ ]; // same as int DynamicQ[ ][$]; // queue of queues Qint QueueQ[$]; // same as int QueueQ[$][$]; // associative array of queues Qint AssociativeQ[string]; // same as //int AssociativeQ[string][$];
初始化这个队列动态数组大小为2,然后分别初始化这两个数组:
DynamicQ = new[2]; //Dynamic Array size of 2.
DynamicQ[0] = {1,2,3};
DynamicQ[1].push_back(1);
初始化队列队列
QueueQ[0].push_front(7); QueueQ[1].push_back(6); QueueQ[2].push_back(1);
初始化队列关联数组
//Queue at associative index/key "one"
AssociativeQ["one"].push_front(5);
//Queue at associative index/key "two"
AssociativeQ["two"] = {5,6,7,8};
审核编辑:刘清
全部0条评论
快来发表一下你的评论吧 !