sc_time.cc (13335:299a16ef8e3c) sc_time.cc (13410:a03275d35b40)
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

--- 13 unchanged lines hidden (view full) ---

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

--- 13 unchanged lines hidden (view full) ---

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 <cmath>
31#include <cstring>
30#include <sstream>
31#include <vector>
32
33#include "base/types.hh"
32#include <sstream>
33#include <vector>
34
35#include "base/types.hh"
34#include "python/pybind11/pybind.hh"
35#include "sim/core.hh"
36#include "sim/core.hh"
36#include "systemc/core/python.hh"
37#include "systemc/core/time.hh"
38#include "systemc/ext/core/messages.hh"
39#include "systemc/ext/core/sc_main.hh"
40#include "systemc/ext/core/sc_time.hh"
41#include "systemc/ext/utils/sc_report_handler.hh"
42
43namespace sc_core
44{
45
46namespace
47{
48
37#include "systemc/core/time.hh"
38#include "systemc/ext/core/messages.hh"
39#include "systemc/ext/core/sc_main.hh"
40#include "systemc/ext/core/sc_time.hh"
41#include "systemc/ext/utils/sc_report_handler.hh"
42
43namespace sc_core
44{
45
46namespace
47{
48
49bool timeFixed = false;
50bool pythonReady = false;
51
52struct SetInfo
53{
54 SetInfo(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu) :
55 time(time), d(d), tu(tu)
56 {}
57
58 ::sc_core::sc_time *time;
59 double d;
60 ::sc_core::sc_time_unit tu;
61};
62std::vector<SetInfo> toSet;
63
64void
49void
65setWork(sc_time *time, double d, ::sc_core::sc_time_unit tu)
50set(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu)
66{
51{
52 if (d != 0)
53 fixClockFrequency();
54
67 double scale = sc_gem5::TimeUnitScale[tu] * SimClock::Float::s;
68 // Accellera claims there is a linux bug, and that these next two
69 // lines work around them.
70 volatile double tmp = d * scale + 0.5;
71 *time = sc_time::from_value(static_cast<uint64_t>(tmp));
72}
73
55 double scale = sc_gem5::TimeUnitScale[tu] * SimClock::Float::s;
56 // Accellera claims there is a linux bug, and that these next two
57 // lines work around them.
58 volatile double tmp = d * scale + 0.5;
59 *time = sc_time::from_value(static_cast<uint64_t>(tmp));
60}
61
74void
75fixTime()
76{
77 auto ticks = pybind11::module::import("m5.ticks");
78 auto fix_global_frequency = ticks.attr("fixGlobalFrequency");
79 fix_global_frequency();
80
81 for (auto &t: toSet)
82 setWork(t.time, t.d, t.tu);
83 toSet.clear();
84}
85
86void
87attemptToFixTime()
88{
89 // Only fix time once.
90 if (!timeFixed) {
91 timeFixed = true;
92
93 // If we've run, python is working and we haven't fixed time yet.
94 if (pythonReady)
95 fixTime();
96 }
97}
98
99void
100setGlobalFrequency(Tick ticks_per_second)
101{
102 auto ticks = pybind11::module::import("m5.ticks");
103 auto set_global_frequency = ticks.attr("setGlobalFrequency");
104 set_global_frequency(ticks_per_second);
105 fixTime();
106}
107
108void
109set(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu)
110{
111 if (d != 0)
112 attemptToFixTime();
113 if (pythonReady) {
114 // Time should be working. Set up this sc_time.
115 setWork(time, d, tu);
116 } else {
117 // Time isn't set up yet. Defer setting up this sc_time.
118 toSet.emplace_back(time, d, tu);
119 }
120}
121
122class TimeSetter : public ::sc_gem5::PythonReadyFunc
123{
124 public:
125 TimeSetter() : ::sc_gem5::PythonReadyFunc() {}
126
127 void
128 run() override
129 {
130 // Record that we've run and python/pybind should be usable.
131 pythonReady = true;
132
133 // If time is already fixed, let python know.
134 if (timeFixed)
135 fixTime();
136 }
137} timeSetter;
138
139double defaultUnit = 1.0e-9;
140
141} // anonymous namespace
142
143sc_time::sc_time() : val(0) {}
144
145sc_time::sc_time(double d, sc_time_unit tu)
146{

--- 137 unchanged lines hidden (view full) ---

284{
285 os << sc_time_tuple(*this).to_string();
286}
287
288sc_time
289sc_time::from_value(sc_dt::uint64 u)
290{
291 if (u)
62double defaultUnit = 1.0e-9;
63
64} // anonymous namespace
65
66sc_time::sc_time() : val(0) {}
67
68sc_time::sc_time(double d, sc_time_unit tu)
69{

--- 137 unchanged lines hidden (view full) ---

207{
208 os << sc_time_tuple(*this).to_string();
209}
210
211sc_time
212sc_time::from_value(sc_dt::uint64 u)
213{
214 if (u)
292 attemptToFixTime();
215 fixClockFrequency();
293 sc_time t;
294 t.val = u;
295 return t;
296}
297
298sc_time
299sc_time::from_seconds(double d)
300{

--- 82 unchanged lines hidden (view full) ---

383 SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_, "simulation running");
384
385 static bool specified = false;
386 if (specified)
387 SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_, "already specified");
388
389 // This won't detect the timescale being fixed outside of systemc, but
390 // it's at least some protection.
216 sc_time t;
217 t.val = u;
218 return t;
219}
220
221sc_time
222sc_time::from_seconds(double d)
223{

--- 82 unchanged lines hidden (view full) ---

306 SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_, "simulation running");
307
308 static bool specified = false;
309 if (specified)
310 SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_, "already specified");
311
312 // This won't detect the timescale being fixed outside of systemc, but
313 // it's at least some protection.
391 if (timeFixed) {
314 if (clockFrequencyFixed()) {
392 SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_,
393 "sc_time object(s) constructed");
394 }
395
396 double seconds = d * sc_gem5::TimeUnitScale[tu];
397 if (seconds < sc_gem5::TimeUnitScale[SC_FS])
398 SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_, "value smaller than 1 fs");
399

--- 5 unchanged lines hidden (view full) ---

405 // Get rid of fractional parts of d.
406 while (d < 1.0 && tu > SC_FS) {
407 d *= 1000;
408 tu = (sc_time_unit)(tu - 1);
409 }
410
411 Tick ticks_per_second =
412 sc_gem5::TimeUnitFrequency[tu] / static_cast<Tick>(d);
315 SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_,
316 "sc_time object(s) constructed");
317 }
318
319 double seconds = d * sc_gem5::TimeUnitScale[tu];
320 if (seconds < sc_gem5::TimeUnitScale[SC_FS])
321 SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_, "value smaller than 1 fs");
322

