Tutorials
Learn More
Constant class properties in SV
To make class properties read-only, a const specifier is used. Since class objects are dynamic in nature, SystemVerilog provides two types of constants
- Global constants
- Instance constants
In both types, class properties have to specify with the const keyword.
The class methods can not use a const specifier.
Global constants
During variable declaration, an initial value is assigned, such class properties known as global constants. The value of a variable can not be changed after the declaration of the variable.
Global constant Example
class transaction;
bit [31:0] data;
const int id = 1;
function void display();
$display("data = %0d and id = %0d", data, id);
endfunction
endclass
module class_example;
transaction tr;
initial begin
tr = new();
tr.data = 100;
//tr.id = 2; // Invalid const usage
tr.display();
end
endmodule
Output:
data = 100 and id = 1
Instance constants
During variable declaration, an initial value is not assigned, such class properties are known as instance constants. An instance constant allows a user to assign value in run time only once in the class constructor,
Instance constant Example
class transaction;
bit [31:0] data;
const int id;
function new();
data = 100;
id = 1; // run time assignment only once
endfunction
function void display();
$display("data = %0d and id = %0d", data, id);
endfunction
endclass
module class_example;
transaction tr;
initial begin
tr = new();
tr.data = 200;
//tr.id = 2; // Invalid const usage
tr.display();
end
endmodule
Output:
data = 200 and id = 1
System Verilog Tutorials