SystemC 3.0.0
Accellera SystemC proof-of-concept library
sc_time.h
Go to the documentation of this file.
1/*****************************************************************************
2
3 Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4 more contributor license agreements. See the NOTICE file distributed
5 with this work for additional information regarding copyright ownership.
6 Accellera licenses this file to you under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with the
8 License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15 implied. See the License for the specific language governing
16 permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22 sc_time.h -- The time class.
23
24 Original Author: Martin Janssen, Synopsys, Inc., 2001-05-21
25
26 CHANGE LOG AT THE END OF THE FILE
27 *****************************************************************************/
28
29
30#ifndef SC_TIME_H
31#define SC_TIME_H
32
33
36
37#include <iostream>
38
39#include <string_view>
40
41namespace sc_core {
42
43// forward declarations
44
45class sc_simcontext;
46class sc_module;
47class sc_event_or_list;
48class sc_event_and_list;
49class sc_prim_channel;
50class sc_event;
52
53// friend operator declarations
54
55 const sc_time operator + ( const sc_time&, const sc_time& );
56 const sc_time operator - ( const sc_time&, const sc_time& );
57 const sc_time operator * ( const sc_time&, double );
58 const sc_time operator * ( double, const sc_time& );
59 const sc_time operator / ( const sc_time&, double );
60 double operator / ( const sc_time&, const sc_time& );
61
62// ----------------------------------------------------------------------------
63// Internal time representation
64//
65// Implementation defined
66// LRM: Time shall be represented internally as an unsigned
67// integer of at least 64 bits declared as sc_time::value_type.
68// ----------------------------------------------------------------------------
69
70#define SC_TIME_DT sc_dt::uint64
71
72// ----------------------------------------------------------------------------
73// ENUM : sc_time_unit
74//
75// Enumeration of time units.
76// NOTE: From IEEE Std 1666-2023 onwards, enumeration constant values are
77// implementation-defined. The constant values for SC_SEC, SC_MS,
78// SC_US, SC_NS, SC_PS and SC_FS follow IEEE Std 1666-2011 to enable
79// backwards compatibility.
80// ----------------------------------------------------------------------------
81
82enum sc_time_unit { SC_SEC = 5, SC_MS = 4, SC_US = 3, SC_NS = 2,
83 SC_PS = 1, SC_FS = 0, SC_AS = -1, SC_ZS = -2, SC_YS = -3 };
84
85// ----------------------------------------------------------------------------
86// CLASS : sc_time
87//
88// The time class.
89// ----------------------------------------------------------------------------
90
92{
93 friend class sc_module;
94 friend class sc_prim_channel;
95 friend class sc_event;
96 friend class sc_clock;
97
98 friend SC_API void next_trigger( const sc_time&, const sc_event_or_list&, sc_simcontext* );
99 friend SC_API void next_trigger( const sc_time&, const sc_event&, sc_simcontext* );
101 friend SC_API void next_trigger( double v, sc_time_unit, sc_simcontext* );
102 friend SC_API void next_trigger( double v, sc_time_unit, const sc_event&, sc_simcontext* );
103 friend SC_API void next_trigger( double v, sc_time_unit, const sc_event_and_list&, sc_simcontext* );
104 friend SC_API void next_trigger( double v, sc_time_unit, const sc_event_or_list&, sc_simcontext* );
105 friend SC_API void wait( const sc_time&, sc_simcontext* );
106 friend SC_API void wait( const sc_time&, const sc_event&, sc_simcontext* );
107 friend SC_API void wait( const sc_time&, const sc_event_and_list&, sc_simcontext* );
108 friend SC_API void wait( double v, sc_time_unit, sc_simcontext* );
109 friend SC_API void wait( double v, sc_time_unit, const sc_event&, sc_simcontext* );
110 friend SC_API void wait( double v, sc_time_unit, const sc_event_and_list&, sc_simcontext* );
111 friend SC_API void wait( double v, sc_time_unit, const sc_event_or_list&, sc_simcontext* );
112
113public:
114
116
117 // constructors
118
119 sc_time();
120 sc_time( const sc_time& );
122
123 // convert time object from string
124 // For C++ versions prior to C++17, offer some (non-standard) backwards compatibility
125 // using std::string instead of std::string_view
126 explicit sc_time( std::string_view strv );
127 static sc_time from_string( std::string_view strv );
128
129 // deprecated, use from_value(v)
130 sc_time( double, bool scale );
131 sc_time( value_type, bool scale );
132
133 // assignment operator
134
135 sc_time& operator = ( const sc_time& );
136
137 // conversion functions
138
139 value_type value() const; // relative to the time resolution
140 double to_double() const; // relative to the time resolution
141 double to_seconds() const;
142 double to_default_time_units() const;
143 const std::string to_string() const;
144
146 static sc_time from_seconds( double );
147
148 // relational operators
149
150 bool operator == ( const sc_time& ) const;
151 bool operator != ( const sc_time& ) const;
152 bool operator < ( const sc_time& ) const;
153 bool operator <= ( const sc_time& ) const;
154 bool operator > ( const sc_time& ) const;
155 bool operator >= ( const sc_time& ) const;
156
157 // arithmetic operators
158
159 sc_time& operator += ( const sc_time& );
160 sc_time& operator -= ( const sc_time& );
161 sc_time& operator *= ( double );
162 sc_time& operator /= ( double );
163 sc_time& operator %= ( const sc_time& );
164
165 friend const sc_time operator + ( const sc_time&, const sc_time& );
166 friend const sc_time operator - ( const sc_time&, const sc_time& );
167 friend const sc_time operator * ( const sc_time&, double );
168 friend const sc_time operator * ( double, const sc_time& );
169 friend const sc_time operator / ( const sc_time&, double );
170 friend double operator / ( const sc_time&, const sc_time& );
171 friend const sc_time operator % ( const sc_time&, const sc_time& );
172
173 // print function
174
175 void print( ::std::ostream& os = std::cout ) const;
176
177private: // implementation-defined
179
180private:
181 value_type m_value;
182};
183
184// ----------------------------------------------------------------------------
185// CLASS : sc_time_tuple
186//
187// The time tuple helper class.
188// ----------------------------------------------------------------------------
189
191{
192 typedef sc_time::value_type value_type;
193 friend class sc_time;
194
195private:
196 explicit sc_time_tuple( value_type v );
197 void init( value_type v );
198
199public:
201 : m_value(), m_unit( SC_SEC ), m_offset(1) {}
202
203 sc_time_tuple( const sc_time & t );
204
205 bool has_value() const;
206 value_type value() const;
207 sc_time_unit unit() const { return m_unit; } // normalized unit
208 const char * unit_symbol() const; // normalized unit symbol
209
210 operator sc_time() const { return sc_time( to_double(), m_unit ); }
211
212 double to_double() const; // relative to the normalized unit
213 std::string to_string() const;
214
215private:
216 value_type m_value;
217 sc_time_unit m_unit;
218 unsigned m_offset;
219};
220
221// stream operator for printing
222
223inline ::std::ostream& operator << ( ::std::ostream&, const sc_time& );
224
225// IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
226
227extern SC_API const sc_time SC_ZERO_TIME;
228
229// constructors
230
231inline
233: m_value( 0 )
234{}
235
236inline
238: m_value( t.m_value )
239{}
240
241inline
242sc_time_tuple::sc_time_tuple( value_type v )
243 : m_value(), m_unit( SC_SEC ), m_offset(1)
244{
245 if( v )
246 init( v );
247}
248
249inline
251 : m_value(), m_unit( SC_SEC ), m_offset(1)
252{
253 if( t != SC_ZERO_TIME )
254 init( t.value() );
255}
256
257inline
260{
261 return sc_time( v, SC_SEC );
262}
263
264
265// assignment operator
266
267inline
268sc_time&
270{
271 m_value = t.m_value;
272 return *this;
273}
274
275
276// conversion functions
277
278inline
280sc_time::value() const // relative to the time resolution
281{
282 return m_value;
283}
284
285
286inline
287double
288sc_time::to_double() const // relative to the time resolution
289{
290 return sc_dt::uint64_to_double( m_value );
291}
292
293
294inline
295double
296sc_time_tuple::to_double() const // relative to the normalized time unit
297{
298 return sc_dt::uint64_to_double( m_value ) * m_offset;
299}
300
301
302inline
303const std::string
305{
306 return sc_time_tuple( *this ).to_string();
307}
308
309
310// relational operators
311
312inline
313bool
315{
316 return ( m_value == t.m_value );
317}
318
319inline
320bool
322{
323 return ( m_value != t.m_value );
324}
325
326inline
327bool
328sc_time::operator < ( const sc_time& t ) const
329{
330 return ( m_value < t.m_value );
331}
332
333inline
334bool
335sc_time::operator <= ( const sc_time& t ) const
336{
337 return ( m_value <= t.m_value );
338}
339
340inline
341bool
343{
344 return ( m_value > t.m_value );
345}
346
347inline
348bool
350{
351 return ( m_value >= t.m_value );
352}
353
354
355// arithmetic operators
356
357inline
358sc_time&
360{
361 m_value += t.m_value;
362 return *this;
363}
364
365inline
366sc_time&
368{
369 m_value -= t.m_value;
370 return *this;
371}
372
373
374inline
375const sc_time
376operator + ( const sc_time& t1, const sc_time& t2 )
377{
378 return sc_time( t1 ) += t2;
379}
380
381inline
382const sc_time
383operator - ( const sc_time& t1, const sc_time& t2 )
384{
385 return sc_time( t1 ) -= t2;
386}
387
388
389inline
390sc_time&
392{
393 // linux bug workaround; don't change next two lines
394 volatile double tmp = sc_dt::uint64_to_double( m_value ) * d + 0.5;
395 m_value = static_cast<sc_dt::int64>( tmp );
396 return *this;
397}
398
399inline
400sc_time&
402{
403 // linux bug workaround; don't change next two lines
404 volatile double tmp = sc_dt::uint64_to_double( m_value ) / d + 0.5;
405 m_value = static_cast<sc_dt::int64>( tmp );
406 return *this;
407}
408
409inline
410sc_time&
412{
413 m_value %= t.m_value;
414 return *this;
415}
416
417inline
418const sc_time
419operator * ( const sc_time& t, double d )
420{
421 sc_time tmp( t );
422 return tmp *= d;
423}
424
425inline
426const sc_time
427operator * ( double d, const sc_time& t )
428{
429 sc_time tmp( t );
430 return tmp *= d;
431}
432
433inline
434const sc_time
435operator / ( const sc_time& t, double d )
436{
437 sc_time tmp( t );
438 return tmp /= d;
439}
440
441inline
442double
443operator / ( const sc_time& t1, const sc_time& t2 )
444{
445 return ( t1.to_double() / t2.to_double() );
446}
447
448inline
449const sc_time
450operator % ( const sc_time& t1, const sc_time& t2 )
451{
452 sc_time tmp(t1);
453 return tmp %= t2;
454}
455
456// operator<< for printing
457
458inline
459::std::ostream&
460operator << ( ::std::ostream& os, const sc_time& t )
461{
462 t.print( os );
463 return os;
464}
465
466
467// ----------------------------------------------------------------------------
468// STRUCT : sc_time_params
469//
470// Struct that holds the time resolution and default time unit.
471// ----------------------------------------------------------------------------
472
474{
475 double time_resolution; // in femto seconds
478
481
484};
485
486
487// ----------------------------------------------------------------------------
488
489// functions for accessing the time resolution and default time unit
490
491SC_API extern void sc_set_time_resolution( double, sc_time_unit );
493
494SC_API extern void sc_set_default_time_unit( double, sc_time_unit );
496
497} // namespace sc_core
498
499#endif
500
501// $Log: sc_time.h,v $
502// Revision 1.5 2011/08/26 20:46:11 acg
503// Andy Goodrich: moved the modification log to the end of the file to
504// eliminate source line number skew when check-ins are done.
505//
506// Revision 1.4 2011/02/18 20:27:14 acg
507// Andy Goodrich: Updated Copyrights.
508//
509// Revision 1.3 2011/02/13 21:47:38 acg
510// Andy Goodrich: update copyright notice.
511//
512// Revision 1.2 2008/05/22 17:06:27 acg
513// Andy Goodrich: updated copyright notice to include 2008.
514//
515// Revision 1.1.1.1 2006/12/15 20:20:05 acg
516// SystemC 2.3
517//
518// Revision 1.4 2006/05/08 18:02:06 acg
519// Andy Goodrich: added David Long's forward declarations for friend
520// functions, methods, and operators to keep the Microsoft compiler happy.
521//
522// Revision 1.3 2006/01/13 18:44:30 acg
523// Added $Log to record CVS changes into the source.
524
525// Taf!
#define SC_TIME_DT
Definition: sc_time.h:70
#define SC_API
Definition: sc_cmnhdr.h:148
auto operator>(const sc_vector_iter< T1, Pol1 > &a, const sc_vector_iter< T2, Pol2 > &b) -> decltype((b< a))
Definition: sc_vector.h:363
const sc_time operator+(const sc_time &, const sc_time &)
Definition: sc_time.h:376
class SC_API sc_simcontext
Definition: sc_object.h:50
bool operator!=(const sc_process_handle &left, const sc_process_handle &right)
auto operator<=(const sc_vector_iter< T1, Pol1 > &a, const sc_vector_iter< T2, Pol2 > &b) -> decltype(!(b< a))
Definition: sc_vector.h:368
class SC_API sc_event
Definition: sc_interface.h:36
inline::std::ostream & operator<<(::std::ostream &os, const sc_fifo< T > &a)
Definition: sc_fifo.h:428
SC_API void wait(int, sc_simcontext *)
bool operator==(const sc_process_handle &left, const sc_process_handle &right)
const sc_time operator%(const sc_time &t1, const sc_time &t2)
Definition: sc_time.h:450
SC_API void sc_set_time_resolution(double, sc_time_unit)
SC_API void next_trigger(sc_simcontext *)
SC_API sc_time sc_get_default_time_unit()
const sc_time operator-(const sc_time &, const sc_time &)
Definition: sc_time.h:383
bool operator<(const sc_process_handle &left, const sc_process_handle &right)
SC_API const sc_time SC_ZERO_TIME
sc_time_unit
Definition: sc_time.h:82
@ SC_MS
Definition: sc_time.h:82
@ SC_PS
Definition: sc_time.h:83
@ SC_SEC
Definition: sc_time.h:82
@ SC_YS
Definition: sc_time.h:83
@ SC_FS
Definition: sc_time.h:83
@ SC_US
Definition: sc_time.h:82
@ SC_AS
Definition: sc_time.h:83
@ SC_ZS
Definition: sc_time.h:83
@ SC_NS
Definition: sc_time.h:82
auto operator>=(const sc_vector_iter< T1, Pol1 > &a, const sc_vector_iter< T2, Pol2 > &b) -> decltype(!(a< b))
Definition: sc_vector.h:358
const sc_time operator*(const sc_time &, double)
Definition: sc_time.h:419
SC_API void sc_set_default_time_unit(double, sc_time_unit)
const sc_time operator/(const sc_time &, double)
Definition: sc_time.h:435
class SC_API sc_module
Definition: sc_object.h:44
SC_API sc_time sc_get_time_resolution()
class SC_API sc_time_tuple
Definition: sc_time.h:51
double uint64_to_double(uint64 a)
Definition: scfx_ieee.h:684
SC_API std::string to_string(sc_enc)
long long int64
Definition: sc_nbdefs.h:215
sc_core::sc_signal_in_if< T > & value(const T &val)
Definition: sc_stub.h:217
bool operator!=(const sc_time &) const
Definition: sc_time.h:321
static sc_time from_seconds(double)
Definition: sc_time.h:259
void print(::std::ostream &os=std::cout) const
sc_time & operator-=(const sc_time &)
Definition: sc_time.h:367
bool operator>=(const sc_time &) const
Definition: sc_time.h:349
bool operator==(const sc_time &) const
Definition: sc_time.h:314
bool operator>(const sc_time &) const
Definition: sc_time.h:342
friend SC_API void next_trigger(const sc_time &, const sc_event_and_list &, sc_simcontext *)
sc_time(double, bool scale)
friend SC_API void wait(const sc_time &, const sc_event &, sc_simcontext *)
friend SC_API void next_trigger(const sc_time &, const sc_event_or_list &, sc_simcontext *)
static sc_time from_string(std::string_view strv)
sc_time(double, sc_time_unit)
double to_double() const
Definition: sc_time.h:288
friend SC_API void next_trigger(const sc_time &, const sc_event &, sc_simcontext *)
sc_time & operator%=(const sc_time &)
Definition: sc_time.h:411
sc_time & operator+=(const sc_time &)
Definition: sc_time.h:359
friend SC_API void wait(const sc_time &, sc_simcontext *)
double to_seconds() const
SC_TIME_DT value_type
Definition: sc_time.h:115
const std::string to_string() const
Definition: sc_time.h:304
sc_time & operator=(const sc_time &)
Definition: sc_time.h:269
double to_default_time_units() const
friend SC_API void wait(const sc_time &, const sc_event_and_list &, sc_simcontext *)
sc_time(std::string_view strv)
value_type value() const
Definition: sc_time.h:280
sc_time(value_type, bool scale)
sc_time & operator/=(double)
Definition: sc_time.h:401
static sc_time from_value(value_type)
bool operator<(const sc_time &) const
Definition: sc_time.h:328
bool operator<=(const sc_time &) const
Definition: sc_time.h:335
sc_time & operator*=(double)
Definition: sc_time.h:391
const char * unit_symbol() const
std::string to_string() const
value_type value() const
bool has_value() const
sc_time_unit unit() const
Definition: sc_time.h:207
double to_double() const
Definition: sc_time.h:296
bool time_resolution_specified
Definition: sc_time.h:476
bool default_time_unit_specified
Definition: sc_time.h:480
sc_time::value_type default_time_unit
Definition: sc_time.h:479