1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
/*
* Copyright 2018 Google, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer;
* redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution;
* neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Authors: Gabe Black
*/
#include <sstream>
#include <vector>
#include "base/logging.hh"
#include "base/types.hh"
#include "python/pybind11/pybind.hh"
#include "sim/core.hh"
#include "systemc/core/python.hh"
#include "systemc/core/time.hh"
#include "systemc/ext/core/sc_main.hh"
#include "systemc/ext/core/sc_time.hh"
#include "systemc/ext/utils/sc_report_handler.hh"
namespace sc_core
{
namespace
{
bool timeFixed = false;
bool pythonReady = false;
struct SetInfo
{
SetInfo(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu) :
time(time), d(d), tu(tu)
{}
::sc_core::sc_time *time;
double d;
::sc_core::sc_time_unit tu;
};
std::vector<SetInfo> toSet;
void
setWork(sc_time *time, double d, ::sc_core::sc_time_unit tu)
{
double scale = sc_gem5::TimeUnitScale[tu] * SimClock::Float::s;
// Accellera claims there is a linux bug, and that these next two
// lines work around them.
volatile double tmp = d * scale + 0.5;
*time = sc_time::from_value(static_cast<uint64_t>(tmp));
}
void
fixTime()
{
auto ticks = pybind11::module::import("m5.ticks");
auto fix_global_frequency = ticks.attr("fixGlobalFrequency");
fix_global_frequency();
for (auto &t: toSet)
setWork(t.time, t.d, t.tu);
toSet.clear();
}
void
attemptToFixTime()
{
// Only fix time once.
if (!timeFixed) {
timeFixed = true;
// If we've run, python is working and we haven't fixed time yet.
if (pythonReady)
fixTime();
}
}
void
setGlobalFrequency(Tick ticks_per_second)
{
auto ticks = pybind11::module::import("m5.ticks");
auto set_global_frequency = ticks.attr("setGlobalFrequency");
set_global_frequency(ticks_per_second);
fixTime();
}
void
set(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu)
{
if (d != 0)
attemptToFixTime();
if (pythonReady) {
// Time should be working. Set up this sc_time.
setWork(time, d, tu);
} else {
// Time isn't set up yet. Defer setting up this sc_time.
toSet.emplace_back(time, d, tu);
}
}
class TimeSetter : public ::sc_gem5::PythonReadyFunc
{
public:
TimeSetter() : ::sc_gem5::PythonReadyFunc() {}
void
run() override
{
// Record that we've run and python/pybind should be usable.
pythonReady = true;
// If time is already fixed, let python know.
if (timeFixed)
fixTime();
}
} timeSetter;
double defaultUnit = 1.0e-9;
} // anonymous namespace
sc_time::sc_time() : val(0) {}
sc_time::sc_time(double d, sc_time_unit tu)
{
val = 0;
set(this, d, tu);
}
sc_time::sc_time(const sc_time &t)
{
val = t.val;
}
sc_time::sc_time(double d, const char *unit)
{
sc_time_unit tu;
for (tu = SC_FS; tu <= SC_SEC; tu = (sc_time_unit)(tu + 1)) {
if (strcmp(unit, sc_gem5::TimeUnitNames[tu]) == 0 ||
strcmp(unit, sc_gem5::TimeUnitConstantNames[tu]) == 0) {
break;
}
}
if (tu > SC_SEC) {
SC_REPORT_ERROR("(E567) sc_time conversion failed",
"invalid unit given");
val = 0;
return;
}
set(this, d, tu);
}
sc_time::sc_time(double d, bool scale)
{
double scaler = scale ? defaultUnit : SimClock::Float::Hz;
set(this, d * scaler, SC_SEC);
}
sc_time::sc_time(sc_dt::uint64 v, bool scale)
{
double scaler = scale ? defaultUnit : SimClock::Float::Hz;
set(this, static_cast<double>(v) * scaler, SC_SEC);
}
sc_time &
sc_time::operator = (const sc_time &t)
{
val = t.val;
return *this;
}
sc_dt::uint64
sc_time::value() const
{
return val;
}
double
sc_time::to_double() const
{
return static_cast<double>(val);
}
double
sc_time::to_seconds() const
{
return to_double() * SimClock::Float::Hz;
}
const std::string
sc_time::to_string() const
{
std::ostringstream ss;
print(ss);
return ss.str();
}
bool
sc_time::operator == (const sc_time &t) const
{
return val == t.val;
}
bool
sc_time::operator != (const sc_time &t) const
{
return val != t.val;
}
bool
sc_time::operator < (const sc_time &t) const
{
return val < t.val;
}
bool
sc_time::operator <= (const sc_time &t) const
{
return val <= t.val;
}
bool
sc_time::operator > (const sc_time &t) const
{
return val > t.val;
}
bool
sc_time::operator >= (const sc_time &t) const
{
return val >= t.val;
}
sc_time &
sc_time::operator += (const sc_time &t)
{
val += t.val;
return *this;
}
sc_time &
sc_time::operator -= (const sc_time &t)
{
val -= t.val;
return *this;
}
sc_time &
sc_time::operator *= (double d)
{
val = static_cast<int64_t>(static_cast<double>(val) * d + 0.5);
return *this;
}
sc_time &
sc_time::operator /= (double d)
{
val = static_cast<int64_t>(static_cast<double>(val) / d + 0.5);
return *this;
}
void
sc_time::print(std::ostream &os) const
{
os << sc_time_tuple(*this).to_string();
}
sc_time
sc_time::from_value(sc_dt::uint64 u)
{
if (u)
attemptToFixTime();
sc_time t;
t.val = u;
return t;
}
sc_time
sc_time::from_seconds(double d)
{
sc_time t;
set(&t, d, SC_SEC);
return t;
}
sc_time
sc_time::from_string(const char *str)
{
char *end = nullptr;
double d = str ? std::strtod(str, &end) : 0.0;
if (str == end || d < 0.0) {
SC_REPORT_ERROR("(E567) sc_time conversion failed",
"invalid value given");
return SC_ZERO_TIME;
}
while (*end && std::isspace(*end))
end++;
return sc_time(d, end);
}
const sc_time
operator + (const sc_time &a, const sc_time &b)
{
return sc_time::from_value(a.value() + b.value());
}
const sc_time
operator - (const sc_time &a, const sc_time &b)
{
return sc_time::from_value(a.value() - b.value());
}
const sc_time
operator * (const sc_time &t, double d)
{
volatile double tmp = static_cast<double>(t.value()) * d + 0.5;
return sc_time::from_value(static_cast<int64_t>(tmp));
}
const sc_time
operator * (double d, const sc_time &t)
{
volatile double tmp = d * static_cast<double>(t.value()) + 0.5;
return sc_time::from_value(static_cast<int64_t>(tmp));
}
const sc_time
operator / (const sc_time &t, double d)
{
volatile double tmp = static_cast<double>(t.value()) / d + 0.5;
return sc_time::from_value(static_cast<int64_t>(tmp));
}
double
operator / (const sc_time &t1, const sc_time &t2)
{
return t1.to_double() / t2.to_double();
}
std::ostream &
operator << (std::ostream &os, const sc_time &t)
{
t.print(os);
return os;
}
const sc_time SC_ZERO_TIME;
void
sc_set_time_resolution(double d, sc_time_unit tu)
{
if (d <= 0.0) {
SC_REPORT_ERROR("(E514) set time resolution failed",
"value not positive");
}
double dummy;
if (modf(log10(d), &dummy) != 0.0) {
SC_REPORT_ERROR("(E514) set time resolution failed",
"value not a power of ten");
}
if (sc_is_running()) {
SC_REPORT_ERROR("(E514) set time resolution failed",
"simulation running");
}
static bool specified = false;
if (specified) {
SC_REPORT_ERROR("(E514) set time resolution failed",
"already specified");
}
// This won't detect the timescale being fixed outside of systemc, but
// it's at least some protection.
if (timeFixed) {
SC_REPORT_ERROR("(E514) set time resolution failed",
"sc_time object(s) constructed");
}
double seconds = d * sc_gem5::TimeUnitScale[tu];
if (seconds < sc_gem5::TimeUnitScale[SC_FS]) {
SC_REPORT_ERROR("(E514) set time resolution failed",
"value smaller than 1 fs");
}
if (seconds > defaultUnit) {
SC_REPORT_WARNING(
"(W516) default time unit changed to time resolution", "");
defaultUnit = seconds;
}
// Get rid of fractional parts of d.
while (d < 1.0 && tu > SC_FS) {
d *= 1000;
tu = (sc_time_unit)(tu - 1);
}
Tick ticks_per_second =
sc_gem5::TimeUnitFrequency[tu] / static_cast<Tick>(d);
setGlobalFrequency(ticks_per_second);
specified = true;
}
sc_time
sc_get_time_resolution()
{
return sc_time::from_value(1);
}
const sc_time &
sc_max_time()
{
static const sc_time MaxScTime = sc_time::from_value(MaxTick);
return MaxScTime;
}
void
sc_set_default_time_unit(double d, sc_time_unit tu)
{
if (d < 0.0) {
SC_REPORT_ERROR("(E515) set default time unit failed",
"value not positive");
}
double dummy;
if (modf(log10(d), &dummy) != 0.0) {
SC_REPORT_ERROR("(E515) set default time unit failed",
"value not a power of ten");
}
if (sc_is_running()) {
SC_REPORT_ERROR("(E515) set default time unit failed",
"simulation running");
}
static bool specified = false;
if (specified) {
SC_REPORT_ERROR("(E515) set default time unit failed",
"already specified");
}
// This won't detect the timescale being fixed outside of systemc, but
// it's at least some protection.
if (timeFixed) {
SC_REPORT_ERROR("(E515) set default time unit failed",
"sc_time object(s) constructed");
}
// Normalize d to seconds.
defaultUnit = d * sc_gem5::TimeUnitScale[tu];
specified = true;
double resolution = SimClock::Float::Hz;
if (resolution == 0.0)
resolution = sc_gem5::TimeUnitScale[SC_PS];
if (defaultUnit < resolution) {
SC_REPORT_ERROR("(E515) set default time unit failed",
"value smaller than time resolution");
}
}
sc_time
sc_get_default_time_unit()
{
return sc_time(defaultUnit, SC_SEC);
}
sc_time_tuple::sc_time_tuple(const sc_time &t) :
_value(), _unit(SC_SEC), _set(true)
{
if (!t.value())
return;
Tick frequency = SimClock::Frequency;
// Shrink the frequency by scaling down the time period, ie converting
// it from cycles per second to cycles per millisecond, etc.
while (_unit > 1 && (frequency % 1000 == 0)) {
_unit = (sc_time_unit)((int)_unit - 1);
frequency /= 1000;
}
// Convert the frequency into a period.
Tick period;
if (frequency > 1) {
_unit = (sc_time_unit)((int)_unit - 1);
period = 1000 / frequency;
} else {
period = frequency;
}
// Scale our integer value by the period.
_value = t.value() * period;
// Shrink the scaled time value by increasing the size of the units
// it's measured by, avoiding fractional parts.
while (_unit < SC_SEC && (_value % 1000) == 0) {
_unit = (sc_time_unit)((int)_unit + 1);
_value /= 1000;
}
}
bool
sc_time_tuple::has_value() const
{
return _set;
}
sc_dt::uint64 sc_time_tuple::value() const { return _value; }
const char *
sc_time_tuple::unit_symbol() const
{
return sc_gem5::TimeUnitNames[_unit];
}
double sc_time_tuple::to_double() const { return static_cast<double>(_value); }
std::string
sc_time_tuple::to_string() const
{
std::ostringstream ss;
ss << _value << ' ' << unit_symbol();
return ss.str();
}
} // namespace sc_core
|