systemverilog中automatic与static

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

本次仿真器是questa sim 10.6c。

上次的传送门在这。

systemverilog之Automatic

如果变量被声明为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);
    $display("@2 auto_cnt = %0d", auto_cnt);
定义为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);
    $display("@2 auto_static_cnt = %0d", auto_static_cnt);
虽然方法定义为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);
    $display("@2 static_cnt = %0d", static_cnt);

在这需要注意的是,虽然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);
    $display("@2 static_auto_cnt = %0d", static_auto_cnt);

仿真结果为:
# @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);
    $display("@2 def_cnt = %0d", def_cnt);
还是请注意,任何隐含为static的方法,如果我们需要对其中的变量进行初始化,一定要指定其是static还是automatic的,否则会报error:
(vlog-2244) Variable 'cnt' is implicitly static. You must either explicitly declare it as static&nbs***bsp;automatic
#&nbs***bsp;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);
    $display("@2 def_cnt_auto = %0d", def_cnt_auto);
仿真结果为:
# @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的,不受此影响。

欢迎点赞,关注,分享~~ 本文原发于微信公众号【数字ic小站

全部评论
楼主厉害,太佩服了,写的这个详细,一定是大神一枚
点赞 回复 分享
发布于 2022-07-05 10:33

相关推荐

06-13 17:33
门头沟学院 Java
顺序不记了,大致顺序是这样的,有的相同知识点写分开了1.基本数据类型2.基本数据类型和包装类型的区别3.==和equals区别4.ArrayList与LinkedList区别5.hashmap底层原理,put操作时会发生什么6.说出几种树型数据结构7.B树和B+树区别8.jvm加载类机制9.线程池核心参数10.创建线程池的几种方式11.callable与runnable区别12.线程池怎么回收线程13.redis三剑客14.布隆过滤器原理,不要背八股,说说真正使用时遇到了问题没有(我说没有,不知道该怎么回答了)15.堆的内存结构16.自己在写项目时有没有遇见过oom,如何处理,不要背八股,根据真实经验,我说不会17.redis死锁怎么办,watchdog机制如何发现是否锁过期18.如何避免redis红锁19.一个表性别与年龄如何加索引20.自己的项目的QPS怎么测的,有没有真正遇到大数量表21.说一说泛型22.springboot自动装配原理23.springmvc与springboot区别24.aop使用过嘛?动态代理与静态代理区别25.spring循环依赖怎么解决26.你说用过es,es如何分片,怎么存的数据,1000万条数据怎么写入库中27.你说用limit,那么在数据量大之后,如何优化28.rabbitmq如何批次发送,批量读取,答了延迟队列和线程池,都不对29.计网知不知道smtp协议,不知道写了对不对,完全听懵了30.springcloud知道嘛?只是了解反问1.做什么的?短信服务,信息量能到千万级2.对我的建议,基础不错,但是不要只背八股,多去实际开发中理解。面试官人不错,虽然没露脸,但是中间会引导我回答问题,不会的也只是说对我要求没那么高。面完问我在济宁生活有没有困难,最快什么时候到,让人事给我聊薪资了。下午人事打电话,问我27届的会不会跑路,还在想办法如何使我不跑路,不想扣我薪资等。之后我再联系吧,还挺想去的😭,我真不跑路哥😢附一张河科大幽默大专图,科大就是大专罢了
查看30道真题和解析
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务