kernel.cc revision 13094:e74742df378b
112852Sgabeblack@google.com/*
212852Sgabeblack@google.com * Copyright 2018 Google, Inc.
312852Sgabeblack@google.com *
412852Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512852Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612852Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712852Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812852Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912852Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012852Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112852Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212852Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312852Sgabeblack@google.com * this software without specific prior written permission.
1412852Sgabeblack@google.com *
1512852Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612852Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712852Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812852Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912852Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012852Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112852Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212852Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312852Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412852Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512852Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612852Sgabeblack@google.com *
2712852Sgabeblack@google.com * Authors: Gabe Black
2812852Sgabeblack@google.com */
2912852Sgabeblack@google.com
3012852Sgabeblack@google.com#include "systemc/core/kernel.hh"
3112852Sgabeblack@google.com
3212852Sgabeblack@google.com#include "base/logging.hh"
3312852Sgabeblack@google.com#include "systemc/core/channel.hh"
3412852Sgabeblack@google.com#include "systemc/core/module.hh"
3512852Sgabeblack@google.com#include "systemc/core/scheduler.hh"
3612852Sgabeblack@google.com
3712852Sgabeblack@google.comnamespace sc_gem5
3812852Sgabeblack@google.com{
3912852Sgabeblack@google.com
4012852Sgabeblack@google.comnamespace
4112852Sgabeblack@google.com{
4212852Sgabeblack@google.com
4312852Sgabeblack@google.combool scMainDone = false;
4412852Sgabeblack@google.combool stopAfterCallbacks = false;
4512852Sgabeblack@google.combool startComplete = false;
4612852Sgabeblack@google.combool endComplete = false;
4712852Sgabeblack@google.com
4812852Sgabeblack@google.comsc_core::sc_status _status = sc_core::SC_ELABORATION;
4912852Sgabeblack@google.com
5012852Sgabeblack@google.com} // anonymous namespace
5112852Sgabeblack@google.com
5212852Sgabeblack@google.combool Kernel::startOfSimulationComplete() { return startComplete; }
5312852Sgabeblack@google.combool Kernel::endOfSimulationComplete() { return endComplete; }
5412852Sgabeblack@google.com
5512852Sgabeblack@google.combool Kernel::scMainFinished() { return scMainDone; }
5612852Sgabeblack@google.comvoid Kernel::scMainFinished(bool finished) { scMainDone = finished; }
5712852Sgabeblack@google.com
5812852Sgabeblack@google.comsc_core::sc_status Kernel::status() { return _status; }
5912852Sgabeblack@google.comvoid Kernel::status(sc_core::sc_status s) { _status = s; }
6012852Sgabeblack@google.com
6112905Sgabeblack@google.comKernel::Kernel(Params *params) :
6212905Sgabeblack@google.com    SimObject(params), t0Event(this, false, EventBase::Default_Pri - 1)
6312905Sgabeblack@google.com{
6412852Sgabeblack@google.com    // Install ourselves as the scheduler's event manager.
6512852Sgabeblack@google.com    ::sc_gem5::scheduler.setEventQueue(eventQueue());
6612852Sgabeblack@google.com}
6712852Sgabeblack@google.com
6812852Sgabeblack@google.comvoid
6912852Sgabeblack@google.comKernel::init()
7012852Sgabeblack@google.com{
7112852Sgabeblack@google.com    if (scMainDone)
7212852Sgabeblack@google.com        return;
7312852Sgabeblack@google.com
7412852Sgabeblack@google.com    if (stopAfterCallbacks)
7512852Sgabeblack@google.com        fatal("Simulation called sc_stop during elaboration.\n");
7612852Sgabeblack@google.com
7712852Sgabeblack@google.com    status(::sc_core::SC_BEFORE_END_OF_ELABORATION);
7812852Sgabeblack@google.com    for (auto m: sc_gem5::allModules) {
7912852Sgabeblack@google.com        callbackModule(m);
8012852Sgabeblack@google.com        m->sc_mod()->before_end_of_elaboration();
8112852Sgabeblack@google.com        for (auto p: m->ports)
8212852Sgabeblack@google.com            p->before_end_of_elaboration();
8312852Sgabeblack@google.com        for (auto e: m->exports)
8412852Sgabeblack@google.com            e->before_end_of_elaboration();
8512852Sgabeblack@google.com    }
8612852Sgabeblack@google.com    callbackModule(nullptr);
8712852Sgabeblack@google.com    for (auto c: sc_gem5::allChannels)
8812852Sgabeblack@google.com        c->sc_chan()->before_end_of_elaboration();
8912852Sgabeblack@google.com
9012852Sgabeblack@google.com    if (stopAfterCallbacks)
9112852Sgabeblack@google.com        stopWork();
9212852Sgabeblack@google.com}
9312852Sgabeblack@google.com
9412852Sgabeblack@google.comvoid
9512852Sgabeblack@google.comKernel::regStats()
9612852Sgabeblack@google.com{
9712852Sgabeblack@google.com    if (scMainDone)
9812852Sgabeblack@google.com        return;
9912852Sgabeblack@google.com
10012852Sgabeblack@google.com    for (auto m: sc_gem5::allModules)
10112852Sgabeblack@google.com        for (auto p: m->ports)
10212852Sgabeblack@google.com            p->_gem5Finalize();
10312852Sgabeblack@google.com
10412852Sgabeblack@google.com    status(::sc_core::SC_END_OF_ELABORATION);
10512852Sgabeblack@google.com    for (auto m: sc_gem5::allModules) {
10612852Sgabeblack@google.com        m->sc_mod()->end_of_elaboration();
10712852Sgabeblack@google.com        for (auto p: m->ports)
10812852Sgabeblack@google.com            p->end_of_elaboration();
10912852Sgabeblack@google.com        for (auto e: m->exports)
11012852Sgabeblack@google.com            e->end_of_elaboration();
11112852Sgabeblack@google.com    }
11212852Sgabeblack@google.com    for (auto c: sc_gem5::allChannels)
11312877Sgabeblack@google.com        c->sc_chan()->end_of_elaboration();
11412905Sgabeblack@google.com
11512905Sgabeblack@google.com    if (stopAfterCallbacks)
11612905Sgabeblack@google.com        stopWork();
11712905Sgabeblack@google.com}
11812905Sgabeblack@google.com
11912905Sgabeblack@google.comvoid
12012905Sgabeblack@google.comKernel::startup()
12112905Sgabeblack@google.com{
12212877Sgabeblack@google.com    if (scMainDone)
12312877Sgabeblack@google.com        return;
12412877Sgabeblack@google.com
12513241Sgabeblack@google.com    status(::sc_core::SC_START_OF_SIMULATION);
12612877Sgabeblack@google.com    for (auto m: sc_gem5::allModules) {
12713241Sgabeblack@google.com        m->sc_mod()->start_of_simulation();
12812877Sgabeblack@google.com        for (auto p: m->ports)
12913241Sgabeblack@google.com            p->start_of_simulation();
13012877Sgabeblack@google.com        for (auto e: m->exports)
13113241Sgabeblack@google.com            e->start_of_simulation();
13212877Sgabeblack@google.com    }
13313241Sgabeblack@google.com    for (auto c: sc_gem5::allChannels)
13412877Sgabeblack@google.com        c->sc_chan()->start_of_simulation();
13513241Sgabeblack@google.com
13612877Sgabeblack@google.com    startComplete = true;
13713241Sgabeblack@google.com
13812877Sgabeblack@google.com    if (stopAfterCallbacks)
13913241Sgabeblack@google.com        stopWork();
14012877Sgabeblack@google.com
14112852Sgabeblack@google.com    kernel->status(::sc_core::SC_RUNNING);
14212852Sgabeblack@google.com
14312852Sgabeblack@google.com    schedule(t0Event, curTick());
14412852Sgabeblack@google.com    // Run update once before the event queue starts.
14512852Sgabeblack@google.com    ::sc_gem5::scheduler.update();
14613241Sgabeblack@google.com}
14712852Sgabeblack@google.com
14813241Sgabeblack@google.comvoid
14912852Sgabeblack@google.comKernel::stop()
15013241Sgabeblack@google.com{
15112852Sgabeblack@google.com    if (status() < ::sc_core::SC_RUNNING)
15213241Sgabeblack@google.com        stopAfterCallbacks = true;
15312852Sgabeblack@google.com    else
15413241Sgabeblack@google.com        stopWork();
15512852Sgabeblack@google.com}
15613241Sgabeblack@google.com
15712852Sgabeblack@google.comvoid
15813241Sgabeblack@google.comKernel::stopWork()
15912852Sgabeblack@google.com{
16013241Sgabeblack@google.com    status(::sc_core::SC_END_OF_SIMULATION);
16112852Sgabeblack@google.com    for (auto m: sc_gem5::allModules) {
16213241Sgabeblack@google.com        m->sc_mod()->end_of_simulation();
16312852Sgabeblack@google.com        for (auto p: m->ports)
16413241Sgabeblack@google.com            p->end_of_simulation();
16512852Sgabeblack@google.com        for (auto e: m->exports)
16612877Sgabeblack@google.com            e->end_of_simulation();
16712877Sgabeblack@google.com    }
16812877Sgabeblack@google.com    for (auto c: sc_gem5::allChannels)
16912877Sgabeblack@google.com        c->sc_chan()->end_of_simulation();
17012913Sgabeblack@google.com
17112913Sgabeblack@google.com    endComplete = true;
17212913Sgabeblack@google.com
17312852Sgabeblack@google.com    status(::sc_core::SC_STOPPED);
17412852Sgabeblack@google.com}
17513241Sgabeblack@google.com
17613241Sgabeblack@google.comvoid
17712852Sgabeblack@google.comKernel::t0Handler()
17813241Sgabeblack@google.com{
17912852Sgabeblack@google.com    ::sc_gem5::scheduler.initPhase();
18012852Sgabeblack@google.com
18112852Sgabeblack@google.com    status(::sc_core::SC_RUNNING);
18212852Sgabeblack@google.com}
18312852Sgabeblack@google.com
18412852Sgabeblack@google.comKernel *kernel;
18512852Sgabeblack@google.com
18612852Sgabeblack@google.com} // namespace sc_gem5
18712852Sgabeblack@google.com
18812852Sgabeblack@google.comsc_gem5::Kernel *
18912852Sgabeblack@google.comSystemC_KernelParams::create()
19012852Sgabeblack@google.com{
19112852Sgabeblack@google.com    panic_if(sc_gem5::kernel,
19212852Sgabeblack@google.com            "Only one systemc kernel object may be defined.\n");
19312852Sgabeblack@google.com    sc_gem5::kernel = new sc_gem5::Kernel(this);
19412852Sgabeblack@google.com    return sc_gem5::kernel;
19512852Sgabeblack@google.com}
196