Class-new () function, static or automatic (SystemVerilog)

Source: Internet
Author: User

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 ();



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.