kernel.cc revision 13281
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"
3612953Sgabeblack@google.com#include "systemc/core/scheduler.hh"
3712837Sgabeblack@google.com
3812982Sgabeblack@google.comnamespace sc_gem5
3912837Sgabeblack@google.com{
4012837Sgabeblack@google.com
4112990Sgabeblack@google.comnamespace
4212990Sgabeblack@google.com{
4312990Sgabeblack@google.com
4413077Sgabeblack@google.combool scMainDone = false;
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
5613077Sgabeblack@google.combool Kernel::scMainFinished() { return scMainDone; }
5713077Sgabeblack@google.comvoid Kernel::scMainFinished(bool finished) { scMainDone = finished; }
5813077Sgabeblack@google.com
5912990Sgabeblack@google.comsc_core::sc_status Kernel::status() { return _status; }
6012990Sgabeblack@google.comvoid Kernel::status(sc_core::sc_status s) { _status = s; }
6112990Sgabeblack@google.com
6212954Sgabeblack@google.comKernel::Kernel(Params *params) :
6313042Sgabeblack@google.com    SimObject(params), t0Event(this, false, EventBase::Default_Pri - 1)
6413042Sgabeblack@google.com{
6513042Sgabeblack@google.com    // Install ourselves as the scheduler's event manager.
6613042Sgabeblack@google.com    ::sc_gem5::scheduler.setEventQueue(eventQueue());
6713042Sgabeblack@google.com}
6812982Sgabeblack@google.com
6912982Sgabeblack@google.comvoid
7012982Sgabeblack@google.comKernel::init()
7112982Sgabeblack@google.com{
7213077Sgabeblack@google.com    if (scMainDone)
7313077Sgabeblack@google.com        return;
7413077Sgabeblack@google.com
7513094Sgabeblack@google.com    if (stopAfterCallbacks)
7613094Sgabeblack@google.com        fatal("Simulation called sc_stop during elaboration.\n");
7713094Sgabeblack@google.com
7812990Sgabeblack@google.com    status(::sc_core::SC_BEFORE_END_OF_ELABORATION);
7913207Sgabeblack@google.com    for (auto p: allPorts)
8013207Sgabeblack@google.com        p->sc_port_base()->before_end_of_elaboration();
8113191Sgabeblack@google.com    for (auto m: sc_gem5::allModules)
8213191Sgabeblack@google.com        m->beforeEndOfElaboration();
8313059Sgabeblack@google.com    for (auto c: sc_gem5::allChannels)
8413059Sgabeblack@google.com        c->sc_chan()->before_end_of_elaboration();
8513281Sgabeblack@google.com
8613281Sgabeblack@google.com    ::sc_gem5::scheduler.elaborationDone(true);
8712982Sgabeblack@google.com}
8812982Sgabeblack@google.com
8912982Sgabeblack@google.comvoid
9012982Sgabeblack@google.comKernel::regStats()
9112982Sgabeblack@google.com{
9213156Sgabeblack@google.com    if (scMainDone || stopAfterCallbacks)
9313077Sgabeblack@google.com        return;
9413077Sgabeblack@google.com
9513188Sgabeblack@google.com    try {
9613207Sgabeblack@google.com        for (auto p: allPorts)
9713207Sgabeblack@google.com            p->finalize();
9813273Sgabeblack@google.com        for (auto p: allPorts)
9913273Sgabeblack@google.com            p->regPort();
10013053Sgabeblack@google.com
10113188Sgabeblack@google.com        status(::sc_core::SC_END_OF_ELABORATION);
10213207Sgabeblack@google.com        for (auto p: allPorts)
10313207Sgabeblack@google.com            p->sc_port_base()->end_of_elaboration();
10413191Sgabeblack@google.com        for (auto m: sc_gem5::allModules)
10513191Sgabeblack@google.com            m->endOfElaboration();
10613188Sgabeblack@google.com        for (auto c: sc_gem5::allChannels)
10713188Sgabeblack@google.com            c->sc_chan()->end_of_elaboration();
10813188Sgabeblack@google.com    } catch (...) {
10913188Sgabeblack@google.com        ::sc_gem5::scheduler.throwToScMain();
11013059Sgabeblack@google.com    }
11112982Sgabeblack@google.com}
11212953Sgabeblack@google.com
11312953Sgabeblack@google.comvoid
11412953Sgabeblack@google.comKernel::startup()
11512837Sgabeblack@google.com{
11613077Sgabeblack@google.com    if (scMainDone)
11713077Sgabeblack@google.com        return;
11813077Sgabeblack@google.com
11913156Sgabeblack@google.com    schedule(t0Event, curTick());
12013156Sgabeblack@google.com
12113156Sgabeblack@google.com    if (stopAfterCallbacks)
12213156Sgabeblack@google.com        return;
12313156Sgabeblack@google.com
12413188Sgabeblack@google.com    try {
12513188Sgabeblack@google.com        status(::sc_core::SC_START_OF_SIMULATION);
12613207Sgabeblack@google.com        for (auto p: allPorts)
12713207Sgabeblack@google.com            p->sc_port_base()->start_of_simulation();
12813191Sgabeblack@google.com        for (auto m: sc_gem5::allModules)
12913191Sgabeblack@google.com            m->startOfSimulation();
13013188Sgabeblack@google.com        for (auto c: sc_gem5::allChannels)
13113188Sgabeblack@google.com            c->sc_chan()->start_of_simulation();
13213188Sgabeblack@google.com    } catch (...) {
13313188Sgabeblack@google.com        ::sc_gem5::scheduler.throwToScMain();
13413059Sgabeblack@google.com    }
13512982Sgabeblack@google.com
13612990Sgabeblack@google.com    startComplete = true;
13712982Sgabeblack@google.com
13812990Sgabeblack@google.com    if (stopAfterCallbacks)
13912982Sgabeblack@google.com        stopWork();
14012982Sgabeblack@google.com
14112982Sgabeblack@google.com    kernel->status(::sc_core::SC_RUNNING);
14212953Sgabeblack@google.com}
14312953Sgabeblack@google.com
14412953Sgabeblack@google.comvoid
14512982Sgabeblack@google.comKernel::stop()
14612982Sgabeblack@google.com{
14712982Sgabeblack@google.com    if (status() < ::sc_core::SC_RUNNING)
14812990Sgabeblack@google.com        stopAfterCallbacks = true;
14912982Sgabeblack@google.com    else
15012982Sgabeblack@google.com        stopWork();
15112982Sgabeblack@google.com}
15212982Sgabeblack@google.com
15312982Sgabeblack@google.comvoid
15412982Sgabeblack@google.comKernel::stopWork()
15512982Sgabeblack@google.com{
15612990Sgabeblack@google.com    status(::sc_core::SC_END_OF_SIMULATION);
15713188Sgabeblack@google.com    try {
15813207Sgabeblack@google.com        for (auto p: allPorts)
15913207Sgabeblack@google.com            p->sc_port_base()->end_of_simulation();
16013191Sgabeblack@google.com        for (auto m: sc_gem5::allModules)
16113191Sgabeblack@google.com            m->endOfSimulation();
16213188Sgabeblack@google.com        for (auto c: sc_gem5::allChannels)
16313188Sgabeblack@google.com            c->sc_chan()->end_of_simulation();
16413188Sgabeblack@google.com    } catch (...) {
16513188Sgabeblack@google.com        ::sc_gem5::scheduler.throwToScMain();
16613059Sgabeblack@google.com    }
16712982Sgabeblack@google.com
16812990Sgabeblack@google.com    endComplete = true;
16912982Sgabeblack@google.com
17012990Sgabeblack@google.com    status(::sc_core::SC_STOPPED);
17112982Sgabeblack@google.com}
17212982Sgabeblack@google.com
17312982Sgabeblack@google.comvoid
17412953Sgabeblack@google.comKernel::t0Handler()
17512953Sgabeblack@google.com{
17613156Sgabeblack@google.com    if (stopAfterCallbacks) {
17713156Sgabeblack@google.com        scheduler.clear();
17813156Sgabeblack@google.com        ::sc_gem5::scheduler.initPhase();
17913156Sgabeblack@google.com        scheduler.scheduleStop(false);
18013156Sgabeblack@google.com    } else {
18113156Sgabeblack@google.com        ::sc_gem5::scheduler.initPhase();
18213156Sgabeblack@google.com        status(::sc_core::SC_RUNNING);
18313156Sgabeblack@google.com    }
18412837Sgabeblack@google.com}
18512837Sgabeblack@google.com
18612982Sgabeblack@google.comKernel *kernel;
18712837Sgabeblack@google.com
18812982Sgabeblack@google.com} // namespace sc_gem5
18912982Sgabeblack@google.com
19012982Sgabeblack@google.comsc_gem5::Kernel *
19112837Sgabeblack@google.comSystemC_KernelParams::create()
19212837Sgabeblack@google.com{
19312982Sgabeblack@google.com    panic_if(sc_gem5::kernel,
19412982Sgabeblack@google.com            "Only one systemc kernel object may be defined.\n");
19512982Sgabeblack@google.com    sc_gem5::kernel = new sc_gem5::Kernel(this);
19612982Sgabeblack@google.com    return sc_gem5::kernel;
19712837Sgabeblack@google.com}
198