简体   繁体   English

在Verilog生成语句中增加多个Genvars

[英]Incrementing Multiple Genvars in Verilog Generate Statement

I'm trying to create a multi-stage comparator in verilog and I can't figure out how to increment multiple genvars in a single generate loop. 我正在尝试用verilog创建一个多阶段比较器,我无法弄清楚如何在单个生成循环中增加多个genvars。 I'm trying the following: 我正在尝试以下方法:

genvar i,j;
//Level 1
generate
  j=0;
  for (i=0;i<128;i=i+1)
  begin: level1Comp
    assign ci1[i] = minw(tc[j],tc[j+1]);
    j = j+2;
  end
endgenerate

And getting the following error: 并收到以下错误:

Error-[SE] Syntax error
  Following verilog source has syntax error :
  "encoder.v", 322: token is '='
    j=0;

Anyone know how to increment multiple genvars in the same generate statement? 任何人都知道如何在同一个生成语句中增加多个genvars? Or at least get equivalent functionality? 或者至少获得相同的功能?

Anyone know how to increment multiple genvars in the same generate statement? 任何人都知道如何在同一个生成语句中增加多个genvars?

This is not allowed because a generate for loop creates an implicit localparam statement for the loop variable and elaborates the items in the loop based only on that localparam. 这是不允许的,因为generate for循环为循环变量创建一个隐式的localparam语句,并仅根据该localparam详细说明循环中的项。 This means any items inside the loop must be valid outside the loop if the genvar was declared as a localparam. 这意味着如果genvar被声明为localparam,则循环内的任何项必须在循环外有效。

genvar i,j;
//Level 1
generate
  j=0;
  for (i=0;i<128;i=i+1)
  begin: level1Comp
    assign ci1[i] = minw(tc[j],tc[j+1]);
    j = j+2;
  end
endgenerate

becomes

//Done for each value of i
genvar j;
localparam integer i = i_for_each_iteration;

j=0; //Not valid outside a procedural context so modelsim complains
assign ci1[i] = minw(tc[j],tc[j+1]);
j = j+2; //Also not valid outside a procedural context

In this case you could create a 'constant' value dependent on the genvar using an explicit parameter inside the loop. 在这种情况下,您可以使用循环内的显式参数创建依赖于genvar的“常量”值。

genvar i;
//Level 1
generate
  for (i=0;i<128;i=i+1)
  begin: level1Comp
    localparam integer j = i*2;
    assign ci1[i] = minw(tc[j],tc[j+1]);
  end
endgenerate

Assuming that ci1 has half the depth of tc and you want, say ci1[0] = min(tc[0], tc[1]) , ci[1] = min(tc[2], tc[3]) etc, the following should work: 假设ci1的深度为你想要的tc一半,比如ci1[0] = min(tc[0], tc[1])ci[1] = min(tc[2], tc[3])等,以下应该工作:

module st_genvar();

  int ci1 [0:127];
  int tc [0:255];

  function int minw(int i1, int i2);
      if(i1 < i2 )
        minw = i1;
      else
        minw = i2;
  endfunction

  genvar i;
  //Level 1
  generate
      for (i=0;i<128;i=i+1)
        begin: level1Comp
            assign ci1[i] = minw(tc[i*2],tc[i*2+1]);
        end
  endgenerate

endmodule

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM