summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/watchdog.cpp
blob: ad0dba7b5be83e78398f786681a277d4a2a3c535 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/***************************************************************************

    Watchdog timer device.

***************************************************************************/

#include "emu.h"
#include "watchdog.h"

#include "emuopts.h"
#include "screen.h"


//**************************************************************************
//  WATCHDOG TIMER DEVICE
//**************************************************************************

DEFINE_DEVICE_TYPE(WATCHDOG_TIMER, watchdog_timer_device, "watchdog", "Watchdog Timer")

//-------------------------------------------------
//  watchdog_timer_device - constructor
//-------------------------------------------------

watchdog_timer_device::watchdog_timer_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, WATCHDOG_TIMER, tag, owner, clock)
	, m_vblank_count(0)
	, m_time(attotime::zero)
	, m_screen(*this, finder_base::DUMMY_TAG)
	, m_enabled(false)
	, m_reset_line(0)
	, m_counter(0)
{
}


//-------------------------------------------------
//  device_validity_check - validate the device
//  configuration
//-------------------------------------------------

void watchdog_timer_device::device_validity_check(validity_checker &valid) const
{
	if (m_vblank_count != 0)
	{
		if (m_screen.finder_tag() == finder_base::DUMMY_TAG)
			osd_printf_error("VBLANK count set without setting screen tag\n");
		else if (!m_screen)
			osd_printf_error("Screen device %s not found\n", m_screen.finder_tag());
	}
}


//-------------------------------------------------
//  device_start - perform device-specific startup
//-------------------------------------------------

void watchdog_timer_device::device_start()
{
	// initialize the watchdog
	m_timer = timer_alloc(FUNC(watchdog_timer_device::watchdog_expired), this);

	if (m_vblank_count != 0)
	{
		// fetch the screen
		if (m_screen)
			m_screen->register_vblank_callback(vblank_state_delegate(&watchdog_timer_device::watchdog_vblank, this));
	}

	save_item(NAME(m_enabled));
	save_item(NAME(m_reset_line));
	save_item(NAME(m_counter));
}


//-------------------------------------------------
//  device_reset - reset the device
//-------------------------------------------------

void watchdog_timer_device::device_reset()
{
	// set up the watchdog timer; only start off enabled if explicitly configured
	m_enabled = (m_vblank_count != 0 || m_time != attotime::zero);
	watchdog_reset();
	m_enabled = true;
}


//-------------------------------------------------
//  watchdog_expired - handle expired timer
//-------------------------------------------------

TIMER_CALLBACK_MEMBER(watchdog_timer_device::watchdog_expired)
{
	watchdog_fired();
}


//-------------------------------------------------
//  watchdog_reset - reset the watchdog timer
//-------------------------------------------------

void watchdog_timer_device::watchdog_reset()
{
	// if we're not enabled, skip it
	if (!m_enabled)
		m_timer->adjust(attotime::never);

	// VBLANK-based watchdog?
	else if (m_vblank_count != 0)
		m_counter = m_vblank_count;

	// timer-based watchdog?
	else if (m_time != attotime::zero)
		m_timer->adjust(m_time);

	// default to an obscene amount of time (3 seconds)
	else
		m_timer->adjust(attotime::from_seconds(3));
}


//-------------------------------------------------
//  watchdog_enable - enable the watchdog timer
//-------------------------------------------------

void watchdog_timer_device::watchdog_enable(int state)
{
	const bool enable = bool(state);

	// when re-enabled, we reset our state
	if (m_enabled != enable)
	{
		m_enabled = enable;
		watchdog_reset();
	}
}


//-------------------------------------------------
//  watchdog_fired - trigger machine reset
//-------------------------------------------------

void watchdog_timer_device::watchdog_fired()
{
	logerror("Reset caused by the watchdog!!!\n");

	bool verbose = machine().options().verbose();
#ifdef MAME_DEBUG
	verbose = true;
#endif
	if (verbose)
		popmessage("Reset caused by the watchdog!!!\n");

	machine().schedule_soft_reset();
}


//-------------------------------------------------
//  watchdog_vblank - VBLANK state callback for
//  watchdog timers
//-------------------------------------------------

void watchdog_timer_device::watchdog_vblank(screen_device &screen, bool vblank_state)
{
	// VBLANK starting
	if (vblank_state && m_enabled)
	{
		// check the watchdog
		if (m_vblank_count != 0)
			if (--m_counter == 0)
				watchdog_fired();
	}
}


//**************************************************************************
//  WATCHDOG READ/WRITE HELPERS
//**************************************************************************

//-------------------------------------------------
//  8-bit reset read/write handlers
//-------------------------------------------------

void watchdog_timer_device::reset_w(u8 data)
{
	watchdog_reset();
}

u8 watchdog_timer_device::reset_r(address_space &space)
{
	if (!machine().side_effects_disabled())
		watchdog_reset();

	return space.unmap();
}


//-------------------------------------------------
//  16-bit reset read/write handlers
//-------------------------------------------------

void watchdog_timer_device::reset16_w(u16 data)
{
	watchdog_reset();
}

u16 watchdog_timer_device::reset16_r(address_space &space)
{
	if (!machine().side_effects_disabled())
		watchdog_reset();

	return space.unmap();
}


//-------------------------------------------------
//  32-bit reset read/write handlers
//-------------------------------------------------

void watchdog_timer_device::reset32_w(u32 data)
{
	watchdog_reset();
}

u32 watchdog_timer_device::reset32_r(address_space &space)
{
	if (!machine().side_effects_disabled())
		watchdog_reset();

	return space.unmap();
}


//-------------------------------------------------
//  reset writeline handler
//-------------------------------------------------

void watchdog_timer_device::reset_line_w(int state)
{
	state = state ? 1 : 0;

	// reset watchdog on rising edge
	if (state && !m_reset_line)
		watchdog_reset();

	m_reset_line = state;
}