112837Sgabeblack@google.com/*
212837Sgabeblack@google.com * Copyright 2018 Google, Inc.
312837Sgabeblack@google.com *
412837Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512837Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612837Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812837Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012837Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112837Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212837Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312837Sgabeblack@google.com * this software without specific prior written permission.
1412837Sgabeblack@google.com *
1512837Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612837Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712837Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812837Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912837Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012837Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112837Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212837Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312837Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412837Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512837Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612837Sgabeblack@google.com *
2712837Sgabeblack@google.com * Authors: Gabe Black
2812837Sgabeblack@google.com */
2912837Sgabeblack@google.com
3012837Sgabeblack@google.com#include "systemc/core/kernel.hh"
3112982Sgabeblack@google.com
3212982Sgabeblack@google.com#include "base/logging.hh"
3313059Sgabeblack@google.com#include "systemc/core/channel.hh"
3412982Sgabeblack@google.com#include "systemc/core/module.hh"
3513207Sgabeblack@google.com#include "systemc/core/port.hh"
3613403Sgabeblack@google.com#include "systemc/core/sc_main_fiber.hh"
3712953Sgabeblack@google.com#include "systemc/core/scheduler.hh"
3812837Sgabeblack@google.com
3912982Sgabeblack@google.comnamespace sc_gem5
4012837Sgabeblack@google.com{
4112837Sgabeblack@google.com
4212990Sgabeblack@google.comnamespace
4312990Sgabeblack@google.com{
4412990Sgabeblack@google.com
4512990Sgabeblack@google.combool stopAfterCallbacks = false;
4612990Sgabeblack@google.combool startComplete = false;
4712990Sgabeblack@google.combool endComplete = false;
4812990Sgabeblack@google.com
4912990Sgabeblack@google.comsc_core::sc_status _status = sc_core::SC_ELABORATION;
5012990Sgabeblack@google.com
5112990Sgabeblack@google.com} // anonymous namespace
5212990Sgabeblack@google.com
5312990Sgabeblack@google.combool Kernel::startOfSimulationComplete() { return startComplete; }
5412990Sgabeblack@google.combool Kernel::endOfSimulationComplete() { return endComplete; }
5512990Sgabeblack@google.com
5612990Sgabeblack@google.comsc_core::sc_status Kernel::status() { return _status; }
5712990Sgabeblack@google.comvoid Kernel::status(sc_core::sc_status s) { _status = s; }
5812990Sgabeblack@google.com
5912954Sgabeblack@google.comKernel::Kernel(Params *params) :
6013042Sgabeblack@google.com    SimObject(params), t0Event(this, false, EventBase::Default_Pri - 1)
6113042Sgabeblack@google.com{
6213042Sgabeblack@google.com    // Install ourselves as the scheduler's event manager.
6313042Sgabeblack@google.com    ::sc_gem5::scheduler.setEventQueue(eventQueue());
6413042Sgabeblack@google.com}
6512982Sgabeblack@google.com
6612982Sgabeblack@google.comvoid
6712982Sgabeblack@google.comKernel::init()
6812982Sgabeblack@google.com{
6913403Sgabeblack@google.com    if (scMainFiber.finished())
7013077Sgabeblack@google.com        return;
7113077Sgabeblack@google.com
7213094Sgabeblack@google.com    if (stopAfterCallbacks)
7313094Sgabeblack@google.com        fatal("Simulation called sc_stop during elaboration.\n");
7413094Sgabeblack@google.com
7512990Sgabeblack@google.com    status(::sc_core::SC_BEFORE_END_OF_ELABORATION);
7613207Sgabeblack@google.com    for (auto p: allPorts)
7713207Sgabeblack@google.com        p->sc_port_base()->before_end_of_elaboration();
7813191Sgabeblack@google.com    for (auto m: sc_gem5::allModules)
7913191Sgabeblack@google.com        m->beforeEndOfElaboration();
8013059Sgabeblack@google.com    for (auto c: sc_gem5::allChannels)
8113059Sgabeblack@google.com        c->sc_chan()->before_end_of_elaboration();
8213281Sgabeblack@google.com
8313281Sgabeblack@google.com    ::sc_gem5::scheduler.elaborationDone(true);
8412982Sgabeblack@google.com}
8512982Sgabeblack@google.com
8612982Sgabeblack@google.comvoid
8712982Sgabeblack@google.comKernel::regStats()
8812982Sgabeblack@google.com{
8913403Sgabeblack@google.com    if (scMainFiber.finished() || stopAfterCallbacks)
9013077Sgabeblack@google.com        return;
9113077Sgabeblack@google.com
9213188Sgabeblack@google.com    try {
9313207Sgabeblack@google.com        for (auto p: allPorts)
9413207Sgabeblack@google.com            p->finalize();
9513273Sgabeblack@google.com        for (auto p: allPorts)
9613273Sgabeblack@google.com            p->regPort();
9713053Sgabeblack@google.com
9813188Sgabeblack@google.com        status(::sc_core::SC_END_OF_ELABORATION);
9913207Sgabeblack@google.com        for (auto p: allPorts)
10013207Sgabeblack@google.com            p->sc_port_base()->end_of_elaboration();
10113191Sgabeblack@google.com        for (auto m: sc_gem5::allModules)
10213191Sgabeblack@google.com            m->endOfElaboration();
10313188Sgabeblack@google.com        for (auto c: sc_gem5::allChannels)
10413188Sgabeblack@google.com            c->sc_chan()->end_of_elaboration();
10513188Sgabeblack@google.com    } catch (...) {
10613701Sgabeblack@google.com        ::sc_gem5::scheduler.throwUp();
10713059Sgabeblack@google.com    }
10812982Sgabeblack@google.com}
10912953Sgabeblack@google.com
11012953Sgabeblack@google.comvoid
11112953Sgabeblack@google.comKernel::startup()
11212837Sgabeblack@google.com{
11313403Sgabeblack@google.com    if (scMainFiber.finished())
11413077Sgabeblack@google.com        return;
11513077Sgabeblack@google.com
11613156Sgabeblack@google.com    schedule(t0Event, curTick());
11713156Sgabeblack@google.com
11813156Sgabeblack@google.com    if (stopAfterCallbacks)
11913156Sgabeblack@google.com        return;
12013156Sgabeblack@google.com
12113188Sgabeblack@google.com    try {
12213188Sgabeblack@google.com        status(::sc_core::SC_START_OF_SIMULATION);
12313207Sgabeblack@google.com        for (auto p: allPorts)
12413207Sgabeblack@google.com            p->sc_port_base()->start_of_simulation();
12513191Sgabeblack@google.com        for (auto m: sc_gem5::allModules)
12613191Sgabeblack@google.com            m->startOfSimulation();
12713188Sgabeblack@google.com        for (auto c: sc_gem5::allChannels)
12813188Sgabeblack@google.com            c->sc_chan()->start_of_simulation();
12913188Sgabeblack@google.com    } catch (...) {
13013701Sgabeblack@google.com        ::sc_gem5::scheduler.throwUp();
13113059Sgabeblack@google.com    }
13212982Sgabeblack@google.com
13312990Sgabeblack@google.com    startComplete = true;
13412982Sgabeblack@google.com
13512990Sgabeblack@google.com    if (stopAfterCallbacks)
13612982Sgabeblack@google.com        stopWork();
13712982Sgabeblack@google.com
13812982Sgabeblack@google.com    kernel->status(::sc_core::SC_RUNNING);
13912953Sgabeblack@google.com}
14012953Sgabeblack@google.com
14112953Sgabeblack@google.comvoid
14212982Sgabeblack@google.comKernel::stop()
14312982Sgabeblack@google.com{
14412982Sgabeblack@google.com    if (status() < ::sc_core::SC_RUNNING)
14512990Sgabeblack@google.com        stopAfterCallbacks = true;
14612982Sgabeblack@google.com    else
14712982Sgabeblack@google.com        stopWork();
14812982Sgabeblack@google.com}
14912982Sgabeblack@google.com
15012982Sgabeblack@google.comvoid
15112982Sgabeblack@google.comKernel::stopWork()
15212982Sgabeblack@google.com{
15312990Sgabeblack@google.com    status(::sc_core::SC_END_OF_SIMULATION);
15413188Sgabeblack@google.com    try {
15513207Sgabeblack@google.com        for (auto p: allPorts)
15613207Sgabeblack@google.com            p->sc_port_base()->end_of_simulation();
15713191Sgabeblack@google.com        for (auto m: sc_gem5::allModules)
15813191Sgabeblack@google.com            m->endOfSimulation();
15913188Sgabeblack@google.com        for (auto c: sc_gem5::allChannels)
16013188Sgabeblack@google.com            c->sc_chan()->end_of_simulation();
16113188Sgabeblack@google.com    } catch (...) {
16213701Sgabeblack@google.com        ::sc_gem5::scheduler.throwUp();
16313059Sgabeblack@google.com    }
16412982Sgabeblack@google.com
16512990Sgabeblack@google.com    endComplete = true;
16612982Sgabeblack@google.com
16712990Sgabeblack@google.com    status(::sc_core::SC_STOPPED);
16812982Sgabeblack@google.com}
16912982Sgabeblack@google.com
17012982Sgabeblack@google.comvoid
17112953Sgabeblack@google.comKernel::t0Handler()
17212953Sgabeblack@google.com{
17313156Sgabeblack@google.com    if (stopAfterCallbacks) {
17413156Sgabeblack@google.com        scheduler.clear();
17513156Sgabeblack@google.com        ::sc_gem5::scheduler.initPhase();
17613156Sgabeblack@google.com        scheduler.scheduleStop(false);
17713156Sgabeblack@google.com    } else {
17813156Sgabeblack@google.com        ::sc_gem5::scheduler.initPhase();
17913156Sgabeblack@google.com        status(::sc_core::SC_RUNNING);
18013156Sgabeblack@google.com    }
18112837Sgabeblack@google.com}
18212837Sgabeblack@google.com
18312982Sgabeblack@google.comKernel *kernel;
18412837Sgabeblack@google.com
18512982Sgabeblack@google.com} // namespace sc_gem5
18612982Sgabeblack@google.com
18712982Sgabeblack@google.comsc_gem5::Kernel *
18812837Sgabeblack@google.comSystemC_KernelParams::create()
18912837Sgabeblack@google.com{
19012982Sgabeblack@google.com    panic_if(sc_gem5::kernel,
19112982Sgabeblack@google.com            "Only one systemc kernel object may be defined.\n");
19212982Sgabeblack@google.com    sc_gem5::kernel = new sc_gem5::Kernel(this);
19312982Sgabeblack@google.com    return sc_gem5::kernel;
19412837Sgabeblack@google.com}
195