summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/watchdog.cpp
blob: cfd6bfeb7dfe1af12a512b2914d5140b93ff5e10 (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
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/***************************************************************************

    watchdog.c

    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_tag(nullptr)
{
}


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

void watchdog_timer_device::device_validity_check(validity_checker &valid) const
{
	if (m_vblank_count != 0)
	{
		screen_device *screen = dynamic_cast<screen_device *>(siblingdevice(m_screen_tag));
		if (screen == nullptr)
			osd_printf_error("Invalid screen tag specified\n");
	}
}


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

void watchdog_timer_device::device_start()
{
	// initialize the watchdog
	m_counter = 0;
	m_timer = timer_alloc();

	if (m_vblank_count != 0)
	{
		// fetch the screen
		screen_device *screen = siblingdevice<screen_device>(m_screen_tag);
		if (screen != nullptr)
			screen->register_vblank_callback(vblank_state_delegate(&watchdog_timer_device::watchdog_vblank, this));
	}
	save_item(NAME(m_enabled));
	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;
}


//-------------------------------------------------
//  device_timer - handle timer expiration events
//-------------------------------------------------

void watchdog_timer_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	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 - reset the watchdog timer
//-------------------------------------------------

void watchdog_timer_device::watchdog_enable(bool enable)
{
	// 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
//-------------------------------------------------

WRITE8_MEMBER( watchdog_timer_device::reset_w ) { watchdog_reset(); }
READ8_MEMBER( watchdog_timer_device::reset_r ) { watchdog_reset(); return space.unmap(); }


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

WRITE16_MEMBER( watchdog_timer_device::reset16_w ) { watchdog_reset(); }
READ16_MEMBER( watchdog_timer_device::reset16_r ) { watchdog_reset(); return space.unmap(); }


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

WRITE32_MEMBER( watchdog_timer_device::reset32_w ) { watchdog_reset(); }
READ32_MEMBER( watchdog_timer_device::reset32_r ) { watchdog_reset(); return space.unmap(); }