summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/netlist/nl_time.h
blob: 33729fb38d9fd68526f8b45f4404afd319c405fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
 * nltime.h
 */

#ifndef NLTIME_H_
#define NLTIME_H_

#include "nl_config.h"

//============================================================
//  MACROS
//============================================================

#define NLTIME_FROM_NS(_t)  netlist_time::from_nsec(_t)
#define NLTIME_FROM_US(_t)  netlist_time::from_usec(_t)
#define NLTIME_FROM_MS(_t)  netlist_time::from_msec(_t)
#define NLTIME_IMMEDIATE    netlist_time::from_nsec(0)

// ----------------------------------------------------------------------------------------
// net_list_time
// ----------------------------------------------------------------------------------------

struct netlist_time
{
public:

	typedef UINT64 INTERNALTYPE;

	static const INTERNALTYPE RESOLUTION = NETLIST_INTERNAL_RES;

	ATTR_HOT inline netlist_time() : m_time(0) {}

	ATTR_HOT friend inline const netlist_time operator-(const netlist_time &left, const netlist_time &right);
	ATTR_HOT friend inline const netlist_time operator+(const netlist_time &left, const netlist_time &right);
	ATTR_HOT friend inline const netlist_time operator*(const netlist_time &left, const UINT32 factor);
	ATTR_HOT friend inline const UINT32 operator/(const netlist_time &left, const netlist_time &right);
	ATTR_HOT friend inline bool operator>(const netlist_time &left, const netlist_time &right);
	ATTR_HOT friend inline bool operator<(const netlist_time &left, const netlist_time &right);
	ATTR_HOT friend inline bool operator>=(const netlist_time &left, const netlist_time &right);
	ATTR_HOT friend inline bool operator<=(const netlist_time &left, const netlist_time &right);

	ATTR_HOT inline const netlist_time &operator=(const netlist_time &right) { m_time = right.m_time; return *this; }
	ATTR_HOT inline const netlist_time &operator=(const double &right) { m_time = (INTERNALTYPE) ( right * (double) RESOLUTION); return *this; }
	ATTR_HOT inline operator double() const { return as_double(); }

	ATTR_HOT inline const netlist_time &operator+=(const netlist_time &right) { m_time += right.m_time; return *this; }

	ATTR_HOT inline const INTERNALTYPE as_raw() const { return m_time; }
	ATTR_HOT inline const double as_double() const { return (double) m_time / (double) RESOLUTION; }

	// for save states ....
	ATTR_HOT inline INTERNALTYPE *get_internaltype_ptr() { return &m_time; }

	ATTR_HOT static inline const netlist_time from_nsec(const int ns) { return netlist_time((UINT64) ns * (RESOLUTION / U64(1000000000))); }
	ATTR_HOT static inline const netlist_time from_usec(const int us) { return netlist_time((UINT64) us * (RESOLUTION / U64(1000000))); }
	ATTR_HOT static inline const netlist_time from_msec(const int ms) { return netlist_time((UINT64) ms * (RESOLUTION / U64(1000))); }
	ATTR_HOT static inline const netlist_time from_hz(const UINT64 hz) { return netlist_time(RESOLUTION / hz); }
	ATTR_HOT static inline const netlist_time from_double(const double t) { return netlist_time((INTERNALTYPE) ( t * (double) RESOLUTION)); }
	ATTR_HOT static inline const netlist_time from_raw(const INTERNALTYPE raw) { return netlist_time(raw); }

	static const netlist_time zero;

protected:

	ATTR_HOT inline netlist_time(const INTERNALTYPE val) : m_time(val) {}

	INTERNALTYPE m_time;
};

ATTR_HOT inline const netlist_time operator-(const netlist_time &left, const netlist_time &right)
{
	return netlist_time::from_raw(left.m_time - right.m_time);
}

ATTR_HOT inline const netlist_time operator*(const netlist_time &left, const UINT32 factor)
{
	return netlist_time::from_raw(left.m_time * factor);
}

ATTR_HOT inline const UINT32 operator/(const netlist_time &left, const netlist_time &right)
{
	return left.m_time / right.m_time;
}

ATTR_HOT inline const netlist_time operator+(const netlist_time &left, const netlist_time &right)
{
	return netlist_time::from_raw(left.m_time + right.m_time);
}

ATTR_HOT inline bool operator<(const netlist_time &left, const netlist_time &right)
{
	return (left.m_time < right.m_time);
}

ATTR_HOT inline bool operator>(const netlist_time &left, const netlist_time &right)
{
	return (left.m_time > right.m_time);
}

ATTR_HOT inline bool operator<=(const netlist_time &left, const netlist_time &right)
{
	return (left.m_time <= right.m_time);
}

ATTR_HOT inline bool operator>=(const netlist_time &left, const netlist_time &right)
{
	return (left.m_time >= right.m_time);
}


#endif /* NLTIME_H_ */