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_PROCESS_HH__
31#define __SYSTEMC_CORE_PROCESS_HH__
32
33#include <functional>
34#include <memory>
35#include <vector>
36
37#include "base/fiber.hh"
38#include "systemc/core/list.hh"
39#include "systemc/core/module.hh"
40#include "systemc/core/object.hh"
41#include "systemc/core/sched_event.hh"
42#include "systemc/core/sensitivity.hh"
43#include "systemc/ext/channel/sc_signal_in_if.hh"
44#include "systemc/ext/core/sc_event.hh"
45#include "systemc/ext/core/sc_module.hh"
46#include "systemc/ext/core/sc_process_handle.hh"
47
48namespace sc_core
49{
50
51class sc_join;
52
53} // namespace sc_core
54
55namespace sc_gem5
56{
57
58class ScHalt
59{};
60
61class Process;
62class Reset;
63
64class Process : public ::sc_core::sc_process_b, public ListNode
65{
66  public:
67    virtual ::sc_core::sc_curr_proc_kind procKind() const = 0;
68    bool needsStart() const { return _needsStart; }
69    void needsStart(bool ns) { _needsStart = ns; }
70    bool dynamic() const { return _dynamic; }
71    bool isUnwinding() const { return _isUnwinding; }
72    void isUnwinding(bool v) { _isUnwinding = v; }
73    bool terminated() const { return _terminated; }
74
75    bool scheduled() const { return _scheduled; }
76    void scheduled(bool new_val) { _scheduled = new_val; }
77
78    void forEachKid(const std::function<void(Process *)> &work);
79
80    bool suspended() const { return _suspended; }
81    bool disabled() const { return _disabled; }
82
83    void suspend(bool inc_kids);
84    void resume(bool inc_kids);
85    void disable(bool inc_kids);
86    void enable(bool inc_kids);
87
88    void kill(bool inc_kids);
89    void reset(bool inc_kids);
90    void throw_it(ExceptionWrapperBase &exc, bool inc_kids);
91
92    void injectException(ExceptionWrapperBase &exc);
93    ExceptionWrapperBase *excWrapper;
94
95    void syncResetOn(bool inc_kids);
96    void syncResetOff(bool inc_kids);
97
98    void signalReset(bool set, bool sync);
99
100    void incref() { refCount++; }
101    void decref() { refCount--; }
102
103    ::sc_core::sc_event &resetEvent() { return _resetEvent; }
104    ::sc_core::sc_event &terminatedEvent() { return _terminatedEvent; }
105
106    void setStackSize(size_t size) { stackSize = size; }
107
108    void run();
109
110    void addStatic(StaticSensitivity *);
111    void setDynamic(DynamicSensitivity *);
112    void clearDynamic() { setDynamic(nullptr); }
113    void addReset(Reset *);
114
115    ScEvent timeoutEvent;
116    void setTimeout(::sc_core::sc_time t);
117    void cancelTimeout();
118
119    void satisfySensitivity(Sensitivity *);
120
121    void ready();
122
123    virtual Fiber *fiber() { return Fiber::primaryFiber(); }
124
125    static Process *newest() { return _newest; }
126
127    void lastReport(::sc_core::sc_report *report);
128    ::sc_core::sc_report *lastReport() const;
129
130    bool hasStaticSensitivities() { return !staticSensitivities.empty(); }
131    bool internal() { return _internal; }
132    bool timedOut() { return _timedOut; }
133    bool inReset() { return _syncReset || syncResetCount || asyncResetCount; }
134
135    bool dontInitialize() { return _dontInitialize; }
136    void dontInitialize(bool di) { _dontInitialize = di; }
137
138    void joinWait(::sc_core::sc_join *join) { joinWaiters.push_back(join); }
139
140    void waitCount(int count) { _waitCount = count; }
141
142    const char *uniqueName(const char *seed) { return nameGen.gen(seed); }
143
144  protected:
145    void timeout();
146
147    Process(const char *name, ProcessFuncWrapper *func, bool internal=false);
148
149    static Process *_newest;
150
151    virtual ~Process()
152    {
153        popListNode();
154        delete func;
155        for (auto s: staticSensitivities) {
156            s->clear();
157            delete s;
158        }
159        clearDynamic();
160    }
161
162    InternalScEvent _resetEvent;
163    InternalScEvent _terminatedEvent;
164
165    ProcessFuncWrapper *func;
166
167    bool _internal;
168
169    // Needed to support the deprecated "timed_out" function.
170    bool _timedOut;
171
172    bool _dontInitialize;
173
174    bool _needsStart;
175    bool _dynamic;
176    bool _isUnwinding;
177    bool _terminated;
178    bool _scheduled;
179
180    void terminate();
181
182    bool _suspended;
183    bool _suspendedReady;
184    bool _disabled;
185
186    bool _syncReset;
187
188    int syncResetCount;
189    int asyncResetCount;
190
191    int _waitCount;
192
193    int refCount;
194
195    size_t stackSize;
196
197    StaticSensitivities staticSensitivities;
198    DynamicSensitivity *dynamicSensitivity;
199    std::vector<Reset *> resets;
200
201    std::unique_ptr<::sc_core::sc_report> _lastReport;
202
203    std::vector<::sc_core::sc_join *> joinWaiters;
204
205    UniqueNameGen nameGen;
206};
207
208class Reset
209{
210  public:
211    Reset(Process *p, bool s, bool v) :
212        _process(p), _signal(nullptr), _sync(s), _value(v)
213    {}
214
215    bool
216    install(const sc_core::sc_signal_in_if<bool> *s)
217    {
218        _signal = s;
219
220        if (_signal->_addReset(this)) {
221            _process->addReset(this);
222            if (_signal->read() == _value)
223                update();
224            return true;
225        }
226        return false;
227    }
228    void update() { _process->signalReset(_signal->read() == _value, _sync); }
229
230    Process *process() { return _process; }
231    const sc_core::sc_signal_in_if<bool> *signal() { return _signal; }
232    bool sync() { return _sync; }
233    bool value() { return _value; }
234
235  private:
236    Process *_process;
237    const sc_core::sc_signal_in_if<bool> *_signal;
238    bool _sync;
239    bool _value;
240};
241
242void newReset(const sc_core::sc_port_base *pb, Process *p, bool s, bool v);
243void newReset(const sc_core::sc_signal_in_if<bool> *sig, Process *p,
244        bool s, bool v);
245
246} // namespace sc_gem5
247
248#endif  //__SYSTEMC_CORE_PROCESS_HH__
249