summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/clock.h
blob: 1dc4739ce6c82936689181062a0a7405d918fafa (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
// license:BSD-3-Clause
// copyright-holders:smf, hap
/*

  Generic clock signal device

*/

#ifndef MAME_MACHINE_CLOCK_H
#define MAME_MACHINE_CLOCK_H

#pragma once

class clock_device : public device_t
{
public:
	clock_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock = 0);

	auto signal_handler() { return m_signal_handler.bind(); }
	auto &set_period(attotime period) { m_period = period; reinit(); return *this; }
	auto &set_pulse_width(attotime pw) { assert(!pw.is_never()); m_pw = pw; reinit(); return *this; }
	auto &set_duty_cycle(double duty) { assert(duty >= 0.0 && duty <= 1.0); m_duty = duty; m_pw = attotime::never; reinit(); return *this; }

	DECLARE_READ_LINE_MEMBER(signal_r) { return m_signal; }

protected:
	virtual void device_start() override;
	virtual void device_reset() override { output(); }
	virtual void device_clock_changed() override { reinit(); }

	TIMER_CALLBACK_MEMBER(clock_init);
	TIMER_CALLBACK_MEMBER(clock_tick);

private:
	void reinit();
	void output();

	int m_signal;
	int m_output;
	double m_duty;
	attotime m_period;
	attotime m_pw;
	attotime m_thigh;
	attotime m_tlow;

	emu_timer *m_timer_init;
	emu_timer *m_timer_tick;

	devcb_write_line m_signal_handler;
};

DECLARE_DEVICE_TYPE(CLOCK, clock_device)

#endif // MAME_MACHINE_CLOCK_H