嵌入式技术
SystemVerilog中可以将类属性声明为常量,即“只读”。目的就是希望,别人可以读但是不能修改它的值。
这个需求可以通过const关键字实现。如果在类属性声明前面加上了cons关键字,那么在仿真运行过程中,这个变量的值无法被修改。
const变量的初始值可以在声明的时候,也可以在类的构造函数中初始化。
class packet;
const int serialNum = 'h1234;
const int packetID;
//serialNum = 'hf; //COMPILE ERROR
//packetID = 'hff; //COMPILE ERROR
//constructor
function new;
packetID = 'h4567;
//const assignment in the constructor
endfunction
function void disp(input int packetID);
$display("From packet");
$display(" serialNum = %h",this.serialNum);
$display(" packetID = %h",this.packetID);
endfunction
endclass
class eth_packet extends packet;
function new;
super.new;
//Instance constant cannot be re-initialized.
//packetID = 'h ff; //COMPILE ERROR
endfunction
function void eth_disp;
super.disp(packetID);
endfunction
endclass
module class_TOP( );
initial begin
eth_packet e1;
e1 = new( );
e1.eth_disp;
end
endmodule
仿真log:
From packet serialNum = 00001234 packetID = 00004567 V C S S i m u l a t i o n R e p o r t
在上面的示例中,我们声明了2个常量。
“serialNum” :在声明的时候初始化;
“packetID”:在构造函数中初始化。
所以后面对常量赋值会导致编译错误。
审核编辑:汤梓红
全部0条评论
快来发表一下你的评论吧 !