sc_port.hh revision 13290
17732SAli.Saidi@ARM.com/*
27732SAli.Saidi@ARM.com * Copyright 2018 Google, Inc.
37732SAli.Saidi@ARM.com *
47732SAli.Saidi@ARM.com * Redistribution and use in source and binary forms, with or without
57732SAli.Saidi@ARM.com * modification, are permitted provided that the following conditions are
67732SAli.Saidi@ARM.com * met: redistributions of source code must retain the above copyright
77732SAli.Saidi@ARM.com * notice, this list of conditions and the following disclaimer;
87732SAli.Saidi@ARM.com * redistributions in binary form must reproduce the above copyright
97732SAli.Saidi@ARM.com * notice, this list of conditions and the following disclaimer in the
107732SAli.Saidi@ARM.com * documentation and/or other materials provided with the distribution;
117732SAli.Saidi@ARM.com * neither the name of the copyright holders nor the names of its
127732SAli.Saidi@ARM.com * contributors may be used to endorse or promote products derived from
137732SAli.Saidi@ARM.com * this software without specific prior written permission.
147732SAli.Saidi@ARM.com *
157732SAli.Saidi@ARM.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
167732SAli.Saidi@ARM.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
177732SAli.Saidi@ARM.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
187732SAli.Saidi@ARM.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
197732SAli.Saidi@ARM.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
207732SAli.Saidi@ARM.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
217732SAli.Saidi@ARM.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
227732SAli.Saidi@ARM.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
237732SAli.Saidi@ARM.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
247732SAli.Saidi@ARM.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
257732SAli.Saidi@ARM.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
267732SAli.Saidi@ARM.com *
277732SAli.Saidi@ARM.com * Authors: Gabe Black
287732SAli.Saidi@ARM.com */
297732SAli.Saidi@ARM.com
307732SAli.Saidi@ARM.com#ifndef __SYSTEMC_EXT_CORE_SC_PORT_HH__
317732SAli.Saidi@ARM.com#define __SYSTEMC_EXT_CORE_SC_PORT_HH__
327732SAli.Saidi@ARM.com
337732SAli.Saidi@ARM.com#include <typeinfo>
347732SAli.Saidi@ARM.com#include <vector>
357732SAli.Saidi@ARM.com
367732SAli.Saidi@ARM.com#include "../utils/sc_report_handler.hh"
377732SAli.Saidi@ARM.com#include "sc_module.hh" // for sc_gen_unique_name
387732SAli.Saidi@ARM.com#include "sc_object.hh"
397732SAli.Saidi@ARM.com
407732SAli.Saidi@ARM.comnamespace sc_gem5
417732SAli.Saidi@ARM.com{
427732SAli.Saidi@ARM.com
439660SAndreas.Sandberg@ARM.comclass Port;
447732SAli.Saidi@ARM.com
458987SAli.Saidi@ARM.com};
468987SAli.Saidi@ARM.com
478987SAli.Saidi@ARM.comnamespace sc_core
488987SAli.Saidi@ARM.com{
497732SAli.Saidi@ARM.com
507732SAli.Saidi@ARM.comclass sc_interface;
517732SAli.Saidi@ARM.comclass sc_trace_file;
527732SAli.Saidi@ARM.com
537732SAli.Saidi@ARM.com// Nonstandard
549660SAndreas.Sandberg@ARM.com// Despite having a warning "FOR INTERNAL USE ONLY!" in all caps above this
559660SAndreas.Sandberg@ARM.com// class definition in the Accellera implementation, it appears in their
569660SAndreas.Sandberg@ARM.com// examples and test programs, and so we need to have it here as well.
579660SAndreas.Sandberg@ARM.comstruct sc_trace_params
589660SAndreas.Sandberg@ARM.com{
599660SAndreas.Sandberg@ARM.com    sc_trace_file *tf;
609660SAndreas.Sandberg@ARM.com    std::string name;
619660SAndreas.Sandberg@ARM.com
629660SAndreas.Sandberg@ARM.com    sc_trace_params(sc_trace_file *tf, const std::string &name) :
639660SAndreas.Sandberg@ARM.com        tf(tf), name(name)
649660SAndreas.Sandberg@ARM.com    {}
659660SAndreas.Sandberg@ARM.com};
667732SAli.Saidi@ARM.comtypedef std::vector<sc_trace_params *> sc_trace_params_vec;
679660SAndreas.Sandberg@ARM.com
689660SAndreas.Sandberg@ARM.comenum sc_port_policy
699660SAndreas.Sandberg@ARM.com{
709660SAndreas.Sandberg@ARM.com    SC_ONE_OR_MORE_BOUND, // Default
719660SAndreas.Sandberg@ARM.com    SC_ZERO_OR_MORE_BOUND,
729660SAndreas.Sandberg@ARM.com    SC_ALL_BOUND
739660SAndreas.Sandberg@ARM.com};
749660SAndreas.Sandberg@ARM.com
759660SAndreas.Sandberg@ARM.comclass sc_port_base : public sc_object
769660SAndreas.Sandberg@ARM.com{
779660SAndreas.Sandberg@ARM.com  public:
787732SAli.Saidi@ARM.com    sc_port_base(const char *name, int n, sc_port_policy p);
799660SAndreas.Sandberg@ARM.com    virtual ~sc_port_base();
809660SAndreas.Sandberg@ARM.com
819660SAndreas.Sandberg@ARM.com    void warn_unimpl(const char *func) const;
829660SAndreas.Sandberg@ARM.com
839660SAndreas.Sandberg@ARM.com    int maxSize() const;
849660SAndreas.Sandberg@ARM.com    int size() const;
859660SAndreas.Sandberg@ARM.com
869660SAndreas.Sandberg@ARM.com    const char *kind() const { return "sc_port_base"; }
879660SAndreas.Sandberg@ARM.com
889660SAndreas.Sandberg@ARM.com  protected:
899660SAndreas.Sandberg@ARM.com    // Implementation defined, but depended on by the tests.
909660SAndreas.Sandberg@ARM.com    void bind(sc_interface &);
919660SAndreas.Sandberg@ARM.com    void bind(sc_port_base &);
929660SAndreas.Sandberg@ARM.com
939660SAndreas.Sandberg@ARM.com    friend class ::sc_gem5::Module;
949660SAndreas.Sandberg@ARM.com
959660SAndreas.Sandberg@ARM.com    // Implementation defined, but depended on by the tests.
969660SAndreas.Sandberg@ARM.com    virtual int vbind(sc_interface &) = 0;
979660SAndreas.Sandberg@ARM.com    virtual int vbind(sc_port_base &) = 0;
989660SAndreas.Sandberg@ARM.com
999660SAndreas.Sandberg@ARM.com    virtual void before_end_of_elaboration() = 0;
1009660SAndreas.Sandberg@ARM.com    virtual void end_of_elaboration() = 0;
1019660SAndreas.Sandberg@ARM.com    virtual void start_of_simulation() = 0;
1029660SAndreas.Sandberg@ARM.com    virtual void end_of_simulation() = 0;
1039660SAndreas.Sandberg@ARM.com
1049660SAndreas.Sandberg@ARM.com    void report_error(const char *id, const char *add_msg) const;
1059660SAndreas.Sandberg@ARM.com
1069660SAndreas.Sandberg@ARM.com  private:
1079660SAndreas.Sandberg@ARM.com    friend class ::sc_gem5::Port;
1089660SAndreas.Sandberg@ARM.com    friend class ::sc_gem5::Kernel;
1099660SAndreas.Sandberg@ARM.com
1109660SAndreas.Sandberg@ARM.com    virtual sc_interface *_gem5Interface(int n) const = 0;
1119660SAndreas.Sandberg@ARM.com    virtual void _gem5AddInterface(sc_interface *i) = 0;
1129660SAndreas.Sandberg@ARM.com
1139660SAndreas.Sandberg@ARM.com    ::sc_gem5::Port *_gem5Port;
1149660SAndreas.Sandberg@ARM.com    virtual const char *_ifTypeName() const = 0;
1159660SAndreas.Sandberg@ARM.com};
1169660SAndreas.Sandberg@ARM.com
1179660SAndreas.Sandberg@ARM.comtemplate <class IF>
1189660SAndreas.Sandberg@ARM.comclass sc_port_b : public sc_port_base
1199660SAndreas.Sandberg@ARM.com{
120  public:
121    void operator () (IF &i) { bind(i); }
122    void operator () (sc_port_b<IF> &p) { bind(p); }
123
124    virtual void bind(IF &i) { sc_port_base::bind(i); }
125    virtual void bind(sc_port_b<IF> &p) { sc_port_base::bind(p); }
126
127    IF *
128    operator -> ()
129    {
130        if (_interfaces.empty()) {
131            report_error("(E112) get interface failed", "port is not bound");
132            sc_abort();
133        }
134        return _interfaces[0];
135    }
136    const IF *
137    operator -> () const
138    {
139        if (_interfaces.empty()) {
140            report_error("(E112) get interface failed", "port is not bound");
141            sc_abort();
142        }
143        return _interfaces[0];
144    }
145
146    IF *
147    operator [] (int n)
148    {
149        if (n < 0 || n >= size()) {
150            report_error("(E112) get interface failed", "index out of range");
151            return NULL;
152        }
153        return _interfaces[n];
154    }
155    const IF *
156    operator [] (int n) const
157    {
158        if (n < 0 || n >= size()) {
159            report_error("(E112) get interface failed", "index out of range");
160            return NULL;
161        }
162        return _interfaces[n];
163    }
164
165    sc_interface *
166    get_interface()
167    {
168        if (_interfaces.empty())
169            return NULL;
170        return _interfaces[0];
171    }
172    const sc_interface *
173    get_interface() const
174    {
175        if (_interfaces.empty())
176            return NULL;
177        return _interfaces[0];
178    }
179
180  protected:
181    void before_end_of_elaboration() {}
182    void end_of_elaboration() {}
183    void start_of_simulation() {}
184    void end_of_simulation() {}
185
186    explicit sc_port_b(int n, sc_port_policy p) :
187            sc_port_base(sc_gen_unique_name("port"), n, p)
188    {}
189    sc_port_b(const char *name, int n, sc_port_policy p) :
190            sc_port_base(name, n, p)
191    {}
192    virtual ~sc_port_b() {}
193
194    // Implementation defined, but depended on by the tests.
195    int
196    vbind(sc_interface &i)
197    {
198        IF *interface = dynamic_cast<IF *>(&i);
199        if (!interface)
200            return 2;
201        sc_port_base::bind(*interface);
202        return 0;
203    }
204    int
205    vbind(sc_port_base &pb)
206    {
207        sc_port_b<IF> *p = dynamic_cast<sc_port_b<IF> *>(&pb);
208        if (!p)
209            return 2;
210        sc_port_base::bind(*p);
211        return 0;
212    }
213
214  private:
215    std::vector<IF *> _interfaces;
216
217    sc_interface *
218    _gem5Interface(int n) const
219    {
220        if (n < 0 || n >= size()) {
221            report_error("(E112) get interface failed", "index out of range");
222            return NULL;
223        }
224        return _interfaces[n];
225    }
226    void
227    _gem5AddInterface(sc_interface *iface)
228    {
229        IF *interface = dynamic_cast<IF *>(iface);
230        sc_assert(interface);
231        for (int i = 0; i < _interfaces.size(); i++) {
232            if (interface == _interfaces[i]) {
233                report_error("(E107) bind interface to port failed",
234                        "interface already bound to port");
235            }
236        }
237        _interfaces.push_back(interface);
238    }
239
240    const char *_ifTypeName() const { return typeid(IF).name(); }
241
242    // Disabled
243    sc_port_b() {}
244    sc_port_b(const sc_port_b<IF> &) {}
245    sc_port_b<IF> &operator = (const sc_port_b<IF> &) { return *this; }
246};
247
248template <class IF, int N=1, sc_port_policy P=SC_ONE_OR_MORE_BOUND>
249class sc_port : public sc_port_b<IF>
250{
251  public:
252    sc_port() : sc_port_b<IF>(N, P) {}
253    explicit sc_port(const char *name) : sc_port_b<IF>(name, N, P) {}
254    virtual ~sc_port() {}
255
256    // Deprecated binding constructors.
257    explicit sc_port(const IF &interface) : 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(const_cast<IF &>(interface));
262    }
263    sc_port(const char *name, const IF &interface) : sc_port_b<IF>(name, N, P)
264    {
265        this->warn_unimpl(__PRETTY_FUNCTION__);
266        // Should warn that these are deprecated. See Accellera sc_port.h.
267        sc_port_b<IF>::bind(const_cast<IF &>(interface));
268    }
269    explicit sc_port(sc_port_b<IF> &parent) : sc_port_b<IF>(N, P)
270    {
271        this->warn_unimpl(__PRETTY_FUNCTION__);
272        // Should warn that these are deprecated. See Accellera sc_port.h.
273        sc_port_b<IF>::bind(parent);
274    }
275    sc_port(const char *name, sc_port_b<IF> &parent) :
276        sc_port_b<IF>(name, N, P)
277    {
278        this->warn_unimpl(__PRETTY_FUNCTION__);
279        // Should warn that these are deprecated. See Accellera sc_port.h.
280        sc_port_b<IF>::bind(parent);
281    }
282    explicit sc_port(sc_port<IF, N, P> &parent) : sc_port_b<IF>(N, P)
283    {
284        this->warn_unimpl(__PRETTY_FUNCTION__);
285        // Should warn that these are deprecated. See Accellera sc_port.h.
286        sc_port_b<IF>::bind(parent);
287    }
288    sc_port(const char *name, sc_port<IF, N, P> &parent) :
289        sc_port_b<IF>(name, N, P)
290    {
291        this->warn_unimpl(__PRETTY_FUNCTION__);
292        // Should warn that these are deprecated. See Accellera sc_port.h.
293        sc_port_b<IF>::bind(parent);
294    }
295
296    virtual const char *kind() const { return "sc_port"; }
297
298  private:
299    // Disabled
300    sc_port(const sc_port<IF, N, P> &) {}
301    sc_port<IF, N, P> &operator = (const sc_port<IF, N, P> &) { return *this; }
302};
303
304} // namespace sc_core
305
306#endif  //__SYSTEMC_EXT_CORE_SC_PORT_HH__
307