sc_port.hh (13207:034ca389a810) sc_port.hh (13245:c666c5d4996b)
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 <vector>
34
35#include "../utils/sc_report_handler.hh"
36#include "sc_module.hh" // for sc_gen_unique_name
37#include "sc_object.hh"
38
39namespace sc_gem5
40{
41
42class Port;
43
44};
45
46namespace sc_core
47{
48
49class sc_interface;
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 <vector>
34
35#include "../utils/sc_report_handler.hh"
36#include "sc_module.hh" // for sc_gen_unique_name
37#include "sc_object.hh"
38
39namespace sc_gem5
40{
41
42class Port;
43
44};
45
46namespace sc_core
47{
48
49class sc_interface;
50class sc_trace_file;
50
51
52// Nonstandard
53// Despite having a warning "FOR INTERNAL USE ONLY!" in all caps above this
54// class definition in the Accellera implementation, it appears in their
55// examples and test programs, and so we need to have it here as well.
56struct sc_trace_params
57{
58 sc_trace_file *tf;
59 std::string name;
60
61 sc_trace_params(sc_trace_file *tf, const std::string &name) :
62 tf(tf), name(name)
63 {}
64};
65typedef std::vector<sc_trace_params *> sc_trace_params_vec;
66
51enum sc_port_policy
52{
53 SC_ONE_OR_MORE_BOUND, // Default
54 SC_ZERO_OR_MORE_BOUND,
55 SC_ALL_BOUND
56};
57
58class sc_port_base : public sc_object
59{
60 public:
61 sc_port_base(const char *name, int n, sc_port_policy p);
62 virtual ~sc_port_base();
63
64 void warn_unimpl(const char *func) const;
65
66 int maxSize() const;
67 int size() const;
68
69 const char *kind() const { return "sc_port_base"; }
70
71 protected:
72 // Implementation defined, but depended on by the tests.
73 void bind(sc_interface &);
74 void bind(sc_port_base &);
75
76 friend class ::sc_gem5::Module;
77
78 // Implementation defined, but depended on by the tests.
79 virtual int vbind(sc_interface &) = 0;
80 virtual int vbind(sc_port_base &) = 0;
81
82 virtual void before_end_of_elaboration() = 0;
83 virtual void end_of_elaboration() = 0;
84 virtual void start_of_simulation() = 0;
85 virtual void end_of_simulation() = 0;
86
87 private:
88 friend class ::sc_gem5::Port;
89 friend class ::sc_gem5::Kernel;
90
91 virtual sc_interface *_gem5Interface(int n) const = 0;
92 virtual void _gem5AddInterface(sc_interface *i) = 0;
93
94 ::sc_gem5::Port *_gem5Port;
95};
96
97template <class IF>
98class sc_port_b : public sc_port_base
99{
100 public:
101 void operator () (IF &i) { bind(i); }
102 void operator () (sc_port_b<IF> &p) { bind(p); }
103
104 virtual void bind(IF &i) { sc_port_base::bind(i); }
105 virtual void bind(sc_port_b<IF> &p) { sc_port_base::bind(p); }
106
107 IF *
108 operator -> ()
109 {
110 sc_assert(!_interfaces.empty());
111 return _interfaces[0];
112 }
113 const IF *
114 operator -> () const
115 {
116 sc_assert(!_interfaces.empty());
117 return _interfaces[0];
118 }
119
120 IF *
121 operator [] (int n)
122 {
123 sc_assert(_interfaces.size() > n);
124 return _interfaces[n];
125 }
126 const IF *
127 operator [] (int n) const
128 {
129 sc_assert(_interfaces.size() > n);
130 return _interfaces[n];
131 }
132
133 sc_interface *
134 get_interface()
135 {
136 sc_assert(!_interfaces.empty());
137 return _interfaces[0];
138 }
139 const sc_interface *
140 get_interface() const
141 {
142 sc_assert(!_interfaces.empty());
143 return _interfaces[0];
144 }
145
146 protected:
147 void before_end_of_elaboration() {}
148 void end_of_elaboration() {}
149 void start_of_simulation() {}
150 void end_of_simulation() {}
151
152 explicit sc_port_b(int n, sc_port_policy p) :
153 sc_port_base(sc_gen_unique_name("port"), n, p)
154 {}
155 sc_port_b(const char *name, int n, sc_port_policy p) :
156 sc_port_base(name, n, p)
157 {}
158 virtual ~sc_port_b() {}
159
160 // Implementation defined, but depended on by the tests.
161 int
162 vbind(sc_interface &i)
163 {
164 IF *interface = dynamic_cast<IF *>(&i);
165 if (!interface)
166 return 2;
167 sc_port_base::bind(*interface);
168 return 0;
169 }
170 int
171 vbind(sc_port_base &pb)
172 {
173 sc_port_b<IF> *p = dynamic_cast<sc_port_b<IF> *>(&pb);
174 if (!p)
175 return 2;
176 sc_port_base::bind(*p);
177 return 0;
178 }
179
180 private:
181 std::vector<IF *> _interfaces;
182
183 sc_interface *
184 _gem5Interface(int n) const
185 {
186 sc_assert(_interfaces.size() > n);
187 return _interfaces[n];
188 }
189 void
190 _gem5AddInterface(sc_interface *i)
191 {
192 IF *interface = dynamic_cast<IF *>(i);
193 sc_assert(interface);
194 _interfaces.push_back(interface);
195 }
196
197 // Disabled
198 sc_port_b() {}
199 sc_port_b(const sc_port_b<IF> &) {}
200 sc_port_b<IF> &operator = (const sc_port_b<IF> &) { return *this; }
201};
202
203template <class IF, int N=1, sc_port_policy P=SC_ONE_OR_MORE_BOUND>
204class sc_port : public sc_port_b<IF>
205{
206 public:
207 sc_port() : sc_port_b<IF>(N, P) {}
208 explicit sc_port(const char *name) : sc_port_b<IF>(name, N, P) {}
209 virtual ~sc_port() {}
210
211 // Deprecated binding constructors.
212 explicit sc_port(const IF &interface) : sc_port_b<IF>(N, P)
213 {
214 this->warn_unimpl(__PRETTY_FUNCTION__);
215 // Should warn that these are deprecated. See Accellera sc_port.h.
216 sc_port_b<IF>::bind(const_cast<IF &>(interface));
217 }
218 sc_port(const char *name, const IF &interface) : sc_port_b<IF>(name, N, P)
219 {
220 this->warn_unimpl(__PRETTY_FUNCTION__);
221 // Should warn that these are deprecated. See Accellera sc_port.h.
222 sc_port_b<IF>::bind(const_cast<IF &>(interface));
223 }
224 explicit sc_port(sc_port_b<IF> &parent) : sc_port_b<IF>(N, P)
225 {
226 this->warn_unimpl(__PRETTY_FUNCTION__);
227 // Should warn that these are deprecated. See Accellera sc_port.h.
228 sc_port_b<IF>::bind(parent);
229 }
230 sc_port(const char *name, sc_port_b<IF> &parent) :
231 sc_port_b<IF>(name, N, P)
232 {
233 this->warn_unimpl(__PRETTY_FUNCTION__);
234 // Should warn that these are deprecated. See Accellera sc_port.h.
235 sc_port_b<IF>::bind(parent);
236 }
237 explicit sc_port(sc_port<IF, N, P> &parent) : sc_port_b<IF>(N, P)
238 {
239 this->warn_unimpl(__PRETTY_FUNCTION__);
240 // Should warn that these are deprecated. See Accellera sc_port.h.
241 sc_port_b<IF>::bind(parent);
242 }
243 sc_port(const char *name, sc_port<IF, N, P> &parent) :
244 sc_port_b<IF>(name, N, P)
245 {
246 this->warn_unimpl(__PRETTY_FUNCTION__);
247 // Should warn that these are deprecated. See Accellera sc_port.h.
248 sc_port_b<IF>::bind(parent);
249 }
250
251 virtual const char *kind() const { return "sc_port"; }
252
253 private:
254 // Disabled
255 sc_port(const sc_port<IF, N, P> &) {}
256 sc_port<IF, N, P> &operator = (const sc_port<IF, N, P> &) { return *this; }
257};
258
259} // namespace sc_core
260
261#endif //__SYSTEMC_EXT_CORE_SC_PORT_HH__
67enum sc_port_policy
68{
69 SC_ONE_OR_MORE_BOUND, // Default
70 SC_ZERO_OR_MORE_BOUND,
71 SC_ALL_BOUND
72};
73
74class sc_port_base : public sc_object
75{
76 public:
77 sc_port_base(const char *name, int n, sc_port_policy p);
78 virtual ~sc_port_base();
79
80 void warn_unimpl(const char *func) const;
81
82 int maxSize() const;
83 int size() const;
84
85 const char *kind() const { return "sc_port_base"; }
86
87 protected:
88 // Implementation defined, but depended on by the tests.
89 void bind(sc_interface &);
90 void bind(sc_port_base &);
91
92 friend class ::sc_gem5::Module;
93
94 // Implementation defined, but depended on by the tests.
95 virtual int vbind(sc_interface &) = 0;
96 virtual int vbind(sc_port_base &) = 0;
97
98 virtual void before_end_of_elaboration() = 0;
99 virtual void end_of_elaboration() = 0;
100 virtual void start_of_simulation() = 0;
101 virtual void end_of_simulation() = 0;
102
103 private:
104 friend class ::sc_gem5::Port;
105 friend class ::sc_gem5::Kernel;
106
107 virtual sc_interface *_gem5Interface(int n) const = 0;
108 virtual void _gem5AddInterface(sc_interface *i) = 0;
109
110 ::sc_gem5::Port *_gem5Port;
111};
112
113template <class IF>
114class sc_port_b : public sc_port_base
115{
116 public:
117 void operator () (IF &i) { bind(i); }
118 void operator () (sc_port_b<IF> &p) { bind(p); }
119
120 virtual void bind(IF &i) { sc_port_base::bind(i); }
121 virtual void bind(sc_port_b<IF> &p) { sc_port_base::bind(p); }
122
123 IF *
124 operator -> ()
125 {
126 sc_assert(!_interfaces.empty());
127 return _interfaces[0];
128 }
129 const IF *
130 operator -> () const
131 {
132 sc_assert(!_interfaces.empty());
133 return _interfaces[0];
134 }
135
136 IF *
137 operator [] (int n)
138 {
139 sc_assert(_interfaces.size() > n);
140 return _interfaces[n];
141 }
142 const IF *
143 operator [] (int n) const
144 {
145 sc_assert(_interfaces.size() > n);
146 return _interfaces[n];
147 }
148
149 sc_interface *
150 get_interface()
151 {
152 sc_assert(!_interfaces.empty());
153 return _interfaces[0];
154 }
155 const sc_interface *
156 get_interface() const
157 {
158 sc_assert(!_interfaces.empty());
159 return _interfaces[0];
160 }
161
162 protected:
163 void before_end_of_elaboration() {}
164 void end_of_elaboration() {}
165 void start_of_simulation() {}
166 void end_of_simulation() {}
167
168 explicit sc_port_b(int n, sc_port_policy p) :
169 sc_port_base(sc_gen_unique_name("port"), n, p)
170 {}
171 sc_port_b(const char *name, int n, sc_port_policy p) :
172 sc_port_base(name, n, p)
173 {}
174 virtual ~sc_port_b() {}
175
176 // Implementation defined, but depended on by the tests.
177 int
178 vbind(sc_interface &i)
179 {
180 IF *interface = dynamic_cast<IF *>(&i);
181 if (!interface)
182 return 2;
183 sc_port_base::bind(*interface);
184 return 0;
185 }
186 int
187 vbind(sc_port_base &pb)
188 {
189 sc_port_b<IF> *p = dynamic_cast<sc_port_b<IF> *>(&pb);
190 if (!p)
191 return 2;
192 sc_port_base::bind(*p);
193 return 0;
194 }
195
196 private:
197 std::vector<IF *> _interfaces;
198
199 sc_interface *
200 _gem5Interface(int n) const
201 {
202 sc_assert(_interfaces.size() > n);
203 return _interfaces[n];
204 }
205 void
206 _gem5AddInterface(sc_interface *i)
207 {
208 IF *interface = dynamic_cast<IF *>(i);
209 sc_assert(interface);
210 _interfaces.push_back(interface);
211 }
212
213 // Disabled
214 sc_port_b() {}
215 sc_port_b(const sc_port_b<IF> &) {}
216 sc_port_b<IF> &operator = (const sc_port_b<IF> &) { return *this; }
217};
218
219template <class IF, int N=1, sc_port_policy P=SC_ONE_OR_MORE_BOUND>
220class sc_port : public sc_port_b<IF>
221{
222 public:
223 sc_port() : sc_port_b<IF>(N, P) {}
224 explicit sc_port(const char *name) : sc_port_b<IF>(name, N, P) {}
225 virtual ~sc_port() {}
226
227 // Deprecated binding constructors.
228 explicit sc_port(const IF &interface) : sc_port_b<IF>(N, P)
229 {
230 this->warn_unimpl(__PRETTY_FUNCTION__);
231 // Should warn that these are deprecated. See Accellera sc_port.h.
232 sc_port_b<IF>::bind(const_cast<IF &>(interface));
233 }
234 sc_port(const char *name, const IF &interface) : sc_port_b<IF>(name, N, P)
235 {
236 this->warn_unimpl(__PRETTY_FUNCTION__);
237 // Should warn that these are deprecated. See Accellera sc_port.h.
238 sc_port_b<IF>::bind(const_cast<IF &>(interface));
239 }
240 explicit sc_port(sc_port_b<IF> &parent) : sc_port_b<IF>(N, P)
241 {
242 this->warn_unimpl(__PRETTY_FUNCTION__);
243 // Should warn that these are deprecated. See Accellera sc_port.h.
244 sc_port_b<IF>::bind(parent);
245 }
246 sc_port(const char *name, sc_port_b<IF> &parent) :
247 sc_port_b<IF>(name, N, P)
248 {
249 this->warn_unimpl(__PRETTY_FUNCTION__);
250 // Should warn that these are deprecated. See Accellera sc_port.h.
251 sc_port_b<IF>::bind(parent);
252 }
253 explicit sc_port(sc_port<IF, N, P> &parent) : sc_port_b<IF>(N, P)
254 {
255 this->warn_unimpl(__PRETTY_FUNCTION__);
256 // Should warn that these are deprecated. See Accellera sc_port.h.
257 sc_port_b<IF>::bind(parent);
258 }
259 sc_port(const char *name, sc_port<IF, N, P> &parent) :
260 sc_port_b<IF>(name, N, P)
261 {
262 this->warn_unimpl(__PRETTY_FUNCTION__);
263 // Should warn that these are deprecated. See Accellera sc_port.h.
264 sc_port_b<IF>::bind(parent);
265 }
266
267 virtual const char *kind() const { return "sc_port"; }
268
269 private:
270 // Disabled
271 sc_port(const sc_port<IF, N, P> &) {}
272 sc_port<IF, N, P> &operator = (const sc_port<IF, N, P> &) { return *this; }
273};
274
275} // namespace sc_core
276
277#endif //__SYSTEMC_EXT_CORE_SC_PORT_HH__