1/***************************************************************************** 2 3 Licensed to Accellera Systems Initiative Inc. (Accellera) under one or 4 more contributor license agreements. See the NOTICE file distributed 5 with this work for additional information regarding copyright ownership. 6 Accellera licenses this file to you under the Apache License, Version 2.0 7 (the "License"); you may not use this file except in compliance with the 8 License. You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 implied. See the License for the specific language governing 16 permissions and limitations under the License. 17 18 *****************************************************************************/ 19#ifndef __MULTI_PASSTHROUGH_INITIATOR_SOCKET_H__ 20#define __MULTI_PASSTHROUGH_INITIATOR_SOCKET_H__ 21 22#include "multi_socket_bases.h" 23 24namespace tlm_utils { 25 26/* 27This class implements a trivial multi initiator socket. 28The triviality refers to the fact that the socket does not 29do blocking to non-blocking or non-blocking to blocking conversions. 30 31It allows to connect multiple targets to this socket. 32The user has to register callbacks for the bw interface methods 33he likes to use. The callbacks are basically equal to the bw interface 34methods but carry an additional integer that indicates to which 35index of this socket the calling target is connected. 36*/ 37template <typename MODULE, 38 unsigned int BUSWIDTH = 32, 39 typename TYPES = tlm::tlm_base_protocol_types, 40 unsigned int N=0 41#if !(defined SYSTEMC_VERSION & SYSTEMC_VERSION <= 20050714) 42 ,sc_core::sc_port_policy POL = sc_core::SC_ONE_OR_MORE_BOUND 43#endif 44 > 45class multi_passthrough_initiator_socket: public multi_init_base< BUSWIDTH, 46 TYPES, 47 N 48#if !(defined SYSTEMC_VERSION & SYSTEMC_VERSION <= 20050714) 49 ,POL 50#endif 51 > 52{ 53 54public: 55 56 //typedefs 57 // tlm 2.0 types for nb_transport 58 typedef typename TYPES::tlm_payload_type transaction_type; 59 typedef typename TYPES::tlm_phase_type phase_type; 60 typedef tlm::tlm_sync_enum sync_enum_type; 61 62 // typedefs to keep the fn ptr notations short 63 typedef sync_enum_type (MODULE::*nb_cb)(int, 64 transaction_type&, 65 phase_type&, 66 sc_core::sc_time&); 67 typedef void (MODULE::*dmi_cb)(int, sc_dt::uint64, sc_dt::uint64); 68 69 typedef multi_init_base<BUSWIDTH, 70 TYPES, 71 N 72#if !(defined SYSTEMC_VERSION & SYSTEMC_VERSION <= 20050714) 73 ,POL 74#endif 75 > base_type; 76 77 typedef typename base_type::base_target_socket_type base_target_socket_type; 78 79 //CTOR 80 multi_passthrough_initiator_socket() 81 : base_type(sc_core::sc_gen_unique_name("multi_passthrough_initiator_socket")) 82 , m_hierarch_bind(0) 83 , m_beoe_disabled(false) 84 , m_dummy(42) 85 { 86 } 87 88 //CTOR 89 multi_passthrough_initiator_socket(const char* name) 90 : base_type(name) 91 , m_hierarch_bind(0) 92 , m_beoe_disabled(false) 93 , m_dummy(42) 94 { 95 } 96 97 ~multi_passthrough_initiator_socket(){ 98 //clean up everything allocated by 'new' 99 for (unsigned int i=0; i<m_binders.size(); i++) delete m_binders[i]; 100 } 101 102 //simple helpers for warnings an errors to shorten in code notation 103 void display_warning(const std::string& text) const { 104 std::stringstream s; 105 s<<"WARNING in instance "<<base_type::name()<<": "<<text; 106 SC_REPORT_WARNING("/OSCI_TLM-2/multi_socket", s.str().c_str()); 107 } 108 109 void display_error(const std::string& text) const { 110 std::stringstream s; 111 s<<"ERROR in instance "<<base_type::name()<<": "<<text; 112 SC_REPORT_ERROR("/OSCI_TLM-2/multi_socket", s.str().c_str()); 113 } 114 115 116 //register callback for nb transport of bw interface 117 void register_nb_transport_bw(MODULE* mod, 118 sync_enum_type (MODULE::*cb)(int, 119 transaction_type&, 120 phase_type&, 121 sc_core::sc_time&)) 122 { 123 //warn if there already is a callback 124 if (!m_nb_f.empty()){ 125 display_warning("NBTransport_bw callback already registered."); 126 return; 127 } 128 129 //set the functor 130 m_nb_f.set_function(mod, cb); 131 } 132 133 //register callback for dmi function of bw interface 134 void register_invalidate_direct_mem_ptr(MODULE* mod, 135 void (MODULE::*cb)(int, sc_dt::uint64, sc_dt::uint64)) 136 { 137 //warn if there already is a callback 138 if (!m_dmi_f.empty()){ 139 display_warning("InvalidateDMI callback already registered."); 140 return; 141 } 142 143 //set the functor 144 m_dmi_f.set_function(mod, cb); 145 } 146 147 //Override virtual functions of the tlm_initiator_socket: 148 // this function is called whenever an sc_port (as part of a target socket) 149 // wants to bind to the export of the underlying tlm_initiator_socket 150 //At this time a callback binder is created an returned to the sc_port 151 // of the target socket, so that it binds to the callback binder 152 virtual tlm::tlm_bw_transport_if<TYPES>& get_base_interface() 153 { 154 m_binders.push_back(new callback_binder_bw<TYPES>(m_binders.size())); 155 return *m_binders[m_binders.size()-1]; 156 } 157 158 // const overload not allowed for multi-sockets 159 virtual const tlm::tlm_bw_transport_if<TYPES>& get_base_interface() const 160 { 161 display_error("'get_base_interface()' const not allowed for multi-sockets."); 162 return base_type::get_base_interface(); 163 } 164 165 //Override virtual functions of the tlm_initiator_socket: 166 // this function is called whenever an sc_export (as part of a initiator socket) 167 // wants to bind to the export of the underlying tlm_initiator_socket 168 // i.e. a hierarchical bind takes place 169 virtual sc_core::sc_export<tlm::tlm_bw_transport_if<TYPES> >& get_base_export() 170 { 171 if (!m_beoe_disabled) //we are not bound hierarchically 172 base_type::m_export.bind(m_dummy); //so we bind the dummy to avoid a SystemC error 173 return base_type::get_base_export(); //and then return our own export so that the hierarchical binding is set up properly 174 } 175 176 virtual const sc_core::sc_export<tlm::tlm_bw_transport_if<TYPES> >& get_base_export() const 177 { 178 return base_type::get_base_export(); 179 } 180 181 //bind against a target socket 182 virtual void bind(base_target_socket_type& s) 183 { 184 //error if this socket is already bound hierarchically 185 if (m_hierarch_bind) 186 display_error("Already hierarchically bound."); 187 188 base_type::bind(s); //satisfy systemC, leads to a call to get_base_interface() 189 190 //try to cast the target socket into a fw interface 191 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); 192 if (!p_ex_s) display_error("Multi socket not bound to tlm_socket."); 193 194 //try a cast into a multi sockets 195 multi_to_multi_bind_base<TYPES>* test=dynamic_cast<multi_to_multi_bind_base<TYPES>*> (p_ex_s); 196 if (test) //did we just do a multi-multi bind?? 197 //if that is the case the multi target socket must have just created a callback binder 198 // which we want to get from it. 199 //Moreover, we also just created one, which we will pass to it. 200 m_sockets.push_back(test->get_last_binder(m_binders[m_binders.size()-1])); 201 else{ // if not just bind normally 202 sc_core::sc_export<tlm::tlm_fw_transport_if<TYPES> >& ex_s=*p_ex_s; 203 m_sockets.push_back(&((tlm::tlm_fw_transport_if<TYPES>&)ex_s)); //store the interface we are bound against 204 } 205 } 206 207 //operator notation for direct bind 208 void operator() (base_target_socket_type& s) 209 { 210 bind(s); 211 } 212 213 //SystemC standard callback before end of elaboration 214 void before_end_of_elaboration(){ 215 //if our export hasn't been bound yet (due to a hierarch binding) 216 // we bind it now to avoid a SystemC error. 217 //We must do that, because it is legal not to register a callback on this socket 218 // as the user might only use b_transport 219 if (!base_type::m_export.get_interface()){ 220 base_type::m_export.bind(m_dummy); 221 } 222 223 //'break' here if the socket was told not to do callback binding 224 if (m_beoe_disabled) return; 225 226 //get the callback binders of the top of the hierachical bind chain 227 // NOTE: this could be the same socket if there is no hierachical bind 228 std::vector<callback_binder_bw<TYPES>* >& binders=get_hierarch_bind()->get_binders(); 229 230 //get the interfaces bound to the top of the hierachical bind chain 231 // NOTE: this could be the same socket if there is no hierachical bind 232 m_used_sockets=get_hierarch_bind()->get_sockets(); 233 234 //register the callbacks of this socket with the callback binders 235 // we just got from the top of the hierachical bind chain 236 for (unsigned int i=0; i<binders.size(); i++) { 237 binders[i]->set_callbacks(m_nb_f, m_dmi_f); 238 } 239 } 240 241 // 242 // Bind multi initiator socket to multi initiator socket (hierarchical bind) 243 // 244 virtual void bind(base_type& s) 245 { 246 if (m_binders.size()) //a multi socket is either bound hierarchically or directly 247 display_error("Socket already directly bound."); 248 if (m_hierarch_bind){ 249 display_warning("Socket already bound hierarchically. Bind attempt ignored."); 250 return; 251 } 252 253 //remember to which socket we are hierarchically bound and disable it, 254 // so that it won't try to register callbacks itself 255 s.disable_cb_bind(); 256 m_hierarch_bind=&s; 257 base_type::bind(s); //satisfy SystemC 258 } 259 260 //operator notation for hierarchical bind 261 void operator() (base_type& s) 262 { 263 bind(s); 264 } 265 266 //get access to sub port 267 tlm::tlm_fw_transport_if<TYPES>* operator[](int i){return m_used_sockets[i];} 268 269 //get the number of bound targets 270 // NOTE: this is only valid at end of elaboration! 271 unsigned int size() {return get_hierarch_bind()->get_sockets().size();} 272 273protected: 274 //implementation of base class interface 275 base_type* get_hierarch_bind(){if (m_hierarch_bind) return m_hierarch_bind->get_hierarch_bind(); else return this;} 276 void disable_cb_bind(){ m_beoe_disabled=true;} 277 std::vector<callback_binder_bw<TYPES>* >& get_binders(){return m_binders;} 278 std::vector<tlm::tlm_fw_transport_if<TYPES>*>& get_sockets(){return m_sockets;} 279 //vector of connected sockets 280 std::vector<tlm::tlm_fw_transport_if<TYPES>*> m_sockets; 281 std::vector<tlm::tlm_fw_transport_if<TYPES>*> m_used_sockets; 282 //vector of binders that convert untagged interface into tagged interface 283 std::vector<callback_binder_bw<TYPES>*> m_binders; 284 285 base_type* m_hierarch_bind; //pointer to hierarchical bound multi port 286 bool m_beoe_disabled; // bool that remembers whether this socket shall bind callbacks or not 287 callback_binder_bw<TYPES> m_dummy; //a callback binder that is bound to the underlying export 288 // in case there was no real bind 289 290 //callbacks as functors 291 // (allows to pass the callback to another socket that does not know the type of the module that owns 292 // the callbacks) 293 typename callback_binder_bw<TYPES>::nb_func_type m_nb_f; 294 typename callback_binder_bw<TYPES>::dmi_func_type m_dmi_f; 295}; 296 297} 298 299#endif 300