sc_time.cc revision 13263
112837Sgabeblack@google.com/*
212837Sgabeblack@google.com * Copyright 2018 Google, Inc.
312837Sgabeblack@google.com *
412837Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512837Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612837Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812837Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012837Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112837Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212837Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312837Sgabeblack@google.com * this software without specific prior written permission.
1412837Sgabeblack@google.com *
1512837Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612837Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712837Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812837Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912837Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012837Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112837Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212837Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312837Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412837Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512837Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612837Sgabeblack@google.com *
2712837Sgabeblack@google.com * Authors: Gabe Black
2812837Sgabeblack@google.com */
2912837Sgabeblack@google.com
3013177Sgabeblack@google.com#include <sstream>
3113039Sgabeblack@google.com#include <vector>
3213039Sgabeblack@google.com
3312837Sgabeblack@google.com#include "base/logging.hh"
3412989Sgabeblack@google.com#include "base/types.hh"
3512986Sgabeblack@google.com#include "python/pybind11/pybind.hh"
3613195Sgabeblack@google.com#include "sim/core.hh"
3713039Sgabeblack@google.com#include "systemc/core/python.hh"
3813252Sgabeblack@google.com#include "systemc/core/time.hh"
3913149Sgabeblack@google.com#include "systemc/ext/core/sc_main.hh"
4012837Sgabeblack@google.com#include "systemc/ext/core/sc_time.hh"
4113149Sgabeblack@google.com#include "systemc/ext/utils/sc_report_handler.hh"
4212837Sgabeblack@google.com
4312837Sgabeblack@google.comnamespace sc_core
4412837Sgabeblack@google.com{
4512837Sgabeblack@google.com
4612983Sgabeblack@google.comnamespace
4712837Sgabeblack@google.com{
4812983Sgabeblack@google.com
4913039Sgabeblack@google.combool timeFixed = false;
5013039Sgabeblack@google.combool pythonReady = false;
5113039Sgabeblack@google.com
5213039Sgabeblack@google.comstruct SetInfo
5313039Sgabeblack@google.com{
5413039Sgabeblack@google.com    SetInfo(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu) :
5513039Sgabeblack@google.com        time(time), d(d), tu(tu)
5613039Sgabeblack@google.com    {}
5713039Sgabeblack@google.com
5813039Sgabeblack@google.com    ::sc_core::sc_time *time;
5913039Sgabeblack@google.com    double d;
6013039Sgabeblack@google.com    ::sc_core::sc_time_unit tu;
6113039Sgabeblack@google.com};
6213039Sgabeblack@google.comstd::vector<SetInfo> toSet;
6313039Sgabeblack@google.com
6412986Sgabeblack@google.comvoid
6513039Sgabeblack@google.comsetWork(sc_time *time, double d, ::sc_core::sc_time_unit tu)
6612986Sgabeblack@google.com{
6713252Sgabeblack@google.com    double scale = sc_gem5::TimeUnitScale[tu] * SimClock::Float::s;
6813039Sgabeblack@google.com    // Accellera claims there is a linux bug, and that these next two
6913039Sgabeblack@google.com    // lines work around them.
7013039Sgabeblack@google.com    volatile double tmp = d * scale + 0.5;
7113039Sgabeblack@google.com    *time = sc_time::from_value(static_cast<uint64_t>(tmp));
7213039Sgabeblack@google.com}
7312986Sgabeblack@google.com
7413039Sgabeblack@google.comvoid
7513039Sgabeblack@google.comfixTime()
7613039Sgabeblack@google.com{
7712986Sgabeblack@google.com    auto ticks = pybind11::module::import("m5.ticks");
7812986Sgabeblack@google.com    auto fix_global_frequency = ticks.attr("fixGlobalFrequency");
7912986Sgabeblack@google.com    fix_global_frequency();
8013039Sgabeblack@google.com
8113039Sgabeblack@google.com    for (auto &t: toSet)
8213039Sgabeblack@google.com        setWork(t.time, t.d, t.tu);
8313039Sgabeblack@google.com    toSet.clear();
8412986Sgabeblack@google.com}
8512986Sgabeblack@google.com
8613039Sgabeblack@google.comvoid
8713195Sgabeblack@google.comattemptToFixTime()
8813195Sgabeblack@google.com{
8913195Sgabeblack@google.com    // Only fix time once.
9013195Sgabeblack@google.com    if (!timeFixed) {
9113195Sgabeblack@google.com        timeFixed = true;
9213195Sgabeblack@google.com
9313195Sgabeblack@google.com        // If we've run, python is working and we haven't fixed time yet.
9413195Sgabeblack@google.com        if (pythonReady)
9513195Sgabeblack@google.com            fixTime();
9613195Sgabeblack@google.com    }
9713195Sgabeblack@google.com}
9813195Sgabeblack@google.com
9913195Sgabeblack@google.comvoid
10013149Sgabeblack@google.comsetGlobalFrequency(Tick ticks_per_second)
10113149Sgabeblack@google.com{
10213149Sgabeblack@google.com    auto ticks = pybind11::module::import("m5.ticks");
10313149Sgabeblack@google.com    auto set_global_frequency = ticks.attr("setGlobalFrequency");
10413149Sgabeblack@google.com    set_global_frequency(ticks_per_second);
10513149Sgabeblack@google.com    fixTime();
10613149Sgabeblack@google.com}
10713149Sgabeblack@google.com
10813149Sgabeblack@google.comvoid
10913039Sgabeblack@google.comset(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu)
11013039Sgabeblack@google.com{
11113195Sgabeblack@google.com    if (d != 0)
11213195Sgabeblack@google.com        attemptToFixTime();
11313039Sgabeblack@google.com    if (pythonReady) {
11413039Sgabeblack@google.com        // Time should be working. Set up this sc_time.
11513039Sgabeblack@google.com        setWork(time, d, tu);
11613039Sgabeblack@google.com    } else {
11713039Sgabeblack@google.com        // Time isn't set up yet. Defer setting up this sc_time.
11813039Sgabeblack@google.com        toSet.emplace_back(time, d, tu);
11913039Sgabeblack@google.com    }
12013039Sgabeblack@google.com}
12113039Sgabeblack@google.com
12213039Sgabeblack@google.comclass TimeSetter : public ::sc_gem5::PythonReadyFunc
12313039Sgabeblack@google.com{
12413039Sgabeblack@google.com  public:
12513039Sgabeblack@google.com    TimeSetter() : ::sc_gem5::PythonReadyFunc() {}
12613039Sgabeblack@google.com
12713039Sgabeblack@google.com    void
12813039Sgabeblack@google.com    run() override
12913039Sgabeblack@google.com    {
13013039Sgabeblack@google.com        // Record that we've run and python/pybind should be usable.
13113039Sgabeblack@google.com        pythonReady = true;
13213039Sgabeblack@google.com
13313039Sgabeblack@google.com        // If time is already fixed, let python know.
13413039Sgabeblack@google.com        if (timeFixed)
13513039Sgabeblack@google.com            fixTime();
13613039Sgabeblack@google.com    }
13713039Sgabeblack@google.com} timeSetter;
13813039Sgabeblack@google.com
13913124Sgabeblack@google.comdouble defaultUnit = 1.0e-9;
14013124Sgabeblack@google.com
14112983Sgabeblack@google.com} // anonymous namespace
14212983Sgabeblack@google.com
14312983Sgabeblack@google.comsc_time::sc_time() : val(0) {}
14412983Sgabeblack@google.com
14512983Sgabeblack@google.comsc_time::sc_time(double d, sc_time_unit tu)
14612983Sgabeblack@google.com{
14712983Sgabeblack@google.com    val = 0;
14813195Sgabeblack@google.com    set(this, d, tu);
14912837Sgabeblack@google.com}
15012837Sgabeblack@google.com
15112983Sgabeblack@google.comsc_time::sc_time(const sc_time &t)
15212837Sgabeblack@google.com{
15312983Sgabeblack@google.com    val = t.val;
15412837Sgabeblack@google.com}
15512837Sgabeblack@google.com
15613057Sgabeblack@google.comsc_time::sc_time(double d, bool scale)
15712925Sgabeblack@google.com{
15813195Sgabeblack@google.com    double scaler = scale ? defaultUnit : SimClock::Float::Hz;
15913195Sgabeblack@google.com    set(this, d * scaler, SC_SEC);
16012925Sgabeblack@google.com}
16112925Sgabeblack@google.com
16213057Sgabeblack@google.comsc_time::sc_time(sc_dt::uint64 v, bool scale)
16312925Sgabeblack@google.com{
16413195Sgabeblack@google.com    double scaler = scale ? defaultUnit : SimClock::Float::Hz;
16513195Sgabeblack@google.com    set(this, static_cast<double>(v) * scaler, SC_SEC);
16612925Sgabeblack@google.com}
16712925Sgabeblack@google.com
16812837Sgabeblack@google.comsc_time &
16912983Sgabeblack@google.comsc_time::operator = (const sc_time &t)
17012837Sgabeblack@google.com{
17112983Sgabeblack@google.com    val = t.val;
17212837Sgabeblack@google.com    return *this;
17312837Sgabeblack@google.com}
17412837Sgabeblack@google.com
17512837Sgabeblack@google.comsc_dt::uint64
17612837Sgabeblack@google.comsc_time::value() const
17712837Sgabeblack@google.com{
17812983Sgabeblack@google.com    return val;
17912837Sgabeblack@google.com}
18012837Sgabeblack@google.com
18112837Sgabeblack@google.comdouble
18212837Sgabeblack@google.comsc_time::to_double() const
18312837Sgabeblack@google.com{
18413040Sgabeblack@google.com    return static_cast<double>(val);
18512837Sgabeblack@google.com}
18612837Sgabeblack@google.comdouble
18712837Sgabeblack@google.comsc_time::to_seconds() const
18812837Sgabeblack@google.com{
18913195Sgabeblack@google.com    return to_double() * SimClock::Float::Hz;
19012837Sgabeblack@google.com}
19112837Sgabeblack@google.com
19212837Sgabeblack@google.comconst std::string
19312837Sgabeblack@google.comsc_time::to_string() const
19412837Sgabeblack@google.com{
19513177Sgabeblack@google.com    std::ostringstream ss;
19613177Sgabeblack@google.com    print(ss);
19713177Sgabeblack@google.com    return ss.str();
19812837Sgabeblack@google.com}
19912837Sgabeblack@google.com
20012837Sgabeblack@google.combool
20112983Sgabeblack@google.comsc_time::operator == (const sc_time &t) const
20212837Sgabeblack@google.com{
20312983Sgabeblack@google.com    return val == t.val;
20412837Sgabeblack@google.com}
20512837Sgabeblack@google.com
20612837Sgabeblack@google.combool
20712983Sgabeblack@google.comsc_time::operator != (const sc_time &t) const
20812837Sgabeblack@google.com{
20912983Sgabeblack@google.com    return val != t.val;
21012837Sgabeblack@google.com}
21112837Sgabeblack@google.com
21212837Sgabeblack@google.combool
21312983Sgabeblack@google.comsc_time::operator < (const sc_time &t) const
21412837Sgabeblack@google.com{
21512983Sgabeblack@google.com    return val < t.val;
21612837Sgabeblack@google.com}
21712837Sgabeblack@google.com
21812837Sgabeblack@google.combool
21912983Sgabeblack@google.comsc_time::operator <= (const sc_time &t) const
22012837Sgabeblack@google.com{
22112983Sgabeblack@google.com    return val <= t.val;
22212837Sgabeblack@google.com}
22312837Sgabeblack@google.com
22412837Sgabeblack@google.combool
22512983Sgabeblack@google.comsc_time::operator > (const sc_time &t) const
22612837Sgabeblack@google.com{
22712983Sgabeblack@google.com    return val > t.val;
22812837Sgabeblack@google.com}
22912837Sgabeblack@google.com
23012837Sgabeblack@google.combool
23112983Sgabeblack@google.comsc_time::operator >= (const sc_time &t) const
23212837Sgabeblack@google.com{
23312983Sgabeblack@google.com    return val >= t.val;
23412837Sgabeblack@google.com}
23512837Sgabeblack@google.com
23612837Sgabeblack@google.comsc_time &
23712983Sgabeblack@google.comsc_time::operator += (const sc_time &t)
23812837Sgabeblack@google.com{
23912983Sgabeblack@google.com    val += t.val;
24012837Sgabeblack@google.com    return *this;
24112837Sgabeblack@google.com}
24212837Sgabeblack@google.com
24312837Sgabeblack@google.comsc_time &
24412983Sgabeblack@google.comsc_time::operator -= (const sc_time &t)
24512837Sgabeblack@google.com{
24612983Sgabeblack@google.com    val -= t.val;
24712837Sgabeblack@google.com    return *this;
24812837Sgabeblack@google.com}
24912837Sgabeblack@google.com
25012837Sgabeblack@google.comsc_time &
25113040Sgabeblack@google.comsc_time::operator *= (double d)
25212837Sgabeblack@google.com{
25313040Sgabeblack@google.com    val = static_cast<int64_t>(static_cast<double>(val) * d + 0.5);
25412837Sgabeblack@google.com    return *this;
25512837Sgabeblack@google.com}
25612837Sgabeblack@google.com
25712837Sgabeblack@google.comsc_time &
25813040Sgabeblack@google.comsc_time::operator /= (double d)
25912837Sgabeblack@google.com{
26013040Sgabeblack@google.com    val = static_cast<int64_t>(static_cast<double>(val) / d + 0.5);
26112837Sgabeblack@google.com    return *this;
26212837Sgabeblack@google.com}
26312837Sgabeblack@google.com
26412837Sgabeblack@google.comvoid
26512983Sgabeblack@google.comsc_time::print(std::ostream &os) const
26612837Sgabeblack@google.com{
26713263Sgabeblack@google.com    os << sc_time_tuple(*this).to_string();
26812837Sgabeblack@google.com}
26912837Sgabeblack@google.com
27012923Sgabeblack@google.comsc_time
27112983Sgabeblack@google.comsc_time::from_value(sc_dt::uint64 u)
27212923Sgabeblack@google.com{
27313195Sgabeblack@google.com    if (u)
27413195Sgabeblack@google.com        attemptToFixTime();
27512983Sgabeblack@google.com    sc_time t;
27612983Sgabeblack@google.com    t.val = u;
27712983Sgabeblack@google.com    return t;
27812923Sgabeblack@google.com}
27912923Sgabeblack@google.com
28012923Sgabeblack@google.comsc_time
28113057Sgabeblack@google.comsc_time::from_seconds(double d)
28212923Sgabeblack@google.com{
28313057Sgabeblack@google.com    sc_time t;
28413057Sgabeblack@google.com    set(&t, d, SC_SEC);
28513057Sgabeblack@google.com    return t;
28612923Sgabeblack@google.com}
28712923Sgabeblack@google.com
28812923Sgabeblack@google.comsc_time
28912923Sgabeblack@google.comsc_time::from_string(const char *str)
29012923Sgabeblack@google.com{
29112923Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
29212923Sgabeblack@google.com    return sc_time();
29312923Sgabeblack@google.com}
29412923Sgabeblack@google.com
29512837Sgabeblack@google.comconst sc_time
29612989Sgabeblack@google.comoperator + (const sc_time &a, const sc_time &b)
29712837Sgabeblack@google.com{
29812989Sgabeblack@google.com    return sc_time::from_value(a.value() + b.value());
29912837Sgabeblack@google.com}
30012837Sgabeblack@google.com
30112837Sgabeblack@google.comconst sc_time
30212989Sgabeblack@google.comoperator - (const sc_time &a, const sc_time &b)
30312837Sgabeblack@google.com{
30412989Sgabeblack@google.com    return sc_time::from_value(a.value() - b.value());
30512837Sgabeblack@google.com}
30612837Sgabeblack@google.com
30712837Sgabeblack@google.comconst sc_time
30813040Sgabeblack@google.comoperator * (const sc_time &t, double d)
30912837Sgabeblack@google.com{
31013040Sgabeblack@google.com    volatile double tmp = static_cast<double>(t.value()) * d + 0.5;
31113040Sgabeblack@google.com    return sc_time::from_value(static_cast<int64_t>(tmp));
31212837Sgabeblack@google.com}
31312837Sgabeblack@google.com
31412837Sgabeblack@google.comconst sc_time
31513040Sgabeblack@google.comoperator * (double d, const sc_time &t)
31612837Sgabeblack@google.com{
31713040Sgabeblack@google.com    volatile double tmp = d * static_cast<double>(t.value()) + 0.5;
31813040Sgabeblack@google.com    return sc_time::from_value(static_cast<int64_t>(tmp));
31912837Sgabeblack@google.com}
32012837Sgabeblack@google.com
32112837Sgabeblack@google.comconst sc_time
32213040Sgabeblack@google.comoperator / (const sc_time &t, double d)
32312837Sgabeblack@google.com{
32413040Sgabeblack@google.com    volatile double tmp = static_cast<double>(t.value()) / d + 0.5;
32513040Sgabeblack@google.com    return sc_time::from_value(static_cast<int64_t>(tmp));
32612837Sgabeblack@google.com}
32712837Sgabeblack@google.com
32812837Sgabeblack@google.comdouble
32913040Sgabeblack@google.comoperator / (const sc_time &t1, const sc_time &t2)
33012837Sgabeblack@google.com{
33113040Sgabeblack@google.com    return t1.to_double() / t2.to_double();
33212837Sgabeblack@google.com}
33312837Sgabeblack@google.com
33412837Sgabeblack@google.comstd::ostream &
33512983Sgabeblack@google.comoperator << (std::ostream &os, const sc_time &t)
33612837Sgabeblack@google.com{
33712983Sgabeblack@google.com    t.print(os);
33812837Sgabeblack@google.com    return os;
33912837Sgabeblack@google.com}
34012837Sgabeblack@google.com
34112837Sgabeblack@google.comconst sc_time SC_ZERO_TIME;
34212837Sgabeblack@google.com
34312837Sgabeblack@google.comvoid
34413149Sgabeblack@google.comsc_set_time_resolution(double d, sc_time_unit tu)
34512837Sgabeblack@google.com{
34613151Sgabeblack@google.com    if (d <= 0.0) {
34713149Sgabeblack@google.com        SC_REPORT_ERROR("(E514) set time resolution failed",
34813149Sgabeblack@google.com                "value not positive");
34913149Sgabeblack@google.com    }
35013149Sgabeblack@google.com    double dummy;
35113149Sgabeblack@google.com    if (modf(log10(d), &dummy) != 0.0) {
35213149Sgabeblack@google.com        SC_REPORT_ERROR("(E514) set time resolution failed",
35313149Sgabeblack@google.com                "value not a power of ten");
35413149Sgabeblack@google.com    }
35513149Sgabeblack@google.com    if (sc_is_running()) {
35613149Sgabeblack@google.com        SC_REPORT_ERROR("(E514) set time resolution failed",
35713149Sgabeblack@google.com                "simulation running");
35813149Sgabeblack@google.com    }
35913149Sgabeblack@google.com    static bool specified = false;
36013149Sgabeblack@google.com    if (specified) {
36113149Sgabeblack@google.com        SC_REPORT_ERROR("(E514) set time resolution failed",
36213149Sgabeblack@google.com                "already specified");
36313149Sgabeblack@google.com    }
36413149Sgabeblack@google.com    // This won't detect the timescale being fixed outside of systemc, but
36513149Sgabeblack@google.com    // it's at least some protection.
36613149Sgabeblack@google.com    if (timeFixed) {
36713149Sgabeblack@google.com        SC_REPORT_ERROR("(E514) set time resolution failed",
36813149Sgabeblack@google.com                "sc_time object(s) constructed");
36913149Sgabeblack@google.com    }
37013149Sgabeblack@google.com
37113252Sgabeblack@google.com    double seconds = d * sc_gem5::TimeUnitScale[tu];
37213252Sgabeblack@google.com    if (seconds < sc_gem5::TimeUnitScale[SC_FS]) {
37313149Sgabeblack@google.com        SC_REPORT_ERROR("(E514) set time resolution failed",
37413149Sgabeblack@google.com                "value smaller than 1 fs");
37513149Sgabeblack@google.com    }
37613195Sgabeblack@google.com
37713195Sgabeblack@google.com    if (seconds > defaultUnit) {
37813195Sgabeblack@google.com        SC_REPORT_WARNING(
37913195Sgabeblack@google.com                "(W516) default time unit changed to time resolution", "");
38013195Sgabeblack@google.com        defaultUnit = seconds;
38113195Sgabeblack@google.com    }
38213195Sgabeblack@google.com
38313195Sgabeblack@google.com    // Get rid of fractional parts of d.
38413195Sgabeblack@google.com    while (d < 1.0 && tu > SC_FS) {
38513195Sgabeblack@google.com        d *= 1000;
38613195Sgabeblack@google.com        tu = (sc_time_unit)(tu - 1);
38713195Sgabeblack@google.com    }
38813195Sgabeblack@google.com
38913252Sgabeblack@google.com    Tick ticks_per_second =
39013252Sgabeblack@google.com        sc_gem5::TimeUnitFrequency[tu] / static_cast<Tick>(d);
39113149Sgabeblack@google.com    setGlobalFrequency(ticks_per_second);
39213149Sgabeblack@google.com    specified = true;
39312837Sgabeblack@google.com}
39412837Sgabeblack@google.com
39512837Sgabeblack@google.comsc_time
39612837Sgabeblack@google.comsc_get_time_resolution()
39712837Sgabeblack@google.com{
39813149Sgabeblack@google.com    return sc_time::from_value(1);
39912837Sgabeblack@google.com}
40012837Sgabeblack@google.com
40112837Sgabeblack@google.comconst sc_time &
40212837Sgabeblack@google.comsc_max_time()
40312837Sgabeblack@google.com{
40412989Sgabeblack@google.com    static const sc_time MaxScTime = sc_time::from_value(MaxTick);
40512989Sgabeblack@google.com    return MaxScTime;
40612837Sgabeblack@google.com}
40712837Sgabeblack@google.com
40812916Sgabeblack@google.comvoid
40913124Sgabeblack@google.comsc_set_default_time_unit(double d, sc_time_unit tu)
41012916Sgabeblack@google.com{
41113151Sgabeblack@google.com    if (d < 0.0) {
41213151Sgabeblack@google.com        SC_REPORT_ERROR("(E515) set default time unit failed",
41313151Sgabeblack@google.com                "value not positive");
41413151Sgabeblack@google.com    }
41513151Sgabeblack@google.com    double dummy;
41613151Sgabeblack@google.com    if (modf(log10(d), &dummy) != 0.0) {
41713151Sgabeblack@google.com        SC_REPORT_ERROR("(E515) set default time unit failed",
41813151Sgabeblack@google.com                "value not a power of ten");
41913151Sgabeblack@google.com    }
42013151Sgabeblack@google.com    if (sc_is_running()) {
42113151Sgabeblack@google.com        SC_REPORT_ERROR("(E515) set default time unit failed",
42213151Sgabeblack@google.com                "simulation running");
42313151Sgabeblack@google.com    }
42413151Sgabeblack@google.com    static bool specified = false;
42513151Sgabeblack@google.com    if (specified) {
42613151Sgabeblack@google.com        SC_REPORT_ERROR("(E515) set default time unit failed",
42713151Sgabeblack@google.com                "already specified");
42813151Sgabeblack@google.com    }
42913151Sgabeblack@google.com    // This won't detect the timescale being fixed outside of systemc, but
43013151Sgabeblack@google.com    // it's at least some protection.
43113151Sgabeblack@google.com    if (timeFixed) {
43213151Sgabeblack@google.com        SC_REPORT_ERROR("(E515) set default time unit failed",
43313151Sgabeblack@google.com                "sc_time object(s) constructed");
43413151Sgabeblack@google.com    }
43513151Sgabeblack@google.com
43613151Sgabeblack@google.com    // Normalize d to seconds.
43713252Sgabeblack@google.com    defaultUnit = d * sc_gem5::TimeUnitScale[tu];
43813151Sgabeblack@google.com    specified = true;
43913247Sgabeblack@google.com
44013247Sgabeblack@google.com    double resolution = SimClock::Float::Hz;
44113247Sgabeblack@google.com    if (resolution == 0.0)
44213252Sgabeblack@google.com        resolution = sc_gem5::TimeUnitScale[SC_PS];
44313247Sgabeblack@google.com    if (defaultUnit < resolution) {
44413247Sgabeblack@google.com        SC_REPORT_ERROR("(E515) set default time unit failed",
44513247Sgabeblack@google.com                "value smaller than time resolution");
44613247Sgabeblack@google.com    }
44712916Sgabeblack@google.com}
44812916Sgabeblack@google.com
44912916Sgabeblack@google.comsc_time
45012916Sgabeblack@google.comsc_get_default_time_unit()
45112916Sgabeblack@google.com{
45213124Sgabeblack@google.com    return sc_time(defaultUnit, SC_SEC);
45312916Sgabeblack@google.com}
45412916Sgabeblack@google.com
45513263Sgabeblack@google.comsc_time_tuple::sc_time_tuple(const sc_time &t) :
45613263Sgabeblack@google.com    _value(), _unit(SC_SEC), _set(true)
45712927Sgabeblack@google.com{
45813263Sgabeblack@google.com    if (!t.value())
45913263Sgabeblack@google.com        return;
46013263Sgabeblack@google.com
46113263Sgabeblack@google.com    Tick frequency = SimClock::Frequency;
46213263Sgabeblack@google.com
46313263Sgabeblack@google.com    // Shrink the frequency by scaling down the time period, ie converting
46413263Sgabeblack@google.com    // it from cycles per second to cycles per millisecond, etc.
46513263Sgabeblack@google.com    while (_unit > 1 && (frequency % 1000 == 0)) {
46613263Sgabeblack@google.com        _unit = (sc_time_unit)((int)_unit - 1);
46713263Sgabeblack@google.com        frequency /= 1000;
46813263Sgabeblack@google.com    }
46913263Sgabeblack@google.com
47013263Sgabeblack@google.com    // Convert the frequency into a period.
47113263Sgabeblack@google.com    Tick period;
47213263Sgabeblack@google.com    if (frequency > 1) {
47313263Sgabeblack@google.com        _unit = (sc_time_unit)((int)_unit - 1);
47413263Sgabeblack@google.com        period = 1000 / frequency;
47513263Sgabeblack@google.com    } else {
47613263Sgabeblack@google.com        period = frequency;
47713263Sgabeblack@google.com    }
47813263Sgabeblack@google.com
47913263Sgabeblack@google.com    // Scale our integer value by the period.
48013263Sgabeblack@google.com    _value = t.value() * period;
48113263Sgabeblack@google.com
48213263Sgabeblack@google.com    // Shrink the scaled time value by increasing the size of the units
48313263Sgabeblack@google.com    // it's measured by, avoiding fractional parts.
48413263Sgabeblack@google.com    while (_unit < SC_SEC && (_value % 1000) == 0) {
48513263Sgabeblack@google.com        _unit = (sc_time_unit)((int)_unit + 1);
48613263Sgabeblack@google.com        _value /= 1000;
48713263Sgabeblack@google.com    }
48812927Sgabeblack@google.com}
48912927Sgabeblack@google.com
49012927Sgabeblack@google.combool
49112927Sgabeblack@google.comsc_time_tuple::has_value() const
49212927Sgabeblack@google.com{
49313263Sgabeblack@google.com    return _set;
49412927Sgabeblack@google.com}
49512927Sgabeblack@google.com
49613263Sgabeblack@google.comsc_dt::uint64 sc_time_tuple::value() const { return _value; }
49712927Sgabeblack@google.com
49812927Sgabeblack@google.comconst char *
49912927Sgabeblack@google.comsc_time_tuple::unit_symbol() const
50012927Sgabeblack@google.com{
50113263Sgabeblack@google.com    return sc_gem5::TimeUnitNames[_unit];
50212927Sgabeblack@google.com}
50312927Sgabeblack@google.com
50413263Sgabeblack@google.comdouble sc_time_tuple::to_double() const { return static_cast<double>(_value); }
50512927Sgabeblack@google.com
50612927Sgabeblack@google.comstd::string
50712927Sgabeblack@google.comsc_time_tuple::to_string() const
50812927Sgabeblack@google.com{
50913263Sgabeblack@google.com    std::ostringstream ss;
51013263Sgabeblack@google.com    ss << _value << ' ' << unit_symbol();
51113263Sgabeblack@google.com    return ss.str();
51212927Sgabeblack@google.com}
51312927Sgabeblack@google.com
51412837Sgabeblack@google.com} // namespace sc_core
515