sc_port.hh revision 13273
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
10412957Sgabeblack@google.com  private:
10513207Sgabeblack@google.com    friend class ::sc_gem5::Port;
10613053Sgabeblack@google.com    friend class ::sc_gem5::Kernel;
10713053Sgabeblack@google.com
10813053Sgabeblack@google.com    virtual sc_interface *_gem5Interface(int n) const = 0;
10913053Sgabeblack@google.com    virtual void _gem5AddInterface(sc_interface *i) = 0;
11012957Sgabeblack@google.com
11113207Sgabeblack@google.com    ::sc_gem5::Port *_gem5Port;
11213273Sgabeblack@google.com    virtual const char *_ifTypeName() const = 0;
11312837Sgabeblack@google.com};
11412837Sgabeblack@google.com
11512837Sgabeblack@google.comtemplate <class IF>
11612837Sgabeblack@google.comclass sc_port_b : public sc_port_base
11712837Sgabeblack@google.com{
11812837Sgabeblack@google.com  public:
11913053Sgabeblack@google.com    void operator () (IF &i) { bind(i); }
12013053Sgabeblack@google.com    void operator () (sc_port_b<IF> &p) { bind(p); }
12112837Sgabeblack@google.com
12213053Sgabeblack@google.com    virtual void bind(IF &i) { sc_port_base::bind(i); }
12313053Sgabeblack@google.com    virtual void bind(sc_port_b<IF> &p) { sc_port_base::bind(p); }
12412837Sgabeblack@google.com
12513090Sgabeblack@google.com    IF *
12613090Sgabeblack@google.com    operator -> ()
12713090Sgabeblack@google.com    {
12813090Sgabeblack@google.com        sc_assert(!_interfaces.empty());
12913090Sgabeblack@google.com        return _interfaces[0];
13013090Sgabeblack@google.com    }
13113090Sgabeblack@google.com    const IF *
13213090Sgabeblack@google.com    operator -> () const
13313090Sgabeblack@google.com    {
13413090Sgabeblack@google.com        sc_assert(!_interfaces.empty());
13513090Sgabeblack@google.com        return _interfaces[0];
13613090Sgabeblack@google.com    }
13712837Sgabeblack@google.com
13813090Sgabeblack@google.com    IF *
13913090Sgabeblack@google.com    operator [] (int n)
14013090Sgabeblack@google.com    {
14113090Sgabeblack@google.com        sc_assert(_interfaces.size() > n);
14213090Sgabeblack@google.com        return _interfaces[n];
14313090Sgabeblack@google.com    }
14413090Sgabeblack@google.com    const IF *
14513090Sgabeblack@google.com    operator [] (int n) const
14613090Sgabeblack@google.com    {
14713090Sgabeblack@google.com        sc_assert(_interfaces.size() > n);
14813090Sgabeblack@google.com        return _interfaces[n];
14913090Sgabeblack@google.com    }
15012837Sgabeblack@google.com
15113090Sgabeblack@google.com    sc_interface *
15213090Sgabeblack@google.com    get_interface()
15313090Sgabeblack@google.com    {
15413090Sgabeblack@google.com        sc_assert(!_interfaces.empty());
15513090Sgabeblack@google.com        return _interfaces[0];
15613090Sgabeblack@google.com    }
15713090Sgabeblack@google.com    const sc_interface *
15813090Sgabeblack@google.com    get_interface() const
15913090Sgabeblack@google.com    {
16013090Sgabeblack@google.com        sc_assert(!_interfaces.empty());
16113090Sgabeblack@google.com        return _interfaces[0];
16213090Sgabeblack@google.com    }
16312837Sgabeblack@google.com
16412837Sgabeblack@google.com  protected:
16513059Sgabeblack@google.com    void before_end_of_elaboration() {}
16613059Sgabeblack@google.com    void end_of_elaboration() {}
16713059Sgabeblack@google.com    void start_of_simulation() {}
16813059Sgabeblack@google.com    void end_of_simulation() {}
16912837Sgabeblack@google.com
17012842Sgabeblack@google.com    explicit sc_port_b(int n, sc_port_policy p) :
17113036Sgabeblack@google.com            sc_port_base(sc_gen_unique_name("port"), n, p)
17212842Sgabeblack@google.com    {}
17312842Sgabeblack@google.com    sc_port_b(const char *name, int n, sc_port_policy p) :
17412842Sgabeblack@google.com            sc_port_base(name, n, p)
17512842Sgabeblack@google.com    {}
17612842Sgabeblack@google.com    virtual ~sc_port_b() {}
17712837Sgabeblack@google.com
17812944Sgabeblack@google.com    // Implementation defined, but depended on by the tests.
17912944Sgabeblack@google.com    int
18013053Sgabeblack@google.com    vbind(sc_interface &i)
18112944Sgabeblack@google.com    {
18213053Sgabeblack@google.com        IF *interface = dynamic_cast<IF *>(&i);
18313053Sgabeblack@google.com        if (!interface)
18413053Sgabeblack@google.com            return 2;
18513053Sgabeblack@google.com        sc_port_base::bind(*interface);
18612944Sgabeblack@google.com        return 0;
18712944Sgabeblack@google.com    }
18812944Sgabeblack@google.com    int
18913053Sgabeblack@google.com    vbind(sc_port_base &pb)
19012944Sgabeblack@google.com    {
19113053Sgabeblack@google.com        sc_port_b<IF> *p = dynamic_cast<sc_port_b<IF> *>(&pb);
19213053Sgabeblack@google.com        if (!p)
19313053Sgabeblack@google.com            return 2;
19413053Sgabeblack@google.com        sc_port_base::bind(*p);
19512944Sgabeblack@google.com        return 0;
19612944Sgabeblack@google.com    }
19712944Sgabeblack@google.com
19812837Sgabeblack@google.com  private:
19913053Sgabeblack@google.com    std::vector<IF *> _interfaces;
20013053Sgabeblack@google.com
20113090Sgabeblack@google.com    sc_interface *
20213090Sgabeblack@google.com    _gem5Interface(int n) const
20313090Sgabeblack@google.com    {
20413090Sgabeblack@google.com        sc_assert(_interfaces.size() > n);
20513090Sgabeblack@google.com        return _interfaces[n];
20613090Sgabeblack@google.com    }
20713053Sgabeblack@google.com    void
20813053Sgabeblack@google.com    _gem5AddInterface(sc_interface *i)
20913053Sgabeblack@google.com    {
21013053Sgabeblack@google.com        IF *interface = dynamic_cast<IF *>(i);
21113053Sgabeblack@google.com        sc_assert(interface);
21213053Sgabeblack@google.com        _interfaces.push_back(interface);
21313053Sgabeblack@google.com    }
21413053Sgabeblack@google.com
21513273Sgabeblack@google.com    const char *_ifTypeName() const { return typeid(IF).name(); }
21613273Sgabeblack@google.com
21712837Sgabeblack@google.com    // Disabled
21812837Sgabeblack@google.com    sc_port_b() {}
21912837Sgabeblack@google.com    sc_port_b(const sc_port_b<IF> &) {}
22012837Sgabeblack@google.com    sc_port_b<IF> &operator = (const sc_port_b<IF> &) { return *this; }
22112837Sgabeblack@google.com};
22212837Sgabeblack@google.com
22312837Sgabeblack@google.comtemplate <class IF, int N=1, sc_port_policy P=SC_ONE_OR_MORE_BOUND>
22412837Sgabeblack@google.comclass sc_port : public sc_port_b<IF>
22512837Sgabeblack@google.com{
22612837Sgabeblack@google.com  public:
22712868Sgabeblack@google.com    sc_port() : sc_port_b<IF>(N, P) {}
22812842Sgabeblack@google.com    explicit sc_port(const char *name) : sc_port_b<IF>(name, N, P) {}
22912842Sgabeblack@google.com    virtual ~sc_port() {}
23012837Sgabeblack@google.com
23112868Sgabeblack@google.com    // Deprecated binding constructors.
23212868Sgabeblack@google.com    explicit sc_port(const IF &interface) : sc_port_b<IF>(N, P)
23312868Sgabeblack@google.com    {
23412868Sgabeblack@google.com        this->warn_unimpl(__PRETTY_FUNCTION__);
23512868Sgabeblack@google.com        // Should warn that these are deprecated. See Accellera sc_port.h.
23612868Sgabeblack@google.com        sc_port_b<IF>::bind(const_cast<IF &>(interface));
23712868Sgabeblack@google.com    }
23812868Sgabeblack@google.com    sc_port(const char *name, const IF &interface) : sc_port_b<IF>(name, N, P)
23912868Sgabeblack@google.com    {
24012868Sgabeblack@google.com        this->warn_unimpl(__PRETTY_FUNCTION__);
24112868Sgabeblack@google.com        // Should warn that these are deprecated. See Accellera sc_port.h.
24212868Sgabeblack@google.com        sc_port_b<IF>::bind(const_cast<IF &>(interface));
24312868Sgabeblack@google.com    }
24412868Sgabeblack@google.com    explicit sc_port(sc_port_b<IF> &parent) : sc_port_b<IF>(N, P)
24512868Sgabeblack@google.com    {
24612868Sgabeblack@google.com        this->warn_unimpl(__PRETTY_FUNCTION__);
24712868Sgabeblack@google.com        // Should warn that these are deprecated. See Accellera sc_port.h.
24812868Sgabeblack@google.com        sc_port_b<IF>::bind(parent);
24912868Sgabeblack@google.com    }
25012868Sgabeblack@google.com    sc_port(const char *name, sc_port_b<IF> &parent) :
25112868Sgabeblack@google.com        sc_port_b<IF>(name, N, P)
25212868Sgabeblack@google.com    {
25312868Sgabeblack@google.com        this->warn_unimpl(__PRETTY_FUNCTION__);
25412868Sgabeblack@google.com        // Should warn that these are deprecated. See Accellera sc_port.h.
25512868Sgabeblack@google.com        sc_port_b<IF>::bind(parent);
25612868Sgabeblack@google.com    }
25712868Sgabeblack@google.com    explicit sc_port(sc_port<IF, N, P> &parent) : 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(parent);
26212868Sgabeblack@google.com    }
26312868Sgabeblack@google.com    sc_port(const char *name, sc_port<IF, N, P> &parent) :
26412868Sgabeblack@google.com        sc_port_b<IF>(name, N, P)
26512868Sgabeblack@google.com    {
26612868Sgabeblack@google.com        this->warn_unimpl(__PRETTY_FUNCTION__);
26712868Sgabeblack@google.com        // Should warn that these are deprecated. See Accellera sc_port.h.
26812868Sgabeblack@google.com        sc_port_b<IF>::bind(parent);
26912868Sgabeblack@google.com    }
27012868Sgabeblack@google.com
27112842Sgabeblack@google.com    virtual const char *kind() const { return "sc_port"; }
27212837Sgabeblack@google.com
27312837Sgabeblack@google.com  private:
27412837Sgabeblack@google.com    // Disabled
27512837Sgabeblack@google.com    sc_port(const sc_port<IF, N, P> &) {}
27612837Sgabeblack@google.com    sc_port<IF, N, P> &operator = (const sc_port<IF, N, P> &) { return *this; }
27712837Sgabeblack@google.com};
27812837Sgabeblack@google.com
27912837Sgabeblack@google.com} // namespace sc_core
28012837Sgabeblack@google.com
28112837Sgabeblack@google.com#endif  //__SYSTEMC_EXT_CORE_SC_PORT_HH__
282