gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sc_port_wrapper.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Google LLC.
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: Chun-Chen TK Hsu
28  */
29 
30 #ifndef __SYSTEMC_SC_PORT_WRAPPER_HH__
31 #define __SYSTEMC_SC_PORT_WRAPPER_HH__
32 
33 #include <string>
34 #include <type_traits>
35 
36 #include "base/logging.hh"
37 #include "sim/port.hh"
41 
42 namespace sc_gem5
43 {
44 
45 // Forward declaration
46 template <typename IF>
48 template <typename IF>
50 template <typename IF>
52 
53 template <typename IF>
54 class ScPortWrapper : public ::Port
55 {
56  public:
58 
59  ScPortWrapper(ScPort& p, const std::string& name, PortID id)
60  : Port(name, id), port_(p)
61  {}
62 
63  ScPort&
64  port()
65  {
66  return port_;
67  }
68 
69  void
70  unbind() override
71  {
72  panic("sc_port can't be unbound.");
73  }
74 
75  void
76  bind(::Port& peer) override
77  {
78  // Try ScPortWrapper or ScInterfaceWrapper
79  if (auto* beer = dynamic_cast<ScPortWrapper<IF>*>(&peer)) {
80  port_.bind(beer->port());
81  } else if (auto* iface =
82  dynamic_cast<ScInterfaceWrapper<IF>*>(&peer)) {
83  port_.bind(iface->interface());
84  } else {
85  fatal("Attempt to bind sc_port %s to incompatible port %s.",
86  name(), peer.name());
87  }
88  Port::bind(peer);
89  }
90 
91  private:
93 };
94 
95 template <typename IF>
96 class ScInterfaceWrapper : public ::Port
97 {
98  public:
99  ScInterfaceWrapper(IF& i, const std::string name, PortID id)
100  : Port(name, id), iface_(i)
101  {}
102 
103  IF&
105  {
106  return iface_;
107  }
108 
109  void
110  unbind() override
111  {
112  panic("sc_interface can't be unbound.");
113  }
114 
115  void
116  bind(::Port& peer) override
117  {
118  // fatal error if peer is neither ScPortWrapper nor ScExportWrapper
119  fatal_if(!dynamic_cast<ScPortWrapper<IF>*>(&peer) &&
120  !dynamic_cast<ScExportWrapper<IF>*>(&peer),
121  "Attempt to bind sc_interface %s to incompatible port %s.",
122  name(), peer.name());
123 
124  // Don't bind to peer otherwise we may have error messages saying that
125  // this interface has already be bound since the peer may already did
126  // that. Just let sc_port or sc_export do the binding
127  Port::bind(peer);
128  }
129 
130  private:
131  IF& iface_;
132 };
133 
134 template <typename IF>
135 class ScExportWrapper : public ::Port
136 {
137  public:
139 
140  ScExportWrapper(ScExport& p, const std::string& name, PortID id)
141  : Port(name, id), port_(p)
142  {}
143 
144  ScExport&
146  {
147  return port_;
148  }
149 
150  void
151  unbind() override
152  {
153  panic("sc_export cannot be unbound.");
154  }
155 
156  void
157  bind(::Port& peer) override
158  {
159  auto* iface = dynamic_cast<ScInterfaceWrapper<IF>*>(&peer);
160  fatal_if(!iface,
161  "Attempt to bind sc_export %s to incompatible port %s.",
162  name(), peer.name());
163 
164  port_.bind(iface->interface());
165  Port::bind(peer);
166  }
167 
168  private:
170 };
171 
172 } // namespace sc_gem5
173 
174 #endif // __SYSTEMC_SC_PORT_WRAPPER_HH__
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:167
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:175
Bitfield< 7 > i
void unbind() override
Dettach from a peer port.
void bind(::Port &peer) override
virtual void bind(IF &i)
Definition: sc_port.hh:126
void unbind() override
Dettach from a peer port.
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:203
void bind(::Port &peer) override
void unbind() override
Dettach from a peer port.
Port Object Declaration.
ScPortWrapper(ScPort &p, const std::string &name, PortID id)
ScExportWrapper(ScExport &p, const std::string &name, PortID id)
const std::string name() const
Return port name (for DPRINTF).
Definition: port.hh:106
void bind(::sc_core::sc_interface *interface)
Definition: port.hh:140
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
Definition: types.hh:237
Bitfield< 0 > p
void bind(::Port &peer) override
ScInterfaceWrapper(IF &i, const std::string name, PortID id)

Generated on Fri Feb 28 2020 16:27:04 for gem5 by doxygen 1.8.13