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