Title

SystemVerilog -- 2.8 Data Types ~ SystemVerilog Dynamic Arrays

SystemVerilog Dynamic Arrays

Dynamic Arrays是一个unpacked Arrays,其大小可以在运行时设置或更改。因此与静态数组完全不同,静态数组的大小是在数组声明期间预先确定的。Dynamic Arrays的默认大小为零,直到由构造函数设置。new()

Syntax

Dynamic Array的尺寸由空方括号指定。[]

[data_type] [identifier_name] [];

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

该函数用于数组分配大小,并在需要时初始化其元素。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", array[i]);
  end
enmodule

模拟日志

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 Mathods

Function Description
function int size (); Returns the current size of the array, 0 if array has not been created
function void delete (); Empties the array resulting in a zero-sized array
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

模拟日志

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

How to add new items to a dynamic array ?

很多时候,我们可能需要在不丢失其原始内容的情况下向现有的动态数组添加新元素。由于运算符用于为数组分配特定大小,因此我们还必须在创建后将旧数组内容复制到新数组中。new()

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 contentts of "id"
    $display ("id = %p", id);

    // Grow size by 1 and copy sxisting 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 contentts 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

模拟日志

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.

posted on 2024-05-02 20:02  松—松  阅读(19)  评论(0编辑  收藏  举报

导航