sc_time.cc (13177:7b750aeab360) | sc_time.cc (13195:de9e5572ac44) |
---|---|
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" | 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 "sim/core.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 | 37#include "systemc/core/python.hh" 38#include "systemc/ext/core/sc_main.hh" 39#include "systemc/ext/core/sc_time.hh" 40#include "systemc/ext/utils/sc_report_handler.hh" 41 42namespace sc_core 43{ 44 --- 13 unchanged lines hidden (view full) --- 58 [SC_FS] = 1.0e-15, 59 [SC_PS] = 1.0e-12, 60 [SC_NS] = 1.0e-9, 61 [SC_US] = 1.0e-6, 62 [SC_MS] = 1.0e-3, 63 [SC_SEC] = 1.0 64}; 65 |
66Tick TimeUnitFrequency[] = { 67 [SC_FS] = 1ULL * 1000 * 1000 * 1000 * 1000 * 1000, 68 [SC_PS] = 1ULL * 1000 * 1000 * 1000 * 1000, 69 [SC_NS] = 1ULL * 1000 * 1000 * 1000, 70 [SC_US] = 1ULL * 1000 * 1000, 71 [SC_MS] = 1ULL * 1000, 72 [SC_SEC] = 1ULL 73}; 74 |
|
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{ | 75bool timeFixed = false; 76bool pythonReady = false; 77 78struct SetInfo 79{ 80 SetInfo(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu) : 81 time(time), d(d), tu(tu) 82 {} 83 84 ::sc_core::sc_time *time; 85 double d; 86 ::sc_core::sc_time_unit tu; 87}; 88std::vector<SetInfo> toSet; 89 90void 91setWork(sc_time *time, double d, ::sc_core::sc_time_unit tu) 92{ |
83 //XXX Assuming the time resolution is 1ps. 84 double scale = TimeUnitScale[tu] / TimeUnitScale[SC_PS]; | 93 double scale = TimeUnitScale[tu] * SimClock::Float::s; |
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 | 94 // Accellera claims there is a linux bug, and that these next two 95 // lines work around them. 96 volatile double tmp = d * scale + 0.5; 97 *time = sc_time::from_value(static_cast<uint64_t>(tmp)); 98} 99 100void 101fixTime() 102{ 103 auto ticks = pybind11::module::import("m5.ticks"); 104 auto fix_global_frequency = ticks.attr("fixGlobalFrequency"); 105 fix_global_frequency(); 106 107 for (auto &t: toSet) 108 setWork(t.time, t.d, t.tu); 109 toSet.clear(); 110} 111 112void |
113attemptToFixTime() 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} 124 125void |
|
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{ | 126setGlobalFrequency(Tick ticks_per_second) 127{ 128 auto ticks = pybind11::module::import("m5.ticks"); 129 auto set_global_frequency = ticks.attr("setGlobalFrequency"); 130 set_global_frequency(ticks_per_second); 131 fixTime(); 132} 133 134void 135set(::sc_core::sc_time *time, double d, ::sc_core::sc_time_unit tu) 136{ |
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 } | 137 if (d != 0) 138 attemptToFixTime(); |
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; | 139 if (pythonReady) { 140 // Time should be working. Set up this sc_time. 141 setWork(time, d, tu); 142 } else { 143 // Time isn't set up yet. Defer setting up this sc_time. 144 toSet.emplace_back(time, d, tu); 145 } 146} --- 19 unchanged lines hidden (view full) --- 166 167} // anonymous namespace 168 169sc_time::sc_time() : val(0) {} 170 171sc_time::sc_time(double d, sc_time_unit tu) 172{ 173 val = 0; |
158 if (d != 0) 159 set(this, d, tu); | 174 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{ | 175} 176 177sc_time::sc_time(const sc_time &t) 178{ 179 val = t.val; 180} 181 182sc_time::sc_time(double d, bool scale) 183{ |
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); | 184 double scaler = scale ? defaultUnit : SimClock::Float::Hz; 185 set(this, d * scaler, SC_SEC); |
174} 175 176sc_time::sc_time(sc_dt::uint64 v, bool scale) 177{ | 186} 187 188sc_time::sc_time(sc_dt::uint64 v, bool scale) 189{ |
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); | 190 double scaler = scale ? defaultUnit : SimClock::Float::Hz; 191 set(this, static_cast<double>(v) * scaler, SC_SEC); |
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{ | 192} 193 194sc_time & 195sc_time::operator = (const sc_time &t) 196{ 197 val = t.val; 198 return *this; 199} --- 7 unchanged lines hidden (view full) --- 207double 208sc_time::to_double() const 209{ 210 return static_cast<double>(val); 211} 212double 213sc_time::to_seconds() const 214{ |
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; | 215 return to_double() * SimClock::Float::Hz; |
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 { | 216} 217 218const std::string 219sc_time::to_string() const 220{ 221 std::ostringstream ss; 222 print(ss); 223 return ss.str(); --- 64 unchanged lines hidden (view full) --- 288} 289 290void 291sc_time::print(std::ostream &os) const 292{ 293 if (val == 0) { 294 os << "0 s"; 295 } else { |
290 //XXX Assuming the time resolution is 1ps. 291 sc_time_unit tu = SC_PS; 292 uint64_t scaled = val; | 296 Tick frequency = SimClock::Frequency; 297 298 // Shrink the frequency by scaling down the time period, ie converting 299 // it from cycles per second to cycles per millisecond, etc. 300 sc_time_unit tu = SC_SEC; 301 while (tu > 1 && (frequency % 1000 == 0)) { 302 tu = (sc_time_unit)((int)tu - 1); 303 frequency /= 1000; 304 } 305 306 // Convert the frequency into a period. 307 Tick period; 308 if (frequency > 1) { 309 tu = (sc_time_unit)((int)tu - 1); 310 period = 1000 / frequency; 311 } else { 312 period = frequency; 313 } 314 315 // Scale our integer value by the period. 316 uint64_t scaled = val * period; 317 318 // Shrink the scaled time value by increasing the size of the units 319 // it's measured by, avoiding fractional parts. |
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{ | 320 while (tu < SC_SEC && (scaled % 1000) == 0) { 321 tu = (sc_time_unit)((int)tu + 1); 322 scaled /= 1000; 323 } 324 325 os << scaled << ' ' << TimeUnitNames[tu]; 326 } 327} 328 329sc_time 330sc_time::from_value(sc_dt::uint64 u) 331{ |
332 if (u) 333 attemptToFixTime(); |
|
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 | 334 sc_time t; 335 t.val = u; 336 return t; 337} 338 339sc_time 340sc_time::from_seconds(double d) 341{ --- 80 unchanged lines hidden (view full) --- 422 } 423 // This won't detect the timescale being fixed outside of systemc, but 424 // it's at least some protection. 425 if (timeFixed) { 426 SC_REPORT_ERROR("(E514) set time resolution failed", 427 "sc_time object(s) constructed"); 428 } 429 |
401 // Normalize d to seconds. 402 d *= TimeUnitScale[tu]; 403 if (d < TimeUnitScale[SC_FS]) { | 430 double seconds = d * TimeUnitScale[tu]; 431 if (seconds < TimeUnitScale[SC_FS]) { |
404 SC_REPORT_ERROR("(E514) set time resolution failed", 405 "value smaller than 1 fs"); 406 } | 432 SC_REPORT_ERROR("(E514) set time resolution failed", 433 "value smaller than 1 fs"); 434 } |
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); | 435 436 if (seconds > defaultUnit) { 437 SC_REPORT_WARNING( 438 "(W516) default time unit changed to time resolution", ""); 439 defaultUnit = seconds; 440 } 441 442 // Get rid of fractional parts of d. 443 while (d < 1.0 && tu > SC_FS) { 444 d *= 1000; 445 tu = (sc_time_unit)(tu - 1); 446 } 447 448 Tick ticks_per_second = TimeUnitFrequency[tu] / 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 --- | 449 setGlobalFrequency(ticks_per_second); 450 specified = true; 451} 452 453sc_time 454sc_get_time_resolution() 455{ 456 return sc_time::from_value(1); --- 89 unchanged lines hidden --- |