sc_port.hh revision 13290
112837Sgabeblack@google.com/*
212837Sgabeblack@google.com * Copyright 2018 Google, Inc.
312837Sgabeblack@google.com *
412837Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512837Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612837Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812837Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012837Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112837Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212837Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312837Sgabeblack@google.com * this software without specific prior written permission.
1412837Sgabeblack@google.com *
1512837Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612837Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712837Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812837Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912837Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012837Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112837Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212837Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312837Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412837Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512837Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612837Sgabeblack@google.com *
2712837Sgabeblack@google.com * Authors: Gabe Black
2812837Sgabeblack@google.com */
2912837Sgabeblack@google.com
3012837Sgabeblack@google.com#ifndef __SYSTEMC_EXT_CORE_SC_PORT_HH__
3112837Sgabeblack@google.com#define __SYSTEMC_EXT_CORE_SC_PORT_HH__
3212837Sgabeblack@google.com
3313273Sgabeblack@google.com#include <typeinfo>
3412957Sgabeblack@google.com#include <vector>
3512957Sgabeblack@google.com
3613053Sgabeblack@google.com#include "../utils/sc_report_handler.hh"
3712842Sgabeblack@google.com#include "sc_module.hh" // for sc_gen_unique_name
3812837Sgabeblack@google.com#include "sc_object.hh"
3912837Sgabeblack@google.com
4012957Sgabeblack@google.comnamespace sc_gem5
4112957Sgabeblack@google.com{
4212957Sgabeblack@google.com
4313207Sgabeblack@google.comclass Port;
4412957Sgabeblack@google.com
4512957Sgabeblack@google.com};
4612957Sgabeblack@google.com
4712837Sgabeblack@google.comnamespace sc_core
4812837Sgabeblack@google.com{
4912837Sgabeblack@google.com
5012837Sgabeblack@google.comclass sc_interface;
5113245Sgabeblack@google.comclass sc_trace_file;
5213245Sgabeblack@google.com
5313245Sgabeblack@google.com// Nonstandard
5413245Sgabeblack@google.com// Despite having a warning "FOR INTERNAL USE ONLY!" in all caps above this
5513245Sgabeblack@google.com// class definition in the Accellera implementation, it appears in their
5613245Sgabeblack@google.com// examples and test programs, and so we need to have it here as well.
5713245Sgabeblack@google.comstruct sc_trace_params
5813245Sgabeblack@google.com{
5913245Sgabeblack@google.com    sc_trace_file *tf;
6013245Sgabeblack@google.com    std::string name;
6113245Sgabeblack@google.com
6213245Sgabeblack@google.com    sc_trace_params(sc_trace_file *tf, const std::string &name) :
6313245Sgabeblack@google.com        tf(tf), name(name)
6413245Sgabeblack@google.com    {}
6513245Sgabeblack@google.com};
6613245Sgabeblack@google.comtypedef std::vector<sc_trace_params *> sc_trace_params_vec;
6712837Sgabeblack@google.com
6812837Sgabeblack@google.comenum sc_port_policy
6912837Sgabeblack@google.com{
7012837Sgabeblack@google.com    SC_ONE_OR_MORE_BOUND, // Default
7112837Sgabeblack@google.com    SC_ZERO_OR_MORE_BOUND,
7212837Sgabeblack@google.com    SC_ALL_BOUND
7312837Sgabeblack@google.com};
7412837Sgabeblack@google.com
7512837Sgabeblack@google.comclass sc_port_base : public sc_object
7612837Sgabeblack@google.com{
7712837Sgabeblack@google.com  public:
7812957Sgabeblack@google.com    sc_port_base(const char *name, int n, sc_port_policy p);
7913207Sgabeblack@google.com    virtual ~sc_port_base();
8012842Sgabeblack@google.com
8112842Sgabeblack@google.com    void warn_unimpl(const char *func) const;
8212938Sgabeblack@google.com
8312957Sgabeblack@google.com    int maxSize() const;
8412957Sgabeblack@google.com    int size() const;
8512957Sgabeblack@google.com
8613202Sgabeblack@google.com    const char *kind() const { return "sc_port_base"; }
8713202Sgabeblack@google.com
8812938Sgabeblack@google.com  protected:
8912938Sgabeblack@google.com    // Implementation defined, but depended on by the tests.
9012938Sgabeblack@google.com    void bind(sc_interface &);
9112938Sgabeblack@google.com    void bind(sc_port_base &);
9212944Sgabeblack@google.com
9313091Sgabeblack@google.com    friend class ::sc_gem5::Module;
9413091Sgabeblack@google.com
9512944Sgabeblack@google.com    // Implementation defined, but depended on by the tests.
9612944Sgabeblack@google.com    virtual int vbind(sc_interface &) = 0;
9712944Sgabeblack@google.com    virtual int vbind(sc_port_base &) = 0;
9812957Sgabeblack@google.com
9913059Sgabeblack@google.com    virtual void before_end_of_elaboration() = 0;
10013059Sgabeblack@google.com    virtual void end_of_elaboration() = 0;
10113059Sgabeblack@google.com    virtual void start_of_simulation() = 0;
10213059Sgabeblack@google.com    virtual void end_of_simulation() = 0;
10313059Sgabeblack@google.com
10413290Sgabeblack@google.com    void report_error(const char *id, const char *add_msg) const;
10513290Sgabeblack@google.com
10612957Sgabeblack@google.com  private:
10713207Sgabeblack@google.com    friend class ::sc_gem5::Port;
10813053Sgabeblack@google.com    friend class ::sc_gem5::Kernel;
10913053Sgabeblack@google.com
11013053Sgabeblack@google.com    virtual sc_interface *_gem5Interface(int n) const = 0;
11113053Sgabeblack@google.com    virtual void _gem5AddInterface(sc_interface *i) = 0;
11212957Sgabeblack@google.com
11313207Sgabeblack@google.com    ::sc_gem5::Port *_gem5Port;
11413273Sgabeblack@google.com    virtual const char *_ifTypeName() const = 0;
11512837Sgabeblack@google.com};
11612837Sgabeblack@google.com
11712837Sgabeblack@google.comtemplate <class IF>
11812837Sgabeblack@google.comclass sc_port_b : public sc_port_base
11912837Sgabeblack@google.com{
12012837Sgabeblack@google.com  public:
12113053Sgabeblack@google.com    void operator () (IF &i) { bind(i); }
12213053Sgabeblack@google.com    void operator () (sc_port_b<IF> &p) { bind(p); }
12312837Sgabeblack@google.com
12413053Sgabeblack@google.com    virtual void bind(IF &i) { sc_port_base::bind(i); }
12513053Sgabeblack@google.com    virtual void bind(sc_port_b<IF> &p) { sc_port_base::bind(p); }
12612837Sgabeblack@google.com
12713090Sgabeblack@google.com    IF *
12813090Sgabeblack@google.com    operator -> ()
12913090Sgabeblack@google.com    {
13013290Sgabeblack@google.com        if (_interfaces.empty()) {
13113290Sgabeblack@google.com            report_error("(E112) get interface failed", "port is not bound");
13213290Sgabeblack@google.com            sc_abort();
13313290Sgabeblack@google.com        }
13413090Sgabeblack@google.com        return _interfaces[0];
13513090Sgabeblack@google.com    }
13613090Sgabeblack@google.com    const IF *
13713090Sgabeblack@google.com    operator -> () const
13813090Sgabeblack@google.com    {
13913290Sgabeblack@google.com        if (_interfaces.empty()) {
14013290Sgabeblack@google.com            report_error("(E112) get interface failed", "port is not bound");
14113290Sgabeblack@google.com            sc_abort();
14213290Sgabeblack@google.com        }
14313090Sgabeblack@google.com        return _interfaces[0];
14413090Sgabeblack@google.com    }
14512837Sgabeblack@google.com
14613090Sgabeblack@google.com    IF *
14713090Sgabeblack@google.com    operator [] (int n)
14813090Sgabeblack@google.com    {
14913290Sgabeblack@google.com        if (n < 0 || n >= size()) {
15013290Sgabeblack@google.com            report_error("(E112) get interface failed", "index out of range");
15113290Sgabeblack@google.com            return NULL;
15213290Sgabeblack@google.com        }
15313090Sgabeblack@google.com        return _interfaces[n];
15413090Sgabeblack@google.com    }
15513090Sgabeblack@google.com    const IF *
15613090Sgabeblack@google.com    operator [] (int n) const
15713090Sgabeblack@google.com    {
15813290Sgabeblack@google.com        if (n < 0 || n >= size()) {
15913290Sgabeblack@google.com            report_error("(E112) get interface failed", "index out of range");
16013290Sgabeblack@google.com            return NULL;
16113290Sgabeblack@google.com        }
16213090Sgabeblack@google.com        return _interfaces[n];
16313090Sgabeblack@google.com    }
16412837Sgabeblack@google.com
16513090Sgabeblack@google.com    sc_interface *
16613090Sgabeblack@google.com    get_interface()
16713090Sgabeblack@google.com    {
16813290Sgabeblack@google.com        if (_interfaces.empty())
16913290Sgabeblack@google.com            return NULL;
17013090Sgabeblack@google.com        return _interfaces[0];
17113090Sgabeblack@google.com    }
17213090Sgabeblack@google.com    const sc_interface *
17313090Sgabeblack@google.com    get_interface() const
17413090Sgabeblack@google.com    {
17513290Sgabeblack@google.com        if (_interfaces.empty())
17613290Sgabeblack@google.com            return NULL;
17713090Sgabeblack@google.com        return _interfaces[0];
17813090Sgabeblack@google.com    }
17912837Sgabeblack@google.com
18012837Sgabeblack@google.com  protected:
18113059Sgabeblack@google.com    void before_end_of_elaboration() {}
18213059Sgabeblack@google.com    void end_of_elaboration() {}
18313059Sgabeblack@google.com    void start_of_simulation() {}
18413059Sgabeblack@google.com    void end_of_simulation() {}
18512837Sgabeblack@google.com
18612842Sgabeblack@google.com    explicit sc_port_b(int n, sc_port_policy p) :
18713036Sgabeblack@google.com            sc_port_base(sc_gen_unique_name("port"), n, p)
18812842Sgabeblack@google.com    {}
18912842Sgabeblack@google.com    sc_port_b(const char *name, int n, sc_port_policy p) :
19012842Sgabeblack@google.com            sc_port_base(name, n, p)
19112842Sgabeblack@google.com    {}
19212842Sgabeblack@google.com    virtual ~sc_port_b() {}
19312837Sgabeblack@google.com
19412944Sgabeblack@google.com    // Implementation defined, but depended on by the tests.
19512944Sgabeblack@google.com    int
19613053Sgabeblack@google.com    vbind(sc_interface &i)
19712944Sgabeblack@google.com    {
19813053Sgabeblack@google.com        IF *interface = dynamic_cast<IF *>(&i);
19913053Sgabeblack@google.com        if (!interface)
20013053Sgabeblack@google.com            return 2;
20113053Sgabeblack@google.com        sc_port_base::bind(*interface);
20212944Sgabeblack@google.com        return 0;
20312944Sgabeblack@google.com    }
20412944Sgabeblack@google.com    int
20513053Sgabeblack@google.com    vbind(sc_port_base &pb)
20612944Sgabeblack@google.com    {
20713053Sgabeblack@google.com        sc_port_b<IF> *p = dynamic_cast<sc_port_b<IF> *>(&pb);
20813053Sgabeblack@google.com        if (!p)
20913053Sgabeblack@google.com            return 2;
21013053Sgabeblack@google.com        sc_port_base::bind(*p);
21112944Sgabeblack@google.com        return 0;
21212944Sgabeblack@google.com    }
21312944Sgabeblack@google.com
21412837Sgabeblack@google.com  private:
21513053Sgabeblack@google.com    std::vector<IF *> _interfaces;
21613053Sgabeblack@google.com
21713090Sgabeblack@google.com    sc_interface *
21813090Sgabeblack@google.com    _gem5Interface(int n) const
21913090Sgabeblack@google.com    {
22013290Sgabeblack@google.com        if (n < 0 || n >= size()) {
22113290Sgabeblack@google.com            report_error("(E112) get interface failed", "index out of range");
22213290Sgabeblack@google.com            return NULL;
22313290Sgabeblack@google.com        }
22413090Sgabeblack@google.com        return _interfaces[n];
22513090Sgabeblack@google.com    }
22613053Sgabeblack@google.com    void
22713290Sgabeblack@google.com    _gem5AddInterface(sc_interface *iface)
22813053Sgabeblack@google.com    {
22913290Sgabeblack@google.com        IF *interface = dynamic_cast<IF *>(iface);
23013053Sgabeblack@google.com        sc_assert(interface);
23113290Sgabeblack@google.com        for (int i = 0; i < _interfaces.size(); i++) {
23213290Sgabeblack@google.com            if (interface == _interfaces[i]) {
23313290Sgabeblack@google.com                report_error("(E107) bind interface to port failed",
23413290Sgabeblack@google.com                        "interface already bound to port");
23513290Sgabeblack@google.com            }
23613290Sgabeblack@google.com        }
23713053Sgabeblack@google.com        _interfaces.push_back(interface);
23813053Sgabeblack@google.com    }
23913053Sgabeblack@google.com
24013273Sgabeblack@google.com    const char *_ifTypeName() const { return typeid(IF).name(); }
24113273Sgabeblack@google.com
24212837Sgabeblack@google.com    // Disabled
24312837Sgabeblack@google.com    sc_port_b() {}
24412837Sgabeblack@google.com    sc_port_b(const sc_port_b<IF> &) {}
24512837Sgabeblack@google.com    sc_port_b<IF> &operator = (const sc_port_b<IF> &) { return *this; }
24612837Sgabeblack@google.com};
24712837Sgabeblack@google.com
24812837Sgabeblack@google.comtemplate <class IF, int N=1, sc_port_policy P=SC_ONE_OR_MORE_BOUND>
24912837Sgabeblack@google.comclass sc_port : public sc_port_b<IF>
25012837Sgabeblack@google.com{
25112837Sgabeblack@google.com  public:
25212868Sgabeblack@google.com    sc_port() : sc_port_b<IF>(N, P) {}
25312842Sgabeblack@google.com    explicit sc_port(const char *name) : sc_port_b<IF>(name, N, P) {}
25412842Sgabeblack@google.com    virtual ~sc_port() {}
25512837Sgabeblack@google.com
25612868Sgabeblack@google.com    // Deprecated binding constructors.
25712868Sgabeblack@google.com    explicit sc_port(const IF &interface) : sc_port_b<IF>(N, P)
25812868Sgabeblack@google.com    {
25912868Sgabeblack@google.com        this->warn_unimpl(__PRETTY_FUNCTION__);
26012868Sgabeblack@google.com        // Should warn that these are deprecated. See Accellera sc_port.h.
26112868Sgabeblack@google.com        sc_port_b<IF>::bind(const_cast<IF &>(interface));
26212868Sgabeblack@google.com    }
26312868Sgabeblack@google.com    sc_port(const char *name, const IF &interface) : sc_port_b<IF>(name, N, P)
26412868Sgabeblack@google.com    {
26512868Sgabeblack@google.com        this->warn_unimpl(__PRETTY_FUNCTION__);
26612868Sgabeblack@google.com        // Should warn that these are deprecated. See Accellera sc_port.h.
26712868Sgabeblack@google.com        sc_port_b<IF>::bind(const_cast<IF &>(interface));
26812868Sgabeblack@google.com    }
26912868Sgabeblack@google.com    explicit sc_port(sc_port_b<IF> &parent) : sc_port_b<IF>(N, P)
27012868Sgabeblack@google.com    {
27112868Sgabeblack@google.com        this->warn_unimpl(__PRETTY_FUNCTION__);
27212868Sgabeblack@google.com        // Should warn that these are deprecated. See Accellera sc_port.h.
27312868Sgabeblack@google.com        sc_port_b<IF>::bind(parent);
27412868Sgabeblack@google.com    }
27512868Sgabeblack@google.com    sc_port(const char *name, sc_port_b<IF> &parent) :
27612868Sgabeblack@google.com        sc_port_b<IF>(name, N, P)
27712868Sgabeblack@google.com    {
27812868Sgabeblack@google.com        this->warn_unimpl(__PRETTY_FUNCTION__);
27912868Sgabeblack@google.com        // Should warn that these are deprecated. See Accellera sc_port.h.
28012868Sgabeblack@google.com        sc_port_b<IF>::bind(parent);
28112868Sgabeblack@google.com    }
28212868Sgabeblack@google.com    explicit sc_port(sc_port<IF, N, P> &parent) : sc_port_b<IF>(N, P)
28312868Sgabeblack@google.com    {
28412868Sgabeblack@google.com        this->warn_unimpl(__PRETTY_FUNCTION__);
28512868Sgabeblack@google.com        // Should warn that these are deprecated. See Accellera sc_port.h.
28612868Sgabeblack@google.com        sc_port_b<IF>::bind(parent);
28712868Sgabeblack@google.com    }
28812868Sgabeblack@google.com    sc_port(const char *name, sc_port<IF, N, P> &parent) :
28912868Sgabeblack@google.com        sc_port_b<IF>(name, N, P)
29012868Sgabeblack@google.com    {
29112868Sgabeblack@google.com        this->warn_unimpl(__PRETTY_FUNCTION__);
29212868Sgabeblack@google.com        // Should warn that these are deprecated. See Accellera sc_port.h.
29312868Sgabeblack@google.com        sc_port_b<IF>::bind(parent);
29412868Sgabeblack@google.com    }
29512868Sgabeblack@google.com
29612842Sgabeblack@google.com    virtual const char *kind() const { return "sc_port"; }
29712837Sgabeblack@google.com
29812837Sgabeblack@google.com  private:
29912837Sgabeblack@google.com    // Disabled
30012837Sgabeblack@google.com    sc_port(const sc_port<IF, N, P> &) {}
30112837Sgabeblack@google.com    sc_port<IF, N, P> &operator = (const sc_port<IF, N, P> &) { return *this; }
30212837Sgabeblack@google.com};
30312837Sgabeblack@google.com
30412837Sgabeblack@google.com} // namespace sc_core
30512837Sgabeblack@google.com
30612837Sgabeblack@google.com#endif  //__SYSTEMC_EXT_CORE_SC_PORT_HH__
307