SV——automatic

參考:

IEEE 1800 6.21 Scope and lifetime

 

1. SV中變量存儲

  1. Variables declared outside a module, program, interface, checker, task, or function are local to the compilation unit and have a static lifetime (exist for the whole simulation).

  2. Variables declared inside a module, interface, program, or checker, but outside a task, process, or function, are local in scope(局部有效) and have a static lifetime.

  3. Variables declared inside a static task, function, or block are local in scope and default to a static lifetime.

  4. Tasks and functions may be declared as automatic. Variables declared in an automatic task, function, or block are local in scope, default to the lifetime of the call or block(automatic類型的變量的生存週期是函數調用時間或者塊語句的生存週期), and are initialized on each entry to the call or block。

  5. An automatic block is one in which declarations are automatic by default. Specific variables within an automatic task, function, or block can be explicitly declared as static. Such variables have a static lifetime.

上面前三條是一個意思,變量都是存在靜態存儲區,如果有多個線程同時調用任務或者函數,任務或函數內地變量是共享的,這樣會發生問題。

後面兩條是一個意思,變量存在動態存儲區,每次對任務或者函數的調用都會分配動態存儲區域來存儲變量,任務或者函數執行完,自動釋放動態存儲區。

 

1800中的例子

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

module top_legal;

int svar1 = 1; // static keyword optional

initial begin

    for (int i=0; i<3; i++) begin

        automatic int loop3 = 0; // executes every loop

        for (int j=0; j<3; j++) begin

        loop3++;

        $display(loop3);

        end

    end // prints 1 2 3 1 2 3 1 2 3

    for (int i=0; i<3; i++) begin

        static int loop2 = 0; // executes once at time zero

        for (int j=0; j<3; j++) begin

            loop2++;

            $display(loop2);

        end

    end // prints 1 2 3 4 5 6 7 8 9

end

endmodule : top_legal

module top_illegal; // should not compile

initial begin

    int svar2 = 2; // static/automatic needed to show intent

    for (int i=0; i<3; i++) begin

        int loop3 = 0; // illegal statement

        for (int i=0; i<3; i++) begin

            loop3++;

            $display(loop3);

        end

    end

end

endmodule : top_illegal

// 其實int loop3=2,不加static也不加automatic輸出是 1 2 3 1 2 3 1 2 3;不知道上面爲什麼說是illegal statement

 

注意:

  Class methods (see Clause 8) and declared for loop variables (see 12.7.1) are by default automatic,regardless of the lifetime attribute of the scope in which they are declared.

  類方法和for循環中的變量默認類型就是automatic,這應該也就是上面爲什麼不加automatic也輸出1 2 3 1 2 3 1 2 3的原因吧。

   在SV中的方法,如果是ref類型,那麼必須是automatic方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章