Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 37 ยท Concurrent and Sequential Code

if, case and Loops in VHDL

Write sequential VHDL: if/elsif/else, case with when others, for and while loops, exit and next, and how loops unroll into hardware instead of running over time.

Inside a process you can use familiar control structures: if, case and loops. They look like software, but remember what they become: an if is a multiplexer or enable, a case is a multiplexer or decoder, and a loop is unrolled into parallel hardware. Nothing "runs through" a loop over time in synthesized logic.

if / elsif / else

VHDL
if a > b then
  max <= a;
elsif a = b then
  max <= a;
else
  max <= b;
end if;

Note the keyword elsif (not else if) and the closing end if;. Conditions are checked in order, so the structure implies priority, like when ... else. The condition must be of type boolean: comparisons give a boolean, but a bare std_logic does not, so write if en = '1' then. (VHDL-2008 allows if en then through the ?? condition operator, which is often avoided for clarity.)

case

case selects one branch based on the value of one expression. Unlike if, there is no priority, and all choices must be covered exactly once:

VHDL
case opcode is
  when op_add => result <= a + b;
  when op_sub => result <= a - b;
  when op_and => result <= a and b;
  when others => result <= (others => '0');
end case;

Rules:

  • Choices can be single values (when "00"), alternatives (when "01" | "10"), or ranges for integers (when 0 to 3).
  • when others is required unless every value is listed. For std_logic_vector selectors, others is almost always needed because of 'U', 'X' and friends.
  • For a case on an std_logic_vector, the choice values must be of the same length as the expression.

Use case for state machines and decoders, where it reads like a table.

C Stand-in (C): a small ALU with case

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

(OP_XOR falls into default, like a missing opcode falling into when others.)

for loops

VHDL
process (all)
  variable count : natural range 0 to 8;
begin
  count := 0;
  for i in data'range loop
    if data(i) = '1' then
      count := count + 1;
    end if;
  end loop;
  ones <= to_unsigned(count, 4);
end process;

The loop parameter i is created by the loop, takes each value in the range, and cannot be assigned. The range must be static (known at synthesis): data'range, 0 to 7 or a constant. The tool unrolls the loop: eight copies of the if and an adder chain. The longer the loop, the larger and slower the circuit, which is a key difference from software.

while loops, exit and next

VHDL
while n > 0 loop
  n := n - 1;
end loop;

for i in 0 to 15 loop
  next when data(i) = '0';       -- skip to the next iteration
  first := i;
  exit;                          -- leave the loop
end loop;

while loops are synthesizable only if the tool can determine the iteration count at compile time, so in hardware you will mostly use for. In testbenches there is no such limit.

A search loop that finds the lowest set bit is a good example. The stand-in below does the same in C, and also shows the "unrolled" idea by testing all bits independently and combining the results:

C Stand-in (C): population count and first set bit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Loops are not time

This stand-in compares how many "operations" a loop costs in software time with the hardware picture: a software loop of 8 steps takes 8 clock cycles on a CPU, while the unrolled hardware performs all 8 bit tests at once and just needs a longer adder chain.

C Stand-in (C): sequential time vs unrolled width

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Writing the loop body so a tool can build a balanced tree instead of a long chain is a typical optimization. Modern synthesizers often do it for you, but check the timing report for wide loops.

Common mistakes

  • Using else if instead of elsif produces an unexpected nested if that also needs its own end if;.
  • Forgetting when others in a case.
  • Writing a for loop whose bound is a signal value (not static), which tools reject.
  • Assigning a signal in a loop and then reading it in the same iteration, expecting the new value (see Signals and Variables).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which keyword is correct for the middle branch of an if?

    VHDL uses elsif, and one end if; closes the whole statement.
  2. 2. What must a case statement on an std_logic_vector almost always include?

    Std_logic has nine values, so listing 0 and 1 patterns is not exhaustive; others covers the rest.
  3. 3. What does a synthesizable for loop become in hardware?

    Loops are unrolled at synthesis time, so large loops mean large circuits.
  4. 4. Why is for i in 0 to n loop problematic if n is a signal?

    The tool must know how many copies to build, so bounds have to be constants or generics.

Summary

  • if/elsif/else implies priority; case has no priority and must cover all values.
  • Always include when others for non-exhaustive selectors.
  • for loops with static bounds are unrolled into parallel hardware; exit and next control flow in loops.
  • Loops are not time: size and delay grow with iteration count.

Next: Generate Statements