|HOME |ABOUT |ARTICLES |ACK |FEEDBACK |TOC |LINKS |BLOG |JOBS |


Tutorials



CONSTANT




SystemVerilog adds another form of a local constant, const. A const form of constant differs from a localparam constant in that the localparam must be set during elaboration, whereas a const can be set during simulation, such as in an automatic task.



Constant Class



An instance of a class (an object handle) can also be declared with the const keyword.



EXAMPLE
const class_name object = new(3,3,4);




In other words, the object acts like a variable that cannot be written. The arguments to the new method must be constant expressions. The members of the object can be written (except for those members that are declared const).



Global Constant



Global constant class properties include an initial value as part of their declaration. They are similar to other const variables in that they cannot be assigned a value anywhere other than in the declaration.



EXAMPLE
class Jumbo_Packet;
const int max_size = 9 * 1024; // global constant
byte payload [];

function new( int size );
payload = new[ size > max_size ? max_size : size ];
endfunction
endclass

EXAMPLE: error : chaning const varible
class A ;
const int i = 10;
endclass

program main ;
A my_a;

initial
begin
my_a = new();
my_a.i = 55;
end
endprogram

RESULT

Error : Variable 'i' declared as 'const' .


Instance Constants



Instance constants do not include an initial value in their declaration, only the const qualifier. This type of constant can be assigned a value at run time, but the assignment can only be done once in the corresponding class constructor not from outside or any other method.



EXAMPLE
class A ;
const int i;

function new();
i = 20;
endfunction
endclass

program main ;
A my_a;

initial
begin
my_a = new();
$display(my_a.i);
end
endprogram

RESULT

20


EXAMPLE: error : assignment done twice
class A ;
const int i;

function new();
i = 20;
i++;
endfunction
endclass

RESULT

Instance constants assignment can only be done once


EXAMPLE: error : assignment in other method
class A ;
const int i;

task set();
i = 20;
endtask
endclass

RESULT

Instance constants assignment allowed only in class constructor.




Typically, global constants are also declared static because they are the same for all instances of the class. However, an instance constant cannot be declared static because that would disallow all assignments in the constructor



Index
Introduction
Class
Object
This
Inheritance
Encapsulation
Polymorphism
Abstract Classes
Parameterised Class
Nested Classes
Constant
Static
Casting
Copy
Scope Resolution Operator
Null
External Declaration
Classes And Structures
Typedef Class
Pure
Other Oops Features
Misc

Report a Bug or Comment on This section - Your input is what keeps Testbench.in improving with time!





<< PREVIOUS PAGE

TOP

NEXT PAGE >>

copyright 2007-2017 :: all rights reserved www.testbench.in::Disclaimer