Wednesday, July 22, 2020

Data Hiding and Encapsulation in System Verilog

Hi Everyone !!!,
    In this section we are going to see concepts of Data Hiding&Encapsulation in System Verilog.
  
    We may use the Base Classes or Base Class library provided by third party sources. We have seen how to access the Class properties and methods. For example "Class Members" in system verilog can be accessed using the class handles. By Default Class members are Public in nature  which means class members can be accessed directly from outside of that class. 
     Data hiding is the process of hiding or making visible of properties  in particular places of the class by using local and protected keywords is known as data hiding.
     For safety purpose or preventing corruption of logic's in base classes system verilog supports two qualifiers .
        Two Qualifiers
      * Local                    * Protected 
Local Qualifier:
       By declaring properties as local. We can't access the outside of the class.

      Example:
        1)                  class  data_hide;
                                local int x;  //** here integer x is declared as "local"
                             endclass
                             module TB;
                                   data_hide  d;
                                   initial begin
                                          d=new();
                                          d.x=10;  //** here we are accessing local qualifier variable in outside of the class which is not possible it leads to compilation error.
                                   end
                             endclass

2)                      class  data_hide;
                                local int x;  //** here integer x is declared as "local"
                                   task display();
                                      x=5;
                                      $display("Value %0d",x);  //it is allowed to use base class only
                                   endtask
                             endclass
                             module TB;
                                   data_hide  d;
                                   initial begin
                                          d=new();
                                          d.display();  
                                   end
                             endclass
     
Note : local qualifier properties are not allowed to access outside of the class and derived classes.

         3)               class parent;
                                  local int x;  //** here integer x is declared as "local"
                            endclass
                           class child extends parent;
                               x= 10;   /** here we are accessing  local qualifier variable in derived which is not possible it leads to compilation error.
                            endclass

Protected Qualifier:
       By declaring properties as protected. We can't access the outside of the class . Which is allowed to access in derived class and parent class only.

        Example:
        1)                  class  data_hide;
                                protected int x;  //** here integer x is declared as "protected"
                             endclass
                             module TB;
                                   data_hide  d;
                                   initial begin
                                          d=new();
                                          d.x=10;  //** here we are accessing protected qualifier variable  in outside of the class which is not possible it leads to compilation error.
                                   end
                             endclass 
        
       
      2)                   class parent;
                                  protected int x;  //** here integer x is declared as "protected"
                            endclass
                           class child extends parent;
                               x= 10;   /** protected variables are allowed to use derived class (child)only
                            endclass
                         

Note: We can declare function/tasks/variables as local or protected based on our use case.


Constant Class Properties:
               Sometimes in our Base Classes, We need some our class Properties to be read only & not allowed to change those Properties. This behavior can be achieved by using "const" keyword.
                Which means if the class property is declared with const keyword , it can not be modified or updated whole run time. It is two types.
                             *Global Constant
                              *Instance Constant

Global Constants:
                The value is assigned at the time of declaration with const qualifier. Then the same value is kept by that property. We are not allowed to change the property anywhere other than in the declaration till the run time. 
                   Example:
                                              class glob_const;
                                                 const int data=10;  //Global constants  ( global constants are allowed to assign to a non const variable and not allowed to override (allowed to declare static const int data also//
                                                 byte addr[];
                                                 function new(int size);
                                                     addr=new[size > data?data:size];
                                                 endfunction
                                            endclass
                                             module TB;
                                                 glob_const p;
                                                  initial begin
                                                      p=new(2);
                                                   end
                                               endmodule
                                              
                    2)
               
                                            class glob_const;
                                                 const int data=10;  //Global constants
                                                 byte addr[];
                                                 function new(int size);
                                                      data=15;  //Here I try to override global constant variable it leads to compilation error.
                                                     addr=new[size > data?data:size];
                                                 endfunction
                                            endclass
                                             module TB;
                                                 glob_const p;
                                                  initial begin
                                                      p=new(2);
                                                   end
                                               endmodule
                                           
Instance Constants:
                 For instance Constant properties, It has a two step process. First the property is declared inside the class with const keyword. Second the value to that property is assigned inside the constructor of that class. Here after , this initialized value is not allowed to be modify. 

Lets see an brief example 
                
                                                class inst_const;
                                                 const int data;  //Instance constants (not allowed to declare as static const int)
                                                 byte addr[];
                                                 function new();
                                                      data=$urandom%4096;  //one assignment in new-> its allowed in instance constant
                                                     addr=new[ data];
                                                 endfunction
                                            endclass
                                             module TB;
                                                 inst_const p;
                                                  initial begin
                                                      p=new();
                                                   end
                                               endmodule

Keypoints:
            Instance constants do not include an initial value in their declaration. This type of constants can be assigned a value at run time, but assignment can only done in the corresponding class constructor.
            Global constants are allowed to declare static because they same for all instances of the class.But instance constant can't be declared as static because it won't allow the assignment in constructor.


Hope this concepts will be useful to everyone !!!
Giving feedback is more precious than writing an article!!! 😊😊😊😊😊
Always welcome both positive and negative feedback's !!!
Feel free to post any queries related SV and UVM  

"knowledge will give you the power,character will give you the respect so power and respect is the most demanded one in the world !!! Be a demanded one !!!!!!!"  






        


4 comments:

  1. For the local/protected qualifier, Clear explanations are already there. But It would be better to add more clarity on the local/protected qualifier in the calling & called methods.

    ReplyDelete
    Replies
    1. Sure I will explain on further blogs.Thanks for the inputs!!!

      Delete

`define Macro usage in System Verilog

 Hi Everyone !!! In this blog we are going to see the usage of `define macro in System Verilog. A text macro substitution facility has been ...