In SystemVerilog, variables declared as local are restricted in visibility to the methods of the same class. These local variables are not accessible from child classes or outside the class where they are defined. However, non-local methods can access and even override local members in child classes.

Let’s walk through an example to better understand how the local keyword works and how it limits variable access.

Example 1: Using Local and Public Variables in a Class

In the following example, we declare two variables: one public and one local. We will try to access the local variable outside the class, which should result in an error.

Code Example

class MyClass;
  // By default, all variables are public
  byte publicVar;
  local byte localVar; // This variable is local to the class

  // Function to display the variable values
  function void display();
    $display ("publicVar=0x%0h, localVar=0x%0h", publicVar, localVar);
  endfunction
endclass

module testbench;
  initial begin
    // Create an object of MyClass and call the display function
    MyClass obj = new();
    obj.display(); // This works fine

    // Public variable can be accessed directly via the object
    $display ("publicVar = 0x%0h", obj.publicVar);

    // Accessing the local variable from outside the class will cause an error
    $display ("localVar = 0x%0h", obj.localVar); // Error
  end
endmodule

Expected Output

When running this code, the compiler will show an error for the line where the local variable localVar is accessed from outside the class. This happens because local variables are not visible outside the class where they are declared.

Simulation Log

$display ("localVar = 0x%0h", obj.localVar);
                                               |
ncvlog: *E,CLSNLO (testbench.sv,24|47): Access to local member 'localVar' in class 'MyClass' is not allowed here.
irun: *E,VLGERR: An error occurred during parsing. Review the log file for errors with the code *E and fix those identified problems to proceed. Exiting with code (status 1).

If we remove the problematic line of code and run the simulation again, the output will display the public variable and the local variable, but only within the class method display().

Code After Fix

module testbench;
  initial begin
    MyClass obj = new();

    // This should print both public and local variables
    obj.display();

    // Public variables are always accessible
    $display ("publicVar = 0x%0h", obj.publicVar);
  end
endmodule

Simulation Log Output

publicVar=0x0, localVar=0x0
publicVar = 0x0

Example 2: Accessing Local Variables from Child Classes

In this example, we will create a child class that extends a base class containing a local variable. We expect to see a compile-time error because local variables are not inherited by child classes.

Code Example

// Base class with a local variable
class BaseClass;
  local byte localVar;
endclass

// Child class that extends BaseClass
class ChildClass extends BaseClass;
  function void show();
    // Try to access the local variable from the base class
    $display ("localVar = 0x%0h", localVar); // Error
  endfunction
endclass

module testbench;
  initial begin
    // Create an object of the child class and try to access the local member
    ChildClass obj = new();
    obj.show(); // This will result in an error
  end
endmodule

Expected Output

The compiler will give an error because the child class cannot access the local variable from the base class.

Simulation Log

$display ("localVar = 0x%0h", localVar);
                                           |
ncvlog: *E,CLSNLO (testbench.sv,10|43): Access to local member 'localVar' in class 'BaseClass' is not allowed here.
irun: *E,VLGERR: An error occurred during parsing. Review the log file for errors with the code *E and fix those identified problems to proceed. Exiting with code (status 1).

Key Takeaways

  1. Local Variables: The local keyword in SystemVerilog makes variables visible only within the class where they are declared. They cannot be accessed outside the class or by child classes.
  2. Public Variables: Public variables, on the other hand, are accessible from anywhere via the class object.
  3. Access Restrictions: Using the local keyword is useful when you need to limit the scope of variables and protect class members from being accessed or modified outside the class.

Comparison Table: Local vs Public Variables

FeatureLocal VariablesPublic Variables
ScopeOnly accessible within the classAccessible from anywhere
InheritanceNot inherited by child classesInherited and can be overridden
Access from Outside ClassNot allowedAllowed

Conclusion

Understanding the SystemVerilog local keyword helps you manage variable visibility effectively. By using local, you can ensure that certain variables are not exposed outside their class or inherited by child classes. This is an important feature for maintaining encapsulation in your SystemVerilog designs.

Scroll to Top