summaryrefslogtreecommitdiff
path: root/src/systemc/core/sc_time.cc
blob: 69d58e5122539284df95589ac5a4fd14a00831a0 (plain)
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
/*
 * 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 "systemc/core/python.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
{

const char *TimeUnitNames[] = {
    [SC_FS] = "fs",
    [SC_PS] = "ps",
    [SC_NS] = "ns",
    [SC_US] = "us",
    [SC_MS] = "ms",
    [SC_SEC] = "s"
};

double TimeUnitScale[] = {
    [SC_FS] = 1.0e-15,
    [SC_PS] = 1.0e-12,
    [SC_NS] = 1.0e-9,
    [SC_US] = 1.0e-6,
    [SC_MS] = 1.0e-3,
    [SC_SEC] = 1.0
};

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)
{
    //XXX Assuming the time resolution is 1ps.
    double scale = TimeUnitScale[tu] / TimeUnitScale[SC_PS];
    // 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
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)
{
    // 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();
    }
    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;
    if (d != 0)
        set(this, d, tu);
}

sc_time::sc_time(const sc_time &t)
{
    val = t.val;
}

sc_time::sc_time(double d, bool scale)
{
    //XXX Assuming the time resolution is 1ps.
    if (scale)
        set(this, d * defaultUnit, SC_SEC);
    else
        set(this, d, SC_PS);
}

sc_time::sc_time(sc_dt::uint64 v, bool scale)
{
    //XXX Assuming the time resolution is 1ps.
    if (scale)
        set(this, static_cast<double>(v) * defaultUnit, SC_SEC);
    else
        set(this, static_cast<double>(v), SC_PS);
}

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
{
    double d = to_double();
    //XXX Assuming the time resolution is 1ps.
    double scale = TimeUnitScale[SC_PS] / TimeUnitScale[SC_SEC];
    return d * scale;
}

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
{
    if (val == 0) {
        os << "0 s";
    } else {
        //XXX Assuming the time resolution is 1ps.
        sc_time_unit tu = SC_PS;
        uint64_t scaled = val;
        while (tu < SC_SEC && (scaled % 1000) == 0) {
            tu = (sc_time_unit)((int)tu + 1);
            scaled /= 1000;
        }

        os << scaled << ' ' << TimeUnitNames[tu];
    }
}

sc_time
sc_time::from_value(sc_dt::uint64 u)
{
    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)
{
    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
    return sc_time();
}

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");
    }

    // Normalize d to seconds.
    d *= TimeUnitScale[tu];
    if (d < TimeUnitScale[SC_FS]) {
        SC_REPORT_ERROR("(E514) set time resolution failed",
                "value smaller than 1 fs");
    }
    // Change d from a period to a frequency.
    d = 1 / d;
    // Convert to integer ticks.
    Tick ticks_per_second = 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 * TimeUnitScale[tu];
    specified = true;
}

sc_time
sc_get_default_time_unit()
{
    return sc_time(defaultUnit, SC_SEC);
}

sc_time_tuple::sc_time_tuple(const sc_time &)
{
    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
}

bool
sc_time_tuple::has_value() const
{
    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
    return false;
}

sc_dt::uint64
sc_time_tuple::value() const
{
    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
    return 0;
}

const char *
sc_time_tuple::unit_symbol() const
{
    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
    return "";
}

double
sc_time_tuple::to_double() const
{
    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
    return 0.0;
}

std::string
sc_time_tuple::to_string() const
{
    warn("%s not implemented.\n", __PRETTY_FUNCTION__);
    return "";
}

} // namespace sc_core