1. 程式人生 > >systemverilog中的數組操作

systemverilog中的數組操作

shuff cti 排序 oid 數組 () == ted 數組操作

sv中的數組基本操作:

/*    
  Exercsise platform :     Questa Sim 10.1b
*/
class Array;
  int array[9:0] ;
  
  function new();
     for( int i = 0 ; i < 10 ; i++ )
         array[i] = i ;
      /*
          array = '{'{1,2,3},'{5{5}},default:0};
          無法使用這種基本的賦值方式,可能是編譯器的版本過低了
      */
  endfunction:new
  
   function void print();
    foreach(array[i]) begin
      $display(" array[%d] = %d ",i,array[i]);
    end
    for ( int i = 0 ; i < 10 ; i++ )begin
      $write(" ** ");
    end
    $display();
  endfunction:print
  
  function void funcs();
      int pos[$] ;
    //   縮減操作 xor , or , and ,  xnor  , sum , product 等
    $display("the sum is %d ",array.sum());
    $display("the and is %d ",array.and());
    $display("the  or is %d ",array.or() );
    $display("the xor is %d ",array.xor());
    $display("the product is %d ",array.product());
    
    //   索引操作:取得相應的所需要的數組原素位置
    $display(" max value is %d ",array.max()[0]);
    $display(" min value is %d ",array.min()[0]);
    array = array.unique();
    print();       //  在類中時,調用自己類的函數,可以直接調用。這點與cpp相同
    pos = array.find with ( item == 7 ) ; 
    //  find 有多種變體: find_first   find_last  find_first_index   find_last_index
    $display(" pos : %d ? value: %d ",pos[0],array[pos[0]]);
    
    //  排序操作: sort ,  rsort  , reverse , shuffle , 
    // 其中 sort ,rsort  可以與with相結合,根據with後的條件排序
    array.shuffle();
    $display("shuffled:");
    print();
    array.sort();
    $display("Sorted:");
    print();
    array.rsort();
    $display("Resorted:");
    print();
    array.reverse();
    $display("Reversed:");
    print();  
  endfunction:funcs
endclass:Array
module top;
  Array array;
  int arr[] ;     //   動態數組的聲明
  
  initial begin
    //  initial process
    array=new();
    array.print();
    array.funcs();
    arr = new[20];   //   分配內存大小
    foreach(arr[i]) $display(" arr[%2d] = %d ",i,arr[i]); 
    $display("***************");
    arr.delete();
  end
endmodule
//   所有關於定長數組的操作都可以用在動態數組上


systemverilog中的數組操作