When to use global variable and when to use config_db?

Hello All,

I guess we have around three common ways to share variable value across UVM components,

  1. Global variable
  2. config_db
  3. uvm_event

In our project, we are interchangeably using these. Some of the places we have used global variable (by declaring in tb_pkg.sv) and at some places we are utilizing config_db.

So I wish to know what is the good programming style to share variable values between components. I am aware that “using global variable” is not a good idea but I am not knowing its disadvantages too.

I am aware if we want to set and get variable onetime(static) then config_db would be the best choice(set/get interface handle,class objects) but if I need to get some variable values per clk (dynamically) then wouldn’t using config_db be a overhead? What should we use in this situation then?

One common good way I am seeing is to make global_cfg class and declare all common variables,methods inside it and set it in a config_db(from the test) and pass the same handle to all the uvm components down the hierarchy without cloning it.

Can anyone help me what should be the good way to share variables based on above situations? (Not sure if this has already been discussed)

In reply to supal:

The main problem with global variables is scalability. If your design starts growing and you need multiple instances, it becomes difficult to maintain. Many languages discourage the use of global variables. You can search for it and find many articles, such as this.

We recommend using a single config_object for each agent. If you find many items in your agent config object duplicated, you may want to put them in an env config object.

In reply to dave_59:

Thanks Dave. I understood !