Chapter 4 - Behavioral Descriptions

Section 2 - Using Variables

There are two major kinds of objects used to hold data. The first kind, used mostly in structural and data flow descriptions, is the signal. The second, which can only be used in processes is called a variable. A variable behaves like you would expect in a software programming language, which is much different than the behavior of a signal.

Although variables represent data like the signal, they do not have or cause events and are modified differently. Variables are modified with the variable assignment. For example,

a:=b;
assigns the value of b to a. The value is simply copied to a immediately. Since variables may only be used in processes, the assignment statement may only appear in a process. The assignment is performed when the process is executed, as explained in the last section.

The following example shows how a variable is used in a process.

count: process (x)
  variable cnt : integer := -1;
begin
  cnt:=cnt+1;
end process;
Variable declarations appear before the begin keyword of a process statement as in the example. The variable declaration is the same as the signal declaration except the key word variable is used instead of signal. The declaration in this example includes an optional part, which specifies the initial value of the variable, when a simulation begins. The initialization part is included by adding the := and some constant expression after the type part of the declaration. This initialization part may also be included in signal declarations. The variable cnt is declared to be of the type integer. The integer type represents negative and positive integer values.

The process in the example contains one statement, the assignment statement. This assignment computes the value of cnt plus one and immediately stores that new value in the variable cnt. Thus, cnt will be incremented by one each time this process is executed. Remember, from the last section, that a process is executed once at the beginning of simulation and then each time an event occurs on any signal in its sensitivity list. Since the value is initialized to -1, and the process is executed once before beginning simulation, the value of cnt will be 0 when simulation begins. Once simulation begins, cnt will be incremented by one each time the signal x changes, since x is in the sensitivity list. If x is a bit signal, then this process will count the number of rising and falling edges that occur on the signal x.

The previous section is Behavioral Descriptions - The Process Statement.
The next section is Behavioral Descriptions - Sequential Statements.


Copyright 1995, Green Mountain Computing Systems.
Copying this document is strictly prohibited. Making any non-volatile or semi-permanent copies of this document is a violation of international copyright laws.