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>
32#include <sstream>
33#include <vector>
34
35#include "base/types.hh"
36#include "sim/core.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
49void
50set(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu)
51{
52    if (d != 0)
53        fixClockFrequency();
54
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
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)
215        fixClockFrequency();
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.
314    if (clockFrequencyFixed()) {
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);
336    setClockFrequency(ticks_per_second);
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.
373    if (clockFrequencyFixed()) {
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
457