首页 > 其他分享 >SystemVerilog中动态数组调整大小

SystemVerilog中动态数组调整大小

时间:2022-09-07 21:58:22浏览次数:73  
标签:abc array1 oarray carray 数组 动态 display SystemVerilog size

题目:现有一个大小为100的动态数组如下,如何把它定义成大小为200,并且前100个元素仍然保留的数组,请写出具体代码

integer data;

data = new[100];

 

其实就一句代码:

data = new [ data.size( ) + 100 ] (data) ;

 

总结:Dynamic Arrays

动态数组是一个非组合数组,其大小可以在运行时设置或更改。需要使用“new”操作符来实例化一个动态数组。在实例化时,设置动态数组的大小。默认大小由“new”构造函数或数组赋值设置。

动态数组维度在数组声明中由 [ ] 表示。语法是:data_type array_name [ ];

看一个例子:

module darray;
         integer da [ ]; //dynamic array ‘da’ of type integer
       initial
         begin
           da = new[4]; //construct and allocate a size of 4 elements
           $display($stime,,, "da size = %0d",da.size);
           da.delete( ); //delete elements of an array
           $display($stime,,, "da size = %0d",da.size);
           da = '{1,2,3,4}; //array initialization
           $display($stime,,, "da = ",da);
         end
endmodule

仿真结果:

Compiler version S-2021.09; Runtime version S-2021.09; Sep 7 08:02 2022
0 da size = 4
0 da size = 0
0 da = '{1, 2, 3, 4}
V C S S i m u l a t i o n R e p o r t

动态数组 – 调整大小

看一个示例,展示如何添加元素和调整动态数组的大小:

module darray;
         bit [7:0] d_array1[ ];
         initial begin
           //memory allocation
           d_array1 = new[2];
           $display($stime,,, "d_array1 size = %0d",d_array1.size);
           $display("\n");
           //array assignment - changes the size of the array
           d_array1 = {2,3,4};  //add 1 more element to the array
           $display($stime,,, "d_array1 size = %0d",d_array1.size);
           $display($stime,,, "d_array1=",d_array1);
           $display("\n");
           d_array1[2]=5;
           $display($stime,,, "d_array1[0]=",d_array1[0]);
           $display($stime,,, "d_array1[1]=",d_array1[1]);
           $display($stime,,, "d_array1[2]=",d_array1[2]);
           $display($stime,,, "d_array1=",d_array1);
           $display("\n");
           d_array1[3]=6; //will not change the size of the array or
                          //add a new element - Warning
           $display($stime,,, "d_array1 size = %0d",d_array1.size);
           $display($stime,,, "d_array1=",d_array1);
           $display("\n");
           d_array1 = {2,3,4,6};
           $display($stime,,, "d_array1 size = %0d",d_array1.size);
           $display($stime,,, "d_array1=",d_array1);
           $display("\n");
           //increase the size of d_array1
           d_array1 = new [d_array1.size( ) + 1] (d_array1) ;
           $display($stime,,, "d_array1 size = %0d",d_array1.size);
           $display($stime,,, "d_array1=",d_array1);
         end
       endmodule

仿真结果:

Compiler version S-2021.09; Runtime version S-2021.09; Sep 7 08:06 2022
0 d_array1 size = 2


0 d_array1 size = 3
0 d_array1='{'h2, 'h3, 'h4}


0 d_array1[0]= 2
0 d_array1[1]= 3
0 d_array1[2]= 5
0 d_array1='{'h2, 'h3, 'h5}


0 d_array1 size = 3
0 d_array1='{'h2, 'h3, 'h5}


0 d_array1 size = 4
0 d_array1='{'h2, 'h3, 'h4, 'h6}


0 d_array1 size = 5
0 d_array1='{'h2, 'h3, 'h4, 'h6, 'h0}
V C S S i m u l a t i o n R e p o r t

 

动态数组的复制

可以将一个动态数组复制到另一个动态数组。这允许灵活地重用一个数组来创建另一个数组。一个例子:

module darray; //copying of arrays
         int oarray [ ];
         int carray [ ];
         initial begin
           // Allocate 5 memory locations to "oarray" and
           // initialize with values
           oarray = new [5];
           oarray = '{1, 2, 3, 4, 5};
           carray = oarray;  // copy "oarray" to "carray"
           $display ("carray = %p", carray);
           // Grow size by 1 and copy existing elements to the "carray"
           carray = new [carray.size( ) + 1] (carray);
           $display("carray size = %0d",carray.size);
           // Assign value 6 to the newly added location [index 5]
           carray [carray.size( ) - 1] = 6;
           $display("carray[5]=%0d",carray[5]);
           // Display contents of new "carray"
           $display ("carray = %p", carray);
           oarray = carray; //copy carray to oarray
           $display ("oarray = %p", oarray);
           // Display size of both arrays
           $display ("oarray.size( ) = %0d, carray.size( ) = %0d", oarray.size( ), carray.size( ));
         end
       endmodule

仿真结果:

Compiler version S-2021.09; Runtime version S-2021.09; Sep 7 08:38 2022
carray = '{1, 2, 3, 4, 5}
carray size = 6
carray[5]=6
carray = '{1, 2, 3, 4, 5, 6}
oarray = '{1, 2, 3, 4, 5, 6}
oarray.size( ) = 6, carray.size( ) = 6
V C S S i m u l a t i o n R e p o r t

我们声明了两个动态数组,即“oarray”和“carray”。我们初始化“oarray”,然后将其复制到“carray”中。然后我们增加“carray”的大小并将其复制回“oarray”。

注意:SV中 直接使用{,,,} 也可以对动态数组赋值,例如  oarray = '{1, 2, 3, 4, 5}; 或 oarray = {1, 2, 3, 4, 5};都可以。

多维动态数组

直接看个例子:

module darray;
        int abc[ ][ ];  //array of arrays
        initial begin
          abc = new[3]; //sub array still not created
          $display("abc = ",abc);
          //Create sub-arrays
          foreach (abc[i]) begin
            abc[i] = new[4];
            $display("abc[%0d] = %p", i, abc[i]);
          end
          $display("abc = ",abc);
          //assign values to array and sub-array
          foreach(abc[i , j]) begin
            abc[i][j] = (j+1)+i;
          end
          //display
          foreach (abc[i , j]) begin
            $display("abc[%0d][%0d] = %0d", i, j, abc[i][j]);
          end
          $display("abc = ",abc);
        end
endmodule

仿真结果:

Compiler version S-2021.09; Runtime version S-2021.09; Sep 7 09:38 2022
abc = '{'{}, '{}, '{}}
abc[0] = '{0, 0, 0, 0}
abc[1] = '{0, 0, 0, 0}
abc[2] = '{0, 0, 0, 0}
abc = '{'{0, 0, 0, 0} , '{0, 0, 0, 0} , '{0, 0, 0, 0} }
abc[0][0] = 1
abc[0][1] = 2
abc[0][2] = 3
abc[0][3] = 4
abc[1][0] = 2
abc[1][1] = 3
abc[1][2] = 4
abc[1][3] = 5
abc[2][0] = 3
abc[2][1] = 4
abc[2][2] = 5
abc[2][3] = 6
abc = '{'{1, 2, 3, 4} , '{2, 3, 4, 5} , '{3, 4, 5, 6} }
V C S S i m u l a t i o n R e p o r t

 

标签:abc,array1,oarray,carray,数组,动态,display,SystemVerilog,size
From: https://www.cnblogs.com/fuqiangblog/p/16666890.html

相关文章