Supporting Class,class in SystemVerilog is somewhat similar to module, which is similar to the class in C + +.
Class only has the new () function to really open up the memory, otherwise it is only an empty, no actual existence.
Sonet_static sta1;//This is just an empty sta1.
Sta1 = new ();//really open up the memory space, and initialization, if the new () function is not implemented in class, there will be the system default new (), to open up memory work.
The static and automatic:static variable is static and exists throughout the execution of the program, and no matter how many times it is instantiated, there is only one copy in memory, each instance pointing to this address, sharing a variable Automatic is a new memory that each instance produces, is not shared, is separate, different, default is automatic.
Static
' Timescale 1 NS/1 PS program
sim_top ();
Class Data_packet;
static integer Data = hff;//static variable declaration
function new ();
data--;
Endfunction
endclass
initial
begin
Automatic data_packet packet_one = new;
Automatic Data_packet packet_two = new;
$display ("%d--value of static data packet before decrement", Packet_one. Data);
$display ("\n%d--value of static data packet after decrement", Packet_two. Data);
End
Endprogram
Output
# 253--value of static data packet before decrement
# 253--value of static data packet after decrement
Automatic
' Timescale 1 NS/1 PS program
sim_top ();
Class Data_packet;
static integer data = ' hff;//static variable declaration
integer data = ' hff;//static variable declaration
function new ();
data--;
Endfunction
endclass
initial
begin
Automatic Data_packet packet_one = new ();
Automatic Data_packet packet_two = new ();
$display ("%d--value of static data packet before decrement", Packet_one. Data);
$display ("\n%d--value of static data packet after decrement", Packet_two. Data);
End
Endprogram
Output
# 254--value of static data packet before decrement
# 254--value of static data packet after Decreme Nt
Static
' Timescale 1 ns/1 ps
module sim_top ();
Class sonet_static;
Static bit count = 1 ' B1;
bit indicator;
function new ();
indicator = count++;
Endfunction
endclass
sonet_static sta1,sta2;//objects of class
initial
begin
sta1 = new ();
Sta2 = new ();
$display ("indicator =%d, Count =%d", Sta1.indicator, Sta1.count);
$display ("Second indicator =%d, Count =%d", Sta2.indicator, Sta2.count);
End
Endmodule
Output
# indicator = 1, count = 1
# Second indicator = 0, count = 1
Note A small knowledge point: If class is declared in initial begin, you need to specify a static or automatic, and if you do not need to specify a static or automatic outside of the initial begin, such as
Automatic Data_packet Packet_one = new ();