Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

generate for loop

4,235 views
Skip to first unread message

bil050

unread,
Jul 20, 2010, 6:28:22 AM7/20/10
to
Hi,
I sit possible to write something like this:
[code]
genvar i,j;
generate for (i = N;i > 0; i = i - 1)
for( j = 0 ; j< i;j = j + 1) begin:dec
......
end
endgenerate

[\code]

Thanks,

John_H

unread,
Jul 20, 2010, 7:00:22 AM7/20/10
to
On Jul 20, 6:28 am, bil050 <irinali...@gmail.com> wrote:
> Hi,
> I sit possible to write something like this:

You just did!

Actually what you're showing would generate N for loops, not N(N+1)/2
statements from your inner loop.

If you want those N(N+1)/2 statements, nest: use two generate for and
two endgenerate statements and you should get what you want.

bil050

unread,
Jul 20, 2010, 7:23:32 AM7/20/10
to

Thank you for your reply!
This will generate two nested genrates?...

bil050

unread,
Jul 20, 2010, 7:28:45 AM7/20/10
to

May be you can show the example for it?

Jonathan Bromley

unread,
Jul 20, 2010, 11:35:31 AM7/20/10
to
On Tue, 20 Jul 2010 04:00:22 -0700 (PDT), John_H wrote:

>Actually what you're showing would generate N for loops, not N(N+1)/2
>statements from your inner loop.

??? I don't really understand that. I am clear that
the OP's code would indeed generate a "triangular array"
with N instances of the inner loop's body from the first i,
N-1 instances for the second and so on. The only mistake
was that the outer loop also needs a begin...end even though
it contains only the one single "for".

>If you want those N(N+1)/2 statements, nest: use two generate for and
>two endgenerate statements and you should get what you want.

I don't think you're allowed to nest generate...endgenerate, and
it is definitely not necessary. Since Verilog-2005 you don't
even need the generate/endgenerate keywords, although I usually
put them in to make my intent clear.

Here's a complete example that actually runs, and displays
enough for you to see what's going on. I've coded it so
that it will work in a Verilog-2001 simulator.

module gen;
parameter N = 3;
genvar i,j;
generate
for (i=N; i>0; i=i-1) begin: outer
for (j=0; j<i; j=j+1) begin: inner
initial $display("%m: i=%0d, j=%0d", i, j);
end
end
endgenerate
endmodule

It's also educational to remove the label from one or other
of the loops, and see what happens. That won't work unless
your simulator supports Verilog-2005 or later, and you'll
see why it's a bad idea anyway. Note carefully the output
from %m in the $displays.
--
Jonathan Bromley

John_H

unread,
Jul 21, 2010, 7:00:13 AM7/21/10
to
On Jul 20, 11:35 am, Jonathan Bromley <s...@oxfordbromley.plus.com>
wrote:

I haven't generated a generate in quite some time but since a
"generate for" is used to replicate the content in its inner loop and
a generate can be inside or outside an always block (can't it?)
wouldn't the result of a single generate block be a repetition of the
internal for statement?

I believe most of my generates came from Verilog-2001.

The untested code I would expect to work would be as follows:

genvar i,j;
generate for (i = N;i > 0; i = i - 1)

generate for( j = 0 ; j< i;j = j + 1)


begin:dec
......
end
endgenerate

endgenerate

I'm happy to be wrong about this.

bil050

unread,
Jul 21, 2010, 8:01:40 AM7/21/10
to
> I'm happy to be wrong about this.- Hide quoted text -
>
> - Show quoted text -

Nested generate are not allowed
the solution I used :
genvar i,j;
generate
for( i = `inputNUM - 1; i > 0; i = i - 1)
begin: chip_loop_out
for ( j = 0; j < i; j = j + 1)
begin: chip_loop_in
...
end
end
endgenerate

Jonathan Bromley

unread,
Jul 21, 2010, 1:36:32 PM7/21/10
to
> I haven't generated a generate in quite some time but since a
> "generate for" is used to replicate the content in its inner loop and
> a generate can be inside or outside an always block (can't it?)

No, it certainly can't. It can be used only at the top level of a
module.

> wouldn't the result of a single generate block be a repetition of the
> internal for statement?

Yes, but that "for" is NOT a procedural for-loop; it's a generate loop
that describes static replication of its contents. The "for" syntax
(and, come to that, "if" and "case") were borrowed for use inside
generates, because there can be no confusion; for/if/case are
procedural constructs only when used inside an always or initial
block.

> I believe most of my generates came from Verilog-2001.

Fair enough, but that doesn't alter the basics. V-2005 made some
things a bit more flexible - you can omit generate/endgenerate
keywords, and begin...end block labels in generates can be omitted and
will be created automatically by the compiler. It also tightened up
the rules about scopes and the behaviour of genvar, but that was
mainly to plug some obscure ambiguities and doesn't affect the
standard uses.

> The untested code I would expect to work would be as follows:
>
> genvar i,j;
> generate for (i = N;i > 0; i = i - 1)
>   generate for( j = 0 ; j< i;j = j + 1)
>     begin:dec
>       ......
>     end
>   endgenerate
> endgenerate
>
> I'm happy to be wrong about this.

Remove the inner generate and endgenerate, and provide a labelled
begin...end on the outer for-loop, and you're OK.
--
Jonathan Bromley

John_H

unread,
Jul 21, 2010, 6:58:26 PM7/21/10
to
On Jul 21, 1:36 pm, Jonathan Bromley <s...@oxfordbromley.plus.com>
wrote:

I appreciate the clarification (since my use is obviously rusty) and
happy that bil050 got his generate working.

0 new messages