【问题标题】:SystemVerilog: associative array of dynamic arraysSystemVerilog:动态数组的关联数组
【发布时间】:2017-04-14 12:44:13
【问题描述】:

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

具体来说 - 我需要一个从某种类型请求的 id(整数)到字节数组(对请求的响应)的映射,但是每个字节数组的 size 只是在运行时已知。

如果不可能,有没有办法用一个关联的指针数组或类似对象的指针?或者对于这些类型的数据结构的解决方案有什么其他想法?

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

谢谢

【问题讨论】:

    标签: multidimensional-array associative-array system-verilog dynamic-arrays


    【解决方案1】:

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

    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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多