summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/clock.cpp
blob: 803e3d3a55f3bdba2658bbf154e3e1446f22be9d (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// license:BSD-3-Clause
// copyright-holders:smf, hap
/*

Generic clock signal device

Set the period either with device_t m_clock, or with set_period if it needs
to be more fine-tuned (m_clock has higher priority).

The duty cycle can be changed with set_duty_cycle (default is 50%), or the
pulse width (active time) can be set directly with set_pulse_width.

Output signal at machine start is right after falling edge.

TODO:
- add one-shot trigger? eg. monostable 555

*/

#include "emu.h"
#include "clock.h"

DEFINE_DEVICE_TYPE(CLOCK, clock_device, "clock", "Clock")

clock_device::clock_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock) :
	device_t(mconfig, CLOCK, tag, owner, clock),
	m_signal(0),
	m_output(-1),
	m_duty(0.5),
	m_period(attotime::never),
	m_pw(attotime::never),
	m_timer_init(nullptr),
	m_timer_tick(nullptr),
	m_signal_handler(*this)
{
}

void clock_device::device_start()
{
	m_signal_handler.resolve_safe();

	save_item(NAME(m_signal));
	save_item(NAME(m_output));
	save_item(NAME(m_duty));
	save_item(NAME(m_period));
	save_item(NAME(m_pw));
	save_item(NAME(m_thigh));
	save_item(NAME(m_tlow));

	m_timer_init = timer_alloc(TID_CLOCK_INIT);
	m_timer_tick = timer_alloc(TID_CLOCK_TICK);
	reinit();
}

void clock_device::reinit()
{
	if (!m_timer_init)
		return;

	// not using synchronize(), that may retrigger more than once
	m_timer_init->adjust(attotime::zero);
}

void clock_device::output()
{
	if (m_signal != m_output)
	{
		m_output = m_signal;
		m_signal_handler(m_output);
	}
}

void clock_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	switch (id)
	{
		case TID_CLOCK_INIT:
		{
			attotime period = (m_clock > 0) ? attotime::from_hz(m_clock) : m_period;
			assert(!period.is_zero());

			if (period.is_never())
			{
				m_timer_tick->adjust(attotime::never);
				return;
			}

			if (!m_pw.is_never())
			{
				// set timing via pulse width
				attotime pw = m_pw;
				if (pw > period)
					pw = period;

				m_thigh = pw;
				m_tlow = period - pw;
			}
			else
			{
				// set timing via duty cycle
				if (m_duty == 0.5)
				{
					m_thigh = period / 2;
					m_tlow = m_thigh;
				}
				else if (m_duty == 0.0)
				{
					m_thigh = attotime::zero;
					m_tlow = period;
				}
				else if (m_duty == 1.0)
				{
					m_thigh = period;
					m_tlow = attotime::zero;
				}
				else
				{
					double p = period.as_double();
					m_thigh = attotime::from_double(m_duty * p);
					m_tlow = attotime::from_double((1.0 - m_duty) * p);
				}
			}

			attotime next = m_signal ? m_thigh : m_tlow;
			if (next < m_timer_tick->remaining())
				m_timer_tick->adjust(next);

			break;
		}

		case TID_CLOCK_TICK:
			if (m_thigh.is_zero())
				m_signal = 0;
			else if (m_tlow.is_zero())
				m_signal = 1;
			else
				m_signal ^= 1;

			m_timer_tick->adjust(m_signal ? m_thigh : m_tlow);
			output();
			break;

		default:
			break;
	}
}