[Interview Question] You have multiple analysis ports in your environment, and you need to broadcast a packet to all ports. How can you implement this in UVM? Write the relevant code snippet

Hi all, I was asked this question in the interview and the solution i wrote was -

class broadcast #(pkt) extends uvm_component;
  `uvm_component_utils(broadcast)
  
  uvm_analysis_imp #(pkt, broadcast) port_in;
  uvm_analysis_port #(pkt) port_out;
  
  function new(string name = "broadcast", uvm_component parent = null);
    super.new(name, parent);
  endfunction
  
  virtual function void write(pkt p);
    port_out.write(p);
  endfunction  
endclass
  
class env extends uvm_env;
  `uvm_component_utils(env)
  
  uvm_analysis_port #(pkt) analysis_port1;
  uvm_analysis_port #(pkt) analysis_port2;
  broadcast #(pkt) bcast;
  
  function new(string name = "env", uvm_component parent = "null");
    super.new(name, parent);
  endfunction
  
  function void build_phase(uvm_phase phase);
    super.build_phase(phase);
    bcast = broadcast#(pkt)::type_id::create("bcast",this);
    analysis_port1 = new("analysis_port1", this);
    analysis_port2 = new("analysis_port2", this);
    
    bcast.outp.connect(analysis_port1);
    bcast.outp.connect(analysis_port2);
  endfunction
  
  function void connect_phase(uvm_phase phase);
    super.connect_phase(phase);
    producer.analysis_port.connect(bcast.inp);
  endfunction
endclass

Is this solution correct? Can you suggest any improvement?

This is a very vague question. Does “multiple ports in your environment“ mean the environment component, or the entire hierarchy of the environment including sub-components? Normally analysis ports are sent from monitors when sending multiple transactions. And when it asks “broadcast a packet to all ports”, does that mean the same packet to all ports?

Hi Dave!

  1. The ports are meant to be in environment component.
  2. Yes, Same packet is needed to be send to all ports in that env component