multi_passthrough_initiator_socket.h revision 13511
113511Sgabeblack@google.com/*****************************************************************************
213511Sgabeblack@google.com
313511Sgabeblack@google.com  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
413511Sgabeblack@google.com  more contributor license agreements.  See the NOTICE file distributed
513511Sgabeblack@google.com  with this work for additional information regarding copyright ownership.
613511Sgabeblack@google.com  Accellera licenses this file to you under the Apache License, Version 2.0
713511Sgabeblack@google.com  (the "License"); you may not use this file except in compliance with the
813511Sgabeblack@google.com  License.  You may obtain a copy of the License at
913511Sgabeblack@google.com
1013511Sgabeblack@google.com    http://www.apache.org/licenses/LICENSE-2.0
1113511Sgabeblack@google.com
1213511Sgabeblack@google.com  Unless required by applicable law or agreed to in writing, software
1313511Sgabeblack@google.com  distributed under the License is distributed on an "AS IS" BASIS,
1413511Sgabeblack@google.com  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
1513511Sgabeblack@google.com  implied.  See the License for the specific language governing
1613511Sgabeblack@google.com  permissions and limitations under the License.
1713511Sgabeblack@google.com
1813511Sgabeblack@google.com *****************************************************************************/
1913511Sgabeblack@google.com#ifndef TLM_UTILS_MULTI_PASSTHROUGH_INITIATOR_SOCKET_H_INCLUDED_
2013511Sgabeblack@google.com#define TLM_UTILS_MULTI_PASSTHROUGH_INITIATOR_SOCKET_H_INCLUDED_
2113511Sgabeblack@google.com
2213511Sgabeblack@google.com#include "multi_socket_bases.h"
2313511Sgabeblack@google.com
2413511Sgabeblack@google.comnamespace tlm_utils {
2513511Sgabeblack@google.com
2613511Sgabeblack@google.com/*
2713511Sgabeblack@google.comThis class implements a trivial multi initiator socket.
2813511Sgabeblack@google.comThe triviality refers to the fact that the socket does not
2913511Sgabeblack@google.comdo blocking to non-blocking or non-blocking to blocking conversions.
3013511Sgabeblack@google.com
3113511Sgabeblack@google.comIt allows to connect multiple targets to this socket.
3213511Sgabeblack@google.comThe user has to register callbacks for the bw interface methods
3313511Sgabeblack@google.comhe likes to use. The callbacks are basically equal to the bw interface
3413511Sgabeblack@google.commethods but carry an additional integer that indicates to which
3513511Sgabeblack@google.comindex of this socket the calling target is connected.
3613511Sgabeblack@google.com*/
3713511Sgabeblack@google.comtemplate <typename MODULE,
3813511Sgabeblack@google.com          unsigned int BUSWIDTH = 32,
3913511Sgabeblack@google.com          typename TYPES = tlm::tlm_base_protocol_types,
4013511Sgabeblack@google.com          unsigned int N=0, sc_core::sc_port_policy POL = sc_core::SC_ONE_OR_MORE_BOUND>
4113511Sgabeblack@google.comclass multi_passthrough_initiator_socket
4213511Sgabeblack@google.com  : public multi_init_base< BUSWIDTH, TYPES, N, POL>
4313511Sgabeblack@google.com{
4413511Sgabeblack@google.compublic:
4513511Sgabeblack@google.com
4613511Sgabeblack@google.com  //typedefs
4713511Sgabeblack@google.com  //  tlm 2.0 types for nb_transport
4813511Sgabeblack@google.com  typedef typename TYPES::tlm_payload_type              transaction_type;
4913511Sgabeblack@google.com  typedef typename TYPES::tlm_phase_type                phase_type;
5013511Sgabeblack@google.com  typedef tlm::tlm_sync_enum                            sync_enum_type;
5113511Sgabeblack@google.com
5213511Sgabeblack@google.com  //  typedefs to keep the fn ptr notations short
5313511Sgabeblack@google.com  typedef sync_enum_type (MODULE::*nb_cb)(int,
5413511Sgabeblack@google.com                                         transaction_type&,
5513511Sgabeblack@google.com                                         phase_type&,
5613511Sgabeblack@google.com                                         sc_core::sc_time&);
5713511Sgabeblack@google.com  typedef void (MODULE::*dmi_cb)(int, sc_dt::uint64, sc_dt::uint64);
5813511Sgabeblack@google.com
5913511Sgabeblack@google.com  typedef multi_init_base<BUSWIDTH, TYPES, N, POL> base_type;
6013511Sgabeblack@google.com
6113511Sgabeblack@google.com  typedef typename base_type::base_target_socket_type base_target_socket_type;
6213511Sgabeblack@google.com
6313511Sgabeblack@google.com  static const char* default_name()
6413511Sgabeblack@google.com    { return sc_core::sc_gen_unique_name("multi_passthrough_initiator_socket"); }
6513511Sgabeblack@google.com
6613511Sgabeblack@google.com  //CTOR
6713511Sgabeblack@google.com  explicit multi_passthrough_initiator_socket(const char* name = default_name())
6813511Sgabeblack@google.com      : base_type(name)
6913511Sgabeblack@google.com      , m_hierarch_bind(0)
7013511Sgabeblack@google.com      , m_beoe_disabled(false)
7113511Sgabeblack@google.com      , m_dummy(this,42)
7213511Sgabeblack@google.com  {
7313511Sgabeblack@google.com  }
7413511Sgabeblack@google.com
7513511Sgabeblack@google.com  ~multi_passthrough_initiator_socket(){
7613511Sgabeblack@google.com    //clean up everything allocated by 'new'
7713511Sgabeblack@google.com    for (unsigned int i=0; i<m_binders.size(); i++) delete m_binders[i];
7813511Sgabeblack@google.com  }
7913511Sgabeblack@google.com
8013511Sgabeblack@google.com  //register callback for nb transport of bw interface
8113511Sgabeblack@google.com  void register_nb_transport_bw(MODULE* mod,
8213511Sgabeblack@google.com                              sync_enum_type (MODULE::*cb)(int,
8313511Sgabeblack@google.com                                                           transaction_type&,
8413511Sgabeblack@google.com                                                           phase_type&,
8513511Sgabeblack@google.com                                                           sc_core::sc_time&))
8613511Sgabeblack@google.com  {
8713511Sgabeblack@google.com    //warn if there already is a callback
8813511Sgabeblack@google.com    if (m_nb_f.is_valid()){
8913511Sgabeblack@google.com      display_warning("NBTransport_bw callback already registered.");
9013511Sgabeblack@google.com      return;
9113511Sgabeblack@google.com    }
9213511Sgabeblack@google.com
9313511Sgabeblack@google.com    //set the functor
9413511Sgabeblack@google.com    m_nb_f.set_function(mod, cb);
9513511Sgabeblack@google.com  }
9613511Sgabeblack@google.com
9713511Sgabeblack@google.com  //register callback for dmi function of bw interface
9813511Sgabeblack@google.com  void register_invalidate_direct_mem_ptr(MODULE* mod,
9913511Sgabeblack@google.com                             void (MODULE::*cb)(int, sc_dt::uint64, sc_dt::uint64))
10013511Sgabeblack@google.com  {
10113511Sgabeblack@google.com    //warn if there already is a callback
10213511Sgabeblack@google.com    if (m_dmi_f.is_valid()){
10313511Sgabeblack@google.com      display_warning("InvalidateDMI callback already registered.");
10413511Sgabeblack@google.com      return;
10513511Sgabeblack@google.com    }
10613511Sgabeblack@google.com
10713511Sgabeblack@google.com    //set the functor
10813511Sgabeblack@google.com    m_dmi_f.set_function(mod, cb);
10913511Sgabeblack@google.com  }
11013511Sgabeblack@google.com
11113511Sgabeblack@google.com  //Override virtual functions of the tlm_initiator_socket:
11213511Sgabeblack@google.com  // this function is called whenever an sc_port (as part of a target socket)
11313511Sgabeblack@google.com  //  wants to bind to the export of the underlying tlm_initiator_socket
11413511Sgabeblack@google.com  //At this time a callback binder is created an returned to the sc_port
11513511Sgabeblack@google.com  // of the target socket, so that it binds to the callback binder
11613511Sgabeblack@google.com  virtual tlm::tlm_bw_transport_if<TYPES>& get_base_interface()
11713511Sgabeblack@google.com  {
11813511Sgabeblack@google.com    m_binders.push_back(new callback_binder_bw<TYPES>(this, m_binders.size()));
11913511Sgabeblack@google.com    return *m_binders[m_binders.size()-1];
12013511Sgabeblack@google.com  }
12113511Sgabeblack@google.com
12213511Sgabeblack@google.com  // const overload not allowed for multi-sockets
12313511Sgabeblack@google.com  virtual const tlm::tlm_bw_transport_if<TYPES>& get_base_interface() const
12413511Sgabeblack@google.com  {
12513511Sgabeblack@google.com    display_error("'get_base_interface()' const not allowed for multi-sockets.");
12613511Sgabeblack@google.com    return base_type::get_base_interface();
12713511Sgabeblack@google.com  }
12813511Sgabeblack@google.com
12913511Sgabeblack@google.com  //Override virtual functions of the tlm_initiator_socket:
13013511Sgabeblack@google.com  // this function is called whenever an sc_export (as part of a initiator socket)
13113511Sgabeblack@google.com  //  wants to bind to the export of the underlying tlm_initiator_socket
13213511Sgabeblack@google.com  //   i.e. a hierarchical bind takes place
13313511Sgabeblack@google.com  virtual sc_core::sc_export<tlm::tlm_bw_transport_if<TYPES> >& get_base_export()
13413511Sgabeblack@google.com  {
13513511Sgabeblack@google.com    if (!m_beoe_disabled) //we are not bound hierarchically
13613511Sgabeblack@google.com      base_type::m_export.bind(m_dummy);  //so we bind the dummy to avoid a SystemC error
13713511Sgabeblack@google.com    return base_type::get_base_export(); //and then return our own export so that the hierarchical binding is set up properly
13813511Sgabeblack@google.com  }
13913511Sgabeblack@google.com
14013511Sgabeblack@google.com  virtual const sc_core::sc_export<tlm::tlm_bw_transport_if<TYPES> >& get_base_export() const
14113511Sgabeblack@google.com  {
14213511Sgabeblack@google.com    return base_type::get_base_export();
14313511Sgabeblack@google.com  }
14413511Sgabeblack@google.com
14513511Sgabeblack@google.com  //bind against a target socket
14613511Sgabeblack@google.com  virtual void bind(base_target_socket_type& s)
14713511Sgabeblack@google.com  {
14813511Sgabeblack@google.com    //error if this socket is already bound hierarchically
14913511Sgabeblack@google.com    if (m_hierarch_bind) {
15013511Sgabeblack@google.com      display_error("Already hierarchically bound.");
15113511Sgabeblack@google.com      return;
15213511Sgabeblack@google.com    }
15313511Sgabeblack@google.com
15413511Sgabeblack@google.com    base_type::bind(s); //satisfy systemC, leads to a call to get_base_interface()
15513511Sgabeblack@google.com
15613511Sgabeblack@google.com    //try to cast the target socket into a fw interface
15713511Sgabeblack@google.com    sc_core::sc_export<tlm::tlm_fw_transport_if<TYPES> >* p_ex_s=dynamic_cast<sc_core::sc_export<tlm::tlm_fw_transport_if<TYPES> >*>(&s);
15813511Sgabeblack@google.com    if (!p_ex_s) {
15913511Sgabeblack@google.com      display_error("Multi socket not bound to tlm_socket.");
16013511Sgabeblack@google.com      return;
16113511Sgabeblack@google.com    }
16213511Sgabeblack@google.com
16313511Sgabeblack@google.com    //try a cast into a multi sockets
16413511Sgabeblack@google.com    multi_to_multi_bind_base<TYPES>* test=dynamic_cast<multi_to_multi_bind_base<TYPES>*> (p_ex_s);
16513511Sgabeblack@google.com    if (test) //did we just do a multi-multi bind??
16613511Sgabeblack@google.com      //if that is the case the multi target socket must have just created a callback binder
16713511Sgabeblack@google.com      // which we want to get from it.
16813511Sgabeblack@google.com      //Moreover, we also just created one, which we will pass to it.
16913511Sgabeblack@google.com      m_sockets.push_back(test->get_last_binder(m_binders[m_binders.size()-1]));
17013511Sgabeblack@google.com    else{  // if not just bind normally
17113511Sgabeblack@google.com      sc_core::sc_export<tlm::tlm_fw_transport_if<TYPES> >& ex_s=*p_ex_s;
17213511Sgabeblack@google.com      m_sockets.push_back(&((tlm::tlm_fw_transport_if<TYPES>&)ex_s)); //store the interface we are bound against
17313511Sgabeblack@google.com    }
17413511Sgabeblack@google.com  }
17513511Sgabeblack@google.com
17613511Sgabeblack@google.com  //operator notation for direct bind
17713511Sgabeblack@google.com  void operator() (base_target_socket_type& s)
17813511Sgabeblack@google.com  {
17913511Sgabeblack@google.com    bind(s);
18013511Sgabeblack@google.com  }
18113511Sgabeblack@google.com
18213511Sgabeblack@google.com  //SystemC standard callback before end of elaboration
18313511Sgabeblack@google.com  void before_end_of_elaboration(){
18413511Sgabeblack@google.com    //if our export hasn't been bound yet (due to a hierarch binding)
18513511Sgabeblack@google.com    // we bind it now to avoid a SystemC error.
18613511Sgabeblack@google.com    //We must do that, because it is legal not to register a callback on this socket
18713511Sgabeblack@google.com    // as the user might only use b_transport
18813511Sgabeblack@google.com    if (!base_type::m_export.get_interface()){
18913511Sgabeblack@google.com      base_type::m_export.bind(m_dummy);
19013511Sgabeblack@google.com    }
19113511Sgabeblack@google.com
19213511Sgabeblack@google.com    //'break' here if the socket was told not to do callback binding
19313511Sgabeblack@google.com    if (m_beoe_disabled) return;
19413511Sgabeblack@google.com
19513511Sgabeblack@google.com    //get the callback binders of the top of the hierachical bind chain
19613511Sgabeblack@google.com    // NOTE: this could be the same socket if there is no hierachical bind
19713511Sgabeblack@google.com    std::vector<callback_binder_bw<TYPES>* >& binders=get_hierarch_bind()->get_binders();
19813511Sgabeblack@google.com
19913511Sgabeblack@google.com    //get the interfaces bound to the top of the hierachical bind chain
20013511Sgabeblack@google.com    // NOTE: this could be the same socket if there is no hierachical bind
20113511Sgabeblack@google.com    m_used_sockets=get_hierarch_bind()->get_sockets();
20213511Sgabeblack@google.com
20313511Sgabeblack@google.com    //register the callbacks of this socket with the callback binders
20413511Sgabeblack@google.com    // we just got from the top of the hierachical bind chain
20513511Sgabeblack@google.com    for (unsigned int i=0; i<binders.size(); i++) {
20613511Sgabeblack@google.com      binders[i]->set_callbacks(m_nb_f, m_dmi_f);
20713511Sgabeblack@google.com    }
20813511Sgabeblack@google.com  }
20913511Sgabeblack@google.com
21013511Sgabeblack@google.com  //
21113511Sgabeblack@google.com  // Bind multi initiator socket to multi initiator socket (hierarchical bind)
21213511Sgabeblack@google.com  //
21313511Sgabeblack@google.com  virtual void bind(base_type& s)
21413511Sgabeblack@google.com  {
21513511Sgabeblack@google.com    if (m_binders.size()) {
21613511Sgabeblack@google.com      //a multi socket is either bound hierarchically or directly
21713511Sgabeblack@google.com      display_error("Socket already directly bound.");
21813511Sgabeblack@google.com      return;
21913511Sgabeblack@google.com    }
22013511Sgabeblack@google.com    if (m_hierarch_bind){
22113511Sgabeblack@google.com      display_warning("Socket already bound hierarchically. Bind attempt ignored.");
22213511Sgabeblack@google.com      return;
22313511Sgabeblack@google.com    }
22413511Sgabeblack@google.com
22513511Sgabeblack@google.com    //remember to which socket we are hierarchically bound and disable it,
22613511Sgabeblack@google.com    // so that it won't try to register callbacks itself
22713511Sgabeblack@google.com    s.disable_cb_bind();
22813511Sgabeblack@google.com    m_hierarch_bind=&s;
22913511Sgabeblack@google.com    base_type::bind(s); //satisfy SystemC
23013511Sgabeblack@google.com  }
23113511Sgabeblack@google.com
23213511Sgabeblack@google.com  //operator notation for hierarchical bind
23313511Sgabeblack@google.com  void operator() (base_type& s)
23413511Sgabeblack@google.com  {
23513511Sgabeblack@google.com    bind(s);
23613511Sgabeblack@google.com  }
23713511Sgabeblack@google.com
23813511Sgabeblack@google.com  //get access to sub port
23913511Sgabeblack@google.com  tlm::tlm_fw_transport_if<TYPES>* operator[](int i){return m_used_sockets[i];}
24013511Sgabeblack@google.com
24113511Sgabeblack@google.com  //get the number of bound targets
24213511Sgabeblack@google.com  // NOTE: this is only valid at end of elaboration!
24313511Sgabeblack@google.com  unsigned int size() {return get_hierarch_bind()->get_sockets().size();}
24413511Sgabeblack@google.com
24513511Sgabeblack@google.comprotected:
24613511Sgabeblack@google.com  using base_type::display_warning;
24713511Sgabeblack@google.com  using base_type::display_error;
24813511Sgabeblack@google.com
24913511Sgabeblack@google.com  //implementation of base class interface
25013511Sgabeblack@google.com  base_type* get_hierarch_bind(){if (m_hierarch_bind) return m_hierarch_bind->get_hierarch_bind(); else return this;}
25113511Sgabeblack@google.com  void disable_cb_bind(){ m_beoe_disabled=true;}
25213511Sgabeblack@google.com  std::vector<callback_binder_bw<TYPES>* >& get_binders(){return m_binders;}
25313511Sgabeblack@google.com  std::vector<tlm::tlm_fw_transport_if<TYPES>*>& get_sockets(){return m_sockets;}
25413511Sgabeblack@google.com  //vector of connected sockets
25513511Sgabeblack@google.com  std::vector<tlm::tlm_fw_transport_if<TYPES>*> m_sockets;
25613511Sgabeblack@google.com  std::vector<tlm::tlm_fw_transport_if<TYPES>*> m_used_sockets;
25713511Sgabeblack@google.com  //vector of binders that convert untagged interface into tagged interface
25813511Sgabeblack@google.com  std::vector<callback_binder_bw<TYPES>*> m_binders;
25913511Sgabeblack@google.com
26013511Sgabeblack@google.com  base_type*  m_hierarch_bind; //pointer to hierarchical bound multi port
26113511Sgabeblack@google.com  bool m_beoe_disabled;  // bool that remembers whether this socket shall bind callbacks or not
26213511Sgabeblack@google.com  callback_binder_bw<TYPES> m_dummy; //a callback binder that is bound to the underlying export
26313511Sgabeblack@google.com                                     // in case there was no real bind
26413511Sgabeblack@google.com
26513511Sgabeblack@google.com  //callbacks as functors
26613511Sgabeblack@google.com  // (allows to pass the callback to another socket that does not know the type of the module that owns
26713511Sgabeblack@google.com  //  the callbacks)
26813511Sgabeblack@google.com  typename callback_binder_bw<TYPES>::nb_func_type  m_nb_f;
26913511Sgabeblack@google.com  typename callback_binder_bw<TYPES>::dmi_func_type m_dmi_f;
27013511Sgabeblack@google.com};
27113511Sgabeblack@google.com
27213511Sgabeblack@google.comtemplate <typename MODULE,
27313511Sgabeblack@google.com          unsigned int BUSWIDTH = 32,
27413511Sgabeblack@google.com          typename TYPES = tlm::tlm_base_protocol_types,
27513511Sgabeblack@google.com          unsigned int N=0>
27613511Sgabeblack@google.comclass multi_passthrough_initiator_socket_optional
27713511Sgabeblack@google.com  : public multi_passthrough_initiator_socket<MODULE,BUSWIDTH,TYPES,N,sc_core::SC_ZERO_OR_MORE_BOUND>
27813511Sgabeblack@google.com{
27913511Sgabeblack@google.com  typedef multi_passthrough_initiator_socket<MODULE,BUSWIDTH,TYPES,N,sc_core::SC_ZERO_OR_MORE_BOUND> socket_b;
28013511Sgabeblack@google.compublic:
28113511Sgabeblack@google.com  multi_passthrough_initiator_socket_optional() : socket_b() {}
28213511Sgabeblack@google.com  explicit multi_passthrough_initiator_socket_optional(const char* name) : socket_b(name) {}
28313511Sgabeblack@google.com};
28413511Sgabeblack@google.com
28513511Sgabeblack@google.com} // namespace tlm_utils
28613511Sgabeblack@google.com#endif // TLM_UTILS_MULTI_PASSTHROUGH_INITIATOR_SOCKET_H_INCLUDED_
287