SystemVerilog提供了幾個內(nèi)置方法來支持數(shù)組搜索、排序等功能。
Array Locator Methods
下表是數(shù)組定位方法,需要附帶" with "子語句,基于給定的表達式上從現(xiàn)有數(shù)組中篩選出某些元素。所有滿足給定表達式的元素都會返回到一個隊列中:
	
module arrayLocator; string ques[$]; //queue of string type int intA[int]; //associative array int quei[$]; //queue of int type int x; initial begin intA[1] = 3; intA[2] = 2; intA[3] = 6; intA[4] = 7; intA[5] = 3; //returns all elements stratifying the 'with' expression quei = intA.fnd( x ) with ( x > 5 ); $display("fnd(x)::quei=%0p",quei); //returns all elements stratifying the 'with' expression quei = intA.fnd( x ) with ( x < 5 ); $display("fnd(x)::quei=%0p",quei); //returns the indices of all elements //that satisfy the 'with' expression //quei = intA.fnd_index with (item == 3); quei = intA.fnd_index with (item > 1); $display("fnd_index::quei=%0p",quei); //returns the frst element satisfying 'with' expression quei = intA.fnd_frst with (item > 3); $display("fnd_frst::quei=%0p",quei); //returns the frst element satisfying 'with' expression end endmodule
仿真log:
fnd(x)::quei='{6, 7}
fnd(x)::quei='{3, 2, 3}
fnd_index::quei='{1, 2, 3, 4, 5}
fnd_frst::quei='{6}
 V C S S i m u l a t i o n R e p o r t
首先聲明一些數(shù)組和隊列。這些隊列是必需的,因為需要作為數(shù)組方法的返回值。
給int數(shù)組“intA”的元素賦值。
使用fnd定位方法如下:
quei = intA.fnd( x ) with ( x > 5 );
 $display("fnd(x)::quei=%0p",quei);
將返回所有>5的元素(6和7)
fnd(x)::quei='{6, 7}
再次使用fnd,返回所有<5的元素(3,2,3):
 quei = intA.fnd( x ) with ( x < 5 );
 $display("fnd(x)::quei=%0p",quei);
使用“fnd_index”,它將返回所有滿足with表達式的元素的索引。
 quei = intA.fnd_index with (item > 1);
 $display("fnd_index::quei=%0p",quei);
“intA”數(shù)組中的元素是3,2,6,7,3。所有值都是>1,所以仿真打印
fnd_index::quei='{1, 2, 3, 4, 5}
使用with子句查找第一個>3的元素。
 quei = intA.fnd_frst with (item > 3);
 $display("fnd_frst::quei=%0p",quei);
所找到的值是6,所以會打?。?/p>
fnd_frst::quei='{6}
現(xiàn)在讓我們看看其他可以不需要with子語句的定位方法。
	
module arrayLocator;
 string str[5] = '{"bob", "kim", "Derek", "bob", "kim"};
 string ques[$]; //queue of strings
 int intA[int]; //associative array
 int quei[$]; //queue of int
 int x;
 initial begin
 intA[1] = 3;
 intA[2] = 2;
 intA[3] = 6;
 intA[4] = 7;
 intA[5] = 3;
 // Find smallest item
 quei = intA.min;
 $display("quei=%p",quei);
 // Find string with largest numerical value in 'str'
 ques = str.max;
 $display("ques=%p",ques);
 // Find all unique string elements in 'str'
 ques = str.unique;
 $display("ques=%p",ques);
 // Find all unique indices in 'intA'
 quei = intA.unique_index;
 $display("quei=%p",quei);
 end
 endmodule
仿真log:
quei='{2}
ques='{"kim"}
ques='{"bob", "kim", "Derek"}
quei='{1, 2, 3, 4}
使用方法“min”在“intA”數(shù)組中查找值最小的元素:
 // Find smallest item
 quei = intA.min;
 $display("quei=%p",quei);
“intA”的元素是3,2,6,7,3。最小的值是2,所以打印:
quei='{2}
我們使用max方法在字符串數(shù)組" str "中搜索數(shù)值最大的元素:
ques = str.max;
$display("ques=%p",ques);
“str”的值為“bob”、“kim”、“Derek”、“bob”和“kim”。所以最大的數(shù)值是“kim”:
ques='{"kim"}
接下來,我們查找字符串" str "中所有唯一的元素:
ques = str.unique;
$display("ques=%p",ques);
“str”的值為“bob”、“kim”、“Derek”、“bob”和“kim”。因為“bob”和“kim”是重復(fù)的,它們不是唯一的。因此,我們在仿真log中看到以下內(nèi)容:
ques='{"bob", "kim", "Derek"}
最后,我們搜索數(shù)組" intA "中的所有唯一元素的下標:
quei = intA.unique_index;
$display("quei=%p",quei);
“intA”的元素是3,2,6,7,3。所以指標1 2 3 4處的值是獨一無二的。索引5的最后一個值3是重復(fù)的。因此,仿真打印如下:
quei='{1, 2, 3, 4}
Array Ordering Methods
數(shù)組排序方法對數(shù)組的元素進行重新排序,但關(guān)聯(lián)數(shù)組除外。
	
