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 <vector> 31 32#include "base/logging.hh" 33#include "base/types.hh" 34#include "python/pybind11/pybind.hh" 35#include "systemc/core/python.hh" 36#include "systemc/ext/core/sc_time.hh" 37 38namespace sc_core 39{ 40 41namespace 42{ 43 44const char *TimeUnitNames[] = { 45 [SC_FS] = "fs", 46 [SC_PS] = "ps", 47 [SC_NS] = "ns", 48 [SC_US] = "us", 49 [SC_MS] = "ms", 50 [SC_SEC] = "s" 51}; 52 53double TimeUnitScale[] = { 54 [SC_FS] = 1.0e-15, 55 [SC_PS] = 1.0e-12, 56 [SC_NS] = 1.0e-9, 57 [SC_US] = 1.0e-6, 58 [SC_MS] = 1.0e-3, 59 [SC_SEC] = 1.0 60}; 61 62bool timeFixed = false; 63bool pythonReady = false; 64 65struct SetInfo 66{ 67 SetInfo(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu) : 68 time(time), d(d), tu(tu) 69 {} 70 71 ::sc_core::sc_time *time; 72 double d; 73 ::sc_core::sc_time_unit tu; 74}; 75std::vector<SetInfo> toSet; 76 77void 78setWork(sc_time *time, double d, ::sc_core::sc_time_unit tu) 79{ 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 *time = sc_time::from_value(static_cast<uint64_t>(tmp)); 86} 87 88void 89fixTime() 90{ 91 auto ticks = pybind11::module::import("m5.ticks"); 92 auto fix_global_frequency = ticks.attr("fixGlobalFrequency"); 93 fix_global_frequency(); 94 95 for (auto &t: toSet) 96 setWork(t.time, t.d, t.tu); 97 toSet.clear(); 98} 99 100void 101set(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu) 102{ 103 // Only fix time once. 104 if (!timeFixed) { 105 timeFixed = true; 106 107 // If we've run, python is working and we haven't fixed time yet. 108 if (pythonReady) 109 fixTime(); 110 } 111 if (pythonReady) { 112 // Time should be working. Set up this sc_time. 113 setWork(time, d, tu); 114 } else { 115 // Time isn't set up yet. Defer setting up this sc_time. 116 toSet.emplace_back(time, d, tu); 117 } 118} 119 120class TimeSetter : public ::sc_gem5::PythonReadyFunc 121{ 122 public: 123 TimeSetter() : ::sc_gem5::PythonReadyFunc() {} 124 125 void 126 run() override 127 { 128 // Record that we've run and python/pybind should be usable. 129 pythonReady = true; 130 131 // If time is already fixed, let python know. 132 if (timeFixed) 133 fixTime(); 134 } 135} timeSetter; 136 137} // anonymous namespace 138 139sc_time::sc_time() : val(0) {} 140 141sc_time::sc_time(double d, sc_time_unit tu) 142{ 143 val = 0; 144 if (d != 0) 145 set(this, d, tu); 146} 147 148sc_time::sc_time(const sc_time &t) 149{ 150 val = t.val; 151} 152 153sc_time::sc_time(double, bool) 154{ 155 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 156} 157 158sc_time::sc_time(sc_dt::uint64, bool) 159{ 160 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 161} 162 163sc_time & 164sc_time::operator = (const sc_time &t) 165{ 166 val = t.val; 167 return *this; 168} 169 170sc_dt::uint64 171sc_time::value() const 172{ 173 return val; 174} 175 176double 177sc_time::to_double() const 178{
| 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 <vector> 31 32#include "base/logging.hh" 33#include "base/types.hh" 34#include "python/pybind11/pybind.hh" 35#include "systemc/core/python.hh" 36#include "systemc/ext/core/sc_time.hh" 37 38namespace sc_core 39{ 40 41namespace 42{ 43 44const char *TimeUnitNames[] = { 45 [SC_FS] = "fs", 46 [SC_PS] = "ps", 47 [SC_NS] = "ns", 48 [SC_US] = "us", 49 [SC_MS] = "ms", 50 [SC_SEC] = "s" 51}; 52 53double TimeUnitScale[] = { 54 [SC_FS] = 1.0e-15, 55 [SC_PS] = 1.0e-12, 56 [SC_NS] = 1.0e-9, 57 [SC_US] = 1.0e-6, 58 [SC_MS] = 1.0e-3, 59 [SC_SEC] = 1.0 60}; 61 62bool timeFixed = false; 63bool pythonReady = false; 64 65struct SetInfo 66{ 67 SetInfo(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu) : 68 time(time), d(d), tu(tu) 69 {} 70 71 ::sc_core::sc_time *time; 72 double d; 73 ::sc_core::sc_time_unit tu; 74}; 75std::vector<SetInfo> toSet; 76 77void 78setWork(sc_time *time, double d, ::sc_core::sc_time_unit tu) 79{ 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 *time = sc_time::from_value(static_cast<uint64_t>(tmp)); 86} 87 88void 89fixTime() 90{ 91 auto ticks = pybind11::module::import("m5.ticks"); 92 auto fix_global_frequency = ticks.attr("fixGlobalFrequency"); 93 fix_global_frequency(); 94 95 for (auto &t: toSet) 96 setWork(t.time, t.d, t.tu); 97 toSet.clear(); 98} 99 100void 101set(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu) 102{ 103 // Only fix time once. 104 if (!timeFixed) { 105 timeFixed = true; 106 107 // If we've run, python is working and we haven't fixed time yet. 108 if (pythonReady) 109 fixTime(); 110 } 111 if (pythonReady) { 112 // Time should be working. Set up this sc_time. 113 setWork(time, d, tu); 114 } else { 115 // Time isn't set up yet. Defer setting up this sc_time. 116 toSet.emplace_back(time, d, tu); 117 } 118} 119 120class TimeSetter : public ::sc_gem5::PythonReadyFunc 121{ 122 public: 123 TimeSetter() : ::sc_gem5::PythonReadyFunc() {} 124 125 void 126 run() override 127 { 128 // Record that we've run and python/pybind should be usable. 129 pythonReady = true; 130 131 // If time is already fixed, let python know. 132 if (timeFixed) 133 fixTime(); 134 } 135} timeSetter; 136 137} // anonymous namespace 138 139sc_time::sc_time() : val(0) {} 140 141sc_time::sc_time(double d, sc_time_unit tu) 142{ 143 val = 0; 144 if (d != 0) 145 set(this, d, tu); 146} 147 148sc_time::sc_time(const sc_time &t) 149{ 150 val = t.val; 151} 152 153sc_time::sc_time(double, bool) 154{ 155 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 156} 157 158sc_time::sc_time(sc_dt::uint64, bool) 159{ 160 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 161} 162 163sc_time & 164sc_time::operator = (const sc_time &t) 165{ 166 val = t.val; 167 return *this; 168} 169 170sc_dt::uint64 171sc_time::value() const 172{ 173 return val; 174} 175 176double 177sc_time::to_double() const 178{
|
179 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 180 return 0.0;
| 179 return static_cast<double>(val);
|
181} 182double 183sc_time::to_seconds() const 184{ 185 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 186 return 0.0; 187} 188 189const std::string 190sc_time::to_string() const 191{ 192 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 193 return ""; 194} 195 196bool 197sc_time::operator == (const sc_time &t) const 198{ 199 return val == t.val; 200} 201 202bool 203sc_time::operator != (const sc_time &t) const 204{ 205 return val != t.val; 206} 207 208bool 209sc_time::operator < (const sc_time &t) const 210{ 211 return val < t.val; 212} 213 214bool 215sc_time::operator <= (const sc_time &t) const 216{ 217 return val <= t.val; 218} 219 220bool 221sc_time::operator > (const sc_time &t) const 222{ 223 return val > t.val; 224} 225 226bool 227sc_time::operator >= (const sc_time &t) const 228{ 229 return val >= t.val; 230} 231 232sc_time & 233sc_time::operator += (const sc_time &t) 234{ 235 val += t.val; 236 return *this; 237} 238 239sc_time & 240sc_time::operator -= (const sc_time &t) 241{ 242 val -= t.val; 243 return *this; 244} 245 246sc_time &
| 180} 181double 182sc_time::to_seconds() const 183{ 184 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 185 return 0.0; 186} 187 188const std::string 189sc_time::to_string() const 190{ 191 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 192 return ""; 193} 194 195bool 196sc_time::operator == (const sc_time &t) const 197{ 198 return val == t.val; 199} 200 201bool 202sc_time::operator != (const sc_time &t) const 203{ 204 return val != t.val; 205} 206 207bool 208sc_time::operator < (const sc_time &t) const 209{ 210 return val < t.val; 211} 212 213bool 214sc_time::operator <= (const sc_time &t) const 215{ 216 return val <= t.val; 217} 218 219bool 220sc_time::operator > (const sc_time &t) const 221{ 222 return val > t.val; 223} 224 225bool 226sc_time::operator >= (const sc_time &t) const 227{ 228 return val >= t.val; 229} 230 231sc_time & 232sc_time::operator += (const sc_time &t) 233{ 234 val += t.val; 235 return *this; 236} 237 238sc_time & 239sc_time::operator -= (const sc_time &t) 240{ 241 val -= t.val; 242 return *this; 243} 244 245sc_time &
|
247sc_time::operator *= (double)
| 246sc_time::operator *= (double d)
|
248{
| 247{
|
249 warn("%s not implemented.\n", __PRETTY_FUNCTION__);
| 248 val = static_cast<int64_t>(static_cast<double>(val) * d + 0.5);
|
250 return *this; 251} 252 253sc_time &
| 249 return *this; 250} 251 252sc_time &
|
254sc_time::operator /= (double)
| 253sc_time::operator /= (double d)
|
255{
| 254{
|
256 warn("%s not implemented.\n", __PRETTY_FUNCTION__);
| 255 val = static_cast<int64_t>(static_cast<double>(val) / d + 0.5);
|
257 return *this; 258} 259 260void 261sc_time::print(std::ostream &os) const 262{ 263 if (val == 0) { 264 os << "0 s"; 265 } else { 266 //XXX Assuming the time resolution is 1ps. 267 sc_time_unit tu = SC_PS; 268 uint64_t scaled = val; 269 while (tu < SC_SEC && (scaled % 1000) == 0) { 270 tu = (sc_time_unit)((int)tu + 1); 271 scaled /= 1000; 272 } 273 274 os << scaled << ' ' << TimeUnitNames[tu]; 275 } 276} 277 278sc_time 279sc_time::from_value(sc_dt::uint64 u) 280{ 281 sc_time t; 282 t.val = u; 283 return t; 284} 285 286sc_time 287sc_time::from_seconds(double) 288{ 289 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 290 return sc_time(); 291} 292 293sc_time 294sc_time::from_string(const char *str) 295{ 296 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 297 return sc_time(); 298} 299 300const sc_time 301operator + (const sc_time &a, const sc_time &b) 302{ 303 return sc_time::from_value(a.value() + b.value()); 304} 305 306const sc_time 307operator - (const sc_time &a, const sc_time &b) 308{ 309 return sc_time::from_value(a.value() - b.value()); 310} 311 312const sc_time
| 256 return *this; 257} 258 259void 260sc_time::print(std::ostream &os) const 261{ 262 if (val == 0) { 263 os << "0 s"; 264 } else { 265 //XXX Assuming the time resolution is 1ps. 266 sc_time_unit tu = SC_PS; 267 uint64_t scaled = val; 268 while (tu < SC_SEC && (scaled % 1000) == 0) { 269 tu = (sc_time_unit)((int)tu + 1); 270 scaled /= 1000; 271 } 272 273 os << scaled << ' ' << TimeUnitNames[tu]; 274 } 275} 276 277sc_time 278sc_time::from_value(sc_dt::uint64 u) 279{ 280 sc_time t; 281 t.val = u; 282 return t; 283} 284 285sc_time 286sc_time::from_seconds(double) 287{ 288 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 289 return sc_time(); 290} 291 292sc_time 293sc_time::from_string(const char *str) 294{ 295 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 296 return sc_time(); 297} 298 299const sc_time 300operator + (const sc_time &a, const sc_time &b) 301{ 302 return sc_time::from_value(a.value() + b.value()); 303} 304 305const sc_time 306operator - (const sc_time &a, const sc_time &b) 307{ 308 return sc_time::from_value(a.value() - b.value()); 309} 310 311const sc_time
|
313operator * (const sc_time &, double)
| 312operator * (const sc_time &t, double d)
|
314{
| 313{
|
315 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 316 return sc_time();
| 314 volatile double tmp = static_cast<double>(t.value()) * d + 0.5; 315 return sc_time::from_value(static_cast<int64_t>(tmp));
|
317} 318 319const sc_time
| 316} 317 318const sc_time
|
320operator * (double, const sc_time &)
| 319operator * (double d, const sc_time &t)
|
321{
| 320{
|
322 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 323 return sc_time();
| 321 volatile double tmp = d * static_cast<double>(t.value()) + 0.5; 322 return sc_time::from_value(static_cast<int64_t>(tmp));
|
324} 325 326const sc_time
| 323} 324 325const sc_time
|
327operator / (const sc_time &, double)
| 326operator / (const sc_time &t, double d)
|
328{
| 327{
|
329 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 330 return sc_time();
| 328 volatile double tmp = static_cast<double>(t.value()) / d + 0.5; 329 return sc_time::from_value(static_cast<int64_t>(tmp));
|
331} 332 333double
| 330} 331 332double
|
334operator / (const sc_time &, const sc_time &)
| 333operator / (const sc_time &t1, const sc_time &t2)
|
335{
| 334{
|
336 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 337 return 0.0;
| 335 return t1.to_double() / t2.to_double();
|
338} 339 340std::ostream & 341operator << (std::ostream &os, const sc_time &t) 342{ 343 t.print(os); 344 return os; 345} 346 347const sc_time SC_ZERO_TIME; 348 349void 350sc_set_time_resolution(double, sc_time_unit) 351{ 352 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 353} 354 355sc_time 356sc_get_time_resolution() 357{ 358 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 359 return sc_time(); 360} 361 362const sc_time & 363sc_max_time() 364{ 365 static const sc_time MaxScTime = sc_time::from_value(MaxTick); 366 return MaxScTime; 367} 368 369void 370sc_set_default_time_unit(double, sc_time_unit) 371{ 372 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 373} 374 375sc_time 376sc_get_default_time_unit() 377{ 378 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 379 return *(sc_time *)nullptr; 380} 381 382sc_time_tuple::sc_time_tuple(const sc_time &) 383{ 384 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 385} 386 387bool 388sc_time_tuple::has_value() const 389{ 390 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 391 return false; 392} 393 394sc_dt::uint64 395sc_time_tuple::value() const 396{ 397 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 398 return 0; 399} 400 401const char * 402sc_time_tuple::unit_symbol() const 403{ 404 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 405 return ""; 406} 407 408double 409sc_time_tuple::to_double() const 410{ 411 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 412 return 0.0; 413} 414 415std::string 416sc_time_tuple::to_string() const 417{ 418 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 419 return ""; 420} 421 422} // namespace sc_core
| 336} 337 338std::ostream & 339operator << (std::ostream &os, const sc_time &t) 340{ 341 t.print(os); 342 return os; 343} 344 345const sc_time SC_ZERO_TIME; 346 347void 348sc_set_time_resolution(double, sc_time_unit) 349{ 350 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 351} 352 353sc_time 354sc_get_time_resolution() 355{ 356 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 357 return sc_time(); 358} 359 360const sc_time & 361sc_max_time() 362{ 363 static const sc_time MaxScTime = sc_time::from_value(MaxTick); 364 return MaxScTime; 365} 366 367void 368sc_set_default_time_unit(double, sc_time_unit) 369{ 370 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 371} 372 373sc_time 374sc_get_default_time_unit() 375{ 376 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 377 return *(sc_time *)nullptr; 378} 379 380sc_time_tuple::sc_time_tuple(const sc_time &) 381{ 382 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 383} 384 385bool 386sc_time_tuple::has_value() const 387{ 388 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 389 return false; 390} 391 392sc_dt::uint64 393sc_time_tuple::value() const 394{ 395 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 396 return 0; 397} 398 399const char * 400sc_time_tuple::unit_symbol() const 401{ 402 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 403 return ""; 404} 405 406double 407sc_time_tuple::to_double() const 408{ 409 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 410 return 0.0; 411} 412 413std::string 414sc_time_tuple::to_string() const 415{ 416 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 417 return ""; 418} 419 420} // namespace sc_core
|