tlm_port_wrapper.hh revision 14189
113819Sgabeblack@google.com/*
213819Sgabeblack@google.com * Copyright 2018 Google, Inc.
313819Sgabeblack@google.com *
413819Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
513819Sgabeblack@google.com * modification, are permitted provided that the following conditions are
613819Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
713819Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
813819Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
913819Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1013819Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1113819Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1213819Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1313819Sgabeblack@google.com * this software without specific prior written permission.
1413819Sgabeblack@google.com *
1513819Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1613819Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1713819Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1813819Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1913819Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2013819Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2113819Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2213819Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2313819Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2413819Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2513819Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2613819Sgabeblack@google.com *
2713819Sgabeblack@google.com * Authors: Gabe Black
2813819Sgabeblack@google.com */
2913819Sgabeblack@google.com
3013819Sgabeblack@google.com#ifndef __SYSTEMC_TLM_PORT_WRAPPER_HH__
3113819Sgabeblack@google.com#define __SYSTEMC_TLM_PORT_WRAPPER_HH__
3213819Sgabeblack@google.com
3313819Sgabeblack@google.com#include "base/logging.hh"
3413819Sgabeblack@google.com#include "sim/port.hh"
3513819Sgabeblack@google.com#include "systemc/ext/tlm_core/2/sockets/sockets.hh"
3613819Sgabeblack@google.com
3713819Sgabeblack@google.comnamespace sc_gem5
3813819Sgabeblack@google.com{
3913819Sgabeblack@google.com
4013819Sgabeblack@google.comtemplate <unsigned int BUSWIDTH=32,
4113819Sgabeblack@google.com          typename TYPES=tlm::tlm_base_protocol_types, int N=1,
4213819Sgabeblack@google.com          sc_core::sc_port_policy POL=sc_core::SC_ONE_OR_MORE_BOUND>
4313819Sgabeblack@google.comclass TlmInitiatorWrapper;
4413819Sgabeblack@google.com
4513819Sgabeblack@google.comtemplate <unsigned int BUSWIDTH=32,
4613819Sgabeblack@google.com          typename TYPES=tlm::tlm_base_protocol_types, int N=1,
4713819Sgabeblack@google.com          sc_core::sc_port_policy POL=sc_core::SC_ONE_OR_MORE_BOUND>
4813819Sgabeblack@google.comclass TlmTargetWrapper;
4913819Sgabeblack@google.com
5013819Sgabeblack@google.comtemplate <unsigned int BUSWIDTH, typename TYPES, int N,
5113819Sgabeblack@google.com          sc_core::sc_port_policy POL>
5213819Sgabeblack@google.comclass TlmInitiatorWrapper : public ::Port
5313819Sgabeblack@google.com{
5413819Sgabeblack@google.com  public:
5513819Sgabeblack@google.com    typedef tlm::tlm_base_initiator_socket<BUSWIDTH,
5613819Sgabeblack@google.com            tlm::tlm_fw_transport_if<TYPES>,
5713819Sgabeblack@google.com            tlm::tlm_bw_transport_if<TYPES>, N, POL>
5813819Sgabeblack@google.com        InitiatorSocket;
5913819Sgabeblack@google.com    typedef typename InitiatorSocket::base_target_socket_type TargetSocket;
6013819Sgabeblack@google.com    typedef TlmTargetWrapper<BUSWIDTH, TYPES, N, POL> TargetWrapper;
6113819Sgabeblack@google.com
6213819Sgabeblack@google.com    InitiatorSocket &initiator() { return _initiator; }
6313819Sgabeblack@google.com
6413819Sgabeblack@google.com    TlmInitiatorWrapper(
6513819Sgabeblack@google.com            InitiatorSocket &i, const std::string &_name, PortID _id) :
6613819Sgabeblack@google.com        Port(_name, _id), _initiator(i)
6713819Sgabeblack@google.com    {}
6813819Sgabeblack@google.com
6913819Sgabeblack@google.com    void
7013819Sgabeblack@google.com    bind(::Port &peer) override
7113819Sgabeblack@google.com    {
7213819Sgabeblack@google.com        auto *target = dynamic_cast<TargetWrapper *>(&peer);
7313819Sgabeblack@google.com        fatal_if(!target, "Attempt to bind TLM initiator socket %s to "
7413819Sgabeblack@google.com                "incompatible port %s.", name(), peer.name());
7513819Sgabeblack@google.com
7613819Sgabeblack@google.com        initiator().bind(target->target());
7714189Sgabeblack@google.com        Port::bind(peer);
7813819Sgabeblack@google.com    }
7913819Sgabeblack@google.com
8013819Sgabeblack@google.com    void
8113819Sgabeblack@google.com    unbind() override
8213819Sgabeblack@google.com    {
8313819Sgabeblack@google.com        panic("TLM sockets can't be unbound.");
8413819Sgabeblack@google.com    }
8513819Sgabeblack@google.com
8613819Sgabeblack@google.com  private:
8713819Sgabeblack@google.com    InitiatorSocket &_initiator;
8813819Sgabeblack@google.com};
8913819Sgabeblack@google.com
9013819Sgabeblack@google.comtemplate <unsigned int BUSWIDTH, typename TYPES, int N,
9113819Sgabeblack@google.com          sc_core::sc_port_policy POL>
9213819Sgabeblack@google.comclass TlmTargetWrapper : public ::Port
9313819Sgabeblack@google.com{
9413819Sgabeblack@google.com  public:
9513819Sgabeblack@google.com    typedef tlm::tlm_base_target_socket<BUSWIDTH,
9613819Sgabeblack@google.com            tlm::tlm_fw_transport_if<TYPES>,
9713819Sgabeblack@google.com            tlm::tlm_bw_transport_if<TYPES>, N, POL>
9813819Sgabeblack@google.com        TargetSocket;
9913819Sgabeblack@google.com
10013819Sgabeblack@google.com    TargetSocket &target() { return _target; }
10113819Sgabeblack@google.com
10213819Sgabeblack@google.com    TlmTargetWrapper(TargetSocket &t, const std::string &_name, PortID _id) :
10313819Sgabeblack@google.com        Port(_name, _id), _target(t)
10413819Sgabeblack@google.com    {}
10513819Sgabeblack@google.com
10613819Sgabeblack@google.com    void
10713819Sgabeblack@google.com    bind(::Port &peer) override
10813819Sgabeblack@google.com    {
10913819Sgabeblack@google.com        // Ignore attempts to bind a target socket. The initiator will
11013819Sgabeblack@google.com        // handle it.
11114189Sgabeblack@google.com        Port::bind(peer);
11213819Sgabeblack@google.com    }
11313819Sgabeblack@google.com
11413819Sgabeblack@google.com    void
11513819Sgabeblack@google.com    unbind() override
11613819Sgabeblack@google.com    {
11713819Sgabeblack@google.com        panic("TLM sockets can't be unbound.");
11813819Sgabeblack@google.com    }
11913819Sgabeblack@google.com
12013819Sgabeblack@google.com  private:
12113819Sgabeblack@google.com    TargetSocket &_target;
12213819Sgabeblack@google.com};
12313819Sgabeblack@google.com
12413819Sgabeblack@google.com} // namespace sc_gem5
12513819Sgabeblack@google.com
12613819Sgabeblack@google.com#endif  //__SYSTEMC_TLM_PORT_WRAPPER_HH__
127