前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >再谈systemverilog中automatic与static

再谈systemverilog中automatic与static

作者头像
数字IC小站
发布2020-06-30 16:15:10
1.3K0
发布2020-06-30 16:15:10
举报
文章被收录于专栏:数字IC小站数字IC小站

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

本次仿真器是questa sim 10.6c。

上次的传送门在这。

systemverilog之Automatic

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

通过几个栗子看其区别:

ex1:

代码语言:javascript
复制
  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,仿真结果如下:

代码语言:javascript
复制
# @1 auto_cnt = 1
# @2 auto_cnt = 1

ex2:

代码语言:javascript
复制
  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,因此仿真结果如下:

代码语言:javascript
复制
# @1 auto_static_cnt = 1
# @2 auto_static_cnt = 2

ex3:

代码语言:javascript
复制
  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。

仿真结果:

代码语言:javascript
复制
# @1 static_cnt = 1
# @2 static_cnt = 2

ex4:

代码语言:javascript
复制
  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));

仿真结果为:

代码语言:javascript
复制
# @1 static_auto_cnt = 1
# @2 static_auto_cnt = 1

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

ex5:

代码语言:javascript
复制
  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:

代码语言:javascript
复制
(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.

上述代码仿真结果为:

代码语言:javascript
复制
# @1 def_cnt = 1
# @2 def_cnt = 2

ex6:

代码语言:javascript
复制
  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));

仿真结果为:

代码语言:javascript
复制
# @1 def_cnt_auto = 1
# @2 def_cnt_auto = 1

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

代码语言:javascript
复制
   function static int static_cnt(input a);
    int cnt ;
    cnt += a;
    return cnt;
  endfunction

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

代码语言:javascript
复制
# @1 static_cnt = 1
# @2 static_cnt = 2

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

ex7:

代码语言:javascript
复制
    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);

答案就是:

代码语言:javascript
复制
# @1 auto_cnt = 1
# @2 auto_cnt = 2

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-12-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数字IC小站 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档