sc_main.cc (12990:19d91b53e04e) sc_main.cc (13038:7bf84150855b)
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"
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/kernel.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.
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.
116void
117systemc_pybind(pybind11::module &m_internal)
116
117struct InstallScMain : public ::sc_gem5::PythonInitFunc
118{
118{
119 pybind11::module m = m_internal.def_submodule("systemc");
120 m.def("sc_main", &sc_main);
121}
122EmbeddedPyBind embed_("systemc", &systemc_pybind);
119 void
120 run(pybind11::module &systemc) override
121 {
122 systemc.def("sc_main", &sc_main);
123 }
124} installScMain;
123
124sc_stop_mode _stop_mode = SC_STOP_FINISH_DELTA;
125
126} // anonymous namespace
127
128int
129sc_argc()
130{
131 return _argc;
132}
133
134const char *const *
135sc_argv()
136{
137 return _argv;
138}
139
140void
141sc_start()
142{
143 Tick now = ::sc_gem5::scheduler.getCurTick();
144 sc_start(sc_time::from_value(MaxTick - now), SC_EXIT_ON_STARVATION);
145}
146
147void
148sc_pause()
149{
150 if (::sc_gem5::Kernel::status() == SC_RUNNING)
151 ::sc_gem5::scheduler.schedulePause();
152}
153
154void
155sc_start(const sc_time &time, sc_starvation_policy p)
156{
157 Tick now = ::sc_gem5::scheduler.getCurTick();
158 ::sc_gem5::scheduler.start(now + time.value(), p == SC_RUN_TO_TIME);
159}
160
161void
162sc_set_stop_mode(sc_stop_mode mode)
163{
164 if (sc_is_running()) {
165 SC_REPORT_ERROR("attempt to set sc_stop mode "
166 "after start will be ignored", "");
167 return;
168 }
169 _stop_mode = mode;
170}
171
172sc_stop_mode
173sc_get_stop_mode()
174{
175 return _stop_mode;
176}
177
178void
179sc_stop()
180{
181 if (::sc_gem5::Kernel::status() == SC_STOPPED)
182 return;
183
184 if (sc_is_running()) {
185 bool finish_delta = (_stop_mode == SC_STOP_FINISH_DELTA);
186 ::sc_gem5::scheduler.scheduleStop(finish_delta);
187 } else {
188 ::sc_gem5::Kernel::stop();
189 }
190}
191
192const sc_time &
193sc_time_stamp()
194{
195 static sc_time tstamp;
196 Tick tick = ::sc_gem5::scheduler.getCurTick();
197 //XXX We're assuming the systemc time resolution is in ps.
198 tstamp = sc_time::from_value(tick / SimClock::Int::ps);
199 return tstamp;
200}
201
202sc_dt::uint64
203sc_delta_count()
204{
205 return sc_gem5::scheduler.numCycles();
206}
207
208bool
209sc_is_running()
210{
211 return sc_get_status() & (SC_RUNNING | SC_PAUSED);
212}
213
214bool
215sc_pending_activity_at_current_time()
216{
217 return ::sc_gem5::scheduler.pendingCurr();
218}
219
220bool
221sc_pending_activity_at_future_time()
222{
223 return ::sc_gem5::scheduler.pendingFuture();
224}
225
226bool
227sc_pending_activity()
228{
229 return sc_pending_activity_at_current_time() ||
230 sc_pending_activity_at_future_time();
231}
232
233sc_time
234sc_time_to_pending_activity()
235{
236 return sc_time::from_value(::sc_gem5::scheduler.timeToPending());
237}
238
239sc_status
240sc_get_status()
241{
242 return ::sc_gem5::kernel ? ::sc_gem5::kernel->status() : SC_ELABORATION;
243}
244
245} // namespace sc_core
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 Tick now = ::sc_gem5::scheduler.getCurTick();
160 ::sc_gem5::scheduler.start(now + time.value(), p == SC_RUN_TO_TIME);
161}
162
163void
164sc_set_stop_mode(sc_stop_mode mode)
165{
166 if (sc_is_running()) {
167 SC_REPORT_ERROR("attempt to set sc_stop mode "
168 "after start will be ignored", "");
169 return;
170 }
171 _stop_mode = mode;
172}
173
174sc_stop_mode
175sc_get_stop_mode()
176{
177 return _stop_mode;
178}
179
180void
181sc_stop()
182{
183 if (::sc_gem5::Kernel::status() == SC_STOPPED)
184 return;
185
186 if (sc_is_running()) {
187 bool finish_delta = (_stop_mode == SC_STOP_FINISH_DELTA);
188 ::sc_gem5::scheduler.scheduleStop(finish_delta);
189 } else {
190 ::sc_gem5::Kernel::stop();
191 }
192}
193
194const sc_time &
195sc_time_stamp()
196{
197 static sc_time tstamp;
198 Tick tick = ::sc_gem5::scheduler.getCurTick();
199 //XXX We're assuming the systemc time resolution is in ps.
200 tstamp = sc_time::from_value(tick / SimClock::Int::ps);
201 return tstamp;
202}
203
204sc_dt::uint64
205sc_delta_count()
206{
207 return sc_gem5::scheduler.numCycles();
208}
209
210bool
211sc_is_running()
212{
213 return sc_get_status() & (SC_RUNNING | SC_PAUSED);
214}
215
216bool
217sc_pending_activity_at_current_time()
218{
219 return ::sc_gem5::scheduler.pendingCurr();
220}
221
222bool
223sc_pending_activity_at_future_time()
224{
225 return ::sc_gem5::scheduler.pendingFuture();
226}
227
228bool
229sc_pending_activity()
230{
231 return sc_pending_activity_at_current_time() ||
232 sc_pending_activity_at_future_time();
233}
234
235sc_time
236sc_time_to_pending_activity()
237{
238 return sc_time::from_value(::sc_gem5::scheduler.timeToPending());
239}
240
241sc_status
242sc_get_status()
243{
244 return ::sc_gem5::kernel ? ::sc_gem5::kernel->status() : SC_ELABORATION;
245}
246
247} // namespace sc_core