sc_main.cc revision 12961
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 <cstring> 31 32#include "base/fiber.hh" 33#include "base/logging.hh" 34#include "base/types.hh" 35#include "python/pybind11/pybind.hh" 36#include "sim/core.hh" 37#include "sim/eventq.hh" 38#include "sim/init.hh" 39#include "systemc/core/scheduler.hh" 40#include "systemc/ext/core/sc_main.hh" 41#include "systemc/ext/utils/sc_report_handler.hh" 42 43// A weak symbol to detect if sc_main has been defined, and if so where it is. 44[[gnu::weak]] int sc_main(int argc, char *argv[]); 45 46namespace sc_core 47{ 48 49namespace 50{ 51 52bool scMainCalled = false; 53 54int _argc = 0; 55char **_argv = NULL; 56 57class ScMainFiber : public Fiber 58{ 59 void 60 main() 61 { 62 if (::sc_main) { 63 ::sc_main(_argc, _argv); 64 } else { 65 // If python tries to call sc_main but no sc_main was defined... 66 fatal("sc_main called but not defined.\n"); 67 } 68 } 69}; 70 71ScMainFiber scMainFiber; 72 73// This wrapper adapts the python version of sc_main to the c++ version. 74void 75sc_main(pybind11::args args) 76{ 77 panic_if(scMainCalled, "sc_main called more than once."); 78 79 _argc = args.size(); 80 _argv = new char *[_argc]; 81 82 // Initialize all the _argvs to NULL so we can delete [] them 83 // unconditionally. 84 for (int idx = 0; idx < _argc; idx++) 85 _argv[idx] = NULL; 86 87 // Attempt to convert all the arguments to strings. If that fails, clean 88 // up after ourselves. Also don't count this as a call to sc_main since 89 // we never got to the c++ version of that function. 90 try { 91 for (int idx = 0; idx < _argc; idx++) { 92 std::string arg = args[idx].cast<std::string>(); 93 _argv[idx] = new char[arg.length() + 1]; 94 strcpy(_argv[idx], arg.c_str()); 95 } 96 } catch (...) { 97 // If that didn't work for some reason (probably a conversion error) 98 // blow away _argv and _argc and pass on the exception. 99 for (int idx = 0; idx < _argc; idx++) 100 delete [] _argv[idx]; 101 delete [] _argv; 102 _argc = 0; 103 throw; 104 } 105 106 // At this point we're going to call the c++ sc_main, so we can't try 107 // again later. 108 scMainCalled = true; 109 110 scMainFiber.run(); 111} 112 113// Make our sc_main wrapper available in the internal _m5 python module under 114// the systemc submodule. 115void 116systemc_pybind(pybind11::module &m_internal) 117{ 118 pybind11::module m = m_internal.def_submodule("systemc"); 119 m.def("sc_main", &sc_main); 120} 121EmbeddedPyBind embed_("systemc", &systemc_pybind); 122 123sc_stop_mode _stop_mode = SC_STOP_FINISH_DELTA; 124sc_status _status = SC_ELABORATION; 125 126} // anonymous namespace 127 128int 129sc_argc() 130{ 131 return _argc; 132} 133 134const char *const * 135sc_argv() 136{ 137 return _argv; 138} 139 140void 141sc_start() 142{ 143 Tick now = curEventQueue() ? curEventQueue()->getCurTick() : 0; 144 sc_start(sc_time::from_value(MaxTick - now), SC_EXIT_ON_STARVATION); 145} 146 147void 148sc_pause() 149{ 150 if (_status == SC_RUNNING) 151 ::sc_gem5::scheduler.schedulePause(); 152} 153 154void 155sc_start(const sc_time &time, sc_starvation_policy p) 156{ 157 _status = SC_RUNNING; 158 159 Tick now = curEventQueue() ? curEventQueue()->getCurTick() : 0; 160 ::sc_gem5::scheduler.start(now + time.value(), p == SC_RUN_TO_TIME); 161 162 if (::sc_gem5::scheduler.paused()) 163 _status = SC_PAUSED; 164 else if (::sc_gem5::scheduler.stopped()) 165 _status = SC_STOPPED; 166} 167 168void 169sc_set_stop_mode(sc_stop_mode mode) 170{ 171 if (sc_is_running()) { 172 SC_REPORT_ERROR("attempt to set sc_stop mode " 173 "after start will be ignored", ""); 174 return; 175 } 176 _stop_mode = mode; 177} 178 179sc_stop_mode 180sc_get_stop_mode() 181{ 182 return _stop_mode; 183} 184 185void 186sc_stop() 187{ 188 if (_status == SC_STOPPED) 189 return; 190 191 if (sc_is_running()) { 192 bool finish_delta = (_stop_mode == SC_STOP_FINISH_DELTA); 193 ::sc_gem5::scheduler.scheduleStop(finish_delta); 194 } else { 195 //XXX Should stop if in one of the various elaboration callbacks. 196 } 197} 198 199const sc_time & 200sc_time_stamp() 201{ 202 static sc_time tstamp; 203 Tick tick = sc_gem5::scheduler.eventQueue().getCurTick(); 204 //XXX We're assuming the systemc time resolution is in ps. 205 tstamp = sc_time::from_value(tick / SimClock::Int::ps); 206 return tstamp; 207} 208 209sc_dt::uint64 210sc_delta_count() 211{ 212 return sc_gem5::scheduler.numCycles(); 213} 214 215bool 216sc_is_running() 217{ 218 return _status & (SC_RUNNING | SC_PAUSED); 219} 220 221bool 222sc_pending_activity_at_current_time() 223{ 224 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 225 return false; 226} 227 228bool 229sc_pending_activity_at_future_time() 230{ 231 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 232 return false; 233} 234 235bool 236sc_pending_activity() 237{ 238 return sc_pending_activity_at_current_time() || 239 sc_pending_activity_at_future_time(); 240} 241 242sc_time 243sc_time_to_pending_activity() 244{ 245 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 246 return sc_time(); 247} 248 249sc_status 250sc_get_status() 251{ 252 return _status; 253} 254 255} // namespace sc_core 256