--- 5 unchanged lines hidden (view full) ---

328 // Get rid of fractional parts of d.
329 while (d < 1.0 && tu > SC_FS) {
330 d *= 1000;
331 tu = (sc_time_unit)(tu - 1);
332 }
333
334 Tick ticks_per_second =
335 sc_gem5::TimeUnitFrequency[tu] / static_cast<Tick>(d);
413 setGlobalFrequency(ticks_per_second);
336 setClockFrequency(ticks_per_second);
414 specified = true;
415}
416
417sc_time
418sc_get_time_resolution()
419{
420 return sc_time::from_value(1);
421}

--- 20 unchanged lines hidden (view full) ---

442 SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_, "simulation running");
443
444 static bool specified = false;
445 if (specified) {
446 SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_, "already specified");
447 }
448 // This won't detect the timescale being fixed outside of systemc, but
449 // it's at least some protection.
337 specified = true;
338}
339
340sc_time
341sc_get_time_resolution()
342{
343 return sc_time::from_value(1);
344}

--- 20 unchanged lines hidden (view full) ---

365 SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_, "simulation running");
366
367 static bool specified = false;
368 if (specified) {
369 SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_, "already specified");
370 }
371 // This won't detect the timescale being fixed outside of systemc, but
372 // it's at least some protection.
450 if (timeFixed) {
373 if (clockFrequencyFixed()) {
451 SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_,
452 "sc_time object(s) constructed");
453 }
454
455 // Normalize d to seconds.
456 defaultUnit = d * sc_gem5::TimeUnitScale[tu];
457 specified = true;
458

--- 75 unchanged lines hidden ---
374 SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_,
375 "sc_time object(s) constructed");
376 }
377
378 // Normalize d to seconds.
379 defaultUnit = d * sc_gem5::TimeUnitScale[tu];
380 specified = true;
381

--- 75 unchanged lines hidden ---