tlm: Initial import of tlm/gem5 bridge code.
[gem5.git] / src / systemc / tlm_port_wrapper.hh
1 /*
2 * Copyright 2018 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30 #ifndef __SYSTEMC_TLM_PORT_WRAPPER_HH__
31 #define __SYSTEMC_TLM_PORT_WRAPPER_HH__
32
33 #include "base/logging.hh"
34 #include "sim/port.hh"
35 #include "systemc/ext/tlm_core/2/sockets/sockets.hh"
36
37 namespace sc_gem5
38 {
39
40 template <unsigned int BUSWIDTH=32,
41 typename TYPES=tlm::tlm_base_protocol_types, int N=1,
42 sc_core::sc_port_policy POL=sc_core::SC_ONE_OR_MORE_BOUND>
43 class TlmInitiatorWrapper;
44
45 template <unsigned int BUSWIDTH=32,
46 typename TYPES=tlm::tlm_base_protocol_types, int N=1,
47 sc_core::sc_port_policy POL=sc_core::SC_ONE_OR_MORE_BOUND>
48 class TlmTargetWrapper;
49
50 template <unsigned int BUSWIDTH, typename TYPES, int N,
51 sc_core::sc_port_policy POL>
52 class TlmInitiatorWrapper : public ::Port
53 {
54 public:
55 typedef tlm::tlm_base_initiator_socket<BUSWIDTH,
56 tlm::tlm_fw_transport_if<TYPES>,
57 tlm::tlm_bw_transport_if<TYPES>, N, POL>
58 InitiatorSocket;
59 typedef typename InitiatorSocket::base_target_socket_type TargetSocket;
60 typedef TlmTargetWrapper<BUSWIDTH, TYPES, N, POL> TargetWrapper;
61
62 InitiatorSocket &initiator() { return _initiator; }
63
64 TlmInitiatorWrapper(
65 InitiatorSocket &i, const std::string &_name, PortID _id) :
66 Port(_name, _id), _initiator(i)
67 {}
68
69 void
70 bind(::Port &peer) override
71 {
72 auto *target = dynamic_cast<TargetWrapper *>(&peer);
73 fatal_if(!target, "Attempt to bind TLM initiator socket %s to "
74 "incompatible port %s.", name(), peer.name());
75
76 initiator().bind(target->target());
77 }
78
79 void
80 unbind() override
81 {
82 panic("TLM sockets can't be unbound.");
83 }
84
85 private:
86 InitiatorSocket &_initiator;
87 };
88
89 template <unsigned int BUSWIDTH, typename TYPES, int N,
90 sc_core::sc_port_policy POL>
91 class TlmTargetWrapper : public ::Port
92 {
93 public:
94 typedef tlm::tlm_base_target_socket<BUSWIDTH,
95 tlm::tlm_fw_transport_if<TYPES>,
96 tlm::tlm_bw_transport_if<TYPES>, N, POL>
97 TargetSocket;
98
99 TargetSocket &target() { return _target; }
100
101 TlmTargetWrapper(TargetSocket &t, const std::string &_name, PortID _id) :
102 Port(_name, _id), _target(t)
103 {}
104
105 void
106 bind(::Port &peer) override
107 {
108 // Ignore attempts to bind a target socket. The initiator will
109 // handle it.
110 }
111
112 void
113 unbind() override
114 {
115 panic("TLM sockets can't be unbound.");
116 }
117
118 private:
119 TargetSocket &_target;
120 };
121
122 } // namespace sc_gem5
123
124 #endif //__SYSTEMC_TLM_PORT_WRAPPER_HH__