Deleted Added
sdiff udiff text old ( 13177:7b750aeab360 ) new ( 13195:de9e5572ac44 )
full compact
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

--- 19 unchanged lines hidden (view full) ---

28 */
29
30#include <sstream>
31#include <vector>
32
33#include "base/logging.hh"
34#include "base/types.hh"
35#include "python/pybind11/pybind.hh"
36#include "systemc/core/python.hh"
37#include "systemc/ext/core/sc_main.hh"
38#include "systemc/ext/core/sc_time.hh"
39#include "systemc/ext/utils/sc_report_handler.hh"
40
41namespace sc_core
42{
43

--- 13 unchanged lines hidden (view full) ---

57 [SC_FS] = 1.0e-15,
58 [SC_PS] = 1.0e-12,
59 [SC_NS] = 1.0e-9,
60 [SC_US] = 1.0e-6,
61 [SC_MS] = 1.0e-3,
62 [SC_SEC] = 1.0
63};
64
65bool timeFixed = false;
66bool pythonReady = false;
67
68struct SetInfo
69{
70 SetInfo(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu) :
71 time(time), d(d), tu(tu)
72 {}
73
74 ::sc_core::sc_time *time;
75 double d;
76 ::sc_core::sc_time_unit tu;
77};
78std::vector<SetInfo> toSet;
79
80void
81setWork(sc_time *time, double d, ::sc_core::sc_time_unit tu)
82{
83 //XXX Assuming the time resolution is 1ps.
84 double scale = TimeUnitScale[tu] / TimeUnitScale[SC_PS];
85 // Accellera claims there is a linux bug, and that these next two
86 // lines work around them.
87 volatile double tmp = d * scale + 0.5;
88 *time = sc_time::from_value(static_cast<uint64_t>(tmp));
89}
90
91void
92fixTime()
93{
94 auto ticks = pybind11::module::import("m5.ticks");
95 auto fix_global_frequency = ticks.attr("fixGlobalFrequency");
96 fix_global_frequency();
97
98 for (auto &t: toSet)
99 setWork(t.time, t.d, t.tu);
100 toSet.clear();
101}
102
103void
104setGlobalFrequency(Tick ticks_per_second)
105{
106 auto ticks = pybind11::module::import("m5.ticks");
107 auto set_global_frequency = ticks.attr("setGlobalFrequency");
108 set_global_frequency(ticks_per_second);
109 fixTime();
110}
111
112void
113set(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu)
114{
115 // Only fix time once.
116 if (!timeFixed) {
117 timeFixed = true;
118
119 // If we've run, python is working and we haven't fixed time yet.
120 if (pythonReady)
121 fixTime();
122 }
123 if (pythonReady) {
124 // Time should be working. Set up this sc_time.
125 setWork(time, d, tu);
126 } else {
127 // Time isn't set up yet. Defer setting up this sc_time.
128 toSet.emplace_back(time, d, tu);
129 }
130}

--- 19 unchanged lines hidden (view full) ---

150
151} // anonymous namespace
152
153sc_time::sc_time() : val(0) {}
154
155sc_time::sc_time(double d, sc_time_unit tu)
156{
157 val = 0;
158 if (d != 0)
159 set(this, d, tu);
160}
161
162sc_time::sc_time(const sc_time &t)
163{
164 val = t.val;
165}
166
167sc_time::sc_time(double d, bool scale)
168{
169 //XXX Assuming the time resolution is 1ps.
170 if (scale)
171 set(this, d * defaultUnit, SC_SEC);
172 else
173 set(this, d, SC_PS);
174}
175
176sc_time::sc_time(sc_dt::uint64 v, bool scale)
177{
178 //XXX Assuming the time resolution is 1ps.
179 if (scale)
180 set(this, static_cast<double>(v) * defaultUnit, SC_SEC);
181 else
182 set(this, static_cast<double>(v), SC_PS);
183}
184
185sc_time &
186sc_time::operator = (const sc_time &t)
187{
188 val = t.val;
189 return *this;
190}

--- 7 unchanged lines hidden (view full) ---

198double
199sc_time::to_double() const
200{
201 return static_cast<double>(val);
202}
203double
204sc_time::to_seconds() const
205{
206 double d = to_double();
207 //XXX Assuming the time resolution is 1ps.
208 double scale = TimeUnitScale[SC_PS] / TimeUnitScale[SC_SEC];
209 return d * scale;
210}
211
212const std::string
213sc_time::to_string() const
214{
215 std::ostringstream ss;
216 print(ss);
217 return ss.str();

--- 64 unchanged lines hidden (view full) ---

282}
283
284void
285sc_time::print(std::ostream &os) const
286{
287 if (val == 0) {
288 os << "0 s";
289 } else {
290 //XXX Assuming the time resolution is 1ps.
291 sc_time_unit tu = SC_PS;
292 uint64_t scaled = val;
293 while (tu < SC_SEC && (scaled % 1000) == 0) {
294 tu = (sc_time_unit)((int)tu + 1);
295 scaled /= 1000;
296 }
297
298 os << scaled << ' ' << TimeUnitNames[tu];
299 }
300}
301
302sc_time
303sc_time::from_value(sc_dt::uint64 u)
304{
305 sc_time t;
306 t.val = u;
307 return t;
308}
309
310sc_time
311sc_time::from_seconds(double d)
312{

--- 80 unchanged lines hidden (view full) ---

393 }
394 // This won't detect the timescale being fixed outside of systemc, but
395 // it's at least some protection.
396 if (timeFixed) {
397 SC_REPORT_ERROR("(E514) set time resolution failed",
398 "sc_time object(s) constructed");
399 }
400
401 // Normalize d to seconds.
402 d *= TimeUnitScale[tu];
403 if (d < TimeUnitScale[SC_FS]) {
404 SC_REPORT_ERROR("(E514) set time resolution failed",
405 "value smaller than 1 fs");
406 }
407 // Change d from a period to a frequency.
408 d = 1 / d;
409 // Convert to integer ticks.
410 Tick ticks_per_second = static_cast<Tick>(d);
411 setGlobalFrequency(ticks_per_second);
412 specified = true;
413}
414
415sc_time
416sc_get_time_resolution()
417{
418 return sc_time::from_value(1);

--- 89 unchanged lines hidden ---