An assignment statement assigns a value to a variable or signal. The syntax for a variable assignment follows.
target := expression;
The syntax for a signal assignment follows.
target <= expression;
Target is a variable or signal (or part of a variable or signal, such as a subarray) that receives the value of the expression. The expression must evaluate to the same type as the target. See the Expressions chapter for more information.
The difference in syntax between variable assignments and signal assignments is that variables use := and signals use <=. The basic semantic difference is that variables are local to a process or subprogram, and their assignments take effect immediately.
Signals need not be local to a process or subprogram, and their assignments take effect at the end of a process. Signals are the only means of communication between processes. For more information on semantic differences, see the section of this chapter.
Assignment statements have five kinds of targets.
A assignment target can be either a variable or a signal; the following descriptions refer to both.
The syntax for an assignment to a simple name target with a variable assignment follows.
identifier := expression;
The syntax for an assignment to a simple name target with a signal assignment follows.
identifier <= expression; -- Signal assignment
Identifier is the name of a signal or variable. The assigned expression must have the same type as the signal or variable. For array types, all elements of the array are assigned values.
The example below shows assignments to simple name targets.
variable A, B: BIT;
signal C: BIT_VECTOR(1 to 4);
-- Target Expression
A := '1'; -- Variable A is assigned '1'
B := '0'; -- Variable B is assigned '0'
C <= -1100"; -- Signal array C is assigned
-- -1100"
The syntax for an assignment to an indexed name target with a variable assignment follows.
identifier(index_expression) := expression;
The syntax for an assignment to an indexed name target with a signal assignment follows.
identifier(index_expression) <= expression;
Identifier is the name of an array type signal or variable. Index_expression must evaluate to an index value for the identifier array's index type and bounds but does not have to be computable (see the Computable Operands section of the Expressions chapter), but more hardware is synthesized if it is not.
The assigned expression must contain the array's element type.
In the example below, the elements for array variable A are assigned values as indexed names. The resulting circuit is shown in the figure following the example.
variable A: BIT_VECTOR(1 to 4);
-- Target Expression;
A(1) := '1'; -- Assigns '1' to the first
-- element of array A.
A(2) := '1'; -- Assigns '1' to the second
-- element of array A.
A(3) := '0'; -- Assigns '0' to the third
-- element of array A.
A(4) := '0'; -- Assigns '0' to the fourth
-- element of array A.
The example below shows two indexed name targets. One of the targets is computable and the other is not. Note the differences in the hardware generated for each assignment as shown in the figure following the example.
signal A, B: BIT_VECTOR(0 to 3);
signal I: INTEGER range 0 to 3;
signal Y, Z: BIT;
A <= -0000";
B <= -0000";
A(I) <= Y; -- Noncomputable index expression
B(3) <= Z; -- Computable index expression
Figure 6.1 Circuit for Computable and Noncomputable Indexed Name Targets |
The syntax for a slice target follows.
identifier(index_expr_1 direction index_expr_2)
Identifier is the name of an array type signal or variable. Each index_expr expression must evaluate to an index value for the identifier array's index type and bounds. Both index_expr expressions must be computable (see the Computable Operands section of the Expressions chapter) and must lie within the bounds of the array. Direction must match the identifier array type's direction - either to or downto.
The assigned expression must contain the array's element type.
In the example below, array variables A and B are assigned the same value.
variable A, B: BIT_VECTOR(1 to 4);
-- Target Expression;
A(1 to 2) := -11"; -- Assigns -11" to the first
-- two elements of array A
A(3 to 4) := -00"; -- Assigns -00" to the last
-- two elements of array A
B(1 to 4) := -1100";-- Assigns -1100" to array B
The syntax for a field target follows.
identifier.field_name
Identifier is the name of a record type signal or variable, and field_name is the name of a field in that record type, preceded by a period (.). The assigned expression must contain the identified field's type. A field can be any type, including an array, record, or aggregate type.
The following example assigns values to the fields of record variables A and B.
type REC is
record
NUM_FIELD: INTEGER range -16 to 15;
ARRAY_FIELD: BIT_VECTOR(3 to 0);
end record;
variable A, B: REC;
-- Target Expression;
A.NUM_FIELD := -12; -- Assigns -12 to record A's
-- field NUM_FIELD
A.ARRAY_FIELD := -0011"; -- Assigns -0011" to record
-- A's field ARRAY_FIELD
A.ARRAY_FIELD(3) := '1'; -- Assigns '1' to the most-
-- significant bit of record
-- A's field ARRAY_FIELD
B := A; -- Assigns values of record
-- A to corresponding fields
-- of B
For more information about field targets, see the Record Types section of the Data Types chapter.
The syntax for an assignment to an aggregate target with a variable assignment follows.
([choice => ] identifier
{,[choice =>] identifier}) := array_expression;
The syntax for an assignment to an aggregate target with a signal assignment follows.
([choice =>] identifier
{,[choice =>] identifier}) <= array_expression;
An aggregate assignment assigns array_expression's element values to one or more variable or signal identifiers.
Each choice (optional) is an index expression selecting an element or a slice of the assigned array_expression. Each identifier must have the element type of array_expression. An identifier can be an array type.
An example of aggregate targets follows.
signal A, B, C, D: BIT;
signal S: BIT_VECTOR(1 to 4);
. . .
variable E, F: BIT;
variable G: BIT_VECTOR(1 to 2);
variable H: BIT_VECTOR(1 to 4);
-- Positional notation
S <= ('0', '1', '0', '0');
(A, B, C, D) <= S; -- Assigns '0' to A
-- Assigns '1' to B
-- Assigns '0' to C
-- Assigns '0' to D
-- Named notation
(3 => E, 4 => F,
2 => G(1), 1 => G(2)) := H;
-- Assigns H(1) to G(2)
-- Assigns H(2) to G(1)
-- Assigns H(3) to E
-- Assigns H(4) to F
You can assign array element values to the identifiers by position or by name. In positional notation, the choice => construct is not used. Identifiers are assigned array element values in order, from the left array bound to the right array bound.
In named notation, the choice => construct identifies specific elements of the assigned array. A choice index expression indicates a single element, such as 3. The type of identifier must match the assigned expression's element type.
Positional and named notation can be mixed, but positional associations must appear before named associations.