1/*
2 * Copyright 2019 Google LLC.
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: Chun-Chen TK Hsu
28 */
29
30#ifndef __SYSTEMC_SC_PORT_WRAPPER_HH__
31#define __SYSTEMC_SC_PORT_WRAPPER_HH__
32
33#include <string>
34#include <type_traits>
35
36#include "base/logging.hh"
37#include "sim/port.hh"
38#include "systemc/ext/core/sc_export.hh"
39#include "systemc/ext/core/sc_interface.hh"
40#include "systemc/ext/core/sc_port.hh"
41
42namespace sc_gem5
43{
44
45// Forward declaration
46template <typename IF>
47class ScPortWrapper;
48template <typename IF>
49class ScInterfaceWrapper;
50template <typename IF>
51class ScExportWrapper;
52
53template <typename IF>
54class ScPortWrapper : public ::Port
55{
56  public:
57    using ScPort = sc_core::sc_port_b<IF>;
58
59    ScPortWrapper(ScPort& p, const std::string& name, PortID id)
60        : Port(name, id), port_(p)
61    {}
62
63    ScPort&
64    port()
65    {
66        return port_;
67    }
68
69    void
70    unbind() override
71    {
72        panic("sc_port can't be unbound.");
73    }
74
75    void
76    bind(::Port& peer) override
77    {
78        // Try ScPortWrapper or ScInterfaceWrapper
79        if (auto* beer = dynamic_cast<ScPortWrapper<IF>*>(&peer)) {
80            port_.bind(beer->port());
81        } else if (auto* iface =
82                       dynamic_cast<ScInterfaceWrapper<IF>*>(&peer)) {
83            port_.bind(iface->interface());
84        } else {
85            fatal("Attempt to bind sc_port %s to incompatible port %s.",
86                  name(), peer.name());
87        }
88        Port::bind(peer);
89    }
90
91  private:
92    ScPort& port_;
93};
94
95template <typename IF>
96class ScInterfaceWrapper : public ::Port
97{
98  public:
99    ScInterfaceWrapper(IF& i, const std::string name, PortID id)
100        : Port(name, id), iface_(i)
101    {}
102
103    IF&
104    interface()
105    {
106        return iface_;
107    }
108
109    void
110    unbind() override
111    {
112        panic("sc_interface can't be unbound.");
113    }
114
115    void
116    bind(::Port& peer) override
117    {
118        // fatal error if peer is neither ScPortWrapper nor ScExportWrapper
119        fatal_if(!dynamic_cast<ScPortWrapper<IF>*>(&peer) &&
120                     !dynamic_cast<ScExportWrapper<IF>*>(&peer),
121                 "Attempt to bind sc_interface %s to incompatible port %s.",
122                 name(), peer.name());
123
124        // Don't bind to peer otherwise we may have error messages saying that
125        // this interface has already be bound since the peer may already did
126        // that. Just let sc_port or sc_export do the binding
127        Port::bind(peer);
128    }
129
130  private:
131    IF& iface_;
132};
133
134template <typename IF>
135class ScExportWrapper : public ::Port
136{
137  public:
138    using ScExport = sc_core::sc_export<IF>;
139
140    ScExportWrapper(ScExport& p, const std::string& name, PortID id)
141        : Port(name, id), port_(p)
142    {}
143
144    ScExport&
145    port()
146    {
147        return port_;
148    }
149
150    void
151    unbind() override
152    {
153        panic("sc_export cannot be unbound.");
154    }
155
156    void
157    bind(::Port& peer) override
158    {
159        auto* iface = dynamic_cast<ScInterfaceWrapper<IF>*>(&peer);
160        fatal_if(!iface,
161                 "Attempt to bind sc_export %s to incompatible port %s.",
162                 name(), peer.name());
163
164        port_.bind(iface->interface());
165        Port::bind(peer);
166    }
167
168  private:
169    ScExport& port_;
170};
171
172}  // namespace sc_gem5
173
174#endif  // __SYSTEMC_SC_PORT_WRAPPER_HH__
175