extension_adaptors.h revision 12855
112955Sgabeblack@google.com/*****************************************************************************
212955Sgabeblack@google.com
312955Sgabeblack@google.com  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
412955Sgabeblack@google.com  more contributor license agreements.  See the NOTICE file distributed
512955Sgabeblack@google.com  with this work for additional information regarding copyright ownership.
612955Sgabeblack@google.com  Accellera licenses this file to you under the Apache License, Version 2.0
712955Sgabeblack@google.com  (the "License"); you may not use this file except in compliance with the
812955Sgabeblack@google.com  License.  You may obtain a copy of the License at
912955Sgabeblack@google.com
1012955Sgabeblack@google.com    http://www.apache.org/licenses/LICENSE-2.0
1112955Sgabeblack@google.com
1212955Sgabeblack@google.com  Unless required by applicable law or agreed to in writing, software
1312955Sgabeblack@google.com  distributed under the License is distributed on an "AS IS" BASIS,
1412955Sgabeblack@google.com  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
1512955Sgabeblack@google.com  implied.  See the License for the specific language governing
1612955Sgabeblack@google.com  permissions and limitations under the License.
1712955Sgabeblack@google.com
1812955Sgabeblack@google.com *****************************************************************************/
1912955Sgabeblack@google.com
2012955Sgabeblack@google.com#ifndef __EXTENSIONS_ADAPTORS_H__
2112955Sgabeblack@google.com#define __EXTENSIONS_ADAPTORS_H__
2212955Sgabeblack@google.com
2312955Sgabeblack@google.com#include "tlm.h"
2412955Sgabeblack@google.com#include "my_extension.h"
2512955Sgabeblack@google.com
2612955Sgabeblack@google.com#include "tlm_utils/simple_initiator_socket.h"
2712955Sgabeblack@google.com#include "tlm_utils/simple_target_socket.h"
2812955Sgabeblack@google.com
2912955Sgabeblack@google.comtemplate <unsigned int BUSWIDTH = 32>
3012955Sgabeblack@google.comclass adapt_ext2gp : public sc_core::sc_module
3112955Sgabeblack@google.com{
3212988Sgabeblack@google.com    public:
3312955Sgabeblack@google.com    typedef tlm::tlm_generic_payload                   initiator_payload_type;
3412955Sgabeblack@google.com    typedef tlm::tlm_generic_payload                   target_payload_type;
3512955Sgabeblack@google.com    typedef tlm_utils::simple_initiator_socket<adapt_ext2gp, BUSWIDTH,
3612957Sgabeblack@google.com                                  tlm::tlm_base_protocol_types> initiator_socket_type;
3712957Sgabeblack@google.com    typedef tlm_utils::simple_target_socket<adapt_ext2gp, BUSWIDTH,
3812955Sgabeblack@google.com                               my_extended_payload_types>  target_socket_type;
3912955Sgabeblack@google.com
4013086Sgabeblack@google.com    target_socket_type  target_socket;
4113086Sgabeblack@google.com    initiator_socket_type initiator_socket;
4212955Sgabeblack@google.com
4312955Sgabeblack@google.com    SC_HAS_PROCESS(adapt_ext2gp);
4412955Sgabeblack@google.com    adapt_ext2gp(sc_core::sc_module_name name_)
4512955Sgabeblack@google.com        : sc_core::sc_module(name_)
4613303Sgabeblack@google.com    {
4713303Sgabeblack@google.com      target_socket.register_nb_transport_fw(this, &adapt_ext2gp::forward_nb_transport);
4813303Sgabeblack@google.com      target_socket.register_transport_dbg(this, &adapt_ext2gp::transport_debug);
4912955Sgabeblack@google.com      target_socket.register_get_direct_mem_ptr(this, &adapt_ext2gp::get_dmi_pointer);
5013303Sgabeblack@google.com
5113303Sgabeblack@google.com      initiator_socket.register_nb_transport_bw(this, &adapt_ext2gp::backward_nb_transport);
5213086Sgabeblack@google.com      initiator_socket.register_invalidate_direct_mem_ptr(this, &adapt_ext2gp::invalidate_dmi_pointers);
5313303Sgabeblack@google.com    }
5413303Sgabeblack@google.com
5512955Sgabeblack@google.com    ///////////////
5613086Sgabeblack@google.com    // NB transport
5713086Sgabeblack@google.com    ///////////////
5813086Sgabeblack@google.com
5913303Sgabeblack@google.com    // Forward direction: The initiator calls this method with an extended
6012955Sgabeblack@google.com    // payload. We leave the extension class in the vector, and it will be
6113303Sgabeblack@google.com    // ignored by the GP target.
6213303Sgabeblack@google.com    tlm::tlm_sync_enum forward_nb_transport(initiator_payload_type& trans,
6313303Sgabeblack@google.com                                            tlm::tlm_phase& phase,
6413303Sgabeblack@google.com                                            sc_core::sc_time& t)
6513303Sgabeblack@google.com    {
6613303Sgabeblack@google.com        return initiator_socket->nb_transport_fw(trans, phase, t);
6713127Sgabeblack@google.com    }
6813303Sgabeblack@google.com    // Backward direction: we can  assume here that the payload we get
6913303Sgabeblack@google.com    // as parameter is the same one that the initiator sent out. Thus, the
7013303Sgabeblack@google.com    // extension vector is known to be present.
7113303Sgabeblack@google.com    tlm::tlm_sync_enum backward_nb_transport(target_payload_type& trans,
7213303Sgabeblack@google.com                                             tlm::tlm_phase& phase,
7313303Sgabeblack@google.com                                             sc_core::sc_time& t)
7413303Sgabeblack@google.com    {
7513303Sgabeblack@google.com        return target_socket->nb_transport_bw(trans, phase, t);
7613303Sgabeblack@google.com    }
7713303Sgabeblack@google.com
7813303Sgabeblack@google.com    bool get_dmi_pointer(target_payload_type& trans,
7913303Sgabeblack@google.com                         tlm::tlm_dmi& dmi_data)
8013303Sgabeblack@google.com    {
8113303Sgabeblack@google.com        bool tmp_ret = initiator_socket->get_direct_mem_ptr(trans,
8213303Sgabeblack@google.com                                                            dmi_data);
8313303Sgabeblack@google.com        return tmp_ret;
8413303Sgabeblack@google.com    }
8512955Sgabeblack@google.com
8612955Sgabeblack@google.com    //////////////////////////
8712955Sgabeblack@google.com    // simple call forwarders:
8812955Sgabeblack@google.com    //////////////////////////
8912955Sgabeblack@google.com    unsigned int transport_debug(target_payload_type& trans)
9012955Sgabeblack@google.com    {
9112955Sgabeblack@google.com        return initiator_socket->transport_dbg(trans);
9212955Sgabeblack@google.com    }
9312955Sgabeblack@google.com    void invalidate_dmi_pointers(sc_dt::uint64 start_range,
9412955Sgabeblack@google.com                                 sc_dt::uint64 end_range)
9512955Sgabeblack@google.com    {
9612955Sgabeblack@google.com        target_socket->invalidate_direct_mem_ptr(start_range,
9712988Sgabeblack@google.com                                                 end_range);
9813303Sgabeblack@google.com    }
9912988Sgabeblack@google.com};
10012988Sgabeblack@google.com
10112988Sgabeblack@google.comtemplate <unsigned int BUSWIDTH = 32>
10212988Sgabeblack@google.comclass adapt_gp2ext : public sc_core::sc_module
10312955Sgabeblack@google.com{
10412955Sgabeblack@google.com    public:
10512955Sgabeblack@google.com    typedef tlm::tlm_generic_payload                   initiator_payload_type;
10612955Sgabeblack@google.com    typedef tlm::tlm_generic_payload                   target_payload_type;
10712955Sgabeblack@google.com    typedef tlm_utils::simple_initiator_socket<adapt_gp2ext, BUSWIDTH,
10812955Sgabeblack@google.com                                  my_extended_payload_types> initiator_socket_type;
10912957Sgabeblack@google.com    typedef tlm_utils::simple_target_socket<adapt_gp2ext, BUSWIDTH,
11013063Sgabeblack@google.com                               tlm::tlm_base_protocol_types> target_socket_type;
11113063Sgabeblack@google.com
11212955Sgabeblack@google.com    target_socket_type  target_socket;
11312955Sgabeblack@google.com    initiator_socket_type initiator_socket;
11412955Sgabeblack@google.com
11512955Sgabeblack@google.com    SC_HAS_PROCESS(adapt_gp2ext);
11612955Sgabeblack@google.com    adapt_gp2ext(sc_core::sc_module_name name_)
11712955Sgabeblack@google.com        : sc_core::sc_module(name_)
11812955Sgabeblack@google.com    {
11912955Sgabeblack@google.com        // Optionally, we can initialize our private extension class
12012955Sgabeblack@google.com        // here, if required.
12112955Sgabeblack@google.com
12212955Sgabeblack@google.com      target_socket.register_nb_transport_fw(this, &adapt_gp2ext::forward_nb_transport);
12312955Sgabeblack@google.com      target_socket.register_transport_dbg(this, &adapt_gp2ext::transport_debug);
12412955Sgabeblack@google.com      target_socket.register_get_direct_mem_ptr(this, &adapt_gp2ext::get_dmi_pointer);
12512955Sgabeblack@google.com
12612955Sgabeblack@google.com      initiator_socket.register_nb_transport_bw(this, &adapt_gp2ext::backward_nb_transport);
12712955Sgabeblack@google.com      initiator_socket.register_invalidate_direct_mem_ptr(this, &adapt_gp2ext::invalidate_dmi_pointers);
12812955Sgabeblack@google.com
12913303Sgabeblack@google.com        m_ext.m_data = 13;
13012955Sgabeblack@google.com    }
13112955Sgabeblack@google.com
13212955Sgabeblack@google.com    ///////////////
13312955Sgabeblack@google.com    // NB transport
13412955Sgabeblack@google.com    ///////////////
13512955Sgabeblack@google.com
13612955Sgabeblack@google.com    // Forward direction: We extend the payload on the fly (if needed).
13712955Sgabeblack@google.com    tlm::tlm_sync_enum forward_nb_transport(initiator_payload_type& trans,
13812955Sgabeblack@google.com                                            tlm::tlm_phase& phase,
13913208Sgabeblack@google.com                                            sc_core::sc_time& t)
14013208Sgabeblack@google.com    {
14113208Sgabeblack@google.com        // If the mandatory extension is not there, we need to add it and
14213208Sgabeblack@google.com        // store it so that we can re-construc the original state.
14313208Sgabeblack@google.com        // Otherwise we don't touch the extension, so that we don't overwrite
14413208Sgabeblack@google.com        // it in e.g. a nonGP->GP->nonGP (initiator->interconnect->target)
14513208Sgabeblack@google.com        // setup.
14613208Sgabeblack@google.com        // Note, however, that there might be situations where we might need to
14713208Sgabeblack@google.com        // re-initialize the extension, e.g. for mutable data fields in
14813208Sgabeblack@google.com        // different system setups.
14913208Sgabeblack@google.com        trans.get_extension(m_initiator_ext);
15013208Sgabeblack@google.com        if (!m_initiator_ext)
15113208Sgabeblack@google.com        {
15213208Sgabeblack@google.com            m_initiator_ext = trans.set_extension(&m_ext);
15313208Sgabeblack@google.com        }
15413208Sgabeblack@google.com        tlm::tlm_sync_enum tmp =
15513208Sgabeblack@google.com        initiator_socket->nb_transport_fw(trans, phase, t);
15613208Sgabeblack@google.com        if (tmp == tlm::TLM_COMPLETED)
15713208Sgabeblack@google.com        {
15813208Sgabeblack@google.com            m_initiator_ext = trans.set_extension(m_initiator_ext);
15913208Sgabeblack@google.com        }
16012955Sgabeblack@google.com        return tmp;
16112955Sgabeblack@google.com    }
16213187Sgabeblack@google.com    // Backward direction: only restore of original extension and static_cast.
16313187Sgabeblack@google.com    tlm::tlm_sync_enum backward_nb_transport(target_payload_type& trans,
16413187Sgabeblack@google.com                                             tlm::tlm_phase& phase,
16513187Sgabeblack@google.com                                             sc_core::sc_time& t)
16613187Sgabeblack@google.com    {
16713073Sgabeblack@google.com        m_initiator_ext = trans.set_extension(m_initiator_ext);
16813073Sgabeblack@google.com        return target_socket->nb_transport_bw(trans, phase, t);
16913073Sgabeblack@google.com    }
17013073Sgabeblack@google.com
17113243Sgabeblack@google.com    bool get_dmi_pointer(target_payload_type& trans,
17213208Sgabeblack@google.com                         tlm::tlm_dmi& dmi_data)
17313208Sgabeblack@google.com    {
17413208Sgabeblack@google.com        // If the mandatory extension is not there, we need to add it and
17513208Sgabeblack@google.com        // store it so that we can re-construc the original state.
17612955Sgabeblack@google.com        // Otherwise we don't touch the extension, so that we don't overwrite
17712955Sgabeblack@google.com        // it in e.g. a nonGP->GP->nonGP (initiator->interconnect->target)
17812955Sgabeblack@google.com        // setup.
17912955Sgabeblack@google.com        my_extension* tmp_ext;
18012955Sgabeblack@google.com        trans.get_extension(tmp_ext);
18113063Sgabeblack@google.com        if (!tmp_ext)
18213063Sgabeblack@google.com        {
18312957Sgabeblack@google.com            trans.set_extension(&m_ext);
18412957Sgabeblack@google.com        }
18513063Sgabeblack@google.com        bool tmp_ret = initiator_socket->get_direct_mem_ptr(trans,
18612957Sgabeblack@google.com                                                            dmi_data);
18713063Sgabeblack@google.com        if(!tmp_ext)
18812955Sgabeblack@google.com        {
18912955Sgabeblack@google.com            trans.clear_extension(tmp_ext);
19012955Sgabeblack@google.com        }
19113299Sgabeblack@google.com        return tmp_ret;
19213299Sgabeblack@google.com    }
19313299Sgabeblack@google.com    //////////////////////////
19413299Sgabeblack@google.com    // simple call forwarders:
19513299Sgabeblack@google.com    //////////////////////////
19613299Sgabeblack@google.com    unsigned int transport_debug(target_payload_type& trans)
19713299Sgabeblack@google.com    {
19813299Sgabeblack@google.com        return initiator_socket->transport_dbg(trans);
19913299Sgabeblack@google.com    }
20013299Sgabeblack@google.com    void invalidate_dmi_pointers(sc_dt::uint64 start_range,
20112955Sgabeblack@google.com                                 sc_dt::uint64 end_range)
20212955Sgabeblack@google.com    {
20313063Sgabeblack@google.com        target_socket->invalidate_direct_mem_ptr(start_range,
20413063Sgabeblack@google.com                                                 end_range);
20512955Sgabeblack@google.com    }
20612955Sgabeblack@google.com
20712955Sgabeblack@google.comprivate:
20812955Sgabeblack@google.com    my_extension  m_ext;
20912955Sgabeblack@google.com    my_extension* m_initiator_ext;
21013243Sgabeblack@google.com};
21112955Sgabeblack@google.com
21212955Sgabeblack@google.com#endif
21313295Sgabeblack@google.com