sc_time.cc revision 12989:f5e0cebe6999
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 "base/logging.hh"
31#include "base/types.hh"
32#include "python/pybind11/pybind.hh"
33#include "systemc/ext/core/sc_time.hh"
34
35namespace sc_core
36{
37
38namespace
39{
40
41const char *TimeUnitNames[] = {
42    [SC_FS] = "fs",
43    [SC_PS] = "ps",
44    [SC_NS] = "ns",
45    [SC_US] = "us",
46    [SC_MS] = "ms",
47    [SC_SEC] = "s"
48};
49
50double TimeUnitScale[] = {
51    [SC_FS] = 1.0e-15,
52    [SC_PS] = 1.0e-12,
53    [SC_NS] = 1.0e-9,
54    [SC_US] = 1.0e-6,
55    [SC_MS] = 1.0e-3,
56    [SC_SEC] = 1.0
57};
58
59void
60fixTimeResolution()
61{
62    static bool fixed = false;
63    if (fixed)
64        return;
65
66    auto ticks = pybind11::module::import("m5.ticks");
67    auto fix_global_frequency = ticks.attr("fixGlobalFrequency");
68    fix_global_frequency();
69    fixed = true;
70}
71
72} // anonymous namespace
73
74sc_time::sc_time() : val(0) {}
75
76sc_time::sc_time(double d, sc_time_unit tu)
77{
78    val = 0;
79    if (d != 0) {
80        fixTimeResolution();
81        //XXX Assuming the time resolution is 1ps.
82        double scale = TimeUnitScale[tu] / TimeUnitScale[SC_PS];
83        // Accellera claims there is a linux bug, and that these next two
84        // lines work around them.
85        volatile double tmp = d * scale + 0.5;
86        val = static_cast<uint64_t>(tmp);
87    }
88}
89
90sc_time::sc_time(const sc_time &t)
91{
92    val = t.val;
93}
94
95sc_time::sc_time(double, bool)
96{
97    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
98}
99
100sc_time::sc_time(sc_dt::uint64, bool)
101{
102    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
103}
104
105sc_time &
106sc_time::operator = (const sc_time &t)
107{
108    val = t.val;
109    return *this;
110}
111
112sc_dt::uint64
113sc_time::value() const
114{
115    return val;
116}
117
118double
119sc_time::to_double() const
120{
121    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
122    return 0.0;
123}
124double
125sc_time::to_seconds() const
126{
127    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
128    return 0.0;
129}
130
131const std::string
132sc_time::to_string() const
133{
134    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
135    return "";
136}
137
138bool
139sc_time::operator == (const sc_time &t) const
140{
141    return val == t.val;
142}
143
144bool
145sc_time::operator != (const sc_time &t) const
146{
147    return val != t.val;
148}
149
150bool
151sc_time::operator < (const sc_time &t) const
152{
153    return val < t.val;
154}
155
156bool
157sc_time::operator <= (const sc_time &t) const
158{
159    return val <= t.val;
160}
161
162bool
163sc_time::operator > (const sc_time &t) const
164{
165    return val > t.val;
166}
167
168bool
169sc_time::operator >= (const sc_time &t) const
170{
171    return val >= t.val;
172}
173
174sc_time &
175sc_time::operator += (const sc_time &t)
176{
177    val += t.val;
178    return *this;
179}
180
181sc_time &
182sc_time::operator -= (const sc_time &t)
183{
184    val -= t.val;
185    return *this;
186}
187
188sc_time &
189sc_time::operator *= (double)
190{
191    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
192    return *this;
193}
194
195sc_time &
196sc_time::operator /= (double)
197{
198    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
199    return *this;
200}
201
202void
203sc_time::print(std::ostream &os) const
204{
205    if (val == 0) {
206        os << "0 s";
207    } else {
208        //XXX Assuming the time resolution is 1ps.
209        sc_time_unit tu = SC_PS;
210        uint64_t scaled = val;
211        while (tu < SC_SEC && (scaled % 1000) == 0) {
212            tu = (sc_time_unit)((int)tu + 1);
213            scaled /= 1000;
214        }
215
216        os << scaled << ' ' << TimeUnitNames[tu];
217    }
218}
219
220sc_time
221sc_time::from_value(sc_dt::uint64 u)
222{
223    sc_time t;
224    t.val = u;
225    return t;
226}
227
228sc_time
229sc_time::from_seconds(double)
230{
231    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
232    return sc_time();
233}
234
235sc_time
236sc_time::from_string(const char *str)
237{
238    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
239    return sc_time();
240}
241
242const sc_time
243operator + (const sc_time &a, const sc_time &b)
244{
245    return sc_time::from_value(a.value() + b.value());
246}
247
248const sc_time
249operator - (const sc_time &a, const sc_time &b)
250{
251    return sc_time::from_value(a.value() - b.value());
252}
253
254const sc_time
255operator * (const sc_time &, double)
256{
257    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
258    return sc_time();
259}
260
261const sc_time
262operator * (double, const sc_time &)
263{
264    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
265    return sc_time();
266}
267
268const sc_time
269operator / (const sc_time &, double)
270{
271    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
272    return sc_time();
273}
274
275double
276operator / (const sc_time &, const sc_time &)
277{
278    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
279    return 0.0;
280}
281
282std::ostream &
283operator << (std::ostream &os, const sc_time &t)
284{
285    t.print(os);
286    return os;
287}
288
289const sc_time SC_ZERO_TIME;
290
291void
292sc_set_time_resolution(double, sc_time_unit)
293{
294    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
295}
296
297sc_time
298sc_get_time_resolution()
299{
300    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
301    return sc_time();
302}
303
304const sc_time &
305sc_max_time()
306{
307    static const sc_time MaxScTime = sc_time::from_value(MaxTick);
308    return MaxScTime;
309}
310
311void
312sc_set_default_time_unit(double, sc_time_unit)
313{
314    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
315}
316
317sc_time
318sc_get_default_time_unit()
319{
320    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
321    return *(sc_time *)nullptr;
322}
323
324sc_time_tuple::sc_time_tuple(const sc_time &)
325{
326    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
327}
328
329bool
330sc_time_tuple::has_value() const
331{
332    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
333    return false;
334}
335
336sc_dt::uint64
337sc_time_tuple::value() const
338{
339    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
340    return 0;
341}
342
343const char *
344sc_time_tuple::unit_symbol() const
345{
346    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
347    return "";
348}
349
350double
351sc_time_tuple::to_double() const
352{
353    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
354    return 0.0;
355}
356
357std::string
358sc_time_tuple::to_string() const
359{
360    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
361    return "";
362}
363
364} // namespace sc_core
365