TLM-2.0  2.0.4
Accellera TLM-2.0 proof-of-concept library
multi_passthrough_initiator_socket.h
Go to the documentation of this file.
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 TLM_UTILS_MULTI_PASSTHROUGH_INITIATOR_SOCKET_H_INCLUDED_
20 #define TLM_UTILS_MULTI_PASSTHROUGH_INITIATOR_SOCKET_H_INCLUDED_
21 
22 #include "multi_socket_bases.h"
23 
24 namespace tlm_utils {
25 
26 /*
27 This class implements a trivial multi initiator socket.
28 The triviality refers to the fact that the socket does not
29 do blocking to non-blocking or non-blocking to blocking conversions.
30 
31 It allows to connect multiple targets to this socket.
32 The user has to register callbacks for the bw interface methods
33 he likes to use. The callbacks are basically equal to the bw interface
34 methods but carry an additional integer that indicates to which
35 index of this socket the calling target is connected.
36 */
37 template <typename MODULE,
38  unsigned int BUSWIDTH = 32,
39  typename TYPES = tlm::tlm_base_protocol_types,
42  : public multi_init_base< BUSWIDTH, TYPES, N, POL>
43 {
44 public:
45 
46  //typedefs
47  // tlm 2.0 types for nb_transport
48  typedef typename TYPES::tlm_payload_type transaction_type;
49  typedef typename TYPES::tlm_phase_type phase_type;
51 
52  // typedefs to keep the fn ptr notations short
53  typedef sync_enum_type (MODULE::*nb_cb)(int,
54  transaction_type&,
55  phase_type&,
57  typedef void (MODULE::*dmi_cb)(int, sc_dt::uint64, sc_dt::uint64);
58 
60 
62 
63  static const char* default_name()
64  { return sc_core::sc_gen_unique_name("multi_passthrough_initiator_socket"); }
65 
66  //CTOR
68  : base_type(name)
69  , m_hierarch_bind(0)
70  , m_beoe_disabled(false)
71  , m_dummy(this,42)
72  {
73  }
74 
76  //clean up everything allocated by 'new'
77  for (unsigned int i=0; i<m_binders.size(); i++) delete m_binders[i];
78  }
79 
80  //register callback for nb transport of bw interface
81  void register_nb_transport_bw(MODULE* mod,
82  sync_enum_type (MODULE::*cb)(int,
83  transaction_type&,
84  phase_type&,
86  {
87  //warn if there already is a callback
88  if (m_nb_f.is_valid()){
89  display_warning("NBTransport_bw callback already registered.");
90  return;
91  }
92 
93  //set the functor
94  m_nb_f.set_function(mod, cb);
95  }
96 
97  //register callback for dmi function of bw interface
99  void (MODULE::*cb)(int, sc_dt::uint64, sc_dt::uint64))
100  {
101  //warn if there already is a callback
102  if (m_dmi_f.is_valid()){
103  display_warning("InvalidateDMI callback already registered.");
104  return;
105  }
106 
107  //set the functor
108  m_dmi_f.set_function(mod, cb);
109  }
110 
111  //Override virtual functions of the tlm_initiator_socket:
112  // this function is called whenever an sc_port (as part of a target socket)
113  // wants to bind to the export of the underlying tlm_initiator_socket
114  //At this time a callback binder is created an returned to the sc_port
115  // of the target socket, so that it binds to the callback binder
117  {
118  m_binders.push_back(new callback_binder_bw<TYPES>(this, m_binders.size()));
119  return *m_binders[m_binders.size()-1];
120  }
121 
122  // const overload not allowed for multi-sockets
124  {
125  display_error("'get_base_interface()' const not allowed for multi-sockets.");
127  }
128 
129  //Override virtual functions of the tlm_initiator_socket:
130  // this function is called whenever an sc_export (as part of a initiator socket)
131  // wants to bind to the export of the underlying tlm_initiator_socket
132  // i.e. a hierarchical bind takes place
134  {
135  if (!m_beoe_disabled) //we are not bound hierarchically
136  base_type::m_export.bind(m_dummy); //so we bind the dummy to avoid a SystemC error
137  return base_type::get_base_export(); //and then return our own export so that the hierarchical binding is set up properly
138  }
139 
141  {
143  }
144 
145  //bind against a target socket
146  virtual void bind(base_target_socket_type& s)
147  {
148  //error if this socket is already bound hierarchically
149  if (m_hierarch_bind) {
150  display_error("Already hierarchically bound.");
151  return;
152  }
153 
154  base_type::bind(s); //satisfy systemC, leads to a call to get_base_interface()
155 
156  //try to cast the target socket into a fw interface
158  if (!p_ex_s) {
159  display_error("Multi socket not bound to tlm_socket.");
160  return;
161  }
162 
163  //try a cast into a multi sockets
165  if (test) //did we just do a multi-multi bind??
166  //if that is the case the multi target socket must have just created a callback binder
167  // which we want to get from it.
168  //Moreover, we also just created one, which we will pass to it.
169  m_sockets.push_back(test->get_last_binder(m_binders[m_binders.size()-1]));
170  else{ // if not just bind normally
172  m_sockets.push_back(&((tlm::tlm_fw_transport_if<TYPES>&)ex_s)); //store the interface we are bound against
173  }
174  }
175 
176  //operator notation for direct bind
177  void operator() (base_target_socket_type& s)
178  {
179  bind(s);
180  }
181 
182  //SystemC standard callback before end of elaboration
184  //if our export hasn't been bound yet (due to a hierarch binding)
185  // we bind it now to avoid a SystemC error.
186  //We must do that, because it is legal not to register a callback on this socket
187  // as the user might only use b_transport
190  }
191 
192  //'break' here if the socket was told not to do callback binding
193  if (m_beoe_disabled) return;
194 
195  //get the callback binders of the top of the hierachical bind chain
196  // NOTE: this could be the same socket if there is no hierachical bind
197  std::vector<callback_binder_bw<TYPES>* >& binders=get_hierarch_bind()->get_binders();
198 
199  //get the interfaces bound to the top of the hierachical bind chain
200  // NOTE: this could be the same socket if there is no hierachical bind
202 
203  //register the callbacks of this socket with the callback binders
204  // we just got from the top of the hierachical bind chain
205  for (unsigned int i=0; i<binders.size(); i++) {
206  binders[i]->set_callbacks(m_nb_f, m_dmi_f);
207  }
208  }
209 
210  //
211  // Bind multi initiator socket to multi initiator socket (hierarchical bind)
212  //
213  virtual void bind(base_type& s)
214  {
215  if (m_binders.size()) {
216  //a multi socket is either bound hierarchically or directly
217  display_error("Socket already directly bound.");
218  return;
219  }
220  if (m_hierarch_bind){
221  display_warning("Socket already bound hierarchically. Bind attempt ignored.");
222  return;
223  }
224 
225  //remember to which socket we are hierarchically bound and disable it,
226  // so that it won't try to register callbacks itself
227  s.disable_cb_bind();
228  m_hierarch_bind=&s;
229  base_type::bind(s); //satisfy SystemC
230  }
231 
232  //operator notation for hierarchical bind
233  void operator() (base_type& s)
234  {
235  bind(s);
236  }
237 
238  //get access to sub port
240 
241  //get the number of bound targets
242  // NOTE: this is only valid at end of elaboration!
243  unsigned int size() {return get_hierarch_bind()->get_sockets().size();}
244 
245 protected:
248 
249  //implementation of base class interface
250  base_type* get_hierarch_bind(){if (m_hierarch_bind) return m_hierarch_bind->get_hierarch_bind(); else return this;}
252  std::vector<callback_binder_bw<TYPES>* >& get_binders(){return m_binders;}
253  std::vector<tlm::tlm_fw_transport_if<TYPES>*>& get_sockets(){return m_sockets;}
254  //vector of connected sockets
255  std::vector<tlm::tlm_fw_transport_if<TYPES>*> m_sockets;
256  std::vector<tlm::tlm_fw_transport_if<TYPES>*> m_used_sockets;
257  //vector of binders that convert untagged interface into tagged interface
258  std::vector<callback_binder_bw<TYPES>*> m_binders;
259 
260  base_type* m_hierarch_bind; //pointer to hierarchical bound multi port
261  bool m_beoe_disabled; // bool that remembers whether this socket shall bind callbacks or not
262  callback_binder_bw<TYPES> m_dummy; //a callback binder that is bound to the underlying export
263  // in case there was no real bind
264 
265  //callbacks as functors
266  // (allows to pass the callback to another socket that does not know the type of the module that owns
267  // the callbacks)
270 };
271 
272 template <typename MODULE,
273  unsigned int BUSWIDTH = 32,
274  typename TYPES = tlm::tlm_base_protocol_types,
275  unsigned int N=0>
277  : public multi_passthrough_initiator_socket<MODULE,BUSWIDTH,TYPES,N,sc_core::SC_ZERO_OR_MORE_BOUND>
278 {
280 public:
282  explicit multi_passthrough_initiator_socket_optional(const char* name) : socket_b(name) {}
283 };
284 
285 } // namespace tlm_utils
286 #endif // TLM_UTILS_MULTI_PASSTHROUGH_INITIATOR_SOCKET_H_INCLUDED_
tlm_base_target_socket_b< BUSWIDTH, fw_interface_type, bw_interface_type > base_target_socket_type
invalidate_dmi_functor< TYPES > dmi_func_type
std::vector< tlm::tlm_fw_transport_if< TYPES > * > m_used_sockets
std::vector< tlm::tlm_fw_transport_if< TYPES > * > & get_sockets()
virtual multi_init_base * get_hierarch_bind()=0
void display_warning(const char *msg) const
virtual tlm::tlm_bw_transport_if< TYPES > & get_base_interface()
nb_transport_functor< TYPES > nb_func_type
std::vector< tlm::tlm_fw_transport_if< TYPES > * > m_sockets
std::vector< callback_binder_bw< TYPES > *> & get_binders()
SC_ONE_OR_MORE_BOUND
virtual const tlm::tlm_bw_transport_if< TYPES > & get_base_interface() const
std::vector< callback_binder_bw< TYPES > * > m_binders
uint64_t uint64
SC_VIRTUAL_ void bind(IF &interface_)
void(MODULE::* dmi_cb)(int, sc_dt::uint64, sc_dt::uint64)
tlm::tlm_fw_transport_if< TYPES > * operator[](int i)
virtual std::vector< tlm::tlm_fw_transport_if< TYPES > * > & get_sockets()=0
virtual std::vector< callback_binder_bw< TYPES > *> & get_binders()=0
void register_invalidate_direct_mem_ptr(MODULE *mod, void(MODULE::*cb)(int, sc_dt::uint64, sc_dt::uint64))
virtual const sc_core::sc_export< tlm::tlm_bw_transport_if< TYPES > > & get_base_export() const
void display_error(const char *msg) const
virtual tlm::tlm_fw_transport_if< TYPES > * get_last_binder(tlm::tlm_bw_transport_if< TYPES > *)=0
void register_nb_transport_bw(MODULE *mod, sync_enum_type(MODULE::*cb)(int, transaction_type &, phase_type &, sc_core::sc_time &))
multi_init_base< BUSWIDTH, TYPES, N, POL > base_type
const char * name() const
sync_enum_type(MODULE::* nb_cb)(int, transaction_type &, phase_type &, sc_core::sc_time &)
SC_API const char * sc_gen_unique_name(const char *, bool preserve_first)
tlm_sync_enum
Definition: tlm_fw_bw_ifs.h:29
virtual sc_core::sc_export< tlm::tlm_bw_transport_if< TYPES > > & get_base_export()
virtual sc_interface * get_interface()