为什么我的输出信号总是出错?

时间:2019-11-28 14:09:51

标签: verilog

我编写了一个简单的Verilog代码,用于计算2个向量之间的内积。我不明白为什么,但是模块输出出现错误-红色信号。 return False变量可能存在一些问题。 有人知道我在想什么吗?

代码-

result

测试-

`resetall
`timescale 1ns/10ps
`include "../hdl/params.v"

module ZCalculationParllel(features_vec, weights_vec, bias, predict);

  // Feature vector in size of |NUMBER_OF_PIXELS| * |PIXEL_PRECISION| 
  input [(`NUMBER_OF_PIXELS * `PIXEL_PRECISION) - 1 : 0] features_vec;

  // Weights vector in size of |NUMBER_OF_PIXELS| * |WEIGHT_BIAS_PRECISION| 
  input [(`NUMBER_OF_PIXELS * `WEIGHT_BIAS_PRECISION) - 1 : 0] weights_vec;

  // Bias vector in size |WEIGHT_BIAS_PRECISION|
  input [`WEIGHT_BIAS_PRECISION - 1 : 0] bias;

   // The output value for prediction
  //output predict; in case we want return one bit represent prediction
  output [(2 * `PIXEL_PRECISION + $bits(`NUMBER_OF_PIXELS)) - 1 : 0] predict;

  // Accomulator array for saving the multiplication result before the adders
  wire [`PIXEL_PRECISION_PLUS_WEIGHT_BIAS_PRECISION - 1 : 0] multiplications [0 : `NUMBER_OF_PIXELS - 1];

  wire [(2 * `PIXEL_PRECISION + $bits(`NUMBER_OF_PIXELS)) - 1 : 0] result;

  genvar k;
  generate
        for (k = 0; k < `NUMBER_OF_PIXELS; k = k + 1)
         begin: elementMul
           assign multiplications[k] = features_vec[(k + 1) * `PIXEL_PRECISION - 1 -: `PIXEL_PRECISION] * weights_vec[(k + 1) * `WEIGHT_BIAS_PRECISION - 1 -: `WEIGHT_BIAS_PRECISION];
        end
    endgenerate

  assign result = 2;
  genvar i;
  generate
        for (i = 0; i < `NUMBER_OF_PIXELS; i = i + 1)
         begin: elementAdd
           assign result = result + multiplications[i];
         end
    endgenerate

  assign predict = result;

endmodule

1 个答案:

答案 0 :(得分:0)

assign是并发的,不是顺序的。如果任何分配有冲突,将导致Xs。

将您的result相关代码替换为以下内容:

reg [(2 * `PIXEL_PRECISION + $bits(`NUMBER_OF_PIXELS)) - 1 : 0] result;
integer i;
always @* begin
  result = 2;
  for (i = 0; i < `NUMBER_OF_PIXELS; i = i + 1) begin
    result = result + multiplications[i];
  end
end