112952Sgabeblack@google.com/*
212952Sgabeblack@google.com * Copyright 2018 Google, Inc.
312952Sgabeblack@google.com *
412952Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512952Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612952Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712952Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812952Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912952Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012952Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112952Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212952Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312952Sgabeblack@google.com * this software without specific prior written permission.
1412952Sgabeblack@google.com *
1512952Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612952Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712952Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812952Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912952Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012952Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112952Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212952Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312952Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412952Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512952Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612952Sgabeblack@google.com *
2712952Sgabeblack@google.com * Authors: Gabe Black
2812952Sgabeblack@google.com */
2912952Sgabeblack@google.com
3012952Sgabeblack@google.com#ifndef __SYSTEMC_CORE_PROCESS_HH__
3112952Sgabeblack@google.com#define __SYSTEMC_CORE_PROCESS_HH__
3212952Sgabeblack@google.com
3312952Sgabeblack@google.com#include <functional>
3412997Sgabeblack@google.com#include <memory>
3512957Sgabeblack@google.com#include <vector>
3612952Sgabeblack@google.com
3712952Sgabeblack@google.com#include "base/fiber.hh"
3812953Sgabeblack@google.com#include "systemc/core/list.hh"
3913285Sgabeblack@google.com#include "systemc/core/module.hh"
4012952Sgabeblack@google.com#include "systemc/core/object.hh"
4113063Sgabeblack@google.com#include "systemc/core/sched_event.hh"
4213206Sgabeblack@google.com#include "systemc/core/sensitivity.hh"
4313288Sgabeblack@google.com#include "systemc/ext/channel/sc_signal_in_if.hh"
4412952Sgabeblack@google.com#include "systemc/ext/core/sc_event.hh"
4512952Sgabeblack@google.com#include "systemc/ext/core/sc_module.hh"
4612952Sgabeblack@google.com#include "systemc/ext/core/sc_process_handle.hh"
4712952Sgabeblack@google.com
4813196Sgabeblack@google.comnamespace sc_core
4913196Sgabeblack@google.com{
5013196Sgabeblack@google.com
5113196Sgabeblack@google.comclass sc_join;
5213196Sgabeblack@google.com
5313196Sgabeblack@google.com} // namespace sc_core
5413196Sgabeblack@google.com
5512952Sgabeblack@google.comnamespace sc_gem5
5612952Sgabeblack@google.com{
5712952Sgabeblack@google.com
5813175Sgabeblack@google.comclass ScHalt
5913175Sgabeblack@google.com{};
6013175Sgabeblack@google.com
6113288Sgabeblack@google.comclass Process;
6213288Sgabeblack@google.comclass Reset;
6313288Sgabeblack@google.com
6413087Sgabeblack@google.comclass Process : public ::sc_core::sc_process_b, public ListNode
6512952Sgabeblack@google.com{
6612952Sgabeblack@google.com  public:
6712952Sgabeblack@google.com    virtual ::sc_core::sc_curr_proc_kind procKind() const = 0;
6812961Sgabeblack@google.com    bool needsStart() const { return _needsStart; }
6913093Sgabeblack@google.com    void needsStart(bool ns) { _needsStart = ns; }
7012952Sgabeblack@google.com    bool dynamic() const { return _dynamic; }
7112952Sgabeblack@google.com    bool isUnwinding() const { return _isUnwinding; }
7212997Sgabeblack@google.com    void isUnwinding(bool v) { _isUnwinding = v; }
7312952Sgabeblack@google.com    bool terminated() const { return _terminated; }
7412952Sgabeblack@google.com
7513328Sgabeblack@google.com    bool scheduled() const { return _scheduled; }
7613328Sgabeblack@google.com    void scheduled(bool new_val) { _scheduled = new_val; }
7713328Sgabeblack@google.com
7812952Sgabeblack@google.com    void forEachKid(const std::function<void(Process *)> &work);
7912952Sgabeblack@google.com
8012952Sgabeblack@google.com    bool suspended() const { return _suspended; }
8112952Sgabeblack@google.com    bool disabled() const { return _disabled; }
8212952Sgabeblack@google.com
8312952Sgabeblack@google.com    void suspend(bool inc_kids);
8412952Sgabeblack@google.com    void resume(bool inc_kids);
8512952Sgabeblack@google.com    void disable(bool inc_kids);
8612952Sgabeblack@google.com    void enable(bool inc_kids);
8712952Sgabeblack@google.com
8812952Sgabeblack@google.com    void kill(bool inc_kids);
8912952Sgabeblack@google.com    void reset(bool inc_kids);
9013306Sgabeblack@google.com    void throw_it(ExceptionWrapperBase &exc, bool inc_kids);
9112952Sgabeblack@google.com
9212952Sgabeblack@google.com    void injectException(ExceptionWrapperBase &exc);
9312952Sgabeblack@google.com    ExceptionWrapperBase *excWrapper;
9412952Sgabeblack@google.com
9512952Sgabeblack@google.com    void syncResetOn(bool inc_kids);
9612952Sgabeblack@google.com    void syncResetOff(bool inc_kids);
9712952Sgabeblack@google.com
9813260Sgabeblack@google.com    void signalReset(bool set, bool sync);
9913260Sgabeblack@google.com
10012952Sgabeblack@google.com    void incref() { refCount++; }
10112952Sgabeblack@google.com    void decref() { refCount--; }
10212952Sgabeblack@google.com
10313308Sgabeblack@google.com    ::sc_core::sc_event &resetEvent() { return _resetEvent; }
10413308Sgabeblack@google.com    ::sc_core::sc_event &terminatedEvent() { return _terminatedEvent; }
10512952Sgabeblack@google.com
10612953Sgabeblack@google.com    void setStackSize(size_t size) { stackSize = size; }
10712953Sgabeblack@google.com
10812953Sgabeblack@google.com    void run();
10912953Sgabeblack@google.com
11013206Sgabeblack@google.com    void addStatic(StaticSensitivity *);
11113206Sgabeblack@google.com    void setDynamic(DynamicSensitivity *);
11213206Sgabeblack@google.com    void clearDynamic() { setDynamic(nullptr); }
11313288Sgabeblack@google.com    void addReset(Reset *);
11413206Sgabeblack@google.com
11513206Sgabeblack@google.com    ScEvent timeoutEvent;
11613206Sgabeblack@google.com    void setTimeout(::sc_core::sc_time t);
11713206Sgabeblack@google.com    void cancelTimeout();
11812957Sgabeblack@google.com
11912959Sgabeblack@google.com    void satisfySensitivity(Sensitivity *);
12012959Sgabeblack@google.com
12112959Sgabeblack@google.com    void ready();
12212959Sgabeblack@google.com
12312953Sgabeblack@google.com    virtual Fiber *fiber() { return Fiber::primaryFiber(); }
12412953Sgabeblack@google.com
12512953Sgabeblack@google.com    static Process *newest() { return _newest; }
12612953Sgabeblack@google.com
12712997Sgabeblack@google.com    void lastReport(::sc_core::sc_report *report);
12812997Sgabeblack@google.com    ::sc_core::sc_report *lastReport() const;
12912997Sgabeblack@google.com
13013180Sgabeblack@google.com    bool hasStaticSensitivities() { return !staticSensitivities.empty(); }
13113180Sgabeblack@google.com    bool internal() { return _internal; }
13213189Sgabeblack@google.com    bool timedOut() { return _timedOut; }
13313260Sgabeblack@google.com    bool inReset() { return _syncReset || syncResetCount || asyncResetCount; }
13413180Sgabeblack@google.com
13513194Sgabeblack@google.com    bool dontInitialize() { return _dontInitialize; }
13613194Sgabeblack@google.com    void dontInitialize(bool di) { _dontInitialize = di; }
13713194Sgabeblack@google.com
13813196Sgabeblack@google.com    void joinWait(::sc_core::sc_join *join) { joinWaiters.push_back(join); }
13913196Sgabeblack@google.com
14013260Sgabeblack@google.com    void waitCount(int count) { _waitCount = count; }
14113260Sgabeblack@google.com
14213285Sgabeblack@google.com    const char *uniqueName(const char *seed) { return nameGen.gen(seed); }
14313285Sgabeblack@google.com
14412952Sgabeblack@google.com  protected:
14513206Sgabeblack@google.com    void timeout();
14613206Sgabeblack@google.com
14713180Sgabeblack@google.com    Process(const char *name, ProcessFuncWrapper *func, bool internal=false);
14812953Sgabeblack@google.com
14912953Sgabeblack@google.com    static Process *_newest;
15012952Sgabeblack@google.com
15112957Sgabeblack@google.com    virtual ~Process()
15212957Sgabeblack@google.com    {
15313072Sgabeblack@google.com        popListNode();
15412957Sgabeblack@google.com        delete func;
15513206Sgabeblack@google.com        for (auto s: staticSensitivities) {
15613206Sgabeblack@google.com            s->clear();
15712957Sgabeblack@google.com            delete s;
15813206Sgabeblack@google.com        }
15913206Sgabeblack@google.com        clearDynamic();
16012957Sgabeblack@google.com    }
16112952Sgabeblack@google.com
16213303Sgabeblack@google.com    InternalScEvent _resetEvent;
16313303Sgabeblack@google.com    InternalScEvent _terminatedEvent;
16412952Sgabeblack@google.com
16512952Sgabeblack@google.com    ProcessFuncWrapper *func;
16613180Sgabeblack@google.com
16713180Sgabeblack@google.com    bool _internal;
16813180Sgabeblack@google.com
16913189Sgabeblack@google.com    // Needed to support the deprecated "timed_out" function.
17013189Sgabeblack@google.com    bool _timedOut;
17113189Sgabeblack@google.com
17213194Sgabeblack@google.com    bool _dontInitialize;
17313194Sgabeblack@google.com
17412961Sgabeblack@google.com    bool _needsStart;
17512952Sgabeblack@google.com    bool _dynamic;
17612952Sgabeblack@google.com    bool _isUnwinding;
17712952Sgabeblack@google.com    bool _terminated;
17813328Sgabeblack@google.com    bool _scheduled;
17912952Sgabeblack@google.com
18012998Sgabeblack@google.com    void terminate();
18112998Sgabeblack@google.com
18212952Sgabeblack@google.com    bool _suspended;
18312959Sgabeblack@google.com    bool _suspendedReady;
18412952Sgabeblack@google.com    bool _disabled;
18512952Sgabeblack@google.com
18612952Sgabeblack@google.com    bool _syncReset;
18712952Sgabeblack@google.com
18813260Sgabeblack@google.com    int syncResetCount;
18913260Sgabeblack@google.com    int asyncResetCount;
19013260Sgabeblack@google.com
19113260Sgabeblack@google.com    int _waitCount;
19213260Sgabeblack@google.com
19312952Sgabeblack@google.com    int refCount;
19412952Sgabeblack@google.com
19512953Sgabeblack@google.com    size_t stackSize;
19612957Sgabeblack@google.com
19713206Sgabeblack@google.com    StaticSensitivities staticSensitivities;
19813206Sgabeblack@google.com    DynamicSensitivity *dynamicSensitivity;
19913288Sgabeblack@google.com    std::vector<Reset *> resets;
20012997Sgabeblack@google.com
20112997Sgabeblack@google.com    std::unique_ptr<::sc_core::sc_report> _lastReport;
20213196Sgabeblack@google.com
20313196Sgabeblack@google.com    std::vector<::sc_core::sc_join *> joinWaiters;
20413285Sgabeblack@google.com
20513285Sgabeblack@google.com    UniqueNameGen nameGen;
20612952Sgabeblack@google.com};
20712952Sgabeblack@google.com
20813288Sgabeblack@google.comclass Reset
20913288Sgabeblack@google.com{
21013288Sgabeblack@google.com  public:
21113288Sgabeblack@google.com    Reset(Process *p, bool s, bool v) :
21213288Sgabeblack@google.com        _process(p), _signal(nullptr), _sync(s), _value(v)
21313288Sgabeblack@google.com    {}
21413288Sgabeblack@google.com
21513288Sgabeblack@google.com    bool
21613288Sgabeblack@google.com    install(const sc_core::sc_signal_in_if<bool> *s)
21713288Sgabeblack@google.com    {
21813288Sgabeblack@google.com        _signal = s;
21913288Sgabeblack@google.com
22013288Sgabeblack@google.com        if (_signal->_addReset(this)) {
22113288Sgabeblack@google.com            _process->addReset(this);
22213288Sgabeblack@google.com            if (_signal->read() == _value)
22313288Sgabeblack@google.com                update();
22413288Sgabeblack@google.com            return true;
22513288Sgabeblack@google.com        }
22613288Sgabeblack@google.com        return false;
22713288Sgabeblack@google.com    }
22813288Sgabeblack@google.com    void update() { _process->signalReset(_signal->read() == _value, _sync); }
22913288Sgabeblack@google.com
23013288Sgabeblack@google.com    Process *process() { return _process; }
23113288Sgabeblack@google.com    const sc_core::sc_signal_in_if<bool> *signal() { return _signal; }
23213288Sgabeblack@google.com    bool sync() { return _sync; }
23313288Sgabeblack@google.com    bool value() { return _value; }
23413288Sgabeblack@google.com
23513288Sgabeblack@google.com  private:
23613288Sgabeblack@google.com    Process *_process;
23713288Sgabeblack@google.com    const sc_core::sc_signal_in_if<bool> *_signal;
23813288Sgabeblack@google.com    bool _sync;
23913288Sgabeblack@google.com    bool _value;
24013288Sgabeblack@google.com};
24113288Sgabeblack@google.com
24213288Sgabeblack@google.comvoid newReset(const sc_core::sc_port_base *pb, Process *p, bool s, bool v);
24313288Sgabeblack@google.comvoid newReset(const sc_core::sc_signal_in_if<bool> *sig, Process *p,
24413288Sgabeblack@google.com        bool s, bool v);
24513288Sgabeblack@google.com
24612952Sgabeblack@google.com} // namespace sc_gem5
24712952Sgabeblack@google.com
24812952Sgabeblack@google.com#endif  //__SYSTEMC_CORE_PROCESS_HH__
249