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