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
3613324Sgabeblack@google.com#include "../channel/messages.hh"
3713053Sgabeblack@google.com#include "../utils/sc_report_handler.hh"
3812842Sgabeblack@google.com#include "sc_module.hh" // for sc_gen_unique_name
3912837Sgabeblack@google.com#include "sc_object.hh"
4012837Sgabeblack@google.com
4112957Sgabeblack@google.comnamespace sc_gem5
4212957Sgabeblack@google.com{
4312957Sgabeblack@google.com
4413207Sgabeblack@google.comclass Port;
4512957Sgabeblack@google.com
4612957Sgabeblack@google.com};
4712957Sgabeblack@google.com
4812837Sgabeblack@google.comnamespace sc_core
4912837Sgabeblack@google.com{
5012837Sgabeblack@google.com
5112837Sgabeblack@google.comclass sc_interface;
5213245Sgabeblack@google.comclass sc_trace_file;
5313245Sgabeblack@google.com
5413245Sgabeblack@google.com// Nonstandard
5513245Sgabeblack@google.com// Despite having a warning "FOR INTERNAL USE ONLY!" in all caps above this
5613245Sgabeblack@google.com// class definition in the Accellera implementation, it appears in their
5713245Sgabeblack@google.com// examples and test programs, and so we need to have it here as well.
5813245Sgabeblack@google.comstruct sc_trace_params
5913245Sgabeblack@google.com{
6013245Sgabeblack@google.com    sc_trace_file *tf;
6113245Sgabeblack@google.com    std::string name;
6213245Sgabeblack@google.com
6313245Sgabeblack@google.com    sc_trace_params(sc_trace_file *tf, const std::string &name) :
6413245Sgabeblack@google.com        tf(tf), name(name)
6513245Sgabeblack@google.com    {}
6613245Sgabeblack@google.com};
6713245Sgabeblack@google.comtypedef std::vector<sc_trace_params *> sc_trace_params_vec;
6812837Sgabeblack@google.com
6912837Sgabeblack@google.comenum sc_port_policy
7012837Sgabeblack@google.com{
7112837Sgabeblack@google.com    SC_ONE_OR_MORE_BOUND, // Default
7212837Sgabeblack@google.com    SC_ZERO_OR_MORE_BOUND,
7312837Sgabeblack@google.com    SC_ALL_BOUND
7412837Sgabeblack@google.com};
7512837Sgabeblack@google.com
7612837Sgabeblack@google.comclass sc_port_base : public sc_object
7712837Sgabeblack@google.com{
7812837Sgabeblack@google.com  public:
7912957Sgabeblack@google.com    sc_port_base(const char *name, int n, sc_port_policy p);
8013207Sgabeblack@google.com    virtual ~sc_port_base();
8112842Sgabeblack@google.com
8213332Sgabeblack@google.com    void warn_port_constructor() const;
8312938Sgabeblack@google.com
8412957Sgabeblack@google.com    int maxSize() const;
8512957Sgabeblack@google.com    int size() const;
8612957Sgabeblack@google.com
8713202Sgabeblack@google.com    const char *kind() const { return "sc_port_base"; }
8813202Sgabeblack@google.com
8912938Sgabeblack@google.com  protected:
9012938Sgabeblack@google.com    // Implementation defined, but depended on by the tests.
9112938Sgabeblack@google.com    void bind(sc_interface &);
9212938Sgabeblack@google.com    void bind(sc_port_base &);
9312944Sgabeblack@google.com
9413091Sgabeblack@google.com    friend class ::sc_gem5::Module;
9513091Sgabeblack@google.com
9612944Sgabeblack@google.com    // Implementation defined, but depended on by the tests.
9712944Sgabeblack@google.com    virtual int vbind(sc_interface &) = 0;
9812944Sgabeblack@google.com    virtual int vbind(sc_port_base &) = 0;
9912957Sgabeblack@google.com
10013059Sgabeblack@google.com    virtual void before_end_of_elaboration() = 0;
10113059Sgabeblack@google.com    virtual void end_of_elaboration() = 0;
10213059Sgabeblack@google.com    virtual void start_of_simulation() = 0;
10313059Sgabeblack@google.com    virtual void end_of_simulation() = 0;
10413059Sgabeblack@google.com
10513290Sgabeblack@google.com    void report_error(const char *id, const char *add_msg) const;
10613290Sgabeblack@google.com
10712957Sgabeblack@google.com  private:
10813207Sgabeblack@google.com    friend class ::sc_gem5::Port;
10913053Sgabeblack@google.com    friend class ::sc_gem5::Kernel;
11013053Sgabeblack@google.com
11113053Sgabeblack@google.com    virtual sc_interface *_gem5Interface(int n) const = 0;
11213053Sgabeblack@google.com    virtual void _gem5AddInterface(sc_interface *i) = 0;
11312957Sgabeblack@google.com
11413207Sgabeblack@google.com    ::sc_gem5::Port *_gem5Port;
11513273Sgabeblack@google.com    virtual const char *_ifTypeName() const = 0;
11613293Sgabeblack@google.com    virtual sc_port_policy _portPolicy() const = 0;
11712837Sgabeblack@google.com};
11812837Sgabeblack@google.com
11912837Sgabeblack@google.comtemplate <class IF>
12012837Sgabeblack@google.comclass sc_port_b : public sc_port_base
12112837Sgabeblack@google.com{
12212837Sgabeblack@google.com  public:
12313053Sgabeblack@google.com    void operator () (IF &i) { bind(i); }
12413053Sgabeblack@google.com    void operator () (sc_port_b<IF> &p) { bind(p); }
12512837Sgabeblack@google.com
12613053Sgabeblack@google.com    virtual void bind(IF &i) { sc_port_base::bind(i); }
12713053Sgabeblack@google.com    virtual void bind(sc_port_b<IF> &p) { sc_port_base::bind(p); }
12812837Sgabeblack@google.com
12913090Sgabeblack@google.com    IF *
13013090Sgabeblack@google.com    operator -> ()
13113090Sgabeblack@google.com    {
13213290Sgabeblack@google.com        if (_interfaces.empty()) {
13313324Sgabeblack@google.com            report_error(SC_ID_GET_IF_, "port is not bound");
13413290Sgabeblack@google.com            sc_abort();
13513290Sgabeblack@google.com        }
13613090Sgabeblack@google.com        return _interfaces[0];
13713090Sgabeblack@google.com    }
13813090Sgabeblack@google.com    const IF *
13913090Sgabeblack@google.com    operator -> () const
14013090Sgabeblack@google.com    {
14113290Sgabeblack@google.com        if (_interfaces.empty()) {
14213324Sgabeblack@google.com            report_error(SC_ID_GET_IF_, "port is not bound");
14313290Sgabeblack@google.com            sc_abort();
14413290Sgabeblack@google.com        }
14513090Sgabeblack@google.com        return _interfaces[0];
14613090Sgabeblack@google.com    }
14712837Sgabeblack@google.com
14813090Sgabeblack@google.com    IF *
14913090Sgabeblack@google.com    operator [] (int n)
15013090Sgabeblack@google.com    {
15113290Sgabeblack@google.com        if (n < 0 || n >= size()) {
15213324Sgabeblack@google.com            report_error(SC_ID_GET_IF_, "index out of range");
15313290Sgabeblack@google.com            return NULL;
15413290Sgabeblack@google.com        }
15513090Sgabeblack@google.com        return _interfaces[n];
15613090Sgabeblack@google.com    }
15713090Sgabeblack@google.com    const IF *
15813090Sgabeblack@google.com    operator [] (int n) const
15913090Sgabeblack@google.com    {
16013290Sgabeblack@google.com        if (n < 0 || n >= size()) {
16113324Sgabeblack@google.com            report_error(SC_ID_GET_IF_, "index out of range");
16213290Sgabeblack@google.com            return NULL;
16313290Sgabeblack@google.com        }
16413090Sgabeblack@google.com        return _interfaces[n];
16513090Sgabeblack@google.com    }
16612837Sgabeblack@google.com
16713090Sgabeblack@google.com    sc_interface *
16813090Sgabeblack@google.com    get_interface()
16913090Sgabeblack@google.com    {
17013290Sgabeblack@google.com        if (_interfaces.empty())
17113290Sgabeblack@google.com            return NULL;
17213090Sgabeblack@google.com        return _interfaces[0];
17313090Sgabeblack@google.com    }
17413090Sgabeblack@google.com    const sc_interface *
17513090Sgabeblack@google.com    get_interface() const
17613090Sgabeblack@google.com    {
17713290Sgabeblack@google.com        if (_interfaces.empty())
17813290Sgabeblack@google.com            return NULL;
17913090Sgabeblack@google.com        return _interfaces[0];
18013090Sgabeblack@google.com    }
18112837Sgabeblack@google.com
18212837Sgabeblack@google.com  protected:
18313059Sgabeblack@google.com    void before_end_of_elaboration() {}
18413059Sgabeblack@google.com    void end_of_elaboration() {}
18513059Sgabeblack@google.com    void start_of_simulation() {}
18613059Sgabeblack@google.com    void end_of_simulation() {}
18712837Sgabeblack@google.com
18812842Sgabeblack@google.com    explicit sc_port_b(int n, sc_port_policy p) :
18913036Sgabeblack@google.com            sc_port_base(sc_gen_unique_name("port"), n, p)
19012842Sgabeblack@google.com    {}
19112842Sgabeblack@google.com    sc_port_b(const char *name, int n, sc_port_policy p) :
19212842Sgabeblack@google.com            sc_port_base(name, n, p)
19312842Sgabeblack@google.com    {}
19412842Sgabeblack@google.com    virtual ~sc_port_b() {}
19512837Sgabeblack@google.com
19612944Sgabeblack@google.com    // Implementation defined, but depended on by the tests.
19712944Sgabeblack@google.com    int
19813053Sgabeblack@google.com    vbind(sc_interface &i)
19912944Sgabeblack@google.com    {
20013053Sgabeblack@google.com        IF *interface = dynamic_cast<IF *>(&i);
20113053Sgabeblack@google.com        if (!interface)
20213053Sgabeblack@google.com            return 2;
20313053Sgabeblack@google.com        sc_port_base::bind(*interface);
20412944Sgabeblack@google.com        return 0;
20512944Sgabeblack@google.com    }
20612944Sgabeblack@google.com    int
20713053Sgabeblack@google.com    vbind(sc_port_base &pb)
20812944Sgabeblack@google.com    {
20913053Sgabeblack@google.com        sc_port_b<IF> *p = dynamic_cast<sc_port_b<IF> *>(&pb);
21013053Sgabeblack@google.com        if (!p)
21113053Sgabeblack@google.com            return 2;
21213053Sgabeblack@google.com        sc_port_base::bind(*p);
21312944Sgabeblack@google.com        return 0;
21412944Sgabeblack@google.com    }
21512944Sgabeblack@google.com
21612837Sgabeblack@google.com  private:
21713053Sgabeblack@google.com    std::vector<IF *> _interfaces;
21813053Sgabeblack@google.com
21913090Sgabeblack@google.com    sc_interface *
22013090Sgabeblack@google.com    _gem5Interface(int n) const
22113090Sgabeblack@google.com    {
22213290Sgabeblack@google.com        if (n < 0 || n >= size()) {
22313324Sgabeblack@google.com            report_error(SC_ID_GET_IF_, "index out of range");
22413290Sgabeblack@google.com            return NULL;
22513290Sgabeblack@google.com        }
22613090Sgabeblack@google.com        return _interfaces[n];
22713090Sgabeblack@google.com    }
22813053Sgabeblack@google.com    void
22913290Sgabeblack@google.com    _gem5AddInterface(sc_interface *iface)
23013053Sgabeblack@google.com    {
23113290Sgabeblack@google.com        IF *interface = dynamic_cast<IF *>(iface);
23213053Sgabeblack@google.com        sc_assert(interface);
23313657Sgabeblack@google.com        for (unsigned i = 0; i < _interfaces.size(); i++) {
23413290Sgabeblack@google.com            if (interface == _interfaces[i]) {
23513324Sgabeblack@google.com                report_error(SC_ID_BIND_IF_TO_PORT_,
23613290Sgabeblack@google.com                        "interface already bound to port");
23713290Sgabeblack@google.com            }
23813290Sgabeblack@google.com        }
23913053Sgabeblack@google.com        _interfaces.push_back(interface);
24013053Sgabeblack@google.com    }
24113053Sgabeblack@google.com
24213273Sgabeblack@google.com    const char *_ifTypeName() const { return typeid(IF).name(); }
24313273Sgabeblack@google.com
24412837Sgabeblack@google.com    // Disabled
24512837Sgabeblack@google.com    sc_port_b() {}
24612837Sgabeblack@google.com    sc_port_b(const sc_port_b<IF> &) {}
24712837Sgabeblack@google.com    sc_port_b<IF> &operator = (const sc_port_b<IF> &) { return *this; }
24812837Sgabeblack@google.com};
24912837Sgabeblack@google.com
25012837Sgabeblack@google.comtemplate <class IF, int N=1, sc_port_policy P=SC_ONE_OR_MORE_BOUND>
25112837Sgabeblack@google.comclass sc_port : public sc_port_b<IF>
25212837Sgabeblack@google.com{
25312837Sgabeblack@google.com  public:
25412868Sgabeblack@google.com    sc_port() : sc_port_b<IF>(N, P) {}
25512842Sgabeblack@google.com    explicit sc_port(const char *name) : sc_port_b<IF>(name, N, P) {}
25612842Sgabeblack@google.com    virtual ~sc_port() {}
25712837Sgabeblack@google.com
25812868Sgabeblack@google.com    // Deprecated binding constructors.
25912868Sgabeblack@google.com    explicit sc_port(const IF &interface) : sc_port_b<IF>(N, P)
26012868Sgabeblack@google.com    {
26113332Sgabeblack@google.com        this->warn_port_constructor();
26212868Sgabeblack@google.com        sc_port_b<IF>::bind(const_cast<IF &>(interface));
26312868Sgabeblack@google.com    }
26412868Sgabeblack@google.com    sc_port(const char *name, const IF &interface) : sc_port_b<IF>(name, N, P)
26512868Sgabeblack@google.com    {
26613332Sgabeblack@google.com        this->warn_port_constructor();
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    {
27113332Sgabeblack@google.com        this->warn_port_constructor();
27212868Sgabeblack@google.com        sc_port_b<IF>::bind(parent);
27312868Sgabeblack@google.com    }
27412868Sgabeblack@google.com    sc_port(const char *name, sc_port_b<IF> &parent) :
27512868Sgabeblack@google.com        sc_port_b<IF>(name, N, P)
27612868Sgabeblack@google.com    {
27713332Sgabeblack@google.com        this->warn_port_constructor();
27812868Sgabeblack@google.com        sc_port_b<IF>::bind(parent);
27912868Sgabeblack@google.com    }
28012868Sgabeblack@google.com    explicit sc_port(sc_port<IF, N, P> &parent) : sc_port_b<IF>(N, P)
28112868Sgabeblack@google.com    {
28213332Sgabeblack@google.com        this->warn_port_constructor();
28312868Sgabeblack@google.com        sc_port_b<IF>::bind(parent);
28412868Sgabeblack@google.com    }
28512868Sgabeblack@google.com    sc_port(const char *name, sc_port<IF, N, P> &parent) :
28612868Sgabeblack@google.com        sc_port_b<IF>(name, N, P)
28712868Sgabeblack@google.com    {
28813332Sgabeblack@google.com        this->warn_port_constructor();
28912868Sgabeblack@google.com        sc_port_b<IF>::bind(parent);
29012868Sgabeblack@google.com    }
29112868Sgabeblack@google.com
29212842Sgabeblack@google.com    virtual const char *kind() const { return "sc_port"; }
29312837Sgabeblack@google.com
29412837Sgabeblack@google.com  private:
29512837Sgabeblack@google.com    // Disabled
29612837Sgabeblack@google.com    sc_port(const sc_port<IF, N, P> &) {}
29712837Sgabeblack@google.com    sc_port<IF, N, P> &operator = (const sc_port<IF, N, P> &) { return *this; }
29813293Sgabeblack@google.com
29913293Sgabeblack@google.com    virtual sc_port_policy _portPolicy() const { return P; }
30012837Sgabeblack@google.com};
30112837Sgabeblack@google.com
30212837Sgabeblack@google.com} // namespace sc_core
30312837Sgabeblack@google.com
30412837Sgabeblack@google.com#endif  //__SYSTEMC_EXT_CORE_SC_PORT_HH__
305