What is UVM environment ?
A UVM environment contains multiple, reusable verification components and defines their default configuration as required by the application. For example, a UVM environment may have multiple agents for different interfaces, a common scoreboard, a functional coverage collector, and additional checkers. This forms the base of any modern verification environment.
Why shouldn't verification components be placed directly in test class ?
It is NOT recommended to instantiate individual verification components directly inside the test class which has the following drawbacks :
- Test writer should know how to configure the environment
- Changes to the topology will require updating of multiple test files, which can take a lot of time
- Tests are not reusable because they rely on a specific environment structure
Hence, it is always recommended to build the testbench class from
uvm_env, which can then be instantiated within multiple tests. This will allow changes in environment topology to be reflected in all the tests. Moreover, the environment should have knobs to configure, enable or disable different verification components for the desired task.
uvm_env is the base class for hierarchical containers of other components that make up a complete environment. It can be reused as a sub-component in a larger environment or even as a stand-alone verification environment that can instantiated directly in various tests.
Steps to create a UVM environment1. Create a custom class inherited from
uvm_env, register with factory, and call
// my_env is user-given name for this class that has been derived from "uvm_env" class my_env extends uvm_env; // [Recommended] Makes this driver more re-usable `uvm_component_utils (my_env) // This is standard code for all components function new (string name = "my_env", uvm_component parent = null); super.new (name, parent); endfunction // Code for rest of the steps come here endclass2. Declare and build verification components
// apb_agnt and other components are assumed to be user-defined classes that already exist in TB apb_agnt m_apb_agnt; func_cov m_func_cov; scbd m_scbd; env_cfg m_env_cfg; // Build components within the "build_phase" virtual function void build_phase (uvm_phase phase); super.build_phase (phase); m_apb_agnt = apb_agnt::type_id::create ("m_apb_agnt", this); m_func_cov = func_cov::type_id::create ("m_func_cov", this); m_scbd = scbd::type_id::create ("m_scbd", this); // [Optional] Collect configuration objects from the test class if applicable if (uvm_config_db #(env_cfg)::get(this, "", "env_cfg", m_env_cfg)) `uvm_fatal ("build_phase", "Did not get a configuration object for env") // [Optional] Pass other configuration objects to sub-components via uvm_config_db endfunction3. Connect verification components together
virtual function void connect_phase (uvm_phase phase); // A few examples: // Connect analysis ports from agent to the scoreboard // Connect functional coverage component analysis ports // ... endfunction
UVM Environment Example
This environment has 2 agents, 3 sub-environments and a scoreboard as represented in the block diagram shown above.
class my_top_env extends uvm_env; `uvm_component_utils (my_env) agent_apb m_apb_agt; agent_wishbone m_wb_agt; env_register m_reg_env; env_analog m_analog_env ; scoreboard m_scbd; function new (string name = "my_env", uvm_component parent); super.new (name, parent); endfunction virtual function void build_phase (uvm_phase phase); super.build_phase (phase); // Instantiate different agents and environments here m_apb_agt = agent_apb::type_id::create ("m_apb_agt", this); m_wb_agt = agent_wishbone::type_id::create ("m_wb_agt", this); m_reg_env = env_register::type_id::create ("m_reg_env", this); foreach (m_analog_env[i]) m_analog_env[i] = env_analog::type_id::create ($sformatf("m_analog_env%0d",m_analog_env[i]), this); m_scbd = scoreboard::type_id::create ("m_scbd", this); endfunction virtual function void connect_phase (uvm_phase phase); // Connect between different environments, agents, analysis ports, and scoreboard here endfunction endclass
Note that env_analog or env_register environments can have other nested environments and agents within it. You can see how powerful UVM becomes in terms of reusability because of this hierarchical structure and TLM interfaces within each component.
Examples in CodeHub
In our code examples and the following sessions of the Basics series, we'll look at a simpler testbench structure which instantiates a single agent and a scoreboard, as shown in the block diagram below.
The testbench structure translates to the following code.
class my_env extends uvm_env ; `uvm_component_utils (my_env) my_agent m_agnt0; my_scoreboard m_scbd0; function new (string name, uvm_component parent); super.new (name, parent); endfunction : new virtual function void build_phase (uvm_phase phase); super.build_phase (phase); m_agnt0 = my_agent::type_id::create ("my_agent", this); m_scbd0 = my_scoreboard::type_id::create ("my_scoreboard", this); endfunction : build_phase virtual function void connect_phase (uvm_phase phase); // Connect the scoreboard with the agent m_agnt0.m_mon0.item_collected_port.connect (m_scbd0.data_export); endfunction endclass
Note that the analysis port of the monitor is connected to the export of the scoreboard in