静态变量 动态变量
module top;
class Transaction;
static int count = 0; // 静态变量
int id; // 动态变量
function new();
id = count++; // 先赋值id = count, 再count++(count等于+1后的值,count++等于count+1前的值)
endfunction
endclass
Transaction t1,t2; // 静态变量在声明时就应该对其初始化 count = 0;
initial begin
// 动态变量是类在实例化时,即调用构造函数new( )才会初始化
t1 = new(); // t1 中:count = 1,id =0 (先赋值id=count=0,再count++)
t2 = new(); // t2 中:count = 2,id =1 (先赋值id=count=1,再count++)
$display("t2 count=%0d and id=%0d",t2.count,t2.id);
$display("Transaction count=%0d",Transaction::count);
end // Transaction类中:count = 2
endmodule
// 父类static变量只在
module top;
class A;
integer public_data ;
local integer local_data;//这个local就相当于C++的私有类型数据
protected integer protected_data;
static integer static_data; // 父类静态变量
function new();//类似C++中的构造函数
begin
public_data = 1;
local_data = 2;
protected_data = 3;
static_data = 0;
end
endfunction
task printA();//task就是多个控制延时功能的function
begin
$write ("value of public_data %0d in A\n", public_data );
$write ("value of local_data %0d in A\n", local_data);
$write ("value of protected_data %0d in A\n", protected_data);
end
endtask
endclass
class B extends A;
task printB();
begin//B extends A 相当于C++的公有继承
$write ("value of public_data %0d in B\n", public_data );
// Below line will give compile error
//$write ("value of local_data %0d in B\n", local_data);
$write ("value of protected_data %0d in B\n", protected_data);
end
endtask
endclass
class C;
A a;
B b;
function new();
begin
a = new();
b = new();
b.public_data = 2;//b公有继承于A类,所以自然能够去修改A们共有的static类型数据
end
endfunction
task printC();
begin
$write ("value of public_data %0d in C\n", a.public_data );
$write ("value of public_data %0d in C\n", b.public_data );
// Below line will give compile error
//$write ("value of local_data %0d in C\n", a.local_data);
//$write ("value of protected_data %0d in C\n", a.protected_data);
//$write ("value of local_data %0d in C\n", b.local_data);
//$write ("value of protected_data %0d in C\n", b.protected_data);
end
endtask
endclass
initial begin
C c = new();
c.a.printA();
c.b.printB();
c.printC();
$write("value of static_data is %0d\n",c.a.static_data); // 子类静态变量与父类同值
$write("value of static_data is %0d\n",c.b.static_data);
c.a.static_data ++; // 父类静态变量改变,子类也跟着改变
$write("value of static_data is %0d\n",c.a.static_data);
$write("value of static_data is %0d\n",c.b.static_data);
c.b.static_data ++; // 子类静态变量改变,父类也跟着改变
$write("value of static_data is %0d\n",c.a.static_data);
$write("value of static_data is %0d\n",c.b.static_data);
end
endmodule
静态方法 动态方法
module内的方法默认是static。
方法被static修饰,那么其内部所有的声明的变量都是 static 的,在仿真开始时即会被创建。
方法被修饰为 automatic,那么其内部所有的声明的变量默认都是 automatic的。在进入该方法后,automatic变量会被创建,而离开该进程/方法后就被销毁。
module static_test;
function automatic int cnt1(input a); // 函数内所有的变量均为automatic
int cnt = 0; // 这个变量在每次调用时都会初始化
cnt += a ;
return cnt;
endfunction
function static int cnt2(input a); // 函数内所有的变量均为static
static int cnt = 0; //这个static 变量 cnt只会初始化一次
cnt += a ;
return cnt;
endfunction
function int cnt3(input a); // module中的方法默认为static
int cnt = 0; // 这个static 变量 cnt只会初始化一次
cnt += a ;
return cnt;
endfunction
endmodule
initial begin
$display("%0d",cnt1(1)); // 输出1
$display("%0d",cnt1(1)); // 输出1
$display("%0d",cnt2(1)); // 输出1
$display("%0d",cnt2(1)); // 输出2
$display("%0d",cnt3(1)); // 输出1
$display("%0d",cnt3(1)); // 输出2
end
system verilog
在module、program、interface、task和function之外声明的变量拥有静态的生命周期,即存在于整个仿真阶段,同C定义的静态变量一样。
在module、interface和program内部声明,且在task、process或者function外部声明的变量也是static变量,且作用域在该块中。
在module、program和interface中定义的task、function默认都是static类型。
在过程块中(task、function、process)定义的变量均跟随它的作用域,即过程块的类型,如果过程块为static,则它们也默认为static,反之亦然。这些变量也可以由用户显式声明为automatic或者static。