blob: 7c84c630e42fd6d36ef691771ca952675724a331 [file] [log] [blame]
Gabe Black0ac709b2018-05-07 17:19:11 -07001/*
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 Black0ac709b2018-05-07 17:19:11 -070026 */
27
Gabe Blacka60868f2018-05-08 19:01:17 -070028#ifndef __SYSTEMC_EXT_CORE_SC_TIME_HH__
29#define __SYSTEMC_EXT_CORE_SC_TIME_HH__
Gabe Black0ac709b2018-05-07 17:19:11 -070030
31#include <stdint.h>
32
33#include <iostream>
34
Gabe Blacka60868f2018-05-08 19:01:17 -070035#include "../dt/int/sc_nbdefs.hh"
Gabe Black0ac709b2018-05-07 17:19:11 -070036
37namespace sc_core
38{
39
40enum sc_time_unit {
41 SC_FS = 0,
42 SC_PS,
43 SC_NS,
44 SC_US,
45 SC_MS,
46 SC_SEC
47};
48
49class sc_time
50{
51 public:
52 sc_time();
53 sc_time(double, sc_time_unit);
54 sc_time(const sc_time &);
55
Gabe Black34a9b862018-09-27 01:20:28 -070056 // Nonstandard
57 sc_time(double, const char *);
58
Gabe Black53779eb2018-06-15 20:48:08 -070059 // Deprecated
60 sc_time(double, bool);
61 sc_time(sc_dt::uint64, bool);
62
Gabe Black0ac709b2018-05-07 17:19:11 -070063 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 Black663f5f62018-06-15 20:16:48 -070083
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 Blackb7ab0292018-07-19 16:53:27 -070088
89 private:
90 uint64_t val;
Gabe Black0ac709b2018-05-07 17:19:11 -070091};
92
93const sc_time operator + (const sc_time &, const sc_time &);
94const sc_time operator - (const sc_time &, const sc_time &);
95
96const sc_time operator * (const sc_time &, double);
97const sc_time operator * (double, const sc_time &);
98const sc_time operator / (const sc_time &, double);
99double operator / (const sc_time &, const sc_time &);
100
101std::ostream &operator << (std::ostream &, const sc_time &);
102
103extern const sc_time SC_ZERO_TIME;
104
105void sc_set_time_resolution(double, sc_time_unit);
106sc_time sc_get_time_resolution();
107const sc_time &sc_max_time();
108
Gabe Black200281b2018-06-15 20:00:36 -0700109// Deprecated
110void sc_set_default_time_unit(double, sc_time_unit);
111sc_time sc_get_default_time_unit();
112
Gabe Black40eb6f92018-06-15 21:30:16 -0700113// Nonstandard
114class sc_time_tuple
115{
116 public:
Gabe Black07cad602018-09-26 22:53:01 -0700117 sc_time_tuple() : _value(), _unit(SC_SEC), _set(false) {}
Gabe Black40eb6f92018-06-15 21:30:16 -0700118 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 Black07cad602018-09-26 22:53:01 -0700135 bool _set;
Gabe Black40eb6f92018-06-15 21:30:16 -0700136};
137
Gabe Black0ac709b2018-05-07 17:19:11 -0700138} // namespace sc_core
139
Gabe Blacka60868f2018-05-08 19:01:17 -0700140#endif //__SYSTEMC_EXT_CORE_SC_TIME_HH__