sc_main.cc revision 12861:6cd674bd189a
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/logging.hh" 33#include "python/pybind11/pybind.hh" 34#include "sim/init.hh" 35#include "systemc/ext/core/sc_main.hh" 36#include "systemc/ext/utils/sc_report_handler.hh" 37 38// A default version of this function in case one isn't otherwise defined. 39// This ensures everything will link properly whether or not the user defined 40// a custom sc_main function. If they didn't but still try to call it, throw 41// an error and die. 42[[gnu::weak]] int 43sc_main(int argc, char *argv[]) 44{ 45 // If python attempts to call sc_main but no sc_main was defined... 46 fatal("sc_main called but not defined.\n"); 47} 48 49namespace sc_core 50{ 51 52namespace 53{ 54 55bool scMainCalled = false; 56 57int _argc = 0; 58char **_argv = NULL; 59 60// This wrapper adapts the python version of sc_main to the c++ version. 61void 62sc_main(pybind11::args args) 63{ 64 panic_if(scMainCalled, "sc_main called more than once."); 65 66 _argc = args.size(); 67 _argv = new char *[_argc]; 68 69 // Initialize all the _argvs to NULL so we can delete [] them 70 // unconditionally. 71 for (int idx = 0; idx < _argc; idx++) 72 _argv[idx] = NULL; 73 74 // Attempt to convert all the arguments to strings. If that fails, clean 75 // up after ourselves. Also don't count this as a call to sc_main since 76 // we never got to the c++ version of that function. 77 try { 78 for (int idx = 0; idx < _argc; idx++) { 79 std::string arg = args[idx].cast<std::string>(); 80 _argv[idx] = new char[arg.length() + 1]; 81 strcpy(_argv[idx], arg.c_str()); 82 } 83 } catch (...) { 84 // If that didn't work for some reason (probably a conversion error) 85 // blow away _argv and _argc and pass on the exception. 86 for (int idx = 0; idx < _argc; idx++) 87 delete [] _argv[idx]; 88 delete [] _argv; 89 _argc = 0; 90 throw; 91 } 92 93 // At this point we're going to call the c++ sc_main, so we can't try 94 // again later. 95 scMainCalled = true; 96 97 //TODO Start a new fiber to call sc_main from. 98 ::sc_main(_argc, _argv); 99} 100 101// Make our sc_main wrapper available in the internal _m5 python module under 102// the systemc submodule. 103void 104systemc_pybind(pybind11::module &m_internal) 105{ 106 pybind11::module m = m_internal.def_submodule("systemc"); 107 m.def("sc_main", &sc_main); 108} 109EmbeddedPyBind embed_("systemc", &systemc_pybind); 110 111sc_stop_mode _stop_mode = SC_STOP_FINISH_DELTA; 112sc_status _status = SC_ELABORATION; 113 114uint64_t _deltaCycles = 0; 115 116} // anonymous namespace 117 118int 119sc_argc() 120{ 121 return _argc; 122} 123 124const char *const * 125sc_argv() 126{ 127 return _argv; 128} 129 130void 131sc_start() 132{ 133 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 134} 135 136void 137sc_pause() 138{ 139 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 140} 141 142void 143sc_start(const sc_time &time, sc_starvation_policy p) 144{ 145 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 146} 147 148void 149sc_set_stop_mode(sc_stop_mode mode) 150{ 151 if (sc_is_running()) { 152 SC_REPORT_ERROR("attempt to set sc_stop mode " 153 "after start will be ignored", ""); 154 return; 155 } 156 _stop_mode = mode; 157} 158 159sc_stop_mode 160sc_get_stop_mode() 161{ 162 return _stop_mode; 163} 164 165void 166sc_stop() 167{ 168 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 169} 170 171const sc_time & 172sc_time_stamp() 173{ 174 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 175 return *(sc_time *)nullptr; 176} 177 178sc_dt::uint64 179sc_delta_count() 180{ 181 return _deltaCycles; 182} 183 184bool 185sc_is_running() 186{ 187 return _status & (SC_RUNNING | SC_PAUSED); 188} 189 190bool 191sc_pending_activity_at_current_time() 192{ 193 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 194 return false; 195} 196 197bool 198sc_pending_activity_at_future_time() 199{ 200 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 201 return false; 202} 203 204bool 205sc_pending_activity() 206{ 207 return sc_pending_activity_at_current_time() || 208 sc_pending_activity_at_future_time(); 209} 210 211sc_time 212sc_time_to_pending_activity() 213{ 214 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 215 return sc_time(); 216} 217 218sc_status 219sc_get_status() 220{ 221 return _status; 222} 223 224} // namespace sc_core 225