sc_time.cc (13151:5ce517287782) sc_time.cc (13177:7b750aeab360)
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 <sstream>
30#include <vector>
31
32#include "base/logging.hh"
33#include "base/types.hh"
34#include "python/pybind11/pybind.hh"
35#include "systemc/core/python.hh"
36#include "systemc/ext/core/sc_main.hh"
37#include "systemc/ext/core/sc_time.hh"
38#include "systemc/ext/utils/sc_report_handler.hh"
39
40namespace sc_core
41{
42
43namespace
44{
45
46const char *TimeUnitNames[] = {
47 [SC_FS] = "fs",
48 [SC_PS] = "ps",
49 [SC_NS] = "ns",
50 [SC_US] = "us",
51 [SC_MS] = "ms",
52 [SC_SEC] = "s"
53};
54
55double TimeUnitScale[] = {
56 [SC_FS] = 1.0e-15,
57 [SC_PS] = 1.0e-12,
58 [SC_NS] = 1.0e-9,
59 [SC_US] = 1.0e-6,
60 [SC_MS] = 1.0e-3,
61 [SC_SEC] = 1.0
62};
63
64bool timeFixed = false;
65bool pythonReady = false;
66
67struct SetInfo
68{
69 SetInfo(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu) :
70 time(time), d(d), tu(tu)
71 {}
72
73 ::sc_core::sc_time *time;
74 double d;
75 ::sc_core::sc_time_unit tu;
76};
77std::vector<SetInfo> toSet;
78
79void
80setWork(sc_time *time, double d, ::sc_core::sc_time_unit tu)
81{
82 //XXX Assuming the time resolution is 1ps.
83 double scale = TimeUnitScale[tu] / TimeUnitScale[SC_PS];
84 // Accellera claims there is a linux bug, and that these next two
85 // lines work around them.
86 volatile double tmp = d * scale + 0.5;
87 *time = sc_time::from_value(static_cast<uint64_t>(tmp));
88}
89
90void
91fixTime()
92{
93 auto ticks = pybind11::module::import("m5.ticks");
94 auto fix_global_frequency = ticks.attr("fixGlobalFrequency");
95 fix_global_frequency();
96
97 for (auto &t: toSet)
98 setWork(t.time, t.d, t.tu);
99 toSet.clear();
100}
101
102void
103setGlobalFrequency(Tick ticks_per_second)
104{
105 auto ticks = pybind11::module::import("m5.ticks");
106 auto set_global_frequency = ticks.attr("setGlobalFrequency");
107 set_global_frequency(ticks_per_second);
108 fixTime();
109}
110
111void
112set(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu)
113{
114 // Only fix time once.
115 if (!timeFixed) {
116 timeFixed = true;
117
118 // If we've run, python is working and we haven't fixed time yet.
119 if (pythonReady)
120 fixTime();
121 }
122 if (pythonReady) {
123 // Time should be working. Set up this sc_time.
124 setWork(time, d, tu);
125 } else {
126 // Time isn't set up yet. Defer setting up this sc_time.
127 toSet.emplace_back(time, d, tu);
128 }
129}
130
131class TimeSetter : public ::sc_gem5::PythonReadyFunc
132{
133 public:
134 TimeSetter() : ::sc_gem5::PythonReadyFunc() {}
135
136 void
137 run() override
138 {
139 // Record that we've run and python/pybind should be usable.
140 pythonReady = true;
141
142 // If time is already fixed, let python know.
143 if (timeFixed)
144 fixTime();
145 }
146} timeSetter;
147
148double defaultUnit = 1.0e-9;
149
150} // anonymous namespace
151
152sc_time::sc_time() : val(0) {}
153
154sc_time::sc_time(double d, sc_time_unit tu)
155{
156 val = 0;
157 if (d != 0)
158 set(this, d, tu);
159}
160
161sc_time::sc_time(const sc_time &t)
162{
163 val = t.val;
164}
165
166sc_time::sc_time(double d, bool scale)
167{
168 //XXX Assuming the time resolution is 1ps.
169 if (scale)
170 set(this, d * defaultUnit, SC_SEC);
171 else
172 set(this, d, SC_PS);
173}
174
175sc_time::sc_time(sc_dt::uint64 v, bool scale)
176{
177 //XXX Assuming the time resolution is 1ps.
178 if (scale)
179 set(this, static_cast<double>(v) * defaultUnit, SC_SEC);
180 else
181 set(this, static_cast<double>(v), SC_PS);
182}
183
184sc_time &
185sc_time::operator = (const sc_time &t)
186{
187 val = t.val;
188 return *this;
189}
190
191sc_dt::uint64
192sc_time::value() const
193{
194 return val;
195}
196
197double
198sc_time::to_double() const
199{
200 return static_cast<double>(val);
201}
202double
203sc_time::to_seconds() const
204{
205 double d = to_double();
206 //XXX Assuming the time resolution is 1ps.
207 double scale = TimeUnitScale[SC_PS] / TimeUnitScale[SC_SEC];
208 return d * scale;
209}
210
211const std::string
212sc_time::to_string() const
213{
31#include <vector>
32
33#include "base/logging.hh"
34#include "base/types.hh"
35#include "python/pybind11/pybind.hh"
36#include "systemc/core/python.hh"
37#include "systemc/ext/core/sc_main.hh"
38#include "systemc/ext/core/sc_time.hh"
39#include "systemc/ext/utils/sc_report_handler.hh"
40
41namespace sc_core
42{
43
44namespace
45{
46
47const char *TimeUnitNames[] = {
48 [SC_FS] = "fs",
49 [SC_PS] = "ps",
50 [SC_NS] = "ns",
51 [SC_US] = "us",
52 [SC_MS] = "ms",
53 [SC_SEC] = "s"
54};
55
56double TimeUnitScale[] = {
57 [SC_FS] = 1.0e-15,
58 [SC_PS] = 1.0e-12,
59 [SC_NS] = 1.0e-9,
60 [SC_US] = 1.0e-6,
61 [SC_MS] = 1.0e-3,
62 [SC_SEC] = 1.0
63};
64
65bool timeFixed = false;
66bool pythonReady = false;
67
68struct SetInfo
69{
70 SetInfo(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu) :
71 time(time), d(d), tu(tu)
72 {}
73
74 ::sc_core::sc_time *time;
75 double d;
76 ::sc_core::sc_time_unit tu;
77};
78std::vector<SetInfo> toSet;
79
80void
81setWork(sc_time *time, double d, ::sc_core::sc_time_unit tu)
82{
83 //XXX Assuming the time resolution is 1ps.
84 double scale = TimeUnitScale[tu] / TimeUnitScale[SC_PS];
85 // Accellera claims there is a linux bug, and that these next two
86 // lines work around them.
87 volatile double tmp = d * scale + 0.5;
88 *time = sc_time::from_value(static_cast<uint64_t>(tmp));
89}
90
91void
92fixTime()
93{
94 auto ticks = pybind11::module::import("m5.ticks");
95 auto fix_global_frequency = ticks.attr("fixGlobalFrequency");
96 fix_global_frequency();
97
98 for (auto &t: toSet)
99 setWork(t.time, t.d, t.tu);
100 toSet.clear();
101}
102
103void
104setGlobalFrequency(Tick ticks_per_second)
105{
106 auto ticks = pybind11::module::import("m5.ticks");
107 auto set_global_frequency = ticks.attr("setGlobalFrequency");
108 set_global_frequency(ticks_per_second);
109 fixTime();
110}
111
112void
113set(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu)
114{
115 // Only fix time once.
116 if (!timeFixed) {
117 timeFixed = true;
118
119 // If we've run, python is working and we haven't fixed time yet.
120 if (pythonReady)
121 fixTime();
122 }
123 if (pythonReady) {
124 // Time should be working. Set up this sc_time.
125 setWork(time, d, tu);
126 } else {
127 // Time isn't set up yet. Defer setting up this sc_time.
128 toSet.emplace_back(time, d, tu);
129 }
130}
131
132class TimeSetter : public ::sc_gem5::PythonReadyFunc
133{
134 public:
135 TimeSetter() : ::sc_gem5::PythonReadyFunc() {}
136
137 void
138 run() override
139 {
140 // Record that we've run and python/pybind should be usable.
141 pythonReady = true;
142
143 // If time is already fixed, let python know.
144 if (timeFixed)
145 fixTime();
146 }
147} timeSetter;
148
149double defaultUnit = 1.0e-9;
150
151} // anonymous namespace
152
153sc_time::sc_time() : val(0) {}
154
155sc_time::sc_time(double d, sc_time_unit tu)
156{
157 val = 0;
158 if (d != 0)
159 set(this, d, tu);
160}
161
162sc_time::sc_time(const sc_time &t)
163{
164 val = t.val;
165}
166
167sc_time::sc_time(double d, bool scale)
168{
169 //XXX Assuming the time resolution is 1ps.
170 if (scale)
171 set(this, d * defaultUnit, SC_SEC);
172 else
173 set(this, d, SC_PS);
174}
175
176sc_time::sc_time(sc_dt::uint64 v, bool scale)
177{
178 //XXX Assuming the time resolution is 1ps.
179 if (scale)
180 set(this, static_cast<double>(v) * defaultUnit, SC_SEC);
181 else
182 set(this, static_cast<double>(v), SC_PS);
183}
184
185sc_time &
186sc_time::operator = (const sc_time &t)
187{
188 val = t.val;
189 return *this;
190}
191
192sc_dt::uint64
193sc_time::value() const
194{
195 return val;
196}
197
198double
199sc_time::to_double() const
200{
201 return static_cast<double>(val);
202}
203double
204sc_time::to_seconds() const
205{
206 double d = to_double();
207 //XXX Assuming the time resolution is 1ps.
208 double scale = TimeUnitScale[SC_PS] / TimeUnitScale[SC_SEC];
209 return d * scale;
210}
211
212const std::string
213sc_time::to_string() const
214{
214 warn("%s not implemented.\n", __PRETTY_FUNCTION__);
215 return "";
215 std::ostringstream ss;
216 print(ss);
217 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 if (val == 0) {
286 os << "0 s";
287 } else {
288 //XXX Assuming the time resolution is 1ps.
289 sc_time_unit tu = SC_PS;
290 uint64_t scaled = val;
291 while (tu < SC_SEC && (scaled % 1000) == 0) {
292 tu = (sc_time_unit)((int)tu + 1);
293 scaled /= 1000;
294 }
295
296 os << scaled << ' ' << TimeUnitNames[tu];
297 }
298}
299
300sc_time
301sc_time::from_value(sc_dt::uint64 u)
302{
303 sc_time t;
304 t.val = u;
305 return t;
306}
307
308sc_time
309sc_time::from_seconds(double d)
310{
311 sc_time t;
312 set(&t, d, SC_SEC);
313 return t;
314}
315
316sc_time
317sc_time::from_string(const char *str)
318{
319 warn("%s not implemented.\n", __PRETTY_FUNCTION__);
320 return sc_time();
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("(E514) set time resolution failed",
376 "value not positive");
377 }
378 double dummy;
379 if (modf(log10(d), &dummy) != 0.0) {
380 SC_REPORT_ERROR("(E514) set time resolution failed",
381 "value not a power of ten");
382 }
383 if (sc_is_running()) {
384 SC_REPORT_ERROR("(E514) set time resolution failed",
385 "simulation running");
386 }
387 static bool specified = false;
388 if (specified) {
389 SC_REPORT_ERROR("(E514) set time resolution failed",
390 "already specified");
391 }
392 // This won't detect the timescale being fixed outside of systemc, but
393 // it's at least some protection.
394 if (timeFixed) {
395 SC_REPORT_ERROR("(E514) set time resolution failed",
396 "sc_time object(s) constructed");
397 }
398
399 // Normalize d to seconds.
400 d *= TimeUnitScale[tu];
401 if (d < TimeUnitScale[SC_FS]) {
402 SC_REPORT_ERROR("(E514) set time resolution failed",
403 "value smaller than 1 fs");
404 }
405 // Change d from a period to a frequency.
406 d = 1 / d;
407 // Convert to integer ticks.
408 Tick ticks_per_second = static_cast<Tick>(d);
409 setGlobalFrequency(ticks_per_second);
410 specified = true;
411}
412
413sc_time
414sc_get_time_resolution()
415{
416 return sc_time::from_value(1);
417}
418
419const sc_time &
420sc_max_time()
421{
422 static const sc_time MaxScTime = sc_time::from_value(MaxTick);
423 return MaxScTime;
424}
425
426void
427sc_set_default_time_unit(double d, sc_time_unit tu)
428{
429 if (d < 0.0) {
430 SC_REPORT_ERROR("(E515) set default time unit failed",
431 "value not positive");
432 }
433 double dummy;
434 if (modf(log10(d), &dummy) != 0.0) {
435 SC_REPORT_ERROR("(E515) set default time unit failed",
436 "value not a power of ten");
437 }
438 if (sc_is_running()) {
439 SC_REPORT_ERROR("(E515) set default time unit failed",
440 "simulation running");
441 }
442 static bool specified = false;
443 if (specified) {
444 SC_REPORT_ERROR("(E515) set default time unit failed",
445 "already specified");
446 }
447 // This won't detect the timescale being fixed outside of systemc, but
448 // it's at least some protection.
449 if (timeFixed) {
450 SC_REPORT_ERROR("(E515) set default time unit failed",
451 "sc_time object(s) constructed");
452 }
453
454 // Normalize d to seconds.
455 defaultUnit = d * TimeUnitScale[tu];
456 specified = true;
457}
458
459sc_time
460sc_get_default_time_unit()
461{
462 return sc_time(defaultUnit, SC_SEC);
463}
464
465sc_time_tuple::sc_time_tuple(const sc_time &)
466{
467 warn("%s not implemented.\n", __PRETTY_FUNCTION__);
468}
469
470bool
471sc_time_tuple::has_value() const
472{
473 warn("%s not implemented.\n", __PRETTY_FUNCTION__);
474 return false;
475}
476
477sc_dt::uint64
478sc_time_tuple::value() const
479{
480 warn("%s not implemented.\n", __PRETTY_FUNCTION__);
481 return 0;
482}
483
484const char *
485sc_time_tuple::unit_symbol() const
486{
487 warn("%s not implemented.\n", __PRETTY_FUNCTION__);
488 return "";
489}
490
491double
492sc_time_tuple::to_double() const
493{
494 warn("%s not implemented.\n", __PRETTY_FUNCTION__);
495 return 0.0;
496}
497
498std::string
499sc_time_tuple::to_string() const
500{
501 warn("%s not implemented.\n", __PRETTY_FUNCTION__);
502 return "";
503}
504
505} // namespace sc_core
218}
219
220bool
221sc_time::operator == (const sc_time &t) const
222{
223 return val == t.val;
224}
225
226bool
227sc_time::operator != (const sc_time &t) const
228{
229 return val != t.val;
230}
231
232bool
233sc_time::operator < (const sc_time &t) const
234{
235 return val < t.val;
236}
237
238bool
239sc_time::operator <= (const sc_time &t) const
240{
241 return val <= t.val;
242}
243
244bool
245sc_time::operator > (const sc_time &t) const
246{
247 return val > t.val;
248}
249
250bool
251sc_time::operator >= (const sc_time &t) const
252{
253 return val >= t.val;
254}
255
256sc_time &
257sc_time::operator += (const sc_time &t)
258{
259 val += t.val;
260 return *this;
261}
262
263sc_time &
264sc_time::operator -= (const sc_time &t)
265{
266 val -= t.val;
267 return *this;
268}
269
270sc_time &
271sc_time::operator *= (double d)
272{
273 val = static_cast<int64_t>(static_cast<double>(val) * d + 0.5);
274 return *this;
275}
276
277sc_time &
278sc_time::operator /= (double d)
279{
280 val = static_cast<int64_t>(static_cast<double>(val) / d + 0.5);
281 return *this;
282}
283
284void
285sc_time::print(std::ostream &os) const
286{
287 if (val == 0) {
288 os << "0 s";
289 } else {
290 //XXX Assuming the time resolution is 1ps.
291 sc_time_unit tu = SC_PS;
292 uint64_t scaled = val;
293 while (tu < SC_SEC && (scaled % 1000) == 0) {
294 tu = (sc_time_unit)((int)tu + 1);
295 scaled /= 1000;
296 }
297
298 os << scaled << ' ' << TimeUnitNames[tu];
299 }
300}
301
302sc_time
303sc_time::from_value(sc_dt::uint64 u)
304{
305 sc_time t;
306 t.val = u;
307 return t;
308}
309
310sc_time
311sc_time::from_seconds(double d)
312{
313 sc_time t;
314 set(&t, d, SC_SEC);
315 return t;
316}
317
318sc_time
319sc_time::from_string(const char *str)
320{
321 warn("%s not implemented.\n", __PRETTY_FUNCTION__);
322 return sc_time();
323}
324
325const sc_time
326operator + (const sc_time &a, const sc_time &b)
327{
328 return sc_time::from_value(a.value() + b.value());
329}
330
331const sc_time
332operator - (const sc_time &a, const sc_time &b)
333{
334 return sc_time::from_value(a.value() - b.value());
335}
336
337const sc_time
338operator * (const sc_time &t, double d)
339{
340 volatile double tmp = static_cast<double>(t.value()) * d + 0.5;
341 return sc_time::from_value(static_cast<int64_t>(tmp));
342}
343
344const sc_time
345operator * (double d, const sc_time &t)
346{
347 volatile double tmp = d * static_cast<double>(t.value()) + 0.5;
348 return sc_time::from_value(static_cast<int64_t>(tmp));
349}
350
351const sc_time
352operator / (const sc_time &t, double d)
353{
354 volatile double tmp = static_cast<double>(t.value()) / d + 0.5;
355 return sc_time::from_value(static_cast<int64_t>(tmp));
356}
357
358double
359operator / (const sc_time &t1, const sc_time &t2)
360{
361 return t1.to_double() / t2.to_double();
362}
363
364std::ostream &
365operator << (std::ostream &os, const sc_time &t)
366{
367 t.print(os);
368 return os;
369}
370
371const sc_time SC_ZERO_TIME;
372
373void
374sc_set_time_resolution(double d, sc_time_unit tu)
375{
376 if (d <= 0.0) {
377 SC_REPORT_ERROR("(E514) set time resolution failed",
378 "value not positive");
379 }
380 double dummy;
381 if (modf(log10(d), &dummy) != 0.0) {
382 SC_REPORT_ERROR("(E514) set time resolution failed",
383 "value not a power of ten");
384 }
385 if (sc_is_running()) {
386 SC_REPORT_ERROR("(E514) set time resolution failed",
387 "simulation running");
388 }
389 static bool specified = false;
390 if (specified) {
391 SC_REPORT_ERROR("(E514) set time resolution failed",
392 "already specified");
393 }
394 // This won't detect the timescale being fixed outside of systemc, but
395 // it's at least some protection.
396 if (timeFixed) {
397 SC_REPORT_ERROR("(E514) set time resolution failed",
398 "sc_time object(s) constructed");
399 }
400
401 // Normalize d to seconds.
402 d *= TimeUnitScale[tu];
403 if (d < TimeUnitScale[SC_FS]) {
404 SC_REPORT_ERROR("(E514) set time resolution failed",
405 "value smaller than 1 fs");
406 }
407 // Change d from a period to a frequency.
408 d = 1 / d;
409 // Convert to integer ticks.
410 Tick ticks_per_second = static_cast<Tick>(d);
411 setGlobalFrequency(ticks_per_second);
412 specified = true;
413}
414
415sc_time
416sc_get_time_resolution()
417{
418 return sc_time::from_value(1);
419}
420
421const sc_time &
422sc_max_time()
423{
424 static const sc_time MaxScTime = sc_time::from_value(MaxTick);
425 return MaxScTime;
426}
427
428void
429sc_set_default_time_unit(double d, sc_time_unit tu)
430{
431 if (d < 0.0) {
432 SC_REPORT_ERROR("(E515) set default time unit failed",
433 "value not positive");
434 }
435 double dummy;
436 if (modf(log10(d), &dummy) != 0.0) {
437 SC_REPORT_ERROR("(E515) set default time unit failed",
438 "value not a power of ten");
439 }
440 if (sc_is_running()) {
441 SC_REPORT_ERROR("(E515) set default time unit failed",
442 "simulation running");
443 }
444 static bool specified = false;
445 if (specified) {
446 SC_REPORT_ERROR("(E515) set default time unit failed",
447 "already specified");
448 }
449 // This won't detect the timescale being fixed outside of systemc, but
450 // it's at least some protection.
451 if (timeFixed) {
452 SC_REPORT_ERROR("(E515) set default time unit failed",
453 "sc_time object(s) constructed");
454 }
455
456 // Normalize d to seconds.
457 defaultUnit = d * TimeUnitScale[tu];
458 specified = true;
459}
460
461sc_time
462sc_get_default_time_unit()
463{
464 return sc_time(defaultUnit, SC_SEC);
465}
466
467sc_time_tuple::sc_time_tuple(const sc_time &)
468{
469 warn("%s not implemented.\n", __PRETTY_FUNCTION__);
470}
471
472bool
473sc_time_tuple::has_value() const
474{
475 warn("%s not implemented.\n", __PRETTY_FUNCTION__);
476 return false;
477}
478
479sc_dt::uint64
480sc_time_tuple::value() const
481{
482 warn("%s not implemented.\n", __PRETTY_FUNCTION__);
483 return 0;
484}
485
486const char *
487sc_time_tuple::unit_symbol() const
488{
489 warn("%s not implemented.\n", __PRETTY_FUNCTION__);
490 return "";
491}
492
493double
494sc_time_tuple::to_double() const
495{
496 warn("%s not implemented.\n", __PRETTY_FUNCTION__);
497 return 0.0;
498}
499
500std::string
501sc_time_tuple::to_string() const
502{
503 warn("%s not implemented.\n", __PRETTY_FUNCTION__);
504 return "";
505}
506
507} // namespace sc_core