gpt4 book ai didi

multidimensional-array - 系统验证 : associative array of dynamic arrays

转载 作者:行者123 更新时间:2023-12-01 23:29:53 24 4
gpt4 key购买 nike

在 systemverilog 中 - 是否可以创建动态数组的关联数组?

特别是 - 我需要从某种类型的请求的 ID(整数)到字节数组(对请求的响应)的映射,但是每个字节数组的大小只是在运行时已知。

如果不可能,有没有办法用指针的关联数组或类似指针的对象代替?或者对于解决这些类型的数据结构的任何其他想法?

我知道我可以为数组创建一个包装器类,但是对于这样的基本需求来说这似乎有点麻烦......

谢谢

最佳答案

可以有动态数组的关联数组(或动态数组的动态数组等),例如:

byte AA_OF_DA_OF_BYTE [*][]; 

问题在于,一旦您获得了不止一维的动态数组,System-Verilog 语言就会有点困难,您必须开始编写更多代码:

module ASSOC_OF_DYN;

//
// here's your associative array of dynamic arrays
//

byte AA_OF_DA_OF_BYTE [*][];


//
// iterate over the associative array to fill it full
//

// eg 16 possible dynamic arrays...
int unsigned NO_AI = 16;

// ...of up to 256 bytes
int unsigned MAX_DA_SIZE = 256;

// this array is indexed by consequtive unsigned ints, but you can index by
// whatever you like
initial begin : FILL
for (int AI = 0; AI < NO_AI; AI++) begin : AI_LOOP

// pick a random size for the dynamoc array...
automatic int unsigned DA_SIZE = $urandom_range(0, MAX_DA_SIZE-1);

// ...and allocate the AIth dynamic array
AA_OF_DA_OF_BYTE[AI] = new[DA_SIZE];

// fill the dynamic array - this could be done some other way
for (int DI = 0; DI < DA_SIZE; DI++)
AA_OF_DA_OF_BYTE[AI][DI] = $urandom_range(0, 255); // because it is a byte

end : AI_LOOP
end : FILL


//
// display the filled array
//
final begin : DISPLAY
for (int AI = 0; AI < NO_AI; AI++)
$display("AA_OF_DA_OF_BYTE[%d]= %p", AI, AA_OF_DA_OF_BYTE[AI]);
end : DISPLAY

endmodule

https://www.edaplayground.com/x/kZM

关于multidimensional-array - 系统验证 : associative array of dynamic arrays,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40884205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com