Jump to content

How to use a variable defined in one class into another class?


Recommended Posts

Hello all,
Can A variable defined and used in one task, in one class, be used as it is in another class?But these two classes are completely independent of each other,in other words, they are not inherited from one another. So with this situation, can a variable defined in class Driver, be utilized in class monitor? For eg, I need to use a variable pkt_length (defined and used in driver class) in monitor class also so that I can use it in order to get the packets unpacked in monitor class.

Please help me with this querry. Thanks,

Link to post
Share on other sites

Hi,

 

In Object Oriented Programming, each class defines a set of public methods and/or fields ("API"), thus giving an interface to the services and/or data available from objects of that class. Best practices suggest that fields should normally be inaccessible from outside the class, thus allowing classes to separate interface & implementation (or representation).

Moreover - in System Verilog, variables defined inside functions/tasks are accessible only from within that function/task, and definitely not from within other classes.

 

Methodologically speaking, you don't want your monitor to be dependant on your driver; The monitor should be able to independently run when the agent is in passive mode (i.e. no active driving of the interface, only passive monitoring).

 

Try to find a way to extract the information you need from the monitored interface itself:

For example, accumulate the packet's payload passed on the interface into a queue of bytes, and use queue.size() method to get its bytecount.

Link to post
Share on other sites

Assuming the driver and monitor share the same parent (agent), which is conventional, then you can access any public (i.e. not local, not protected) variables through the parent handle:

 

Agent:

my_driver m_drv;
my_monitor m_mon;
 

 

Driver:

int unsigned m_pkt_len;
 

 

Monitor:

my_agent m_parent_handle;
int unsigned m_pkt_len;  // Not the same as m_pkt_len in the driver.
...
task run_phase(uvm_phase phase);
    $cast(m_parent_handle, get_parent());  // Capture the handle of the parent, which should be the agent.
    m_pkt_len = m_parent_handle.m_drv.m_pkt_len;  // Copy packet length from the driver somewhere in this task.
    // Do what you want.
endtask : run_phase
 

 

However, you should not couple your monitor to your driver in the manner you described.  What if your agent is configured to passive mode and is responsible only for monitoring, which could be the case, for example, if you integrate the agent up into a system-level env from a block-level env, and the active agent would have to be switched to passive to avoid contention with design blocks in the system level.  If you're doing what you're doing as a quick-and-dirty debugging thing, then fine.  But, just be very careful.  The monitor should never know anything about what the driver is doing, usually.

 

If you need some sort of driver/monitor interaction aside from the physical interface, and something more permanent, then I think the better solution is to connect the two in the agent via TLM ports/exports and FIFOs.  You could define a transaction that contains fields for information of interest between the driver and monitor, and pass those objects through the TLM connection.  Or, just pass the transaction itself across the connection.  For example, flow control using control frames would be one example where you might consider connecting between the agent subcomponents using TLM (driver needs info from the frame collected at the monitor to determine if it should drive or stall).

Link to post
Share on other sites

Hello all,

Can A variable defined and used in one task, in one class, be used as it is in another class?But these two classes are completely independent of each other,in other words, they are not inherited from one another. So with this situation, can a variable defined in class Driver, be utilized in class monitor? For eg, I need to use a variable pkt_length (defined and used in driver class) in monitor class also so that I can use it in order to get the packets unpacked in monitor class.

Please help me with this querry. Thanks,

I think maybe you can uvm_config_db, have a try.

Link to post
Share on other sites
  • 7 years later...

There are two ways to do it. 

Method 1: (not preferred)

Assume a variable "var1" is from a class "C1" and you wish to read/update the "var1" from another class "C2". Declare the variable "var1" in C1 class with static access modifier. This make the "var1" as a class variable instead of an instance variable. You can now call/access this "var1" from class "C2" by "C1::var1".

 

Method2: (preferred)

If you do not want to compromise on encapsulation then, define static getter/setter methods to the "var1" of class "C1" and call those to access the "var1" from any other class. This avoids any unexpected read/write operations on the "var1" in the code.

 

Hope this answers your question!! 

Link to post
Share on other sites

There are numerous ways you can address this:

  1. Make the variable public (breaks encapsulation) and provide the object reference.
  2. Provide accessor methods to allow restricted access (best)
  3. Make the attribute static and public, but then you cannot have a different per instance. Effectively, a class specific global variable. (dangerous)

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...