Saturday, June 14, 2014

Decisions and Loops in VHDL

[1] If- Then - Else
The basic syntax for a simple if statement is as follows:

            signal (condition) then
           ...statements
      end if;

The condition is a Boolean expression, of the form a > b or a = b. Note that the comparison operator for equality is a single =, not to be confused with the double == used in some programming languages. For example, if two signals are equal, then set an output high would be written in VHDL as:

            if (a = b) then
            out1 <= `1';
      end if;
If the decision needs to have both the if and else options, then the statement is extended as follows:
      
      if (condition) then
           ...statements
      else
           ...statements     
      end if;
So in the previous example, we could add the else statements as follows: 

            if (a = b) then
           out1 <= `1';
      else
           out1 <= `0';     
      end if;

And finally, multiple if conditions can be implemented using the general form:
      
      if (condition1) then
           ...statements
      elseif (condition2)
           ...statements 
           ...more elsif conditions & statements     
      else
           ...statements
      end if;

With an example: 
      
      if (a > 10) then
           out1 <= `1';
      elseif (a > 5) then
           out1 <= `0';    
      else
           out1 <= `1';
      end if;


[2] Case
IF statement is relatively simple to define multiple conditions, but it becomes a little cumbersome, and so the case statement offers a simple approach to branching, without having to use Boolean conditions in every case. This is especially useful for defining state diagrams or for specific transitions between states using enumerated types. An example of a case statement is:

      case testvariable is
         when 1 =>
            out1 <= '1';
     
         when 2 =>
            out2 <= '1';

         when 3 =>
            out3 <= '1';
      end case;



This can be extended to a range of values, not just a single value;

      case test is
         when 1 to 4 => out1 <= '1';
           
It is also possible to use Boolean conditions and equations. In this case of the default option (i.e., when none of the condition have been met), then the term when others can be used;

      case test is
         when 0 => out1 <= '1';
         when others => out1 <= '0';
      end case;


[3]  For
The most basic loop in VHDL is the FOR loop. This is a loop that executes a fixed number of times. The basic syntax for the FOR loop is shown below:
            for loopvar in start to finish loop           
         ... loop statements
      end loop;

It is also possible to execute a loop that counts down rather than up, and the general form of this loop is:

            for loopvar in start downto finish loop           
         ... loop statements
      end loop;

A typical example of a for loop would be to pack an array with values bit by bit, for example:

            signal a : std_logic_vector (7 downto 0);
      for i in 0 to 7 loop          

         a(i) <= '1';
      end loop;


[4]  While and Loop
Both the while and loop loops have an in-determinant number of loops, compared to the fixed number of loops in a FOR loop and as such are usually not able to be synthesized. For FPGA design, they are not feasible as they will usually cause an error when the VHDL model is compiled by the synthesis software.


[5]  Exit
The exit command allows a FOR loop to be exited completely. This can be useful when a condition is reached and the remainder of the loop is no longer required. The syntax for the exit command is shown below:

            for i in 0 to 7 loop
           if (i = 4) then          

              exit;
          endif;
      endloop;


[6]  Next
The next command allows a FOR loop iteration to be exited; this is slightly different from the exit command in that the current iteration is exited, but the overall loop continues onto the next iteration. This can be useful when a condition is reached and the remainder of the iteration is no longer required. An example for the next command is shown below:

           for i in 0 to 7 loop
           if (i = 4) then          

              next;
          endif;
      endloop;




References:

  • FPGAs World Class Designs, Clive "Max" Maxfield, Elsevier, 2009  
  • http://#.com (source of image)

No comments:

Post a Comment