Translate

Monday, November 28, 2011

Sequential statements

   In order to use sequential statements we need a special concurrent structure called a process because in an architecture body sequential code is not allowed so first let's see what a process is and how it fits in the description of a circuit.
    Here's a diagram of a VHDL program with processes:
    As you can see an entire process can be thought as a concurrent statement and the code inside it is sequential. This means that 2 processes execute concurrently relative to each other and other concurrent code but the statements inside them are sequential.
    A process has the following structure:
              name: process(sensitivity list)
                   variable declarations
              begin
                   sequential statements
              end process;

    The name: part is not mandatory but it helps you find a certain process when you have lots of them in the same vhdl file, although the same thing can be achieved by using comments. Variable declarations are also optional and we will get to them later in the tutorial.
    Let's see what the sensitivity list is. Basically a process executes only when it is triggered by an event (a signal that changes value) so we need to put all the signals that make the internals of that process in the list. Let's take for example a simple 3 input xor function (r<= a xor b xor c). The process for this code must have in it's sensitivity list all 3 input signals because we want the result r to be updated every time one of the inputs changes value. If for example in the list we would put only a then the result would be updated when a changes value but not when b or c change their values. This is a very important thing to remember for when you will design complex circuits. The process for this function looks like this:

              process(a,b,c)
              begin
                   r <= a xor b xor c;
             end process;
    
    Variables are used only in processes. A variable declared in a process is local to that process so you can have more processes with the same variables in a vhdl file. A variable can have all the data types a signal has and it is not that different from signals. Let's see how to declare and use a variable:

              variable count : integer;
              ...................................
              count := count +1;
              ...................................

    So the big difference between a signal and a variable is that a variable is assigned a value with  := .
    
    Sequential statements
    1)  Signal assignments:
         This is the same as in concurrent code but you have to be careful with the order in witch you put the statements because they execute one after the other.

              signal <= value;
   
    2)  Variable assignments:

              variable := value;

    3)  The if statement:
          This is a conditional statement in witch a block of sequential code is executed if a condition is true. The structure of the if statement is:

              if (expression 1) then
                  sequential block 1
              elsif (expression 2) then
                  sequential block 2
             ...............................
             else sequential block n
             end if;

          Another way to write an if statement is:

              if (expression 1) then
                  sequential block 1
              else
                  if (expression 2) then
                      sequential block 2
                  else sequential block 3
                  end if;
              end if;

          So in an if statement the first expression is evaluated and if it is true then the code attached to it is executed. If it is not true the next statement is evaluated and so on. 
          Let's see how the multiplexer from the last tutorials can be designed using the if statement:

              process (s,i0,i1,i2,i3)
              begin
                  if (s="00") then 
                      o <=  i0;
                  elsif (s="01") then
                      o <= i1;
                  elseif (s="10") then
                      o <= i2;
                 else o <= i3;
                 end if;
              end process;

          As you can see in the sensitivity list I have included s and the 4 inputs so that even if s doesn't change, the output will be updated with each input change. In order to test the code replace the concurrent statements from the other examples with the process. The entity is the same.

    4)  The case statement:
          This is similar to the with select statement. Let's see the structure:

              case expression is
                  when value 1 =>
                      sequential block 1
                   when value 2 =>
                       sequential block 2
                   ..............................
                   when value n =>
                       sequential block n
                   when others =>
                       other sequential block
              end case;

          The case is simple: a certain block of sequential code is executed based on the value of expression. Let's see the multiplexer with the case statement:

              process (s,i0,i1,i2,i3)
              begin
                  case s is
                      when "00" =>
                          o <= i0;
                      when "01" =>
                          o <= i1;
                      when "10" =>
                          o <= i2;
                      when others =>
                          o <= i3;
                  end case;
              end process;

    5)  The for loop:
          The for loop executes a sequential block of code over and over again for a number of times. It isn't used as much as the other 2 statements so that's why for now I won't be making an example for it. The structure of o for loop is:
             
              for index in range loop
                  sequential block
              end loop;

          We will use for loops in later tutorials.    
         
          
   

No comments:

Post a Comment