verilog中数组的定义_systemverilog中的数组操作

本文通过一个SystemVerilog类展示了数组的基本操作,包括初始化、打印、求和、位运算、查找、排序等功能。同时,还介绍了关联数组的概念及其在存储大量数据时的高效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sv中的数组基本操作:/*

Exercsise platform :     Questa Sim 10.1b

*/

class Array;

int array[9:0] ;

function new();

for( int i = 0 ; 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 

$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

//   所有关于定长数组的操作都可以用在动态数组上

仿真结果:# ** Note: (vsim-3812) Design is being optimized...

#

# Loading sv_std.std

# Loading work.array_sv_unit(fast)

# Loading work.top(fast)

>>>run

#  array[          9] =           9

#  array[          8] =           8

#  array[          7] =           7

#  array[          6] =           6

#  array[          5] =           5

#  array[          4] =           4

#  array[          3] =           3

#  array[          2] =           2

#  array[          1] =           1

#  array[          0] =           0

#  **  **  **  **  **  **  **  **  **  **

# the sum is          45

# the and is           0

# the  or is          15

# the xor is           1

# the product is           0

#  max value is           9

#  min value is           0

#  array[          9] =           0

#  array[          8] =           1

#  array[          7] =           2

#  array[          6] =           3

#  array[          5] =           4

#  array[          4] =           5

#  array[          3] =           6

#  array[          2] =           7

#  array[          1] =           8

#  array[          0] =           9

#  **  **  **  **  **  **  **  **  **  **

#  pos :           7 ? value:           2

# shuffled:

#  array[          9] =           2

#  array[          8] =           8

#  array[          7] =           0

#  array[          6] =           4

#  array[          5] =           7

#  array[          4] =           1

#  array[          3] =           9

#  array[          2] =           5

#  array[          1] =           6

#  array[          0] =           3

#  **  **  **  **  **  **  **  **  **  **

# Sorted:

#  array[          9] =           0

#  array[          8] =           1

#  array[          7] =           2

#  array[          6] =           3

#  array[          5] =           4

#  array[          4] =           5

#  array[          3] =           6

#  array[          2] =           7

#  array[          1] =           8

#  array[          0] =           9

#  **  **  **  **  **  **  **  **  **  **

# Resorted:

#  array[          9] =           9

#  array[          8] =           8

#  array[          7] =           7

#  array[          6] =           6

#  array[          5] =           5

#  array[          4] =           4

#  array[          3] =           3

#  array[          2] =           2

#  array[          1] =           1

#  array[          0] =           0

#  **  **  **  **  **  **  **  **  **  **

# Reversed:

#  array[          9] =           0

#  array[          8] =           1

#  array[          7] =           2

#  array[          6] =           3

#  array[          5] =           4

#  array[          4] =           5

#  array[          3] =           6

#  array[          2] =           7

#  array[          1] =           8

#  array[          0] =           9

#  **  **  **  **  **  **  **  **  **  **

#  arr[ 0] =           0

#  arr[ 1] =           0

#  arr[ 2] =           0

#  arr[ 3] =           0

#  arr[ 4] =           0

#  arr[ 5] =           0

#  arr[ 6] =           0

#  arr[ 7] =           0

#  arr[ 8] =           0

#  arr[ 9] =           0

#  arr[10] =           0

#  arr[11] =           0

#  arr[12] =           0

#  arr[13] =           0

#  arr[14] =           0

#  arr[15] =           0

#  arr[16] =           0

#  arr[17] =           0

#  arr[18] =           0

#  arr[19] =           0

# ***************

关联数组:

此数组适用于进行存储量较大时的情况,存储方式不是线性的直接存储,而是使用树或者hash的结构,节省空间module coarr;

bit[63:0]  rom[bit[63:0]] ;   // 构建关联数组

bit[63:0]  idx ;     //  相对应的索引

function automatic void print(bit[63:0] pos, ref bit[63:0] rom[bit[63:0]]);

$display(" rom[%d] = %d ",pos,rom[pos]);

endfunction:print

initial begin

idx = 1 ;

repeat(64) begin        //  对关联数组初始化,但实际上只存储了64个值

rom[idx] = idx ;

idx = idx <

end

if( rom.first(idx)) begin     //  找到第一个索引的位置

print(idx,rom);

while( rom.next(idx) ) print(idx,rom);    //   找到之后的索引的位置

end

end

endmodule:coarr

仿真结果:#  rom[            1] =                    1

#  rom[           2] =                    2

#  rom[          4] =                    4

#  rom[           8] =                    8

#  rom[          16] =                   16

#  rom[          32] =                   32

#  rom[          64] =                   64

#  rom[          128] =                  128

#  rom[         256] =                  256

#  rom[          512] =                  512

#  rom[         1024] =                 1024

#  rom[         2048] =                 2048

#  rom[         4096] =                 4096

#  rom[         8192] =                 8192

#  rom[        16384] =                16384

#  rom[        32768] =                32768

#  rom[         65536] =                65536

#  rom[        131072] =               131072

#  rom[        262144] =               262144

#  rom[        524288] =               524288

#  rom[       1048576] =              1048576

#  rom[       2097152] =              2097152

#  rom[       4194304] =              4194304

#  rom[       8388608] =              8388608

#  rom[       16777216] =             16777216

#  rom[       33554432] =             33554432

#  rom[       67108864] =             67108864

#  rom[      134217728] =            134217728

#  rom[      268435456] =            268435456

#  rom[      536870912] =            536870912

#  rom[      1073741824] =           1073741824

#  rom[      2147483648] =           2147483648

#  rom[      4294967296] =           4294967296

#  rom[      8589934592] =           8589934592

#  rom[     17179869184] =          17179869184

#  rom[     34359738368] =          34359738368

#  rom[     68719476736] =          68719476736

#  rom[     137438953472] =         137438953472

#  rom[     274877906944] =         274877906944

#  rom[     549755813888] =         549755813888

#  rom[     1099511627776] =        1099511627776

#  rom[    2199023255552] =        2199023255552

#  rom[    4398046511104] =        4398046511104

#  rom[     8796093022208] =        8796093022208

#  rom[    17592186044416] =       17592186044416

#  rom[    35184372088832] =       35184372088832

#  rom[    70368744177664] =       70368744177664

#  rom[    140737488355328] =      140737488355328

#  rom[   281474976710656] =      281474976710656

#  rom[   562949953421312] =      562949953421312

#  rom[   1125899906842624] =     1125899906842624

#  rom[   2251799813685248] =     2251799813685248

#  rom[   4503599627370496] =     4503599627370496

#  rom[   9007199254740992] =     9007199254740992

#  rom[  18014398509481984] =    18014398509481984

#  rom[  36028797018963968] =    36028797018963968

#  rom[  72057594037927936] =    72057594037927936

#  rom[  144115188075855872] =   144115188075855872

#  rom[  288230376151711744] =   288230376151711744

#  rom[  576460752303423488] =   576460752303423488

#  rom[ 1152921504606846976] =  1152921504606846976

#  rom[ 2305843009213693952] =  2305843009213693952

#  rom[ 4611686018427387904] =  4611686018427387904

#  rom[ 9223372036854775808] =  9223372036854775808

### Verilog 中的数组使用方法 #### 定义和初始化一维数组 SystemVerilog 提供了更丰富的数组类型支持,相比早期版本增强了功能。定义一个简单的定宽一维数组可以如下所示: ```verilog reg [7:0] one_dim_array [0:3]; // 创建了一个有四个8位寄存器元素的一维数组 initial begin one_dim_array[0] = 8'b0000_1111; // 初始化第一个元素 end ``` 此代码片段展示了如何创建并初始化一个具有固定宽度成员的一维数组[^1]。 #### 访问多维数组 对于更高维度的数组,在SystemVerilog中可以通过连续的方括号来访问特定位置的数据项。例如,要操作二维或三维数组,则采用相应的索引形式: ```verilog // 声明两个不同大小的二维数组 integer two_d_arr_a [4][5]; bit two_d_arr_b [$][$]; // 赋值给二维数组的一个具体单元格 two_d_arr_a[2][3] = 1; // 同理可应用于三维数组 logic three_d_arr [2][3][4]; three_d_arr[1][2][3] = 'hFF; ``` 这里说明了通过`addr_expr`参数化地址表达式的方式来进行多维数据结构的操作[^2]。 #### 阻塞与非阻塞赋值的区别 在涉及并发过程(如always块)时,理解阻塞(`=`)和非阻塞(`<=`)两种不同的赋值行为非常重要。前者会立即更新目标信号的状态,并阻止同一进程中后续语句的同时执行;后者则允许所有待处理的任务继续运行直到下一个时间单位到来才完成实际写入动作: ```verilog always @(posedge clk or negedge rst_n) begin : proc_name if (!rst_n) reg_var <= 0; else reg_var <= next_value; end ``` 上述例子体现了典型的同步状态机转换逻辑实现模式,利用非阻塞性质确保每次时钟沿触发后的有序变化[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值