iVerilog错误:常量表达式中不允许引用wire或reg



我是Verilog的新手,如果有人能帮我解决这个错误,我将不胜感激:

output  reg [0:image_width][image_height:0]    result
....
integer i, j, imageX, imageY, x, y, kernelX, kernelY;
....
@(negedge ACLK)
for(x = 0; x < image_width; x++) begin 
    for(y = 0; y < image_height; y++)
    begin
    //multiply every value of the filter with corresponding image pixel
        for(kernelX = 0; kernelX < kernel_width; kernelX++) begin            
            for(kernelY = 0; kernelY < kernel_height; kernelY++)
                begin           
                    imageX = (x - kernel_width / 2 + kernelX + image_width) % image_width;
                    imageY = (y - kernel_height / 2 + kernelY + image_height) % image_height;
                    // ignore input samples which are out of bound
                    if( imageY >= 0 && imageY < image_height && imageX >= 0 && imageX < image_width )
                        //ERROR HERE!!!
                        result[x][y] += image[imageX][imageY] * kernel[kernelX][kernelY];   
                    end
                end
        end
    end
end

我得到的错误是:

错误:常量表达式中不允许引用wire或reg('x'(
错误:这里的数组索引表达式必须是常量
错误:常量表达式中不允许引用wire或reg('imagesX'(
错误:这里的数组索引表达式必须是常量
错误:常量表达式中不允许引用wire或reg('kernelX'(
错误:这里的数组索引表达式必须是常量。

有人能告诉我我做错了什么吗?非常感谢。

这一行就是问题所在:

result[x][y] += image[imageX][imageY] * kernel[kernelX][kernelY];

只有常量表达式才允许对数组进行索引。不允许在矢量索引中使用变量。请记住,您使用的是HDL:您在规定硬件中的物理连接。索引中有一个变量意味着能够动态地重新布线电路。这个SO问题有一些粗略的解决方法,可能对你有用。然而,您应该真正尝试重构您的算法,以避免首先使用变量索引。

顺便说一句,你应该使用非阻塞分配,而不是你目前拥有的阻塞分配。您的代码位于时钟块中,因此应避免阻塞组合逻辑:

imageX <= (x - kernel_width / 2 + kernelX + image_width) % image_width;
imageY <= (y - kernel_height / 2 + kernelY + image_height) % image_height;
// ignore input samples which are out of bound
if( imageY >= 0 && imageY < image_height && imageX >= 0 && imageX < image_width )
    result[x][y] <= result[x][y] + image[imageX][imageY] * kernel[kernelX][kernelY];   
@(negedge ACLK);
               ^

我确信分号不属于那里。正如所写的,for循环都在always块之外。

此外,您的image阵列目前每像素只有一位。这是故意的吗?不管是不是,我建议您重新考虑这个架构;在单个时钟周期中对任何显著大小的图像进行滤波都不会很好地合成。

最新更新