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