sc_port.hh revision 13059
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
3312957Sgabeblack@google.com#include <vector>
3412957Sgabeblack@google.com
3513053Sgabeblack@google.com#include "../utils/sc_report_handler.hh"
3612842Sgabeblack@google.com#include "sc_module.hh" // for sc_gen_unique_name
3712837Sgabeblack@google.com#include "sc_object.hh"
3812837Sgabeblack@google.com
3912957Sgabeblack@google.comnamespace sc_gem5
4012957Sgabeblack@google.com{
4112957Sgabeblack@google.com
4212957Sgabeblack@google.comclass BindInfo;
4312957Sgabeblack@google.comclass PendingSensitivityPort;
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;
5112837Sgabeblack@google.com
5212837Sgabeblack@google.comenum sc_port_policy
5312837Sgabeblack@google.com{
5412837Sgabeblack@google.com    SC_ONE_OR_MORE_BOUND, // Default
5512837Sgabeblack@google.com    SC_ZERO_OR_MORE_BOUND,
5612837Sgabeblack@google.com    SC_ALL_BOUND
5712837Sgabeblack@google.com};
5812837Sgabeblack@google.com
5912837Sgabeblack@google.comclass sc_port_base : public sc_object
6012837Sgabeblack@google.com{
6112837Sgabeblack@google.com  public:
6212957Sgabeblack@google.com    sc_port_base(const char *name, int n, sc_port_policy p);
6312842Sgabeblack@google.com
6412842Sgabeblack@google.com    void warn_unimpl(const char *func) const;
6512938Sgabeblack@google.com
6612957Sgabeblack@google.com    int maxSize() const;
6712957Sgabeblack@google.com    int size() const;
6812957Sgabeblack@google.com
6912938Sgabeblack@google.com  protected:
7012938Sgabeblack@google.com    // Implementation defined, but depended on by the tests.
7112938Sgabeblack@google.com    void bind(sc_interface &);
7212938Sgabeblack@google.com    void bind(sc_port_base &);
7312944Sgabeblack@google.com
7412944Sgabeblack@google.com    // Implementation defined, but depended on by the tests.
7512944Sgabeblack@google.com    virtual int vbind(sc_interface &) = 0;
7612944Sgabeblack@google.com    virtual int vbind(sc_port_base &) = 0;
7712957Sgabeblack@google.com
7813059Sgabeblack@google.com    virtual void before_end_of_elaboration() = 0;
7913059Sgabeblack@google.com    virtual void end_of_elaboration() = 0;
8013059Sgabeblack@google.com    virtual void start_of_simulation() = 0;
8113059Sgabeblack@google.com    virtual void end_of_simulation() = 0;
8213059Sgabeblack@google.com
8312957Sgabeblack@google.com  private:
8412957Sgabeblack@google.com    friend class ::sc_gem5::PendingSensitivityPort;
8513053Sgabeblack@google.com    friend class ::sc_gem5::Kernel;
8613053Sgabeblack@google.com
8713053Sgabeblack@google.com    void _gem5Finalize();
8813053Sgabeblack@google.com
8913053Sgabeblack@google.com    virtual sc_interface *_gem5Interface(int n) const = 0;
9013053Sgabeblack@google.com    virtual void _gem5AddInterface(sc_interface *i) = 0;
9112957Sgabeblack@google.com
9212957Sgabeblack@google.com    std::vector<::sc_gem5::BindInfo *> _gem5BindInfo;
9312957Sgabeblack@google.com    int _maxSize;
9413053Sgabeblack@google.com    int _size;
9513053Sgabeblack@google.com    bool finalized;
9612837Sgabeblack@google.com};
9712837Sgabeblack@google.com
9812837Sgabeblack@google.comtemplate <class IF>
9912837Sgabeblack@google.comclass sc_port_b : public sc_port_base
10012837Sgabeblack@google.com{
10112837Sgabeblack@google.com  public:
10213053Sgabeblack@google.com    void operator () (IF &i) { bind(i); }
10313053Sgabeblack@google.com    void operator () (sc_port_b<IF> &p) { bind(p); }
10412837Sgabeblack@google.com
10513053Sgabeblack@google.com    virtual void bind(IF &i) { sc_port_base::bind(i); }
10613053Sgabeblack@google.com    virtual void bind(sc_port_b<IF> &p) { sc_port_base::bind(p); }
10712837Sgabeblack@google.com
10813053Sgabeblack@google.com    IF *operator -> () { return _interfaces.at(0); }
10913053Sgabeblack@google.com    const IF *operator -> () const { return _interfaces.at(0); }
11012837Sgabeblack@google.com
11113053Sgabeblack@google.com    IF *operator [] (int n) { return _interfaces.at(n); }
11213053Sgabeblack@google.com    const IF *operator [] (int n) const { return _interfaces.at(n); }
11312837Sgabeblack@google.com
11413053Sgabeblack@google.com    sc_interface *get_interface() { return _interfaces.at(0); }
11513053Sgabeblack@google.com    const sc_interface *get_interface() const { return _interfaces.at(0); }
11612837Sgabeblack@google.com
11712837Sgabeblack@google.com  protected:
11813059Sgabeblack@google.com    void before_end_of_elaboration() {}
11913059Sgabeblack@google.com    void end_of_elaboration() {}
12013059Sgabeblack@google.com    void start_of_simulation() {}
12113059Sgabeblack@google.com    void end_of_simulation() {}
12212837Sgabeblack@google.com
12312842Sgabeblack@google.com    explicit sc_port_b(int n, sc_port_policy p) :
12413036Sgabeblack@google.com            sc_port_base(sc_gen_unique_name("port"), n, p)
12512842Sgabeblack@google.com    {}
12612842Sgabeblack@google.com    sc_port_b(const char *name, int n, sc_port_policy p) :
12712842Sgabeblack@google.com            sc_port_base(name, n, p)
12812842Sgabeblack@google.com    {}
12912842Sgabeblack@google.com    virtual ~sc_port_b() {}
13012837Sgabeblack@google.com
13112944Sgabeblack@google.com    // Implementation defined, but depended on by the tests.
13212944Sgabeblack@google.com    int
13313053Sgabeblack@google.com    vbind(sc_interface &i)
13412944Sgabeblack@google.com    {
13513053Sgabeblack@google.com        IF *interface = dynamic_cast<IF *>(&i);
13613053Sgabeblack@google.com        if (!interface)
13713053Sgabeblack@google.com            return 2;
13813053Sgabeblack@google.com        sc_port_base::bind(*interface);
13912944Sgabeblack@google.com        return 0;
14012944Sgabeblack@google.com    }
14112944Sgabeblack@google.com    int
14213053Sgabeblack@google.com    vbind(sc_port_base &pb)
14312944Sgabeblack@google.com    {
14413053Sgabeblack@google.com        sc_port_b<IF> *p = dynamic_cast<sc_port_b<IF> *>(&pb);
14513053Sgabeblack@google.com        if (!p)
14613053Sgabeblack@google.com            return 2;
14713053Sgabeblack@google.com        sc_port_base::bind(*p);
14812944Sgabeblack@google.com        return 0;
14912944Sgabeblack@google.com    }
15012944Sgabeblack@google.com
15112837Sgabeblack@google.com  private:
15213053Sgabeblack@google.com    std::vector<IF *> _interfaces;
15313053Sgabeblack@google.com
15413053Sgabeblack@google.com    sc_interface *_gem5Interface(int n) const { return _interfaces.at(n); }
15513053Sgabeblack@google.com    void
15613053Sgabeblack@google.com    _gem5AddInterface(sc_interface *i)
15713053Sgabeblack@google.com    {
15813053Sgabeblack@google.com        IF *interface = dynamic_cast<IF *>(i);
15913053Sgabeblack@google.com        sc_assert(interface);
16013053Sgabeblack@google.com        _interfaces.push_back(interface);
16113053Sgabeblack@google.com    }
16213053Sgabeblack@google.com
16312837Sgabeblack@google.com    // Disabled
16412837Sgabeblack@google.com    sc_port_b() {}
16512837Sgabeblack@google.com    sc_port_b(const sc_port_b<IF> &) {}
16612837Sgabeblack@google.com    sc_port_b<IF> &operator = (const sc_port_b<IF> &) { return *this; }
16712837Sgabeblack@google.com};
16812837Sgabeblack@google.com
16912837Sgabeblack@google.comtemplate <class IF, int N=1, sc_port_policy P=SC_ONE_OR_MORE_BOUND>
17012837Sgabeblack@google.comclass sc_port : public sc_port_b<IF>
17112837Sgabeblack@google.com{
17212837Sgabeblack@google.com  public:
17312868Sgabeblack@google.com    sc_port() : sc_port_b<IF>(N, P) {}
17412842Sgabeblack@google.com    explicit sc_port(const char *name) : sc_port_b<IF>(name, N, P) {}
17512842Sgabeblack@google.com    virtual ~sc_port() {}
17612837Sgabeblack@google.com
17712868Sgabeblack@google.com    // Deprecated binding constructors.
17812868Sgabeblack@google.com    explicit sc_port(const IF &interface) : sc_port_b<IF>(N, P)
17912868Sgabeblack@google.com    {
18012868Sgabeblack@google.com        this->warn_unimpl(__PRETTY_FUNCTION__);
18112868Sgabeblack@google.com        // Should warn that these are deprecated. See Accellera sc_port.h.
18212868Sgabeblack@google.com        sc_port_b<IF>::bind(const_cast<IF &>(interface));
18312868Sgabeblack@google.com    }
18412868Sgabeblack@google.com    sc_port(const char *name, const IF &interface) : sc_port_b<IF>(name, N, P)
18512868Sgabeblack@google.com    {
18612868Sgabeblack@google.com        this->warn_unimpl(__PRETTY_FUNCTION__);
18712868Sgabeblack@google.com        // Should warn that these are deprecated. See Accellera sc_port.h.
18812868Sgabeblack@google.com        sc_port_b<IF>::bind(const_cast<IF &>(interface));
18912868Sgabeblack@google.com    }
19012868Sgabeblack@google.com    explicit sc_port(sc_port_b<IF> &parent) : sc_port_b<IF>(N, P)
19112868Sgabeblack@google.com    {
19212868Sgabeblack@google.com        this->warn_unimpl(__PRETTY_FUNCTION__);
19312868Sgabeblack@google.com        // Should warn that these are deprecated. See Accellera sc_port.h.
19412868Sgabeblack@google.com        sc_port_b<IF>::bind(parent);
19512868Sgabeblack@google.com    }
19612868Sgabeblack@google.com    sc_port(const char *name, sc_port_b<IF> &parent) :
19712868Sgabeblack@google.com        sc_port_b<IF>(name, N, P)
19812868Sgabeblack@google.com    {
19912868Sgabeblack@google.com        this->warn_unimpl(__PRETTY_FUNCTION__);
20012868Sgabeblack@google.com        // Should warn that these are deprecated. See Accellera sc_port.h.
20112868Sgabeblack@google.com        sc_port_b<IF>::bind(parent);
20212868Sgabeblack@google.com    }
20312868Sgabeblack@google.com    explicit sc_port(sc_port<IF, N, P> &parent) : sc_port_b<IF>(N, P)
20412868Sgabeblack@google.com    {
20512868Sgabeblack@google.com        this->warn_unimpl(__PRETTY_FUNCTION__);
20612868Sgabeblack@google.com        // Should warn that these are deprecated. See Accellera sc_port.h.
20712868Sgabeblack@google.com        sc_port_b<IF>::bind(parent);
20812868Sgabeblack@google.com    }
20912868Sgabeblack@google.com    sc_port(const char *name, sc_port<IF, N, P> &parent) :
21012868Sgabeblack@google.com        sc_port_b<IF>(name, N, P)
21112868Sgabeblack@google.com    {
21212868Sgabeblack@google.com        this->warn_unimpl(__PRETTY_FUNCTION__);
21312868Sgabeblack@google.com        // Should warn that these are deprecated. See Accellera sc_port.h.
21412868Sgabeblack@google.com        sc_port_b<IF>::bind(parent);
21512868Sgabeblack@google.com    }
21612868Sgabeblack@google.com
21712842Sgabeblack@google.com    virtual const char *kind() const { return "sc_port"; }
21812837Sgabeblack@google.com
21912837Sgabeblack@google.com  private:
22012837Sgabeblack@google.com    // Disabled
22112837Sgabeblack@google.com    sc_port(const sc_port<IF, N, P> &) {}
22212837Sgabeblack@google.com    sc_port<IF, N, P> &operator = (const sc_port<IF, N, P> &) { return *this; }
22312837Sgabeblack@google.com};
22412837Sgabeblack@google.com
22512837Sgabeblack@google.com} // namespace sc_core
22612837Sgabeblack@google.com
22712837Sgabeblack@google.com#endif  //__SYSTEMC_EXT_CORE_SC_PORT_HH__
228