SystemVerilog Dynamic Array

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


—A dynamic array is an unpacked array whose size can be set or changed at run time, and hence is quite different from a static array where the size is pre-determined during declaration of the array. The default size of a dynamic array is zero until it is set by the new() constructor.

Syntax

A dynamic array dimensions are specified by the empty square brackets [ ].
动态数组维度由空方括号指定。

[data_type] [identifier_name] [];

bit [7:0] stack [];  // A dynamic array of 8-bit vectorstring 
names [];           // A dynamic array that can contain strings

The new() function is used to allocate a size for the array and initialize its elements if required.
**new()**函数用于为数组分配大小,并在需要时初始化其元素。

Dynamic Array Example

module tb;
 // Create a dynamic array that can hold elements of type int
int 	array [];
initial begin// Create a size for the dynamic array -> size here is 5
        // so that it can hold 5 values
		array = new [5];
        // Initialize the array with five values
		array = '{31, 67, 10, 4, 99};
          
        // Loop through the array and print their values
          foreach (array[i])
              $display ('array[%0d] = %0d', i, array[i]);
   end
endmodule

Simulation Log
ncsim> run
array[0] = 31
array[1] = 67
array[2] = 10
array[3] = 4
array[4] = 99
ncsim: *W,RNQUIE: Simulation is complete.

Dynamic Array Methods

函数描述
function int size();返回数组的大小,如果数组没有被创建则返回值为0
function void delete()清空数组
module tb;
	   // Create a dynamic array that can hold elements of type string
	   string 	fruits [];
	   
	   initial begin
	    // Create a size for the dynamic array -> size here is 5
	    // so that it can hold 5 values  	
	    fruits = new [3];
	
	    // Initialize the array with five values  	
	    fruits = '{'apple', 'orange', 'mango'};  	
	      
	    // Print size of the dynamic array
	    $display ('fruits.size() = %0d', fruits.size());
	    
	    // Empty the dynamic array by deleting all items
		 fruits.delete();
	    $display ('fruits.size() = %0d', fruits.size());
   end
endmodule

Simulation Log
ncsim> run
fruits.size() = 3
fruits.size() = 0
ncsim: *W,RNQUIE: Simulation is complete.

How to add new items to a dynamic array ?

Many times we may need to add new elements to an existing dynamic array without losing its original contents. Since the new() operator is used to allocate a particular size for the array, we also have to copy the old array contents into the new one after creation.

int array [];
array = new [10];

// This creates one more slot in the array, while keeping old contents
array = new [array.size() + 1] (array);

Copying dynamic array example

module tb;
    // Create two dynamic arrays of type int
   int array [];
   int id [];

   initial begin
   // Allocate 5 memory locations to 'array' and initialize with values 
   array = new [5]; 
   array = '{1, 2, 3, 4, 5};
     
   // Point 'id' to 'array'      
   id = array;

   // Display contents of 'id'
   $display ('id = %p', id);

   // Grow size by 1 and copy existing elements to the new dyn.Array 'id' 
   id = new [id.size() + 1] (id);

   // Assign value 6 to the newly added location [index 5] id 
   [id.size() - 1] = 6;

   // Display contents of new 'id'
   $display ('New id = %p', id);

   // Display size of both arrays
   $display('array.size() = %0d, id.size() = %0d', array.size(), id.size());

  end
endmodule

Simulation Log
run -all;
ncsim> run
id = '{1, 2, 3, 4, 5}
New id = '{1, 2, 3, 4, 5, 6}
array.size() = 5, id.size() = 6
ncsim: *W,RNQUIE: Simulation is complete.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值