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