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