0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

SystemVerilog中的操作方法

芯片驗證工程師 ? 來源:芯片驗證工程師 ? 作者:芯片驗證工程師 ? 2022-10-31 10:10 ? 次閱讀

SystemVerilog提供了幾個內(nèi)置方法來支持?jǐn)?shù)組搜索、排序等功能。

Array Locator Methods

下表是數(shù)組定位方法,需要附帶" with "子語句,基于給定的表達(dá)式上從現(xiàn)有數(shù)組中篩選出某些元素。所有滿足給定表達(dá)式的元素都會返回到一個隊列中:

2c9277ce-578c-11ed-a3b6-dac502259ad0.png

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表達(dá)式的元素的索引。

 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子語句的定位方法。

2cfdea2c-578c-11ed-a3b6-dac502259ad0.png

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方法在字符串?dāng)?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 "中的所有唯一元素的下標(biāo):

quei = intA.unique_index;
$display("quei=%p",quei);

“intA”的元素是3,2,6,7,3。所以指標(biāo)1 2 3 4處的值是獨一無二的。索引5的最后一個值3是重復(fù)的。因此,仿真打印如下:

quei='{1, 2, 3, 4}

Array Ordering Methods

數(shù)組排序方法對數(shù)組的元素進(jìn)行重新排序,但關(guān)聯(lián)數(shù)組除外。

2d224b06-578c-11ed-a3b6-dac502259ad0.png

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ù)組計算為單個值。可以使用可選的with子句來指定約簡方法中使用的值。

2d436a70-578c-11ed-a3b6-dac502259ad0.png

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

審核編輯:湯梓紅

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • Verilog
    +關(guān)注

    關(guān)注

    28

    文章

    1333

    瀏覽量

    109713
  • System
    +關(guān)注

    關(guān)注

    0

    文章

    163

    瀏覽量

    36744
  • 數(shù)組
    +關(guān)注

    關(guān)注

    1

    文章

    411

    瀏覽量

    25821

原文標(biāo)題:SystemVerilog中的操作方法

文章出處:【微信號:芯片驗證工程師,微信公眾號:芯片驗證工程師】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。

收藏 人收藏

    評論

    相關(guān)推薦

    GPIO引腳操作方法概述

    目錄一、硬件知識 - LED原理圖1、通過硬件原理圖剖析:怎么點亮的LED?二、GPIO引腳操作方法概述1、如果想要使得GPIO的某個引腳輸出高低電平,該怎么做?2、GPIO寄存器的2種操作方法
    發(fā)表于 01-20 07:38

    RK3288的GPIO操作方法是什么

    GPIO寄存器的操作方法有哪幾種呢?RK3288的GPIO操作方法是什么?
    發(fā)表于 03-09 07:38

    PCB應(yīng)力應(yīng)變測試操作方法

    PCB應(yīng)力應(yīng)變測試操作方法
    發(fā)表于 06-12 22:22

    控溫/恒溫烙鐵操作方法及使用說明

    控溫/恒溫烙鐵操作方法及使用說明 一.
    發(fā)表于 04-18 00:17 ?8489次閱讀

    EWB的基本操作方法

    EWB的基本操作方法 1.創(chuàng)建電路(1)元器件操作  元件選用:打開元件庫欄,移動鼠標(biāo)到需要的元件圖形上,按下左鍵,將元件符
    發(fā)表于 03-05 16:10 ?2.6w次閱讀
    EWB的基本<b class='flag-5'>操作方法</b>

    BGA元件的維修技術(shù)及操作方法

    BGA元件的維修技術(shù)及操作方法 球柵列陣封裝技術(shù)(Ball Gird Arroy),簡稱BGA封裝早在80年代已用于尖端軍備、導(dǎo)彈和航天科技
    發(fā)表于 04-20 14:17 ?8129次閱讀
    BGA元件的維修技術(shù)及<b class='flag-5'>操作方法</b>

    智能儀表組態(tài)操作方法評述

    智能儀表 的應(yīng)用方興未艾,其組態(tài)操作方法多種多樣。看似儀表的使用細(xì)節(jié)問題,確是設(shè)計中所容易忽略的。本文試就該問題進(jìn)行探討評述,并重點對智能流量計幾種常用組態(tài)操作方法
    發(fā)表于 07-21 15:36 ?43次下載

    工業(yè)烤箱操作方法及異?,F(xiàn)象排除

    工業(yè)烤箱操作方法及異?,F(xiàn)象排除
    發(fā)表于 08-23 16:26 ?2693次閱讀

    iphone遠(yuǎn)程控制電腦的操作方法

    iphone遠(yuǎn)程控制電腦的操作方法
    發(fā)表于 02-18 12:53 ?1.3w次閱讀
    iphone遠(yuǎn)程控制電腦的<b class='flag-5'>操作方法</b>

    PROTEL鋪銅操作方法

    PROTEL鋪銅操作方法----鋪銅實用技巧,有用的資料。
    發(fā)表于 03-11 15:33 ?0次下載

    獨立按鍵操作方法

    慧凈HL-1 配套C實驗例程100例【實驗11】獨立按鍵操作方法),很好的C51學(xué)習(xí)資料程序。
    發(fā)表于 03-21 17:01 ?4次下載

    Altium designer阻值圖輸出的詳細(xì)操作方法

    阻值圖輸出的詳細(xì)操作方法
    發(fā)表于 01-08 15:41 ?0次下載

    電子測力計的操作方法

    電子測力計正確操作方法
    發(fā)表于 04-17 15:57 ?2425次閱讀

    光幕傳感器工作原理及操作方法

    本文首先接介紹了光幕傳感器工作原理,其次介紹了光幕傳感器操作方法,最后介紹了光幕傳感器操作方法。
    發(fā)表于 10-12 08:51 ?9799次閱讀

    AD18操作方法

    AD18操作方法
    發(fā)表于 03-28 15:04 ?0次下載