I’m trying to access a static property of a class from different modules, but it doesn’t seem that a static property is shared with class instances located in another module in different file.
For example:
class my_class;
static int count = 0;
function void set_cnt(input int cnt);
count = cnt;
endfunction
endclass
module test ();
mod1 mod1();
mod2 mod2();
endmodule
module mod1 ();
my_class c1;
initial begin
c1 = new();
c1.set_cnt(10);
$display (" the count in c1 is %d", c1.count);
end
endmodule
module mod2 ();
my_class c2;
initial begin
c2 = new();
#1000;
$display (" the count in c2 is %d", c2.count);
end
endmodule
If all of above is in one file, the result is correct: the value of c2.count is 10. But if module mod1, mod2, and test are in their own files, the value of c2.count will be 0.
You didn’t say how you compiled my_class when you split up the files, but I’m assuming you compiled it with each module. Then you get three independent types of my_class. Although they have the same name, they are independent class types because the full name of any user defined type includes the scope containing the declaration. The scope outside of a module is called $unit, and each file has a unique compilation unit scope. You need to put the class declaration in a package and import the package into each module declaration. See http://go.mentor.com/package-import-versus-include .
Thank you very much for the answer. You’re correct that I used “`include my_class.sv” instead of a package. After I changed it to a package, as you suggested, everything looks fine!