Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
12 years ago

processes: while loop does not work, for loop does work. Why?

I want to write this code for a basic synchronous counter more efficiently...

u_count: process(rst_i, clk_i) -- generated on rising edge

variable i: integer :=1;

begin

if rst_i = '1' then

c <= "00000000000000000";

elsif (clk_i ='0' and clk_i'event) then

c(0) <= c(0) xor cyi;

c(1) <= c(1) xor t(0); -- t(n) terms are propagate terms defined earlier as dataflow statements.

c(2) <= c(2) xor t(1);

..

c(16) <= c(16) xor t(15);

end if;

end process;

This code below for a loop counter works (good to know)

u_count: process(rst_i, clk_i) -- generated on rising edge

variable i: integer range 1 to 16;

begin

if rst_i = '1' then

c <= "00000000000000000";

elsif (clk_i ='0' and clk_i'event) then

c(0) <= c(0) xor cyi;

for i in 1 to 16 loop

c(i) <= c(i) xor t(i-1);

end loop;

end if;

end process;

---------------------------------------------------------------------

And this code below with the most basic while loop does not

Error (10536): loop must terminate within 10,000 iterations

u_count: process(rst_i, clk_i) -- generated on rising edge

variable i: integer :=1;

begin

if rst_i = '1' then

c <= "00000000000000000";

elsif (clk_i ='0' and clk_i'event) then

c(0) <= c(0) xor cyi;

while (i <= 16) loop

c(i) <= c(i) xor t(i-1);

i:=i+1; -- how can i get past 16 to 10,000?

end loop;

end if;

end process;

Lots of people get this error, but they don't present a simple, bullet-proof example that does not have some other quirk that obfuscates this issue, so I never could find an explanation for the error.

Thanks.

12 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The initializing statement sets the value only once. But it must be set every time.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    As far as I know, it is almost always discouraged to write a while loop, if it is possible to make it a for loop, in any language.

    This is because it is easy to make a mistake with a while loop, so that it never ends, software nor hardware will like this.

    Ofcourse you could also write a neverending forloop, but a forloop always has at least some exit statement.

    So wether it compiles or not, if you can write it as a for loop, just do.