SystemC  Recoding Infrastructure for SystemC v0.6.2 derived from Accellera SystemC 2.3.1
Accellera SystemC proof-of-concept library
sc_time.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  The following code is derived, directly or indirectly, from the SystemC
4  source code Copyright (c) 1996-2014 by all Contributors.
5  All Rights reserved.
6 
7  The contents of this file are subject to the restrictions and limitations
8  set forth in the SystemC Open Source License (the "License");
9  You may not use this file except in compliance with such restrictions and
10  limitations. You may obtain instructions on how to receive a copy of the
11  License at http://www.accellera.org/. Software distributed by Contributors
12  under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
13  ANY KIND, either express or implied. See the License for the specific
14  language governing rights and limitations under the License.
15 
16  *****************************************************************************/
17 
18 /*****************************************************************************
19 
20  sc_time.h -- The time class.
21 
22  Original Author: Martin Janssen, Synopsys, Inc., 2001-05-21
23 
24  CHANGE LOG AT THE END OF THE FILE
25  *****************************************************************************/
26 
27 
28 #ifndef SC_TIME_H
29 #define SC_TIME_H
30 
31 
34 #include "sysc/utils/sc_iostream.h"
35 
36 namespace sc_core {
37 
38 class sc_simcontext;
39 
40 // friend operator declarations
41 
42  const sc_time operator + ( const sc_time&, const sc_time& );
43  const sc_time operator - ( const sc_time&, const sc_time& );
44  const sc_time operator * ( const sc_time&, double );
45  const sc_time operator * ( double, const sc_time& );
46  const sc_time operator / ( const sc_time&, double );
47  double operator / ( const sc_time&, const sc_time& );
48 
49 
50 // ----------------------------------------------------------------------------
51 // ENUM : sc_time_unit
52 //
53 // Enumeration of time units.
54 // ----------------------------------------------------------------------------
55 
57 {
58  SC_FS = 0,
64 };
65 
66 
67 // ----------------------------------------------------------------------------
68 // CLASS : sc_time
69 //
70 // The time class.
71 // ----------------------------------------------------------------------------
72 
73 class sc_time
74 {
75 public:
76 
77  // current RISC implementation uses 'long long', not 'unsigned long long'
78  // (this was a poor choice to allow for "special" values, not easy to change now)
79  // (08/23/19, RD)
80 // typedef sc_dt::uint64 value_type;
81  typedef long long value_type;
82 
83  // constructors
84 
85  sc_time();
86  sc_time( double, sc_time_unit );
87  sc_time( double, sc_time_unit, sc_simcontext* );
88  sc_time( const sc_time& );
89 
90  static sc_time from_value( value_type );
91 
92  // deprecated, use from_value(v)
93  sc_time( double, bool scale );
94  sc_time( value_type, bool scale );
95 
96  // assignment operator
97 
98  sc_time& operator = ( const sc_time& );
99 
100 
101  // conversion functions
102 
103  value_type value() const; // relative to the time resolution
104  double to_double() const; // relative to the time resolution
105  double to_default_time_units() const;
106  double to_seconds() const;
107  const std::string to_string() const;
108 
109 
110  // relational operators
111 
112  bool operator == ( const sc_time& ) const;
113  bool operator != ( const sc_time& ) const;
114  bool operator < ( const sc_time& ) const;
115  bool operator <= ( const sc_time& ) const;
116  bool operator > ( const sc_time& ) const;
117  bool operator >= ( const sc_time& ) const;
118 
119 
120  // arithmetic operators
121 
122  sc_time& operator += ( const sc_time& );
123  sc_time& operator -= ( const sc_time& );
124 
125  friend const sc_time operator + ( const sc_time&, const sc_time& );
126  friend const sc_time operator - ( const sc_time&, const sc_time& );
127 
128  sc_time& operator *= ( double );
129  sc_time& operator /= ( double );
130  sc_time& operator %= ( const sc_time& );
131 
132  friend const sc_time operator * ( const sc_time&, double );
133  friend const sc_time operator * ( double, const sc_time& );
134  friend const sc_time operator / ( const sc_time&, double );
135  friend double operator / ( const sc_time&, const sc_time& );
136  friend const sc_time operator % ( const sc_time&, const sc_time& );
137 
138 
139  // print function
140 
141  void print( ::std::ostream& os = std::cout ) const;
142 
143 private:
144 
145  value_type m_value;
146 };
147 
148 
149 // print operator
150 
151 inline ::std::ostream& operator << ( ::std::ostream&, const sc_time& );
152 
153 
154 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
155 
156 // constructors
157 
158 inline
160 : m_value( 0 )
161 {}
162 
163 inline
165 : m_value( t.m_value )
166 {}
167 
168 
169 // assignment operator
170 
171 inline
172 sc_time&
174 {
175  m_value = t.m_value;
176  return *this;
177 }
178 
179 
180 // conversion functions
181 
182 inline
184 sc_time::value() const // relative to the time resolution
185 {
186  return m_value;
187 }
188 
189 
190 inline
191 double
192 sc_time::to_double() const // relative to the time resolution
193 {
194  return sc_dt::uint64_to_double( m_value );
195 }
196 
197 
198 // relational operators
199 
200 inline
201 bool
203 {
204  return ( m_value == t.m_value );
205 }
206 
207 inline
208 bool
210 {
211  return ( m_value != t.m_value );
212 }
213 
214 inline
215 bool
216 sc_time::operator < ( const sc_time& t ) const
217 {
218  return ( m_value < t.m_value );
219 }
220 
221 inline
222 bool
224 {
225  return ( m_value <= t.m_value );
226 }
227 
228 inline
229 bool
230 sc_time::operator > ( const sc_time& t ) const
231 {
232  return ( m_value > t.m_value );
233 }
234 
235 inline
236 bool
238 {
239  return ( m_value >= t.m_value );
240 }
241 
242 
243 // arithmetic operators
244 
245 inline
246 sc_time&
248 {
249  m_value += t.m_value;
250  return *this;
251 }
252 
253 inline
254 sc_time&
256 {
257  m_value -= t.m_value;
258  return *this;
259 }
260 
261 
262 inline
263 const sc_time
264 operator + ( const sc_time& t1, const sc_time& t2 )
265 {
266  return sc_time( t1 ) += t2;
267 }
268 
269 inline
270 const sc_time
271 operator - ( const sc_time& t1, const sc_time& t2 )
272 {
273  return sc_time( t1 ) -= t2;
274 }
275 
276 
277 inline
278 sc_time&
280 {
281  // linux bug workaround; don't change next two lines
282  volatile double tmp = sc_dt::uint64_to_double( m_value ) * d + 0.5;
283  m_value = SCAST<sc_dt::int64>( tmp );
284  return *this;
285 }
286 
287 inline
288 sc_time&
290 {
291  // linux bug workaround; don't change next two lines
292  volatile double tmp = sc_dt::uint64_to_double( m_value ) / d + 0.5;
293  m_value = SCAST<sc_dt::int64>( tmp );
294  return *this;
295 }
296 
297 inline
298 sc_time&
300 {
301  m_value %= t.m_value;
302  return *this;
303 }
304 
305 inline
306 const sc_time
307 operator * ( const sc_time& t, double d )
308 {
309  sc_time tmp( t );
310  return tmp *= d;
311 }
312 
313 inline
314 const sc_time
315 operator * ( double d, const sc_time& t )
316 {
317  sc_time tmp( t );
318  return tmp *= d;
319 }
320 
321 inline
322 const sc_time
323 operator / ( const sc_time& t, double d )
324 {
325  sc_time tmp( t );
326  return tmp /= d;
327 }
328 
329 inline
330 double
331 operator / ( const sc_time& t1, const sc_time& t2 )
332 {
333  return ( t1.to_double() / t2.to_double() );
334 }
335 
336 inline
337 const sc_time
338 operator % ( const sc_time& t1, const sc_time& t2 )
339 {
340  sc_time tmp(t1);
341  return tmp %= t2;
342 }
343 
344 // print operator
345 
346 inline
347 ::std::ostream&
348 operator << ( ::std::ostream& os, const sc_time& t )
349 {
350  t.print( os );
351  return os;
352 }
353 
354 
355 // ----------------------------------------------------------------------------
356 // STRUCT : sc_time_params
357 //
358 // Struct that holds the time resolution and default time unit.
359 // ----------------------------------------------------------------------------
360 
362 {
363  double time_resolution; // in femto seconds
366 
367  sc_time::value_type default_time_unit; // in time resolution
369 
370  sc_time_params();
371  ~sc_time_params();
372 };
373 
374 
375 // ----------------------------------------------------------------------------
376 
377 extern const sc_time SC_ZERO_TIME;
378 
379 
380 // functions for accessing the time resolution and default time unit
381 
382 extern void sc_set_time_resolution( double, sc_time_unit );
384 
385 extern void sc_set_default_time_unit( double, sc_time_unit );
387 
388 } // namespace sc_core
389 
390 #endif
391 
392 // $Log: sc_time.h,v $
393 // Revision 1.5 2011/08/26 20:46:11 acg
394 // Andy Goodrich: moved the modification log to the end of the file to
395 // eliminate source line number skew when check-ins are done.
396 //
397 // Revision 1.4 2011/02/18 20:27:14 acg
398 // Andy Goodrich: Updated Copyrights.
399 //
400 // Revision 1.3 2011/02/13 21:47:38 acg
401 // Andy Goodrich: update copyright notice.
402 //
403 // Revision 1.2 2008/05/22 17:06:27 acg
404 // Andy Goodrich: updated copyright notice to include 2008.
405 //
406 // Revision 1.1.1.1 2006/12/15 20:20:05 acg
407 // SystemC 2.3
408 //
409 // Revision 1.4 2006/05/08 18:02:06 acg
410 // Andy Goodrich: added David Long's forward declarations for friend
411 // functions, methods, and operators to keep the Microsoft compiler happy.
412 //
413 // Revision 1.3 2006/01/13 18:44:30 acg
414 // Added $Log to record CVS changes into the source.
415 
416 // Taf!
void sc_set_time_resolution(double, sc_time_unit)
const sc_time operator*(const sc_time &, double)
Definition: sc_time.h:307
const std::string to_string() const
friend const sc_time operator+(const sc_time &, const sc_time &)
long long value_type
Definition: sc_time.h:81
static sc_time from_value(value_type)
sc_time & operator=(const sc_time &)
Definition: sc_time.h:173
inline::std::ostream & operator<<(::std::ostream &os, const sc_fifo< T > &a)
Definition: sc_fifo.h:508
bool default_time_unit_specified
Definition: sc_time.h:368
sc_time sc_get_time_resolution()
double to_double() const
Definition: sc_time.h:192
double to_seconds() const
sc_time sc_get_default_time_unit()
sc_time & operator*=(double)
Definition: sc_time.h:279
const sc_time operator%(const sc_time &t1, const sc_time &t2)
Definition: sc_time.h:338
sc_time & operator/=(double)
Definition: sc_time.h:289
sc_time & operator%=(const sc_time &)
Definition: sc_time.h:299
sc_time::value_type default_time_unit
Definition: sc_time.h:367
friend const sc_time operator%(const sc_time &, const sc_time &)
Definition: sc_time.h:338
const sc_time operator-(const sc_time &, const sc_time &)
Definition: sc_time.h:271
bool operator==(const sc_time &) const
Definition: sc_time.h:202
bool operator<=(const sc_time &) const
Definition: sc_time.h:223
bool operator!=(const sc_time &) const
Definition: sc_time.h:209
The simulation context.
bool time_resolution_specified
Definition: sc_time.h:364
sc_time & operator+=(const sc_time &)
Definition: sc_time.h:247
friend const sc_time operator*(const sc_time &, double)
double to_default_time_units() const
friend const sc_time operator/(const sc_time &, double)
const sc_time SC_ZERO_TIME
const sc_time operator/(const sc_time &, double)
Definition: sc_time.h:323
const sc_time operator+(const sc_time &, const sc_time &)
Definition: sc_time.h:264
value_type value() const
Definition: sc_time.h:184
double uint64_to_double(uint64 a)
Definition: scfx_ieee.h:682
sc_time & operator-=(const sc_time &)
Definition: sc_time.h:255
bool operator>(const sc_time &) const
Definition: sc_time.h:230
bool operator>=(const sc_time &) const
Definition: sc_time.h:237
friend const sc_time operator-(const sc_time &, const sc_time &)
bool operator<(const sc_time &) const
Definition: sc_time.h:216
void print(::std::ostream &os=std::cout) const
void sc_set_default_time_unit(double, sc_time_unit)
sc_time_unit
Definition: sc_time.h:56