将二维实数组传递给 System Verilog 中的函数

Pass 2D real array to a function in System Verilog

我正在尝试将 2D 实数数组传递给 SV 函数,return 同样,但我无法成功执行此任务。这是一个简单的可重现代码:

module Two_d_array_pass();

real x[2][2]='{'{1.0,2.0},'{3.0,4.0}};
real y[2][2]='{'{2.0,2.0},'{2.0,2.0}};
real z[2][2];
int i,j;

initial begin
    foreach(x[i,j]) $display("x[%0d][%0d]=%0f",i,j,x[i][j]);
    foreach(y[i,j]) $display("y[%0d][%0d]=%0f",i,j,y[i][j]);
    z=mult(x,y);
end

function real mult(x,y);
    real c[2][2];
    int i,j;    
    foreach(c[i,j]) 
        c[i][j]=x[i][j]*y[i][j];
    return c;
endfunction

endmodule

这是我得到的错误:

file: Two_d_array_pass.sv
                c[i][j]=x[i][j]*y[i][j];
                        |
xmvlog: *E,DIMERR (Two_d_array_pass.sv,18|10): Bit-select or part-select dimensions do not match declaration.
                c[i][j]=x[i][j]*y[i][j];
                                |
xmvlog: *E,DIMERR (Two_d_array_pass.sv,18|18): Bit-select or part-select dimensions do not match declaration.

任何人都可以更正代码以完成此任务吗?

您还需要将参数更改为 2x2 维。

function real mult(real x[2][2],y[2][2]);

但是对于 return 类型你也需要这样做,但是由于语法限制,解压后的数组需要一个 return 类型的 typedef。

typedef real real2x2[2][2];
function real2x2 mult(real x[2][2],y[2][2]);

那你还不如到处用typedef

module Two_d_array_pass();
  typedef real real2x2[2][2];
  real2x2 x='{'{1.0,2.0},'{3.0,4.0}};
  real2x2 y='{'{2.0,2.0},'{2.0,2.0}};
  real2x2 z;

initial begin
    foreach(x[i,j]) $display("x[%0d][%0d]=%0f",i,j,x[i][j]);
    foreach(y[i,j]) $display("y[%0d][%0d]=%0f",i,j,y[i][j]);
    z=mult(x,y);
end

  function real2x2 mult(real2x2 x,y);  
    foreach(x[i,j]) 
        mult[i][j]=x[i][j]*y[i][j];
  endfunction

endmodule

P.S。您没有单独声明迭代变量 ij,它们是隐式的并且在 foreach 语句的范围内是局部的。如果单独声明它们,它们将成为 foreach 循环未使用的额外变量。