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

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

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

SystemVerilog中的關(guān)聯(lián)數(shù)組

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

關(guān)聯(lián)數(shù)組實際上是一種查找表,內(nèi)存空間直到被使用時才會分配,每個數(shù)據(jù)項都會有一個特定的“鍵(索引)”,索引的類型不局限于整型。

相對于一般的數(shù)組,關(guān)聯(lián)數(shù)組的內(nèi)存空間利用會更加充分,因為它是稀疏的,無需一開始就分配很大的內(nèi)存空間。

當(dāng)變量集合的大小未知或數(shù)據(jù)空間稀疏時,關(guān)聯(lián)數(shù)組是比動態(tài)數(shù)組更好的選擇。例如只需要使用到地址0和地址FF,關(guān)聯(lián)數(shù)組可以只分配2個地址空間,而動態(tài)數(shù)組則需要分配256個地址空間。

打個比方,如果動態(tài)數(shù)組時內(nèi)存,那么關(guān)聯(lián)數(shù)組更像是一個具有“tag”的cache。

關(guān)聯(lián)數(shù)組也是一種unpacked數(shù)組。

 data_type array_id [index_type];

其中data_type是數(shù)組元素的數(shù)據(jù)類型。array_id是數(shù)組的名稱。index_type是索引(或鍵)的數(shù)據(jù)類型。各種數(shù)據(jù)類型的索引示例如下:

//wildcard index. Can be indexed by any integral type
 int myArray[ * ];
 
 //Array that stores 'bit [31:0]' at string type index.
 bit [31:0] myArray[ string ];
 
 //Array that stores 'string' at string type index.
 string myArray [ string ];
 
 // Array that stores 'int' at Class type index
 int myArray [ class ];
 
 //Array that stores 'logic' type at integer type index
 logic myArray[ integer ];
 
 typedef bit signed [7:0] mByte;
 int myArray [mByte]; //'bit signed' index
 

比較特別的是以class作為索引類型的聯(lián)合數(shù)組。

module assoc_arr;
 class AB;
    int a;
    int b;
 endclass
 
 int arr[AB]; //Associative array 'arr' with class 'AB' as index
 AB obj, obj1;
 initial begin
    obj = new();
    obj1= new();
    arr[obj]=20; //Store 20 at the object handle index 'obj'
    $display("%0d",arr[obj]);
    arr[obj1]=10; //Store 10 at the object handle index 'obj1'
    $display("%0d",arr[obj1]);
 end
 endmodule

仿真log:

20
10
 V C S S i m u l a t i o n R e p o r t

聲明類“AB”,并使用它作為關(guān)聯(lián)數(shù)組中的索引

int arr[AB]

聲明兩個AB類型的對象(obj和obj1),并實例化,賦值以這兩個對象為索引的聯(lián)合數(shù)組值。

arr[obj] = 20; 
arr[obj1] = 10;

String Index– Example

下面是一個以字符串為索引類型的聯(lián)合數(shù)組示例:

module assoc_arr;
integer St [string] = '{"Peter":26, "Paul":24, "Mary":22};
integer data;
initial
begin
   $display("St=",St);
   $display("data stored at Peter = %0d",St["Peter"]);
   $display("data stored at Paul = %0d",St["Paul"]);
   $display("data stored at Mary = %0d",St["Mary"]);
   St["mike"] = 20; //new data stored at new index "mike"
   data = St["mike"]; //retrieve data stored at index "mike"
   $display("data stored at mike = %0d",data);
   $display("St=",St);
end
endmodule

仿真log:

run -all;
# KERNEL: St='{"Mary":22, "Paul":24, "Peter":26}
# KERNEL: data stored at Peter = 26
# KERNEL: data stored at Paul = 24
# KERNEL: data stored at Mary = 22
# KERNEL: data stored at mike = 20
# KERNEL: St='{"Mary":22, "Paul":24, "Peter":26, "mike":20}
# KERNEL: Simulation has fnished.

聲明關(guān)聯(lián)數(shù)組“St”,并對其進(jìn)行初始化:

