Gabe Black | 0ac709b | 2018-05-07 17:19:11 -0700 | [diff] [blame] | 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. |
Gabe Black | 0ac709b | 2018-05-07 17:19:11 -0700 | [diff] [blame] | 26 | */ |
| 27 | |
Gabe Black | a60868f | 2018-05-08 19:01:17 -0700 | [diff] [blame] | 28 | #ifndef __SYSTEMC_EXT_CORE_SC_TIME_HH__ |
| 29 | #define __SYSTEMC_EXT_CORE_SC_TIME_HH__ |
Gabe Black | 0ac709b | 2018-05-07 17:19:11 -0700 | [diff] [blame] | 30 | |
| 31 | #include <stdint.h> |
| 32 | |
| 33 | #include <iostream> |
| 34 | |
Gabe Black | a60868f | 2018-05-08 19:01:17 -0700 | [diff] [blame] | 35 | #include "../dt/int/sc_nbdefs.hh" |
Gabe Black | 0ac709b | 2018-05-07 17:19:11 -0700 | [diff] [blame] | 36 | |
| 37 | namespace sc_core |
| 38 | { |
| 39 | |
| 40 | enum sc_time_unit { |
| 41 | SC_FS = 0, |
| 42 | SC_PS, |
| 43 | SC_NS, |
| 44 | SC_US, |
| 45 | SC_MS, |
| 46 | SC_SEC |
| 47 | }; |
| 48 | |
| 49 | class sc_time |
| 50 | { |
| 51 | public: |
| 52 | sc_time(); |
| 53 | sc_time(double, sc_time_unit); |
| 54 | sc_time(const sc_time &); |
| 55 | |
Gabe Black | 34a9b86 | 2018-09-27 01:20:28 -0700 | [diff] [blame] | 56 | // Nonstandard |
| 57 | sc_time(double, const char *); |
| 58 | |
Gabe Black | 53779eb | 2018-06-15 20:48:08 -0700 | [diff] [blame] | 59 | // Deprecated |
| 60 | sc_time(double, bool); |
| 61 | sc_time(sc_dt::uint64, bool); |
| 62 | |
Gabe Black | 0ac709b | 2018-05-07 17:19:11 -0700 | [diff] [blame] | 63 | sc_time &operator = (const sc_time &); |
| 64 | |
| 65 | sc_dt::uint64 value() const; |
| 66 | double to_double() const; |
| 67 | double to_seconds() const; |
| 68 | const std::string to_string() const; |
| 69 | |
| 70 | bool operator == (const sc_time &) const; |
| 71 | bool operator != (const sc_time &) const; |
| 72 | bool operator < (const sc_time &) const; |
| 73 | bool operator <= (const sc_time &) const; |
| 74 | bool operator > (const sc_time &) const; |
| 75 | bool operator >= (const sc_time &) const; |
| 76 | |
| 77 | sc_time &operator += (const sc_time &); |
| 78 | sc_time &operator -= (const sc_time &); |
| 79 | sc_time &operator *= (double); |
| 80 | sc_time &operator /= (double); |
| 81 | |
| 82 | void print(std::ostream & =std::cout) const; |
Gabe Black | 663f5f6 | 2018-06-15 20:16:48 -0700 | [diff] [blame] | 83 | |
| 84 | // Deprecated |
| 85 | static sc_time from_value(sc_dt::uint64); |
| 86 | static sc_time from_seconds(double); |
| 87 | static sc_time from_string(const char *str); |
Gabe Black | b7ab029 | 2018-07-19 16:53:27 -0700 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | uint64_t val; |
Gabe Black | 0ac709b | 2018-05-07 17:19:11 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | const sc_time operator + (const sc_time &, const sc_time &); |
| 94 | const sc_time operator - (const sc_time &, const sc_time &); |
| 95 | |
| 96 | const sc_time operator * (const sc_time &, double); |
| 97 | const sc_time operator * (double, const sc_time &); |
| 98 | const sc_time operator / (const sc_time &, double); |
| 99 | double operator / (const sc_time &, const sc_time &); |
| 100 | |
| 101 | std::ostream &operator << (std::ostream &, const sc_time &); |
| 102 | |
| 103 | extern const sc_time SC_ZERO_TIME; |
| 104 | |
| 105 | void sc_set_time_resolution(double, sc_time_unit); |
| 106 | sc_time sc_get_time_resolution(); |
| 107 | const sc_time &sc_max_time(); |
| 108 | |
Gabe Black | 200281b | 2018-06-15 20:00:36 -0700 | [diff] [blame] | 109 | // Deprecated |
| 110 | void sc_set_default_time_unit(double, sc_time_unit); |
| 111 | sc_time sc_get_default_time_unit(); |
| 112 | |
Gabe Black | 40eb6f9 | 2018-06-15 21:30:16 -0700 | [diff] [blame] | 113 | // Nonstandard |
| 114 | class sc_time_tuple |
| 115 | { |
| 116 | public: |
Gabe Black | 07cad60 | 2018-09-26 22:53:01 -0700 | [diff] [blame] | 117 | sc_time_tuple() : _value(), _unit(SC_SEC), _set(false) {} |
Gabe Black | 40eb6f9 | 2018-06-15 21:30:16 -0700 | [diff] [blame] | 118 | sc_time_tuple(const sc_time &); |
| 119 | |
| 120 | bool has_value() const; |
| 121 | sc_dt::uint64 value() const; |
| 122 | // Normalized unit. |
| 123 | sc_time_unit unit() const { return _unit; } |
| 124 | // Normalized unit symbol. |
| 125 | const char *unit_symbol() const; |
| 126 | |
| 127 | operator sc_time() const { return sc_time(to_double(), _unit); } |
| 128 | |
| 129 | double to_double() const; // Relative to the normalized unit. |
| 130 | std::string to_string() const; |
| 131 | |
| 132 | private: |
| 133 | sc_dt::uint64 _value; |
| 134 | sc_time_unit _unit; |
Gabe Black | 07cad60 | 2018-09-26 22:53:01 -0700 | [diff] [blame] | 135 | bool _set; |
Gabe Black | 40eb6f9 | 2018-06-15 21:30:16 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
Gabe Black | 0ac709b | 2018-05-07 17:19:11 -0700 | [diff] [blame] | 138 | } // namespace sc_core |
| 139 | |
Gabe Black | a60868f | 2018-05-08 19:01:17 -0700 | [diff] [blame] | 140 | #endif //__SYSTEMC_EXT_CORE_SC_TIME_HH__ |