systemverilog中的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方法。

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值