module arrayOrder;
string str[5] = '{"bob", "george", "ringo", "john", 
"paul"};
int intA[8] = '{3,2,1,6,8,7,4,9};
initial begin
$display("BEFORE 'str' reverse: str=%p", str);
str.reverse;
$display("AFTER 'str' reverse: str=%p", str);
$display("BEFORE 'intA' sort: intA=%p", intA);
intA.sort;
$display("AFTER 'intA' sort: intA=%p",intA);
$display("BEFORE 'intA' rsort: intA=%p",intA);
intA.rsort;
$display("AFTER 'intA' rsort: intA=%p",intA);
$display("BEFORE 'intA' shuffe: intA=%p",intA);
intA.shuffe;
$display("AFTER 'intA' shuffe: intA=%p",intA);
end
endmodule
仿真log:
BEFORE 'str' reverse: str='{"bob", "george", "ringo", "john", "paul"}
AFTER 'str' reverse: str='{"paul", "john", "ringo", "george", "bob"}
BEFORE 'intA' sort: str='{3, 2, 1, 6, 8, 7, 4, 9}
AFTER 'intA' sort: intA='{1, 2, 3, 4, 6, 7, 8, 9}
BEFORE 'intA' rsort: intA='{1, 2, 3, 4, 6, 7, 8, 9}
AFTER 'intA' rsort: intA='{9, 8, 7, 6, 4, 3, 2, 1}
BEFORE 'intA' shuffe: intA='{9, 8, 7, 6, 4, 3, 2, 1}
AFTER 'intA' shuffe: intA='{2, 4, 1, 6, 7, 3, 9, 8}
 V C S S i m u l a t i o n R e p o r t
	3.5.3陣列約簡方法
	數(shù)組約簡方法應(yīng)用于任意解包裝的整數(shù)值數(shù)組
	將數(shù)組縮減為單個值??梢允褂每蛇x的with子句來指定
	約簡方法中使用的值。這些方法返回一個相同的值
	類型作為數(shù)組元素類型。表3.5描述了陣列縮減方法。
	下面是一個例子:
Array Reduction Methods
數(shù)組約簡方法應(yīng)用于任意整數(shù)值數(shù)組,將數(shù)組計算為單個值??梢允褂每蛇x的with子句來指定約簡方法中使用的值。
	
module arrayReduction;
 int intA[4] = '{4,3,2,1};
 logic [7:0] intB [2][2] = '{ '{1,2}, '{3,4} };
 int y;
 initial begin
 y = intA.sum;
 $display("intA.sum = %0d",y); //sum = 10 (4+3+2+1)
 y = intA.sum with ( item + 1);
 $display("intA.sum = %0d",y); //sum=14 (5+4+3+2)
 //y = intB.sum; //Compile ERROR
 //y = intB.sum with (item.sum); //OK
 y = intB.sum with (item.sum with (item)); //OK
 $display("intB.sum = %0d",y); //sum = 10 (1+2+3+4)
 //y = intB.xor; //Compile Error
 //y = intB.xor(item) with (item > 0); //Compile Error
 y = intB.xor(item) with (item.xor); //OK
 $display("intB.xor = %0h",y); //xor = 4 (1^2^3^4)
 y = intA.product;
 $display("intA.product = %0d",y); //product = 24 (4*3*2*1)
 y = intA.product(item) with (item + 1);
 $display("intA.product = %0d",y); //product = 120 (5*4*3*2)
 y = intA.and;
 $display("intA.and = %0h",y); //'and' = 0 (4&3&2&1)
 y = intA.or;
 $display("intA.or = %0h",y); //'or' = 7 (4 || 3 || 2 || 1)
 end
 endmodule
仿真log:
intA.sum = 10 intA.sum = 14 intB.sum = 10 intB.xor = 4 intA.product = 24 intA.product = 120 intA.and = 0 intA.or = 7 V C S S i m u l a t i o n R e p o r t
約簡運算符只適用于一維數(shù)組,如果你試著在二維數(shù)組上使用約簡運算符,將得到一個編譯錯誤。
Error-[IMDARMC] Illegal MDA reduction method call
審核編輯:湯梓紅
- 
                                Verilog
                                +關(guān)注
關(guān)注
30文章
1369瀏覽量
113903 - 
                                System
                                +關(guān)注
關(guān)注
0文章
166瀏覽量
38389 - 
                                數(shù)組
                                +關(guān)注
關(guān)注
1文章
420瀏覽量
27069 
原文標題:SystemVerilog中的操作方法
文章出處:【微信號:芯片驗證工程師,微信公眾號:芯片驗證工程師】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
GPIO引腳操作方法概述
EWB的基本操作方法
    
          
        
        
SystemVerilog中的操作方法
                
 
    
           
            
            
                
            
評論