The difference between static and automatic variables


We were discussing the difference between static and automatic variables in a SystemVerilog class.

Whether a variable is static and automatic is called its lifetime. IEEE 1800-2012 gives the syntax for a variable declaration:

data_declaration10 ::= [ const ] [ var ] [ lifetime ] data_type_or_implicit list_of_variable_decl_assignments ; | type_declaration

A static variable exists for the whole simulation; an automatic variable exists only for the lifetime of the task, function or block - they are created when the task, function or block is entered and destroyed when it is left.  This has consequences:
  • An automatic variable is initialized every time the task, function or block containing it is entered. When you think about it, how could it be any other way? The variable didn't exist until the task, function or block was entered. A static variable already exists before the task, function or block is entered and so is not initialized when that task, function or block is entered.
Here is some code that illustrates the difference between static and automatic variables:

module M; 
  initial begin
    static    int os_count_s = 1;
    automatic int os_count_a = 1;
    do
      begin
        static    int is_count_s = 1;
        automatic int is_count_a = 1;
        #1ns $display("os_count_s = ", os_count_s);
             $display("os_count_a = ", os_count_a);
             $display("is_count_s = ", is_count_s);
             $display("is_count_a = ", is_count_a);
        os_count_s += 1;
        os_count_a += 1;
        is_count_s += 1;
        is_count_a += 1;
      end
    while ($time < 10ns);
  end
endmodule       


https://www.edaplayground.com/x/4yxv

If we run this code (eg on EDA Playground) then we see that is_count_a the automatic variable inside the loop is constantly re-initialized to 1, whereas the static variable inside the loop - is_count_s - is only initialized once (at time zero). We also see that the behavior of the variables declared outside the loop ( os_count_s and os_count_a ) is identical:

os_count_s = 1
os_count_a = 1
is_count_s = 1
is_count_a = 1
os_count_s = 2
os_count_a = 2
is_count_s = 2
is_count_a = 1
os_count_s = 3
os_count_a = 3
is_count_s = 3
is_count_a = 1
os_count_s = 4
os_count_a = 4
is_count_s = 4
is_count_a = 1
os_count_s = 5
os_count_a = 5
is_count_s = 5
is_count_a = 1
os_count_s = 6
os_count_a = 6
is_count_s = 6
is_count_a = 1
os_count_s = 7
os_count_a = 7
is_count_s = 7
is_count_a = 1
os_count_s = 8
os_count_a = 8
is_count_s = 8
is_count_a = 1
os_count_s = 9
os_count_a = 9
is_count_s = 9
is_count_a = 1
os_count_s = 10
os_count_a = 10
is_count_s = 10
is_count_a = 1

  
A customer asked me about this. I didn't know the answer, so I wrote a few lines of code on EDA Playground. EDA Playground is great for that, because it's always on. You don't have to queue for licences, wait for EDA tools to start, create new files, fire up editors...

Comments

Popular posts from this blog

Bit Width Casting in SystemVerilog

How to convert a std_logic_vector to a hex string?

Can you add/remove more than 1 item from a queue?