kernel.cc revision 13191
1/* 2 * Copyright 2018 Google, Inc. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer; 8 * redistributions in binary form must reproduce the above copyright 9 * notice, this list of conditions and the following disclaimer in the 10 * documentation and/or other materials provided with the distribution; 11 * neither the name of the copyright holders nor the names of its 12 * contributors may be used to endorse or promote products derived from 13 * this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 * Authors: Gabe Black 28 */ 29 30#include "systemc/core/kernel.hh" 31 32#include "base/logging.hh" 33#include "systemc/core/channel.hh" 34#include "systemc/core/module.hh" 35#include "systemc/core/scheduler.hh" 36 37namespace sc_gem5 38{ 39 40namespace 41{ 42 43bool scMainDone = false; 44bool stopAfterCallbacks = false; 45bool startComplete = false; 46bool endComplete = false; 47 48sc_core::sc_status _status = sc_core::SC_ELABORATION; 49 50} // anonymous namespace 51 52bool Kernel::startOfSimulationComplete() { return startComplete; } 53bool Kernel::endOfSimulationComplete() { return endComplete; } 54 55bool Kernel::scMainFinished() { return scMainDone; } 56void Kernel::scMainFinished(bool finished) { scMainDone = finished; } 57 58sc_core::sc_status Kernel::status() { return _status; } 59void Kernel::status(sc_core::sc_status s) { _status = s; } 60 61Kernel::Kernel(Params *params) : 62 SimObject(params), t0Event(this, false, EventBase::Default_Pri - 1) 63{ 64 // Install ourselves as the scheduler's event manager. 65 ::sc_gem5::scheduler.setEventQueue(eventQueue()); 66} 67 68void 69Kernel::init() 70{ 71 if (scMainDone) 72 return; 73 74 if (stopAfterCallbacks) 75 fatal("Simulation called sc_stop during elaboration.\n"); 76 77 status(::sc_core::SC_BEFORE_END_OF_ELABORATION); 78 for (auto m: sc_gem5::allModules) 79 m->beforeEndOfElaboration(); 80 for (auto c: sc_gem5::allChannels) 81 c->sc_chan()->before_end_of_elaboration(); 82} 83 84void 85Kernel::regStats() 86{ 87 if (scMainDone || stopAfterCallbacks) 88 return; 89 90 try { 91 for (auto m: sc_gem5::allModules) 92 for (auto p: m->ports) 93 p->_gem5Finalize(); 94 95 status(::sc_core::SC_END_OF_ELABORATION); 96 for (auto m: sc_gem5::allModules) 97 m->endOfElaboration(); 98 for (auto c: sc_gem5::allChannels) 99 c->sc_chan()->end_of_elaboration(); 100 } catch (...) { 101 ::sc_gem5::scheduler.throwToScMain(); 102 } 103 104 ::sc_gem5::scheduler.elaborationDone(true); 105} 106 107void 108Kernel::startup() 109{ 110 if (scMainDone) 111 return; 112 113 schedule(t0Event, curTick()); 114 115 if (stopAfterCallbacks) 116 return; 117 118 try { 119 status(::sc_core::SC_START_OF_SIMULATION); 120 for (auto m: sc_gem5::allModules) 121 m->startOfSimulation(); 122 for (auto c: sc_gem5::allChannels) 123 c->sc_chan()->start_of_simulation(); 124 } catch (...) { 125 ::sc_gem5::scheduler.throwToScMain(); 126 } 127 128 startComplete = true; 129 130 if (stopAfterCallbacks) 131 stopWork(); 132 133 kernel->status(::sc_core::SC_RUNNING); 134} 135 136void 137Kernel::stop() 138{ 139 if (status() < ::sc_core::SC_RUNNING) 140 stopAfterCallbacks = true; 141 else 142 stopWork(); 143} 144 145void 146Kernel::stopWork() 147{ 148 status(::sc_core::SC_END_OF_SIMULATION); 149 try { 150 for (auto m: sc_gem5::allModules) 151 m->endOfSimulation(); 152 for (auto c: sc_gem5::allChannels) 153 c->sc_chan()->end_of_simulation(); 154 } catch (...) { 155 ::sc_gem5::scheduler.throwToScMain(); 156 } 157 158 endComplete = true; 159 160 status(::sc_core::SC_STOPPED); 161} 162 163void 164Kernel::t0Handler() 165{ 166 if (stopAfterCallbacks) { 167 scheduler.clear(); 168 ::sc_gem5::scheduler.initPhase(); 169 scheduler.scheduleStop(false); 170 } else { 171 ::sc_gem5::scheduler.initPhase(); 172 status(::sc_core::SC_RUNNING); 173 } 174} 175 176Kernel *kernel; 177 178} // namespace sc_gem5 179 180sc_gem5::Kernel * 181SystemC_KernelParams::create() 182{ 183 panic_if(sc_gem5::kernel, 184 "Only one systemc kernel object may be defined.\n"); 185 sc_gem5::kernel = new sc_gem5::Kernel(this); 186 return sc_gem5::kernel; 187} 188