systemc: Add the non-standard sc_time_tuple class.
[gem5.git] / src / systemc / ext / core / sc_time.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
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 #ifndef __SYSTEMC_EXT_CORE_SC_TIME_HH__
31 #define __SYSTEMC_EXT_CORE_SC_TIME_HH__
32
33 #include <stdint.h>
34
35 #include <iostream>
36
37 #include "../dt/int/sc_nbdefs.hh"
38
39 namespace sc_core
40 {
41
42 enum sc_time_unit {
43 SC_FS = 0,
44 SC_PS,
45 SC_NS,
46 SC_US,
47 SC_MS,
48 SC_SEC
49 };
50
51 class sc_time
52 {
53 public:
54 sc_time();
55 sc_time(double, sc_time_unit);
56 sc_time(const sc_time &);
57
58 // Deprecated
59 sc_time(double, bool);
60 sc_time(sc_dt::uint64, bool);
61
62 sc_time &operator = (const sc_time &);
63
64 sc_dt::uint64 value() const;
65 double to_double() const;
66 double to_seconds() const;
67 const std::string to_string() const;
68
69 bool operator == (const sc_time &) const;
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
76 sc_time &operator += (const sc_time &);
77 sc_time &operator -= (const sc_time &);
78 sc_time &operator *= (double);
79 sc_time &operator /= (double);
80
81 void print(std::ostream & =std::cout) const;
82
83 // Deprecated
84 static sc_time from_value(sc_dt::uint64);
85 static sc_time from_seconds(double);
86 static sc_time from_string(const char *str);
87 };
88
89 const sc_time operator + (const sc_time &, const sc_time &);
90 const sc_time operator - (const sc_time &, const sc_time &);
91
92 const sc_time operator * (const sc_time &, double);
93 const sc_time operator * (double, const sc_time &);
94 const sc_time operator / (const sc_time &, double);
95 double operator / (const sc_time &, const sc_time &);
96
97 std::ostream &operator << (std::ostream &, const sc_time &);
98
99 extern const sc_time SC_ZERO_TIME;
100
101 void sc_set_time_resolution(double, sc_time_unit);
102 sc_time sc_get_time_resolution();
103 const sc_time &sc_max_time();
104
105 // Deprecated
106 void sc_set_default_time_unit(double, sc_time_unit);
107 sc_time sc_get_default_time_unit();
108
109 // Nonstandard
110 class sc_time_tuple
111 {
112 public:
113 sc_time_tuple() : _value(), _unit(SC_SEC), _offset(1) {}
114 sc_time_tuple(const sc_time &);
115
116 bool has_value() const;
117 sc_dt::uint64 value() const;
118 // Normalized unit.
119 sc_time_unit unit() const { return _unit; }
120 // Normalized unit symbol.
121 const char *unit_symbol() const;
122
123 operator sc_time() const { return sc_time(to_double(), _unit); }
124
125 double to_double() const; // Relative to the normalized unit.
126 std::string to_string() const;
127
128 private:
129 sc_dt::uint64 _value;
130 sc_time_unit _unit;
131 unsigned _offset;
132 };
133
134 } // namespace sc_core
135
136 #endif //__SYSTEMC_EXT_CORE_SC_TIME_HH__