再谈systemverilog中automatic与static

前段时间写过一篇关于automatic的文章,最近又看到总结一下:

本次仿真器是questa sim 10.6c。

如果变量被声明为automatic,那么进入该方法后,就会自动创建,离开该方法后,就会被销毁;而static则是在仿真开始时就会被创建,直到仿真结束,可以被多个方法/进程共享。

通过几个栗子看其区别:

ex1:

  function automatic int auto_cnt(input a);
    int cnt = 0;
    cnt += a;
    return cnt;
  endfunction
    $display("@1 auto_cnt = %0d", auto_cnt(1));
    $display("@2 auto_cnt = %0d", auto_cnt(1));

定义为automatic后,cnt默认为automatic,仿真结果如下:

# @1 auto_cnt = 1
# @2 auto_cnt = 1

ex2:

  function automatic int auto_static_cnt(input a);
    static int cnt = 0;
    cnt += a;
    return cnt;
  endfunction
    $display("@1 auto_static_cnt = %0d", auto_static_cnt(1));
    $display("@2 auto_static_cnt = %0d", auto_static_cnt(1));

虽然方法定义为automatic,但是因为cnt定义为static,因此仿真结果如下:

# @1 auto_static_cnt = 1
# @2 auto_static_cnt = 2

ex3:

 function static int static_cnt(input a);
    static int cnt = 0;
    cnt += a;
    return cnt;
  endfunction
    $display("@1 static_cnt = %0d", static_cnt(1));
    $display("@2 static_cnt = %0d", static_cnt(1));

在这需要注意的是,虽然static的function隐含其中的变量就是static,因为我们对cnt进行了初始化,所以必须明确指出其是static还是automatic。

仿真结果:

# @1 static_cnt = 1
# @2 static_cnt = 2

ex4:

  function static int static_auto_cnt(input a);
    automatic int cnt = 0;
    cnt += a;
    return cnt;
  endfunction
    $display("@1 static_auto_cnt = %0d", static_auto_cnt(1));
    $display("@2 static_auto_cnt = %0d", static_auto_cnt(1));

仿真结果为:

# @1 static_auto_cnt = 1
# @2 static_auto_cnt = 1

可以看出,即使方法是static的,但是如果我们把变量定义为automatic,每次结束方法就会销毁该变量。

ex5:

  function int def_cnt(input a);
    static int cnt = 0;
    cnt += a;
    return cnt;
  endfunction
    $display("@1 def_cnt = %0d", def_cnt(1));
    $display("@2 def_cnt = %0d", def_cnt(1));

还是请注意,任何隐含为static的方法,如果我们需要对其中的变量进行初始化,一定要指定其是static还是automatic的,否则会报error:

(vlog-2244) Variable 'cnt' is implicitly static. You must either explicitly declare it as static or automatic
# or remove the initialization in the declaration of variable.

上述代码仿真结果为:

# @1 def_cnt = 1
# @2 def_cnt = 2

ex6:


  function int def_cnt_auto(input a);
    automatic int cnt = 0;
    cnt += a;
    return cnt;
  endfunction
    $display("@1 def_cnt_auto = %0d", def_cnt_auto(1));
    $display("@2 def_cnt_auto = %0d", def_cnt_auto(1));

仿真结果为:

# @1 def_cnt_auto = 1
# @2 def_cnt_auto = 1

最后,我们如果不进行初始化,代码如下:

   function static int static_cnt(input a);
    int cnt ;
    cnt += a;
    return cnt;
  endfunction

这时我们不用显式定义其为static,仿真结果如下:

# @1 static_cnt = 1
# @2 static_cnt = 2

最后的最后,看下如果是外部定义的,在automatic的方法中使用的变量会是什么结果:

ex7:

    int cnt = 0;
  function automatic int auto_cnt(input a);  
    cnt += a;
    return cnt;
  endfunction
     auto_cnt(1);
    $display("@1 auto_cnt = %0d",cnt );
    auto_cnt(1);
    $display("@2 auto_cnt = %0d", cnt);
答案就是:
# @1 auto_cnt = 1
# @2 auto_cnt = 2

由此可见,外部变量默认还是static的,不受此影响。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值