112955Sgabeblack@google.com/*
212955Sgabeblack@google.com * Copyright 2018 Google, Inc.
312955Sgabeblack@google.com *
412955Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512955Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612955Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712955Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812955Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912955Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012955Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112955Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212955Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312955Sgabeblack@google.com * this software without specific prior written permission.
1412955Sgabeblack@google.com *
1512955Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612955Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712955Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812955Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912955Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012955Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112955Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212955Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312955Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412955Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512955Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612955Sgabeblack@google.com *
2712955Sgabeblack@google.com * Authors: Gabe Black
2812955Sgabeblack@google.com */
2912955Sgabeblack@google.com
3012955Sgabeblack@google.com#include "systemc/core/event.hh"
3112955Sgabeblack@google.com
3212988Sgabeblack@google.com#include <algorithm>
3312955Sgabeblack@google.com#include <cstring>
3412955Sgabeblack@google.com#include <utility>
3512955Sgabeblack@google.com
3612957Sgabeblack@google.com#include "sim/core.hh"
3712955Sgabeblack@google.com#include "systemc/core/module.hh"
3812955Sgabeblack@google.com#include "systemc/core/scheduler.hh"
3913317Sgabeblack@google.com#include "systemc/ext/core/messages.hh"
4013086Sgabeblack@google.com#include "systemc/ext/core/sc_main.hh"
4113086Sgabeblack@google.com#include "systemc/ext/core/sc_module.hh"
4212955Sgabeblack@google.com
4312955Sgabeblack@google.comnamespace sc_gem5
4412955Sgabeblack@google.com{
4512955Sgabeblack@google.com
4613303Sgabeblack@google.comEvent::Event(sc_core::sc_event *_sc_event, bool internal) :
4713303Sgabeblack@google.com    Event(_sc_event, nullptr, internal)
4813303Sgabeblack@google.com{}
4912955Sgabeblack@google.com
5013303Sgabeblack@google.comEvent::Event(sc_core::sc_event *_sc_event, const char *_basename_cstr,
5113303Sgabeblack@google.com        bool internal) :
5213086Sgabeblack@google.com    _sc_event(_sc_event), _basename(_basename_cstr ? _basename_cstr : ""),
5313303Sgabeblack@google.com    _inHierarchy(!internal), delayedNotify([this]() { this->notify(); }),
5413303Sgabeblack@google.com    _triggeredStamp(~0ULL)
5512955Sgabeblack@google.com{
5613086Sgabeblack@google.com    if (_basename == "" && ::sc_core::sc_is_running())
5713086Sgabeblack@google.com        _basename = ::sc_core::sc_gen_unique_name("event");
5813086Sgabeblack@google.com
5913303Sgabeblack@google.com    parent = internal ? nullptr : pickParentObj();
6012955Sgabeblack@google.com
6113303Sgabeblack@google.com    if (internal) {
6213303Sgabeblack@google.com        _basename = globalNameGen.gen(_basename);
6313303Sgabeblack@google.com        _name = _basename;
6413303Sgabeblack@google.com    } else {
6513303Sgabeblack@google.com        std::string original_name = _basename;
6613303Sgabeblack@google.com        _basename = pickUniqueName(parent, _basename);
6713127Sgabeblack@google.com
6813303Sgabeblack@google.com        if (parent) {
6913303Sgabeblack@google.com            Object *obj = Object::getFromScObject(parent);
7013303Sgabeblack@google.com            obj->addChildEvent(_sc_event);
7113303Sgabeblack@google.com        } else {
7213303Sgabeblack@google.com            topLevelEvents.emplace(topLevelEvents.end(), _sc_event);
7313303Sgabeblack@google.com        }
7413303Sgabeblack@google.com
7513303Sgabeblack@google.com        std::string path = parent ? (std::string(parent->name()) + ".") : "";
7613303Sgabeblack@google.com
7713303Sgabeblack@google.com        if (original_name != "" && _basename != original_name) {
7813303Sgabeblack@google.com            std::string message = path + original_name +
7913303Sgabeblack@google.com                ". Latter declaration will be renamed to " +
8013303Sgabeblack@google.com                path + _basename;
8113317Sgabeblack@google.com            SC_REPORT_WARNING(sc_core::SC_ID_INSTANCE_EXISTS_,
8213317Sgabeblack@google.com                    message.c_str());
8313303Sgabeblack@google.com        }
8413303Sgabeblack@google.com
8513303Sgabeblack@google.com        _name = path + _basename;
8612955Sgabeblack@google.com    }
8712955Sgabeblack@google.com
8812955Sgabeblack@google.com    allEvents.emplace(allEvents.end(), _sc_event);
8912955Sgabeblack@google.com
9012955Sgabeblack@google.com    // Determine if we're in the hierarchy (created once initialization starts
9112955Sgabeblack@google.com    // means no).
9212955Sgabeblack@google.com}
9312955Sgabeblack@google.com
9412955Sgabeblack@google.comEvent::~Event()
9512955Sgabeblack@google.com{
9612955Sgabeblack@google.com    if (parent) {
9712955Sgabeblack@google.com        Object *obj = Object::getFromScObject(parent);
9812988Sgabeblack@google.com        obj->delChildEvent(_sc_event);
9913303Sgabeblack@google.com    } else if (inHierarchy()) {
10012988Sgabeblack@google.com        EventsIt it = find(topLevelEvents.begin(), topLevelEvents.end(),
10112988Sgabeblack@google.com                           _sc_event);
10212988Sgabeblack@google.com        assert(it != topLevelEvents.end());
10312988Sgabeblack@google.com        std::swap(*it, topLevelEvents.back());
10412955Sgabeblack@google.com        topLevelEvents.pop_back();
10512955Sgabeblack@google.com    }
10612955Sgabeblack@google.com
10712955Sgabeblack@google.com    EventsIt it = findEvent(_name);
10812955Sgabeblack@google.com    std::swap(*it, allEvents.back());
10912955Sgabeblack@google.com    allEvents.pop_back();
11012957Sgabeblack@google.com
11113063Sgabeblack@google.com    if (delayedNotify.scheduled())
11213063Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
11312955Sgabeblack@google.com}
11412955Sgabeblack@google.com
11512955Sgabeblack@google.comconst std::string &
11612955Sgabeblack@google.comEvent::name() const
11712955Sgabeblack@google.com{
11812955Sgabeblack@google.com    return _name;
11912955Sgabeblack@google.com}
12012955Sgabeblack@google.com
12112955Sgabeblack@google.comconst std::string &
12212955Sgabeblack@google.comEvent::basename() const
12312955Sgabeblack@google.com{
12412955Sgabeblack@google.com    return _basename;
12512955Sgabeblack@google.com}
12612955Sgabeblack@google.com
12712955Sgabeblack@google.combool
12812955Sgabeblack@google.comEvent::inHierarchy() const
12912955Sgabeblack@google.com{
13013303Sgabeblack@google.com    return _inHierarchy;
13112955Sgabeblack@google.com}
13212955Sgabeblack@google.com
13312955Sgabeblack@google.comsc_core::sc_object *
13412955Sgabeblack@google.comEvent::getParentObject() const
13512955Sgabeblack@google.com{
13612955Sgabeblack@google.com    return parent;
13712955Sgabeblack@google.com}
13812955Sgabeblack@google.com
13912955Sgabeblack@google.comvoid
14013208Sgabeblack@google.comEvent::notify(StaticSensitivities &senses)
14113208Sgabeblack@google.com{
14213208Sgabeblack@google.com    for (auto s: senses)
14313208Sgabeblack@google.com        s->notify(this);
14413208Sgabeblack@google.com}
14513208Sgabeblack@google.com
14613208Sgabeblack@google.comvoid
14713208Sgabeblack@google.comEvent::notify(DynamicSensitivities &senses)
14813208Sgabeblack@google.com{
14913208Sgabeblack@google.com    int size = senses.size();
15013208Sgabeblack@google.com    int pos = 0;
15113208Sgabeblack@google.com    while (pos < size) {
15213208Sgabeblack@google.com        if (senses[pos]->notify(this))
15313208Sgabeblack@google.com            senses[pos] = senses[--size];
15413208Sgabeblack@google.com        else
15513208Sgabeblack@google.com            pos++;
15613208Sgabeblack@google.com    }
15713208Sgabeblack@google.com    senses.resize(size);
15813208Sgabeblack@google.com}
15913208Sgabeblack@google.com
16013208Sgabeblack@google.comvoid
16112955Sgabeblack@google.comEvent::notify()
16212955Sgabeblack@google.com{
16313317Sgabeblack@google.com    if (scheduler.inUpdate())
16413317Sgabeblack@google.com        SC_REPORT_ERROR(sc_core::SC_ID_IMMEDIATE_NOTIFICATION_, "");
16513187Sgabeblack@google.com
16613073Sgabeblack@google.com    // An immediate notification overrides any pending delayed notification.
16713073Sgabeblack@google.com    if (delayedNotify.scheduled())
16813073Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
16913073Sgabeblack@google.com
17013243Sgabeblack@google.com    _triggeredStamp = scheduler.changeStamp();
17113208Sgabeblack@google.com    notify(staticSenseMethod);
17213208Sgabeblack@google.com    notify(dynamicSenseMethod);
17313208Sgabeblack@google.com    notify(staticSenseThread);
17413208Sgabeblack@google.com    notify(dynamicSenseThread);
17512955Sgabeblack@google.com}
17612955Sgabeblack@google.com
17712955Sgabeblack@google.comvoid
17812955Sgabeblack@google.comEvent::notify(const sc_core::sc_time &t)
17912955Sgabeblack@google.com{
18013063Sgabeblack@google.com    if (delayedNotify.scheduled()) {
18113063Sgabeblack@google.com        if (scheduler.delayed(t) >= delayedNotify.when())
18212957Sgabeblack@google.com            return;
18312957Sgabeblack@google.com
18413063Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
18512957Sgabeblack@google.com    }
18613063Sgabeblack@google.com    scheduler.schedule(&delayedNotify, t);
18712955Sgabeblack@google.com}
18812955Sgabeblack@google.com
18912955Sgabeblack@google.comvoid
19013299Sgabeblack@google.comEvent::notifyDelayed(const sc_core::sc_time &t)
19113299Sgabeblack@google.com{
19213317Sgabeblack@google.com    if (delayedNotify.scheduled())
19313317Sgabeblack@google.com        SC_REPORT_ERROR(sc_core::SC_ID_NOTIFY_DELAYED_, "");
19413299Sgabeblack@google.com    notify(t);
19513299Sgabeblack@google.com}
19613299Sgabeblack@google.com
19713299Sgabeblack@google.comvoid
19812955Sgabeblack@google.comEvent::cancel()
19912955Sgabeblack@google.com{
20013063Sgabeblack@google.com    if (delayedNotify.scheduled())
20113063Sgabeblack@google.com        scheduler.deschedule(&delayedNotify);
20212955Sgabeblack@google.com}
20312955Sgabeblack@google.com
20412955Sgabeblack@google.combool
20512955Sgabeblack@google.comEvent::triggered() const
20612955Sgabeblack@google.com{
20713243Sgabeblack@google.com    return _triggeredStamp == scheduler.changeStamp();
20812955Sgabeblack@google.com}
20912955Sgabeblack@google.com
21013295Sgabeblack@google.comvoid
21113295Sgabeblack@google.comEvent::clearParent()
21213295Sgabeblack@google.com{
21313295Sgabeblack@google.com    if (!parent)
21413295Sgabeblack@google.com        return;
21513295Sgabeblack@google.com    Object::getFromScObject(parent)->delChildEvent(sc_event());
21613295Sgabeblack@google.com    parent = nullptr;
21713295Sgabeblack@google.com    topLevelEvents.emplace(topLevelEvents.end(), sc_event());
21813295Sgabeblack@google.com}
21913295Sgabeblack@google.com
22012955Sgabeblack@google.comEvents topLevelEvents;
22112955Sgabeblack@google.comEvents allEvents;
22212955Sgabeblack@google.com
22312955Sgabeblack@google.comEventsIt
22412955Sgabeblack@google.comfindEvent(const std::string &name)
22512955Sgabeblack@google.com{
22612955Sgabeblack@google.com    EventsIt it;
22712955Sgabeblack@google.com    for (it = allEvents.begin(); it != allEvents.end(); it++)
22812955Sgabeblack@google.com        if (!strcmp((*it)->name(), name.c_str()))
22912955Sgabeblack@google.com            break;
23012955Sgabeblack@google.com
23112955Sgabeblack@google.com    return it;
23212955Sgabeblack@google.com}
23312955Sgabeblack@google.com
23412955Sgabeblack@google.com} // namespace sc_gem5
235