C:/ESLX/projects/TLMWG/tlm2/include/tlm/tlm_utils/multi_socket_bases.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002 
00003   The following code is derived, directly or indirectly, from the SystemC
00004   source code Copyright (c) 1996-2008 by all Contributors.
00005   All Rights reserved.
00006 
00007   The contents of this file are subject to the restrictions and limitations
00008   set forth in the SystemC Open Source License Version 3.0 (the "License");
00009   You may not use this file except in compliance with such restrictions and
00010   limitations. You may obtain instructions on how to receive a copy of the
00011   License at http://www.systemc.org/. Software distributed by Contributors
00012   under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
00013   ANY KIND, either express or implied. See the License for the specific
00014   language governing rights and limitations under the License.
00015 
00016  *****************************************************************************/
00017 
00018 #ifndef __MULTI_SOCKET_BASES_H__
00019 #define __MULTI_SOCKET_BASES_H__
00020 
00021 #include <systemc>
00022 
00023 #if !(SYSTEMC_VERSION <= 20050714)
00024 # include "sysc/kernel/sc_boost.h"
00025 # include "sysc/kernel/sc_spawn.h"
00026 # include "sysc/kernel/sc_spawn_options.h"
00027 #endif
00028 #include "tlm.h"
00029 
00030 #include "boost/function.hpp"
00031 #include "boost/mem_fn.hpp"
00032 #include <map>
00033 
00034 namespace tlm_utils {
00035 
00036 
00037 /*
00038 This class implements the fw interface.
00039 It allows to register a callback for each of the fw interface methods.
00040 The callbacks simply forward the fw interface call, but add the id (an int)
00041 of the callback binder to the signature of the call.
00042 */
00043 template <typename TYPES>
00044 class callback_binder_fw: public tlm::tlm_fw_transport_if<TYPES>{
00045   public:
00046     //typedefs according to the used TYPES class
00047     typedef typename TYPES::tlm_payload_type              transaction_type;
00048     typedef typename TYPES::tlm_phase_type                phase_type;  
00049     typedef tlm::tlm_sync_enum                            sync_enum_type;
00050   
00051     //typedefs for the callbacks
00052     typedef boost::function<sync_enum_type (int i, transaction_type& txn, phase_type& ph, sc_core::sc_time& t)> nb_func_type;
00053     typedef boost::function<void (int i, transaction_type& txn, sc_core::sc_time& t)> b_func_type;
00054     typedef boost::function<unsigned int (int i, transaction_type& txn)> debug_func_type;
00055     typedef boost::function<bool (int i, transaction_type& txn, tlm::tlm_dmi& dmi)> dmi_func_type;
00056 
00057     //ctor: an ID is needed to create a callback binder
00058     callback_binder_fw(int id): m_id(id){
00059     }
00060 
00061     //the nb_transport method of the fw interface
00062     sync_enum_type nb_transport_fw(transaction_type& txn,
00063                                 phase_type& p,
00064                                 sc_core::sc_time& t){
00065       //check if a callback is registered
00066       if (m_nb_f->empty()){
00067         std::cerr<<"No function registered"<<std::endl;
00068         exit(1);
00069       }
00070       else
00071         return (*m_nb_f)(m_id, txn, p, t); //do the callback
00072     }
00073     
00074     //the b_transport method of the fw interface
00075     void b_transport(transaction_type& trans,sc_core::sc_time& t){
00076       //check if a callback is registered
00077       if (m_b_f->empty()){
00078         std::cerr<<"No function registered"<<std::endl;
00079         exit(1);
00080       }
00081       else
00082         (*m_b_f)(m_id, trans,t); //do the callback
00083     }
00084     
00085     //the DMI method of the fw interface
00086     bool get_direct_mem_ptr(transaction_type& trans, tlm::tlm_dmi&  dmi_data){
00087       //check if a callback is registered
00088       if (m_dmi_f->empty()){
00089         std::cerr<<"No function registered"<<std::endl; 
00090         exit(1);
00091       }
00092       else
00093         return (*m_dmi_f)(m_id, trans,dmi_data); //do the callback
00094     }
00095     
00096     //the debug method of the fw interface
00097     unsigned int transport_dbg(transaction_type& trans){
00098       //check if a callback is registered
00099       if (m_dbg_f->empty()){
00100         std::cerr<<"No function registered"<<std::endl; 
00101         exit(1);
00102       }
00103       else
00104         return (*m_dbg_f)(m_id, trans); //do the callback
00105     }
00106     
00107     //the SystemC standard callback register_port:
00108     // - called when a port if bound to the interface
00109     // - allowd to find out who is bound to that callback binder
00110     void register_port(sc_core::sc_port_base& b, const char* name){
00111       m_caller_port=&b;
00112     }
00113     
00114     //register callbacks for all fw interface methods at once
00115     void set_callbacks(nb_func_type& cb1, b_func_type& cb2, dmi_func_type& cb3, debug_func_type& cb4){
00116       m_nb_f=&cb1;
00117       m_b_f=&cb2;
00118       m_dmi_f=&cb3;
00119       m_dbg_f=&cb4;
00120     }
00121     
00122     //getter method to get the port that is bound to that callback binder
00123     // NOTE: this will only return a valid value at end of elaboration
00124     //  (but not before end of elaboration!)
00125     sc_core::sc_port_base* get_other_side(){return m_caller_port;}
00126     
00127   private:
00128     //the ID of the callback binder
00129     int m_id; 
00130     
00131     //the callbacks
00132     nb_func_type* m_nb_f; 
00133     b_func_type*  m_b_f;
00134     debug_func_type* m_dbg_f;
00135     dmi_func_type* m_dmi_f;
00136     
00137     //the port bound to that callback binder
00138     sc_core::sc_port_base* m_caller_port;   
00139 };
00140 
00141 /*
00142 This class implements the bw interface.
00143 It allows to register a callback for each of the bw interface methods.
00144 The callbacks simply forward the bw interface call, but add the id (an int)
00145 of the callback binder to the signature of the call.
00146 */
00147 template <typename TYPES>
00148 class callback_binder_bw: public tlm::tlm_bw_transport_if<TYPES>{
00149   public:
00150     //typedefs according to the used TYPES class
00151     typedef typename TYPES::tlm_payload_type              transaction_type;
00152     typedef typename TYPES::tlm_phase_type                phase_type;  
00153     typedef tlm::tlm_sync_enum                            sync_enum_type;
00154   
00155     //typedefs for the callbacks
00156     typedef boost::function<sync_enum_type (int i, transaction_type& txn, phase_type& p, sc_core::sc_time& t)> nb_func_type;
00157     typedef boost::function<void (int i, sc_dt::uint64 l, sc_dt::uint64 u)> dmi_func_type;
00158 
00159     //ctor: an ID is needed to create a callback binder
00160     callback_binder_bw(int id): m_id(id){
00161     }
00162 
00163     //the nb_transport method of the bw interface
00164     sync_enum_type nb_transport_bw(transaction_type& txn,
00165                                 phase_type& p,
00166                                 sc_core::sc_time& t){
00167       //check if a callback is registered
00168       if (m_nb_f->empty()){
00169         std::cerr<<"No function registered"<<std::endl; //here we could do an automatic nb->b conversion
00170         exit(1);
00171       }
00172       else
00173         return (*m_nb_f)(m_id, txn, p, t); //do the callback
00174     }
00175     
00176     //the DMI method of the bw interface
00177     void invalidate_direct_mem_ptr(sc_dt::uint64 l, sc_dt::uint64 u){
00178       //check if a callback is registered
00179       if (m_nb_f->empty()){
00180         std::cerr<<"No function registered"<<std::endl;
00181         exit(1);
00182       }
00183       else
00184         (*m_dmi_f)(m_id,l,u); //do the callback
00185     }
00186 
00187     //register callbacks for all bw interface methods at once
00188     void set_callbacks(nb_func_type& cb1, dmi_func_type& cb2){
00189       m_nb_f=&cb1;
00190       m_dmi_f=&cb2;
00191     }
00192     
00193   private:
00194     //the ID of the callback binder
00195     int m_id;
00196     //the callbacks
00197     nb_func_type* m_nb_f;
00198     dmi_func_type* m_dmi_f;
00199 };
00200 
00201 
00202 /*
00203 This class forms the base for multi initiator sockets.
00204 It enforces a multi initiator socket to implement all functions
00205 needed to do hierarchical bindings.
00206 */
00207 template <unsigned int BUSWIDTH = 32,
00208           typename TYPES = tlm::tlm_base_protocol_types,
00209           unsigned int N=0
00210 #if !(defined SYSTEMC_VERSION & SYSTEMC_VERSION <= 20050714)
00211           ,sc_core::sc_port_policy POL = sc_core::SC_ONE_OR_MORE_BOUND
00212 #endif
00213           >
00214 class multi_init_base: public tlm::tlm_initiator_socket<BUSWIDTH,
00215                                                   TYPES,
00216                                                   N
00217 #if !(defined SYSTEMC_VERSION & SYSTEMC_VERSION <= 20050714)
00218                                                   ,POL
00219 #endif
00220                                                   >{
00221 public:
00222   //typedef for the base type: the standard tlm initiator socket
00223   typedef tlm::tlm_initiator_socket<BUSWIDTH,
00224                               TYPES,
00225                               N
00226 #if !(defined SYSTEMC_VERSION & SYSTEMC_VERSION <= 20050714)
00227                               ,POL
00228 #endif
00229                               > base_type;
00230   
00231   //this method shall disable the code that does the callback binding
00232   // that registers callbacks to binders
00233   virtual void disable_cb_bind()=0;
00234   
00235   //this method shall return the multi_init_base to which the
00236   // multi_init_base is bound hierarchically
00237   //  If the base is not bound hierarchically it shall return a pointer to itself
00238   virtual multi_init_base* get_hierarch_bind()=0;
00239   
00240   //this method shall return a vector of the callback binders of multi initiator socket
00241   virtual std::vector<callback_binder_bw<TYPES>* >& get_binders()=0;
00242   
00243   //this method shall return a vector of all target interfaces bound to this multi init socket
00244   virtual std::vector<tlm::tlm_fw_transport_if<TYPES>*>&  get_sockets()=0;
00245   
00246   //ctor and dtor
00247   virtual ~multi_init_base(){}
00248   multi_init_base(const char* name):base_type(name){}
00249 };
00250 
00251 /*
00252 This class forms the base for multi target sockets.
00253 It enforces a multi target socket to implement all functions
00254 needed to do hierarchical bindings.
00255 */
00256 template <unsigned int BUSWIDTH = 32,
00257           typename TYPES = tlm::tlm_base_protocol_types,
00258           unsigned int N=0
00259 #if !(defined SYSTEMC_VERSION & SYSTEMC_VERSION <= 20050714)
00260           ,sc_core::sc_port_policy POL = sc_core::SC_ONE_OR_MORE_BOUND
00261 #endif
00262           >
00263 class multi_target_base: public tlm::tlm_target_socket<BUSWIDTH, 
00264                                                 TYPES,
00265                                                 N
00266 #if !(defined SYSTEMC_VERSION & SYSTEMC_VERSION <= 20050714)                                                
00267                                                 ,POL
00268 #endif
00269                                                 >{
00270 public:
00271   //typedef for the base type: the standard tlm target socket
00272   typedef tlm::tlm_target_socket<BUSWIDTH, 
00273                               TYPES,
00274                               N
00275 #if !(defined SYSTEMC_VERSION & SYSTEMC_VERSION <= 20050714)
00276                               ,POL
00277 #endif
00278                               > base_type;
00279   
00280   //this method shall return the multi_init_base to which the
00281   // multi_init_base is bound hierarchically
00282   //  If the base is not bound hierarchically it shall return a pointer to itself                                                
00283   virtual multi_target_base* get_hierarch_bind()=0;
00284   
00285   //this method shall inform the multi target socket that it is bound
00286   // hierarchically and to which other multi target socket it is bound hierarchically
00287   virtual void set_hierarch_bind(multi_target_base*)=0;
00288   
00289   //this method shall return a vector of the callback binders of multi initiator socket
00290   virtual std::vector<callback_binder_fw<TYPES>* >& get_binders()=0;
00291   
00292   //this method shall return a map of all multi initiator sockets that are bound to this multi target
00293   // the key of the map is the index at which the multi initiator i bound, while the value
00294   //  is the interface of the multi initiator socket that is bound at that index
00295   virtual std::map<unsigned int, tlm::tlm_bw_transport_if<TYPES>*>&  get_multi_binds()=0;
00296   
00297   //ctor and dtor
00298   virtual ~multi_target_base(){}
00299   multi_target_base(const char* name):base_type(name){}
00300 };
00301 
00302 /*
00303 All multi sockets must additionally derive from this class.
00304 It enforces a multi socket to implement a function 
00305 needed to do multi init to multi target bindings.
00306 */
00307 template <typename TYPES>
00308 class multi_to_multi_bind_base{
00309 public:
00310   virtual ~multi_to_multi_bind_base(){}
00311   virtual tlm::tlm_fw_transport_if<TYPES>* get_last_binder(tlm::tlm_bw_transport_if<TYPES>*)=0;
00312 };
00313 
00314 }
00315 
00316 #endif

Generated on Thu Jun 5 17:43:03 2008 for TLM 2 by  doxygen 1.5.3