Vhdl: How Can I Execute the for Loop Once in Process Statement

I am new to vhdl and FPGA. For my project I need to use for loop. When I simulate my code it keeps counting and not stopping. I want my code to execute only once, my point is when the loop reaches it's limit code will stop executing. Here is my code.

process(clk)
begin
        
        report "Hello";
      
        for j in 0 to 3 loop 
            if (j<4) then
              cA <= cA + 1;
              report "middle";
              exit;

            end if;
        end loop;  
        
        report "done";
      
 end process;
1

1 Answer

As probably explained in your textbook, a VHDL process is an infinite loop. After the last instruction has been executed, the first one executes.

A process with sensitivity list (clk in your case) is equivalent to the same process without sensitivity list but with a final wait statement on the list. Your process is thus equivalent to:

process
begin    
  report "Hello";
  for j in 0 to 3 loop 
    if (j<4) then
      cA <= cA + 1;
      report "middle";
      exit;
    end if;
  end loop;  
  report "done";
  wait on clk;
end process;

So, your loop executes a first time at the very beginning of the simulation and the process suspends, waiting for a clk change. It resumes each time clk changes, executes the loop again, and suspends. If your clock never stops, your process will always be resumed twice a clock period (once on the rising edge and once on the falling edge).

If you want to execute this loop only once at the very beginning of the simulation, remove the sensitivity list and add wait as last statement. A simple wait means "wait forever". It suspends the process definitely:

process
begin    
  report "Hello";
  for j in 0 to 3 loop 
    if (j<4) then
      cA <= cA + 1;
      report "middle";
      exit;
    end if;
  end loop;  
  report "done";
  wait;
end process;

If, instead, you want to execute the loop and stop only after a particular event happened, say a rising edge of the clock where signal go is asserted high, add another wait statement at the beginning of the process:

process
begin
  wait until rising_edge(clk) and go = '1';
  report "Hello";
  for j in 0 to 3 loop 
    if (j<4) then
      cA <= cA + 1;
      report "middle";
      exit;
    end if;
  end loop;  
  report "done";
  wait;
end process;
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest