1/*
2 * Copyright 2018 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30#ifndef __SYSTEMC_CORE_PORT_HH__
31#define __SYSTEMC_CORE_PORT_HH__
32
33#include <list>
34#include <typeinfo>
35#include <vector>
36
37#include "base/cprintf.hh"
38#include "systemc/ext/core/sc_interface.hh"
39#include "systemc/ext/core/sc_port.hh"
40
41namespace sc_gem5
42{
43
44class StaticSensitivityPort;
45class StaticSensitivityFinder;
46class Reset;
47
48class Port;
49
50extern std::list<Port *> allPorts;
51
52class Port
53{
54  private:
55    ::sc_core::sc_port_base *portBase;
56
57    bool finalized;
58    int _maxSize;
59    int _size;
60
61    bool regPortNeeded;
62
63    void finalizePort(StaticSensitivityPort *port);
64    void finalizeFinder(StaticSensitivityFinder *finder);
65    void finalizeReset(Reset *reset);
66
67    void
68    addInterface(::sc_core::sc_interface *iface)
69    {
70        portBase->_gem5AddInterface(iface);
71        _size++;
72    }
73
74    void
75    addInterfaces(::sc_core::sc_port_base *pb)
76    {
77        // Only the ports farthest from the interfaces call register_port.
78        pb->_gem5Port->regPortNeeded = false;
79        for (int i = 0; i < pb->size(); i++)
80            addInterface(pb->_gem5Interface(i));
81    }
82
83    ::sc_core::sc_interface *
84    getInterface(int i)
85    {
86        return portBase->_gem5Interface(i);
87    }
88
89    struct Binding
90    {
91        explicit Binding(::sc_core::sc_interface *interface) :
92            interface(interface), port(nullptr)
93        {}
94
95        explicit Binding(::sc_core::sc_port_base *port) :
96            interface(nullptr), port(port)
97        {}
98
99        ::sc_core::sc_interface *interface;
100        ::sc_core::sc_port_base *port;
101    };
102
103    struct Sensitivity
104    {
105        Sensitivity(StaticSensitivityPort *port) :
106            port(port), finder(nullptr)
107        {}
108
109        Sensitivity(StaticSensitivityFinder *finder) :
110            port(nullptr), finder(finder)
111        {}
112
113        StaticSensitivityPort *port;
114        StaticSensitivityFinder *finder;
115    };
116
117    std::vector<Binding *> bindings;
118    std::vector<Sensitivity *> sensitivities;
119    std::vector<Reset *> resets;
120
121  public:
122    static Port *
123    fromPort(const ::sc_core::sc_port_base *pb)
124    {
125        return pb->_gem5Port;
126    }
127
128    ::sc_core::sc_port_base *sc_port_base() { return portBase; }
129
130    Port(::sc_core::sc_port_base *port_base, int max) :
131        portBase(port_base), finalized(false), _maxSize(max), _size(0),
132        regPortNeeded(true)
133    {
134        allPorts.push_front(this);
135    }
136
137    ~Port() { allPorts.remove(this); }
138
139    void
140    bind(::sc_core::sc_interface *interface)
141    {
142        if (bindings.empty())
143            addInterface(interface);
144        else
145            bindings.push_back(new Binding(interface));
146    }
147
148    void
149    bind(::sc_core::sc_port_base *port)
150    {
151        bindings.push_back(new Binding(port));
152    }
153
154    void sensitive(StaticSensitivityPort *port);
155    void sensitive(StaticSensitivityFinder *finder);
156    void addReset(Reset *reset);
157
158    void finalize();
159    void regPort();
160
161    int size() { return _size; }
162    int maxSize() { return _maxSize ? _maxSize : _size; }
163};
164
165} // namespace sc_gem5
166
167#endif // __SYSTEMC_CORE_PORT_HH__
168