class-new()函式, static or automatic(systemverilog)
阿新 • • 發佈:2019-01-28
在systemverilog中支援class,class與module有些相似,此class與c++中的class類似。
class只有經過了new()函式才真正開闢了記憶體,否則只是一個空的、沒有實際存在。
Sonet_static sta1;//此時只是一個空的sta1
sta1 = new();//真正開闢了記憶體空間,並進行了初始化,若在class中未實現new()函式,則會有系統預設new(),進行開闢記憶體工作。
static and automatic: static 變數為靜態的,在整個程式執行過程中都存在,並且無論被例項多少次,在記憶體中只存在一份,每次例項都是指向這一個地址,共享一個變數;automatic是每次例項均產生一個新的記憶體,互相不共享,是單獨、不同的,default預設是automatic。
static:
output:`timescale 1 ns / 1 ps program sim_top(); class data_packet; static integer Data = 32'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
# 253--Value of static data packet before decrement
#
# 253--Value of static data packet after decrement
automatic:
output:`timescale 1 ns / 1 ps program sim_top(); class data_packet; //static integer Data = 32'hff;// Static variable declaration integer Data = 32'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
# 254--Value of static data packet before decrement
#
# 254--Value of static data packet after decrement
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("First indicator = %d, count = %d", sta1.indicator, sta1.count);
$display("Second indicator = %d, count = %d", sta2.indicator, sta2.count);
end
endmodule
output:
# First indicator = 1, count = 1
# Second indicator = 0, count = 1
注意一個小知識點:若class是在initial begin中被宣告,則需要指定static or automatic,若在initial begin外則無需指定static or automatic, 如
automatic data_packet packet_one = new();