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