integer St [string] = '{"Peter":26, "Paul":24, "Mary":22};

意味著將26存儲在索引“Peter”中,24存儲在索引中
“Paul”,22存儲在索引“Mary”中。
2. 分別打印數(shù)組。
3. 在新索引“mike”中添加新數(shù)據(jù)。注意,這個數(shù)據(jù)項的內(nèi)存此時才開始分配。

Associative Array Methods

下面是關(guān)聯(lián)數(shù)組提供的一些方法。

ee25bb46-56cc-11ed-a3b6-dac502259ad0.png

module assoc_arr;
int temp, imem[int];
integer St [string] = '{"Peter":20, "Paul":22, "Mary":23};
initial
begin
   if(St.exists( "Peter") ) $display(" Index Peter exists ");
   //Assign data to imem[int]
   imem[ 2'd3 ] = 1;
   imem[ 16'hffff ] = 2;
   imem[ 4'b1000 ] = 3;
   $display( " imem has %0d entries", imem.num );
   if(imem.exists( 4'b1000))       $display("Index 4b'1000 
exist)");
   imem.delete(4'b1000);
if(imem.exists( 4'b1000))  $display("Index 4b'1000 
exists)");
else $display(" Index 4b'1000 does not exist");
   imem[ 4'b1000 ] = 3;
if(imem.first(temp)) $display(" First entry is at index 
%0d ",temp);
if(imem.next(temp)) $display(" Next entry is at index 
%0b ",temp);
if(imem.last(temp)) $display(" Last entry is at index 
%0h",temp);
   imem.delete( ); //delete all entries
   $display(" imem has %0d entries", imem.num );
    $display(" imem = %p", imem);
end
endmodule

仿真log:

Index Peter exists
imem has 3 entries
Index 4b'1000 exists
Index 4b'1000 does not exist
First entry is at index 3
Next entry is at index 1000
Last entry is at index ffff
imem has 0 entries
imem = '{}
 V C S   Simulation  Report

聲明了兩個關(guān)聯(lián)數(shù)組

int imem[int];
integer St [string] = '{"Peter":20, "Paul":22, "Mary":23};

使用.exists()判斷某個索引對應(yīng)的數(shù)據(jù)元素是否存在。

使用.num(),獲取該聯(lián)合數(shù)組具有的元素個數(shù)

使用.delete()刪除特定索引對應(yīng)的元素。如果不指定索引,則所有的元素都會被刪除。

使用方法first()、next()和last()訪問某個聯(lián)合數(shù)組元素

First entry is at index 3
Next entry is at index 1000
Last entry is at index ffff

Associative Array– Default Value

可以在關(guān)聯(lián)數(shù)組聲明時為其指定默認(rèn)值。

module assoc_arr;
 string words [int] = '{default: "hello"};
 initial begin
    $display("words = %p", words['hffff]); //default
    $display("words = %p", words[0]); //default
    $display("words = %p", words[1000]); //default
 words['hffff] = "goodbye";
    $display("words = %p", words);
    $display("words = %p", words[100]); //default
 end
 endmodule

仿真log:

words = "hello"

words = "hello"
words = "hello"
words = '{0xffff:"goodbye"}
words = "hello"
V C S S i m u l a t i o n R e p o r t

數(shù)組“words”的初始化默認(rèn)值是“hello”,當(dāng)我們訪問索引“hffff”、“0”和“1000”時,都會打印默認(rèn)值“hello”。

我們?yōu)閕ndex ' hffff分配一個字符串值" goodbye "。

Creating aDynamic Array ofAssociative Arrays

關(guān)聯(lián)數(shù)組也可以是動態(tài)數(shù)組中的元素,如下示例:

module assoc_arr;
//Create a dynamic array whose elements are associative arrays
int games [ ] [string];
initial begin
   //Create a dynamic array with size of 3 elements
   games = new [3];
   //Initialize the associative array  inside each dynamic
//array element
   games [0] = '{ "football" : 10,"baseball" : 20,"hututu":70 };
   games [1] = '{ "cricket" : 30, "ice hockey" : 40 };
   games [2] = '{ "soccer" : 50, "rugby" : 60 };
   // Iterate through each element of dynamic array
   foreach (games[element])
   // Iterate through each index of the    current element in
   // dynamic array
   foreach (games[element][index])
       $display ("games[%0d][%s] = %0d", element, index, 
   games[element][index]);
end
endmodule

仿真log:

games[0][baseball] = 20
games[0][football] = 10
games[0][hututu] = 70
games[1][cricket] = 30
games[1][ice hockey] = 40
games[2][rugby] = 60
games[2][soccer] = 50
 V C S S i m u l a t i o n R e p o r t

審核編輯:湯梓紅

聲明:本文內(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

    文章

    1335

    瀏覽量

    109847
  • System
    +關(guān)注

    關(guān)注

    0

    文章

    164

    瀏覽量

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

    關(guān)注

    1

    文章

    411

    瀏覽量

    25858

原文標(biāo)題:SystemVerilog中的關(guān)聯(lián)數(shù)組

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

收藏 人收藏

    評論

    相關(guān)推薦

    轉(zhuǎn)一篇Systemverilog的一個牛人總結(jié)

    () ;//釋放空間l 隊列在隊列增加或刪除元素比較方便。l 關(guān)聯(lián)數(shù)組當(dāng)你需要建立一個超大容量的數(shù)組關(guān)聯(lián)數(shù)組,存放稀疏矩陣的值。表示方
    發(fā)表于 08-27 14:50

    求職寶典:寒武紀(jì)2019筆試題

    。(2)動態(tài)數(shù)組:可以在仿真時分配空間或者調(diào)整寬度,這樣在仿真中就可以使用最小的存儲量。在聲明時,其下標(biāo)為空[ ],使用new[ ]操作符來分配空間。(3)關(guān)聯(lián)數(shù)組SystemVerilog提供
    發(fā)表于 12-24 11:55

    PHP數(shù)組排序

    數(shù)組排序(6個) sort() - 以升序?qū)?b class='flag-5'>數(shù)組排序rsort() - 以降序?qū)?b class='flag-5'>數(shù)組排序 reversal sort)asort() - 根據(jù)值,以升序?qū)?b class='flag-5'>關(guān)聯(lián)數(shù)組進(jìn)行排序(associ
    發(fā)表于 11-04 07:48

    基于社交網(wǎng)絡(luò)和關(guān)聯(lián)數(shù)據(jù)的服務(wù)網(wǎng)絡(luò)構(gòu)建方法

    網(wǎng)絡(luò)可用服務(wù)的急劇增加對面向服務(wù)計算技術(shù)的發(fā)展起到了極大的推動作用。針對服務(wù)的規(guī)模和利用率遠(yuǎn)沒有達(dá)到預(yù)期,以及服務(wù)之間交互關(guān)系的復(fù)雜性問題,提出基于社交網(wǎng)絡(luò)和關(guān)聯(lián)數(shù)據(jù)的服務(wù)網(wǎng)絡(luò)構(gòu)建方法。首先,結(jié)合
    發(fā)表于 12-06 13:50 ?0次下載
    基于社交網(wǎng)絡(luò)和<b class='flag-5'>關(guān)聯(lián)數(shù)</b>據(jù)的服務(wù)網(wǎng)絡(luò)構(gòu)建方法

    基于本體的軟件工程關(guān)聯(lián)數(shù)據(jù)的自動構(gòu)建

    針對目前在分布異構(gòu)的大規(guī)模軟件開發(fā)難以高效地知曉信息和發(fā)現(xiàn)知識的問題,將語義網(wǎng)引入軟件工程領(lǐng)域,對多源異構(gòu)數(shù)據(jù)進(jìn)行細(xì)粒度語義關(guān)聯(lián),提出本體構(gòu)建、關(guān)聯(lián)抽取和發(fā)現(xiàn)的方法,實現(xiàn)基于本體的軟件工程關(guān)
    發(fā)表于 12-22 17:03 ?0次下載
    基于本體的軟件工程<b class='flag-5'>關(guān)聯(lián)數(shù)</b>據(jù)的自動構(gòu)建

    SystemVerilog數(shù)組的賦值、索引和切片

    數(shù)組可以作為參數(shù)傳遞給子程序,當(dāng)數(shù)組作為值傳遞給子程序時,會將這個數(shù)組復(fù)制一份傳遞給子程序。
    的頭像 發(fā)表于 10-20 10:13 ?5009次閱讀

    SystemVerilog動態(tài)數(shù)組的大小更改展示

    需要使用" new "操作符實例化一個動態(tài)數(shù)組,使用[]表示。在實例化過程,會設(shè)置動態(tài)數(shù)組的大小。
    的頭像 發(fā)表于 10-21 09:43 ?1359次閱讀

    SystemVerilog的操作方法

    SystemVerilog提供了幾個內(nèi)置方法來支持數(shù)組搜索、排序等功能。
    的頭像 發(fā)表于 10-31 10:10 ?2577次閱讀

    SystemVerilog可以嵌套的數(shù)據(jù)結(jié)構(gòu)

    SystemVerilog除了數(shù)組、隊列和關(guān)聯(lián)數(shù)組等數(shù)據(jù)結(jié)構(gòu),這些數(shù)據(jù)結(jié)構(gòu)還可以嵌套。
    的頭像 發(fā)表于 11-03 09:59 ?1513次閱讀

    網(wǎng)絡(luò)和變量的未壓縮數(shù)組

    SystemVerilog有兩種類型的數(shù)組:壓縮數(shù)組和非壓縮數(shù)組。壓縮數(shù)組是連續(xù)存儲的位的集合,通常稱為向量。非壓縮
    的頭像 發(fā)表于 02-09 14:50 ?656次閱讀
    網(wǎng)絡(luò)和變量的未壓縮<b class='flag-5'>數(shù)組</b>

    使用SystemVerilog解決數(shù)組問題

    數(shù)獨是一種非常流行的游戲,數(shù)獨本質(zhì)上也是一個約束問題,所以我們可以讓SystemVerilog的約束求解器來幫助我們解決。 約束求解器的精妙之處就是,我們只描述約束限制,繁重的數(shù)值生成工作由工具來幫我們完成。 你只需“既要...又要...”,其他的讓下人干吧。
    的頭像 發(fā)表于 03-08 14:06 ?1394次閱讀

    一些有趣的數(shù)組相關(guān)的SystemVerilog約束

    我們在工作中常常會針對數(shù)組施加各式的約束,下面列舉一下有趣的Systemverilog數(shù)組約束示例。
    的頭像 發(fā)表于 03-08 13:12 ?870次閱讀

    一些有趣的數(shù)組相關(guān)的SystemVerilog約束

    我們在工作中常常會針對數(shù)組施加各式的約束,下面列舉一下有趣的**Systemverilog數(shù)組約束**示例
    的頭像 發(fā)表于 05-30 11:13 ?683次閱讀

    帶你了解SystemVerilog關(guān)聯(lián)數(shù)組

    SystemVerilog,我們知道可以使用動態(tài)數(shù)組實現(xiàn)數(shù)組元素個數(shù)的動態(tài)分配,即隨用隨分
    的頭像 發(fā)表于 06-09 09:46 ?6981次閱讀
    帶你了解<b class='flag-5'>SystemVerilog</b><b class='flag-5'>中</b>的<b class='flag-5'>關(guān)聯(lián)數(shù)組</b>

    隨機抽取SV數(shù)組的一個元素方法實現(xiàn)

    如果想從一個關(guān)聯(lián)數(shù)組隨機選取一個元素,需要逐個訪問它之前的元素,原因是沒辦法能夠直接訪問到第N個元素。上面的程序示范了如何從一個以整數(shù)值作為索引**的關(guān)聯(lián)數(shù)組隨機選取一個元素。
    的頭像 發(fā)表于 03-21 10:11 ?694次閱讀
    隨機抽取SV<b class='flag-5'>數(shù)組</b><b class='flag-5'>中</b>的一個元素方法實現(xiàn)