EmbDev.net

Forum: FPGA, VHDL & Verilog Why can't I set a (genvar) outside the control section of a loop?


von Kevin S. (kvnsmnsn)


Rate this post
useful
not useful
I just wrote a design in Verilog that takes two operands as input, 
(leftOp) and (rightOp),  and outputs one operand, (result), where the 
common length of all operands is defined by parameter (nmBits), that 
defaults to 1. I want to loop through the bits in (result), and make 
each the exclusive or of the bit on the opposite side on (leftOp) and 
(rightOp). My code is:
1
module rbwx ( result, leftOp, rightOp);
2
parameter nmBits = 1;
3
integer highBit  = nmBits - 1;
4
output [ highBit:0] result;
5
input  [ highBit:0] leftOp;
6
input  [ highBit:0] rightOp;
7
8
genvar ix, opp;
9
10
for (ix = 0; ix < nmBits; ix = ix + 1)
11
begin
12
  opp = highBit - ix;
13
  xor2 x2( result[ ix], leftOp[ opp], rightOp[ opp]);
14
end
15
16
endmodule
When I run Icarus on this I get:
1
D:\Hf\Verilog\Unpacked\Common>\Icarus\bin\iverilog -g2009 -o rbwx.out rbwx.sv
2
rbwx.sv:12: syntax error
3
rbwx.sv:12: error: Invalid module instantiation
4
5
D:\Hf\Verilog\Unpacked\Common>
So my design is generating a syntax error right where I assign (opp) to 
the difference between (highBit) and (ix). Why can't I do this, since 
(opp) is defined as a (genvar)? Can I only use (genvar)s in the control 
section of a loop?

von Klakx (Guest)


Rate this post
useful
not useful
I think there should also be a generate / endgenerate statement placed 
around the for loop.

von Kevin S. (kvnsmnsn)


Rate this post
useful
not useful
Klakx wrote:
> I think there should also be a generate / endgenerate statement placed
> around the for loop.

Accordingly I copied "rbwx.sv" to "klakx.sv" to get:
1
module klakx ( result, leftOp, rightOp);
2
parameter nmBits = 1;
3
integer highBit  = nmBits - 1;
4
output [ highBit:0] result;
5
input  [ highBit:0] leftOp;
6
input  [ highBit:0] rightOp;
7
8
genvar ix, opp;
9
10
generate
11
  for (ix = 0; ix < nmBits; ix = ix + 1)
12
  begin
13
    opp = highBit - ix;
14
    xor2 x2( result[ ix], leftOp[ opp], rightOp[ opp]);
15
  end
16
endgenerate
17
18
endmodule
and then I tried this with Icarus and got:
1
D:\Hf\Verilog\Unpacked\Common>\Icarus\bin\iverilog -g2009 -o klakx.out klakx.sv
2
klakx.sv:13: syntax error
3
klakx.sv:13: error: Invalid module instantiation
4
5
D:\Hf\Verilog\Unpacked\Common>
Looks like the enclosing the (for) loop with (generate) and 
(endgenerate) didn't fix the problem.

von Klakx (Guest)


Rate this post
useful
not useful
"syntax error" ... icarus is very shy compiler...

localparam is the solution:
1
module klakx ( result, leftOp, rightOp);
2
parameter nmBits = 1;
3
localparam integer highBit  = nmBits - 1; // here
4
output [ highBit:0] result;
5
input  [ highBit:0] leftOp;
6
input  [ highBit:0] rightOp;
7
8
genvar ix; // remove opp here
9
10
generate
11
  for (ix = 0; ix < nmBits; ix = ix + 1)
12
  begin
13
    localparam opp = highBit - ix; // here
14
    xor2 x2( result[ ix], leftOp[ opp], rightOp[ opp]);
15
  end
16
endgenerate
17
18
endmodule

von Kevin S. (kvnsmnsn)


Rate this post
useful
not useful
Klakx, thanks for that last little bit! It's working just fine now.

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.