sensitivity.cc revision 13317
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#include "systemc/core/sensitivity.hh"
31
32#include "base/logging.hh"
33#include "systemc/core/event.hh"
34#include "systemc/core/port.hh"
35#include "systemc/core/process.hh"
36#include "systemc/core/scheduler.hh"
37#include "systemc/ext/channel/sc_in.hh"
38#include "systemc/ext/channel/sc_inout.hh"
39#include "systemc/ext/channel/sc_out.hh"
40#include "systemc/ext/core/messages.hh"
41#include "systemc/ext/core/sc_export.hh"
42#include "systemc/ext/core/sc_interface.hh"
43#include "systemc/ext/core/sc_port.hh"
44
45namespace sc_gem5
46{
47
48/*
49 * Common sensitivity interface.
50 */
51
52void
53Sensitivity::satisfy()
54{
55    process->satisfySensitivity(this);
56}
57
58bool
59Sensitivity::notifyWork(Event *e)
60{
61    satisfy();
62    return true;
63}
64
65bool
66Sensitivity::notify(Event *e)
67{
68    if (scheduler.current() == process) {
69        static bool warned = false;
70        if (!warned) {
71            SC_REPORT_WARNING(sc_core::SC_ID_IMMEDIATE_SELF_NOTIFICATION_,
72                    process->name());
73            warned = true;
74        }
75        return false;
76    }
77
78    if (process->disabled())
79        return false;
80
81    return notifyWork(e);
82}
83
84bool
85Sensitivity::ofMethod()
86{
87    return process->procKind() == sc_core::SC_METHOD_PROC_;
88}
89
90
91/*
92 * Dynamic vs. static sensitivity.
93 */
94
95void
96DynamicSensitivity::addToEvent(const ::sc_core::sc_event *e)
97{
98    Event::getFromScEvent(e)->addSensitivity(this);
99}
100
101void
102DynamicSensitivity::delFromEvent(const ::sc_core::sc_event *e)
103{
104    Event::getFromScEvent(e)->delSensitivity(this);
105}
106
107void
108StaticSensitivity::addToEvent(const ::sc_core::sc_event *e)
109{
110    Event::getFromScEvent(e)->addSensitivity(this);
111}
112
113void
114StaticSensitivity::delFromEvent(const ::sc_core::sc_event *e)
115{
116    Event::getFromScEvent(e)->delSensitivity(this);
117}
118
119
120/*
121 * Static sensitivities.
122 */
123
124void
125newStaticSensitivityEvent(Process *p, const sc_core::sc_event *e)
126{
127    auto s = new StaticSensitivityEvent(p, e);
128    s->addToEvent(s->event);
129    p->addStatic(s);
130}
131
132void
133newStaticSensitivityInterface(Process *p, const sc_core::sc_interface *i)
134{
135    auto s = new StaticSensitivityInterface(p, i);
136    s->addToEvent(s->event);
137    p->addStatic(s);
138}
139
140void
141newStaticSensitivityPort(Process *p, const sc_core::sc_port_base *pb)
142{
143    auto s = new StaticSensitivityPort(p);
144    Port *port = Port::fromPort(pb);
145    port->sensitive(s);
146    p->addStatic(s);
147}
148
149void
150newStaticSensitivityExport(Process *p, const sc_core::sc_export_base *exp)
151{
152    auto s = new StaticSensitivityExport(p, exp);
153    s->addToEvent(s->event);
154    p->addStatic(s);
155}
156
157void
158newStaticSensitivityFinder(Process *p, const sc_core::sc_event_finder *f)
159{
160    auto s = new StaticSensitivityFinder(p, f);
161    Port *port = Port::fromPort(f->port());
162    port->sensitive(s);
163    p->addStatic(s);
164}
165
166
167StaticSensitivityInterface::StaticSensitivityInterface(
168        Process *p, const sc_core::sc_interface *i) :
169    Sensitivity(p), StaticSensitivity(p),
170    SensitivityEvent(p, &i->default_event())
171{}
172
173StaticSensitivityExport::StaticSensitivityExport(
174        Process *p, const sc_core::sc_export_base *exp) :
175    Sensitivity(p), StaticSensitivity(p),
176    SensitivityEvent(p, &exp->get_interface()->default_event())
177{}
178
179const ::sc_core::sc_event &
180StaticSensitivityFinder::find(::sc_core::sc_interface *i)
181{
182    return finder->find_event(i);
183}
184
185
186/*
187 * Dynamic sensitivities.
188 */
189
190void
191newDynamicSensitivityEvent(Process *p, const sc_core::sc_event *e)
192{
193    auto s = new DynamicSensitivityEvent(p, e);
194    s->addToEvent(s->event);
195    p->setDynamic(s);
196}
197
198void
199newDynamicSensitivityEventOrList(
200        Process *p, const sc_core::sc_event_or_list *eol)
201{
202    auto s = new DynamicSensitivityEventOrList(p, eol);
203    for (auto event: s->events)
204        s->addToEvent(event);
205    p->setDynamic(s);
206}
207
208void newDynamicSensitivityEventAndList(
209        Process *p, const sc_core::sc_event_and_list *eal)
210{
211    auto s = new DynamicSensitivityEventAndList(p, eal);
212    for (auto event: s->events)
213        s->addToEvent(event);
214    p->setDynamic(s);
215}
216
217
218DynamicSensitivityEventOrList::DynamicSensitivityEventOrList(
219        Process *p, const sc_core::sc_event_or_list *eol) :
220    Sensitivity(p), DynamicSensitivity(p), SensitivityEvents(p, eol->events)
221{}
222
223bool
224DynamicSensitivityEventOrList::notifyWork(Event *e)
225{
226    events.erase(e->sc_event());
227
228    // All the other events need this deleted from their lists since this
229    // sensitivity has been satisfied without them triggering.
230    for (auto le: events)
231        delFromEvent(le);
232
233    satisfy();
234    return true;
235}
236
237DynamicSensitivityEventAndList::DynamicSensitivityEventAndList(
238        Process *p, const sc_core::sc_event_and_list *eal) :
239    Sensitivity(p), DynamicSensitivity(p), SensitivityEvents(p, eal->events)
240{}
241
242bool
243DynamicSensitivityEventAndList::notifyWork(Event *e)
244{
245    events.erase(e->sc_event());
246
247    // This sensitivity is satisfied if all events have triggered.
248    if (events.empty())
249        satisfy();
250
251    return true;
252}
253
254} // namespace sc_gem5
255