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
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
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 <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{
147 val = 0;
148 set(this, d, tu);
149}
150
151sc_time::sc_time(const sc_time &t)
152{
153 val = t.val;
154}
155
156sc_time::sc_time(double d, const char *unit)
157{
158 sc_time_unit tu;
159 for (tu = SC_FS; tu <= SC_SEC; tu = (sc_time_unit)(tu + 1)) {
160 if (strcmp(unit, sc_gem5::TimeUnitNames[tu]) == 0 ||
161 strcmp(unit, sc_gem5::TimeUnitConstantNames[tu]) == 0) {
162 break;
163 }
164 }
165
166 if (tu > SC_SEC) {
167 SC_REPORT_ERROR(SC_ID_TIME_CONVERSION_FAILED_,"invalid unit given");
168 val = 0;
169 return;
170 }
171 set(this, d, tu);
172}
173
174sc_time::sc_time(double d, bool scale)
175{
176 double scaler = scale ? defaultUnit : SimClock::Float::Hz;
177 set(this, d * scaler, SC_SEC);
178}
179
180sc_time::sc_time(sc_dt::uint64 v, bool scale)
181{
182 double scaler = scale ? defaultUnit : SimClock::Float::Hz;
183 set(this, static_cast<double>(v) * scaler, SC_SEC);
184}
185
186sc_time &
187sc_time::operator = (const sc_time &t)
188{
189 val = t.val;
190 return *this;
191}
192
193sc_dt::uint64
194sc_time::value() const
195{
196 return val;
197}
198
199double
200sc_time::to_double() const
201{
202 return static_cast<double>(val);
203}
204double
205sc_time::to_seconds() const
206{
207 return to_double() * SimClock::Float::Hz;
208}
209
210const std::string
211sc_time::to_string() const
212{
213 std::ostringstream ss;
214 print(ss);
215 return ss.str();
216}
217
218bool
219sc_time::operator == (const sc_time &t) const
220{
221 return val == t.val;
222}
223
224bool
225sc_time::operator != (const sc_time &t) const
226{
227 return val != t.val;
228}
229
230bool
231sc_time::operator < (const sc_time &t) const
232{
233 return val < t.val;
234}
235
236bool
237sc_time::operator <= (const sc_time &t) const
238{
239 return val <= t.val;
240}
241
242bool
243sc_time::operator > (const sc_time &t) const
244{
245 return val > t.val;
246}
247
248bool
249sc_time::operator >= (const sc_time &t) const
250{
251 return val >= t.val;
252}
253
254sc_time &
255sc_time::operator += (const sc_time &t)
256{
257 val += t.val;
258 return *this;
259}
260
261sc_time &
262sc_time::operator -= (const sc_time &t)
263{
264 val -= t.val;
265 return *this;
266}
267
268sc_time &
269sc_time::operator *= (double d)
270{
271 val = static_cast<int64_t>(static_cast<double>(val) * d + 0.5);
272 return *this;
273}
274
275sc_time &
276sc_time::operator /= (double d)
277{
278 val = static_cast<int64_t>(static_cast<double>(val) / d + 0.5);
279 return *this;
280}
281
282void
283sc_time::print(std::ostream &os) const
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{
70 val = 0;
71 set(this, d, tu);
72}
73
74sc_time::sc_time(const sc_time &t)
75{
76 val = t.val;
77}
78
79sc_time::sc_time(double d, const char *unit)
80{
81 sc_time_unit tu;
82 for (tu = SC_FS; tu <= SC_SEC; tu = (sc_time_unit)(tu + 1)) {
83 if (strcmp(unit, sc_gem5::TimeUnitNames[tu]) == 0 ||
84 strcmp(unit, sc_gem5::TimeUnitConstantNames[tu]) == 0) {
85 break;
86 }
87 }
88
89 if (tu > SC_SEC) {
90 SC_REPORT_ERROR(SC_ID_TIME_CONVERSION_FAILED_,"invalid unit given");
91 val = 0;
92 return;
93 }
94 set(this, d, tu);
95}
96
97sc_time::sc_time(double d, bool scale)
98{
99 double scaler = scale ? defaultUnit : SimClock::Float::Hz;
100 set(this, d * scaler, SC_SEC);
101}
102
103sc_time::sc_time(sc_dt::uint64 v, bool scale)
104{
105 double scaler = scale ? defaultUnit : SimClock::Float::Hz;
106 set(this, static_cast<double>(v) * scaler, SC_SEC);
107}
108
109sc_time &
110sc_time::operator = (const sc_time &t)
111{
112 val = t.val;
113 return *this;
114}
115
116sc_dt::uint64
117sc_time::value() const
118{
119 return val;
120}
121
122double
123sc_time::to_double() const
124{
125 return static_cast<double>(val);
126}
127double
128sc_time::to_seconds() const
129{
130 return to_double() * SimClock::Float::Hz;
131}
132
133const std::string
134sc_time::to_string() const
135{
136 std::ostringstream ss;
137 print(ss);
138 return ss.str();
139}
140
141bool
142sc_time::operator == (const sc_time &t) const
143{
144 return val == t.val;
145}
146
147bool
148sc_time::operator != (const sc_time &t) const
149{
150 return val != t.val;
151}
152
153bool
154sc_time::operator < (const sc_time &t) const
155{
156 return val < t.val;
157}
158
159bool
160sc_time::operator <= (const sc_time &t) const
161{
162 return val <= t.val;
163}
164
165bool
166sc_time::operator > (const sc_time &t) const
167{
168 return val > t.val;
169}
170
171bool
172sc_time::operator >= (const sc_time &t) const
173{
174 return val >= t.val;
175}
176
177sc_time &
178sc_time::operator += (const sc_time &t)
179{
180 val += t.val;
181 return *this;
182}
183
184sc_time &
185sc_time::operator -= (const sc_time &t)
186{
187 val -= t.val;
188 return *this;
189}
190
191sc_time &
192sc_time::operator *= (double d)
193{
194 val = static_cast<int64_t>(static_cast<double>(val) * d + 0.5);
195 return *this;
196}
197
198sc_time &
199sc_time::operator /= (double d)
200{
201 val = static_cast<int64_t>(static_cast<double>(val) / d + 0.5);
202 return *this;
203}
204
205void
206sc_time::print(std::ostream &os) const
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{
301 sc_time t;
302 set(&t, d, SC_SEC);
303 return t;
304}
305
306sc_time
307sc_time::from_string(const char *str)
308{
309 char *end = nullptr;
310
311 double d = str ? std::strtod(str, &end) : 0.0;
312 if (str == end || d < 0.0) {
313 SC_REPORT_ERROR(SC_ID_TIME_CONVERSION_FAILED_, "invalid value given");
314 return SC_ZERO_TIME;
315 }
316
317 while (*end && std::isspace(*end))
318 end++;
319
320 return sc_time(d, end);
321}
322
323const sc_time
324operator + (const sc_time &a, const sc_time &b)
325{
326 return sc_time::from_value(a.value() + b.value());
327}
328
329const sc_time
330operator - (const sc_time &a, const sc_time &b)
331{
332 return sc_time::from_value(a.value() - b.value());
333}
334
335const sc_time
336operator * (const sc_time &t, double d)
337{
338 volatile double tmp = static_cast<double>(t.value()) * d + 0.5;
339 return sc_time::from_value(static_cast<int64_t>(tmp));
340}
341
342const sc_time
343operator * (double d, const sc_time &t)
344{
345 volatile double tmp = d * static_cast<double>(t.value()) + 0.5;
346 return sc_time::from_value(static_cast<int64_t>(tmp));
347}
348
349const sc_time
350operator / (const sc_time &t, double d)
351{
352 volatile double tmp = static_cast<double>(t.value()) / d + 0.5;
353 return sc_time::from_value(static_cast<int64_t>(tmp));
354}
355
356double
357operator / (const sc_time &t1, const sc_time &t2)
358{
359 return t1.to_double() / t2.to_double();
360}
361
362std::ostream &
363operator << (std::ostream &os, const sc_time &t)
364{
365 t.print(os);
366 return os;
367}
368
369const sc_time SC_ZERO_TIME;
370
371void
372sc_set_time_resolution(double d, sc_time_unit tu)
373{
374 if (d <= 0.0)
375 SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_, "value not positive");
376
377 double dummy;
378 if (modf(log10(d), &dummy) != 0.0) {
379 SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_,
380 "value not a power of ten");
381 }
382 if (sc_is_running())
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{
224 sc_time t;
225 set(&t, d, SC_SEC);
226 return t;
227}
228
229sc_time
230sc_time::from_string(const char *str)
231{
232 char *end = nullptr;
233
234 double d = str ? std::strtod(str, &end) : 0.0;
235 if (str == end || d < 0.0) {
236 SC_REPORT_ERROR(SC_ID_TIME_CONVERSION_FAILED_, "invalid value given");
237 return SC_ZERO_TIME;
238 }
239
240 while (*end && std::isspace(*end))
241 end++;
242
243 return sc_time(d, end);
244}
245
246const sc_time
247operator + (const sc_time &a, const sc_time &b)
248{
249 return sc_time::from_value(a.value() + b.value());
250}
251
252const sc_time
253operator - (const sc_time &a, const sc_time &b)
254{
255 return sc_time::from_value(a.value() - b.value());
256}
257
258const sc_time
259operator * (const sc_time &t, double d)
260{
261 volatile double tmp = static_cast<double>(t.value()) * d + 0.5;
262 return sc_time::from_value(static_cast<int64_t>(tmp));
263}
264
265const sc_time
266operator * (double d, const sc_time &t)
267{
268 volatile double tmp = d * static_cast<double>(t.value()) + 0.5;
269 return sc_time::from_value(static_cast<int64_t>(tmp));
270}
271
272const sc_time
273operator / (const sc_time &t, double d)
274{
275 volatile double tmp = static_cast<double>(t.value()) / d + 0.5;
276 return sc_time::from_value(static_cast<int64_t>(tmp));
277}
278
279double
280operator / (const sc_time &t1, const sc_time &t2)
281{
282 return t1.to_double() / t2.to_double();
283}
284
285std::ostream &
286operator << (std::ostream &os, const sc_time &t)
287{
288 t.print(os);
289 return os;
290}
291
292const sc_time SC_ZERO_TIME;
293
294void
295sc_set_time_resolution(double d, sc_time_unit tu)
296{
297 if (d <= 0.0)
298 SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_, "value not positive");
299
300 double dummy;
301 if (modf(log10(d), &dummy) != 0.0) {
302 SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_,
303 "value not a power of ten");
304 }
305 if (sc_is_running())
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
400 if (seconds > defaultUnit) {
401 SC_REPORT_WARNING(SC_ID_DEFAULT_TIME_UNIT_CHANGED_, "");
402 defaultUnit = seconds;
403 }
404
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
323 if (seconds > defaultUnit) {
324 SC_REPORT_WARNING(SC_ID_DEFAULT_TIME_UNIT_CHANGED_, "");
325 defaultUnit = seconds;
326 }
327
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}
422
423const sc_time &
424sc_max_time()
425{
426 static const sc_time MaxScTime = sc_time::from_value(MaxTick);
427 return MaxScTime;
428}
429
430void
431sc_set_default_time_unit(double d, sc_time_unit tu)
432{
433 if (d < 0.0)
434 SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_, "value not positive");
435
436 double dummy;
437 if (modf(log10(d), &dummy) != 0.0) {
438 SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_,
439 "value not a power of ten");
440 }
441 if (sc_is_running())
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}
345
346const sc_time &
347sc_max_time()
348{
349 static const sc_time MaxScTime = sc_time::from_value(MaxTick);
350 return MaxScTime;
351}
352
353void
354sc_set_default_time_unit(double d, sc_time_unit tu)
355{
356 if (d < 0.0)
357 SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_, "value not positive");
358
359 double dummy;
360 if (modf(log10(d), &dummy) != 0.0) {
361 SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_,
362 "value not a power of ten");
363 }
364 if (sc_is_running())
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
459 double resolution = SimClock::Float::Hz;
460 if (resolution == 0.0)
461 resolution = sc_gem5::TimeUnitScale[SC_PS];
462 if (defaultUnit < resolution) {
463 SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_,
464 "value smaller than time resolution");
465 }
466}
467
468sc_time
469sc_get_default_time_unit()
470{
471 return sc_time(defaultUnit, SC_SEC);
472}
473
474sc_time_tuple::sc_time_tuple(const sc_time &t) :
475 _value(), _unit(SC_SEC), _set(true)
476{
477 if (!t.value())
478 return;
479
480 Tick frequency = SimClock::Frequency;
481
482 // Shrink the frequency by scaling down the time period, ie converting
483 // it from cycles per second to cycles per millisecond, etc.
484 while (_unit > 1 && (frequency % 1000 == 0)) {
485 _unit = (sc_time_unit)((int)_unit - 1);
486 frequency /= 1000;
487 }
488
489 // Convert the frequency into a period.
490 Tick period;
491 if (frequency > 1) {
492 _unit = (sc_time_unit)((int)_unit - 1);
493 period = 1000 / frequency;
494 } else {
495 period = frequency;
496 }
497
498 // Scale our integer value by the period.
499 _value = t.value() * period;
500
501 // Shrink the scaled time value by increasing the size of the units
502 // it's measured by, avoiding fractional parts.
503 while (_unit < SC_SEC && (_value % 1000) == 0) {
504 _unit = (sc_time_unit)((int)_unit + 1);
505 _value /= 1000;
506 }
507}
508
509bool
510sc_time_tuple::has_value() const
511{
512 return _set;
513}
514
515sc_dt::uint64 sc_time_tuple::value() const { return _value; }
516
517const char *
518sc_time_tuple::unit_symbol() const
519{
520 return sc_gem5::TimeUnitNames[_unit];
521}
522
523double sc_time_tuple::to_double() const { return static_cast<double>(_value); }
524
525std::string
526sc_time_tuple::to_string() const
527{
528 std::ostringstream ss;
529 ss << _value << ' ' << unit_symbol();
530 return ss.str();
531}
532
533} // namespace sc_core
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
382 double resolution = SimClock::Float::Hz;
383 if (resolution == 0.0)
384 resolution = sc_gem5::TimeUnitScale[SC_PS];
385 if (defaultUnit < resolution) {
386 SC_REPORT_ERROR(SC_ID_SET_DEFAULT_TIME_UNIT_,
387 "value smaller than time resolution");
388 }
389}
390
391sc_time
392sc_get_default_time_unit()
393{
394 return sc_time(defaultUnit, SC_SEC);
395}
396
397sc_time_tuple::sc_time_tuple(const sc_time &t) :
398 _value(), _unit(SC_SEC), _set(true)
399{
400 if (!t.value())
401 return;
402
403 Tick frequency = SimClock::Frequency;
404
405 // Shrink the frequency by scaling down the time period, ie converting
406 // it from cycles per second to cycles per millisecond, etc.
407 while (_unit > 1 && (frequency % 1000 == 0)) {
408 _unit = (sc_time_unit)((int)_unit - 1);
409 frequency /= 1000;
410 }
411
412 // Convert the frequency into a period.
413 Tick period;
414 if (frequency > 1) {
415 _unit = (sc_time_unit)((int)_unit - 1);
416 period = 1000 / frequency;
417 } else {
418 period = frequency;
419 }
420
421 // Scale our integer value by the period.
422 _value = t.value() * period;
423
424 // Shrink the scaled time value by increasing the size of the units
425 // it's measured by, avoiding fractional parts.
426 while (_unit < SC_SEC && (_value % 1000) == 0) {
427 _unit = (sc_time_unit)((int)_unit + 1);
428 _value /= 1000;
429 }
430}
431
432bool
433sc_time_tuple::has_value() const
434{
435 return _set;
436}
437
438sc_dt::uint64 sc_time_tuple::value() const { return _value; }
439
440const char *
441sc_time_tuple::unit_symbol() const
442{
443 return sc_gem5::TimeUnitNames[_unit];
444}
445
446double sc_time_tuple::to_double() const { return static_cast<double>(_value); }
447
448std::string
449sc_time_tuple::to_string() const
450{
451 std::ostringstream ss;
452 ss << _value << ' ' << unit_symbol();
453 return ss.str();
454}
455
456} // namespace sc_core