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