sc_time.cc revision 12986
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
3012837Sgabeblack@google.com#include "base/logging.hh"
3112986Sgabeblack@google.com#include "python/pybind11/pybind.hh"
3212837Sgabeblack@google.com#include "systemc/ext/core/sc_time.hh"
3312837Sgabeblack@google.com
3412837Sgabeblack@google.comnamespace sc_core
3512837Sgabeblack@google.com{
3612837Sgabeblack@google.com
3712983Sgabeblack@google.comnamespace
3812837Sgabeblack@google.com{
3912983Sgabeblack@google.com
4012983Sgabeblack@google.comconst char *TimeUnitNames[] = {
4112983Sgabeblack@google.com    [SC_FS] = "fs",
4212983Sgabeblack@google.com    [SC_PS] = "ps",
4312983Sgabeblack@google.com    [SC_NS] = "ns",
4412983Sgabeblack@google.com    [SC_US] = "us",
4512983Sgabeblack@google.com    [SC_MS] = "ms",
4612983Sgabeblack@google.com    [SC_SEC] = "s"
4712983Sgabeblack@google.com};
4812983Sgabeblack@google.com
4912983Sgabeblack@google.comdouble TimeUnitScale[] = {
5012983Sgabeblack@google.com    [SC_FS] = 1.0e-15,
5112983Sgabeblack@google.com    [SC_PS] = 1.0e-12,
5212983Sgabeblack@google.com    [SC_NS] = 1.0e-9,
5312983Sgabeblack@google.com    [SC_US] = 1.0e-6,
5412983Sgabeblack@google.com    [SC_MS] = 1.0e-3,
5512983Sgabeblack@google.com    [SC_SEC] = 1.0
5612983Sgabeblack@google.com};
5712983Sgabeblack@google.com
5812986Sgabeblack@google.comvoid
5912986Sgabeblack@google.comfixTimeResolution()
6012986Sgabeblack@google.com{
6112986Sgabeblack@google.com    static bool fixed = false;
6212986Sgabeblack@google.com    if (fixed)
6312986Sgabeblack@google.com        return;
6412986Sgabeblack@google.com
6512986Sgabeblack@google.com    auto ticks = pybind11::module::import("m5.ticks");
6612986Sgabeblack@google.com    auto fix_global_frequency = ticks.attr("fixGlobalFrequency");
6712986Sgabeblack@google.com    fix_global_frequency();
6812986Sgabeblack@google.com    fixed = true;
6912986Sgabeblack@google.com}
7012986Sgabeblack@google.com
7112983Sgabeblack@google.com} // anonymous namespace
7212983Sgabeblack@google.com
7312983Sgabeblack@google.comsc_time::sc_time() : val(0) {}
7412983Sgabeblack@google.com
7512983Sgabeblack@google.comsc_time::sc_time(double d, sc_time_unit tu)
7612983Sgabeblack@google.com{
7712983Sgabeblack@google.com    val = 0;
7812983Sgabeblack@google.com    if (d != 0) {
7912986Sgabeblack@google.com        fixTimeResolution();
8012983Sgabeblack@google.com        //XXX Assuming the time resolution is 1ps.
8112983Sgabeblack@google.com        double scale = TimeUnitScale[tu] / TimeUnitScale[SC_PS];
8212983Sgabeblack@google.com        // Accellera claims there is a linux bug, and that these next two
8312983Sgabeblack@google.com        // lines work around them.
8412983Sgabeblack@google.com        volatile double tmp = d * scale + 0.5;
8512983Sgabeblack@google.com        val = static_cast<uint64_t>(tmp);
8612983Sgabeblack@google.com    }
8712837Sgabeblack@google.com}
8812837Sgabeblack@google.com
8912983Sgabeblack@google.comsc_time::sc_time(const sc_time &t)
9012837Sgabeblack@google.com{
9112983Sgabeblack@google.com    val = t.val;
9212837Sgabeblack@google.com}
9312837Sgabeblack@google.com
9412925Sgabeblack@google.comsc_time::sc_time(double, bool)
9512925Sgabeblack@google.com{
9612925Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
9712925Sgabeblack@google.com}
9812925Sgabeblack@google.com
9912925Sgabeblack@google.comsc_time::sc_time(sc_dt::uint64, bool)
10012925Sgabeblack@google.com{
10112925Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
10212925Sgabeblack@google.com}
10312925Sgabeblack@google.com
10412837Sgabeblack@google.comsc_time &
10512983Sgabeblack@google.comsc_time::operator = (const sc_time &t)
10612837Sgabeblack@google.com{
10712983Sgabeblack@google.com    val = t.val;
10812837Sgabeblack@google.com    return *this;
10912837Sgabeblack@google.com}
11012837Sgabeblack@google.com
11112837Sgabeblack@google.comsc_dt::uint64
11212837Sgabeblack@google.comsc_time::value() const
11312837Sgabeblack@google.com{
11412983Sgabeblack@google.com    return val;
11512837Sgabeblack@google.com}
11612837Sgabeblack@google.com
11712837Sgabeblack@google.comdouble
11812837Sgabeblack@google.comsc_time::to_double() const
11912837Sgabeblack@google.com{
12012837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
12112837Sgabeblack@google.com    return 0.0;
12212837Sgabeblack@google.com}
12312837Sgabeblack@google.comdouble
12412837Sgabeblack@google.comsc_time::to_seconds() const
12512837Sgabeblack@google.com{
12612837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
12712837Sgabeblack@google.com    return 0.0;
12812837Sgabeblack@google.com}
12912837Sgabeblack@google.com
13012837Sgabeblack@google.comconst std::string
13112837Sgabeblack@google.comsc_time::to_string() const
13212837Sgabeblack@google.com{
13312837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
13412837Sgabeblack@google.com    return "";
13512837Sgabeblack@google.com}
13612837Sgabeblack@google.com
13712837Sgabeblack@google.combool
13812983Sgabeblack@google.comsc_time::operator == (const sc_time &t) const
13912837Sgabeblack@google.com{
14012983Sgabeblack@google.com    return val == t.val;
14112837Sgabeblack@google.com}
14212837Sgabeblack@google.com
14312837Sgabeblack@google.combool
14412983Sgabeblack@google.comsc_time::operator != (const sc_time &t) const
14512837Sgabeblack@google.com{
14612983Sgabeblack@google.com    return val != t.val;
14712837Sgabeblack@google.com}
14812837Sgabeblack@google.com
14912837Sgabeblack@google.combool
15012983Sgabeblack@google.comsc_time::operator < (const sc_time &t) const
15112837Sgabeblack@google.com{
15212983Sgabeblack@google.com    return val < t.val;
15312837Sgabeblack@google.com}
15412837Sgabeblack@google.com
15512837Sgabeblack@google.combool
15612983Sgabeblack@google.comsc_time::operator <= (const sc_time &t) const
15712837Sgabeblack@google.com{
15812983Sgabeblack@google.com    return val <= t.val;
15912837Sgabeblack@google.com}
16012837Sgabeblack@google.com
16112837Sgabeblack@google.combool
16212983Sgabeblack@google.comsc_time::operator > (const sc_time &t) const
16312837Sgabeblack@google.com{
16412983Sgabeblack@google.com    return val > t.val;
16512837Sgabeblack@google.com}
16612837Sgabeblack@google.com
16712837Sgabeblack@google.combool
16812983Sgabeblack@google.comsc_time::operator >= (const sc_time &t) const
16912837Sgabeblack@google.com{
17012983Sgabeblack@google.com    return val >= t.val;
17112837Sgabeblack@google.com}
17212837Sgabeblack@google.com
17312837Sgabeblack@google.comsc_time &
17412983Sgabeblack@google.comsc_time::operator += (const sc_time &t)
17512837Sgabeblack@google.com{
17612983Sgabeblack@google.com    val += t.val;
17712837Sgabeblack@google.com    return *this;
17812837Sgabeblack@google.com}
17912837Sgabeblack@google.com
18012837Sgabeblack@google.comsc_time &
18112983Sgabeblack@google.comsc_time::operator -= (const sc_time &t)
18212837Sgabeblack@google.com{
18312983Sgabeblack@google.com    val -= t.val;
18412837Sgabeblack@google.com    return *this;
18512837Sgabeblack@google.com}
18612837Sgabeblack@google.com
18712837Sgabeblack@google.comsc_time &
18812837Sgabeblack@google.comsc_time::operator *= (double)
18912837Sgabeblack@google.com{
19012837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
19112837Sgabeblack@google.com    return *this;
19212837Sgabeblack@google.com}
19312837Sgabeblack@google.com
19412837Sgabeblack@google.comsc_time &
19512837Sgabeblack@google.comsc_time::operator /= (double)
19612837Sgabeblack@google.com{
19712837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
19812837Sgabeblack@google.com    return *this;
19912837Sgabeblack@google.com}
20012837Sgabeblack@google.com
20112837Sgabeblack@google.comvoid
20212983Sgabeblack@google.comsc_time::print(std::ostream &os) const
20312837Sgabeblack@google.com{
20412983Sgabeblack@google.com    if (val == 0) {
20512983Sgabeblack@google.com        os << "0 s";
20612983Sgabeblack@google.com    } else {
20712983Sgabeblack@google.com        //XXX Assuming the time resolution is 1ps.
20812983Sgabeblack@google.com        sc_time_unit tu = SC_PS;
20912983Sgabeblack@google.com        uint64_t scaled = val;
21012983Sgabeblack@google.com        while (tu < SC_SEC && (scaled % 1000) == 0) {
21112983Sgabeblack@google.com            tu = (sc_time_unit)((int)tu + 1);
21212983Sgabeblack@google.com            scaled /= 1000;
21312983Sgabeblack@google.com        }
21412983Sgabeblack@google.com
21512983Sgabeblack@google.com        os << scaled << ' ' << TimeUnitNames[tu];
21612983Sgabeblack@google.com    }
21712837Sgabeblack@google.com}
21812837Sgabeblack@google.com
21912923Sgabeblack@google.comsc_time
22012983Sgabeblack@google.comsc_time::from_value(sc_dt::uint64 u)
22112923Sgabeblack@google.com{
22212983Sgabeblack@google.com    sc_time t;
22312983Sgabeblack@google.com    t.val = u;
22412983Sgabeblack@google.com    return t;
22512923Sgabeblack@google.com}
22612923Sgabeblack@google.com
22712923Sgabeblack@google.comsc_time
22812923Sgabeblack@google.comsc_time::from_seconds(double)
22912923Sgabeblack@google.com{
23012923Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
23112923Sgabeblack@google.com    return sc_time();
23212923Sgabeblack@google.com}
23312923Sgabeblack@google.com
23412923Sgabeblack@google.comsc_time
23512923Sgabeblack@google.comsc_time::from_string(const char *str)
23612923Sgabeblack@google.com{
23712923Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
23812923Sgabeblack@google.com    return sc_time();
23912923Sgabeblack@google.com}
24012923Sgabeblack@google.com
24112837Sgabeblack@google.comconst sc_time
24212837Sgabeblack@google.comoperator + (const sc_time &, const sc_time &)
24312837Sgabeblack@google.com{
24412837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
24512837Sgabeblack@google.com    return sc_time();
24612837Sgabeblack@google.com}
24712837Sgabeblack@google.com
24812837Sgabeblack@google.comconst sc_time
24912837Sgabeblack@google.comoperator - (const sc_time &, const sc_time &)
25012837Sgabeblack@google.com{
25112837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
25212837Sgabeblack@google.com    return sc_time();
25312837Sgabeblack@google.com}
25412837Sgabeblack@google.com
25512837Sgabeblack@google.comconst sc_time
25612837Sgabeblack@google.comoperator * (const sc_time &, double)
25712837Sgabeblack@google.com{
25812837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
25912837Sgabeblack@google.com    return sc_time();
26012837Sgabeblack@google.com}
26112837Sgabeblack@google.com
26212837Sgabeblack@google.comconst sc_time
26312837Sgabeblack@google.comoperator * (double, const sc_time &)
26412837Sgabeblack@google.com{
26512837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
26612837Sgabeblack@google.com    return sc_time();
26712837Sgabeblack@google.com}
26812837Sgabeblack@google.com
26912837Sgabeblack@google.comconst sc_time
27012837Sgabeblack@google.comoperator / (const sc_time &, double)
27112837Sgabeblack@google.com{
27212837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
27312837Sgabeblack@google.com    return sc_time();
27412837Sgabeblack@google.com}
27512837Sgabeblack@google.com
27612837Sgabeblack@google.comdouble
27712837Sgabeblack@google.comoperator / (const sc_time &, const sc_time &)
27812837Sgabeblack@google.com{
27912837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
28012837Sgabeblack@google.com    return 0.0;
28112837Sgabeblack@google.com}
28212837Sgabeblack@google.com
28312837Sgabeblack@google.comstd::ostream &
28412983Sgabeblack@google.comoperator << (std::ostream &os, const sc_time &t)
28512837Sgabeblack@google.com{
28612983Sgabeblack@google.com    t.print(os);
28712837Sgabeblack@google.com    return os;
28812837Sgabeblack@google.com}
28912837Sgabeblack@google.com
29012837Sgabeblack@google.comconst sc_time SC_ZERO_TIME;
29112837Sgabeblack@google.com
29212837Sgabeblack@google.comvoid
29312837Sgabeblack@google.comsc_set_time_resolution(double, sc_time_unit)
29412837Sgabeblack@google.com{
29512837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
29612837Sgabeblack@google.com}
29712837Sgabeblack@google.com
29812837Sgabeblack@google.comsc_time
29912837Sgabeblack@google.comsc_get_time_resolution()
30012837Sgabeblack@google.com{
30112837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
30212837Sgabeblack@google.com    return sc_time();
30312837Sgabeblack@google.com}
30412837Sgabeblack@google.com
30512837Sgabeblack@google.comconst sc_time &
30612837Sgabeblack@google.comsc_max_time()
30712837Sgabeblack@google.com{
30812837Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
30912837Sgabeblack@google.com    return *(const sc_time *)nullptr;
31012837Sgabeblack@google.com}
31112837Sgabeblack@google.com
31212916Sgabeblack@google.comvoid
31312916Sgabeblack@google.comsc_set_default_time_unit(double, sc_time_unit)
31412916Sgabeblack@google.com{
31512916Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
31612916Sgabeblack@google.com}
31712916Sgabeblack@google.com
31812916Sgabeblack@google.comsc_time
31912916Sgabeblack@google.comsc_get_default_time_unit()
32012916Sgabeblack@google.com{
32112916Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
32212916Sgabeblack@google.com    return *(sc_time *)nullptr;
32312916Sgabeblack@google.com}
32412916Sgabeblack@google.com
32512927Sgabeblack@google.comsc_time_tuple::sc_time_tuple(const sc_time &)
32612927Sgabeblack@google.com{
32712927Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
32812927Sgabeblack@google.com}
32912927Sgabeblack@google.com
33012927Sgabeblack@google.combool
33112927Sgabeblack@google.comsc_time_tuple::has_value() const
33212927Sgabeblack@google.com{
33312927Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
33412927Sgabeblack@google.com    return false;
33512927Sgabeblack@google.com}
33612927Sgabeblack@google.com
33712927Sgabeblack@google.comsc_dt::uint64
33812927Sgabeblack@google.comsc_time_tuple::value() const
33912927Sgabeblack@google.com{
34012927Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
34112927Sgabeblack@google.com    return 0;
34212927Sgabeblack@google.com}
34312927Sgabeblack@google.com
34412927Sgabeblack@google.comconst char *
34512927Sgabeblack@google.comsc_time_tuple::unit_symbol() const
34612927Sgabeblack@google.com{
34712927Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
34812927Sgabeblack@google.com    return "";
34912927Sgabeblack@google.com}
35012927Sgabeblack@google.com
35112927Sgabeblack@google.comdouble
35212927Sgabeblack@google.comsc_time_tuple::to_double() const
35312927Sgabeblack@google.com{
35412927Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
35512927Sgabeblack@google.com    return 0.0;
35612927Sgabeblack@google.com}
35712927Sgabeblack@google.com
35812927Sgabeblack@google.comstd::string
35912927Sgabeblack@google.comsc_time_tuple::to_string() const
36012927Sgabeblack@google.com{
36112927Sgabeblack@google.com    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
36212927Sgabeblack@google.com    return "";
36312927Sgabeblack@google.com}
36412927Sgabeblack@google.com
36512837Sgabeblack@google.com} // namespace sc_core
366