gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sc_port.hh
Go to the documentation of this file.
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_EXT_CORE_SC_PORT_HH__
31 #define __SYSTEMC_EXT_CORE_SC_PORT_HH__
32 
33 #include <typeinfo>
34 #include <vector>
35 
36 #include "../channel/messages.hh"
37 #include "../utils/sc_report_handler.hh"
38 #include "sc_module.hh" // for sc_gen_unique_name
39 #include "sc_object.hh"
40 
41 namespace sc_gem5
42 {
43 
44 class Port;
45 
46 };
47 
48 namespace sc_core
49 {
50 
51 class sc_interface;
52 class sc_trace_file;
53 
54 // Nonstandard
55 // Despite having a warning "FOR INTERNAL USE ONLY!" in all caps above this
56 // class definition in the Accellera implementation, it appears in their
57 // examples and test programs, and so we need to have it here as well.
59 {
61  std::string name;
62 
63  sc_trace_params(sc_trace_file *tf, const std::string &name) :
64  tf(tf), name(name)
65  {}
66 };
68 
70 {
74 };
75 
76 class sc_port_base : public sc_object
77 {
78  public:
79  sc_port_base(const char *name, int n, sc_port_policy p);
80  virtual ~sc_port_base();
81 
82  void warn_port_constructor() const;
83 
84  int maxSize() const;
85  int size() const;
86 
87  const char *kind() const { return "sc_port_base"; }
88 
89  protected:
90  // Implementation defined, but depended on by the tests.
91  void bind(sc_interface &);
92  void bind(sc_port_base &);
93 
94  friend class ::sc_gem5::Module;
95 
96  // Implementation defined, but depended on by the tests.
97  virtual int vbind(sc_interface &) = 0;
98  virtual int vbind(sc_port_base &) = 0;
99 
100  virtual void before_end_of_elaboration() = 0;
101  virtual void end_of_elaboration() = 0;
102  virtual void start_of_simulation() = 0;
103  virtual void end_of_simulation() = 0;
104 
105  void report_error(const char *id, const char *add_msg) const;
106 
107  private:
108  friend class ::sc_gem5::Port;
109  friend class ::sc_gem5::Kernel;
110 
111  virtual sc_interface *_gem5Interface(int n) const = 0;
112  virtual void _gem5AddInterface(sc_interface *i) = 0;
113 
115  virtual const char *_ifTypeName() const = 0;
116  virtual sc_port_policy _portPolicy() const = 0;
117 };
118 
119 template <class IF>
120 class sc_port_b : public sc_port_base
121 {
122  public:
123  void operator () (IF &i) { bind(i); }
124  void operator () (sc_port_b<IF> &p) { bind(p); }
125 
126  virtual void bind(IF &i) { sc_port_base::bind(i); }
127  virtual void bind(sc_port_b<IF> &p) { sc_port_base::bind(p); }
128 
129  IF *
130  operator -> ()
131  {
132  if (_interfaces.empty()) {
133  report_error(SC_ID_GET_IF_, "port is not bound");
134  sc_abort();
135  }
136  return _interfaces[0];
137  }
138  const IF *
139  operator -> () const
140  {
141  if (_interfaces.empty()) {
142  report_error(SC_ID_GET_IF_, "port is not bound");
143  sc_abort();
144  }
145  return _interfaces[0];
146  }
147 
148  IF *
149  operator [] (int n)
150  {
151  if (n < 0 || n >= size()) {
152  report_error(SC_ID_GET_IF_, "index out of range");
153  return NULL;
154  }
155  return _interfaces[n];
156  }
157  const IF *
158  operator [] (int n) const
159  {
160  if (n < 0 || n >= size()) {
161  report_error(SC_ID_GET_IF_, "index out of range");
162  return NULL;
163  }
164  return _interfaces[n];
165  }
166 
167  sc_interface *
169  {
170  if (_interfaces.empty())
171  return NULL;
172  return _interfaces[0];
173  }
174  const sc_interface *
176  {
177  if (_interfaces.empty())
178  return NULL;
179  return _interfaces[0];
180  }
181 
182  protected:
183  void before_end_of_elaboration() override {}
184  void end_of_elaboration() override {}
185  void start_of_simulation() override {}
186  void end_of_simulation() override {}
187 
188  explicit sc_port_b(int n, sc_port_policy p) :
189  sc_port_base(sc_gen_unique_name("port"), n, p)
190  {}
191  sc_port_b(const char *name, int n, sc_port_policy p) :
192  sc_port_base(name, n, p)
193  {}
194  virtual ~sc_port_b() {}
195 
196  // Implementation defined, but depended on by the tests.
197  int
198  vbind(sc_interface &i) override
199  {
200  IF *interface = dynamic_cast<IF *>(&i);
201  if (!interface)
202  return 2;
203  sc_port_base::bind(*interface);
204  return 0;
205  }
206  int
207  vbind(sc_port_base &pb) override
208  {
209  sc_port_b<IF> *p = dynamic_cast<sc_port_b<IF> *>(&pb);
210  if (!p)
211  return 2;
212  sc_port_base::bind(*p);
213  return 0;
214  }
215 
216  private:
218 
219  sc_interface *
220  _gem5Interface(int n) const override
221  {
222  if (n < 0 || n >= size()) {
223  report_error(SC_ID_GET_IF_, "index out of range");
224  return NULL;
225  }
226  return _interfaces[n];
227  }
228  void
230  {
231  IF *interface = dynamic_cast<IF *>(iface);
232  sc_assert(interface);
233  for (unsigned i = 0; i < _interfaces.size(); i++) {
234  if (interface == _interfaces[i]) {
235  report_error(SC_ID_BIND_IF_TO_PORT_,
236  "interface already bound to port");
237  }
238  }
239  _interfaces.push_back(interface);
240  }
241 
242  const char *_ifTypeName() const override { return typeid(IF).name(); }
243 
244  // Disabled
247  sc_port_b<IF> &operator = (const sc_port_b<IF> &) { return *this; }
248 };
249 
250 template <class IF, int N=1, sc_port_policy P=SC_ONE_OR_MORE_BOUND>
251 class sc_port : public sc_port_b<IF>
252 {
253  public:
254  sc_port() : sc_port_b<IF>(N, P) {}
255  explicit sc_port(const char *name) : sc_port_b<IF>(name, N, P) {}
256  virtual ~sc_port() {}
257 
258  // Deprecated binding constructors.
259  explicit sc_port(const IF &interface) : sc_port_b<IF>(N, P)
260  {
261  this->warn_port_constructor();
262  sc_port_b<IF>::bind(const_cast<IF &>(interface));
263  }
264  sc_port(const char *name, const IF &interface) : sc_port_b<IF>(name, N, P)
265  {
266  this->warn_port_constructor();
267  sc_port_b<IF>::bind(const_cast<IF &>(interface));
268  }
269  explicit sc_port(sc_port_b<IF> &parent) : sc_port_b<IF>(N, P)
270  {
271  this->warn_port_constructor();
272  sc_port_b<IF>::bind(parent);
273  }
274  sc_port(const char *name, sc_port_b<IF> &parent) :
275  sc_port_b<IF>(name, N, P)
276  {
277  this->warn_port_constructor();
278  sc_port_b<IF>::bind(parent);
279  }
280  explicit sc_port(sc_port<IF, N, P> &parent) : sc_port_b<IF>(N, P)
281  {
282  this->warn_port_constructor();
283  sc_port_b<IF>::bind(parent);
284  }
285  sc_port(const char *name, sc_port<IF, N, P> &parent) :
286  sc_port_b<IF>(name, N, P)
287  {
288  this->warn_port_constructor();
289  sc_port_b<IF>::bind(parent);
290  }
291 
292  virtual const char *kind() const override { return "sc_port"; }
293 
294  private:
295  // Disabled
297  sc_port<IF, N, P> &operator = (const sc_port<IF, N, P> &) { return *this; }
298 
299  virtual sc_port_policy _portPolicy() const override { return P; }
300 };
301 
302 } // namespace sc_core
303 
304 #endif //__SYSTEMC_EXT_CORE_SC_PORT_HH__
sc_port(const char *name, const IF &interface)
Definition: sc_port.hh:264
Ports are used to interface objects to each other.
Definition: port.hh:60
void before_end_of_elaboration() override
Definition: sc_port.hh:183
const char * _ifTypeName() const override
Definition: sc_port.hh:242
const std::string & name()
Definition: trace.cc:54
Bitfield< 7 > i
sc_trace_params(sc_trace_file *tf, const std::string &name)
Definition: sc_port.hh:63
sc_interface * get_interface()
Definition: sc_port.hh:168
void start_of_simulation() override
Definition: sc_port.hh:185
sc_port(sc_port< IF, N, P > &parent)
Definition: sc_port.hh:280
virtual ~sc_port_b()
Definition: sc_port.hh:194
void end_of_elaboration() override
Definition: sc_port.hh:184
const char * kind() const
Definition: sc_port.hh:87
sc_trace_file * tf
Definition: sc_port.hh:60
virtual const char * kind() const override
Definition: sc_port.hh:292
const char * sc_gen_unique_name(const char *seed)
Definition: sc_module.cc:822
int vbind(sc_interface &i) override
Definition: sc_port.hh:198
const char SC_ID_GET_IF_[]
Definition: messages.cc:51
Bitfield< 31 > n
sc_port(const IF &interface)
Definition: sc_port.hh:259
sc_port_b(const char *name, int n, sc_port_policy p)
Definition: sc_port.hh:191
sc_interface * _gem5Interface(int n) const override
Definition: sc_port.hh:220
virtual sc_port_policy _portPolicy() const override
Definition: sc_port.hh:299
virtual void bind(IF &i)
Definition: sc_port.hh:126
sc_port_b(int n, sc_port_policy p)
Definition: sc_port.hh:188
virtual void bind(sc_port_b< IF > &p)
Definition: sc_port.hh:127
void end_of_simulation() override
Definition: sc_port.hh:186
sc_port(const char *name, sc_port< IF, N, P > &parent)
Definition: sc_port.hh:285
const char SC_ID_BIND_IF_TO_PORT_[]
Definition: messages.cc:46
#define sc_assert(expr)
void _gem5AddInterface(sc_interface *iface) override
Definition: sc_port.hh:229
sc_port_policy
Definition: sc_port.hh:69
sc_port_b(const sc_port_b< IF > &)
Definition: sc_port.hh:246
sc_port(const sc_port< IF, N, P > &)
Definition: sc_port.hh:296
sc_port(const char *name)
Definition: sc_port.hh:255
std::vector< IF * > _interfaces
Definition: sc_port.hh:217
virtual ~sc_port()
Definition: sc_port.hh:256
sc_port(sc_port_b< IF > &parent)
Definition: sc_port.hh:269
std::vector< sc_trace_params * > sc_trace_params_vec
Definition: sc_port.hh:67
const sc_interface * get_interface() const
Definition: sc_port.hh:175
int vbind(sc_port_base &pb) override
Definition: sc_port.hh:207
void sc_abort()
Definition: sc_report.cc:180
::sc_gem5::Port * _gem5Port
Definition: sc_port.hh:114
Bitfield< 0 > p
sc_port(const char *name, sc_port_b< IF > &parent)
Definition: sc_port.hh:274

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