首页
学习
活动
专区
圈层
工具
发布
36 篇文章
1
形式验证与formality基本流程
2
安全地启动sequence
3
【UVM COOKBOOK】Testbench Architecture【二】
4
【UVM COOKBOOK】Testbench Architecture【一】
5
svlib文档翻译(第一至四章)
6
svlib文档翻译(第五章)
7
浅谈便携式激励(PSS)和UVM
8
便携式激励vs形式化vsUVM验证方法在IP块的整个生命周期中的比较分析
9
通过字符串访问generate模块内部的变量
10
Verilog:笔试面试常考易错点整理
11
【源码】手把手教你用Python实现Vivado和ModelSim仿真自动化
12
如何快速生成Verilog代码文件列表?(内附开源C代码)
13
IC工程师的通用技能:文本处理
14
NCVerilog+SimVision+Vivado仿真环境搭建
15
串扰
16
论STA | 数字电路中的串扰
17
STA | 串扰,理论分析
18
低功耗 | UPF + CLP
19
combinational clock gating Vs sequential clock gating
20
Clock Domain Crossing, 跨时钟域检查
21
低功耗 | Glitch Power 分析
22
P&R | 如何在实现全流程中考虑IR-Drop
23
点论 | 组合逻辑环 Combinational loop 知多少
24
UVM的一个简单验证demo
25
systemverilog之Automatic
26
【手把手系列】:芯片设计中的Makefile简明教程
27
“ 一网打尽 ” 二进制、格雷码、独热码编码方式
28
分而治之(Hierarchical Sequences),处理复杂事物的绝对准则
29
断言(assertion),把黑盒变成白盒
30
针对assertion based验证的一些“建议”和“不建议”
31
python脚本练习(5):读写文件步骤
32
python脚本练习(4):正则表达式实例
33
python脚本练习(3):正则表达式实例
34
python脚本练习(2):使用正则表达式的三部曲
35
python脚本练习(1):表格打印
36
VCS门级仿真系列文章之sdf文件和$sdf_annotate

systemverilog之Automatic

Function或task的生命期仅见于Verilog语言。Verilog早期仅有静态生命期(static lifetime),无论是function还是task,用来描述硬件,无论调用多少次,同一个Task或者function都是分配一个地址。

这意味着,过程的参数和局部变量,都没有调用堆栈。这是和其它大多数语言完全不同的,需要特别注意。

这也就意味着,你不能有递归和重入的过程。

代码语言:javascript
复制
example1:

`timescale 1ns/1ps
program test();
 // define the function
 function integer factorial (input [31:0] operand);
 if (operand >= 2)
          factorial =factorial (operand - 1) * operand;
 else
          factorial = 1;
 endfunction: factorial
 // test the function
      integer result;
 initial begin
 for (int n = 0; n <= 5; n++) begin
          result = factorial(n);
          $display("%0d factorial=%0d", n, result);
 end
 end
endprogram

结果如下:

原因是因为这个代码实现的是个递归函数,对于静态变量,只分配一次地址,所有的值在同一个地址计算,因此展开后是无法计算的,计算方式是P=P*n。因此输出的值就是展开后不需要递归的那个值,也就是1.

解决办法是添加automatic,这样就相当于把所有Task和function都放到堆栈中,可以展开计算,自动添加下标。计算方式是p2=p1*n

代码语言:javascript
复制
`timescale 1ns/1ps
program automatic test();
 // define the function
 function integer factorial (input [31:0] operand);
 if (operand >= 2)
          factorial =factorial (operand - 1) * operand;
 else
          factorial = 1;
 endfunction: factorial
 // test the function
      integer result;
 initial begin
 for (int n = 0; n <= 5; n++) begin
          result = factorial(n);
          $display("%0d factorial=%0d", n, result);
 end
 end
endprogram

example2:

代码语言:javascript
复制
program test();
 task add(int a, int b);
    #3;
    $display("the sum is %0d", a+b);
 endtask
 initial
 fork
 begin
        add(2,3);
 end
 begin
        #1;
        add(3,4);
 end
 join
endprogram

结果如下:

原因在于两次值存储的位置是一个地方,因此第二次把第一次覆盖了,打印出同一个值。

如果加上automatic,那么系统自动添加下标,放到堆栈中,相当于两个add,一个add1,一个add2.不会覆盖

修改后代码如下:

代码语言:javascript
复制
program automatic test();
 task add(int a, int b);
    #3;
    $display("the sum is %0d", a+b);
 endtask
 initial
 fork
 begin
        add(2,3);
 end
 begin
        #1;
        add(3,4);
 end
 join
endprogram

仿真结果如下:

因此,在Systemverilog中,我们最好默认加上automatic,除非在极为特殊的情况下才不加,否则很容易出现意外情况。

下一篇
举报
领券