sc_main.cc revision 12956
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
126Tick _max_tick = MaxTick;
127sc_starvation_policy _starvation = SC_EXIT_ON_STARVATION;
128
129} // anonymous namespace
130
131int
132sc_argc()
133{
134    return _argc;
135}
136
137const char *const *
138sc_argv()
139{
140    return _argv;
141}
142
143void
144sc_start()
145{
146    _max_tick = MaxTick;
147    _starvation = SC_EXIT_ON_STARVATION;
148
149    // Switch back gem5.
150    Fiber::primaryFiber()->run();
151}
152
153void
154sc_pause()
155{
156    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
157}
158
159void
160sc_start(const sc_time &time, sc_starvation_policy p)
161{
162    Tick now = curEventQueue() ? curEventQueue()->getCurTick() : 0;
163    _max_tick = now + time.value();
164    _starvation = p;
165
166    // Switch back to gem5.
167    Fiber::primaryFiber()->run();
168}
169
170void
171sc_set_stop_mode(sc_stop_mode mode)
172{
173    if (sc_is_running()) {
174        SC_REPORT_ERROR("attempt to set sc_stop mode "
175                        "after start will be ignored", "");
176        return;
177    }
178    _stop_mode = mode;
179}
180
181sc_stop_mode
182sc_get_stop_mode()
183{
184    return _stop_mode;
185}
186
187void
188sc_stop()
189{
190    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
191}
192
193const sc_time &
194sc_time_stamp()
195{
196    static sc_time tstamp;
197    Tick tick = sc_gem5::scheduler.eventQueue().getCurTick();
198    //XXX We're assuming the systemc time resolution is in ps.
199    tstamp = sc_time::from_value(tick / SimClock::Int::ps);
200    return tstamp;
201}
202
203sc_dt::uint64
204sc_delta_count()
205{
206    return sc_gem5::scheduler.numCycles();
207}
208
209bool
210sc_is_running()
211{
212    return _status & (SC_RUNNING | SC_PAUSED);
213}
214
215bool
216sc_pending_activity_at_current_time()
217{
218    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
219    return false;
220}
221
222bool
223sc_pending_activity_at_future_time()
224{
225    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
226    return false;
227}
228
229bool
230sc_pending_activity()
231{
232    return sc_pending_activity_at_current_time() ||
233           sc_pending_activity_at_future_time();
234}
235
236sc_time
237sc_time_to_pending_activity()
238{
239    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
240    return sc_time();
241}
242
243sc_status
244sc_get_status()
245{
246    return _status;
247}
248
249} // namespace sc_core
250