sc_time.cc (12989:f5e0cebe6999) sc_time.cc (13039:0c8ecf92a420)
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 <vector>
31
30#include "base/logging.hh"
31#include "base/types.hh"
32#include "python/pybind11/pybind.hh"
32#include "base/logging.hh"
33#include "base/types.hh"
34#include "python/pybind11/pybind.hh"
35#include "systemc/core/python.hh"
33#include "systemc/ext/core/sc_time.hh"
34
35namespace sc_core
36{
37
38namespace
39{
40

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

51 [SC_FS] = 1.0e-15,
52 [SC_PS] = 1.0e-12,
53 [SC_NS] = 1.0e-9,
54 [SC_US] = 1.0e-6,
55 [SC_MS] = 1.0e-3,
56 [SC_SEC] = 1.0
57};
58
36#include "systemc/ext/core/sc_time.hh"
37
38namespace sc_core
39{
40
41namespace
42{
43

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

54 [SC_FS] = 1.0e-15,
55 [SC_PS] = 1.0e-12,
56 [SC_NS] = 1.0e-9,
57 [SC_US] = 1.0e-6,
58 [SC_MS] = 1.0e-3,
59 [SC_SEC] = 1.0
60};
61
62bool timeFixed = false;
63bool pythonReady = false;
64
65struct SetInfo
66{
67 SetInfo(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu) :
68 time(time), d(d), tu(tu)
69 {}
70
71 ::sc_core::sc_time *time;
72 double d;
73 ::sc_core::sc_time_unit tu;
74};
75std::vector<SetInfo> toSet;
76
59void
77void
60fixTimeResolution()
78setWork(sc_time *time, double d, ::sc_core::sc_time_unit tu)
61{
79{
62 static bool fixed = false;
63 if (fixed)
64 return;
80 //XXX Assuming the time resolution is 1ps.
81 double scale = TimeUnitScale[tu] / TimeUnitScale[SC_PS];
82 // Accellera claims there is a linux bug, and that these next two
83 // lines work around them.
84 volatile double tmp = d * scale + 0.5;
85 *time = sc_time::from_value(static_cast<uint64_t>(tmp));
86}
65
87
88void
89fixTime()
90{
66 auto ticks = pybind11::module::import("m5.ticks");
67 auto fix_global_frequency = ticks.attr("fixGlobalFrequency");
68 fix_global_frequency();
91 auto ticks = pybind11::module::import("m5.ticks");
92 auto fix_global_frequency = ticks.attr("fixGlobalFrequency");
93 fix_global_frequency();
69 fixed = true;
94
95 for (auto &t: toSet)
96 setWork(t.time, t.d, t.tu);
97 toSet.clear();
70}
71
98}
99
100void
101set(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu)
102{
103 // Only fix time once.
104 if (!timeFixed) {
105 timeFixed = true;
106
107 // If we've run, python is working and we haven't fixed time yet.
108 if (pythonReady)
109 fixTime();
110 }
111 if (pythonReady) {
112 // Time should be working. Set up this sc_time.
113 setWork(time, d, tu);
114 } else {
115 // Time isn't set up yet. Defer setting up this sc_time.
116 toSet.emplace_back(time, d, tu);
117 }
118}
119
120class TimeSetter : public ::sc_gem5::PythonReadyFunc
121{
122 public:
123 TimeSetter() : ::sc_gem5::PythonReadyFunc() {}
124
125 void
126 run() override
127 {
128 // Record that we've run and python/pybind should be usable.
129 pythonReady = true;
130
131 // If time is already fixed, let python know.
132 if (timeFixed)
133 fixTime();
134 }
135} timeSetter;
136
72} // anonymous namespace
73
74sc_time::sc_time() : val(0) {}
75
76sc_time::sc_time(double d, sc_time_unit tu)
77{
78 val = 0;
137} // anonymous namespace
138
139sc_time::sc_time() : val(0) {}
140
141sc_time::sc_time(double d, sc_time_unit tu)
142{
143 val = 0;
79 if (d != 0) {
80 fixTimeResolution();
81 //XXX Assuming the time resolution is 1ps.
82 double scale = TimeUnitScale[tu] / TimeUnitScale[SC_PS];
83 // Accellera claims there is a linux bug, and that these next two
84 // lines work around them.
85 volatile double tmp = d * scale + 0.5;
86 val = static_cast<uint64_t>(tmp);
87 }
144 if (d != 0)
145 set(this, d, tu);
88}
89
90sc_time::sc_time(const sc_time &t)
91{
92 val = t.val;
93}
94
95sc_time::sc_time(double, bool)

--- 269 unchanged lines hidden ---
146}
147
148sc_time::sc_time(const sc_time &t)
149{
150 val = t.val;
151}
152
153sc_time::sc_time(double, bool)

--- 269 unchanged lines hidden ---