4.5 Variable declarations

...
ENTITY e IS
    ...
BEGIN
    ...
END e ;

...
ARCHITECTURE a OF e IS
    ...
BEGIN
    ...
END a ;

...
CONFIGURATION c
        OF e IS
    ...
    ...
    ...
END


...
PACKAGE pkg IS
    ...
    ...
    ...
END pkg ;

variable_declaration ::=

    [ shared ] variable identifier_list :
        subtype_indication [ := expression ] ;


..
PACKAGE BODY pck
        IS
    ...
    ...
    ...
END pck ;

...
b: BLOCK IS
    ...
BEGIN
    ...
END BLOCK b ;

FUNCTION f (...)
    RETURN r IS
    ...
BEGIN
    ...
END f ;

PROCEDURE p (...) IS
    ...
BEGIN
    ...
END p ;

p : PROCESS
    ...
BEGIN
    ...
END PROCESS p ;

4.5.1 Further definitions

identifier_list ::= identifier { , identifier }

subtype_indication ::=
    [ resolution_function_ name ] type_mark [ constraint ]

expression ::=
    relation { and relation }
    | relation { or relation }
    | relation { xor relation }
    | relation [ nand relation ]
    | relation [ nor relation ]

4.5.2 Comment

Shared variables declarations are allowed only in the declarative part of entities, architectures, packages, package bodies and blocks. Shared variables declarations must not be used in processes and subprograms.

More than one process can access shared variables. If several processes access a given shared variable in the same simulation cycle, the sequence of accesses is not prescribed. It is neither the actual value of the variable after the simulation cycle nor the different values of the variable read within the simulation cycle are deterministic. See example below.

4.5.3 Examples

VARIABLE count : positive ;

The variable count of the type positive
is declared.

VARIABLE index : integer
      RANGE 0 TO 99 := 0 ;
VARIABLE memory : bit_matrix
      ( 0 TO 7, 0 TO 1023 ) ;

The variables index and memory are
declared as subtypes of the types integer
and bit_matrix .

VARIABLE address_bus : bit8
      := "00110110" ;
VARIABLE integer_address :
      integer := 2**nb_bit - 1 ;

VARIABLE
bittab : bit_vector (1 TO 9)
      := ( 1 TO 3 => '0', 7 | 9 => 'Z',
          OTHERS => '1' ) ;

VARIABLE
tab : table_type( 0 TO 4 ) :=
      ( 2, 3, 4, -2, 0 ) ;

address_bus is declared as a variable of the type bit8
and initialized with the value "00110110".
integer_address is declared as a variable of the type
integer and initialized with (2**nb_bit-1).

The variable bittab is declared as a subtype of the type
bit_vector and initialized with "000111Z1Z".


The variable tab is declared as a subtype of the type
table_type and initialized with the values 2, 3, 4, -2 and 0.

ARCHITECTURE beh OF sv_example IS
   SHARED VARIABLE counter : integer
      RANGE 0 TO 1 := 0 ;
BEGIN
   p1: PROCESS
   BEGIN
      counter := counter + 1 ;
      wait ;
   END PROCESS p1;
   p2: PROCESS
   BEGIN
      counter := counter - 1;
      wait ;
   END PROCESS p2 ;
END ARCHITECTURE beh;


An example for the non-deterministic of using
shared variables :

A shared variable which is manipulated by two
processes is declared in the architecture.

It is not prescribed, whether
- first p1 increments counter by one (counter = 1) and
   and then p2 decrements counter by one (counter = 0)
- first p2 tries to decrement counter by one, which
   would lead to an error because of the subtype
   violation