Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

Usecase Example

UVM Reporting & Utilities

uvm_event_pool: an agent registers a reset event in the global pool with add(), and the env retrieves it with get_global() to react to resets.

Open in IDE
testbench.sv
//--------------------------------------------------------------------
// www.chipverify.com
//--------------------------------------------------------------------


`include "uvm_macros.svh"
import uvm_pkg::*;

interface my_if;
  logic 	rstn;
endinterface

class my_agent extends uvm_agent;
  `uvm_component_utils(my_agent)
  
  virtual my_if  	vif;
  uvm_event_pool 	m_pool;
  uvm_event 		m_evt_rstn;
  
  function new(string name="my_agent", uvm_component parent=null);
    super.new(name, parent);
  endfunction
  
  virtual function void build_phase(uvm_phase phase);
    uvm_event_pool 		l_pool;
    
    super.build_phase(phase);
    
    if (! uvm_config_db #(virtual my_if)::get(null, "*", "vif", vif))
      `uvm_fatal(get_type_name(), $sformatf("[NOVIF] vif not found !"))
          
    m_evt_rstn = new();
        
    l_pool = uvm_event_pool::get_global_pool();
    l_pool.add("evt_rstn", m_evt_rstn);
  endfunction
      
  virtual task run_phase(uvm_phase phase);
    super.run_phase(phase);
    
    fork
      reset_tracker();
    join_none
  endtask
    
  virtual task reset_tracker();
    `uvm_info (get_type_name(), "[reset_tracker] Start reset monitor", UVM_MEDIUM)
    forever begin
  	  @(negedge vif.rstn);
      m_evt_rstn.trigger();
    end      
  endtask
    
endclass

class my_env extends uvm_env;
  `uvm_component_utils(my_env)
  
  my_agent 	m_agent;
  
  function new(string name="my_env", uvm_component parent=null);
    super.new(name, parent);
  endfunction
  
  virtual function void build_phase(uvm_phase phase);
    super.build_phase(phase);
    
    m_agent = my_agent::type_id::create("m_agent", this);
  endfunction
  
  virtual task run_phase(uvm_phase phase);
    super.run_phase(phase);
    
    fork
      track();
    join_none
  endtask

  virtual task track();
    uvm_event 	l_evt_rstn = uvm_event_pool::get_global("evt_rstn");

    
    `uvm_info(get_type_name(), "[track] Start tracker", UVM_MEDIUM)
    forever begin
      l_evt_rstn.wait_trigger();
      `uvm_info(get_type_name(), $sformatf("[track] Reset found"), UVM_MEDIUM)
    end
  endtask
  
endclass

class my_test extends uvm_test;
  `uvm_component_utils (my_test)
  
  my_env 		 	m_env;
  virtual my_if  	vif;
  
  function new(string name="my_test", uvm_component parent=null);
    super.new(name, parent);
  endfunction
  
  virtual function void build_phase(uvm_phase phase);
    super.build_phase(phase);    
    m_env = my_env::type_id::create("my_env", this);
    
    if (! uvm_config_db #(virtual my_if)::get(null, "*", "vif", vif))
      `uvm_fatal(get_type_name(), $sformatf("[NOVIF] vif not found !"))
  endfunction
  
  virtual task run_phase(uvm_phase phase);        
    super.run_phase(phase);
    phase.raise_objection(this);        
    stimulus();    
    phase.drop_objection(this);
  endtask
    
  virtual task stimulus();
    #10 vif.rstn = 1;
    #20 vif.rstn = 0;
    #100 vif.rstn = 1;
    #200 vif.rstn = 0;
    #400;
  endtask
    
endclass

module tb;
  
  my_if 	m_if();
  
  initial begin
    uvm_config_db#(virtual my_if)::set(null, "*", "vif", m_if);
    run_test("my_test");
  end
  
  initial begin
    $dumpvars;
    $dumpfile("dump.vcd");
  end
endmodule
design.sv
// Code your design here
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.