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