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#include <string> 32 33#include "base/fiber.hh" 34#include "base/logging.hh" 35#include "base/types.hh" 36#include "sim/core.hh" 37#include "sim/eventq.hh" 38#include "sim/init.hh" 39#include "systemc/core/kernel.hh" 40#include "systemc/core/python.hh" 41#include "systemc/core/scheduler.hh" 42#include "systemc/ext/core/sc_main.hh" 43#include "systemc/ext/utils/sc_report_handler.hh" 44 45// A weak symbol to detect if sc_main has been defined, and if so where it is. 46[[gnu::weak]] int sc_main(int argc, char *argv[]); 47 48namespace sc_core 49{ 50 51namespace 52{ 53 54bool scMainCalled = false; 55 56int _argc = 0; 57char **_argv = NULL; 58 59class ScMainFiber : public Fiber 60{ 61 public: 62 std::string resultStr; 63 int resultInt; 64 65 ScMainFiber() : resultInt(1) {} 66 67 void 68 main() 69 { 70 if (::sc_main) { 71 try { 72 resultInt = ::sc_main(_argc, _argv); 73 if (resultInt) 74 resultStr = "sc_main returned non-zero"; 75 else 76 resultStr = "sc_main finished"; 77 // Make sure no systemc events/notifications are scheduled 78 // after sc_main returns. 79 } catch (const sc_report &r) { 80 // There was an exception nobody caught. 81 resultStr = r.what();
| 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#include <string> 32 33#include "base/fiber.hh" 34#include "base/logging.hh" 35#include "base/types.hh" 36#include "sim/core.hh" 37#include "sim/eventq.hh" 38#include "sim/init.hh" 39#include "systemc/core/kernel.hh" 40#include "systemc/core/python.hh" 41#include "systemc/core/scheduler.hh" 42#include "systemc/ext/core/sc_main.hh" 43#include "systemc/ext/utils/sc_report_handler.hh" 44 45// A weak symbol to detect if sc_main has been defined, and if so where it is. 46[[gnu::weak]] int sc_main(int argc, char *argv[]); 47 48namespace sc_core 49{ 50 51namespace 52{ 53 54bool scMainCalled = false; 55 56int _argc = 0; 57char **_argv = NULL; 58 59class ScMainFiber : public Fiber 60{ 61 public: 62 std::string resultStr; 63 int resultInt; 64 65 ScMainFiber() : resultInt(1) {} 66 67 void 68 main() 69 { 70 if (::sc_main) { 71 try { 72 resultInt = ::sc_main(_argc, _argv); 73 if (resultInt) 74 resultStr = "sc_main returned non-zero"; 75 else 76 resultStr = "sc_main finished"; 77 // Make sure no systemc events/notifications are scheduled 78 // after sc_main returns. 79 } catch (const sc_report &r) { 80 // There was an exception nobody caught. 81 resultStr = r.what();
|
82 } 83 ::sc_gem5::Kernel::scMainFinished(true); 84 ::sc_gem5::scheduler.clear(); 85 } else { 86 // If python tries to call sc_main but no sc_main was defined... 87 fatal("sc_main called but not defined.\n"); 88 } 89 } 90}; 91 92ScMainFiber scMainFiber; 93 94// This wrapper adapts the python version of sc_main to the c++ version. 95void 96sc_main(pybind11::args args) 97{ 98 panic_if(scMainCalled, "sc_main called more than once."); 99 100 _argc = args.size(); 101 _argv = new char *[_argc]; 102 103 // Initialize all the _argvs to NULL so we can delete [] them 104 // unconditionally. 105 for (int idx = 0; idx < _argc; idx++) 106 _argv[idx] = NULL; 107 108 // Attempt to convert all the arguments to strings. If that fails, clean 109 // up after ourselves. Also don't count this as a call to sc_main since 110 // we never got to the c++ version of that function. 111 try { 112 for (int idx = 0; idx < _argc; idx++) { 113 std::string arg = args[idx].cast<std::string>(); 114 _argv[idx] = new char[arg.length() + 1]; 115 strcpy(_argv[idx], arg.c_str()); 116 } 117 } catch (...) { 118 // If that didn't work for some reason (probably a conversion error) 119 // blow away _argv and _argc and pass on the exception. 120 for (int idx = 0; idx < _argc; idx++) 121 delete [] _argv[idx]; 122 delete [] _argv; 123 _argc = 0; 124 throw; 125 } 126 127 // At this point we're going to call the c++ sc_main, so we can't try 128 // again later. 129 scMainCalled = true; 130 131 scMainFiber.run(); 132} 133 134int 135sc_main_result_code() 136{ 137 return scMainFiber.resultInt; 138} 139 140std::string 141sc_main_result_str() 142{ 143 return scMainFiber.resultStr; 144} 145 146// Make our sc_main wrapper available in the internal _m5 python module under 147// the systemc submodule. 148 149struct InstallScMain : public ::sc_gem5::PythonInitFunc 150{ 151 void 152 run(pybind11::module &systemc) override 153 { 154 systemc.def("sc_main", &sc_main); 155 systemc.def("sc_main_result_code", &sc_main_result_code); 156 systemc.def("sc_main_result_str", &sc_main_result_str); 157 } 158} installScMain; 159 160sc_stop_mode _stop_mode = SC_STOP_FINISH_DELTA; 161 162} // anonymous namespace 163 164int 165sc_argc() 166{ 167 return _argc; 168} 169 170const char *const * 171sc_argv() 172{ 173 return _argv; 174} 175 176void 177sc_start() 178{ 179 Tick now = ::sc_gem5::scheduler.getCurTick(); 180 sc_start(sc_time::from_value(MaxTick - now), SC_EXIT_ON_STARVATION); 181} 182 183void 184sc_pause() 185{ 186 if (::sc_gem5::Kernel::status() == SC_RUNNING) 187 ::sc_gem5::scheduler.schedulePause(); 188} 189 190void 191sc_start(const sc_time &time, sc_starvation_policy p) 192{ 193 if (time.value() == 0) { 194 ::sc_gem5::scheduler.oneCycle(); 195 } else { 196 Tick now = ::sc_gem5::scheduler.getCurTick(); 197 if (MaxTick - now < time.value()) { 198 SC_REPORT_ERROR("(E544) simulation time value overflow, " 199 "simulation aborted", ""); 200 } 201 ::sc_gem5::scheduler.start(now + time.value(), p == SC_RUN_TO_TIME); 202 } 203} 204 205void 206sc_set_stop_mode(sc_stop_mode mode) 207{ 208 if (sc_is_running()) { 209 SC_REPORT_ERROR("attempt to set sc_stop mode " 210 "after start will be ignored", ""); 211 return; 212 } 213 _stop_mode = mode; 214} 215 216sc_stop_mode 217sc_get_stop_mode() 218{ 219 return _stop_mode; 220} 221 222void 223sc_stop() 224{ 225 if (::sc_gem5::Kernel::status() == SC_STOPPED) 226 return; 227 228 if ((sc_get_status() & SC_RUNNING)) { 229 bool finish_delta = (_stop_mode == SC_STOP_FINISH_DELTA); 230 ::sc_gem5::scheduler.scheduleStop(finish_delta); 231 } else { 232 ::sc_gem5::Kernel::stop(); 233 } 234} 235 236const sc_time & 237sc_time_stamp() 238{ 239 static sc_time tstamp; 240 Tick tick = ::sc_gem5::scheduler.getCurTick(); 241 //XXX We're assuming the systemc time resolution is in ps. 242 // If tick is zero, the time scale may not be fixed yet, and 243 // SimClock::Int::ps may be zero. 244 tstamp = sc_time::from_value(tick ? tick / SimClock::Int::ps : 0); 245 return tstamp; 246} 247 248sc_dt::uint64 249sc_delta_count() 250{ 251 return sc_gem5::scheduler.numCycles(); 252} 253 254bool 255sc_is_running() 256{ 257 return sc_get_status() & (SC_RUNNING | SC_PAUSED); 258} 259 260bool 261sc_pending_activity_at_current_time() 262{ 263 return ::sc_gem5::scheduler.pendingCurr(); 264} 265 266bool 267sc_pending_activity_at_future_time() 268{ 269 return ::sc_gem5::scheduler.pendingFuture(); 270} 271 272bool 273sc_pending_activity() 274{ 275 return sc_pending_activity_at_current_time() || 276 sc_pending_activity_at_future_time(); 277} 278 279sc_time 280sc_time_to_pending_activity() 281{ 282 return sc_time::from_value(::sc_gem5::scheduler.timeToPending()); 283} 284 285sc_status 286sc_get_status() 287{ 288 return ::sc_gem5::kernel ? ::sc_gem5::kernel->status() : SC_ELABORATION; 289} 290 291std::ostream & 292operator << (std::ostream &os, sc_status s) 293{ 294 switch (s) { 295 case SC_ELABORATION: 296 os << "SC_ELABORATION"; 297 break; 298 case SC_BEFORE_END_OF_ELABORATION: 299 os << "SC_BEFORE_END_OF_ELABORATION"; 300 break; 301 case SC_END_OF_ELABORATION: 302 os << "SC_END_OF_ELABORATION"; 303 break; 304 case SC_START_OF_SIMULATION: 305 os << "SC_START_OF_SIMULATION"; 306 break; 307 case SC_RUNNING: 308 os << "SC_RUNNING"; 309 break; 310 case SC_PAUSED: 311 os << "SC_PAUSED"; 312 break; 313 case SC_STOPPED: 314 os << "SC_STOPPED"; 315 break; 316 case SC_END_OF_SIMULATION: 317 os << "SC_END_OF_SIMULATION"; 318 break; 319 320 // Nonstandard 321 case SC_END_OF_INITIALIZATION: 322 os << "SC_END_OF_INITIALIZATION"; 323 break; 324 case SC_END_OF_UPDATE: 325 os << "SC_END_OF_UPDATE"; 326 break; 327 case SC_BEFORE_TIMESTEP: 328 os << "SC_BEFORE_TIMESTEP"; 329 break; 330 331 default: 332 if (s & SC_STATUS_ANY) { 333 const char *prefix = "("; 334 for (sc_status m = (sc_status)0x1; 335 m < SC_STATUS_ANY; m = (sc_status)(m << 1)) { 336 if (m & s) { 337 os << prefix; 338 prefix = "|"; 339 os << m; 340 } 341 } 342 os << ")"; 343 } else { 344 ccprintf(os, "%#x", s); 345 } 346 } 347 348 return os; 349} 350 351} // namespace sc_core
| 86 } 87 ::sc_gem5::Kernel::scMainFinished(true); 88 ::sc_gem5::scheduler.clear(); 89 } else { 90 // If python tries to call sc_main but no sc_main was defined... 91 fatal("sc_main called but not defined.\n"); 92 } 93 } 94}; 95 96ScMainFiber scMainFiber; 97 98// This wrapper adapts the python version of sc_main to the c++ version. 99void 100sc_main(pybind11::args args) 101{ 102 panic_if(scMainCalled, "sc_main called more than once."); 103 104 _argc = args.size(); 105 _argv = new char *[_argc]; 106 107 // Initialize all the _argvs to NULL so we can delete [] them 108 // unconditionally. 109 for (int idx = 0; idx < _argc; idx++) 110 _argv[idx] = NULL; 111 112 // Attempt to convert all the arguments to strings. If that fails, clean 113 // up after ourselves. Also don't count this as a call to sc_main since 114 // we never got to the c++ version of that function. 115 try { 116 for (int idx = 0; idx < _argc; idx++) { 117 std::string arg = args[idx].cast<std::string>(); 118 _argv[idx] = new char[arg.length() + 1]; 119 strcpy(_argv[idx], arg.c_str()); 120 } 121 } catch (...) { 122 // If that didn't work for some reason (probably a conversion error) 123 // blow away _argv and _argc and pass on the exception. 124 for (int idx = 0; idx < _argc; idx++) 125 delete [] _argv[idx]; 126 delete [] _argv; 127 _argc = 0; 128 throw; 129 } 130 131 // At this point we're going to call the c++ sc_main, so we can't try 132 // again later. 133 scMainCalled = true; 134 135 scMainFiber.run(); 136} 137 138int 139sc_main_result_code() 140{ 141 return scMainFiber.resultInt; 142} 143 144std::string 145sc_main_result_str() 146{ 147 return scMainFiber.resultStr; 148} 149 150// Make our sc_main wrapper available in the internal _m5 python module under 151// the systemc submodule. 152 153struct InstallScMain : public ::sc_gem5::PythonInitFunc 154{ 155 void 156 run(pybind11::module &systemc) override 157 { 158 systemc.def("sc_main", &sc_main); 159 systemc.def("sc_main_result_code", &sc_main_result_code); 160 systemc.def("sc_main_result_str", &sc_main_result_str); 161 } 162} installScMain; 163 164sc_stop_mode _stop_mode = SC_STOP_FINISH_DELTA; 165 166} // anonymous namespace 167 168int 169sc_argc() 170{ 171 return _argc; 172} 173 174const char *const * 175sc_argv() 176{ 177 return _argv; 178} 179 180void 181sc_start() 182{ 183 Tick now = ::sc_gem5::scheduler.getCurTick(); 184 sc_start(sc_time::from_value(MaxTick - now), SC_EXIT_ON_STARVATION); 185} 186 187void 188sc_pause() 189{ 190 if (::sc_gem5::Kernel::status() == SC_RUNNING) 191 ::sc_gem5::scheduler.schedulePause(); 192} 193 194void 195sc_start(const sc_time &time, sc_starvation_policy p) 196{ 197 if (time.value() == 0) { 198 ::sc_gem5::scheduler.oneCycle(); 199 } else { 200 Tick now = ::sc_gem5::scheduler.getCurTick(); 201 if (MaxTick - now < time.value()) { 202 SC_REPORT_ERROR("(E544) simulation time value overflow, " 203 "simulation aborted", ""); 204 } 205 ::sc_gem5::scheduler.start(now + time.value(), p == SC_RUN_TO_TIME); 206 } 207} 208 209void 210sc_set_stop_mode(sc_stop_mode mode) 211{ 212 if (sc_is_running()) { 213 SC_REPORT_ERROR("attempt to set sc_stop mode " 214 "after start will be ignored", ""); 215 return; 216 } 217 _stop_mode = mode; 218} 219 220sc_stop_mode 221sc_get_stop_mode() 222{ 223 return _stop_mode; 224} 225 226void 227sc_stop() 228{ 229 if (::sc_gem5::Kernel::status() == SC_STOPPED) 230 return; 231 232 if ((sc_get_status() & SC_RUNNING)) { 233 bool finish_delta = (_stop_mode == SC_STOP_FINISH_DELTA); 234 ::sc_gem5::scheduler.scheduleStop(finish_delta); 235 } else { 236 ::sc_gem5::Kernel::stop(); 237 } 238} 239 240const sc_time & 241sc_time_stamp() 242{ 243 static sc_time tstamp; 244 Tick tick = ::sc_gem5::scheduler.getCurTick(); 245 //XXX We're assuming the systemc time resolution is in ps. 246 // If tick is zero, the time scale may not be fixed yet, and 247 // SimClock::Int::ps may be zero. 248 tstamp = sc_time::from_value(tick ? tick / SimClock::Int::ps : 0); 249 return tstamp; 250} 251 252sc_dt::uint64 253sc_delta_count() 254{ 255 return sc_gem5::scheduler.numCycles(); 256} 257 258bool 259sc_is_running() 260{ 261 return sc_get_status() & (SC_RUNNING | SC_PAUSED); 262} 263 264bool 265sc_pending_activity_at_current_time() 266{ 267 return ::sc_gem5::scheduler.pendingCurr(); 268} 269 270bool 271sc_pending_activity_at_future_time() 272{ 273 return ::sc_gem5::scheduler.pendingFuture(); 274} 275 276bool 277sc_pending_activity() 278{ 279 return sc_pending_activity_at_current_time() || 280 sc_pending_activity_at_future_time(); 281} 282 283sc_time 284sc_time_to_pending_activity() 285{ 286 return sc_time::from_value(::sc_gem5::scheduler.timeToPending()); 287} 288 289sc_status 290sc_get_status() 291{ 292 return ::sc_gem5::kernel ? ::sc_gem5::kernel->status() : SC_ELABORATION; 293} 294 295std::ostream & 296operator << (std::ostream &os, sc_status s) 297{ 298 switch (s) { 299 case SC_ELABORATION: 300 os << "SC_ELABORATION"; 301 break; 302 case SC_BEFORE_END_OF_ELABORATION: 303 os << "SC_BEFORE_END_OF_ELABORATION"; 304 break; 305 case SC_END_OF_ELABORATION: 306 os << "SC_END_OF_ELABORATION"; 307 break; 308 case SC_START_OF_SIMULATION: 309 os << "SC_START_OF_SIMULATION"; 310 break; 311 case SC_RUNNING: 312 os << "SC_RUNNING"; 313 break; 314 case SC_PAUSED: 315 os << "SC_PAUSED"; 316 break; 317 case SC_STOPPED: 318 os << "SC_STOPPED"; 319 break; 320 case SC_END_OF_SIMULATION: 321 os << "SC_END_OF_SIMULATION"; 322 break; 323 324 // Nonstandard 325 case SC_END_OF_INITIALIZATION: 326 os << "SC_END_OF_INITIALIZATION"; 327 break; 328 case SC_END_OF_UPDATE: 329 os << "SC_END_OF_UPDATE"; 330 break; 331 case SC_BEFORE_TIMESTEP: 332 os << "SC_BEFORE_TIMESTEP"; 333 break; 334 335 default: 336 if (s & SC_STATUS_ANY) { 337 const char *prefix = "("; 338 for (sc_status m = (sc_status)0x1; 339 m < SC_STATUS_ANY; m = (sc_status)(m << 1)) { 340 if (m & s) { 341 os << prefix; 342 prefix = "|"; 343 os << m; 344 } 345 } 346 os << ")"; 347 } else { 348 ccprintf(os, "%#x", s); 349 } 350 } 351 352 return os; 353} 354 355} // namespace sc_core
|