Get_config in example code?

The attached code to the Agent page of the cookbook is slightly different from the code published on this page. The config objects have get_config method:

function apb_agent_config apb_agent_config::get_config( uvm_component c );
  apb_agent_config t;

  if (!uvm_config_db #(apb_agent_config)::get(c, "", s_my_config_id, t) )
     `uvm_fatal("CONFIG_LOAD", $sformatf("Cannot get() configuration %s from uvm_config_db. Have you set() it?", s_my_config_id))

  return t;
endfunction

Whilst I see what it does, I don’t get what you would do with this function?

There are several different ways for you to code this, there is no right answer, and the code examples in the cookbook come from several sources.

The above approach may benefit you in some circumstances as it cleans up the complexities of the config_db code.

To get any config object from the UVM config_db, you have to call get() with all the detailed syntax, and you have to check the returned value and fatal error if the config was not available. You may need to repeat that code in several places in your agent: driver, monitor, coverage, sequencer, etc. So one approach for cleaner code and more encapsulation is to put the common code into a method in the config object like the above.

From any of your components that need this config object, you would call:

    apb_agent_config myconfig;
    ...
    function void build_phase(uvm_phase phase);
      myconfig = apb_agent_config::get_config(this);
    endfunction

…and the config object handles all the details of the config_db lookup syntax, and especially, the error checking and reporting, to save you from needing to do this:

    apb_agent_config myconfig;
    localparam string s_my_config_id = "apb_agent_config";
    ...
    function void build_phase(uvm_phase phase);
      if (!uvm_config_db #(apb_agent_config)::get(this, "", s_my_config_id, myconfig) )
        `uvm_fatal("CONFIG_LOAD", $sformatf(
          "Cannot get() configuration %s from uvm_config_db. Have you set() it?",
          s_my_config_id))
    endfunction