sc_port.hh revision 13059:4be5f408e128
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 *operator -> () { return _interfaces.at(0); }
109    const IF *operator -> () const { return _interfaces.at(0); }
110
111    IF *operator [] (int n) { return _interfaces.at(n); }
112    const IF *operator [] (int n) const { return _interfaces.at(n); }
113
114    sc_interface *get_interface() { return _interfaces.at(0); }
115    const sc_interface *get_interface() const { return _interfaces.at(0); }
116
117  protected:
118    void before_end_of_elaboration() {}
119    void end_of_elaboration() {}
120    void start_of_simulation() {}
121    void end_of_simulation() {}
122
123    explicit sc_port_b(int n, sc_port_policy p) :
124            sc_port_base(sc_gen_unique_name("port"), n, p)
125    {}
126    sc_port_b(const char *name, int n, sc_port_policy p) :
127            sc_port_base(name, n, p)
128    {}
129    virtual ~sc_port_b() {}
130
131    // Implementation defined, but depended on by the tests.
132    int
133    vbind(sc_interface &i)
134    {
135        IF *interface = dynamic_cast<IF *>(&i);
136        if (!interface)
137            return 2;
138        sc_port_base::bind(*interface);
139        return 0;
140    }
141    int
142    vbind(sc_port_base &pb)
143    {
144        sc_port_b<IF> *p = dynamic_cast<sc_port_b<IF> *>(&pb);
145        if (!p)
146            return 2;
147        sc_port_base::bind(*p);
148        return 0;
149    }
150
151  private:
152    std::vector<IF *> _interfaces;
153
154    sc_interface *_gem5Interface(int n) const { return _interfaces.at(n); }
155    void
156    _gem5AddInterface(sc_interface *i)
157    {
158        IF *interface = dynamic_cast<IF *>(i);
159        sc_assert(interface);
160        _interfaces.push_back(interface);
161    }
162
163    // Disabled
164    sc_port_b() {}
165    sc_port_b(const sc_port_b<IF> &) {}
166    sc_port_b<IF> &operator = (const sc_port_b<IF> &) { return *this; }
167};
168
169template <class IF, int N=1, sc_port_policy P=SC_ONE_OR_MORE_BOUND>
170class sc_port : public sc_port_b<IF>
171{
172  public:
173    sc_port() : sc_port_b<IF>(N, P) {}
174    explicit sc_port(const char *name) : sc_port_b<IF>(name, N, P) {}
175    virtual ~sc_port() {}
176
177    // Deprecated binding constructors.
178    explicit sc_port(const IF &interface) : sc_port_b<IF>(N, P)
179    {
180        this->warn_unimpl(__PRETTY_FUNCTION__);
181        // Should warn that these are deprecated. See Accellera sc_port.h.
182        sc_port_b<IF>::bind(const_cast<IF &>(interface));
183    }
184    sc_port(const char *name, const IF &interface) : sc_port_b<IF>(name, N, P)
185    {
186        this->warn_unimpl(__PRETTY_FUNCTION__);
187        // Should warn that these are deprecated. See Accellera sc_port.h.
188        sc_port_b<IF>::bind(const_cast<IF &>(interface));
189    }
190    explicit sc_port(sc_port_b<IF> &parent) : sc_port_b<IF>(N, P)
191    {
192        this->warn_unimpl(__PRETTY_FUNCTION__);
193        // Should warn that these are deprecated. See Accellera sc_port.h.
194        sc_port_b<IF>::bind(parent);
195    }
196    sc_port(const char *name, sc_port_b<IF> &parent) :
197        sc_port_b<IF>(name, N, P)
198    {
199        this->warn_unimpl(__PRETTY_FUNCTION__);
200        // Should warn that these are deprecated. See Accellera sc_port.h.
201        sc_port_b<IF>::bind(parent);
202    }
203    explicit sc_port(sc_port<IF, N, P> &parent) : sc_port_b<IF>(N, P)
204    {
205        this->warn_unimpl(__PRETTY_FUNCTION__);
206        // Should warn that these are deprecated. See Accellera sc_port.h.
207        sc_port_b<IF>::bind(parent);
208    }
209    sc_port(const char *name, sc_port<IF, N, P> &parent) :
210        sc_port_b<IF>(name, N, P)
211    {
212        this->warn_unimpl(__PRETTY_FUNCTION__);
213        // Should warn that these are deprecated. See Accellera sc_port.h.
214        sc_port_b<IF>::bind(parent);
215    }
216
217    virtual const char *kind() const { return "sc_port"; }
218
219  private:
220    // Disabled
221    sc_port(const sc_port<IF, N, P> &) {}
222    sc_port<IF, N, P> &operator = (const sc_port<IF, N, P> &) { return *this; }
223};
224
225} // namespace sc_core
226
227#endif  //__SYSTEMC_EXT_CORE_SC_PORT_HH__
228