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