Array basic operations in 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 = ' {' {{5{5}},default:0}; }, ' The basic method of assignment cannot be used, Maybe the compiler's version is too low */ 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[$] ; // reduction of Operation &NBSP;XOR&NBSP;,&NBSP;OR&NBSP; and , xnor , sum , product et $ 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 ()); // Index Operations: Obtain the appropriate array primitive location $display (" max value is %d ", Array.max () [0]); $display (" min value is %d ", Array.min () [0]); array = array.unique (); print (); // when in a class, calls the function of its own class, which can be called directly. This is the same as CPP pos = array.find with ( item == 7 ) ; // find a variety of variants: find_first find_last find_first_index find_last_index $display (" pos : %d ? value: %d ", Pos[0],array[pos[0]]); // Sort operations: sort , rsort , reverse , shuffle , // where sort ,rsort The; can be combined with the with, sorting array.shuffle (); $display according to the conditions with the (" Shuffled: "); print (); array.sort (); $ Display ("Sorted:"); print (); array.rsort (); $display ("resorted:"); print (); array.reverse (); $display ("Reversed:"); print (); endfunction: funcsendclass:arraymodule top; array array; int arr[] ; // declaration of dynamic Arrays initial begin // initial process array=new (); Array.print (); array.funcs (); arr = new[20]; // Allocating memory Size foreach (Arr[i]) $display (" arr[%2d] = %d ", I,arr[i]); $display ("***************"); arr.delete (); endendmodule// all operations on fixed-length arrays can be used on dynamic arrays
Array operations in the SystemVerilog