这段代码中B和C的正确值是多少?

问题描述 投票:0回答:1
class A;
    function int foo();
        int a;
        return ++a;
    endfunction
  
endclass

program tb;
    A a =new;
    int b, c;

    initial begin
        for (int i = 0; i < 10; i++) begin
            b = a.foo();
            c = foo(); // Calls a different foo() outside of the class A
            $display("B = %0d", b);
            $display("C = %0d", c);
        end
    end

    function int foo();
        int a;
        return ++a;
    endfunction
endprogram

我认为,由于 a 正在初始化,因此每个函数调用都将在整个 for 循环中 B 和 C 值都变为 1。但我在在线 EDA 游乐场上运行了这个,它打印出了这个结果。

B = 1
C = 1
B = 1
C = 2
B = 1
C = 3
B = 1
C = 4
B = 1
C = 5
B = 1
C = 6
B = 1
C = 7
B = 1
C = 8
B = 1
C = 9
B = 1
C = 10
$finish at simulation time                    0
           V C S   S i m u l a t i o n   R e p o r t

有人可以解释一下 C 的值是如何递增的吗?

verilog system-verilog
1个回答
0
投票

foo
函数是一个自动函数。

程序

foo
函数是静态函数。

这种差异导致了 2 个函数的行为差异。

program
中,如果您将函数显式声明为
automatic
,它将像类中的函数一样运行。变化:

function int foo();

至:

function automatic int foo();

输出:

B = 1
C = 1
B = 1
C = 1
B = 1
C = 1
B = 1
C = 1
B = 1
C = 1
B = 1
C = 1
B = 1
C = 1
B = 1
C = 1
B = 1
C = 1
B = 1
C = 1
© www.soinside.com 2019 - 2024. All rights reserved.