Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
13 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

    To write efficient code for a synchronous counter, just type

    c <= c +1;

    If you want to write low level code for some reason, e.g. as an exercise, you are restricted to synthesizable VHDL constructs.

    You probably have noticed that not all legal VHDL constructs are synthesizable. They can be however used in testbenches.

    Because iterations are enrolled to parallel hardware, it's essential that the number of iterations is constant and can be determined at "compile" time. The while statement in your example has a constant number of iterations, but other possible while statements don't. For this reason, synthesis tools don't support it.

    For Quartus, the supported VHDL constructs are listed in the online help. The selection is mostly identical to IEEE 1076.6, ieee standard for vhdl register transfer level (rtl) synthesis.

    P.S.: Regarding "efficient code". Even if your code is synthesizable, there's a certain risk that it's not recognized as arithmetic operation and thus won't utilize the fast carry chain. In this case, the resulting hardware will be considerably less efficient, slower and consuming more logic cells than that generated by the simple c <= c +1.