在系统Verilog

时间:2017-06-14 03:10:50

标签: system-verilog dynamic-arrays

我想从函数返回一个动态数组。但是在声明动态数组时我收到错误。有人可以帮助我如何定义它。我几乎没有具体的怀疑

  1. 如果我的返回类型是动态数组(out_data),我是否需要像这样分配内存空间:pre_processing= new [temp_i.len()]
  2. 我在宣布bit temp_i_b[]时收到错误,是否需要以其他方式执行此操作?
  3. 要返回数据(在这种情况下是动态数组),我们是否需要使用return关键字?
  4. 在这方面的任何帮助都非常值得赞赏。

      module test1();
    
    typedef  bit [511:0] out_data[];
    
    string a="A";
    
    function out_data pre_processing(string temp_i);
    
    
         bit temp_i_b[];
         int k,d_zero,l;
         bit d[];
         bit tempor[];
         out_data temp_out;
         int n;
         int T=0;
         int i=0;
         int size;
         pre_processing= new [temp_i.len()]  ; 
         temp_i_b=new[24](temp_i_b);
    
    size =(temp_i.len())*4'd8;
    $display(size);
    $display(temp_i);
    $display("%b",temp_i.atobin());//
    foreach(temp_i_b[i]) begin
      temp_i_b[i]=temp_i.atobin()[i];
    
    //temp[3:0]=4'b1111
    foreach(temp_i_b[i])
      $display("temp[%d]=%d\n",i,temp_i_b[i]);
    end
         d=new[10](d);
    
    
    
         temp_out=new [3] (temp_out);
    
         tempor=new [512*3] (tempor);
    
    
    
        l=temp_i.len()*8;//l=24
        k=447-l;
    
         if(k<0)
          k=512+k;
    
    
         do
         begin
          d[i]=l%2;
          l=l/2;
          i++;             
         end while(l>0);
    
         T=l+1+k+d.size();
         n=T/512;
          d_zero=n*512-T;         
          temp_i_b={temp_i_b,1'b1};
    foreach(tempor[i])      
      tempor[i]= temp_i_b << (k+d_zero);
         foreach(d[i])
         tempor[tempor.size()-1:0]={tempor[tempor.size()-1:0],d[d.size()-i-1]};
    
         //end      
         foreach(tempor[i]) begin  
          foreach(temp_out[i]) begin        
            while(tempor[$size(tempor)-1:0]!=0) begin
              temp_out[i]=tempor[$size(tempor)-1:$size(tempor)-512];
         tempor[$size(tempor)-1:0]=tempor[$size(tempor)-1:0] << 512;
          end
          end
         end
    
          foreach(temp_out[i])
          return temp_out;
          endfunction
    
    
    out_data my_q;
    initial begin
    
      my_q=pre_processing("111");
    
      $display("%d",a.atoi());
      $display("output=%b",my_q[0]);
        end
    
    
      endmodule
    

1 个答案:

答案 0 :(得分:0)

  

如果我的返回类型是动态数组(out_data),我是否需要像这样分配内存空间(pre_processing = new [temp_i.len()])

是。您需要为其分配内存。

  

我在声明位temp_i_b []时收到错误,是否需要以其他方式执行此操作?

您需要在pre_processing= new [temp_i.len()];

之前声明所有变量

还有其他一些错误 -

  • 如果没有索引,您将无法访问动态数组:tempor[]=temp_i_b << (k+d_zero);
  • 我不认为这样的作业是允许的 - my_q=pre_processing(temp_i="ABC");或者你是否想要进行平等检查?
  • 您的typedef语句似乎有点奇怪 - typedef bit [511:0] out_data[];我认为在out_data结尾处不需要[]
  

要返回数据(本例中为动态数组),我们是否需要使用“return”关键字?

不,如果它是全球可访问的,否则是。