summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/output.cpp
blob: d374ef1e9a8e9c89e348823caeba6a6696e20271 (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// license:BSD-3-Clause
// copyright-holders:Nicola Salmoria, Aaron Giles, Vas Crabb
/***************************************************************************

    output.cpp

    General purpose output routines.

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

#include "emu.h"
#include "output.h"

#include "coreutil.h"

#include <algorithm>


#define OUTPUT_VERBOSE 0



//**************************************************************************
//  OUTPUT ITEM
//**************************************************************************

output_manager::output_item::output_item(
		output_manager &manager,
		std::string &&name,
		u32 id,
		s32 value)
	: m_manager(manager)
	, m_name(std::move(name))
	, m_id(id)
	, m_value(value)
	, m_notifylist()
{
}


void output_manager::output_item::notify(s32 value)
{
	if (OUTPUT_VERBOSE)
		m_manager.machine().logerror("Output %s = %d (was %d)\n", m_name, value, m_value);
	m_value = value;

	// call the local notifiers first
	for (auto const &notify : m_notifylist)
		notify(m_name.c_str(), value);

	// call the global notifiers next
	for (auto const &notify : m_manager.m_global_notifylist)
		notify(m_name.c_str(), value);
}



//**************************************************************************
//  OUTPUT ITEM PROXY
//**************************************************************************

void output_manager::item_proxy::resolve(device_t &device, std::string const &name)
{
	assert(!m_item);
	m_item = &device.machine().output().find_or_create_item(name.c_str(), 0);
}



//**************************************************************************
//  OUTPUT MANAGER
//**************************************************************************

/*-------------------------------------------------
    output_manager - constructor
-------------------------------------------------*/

output_manager::output_manager(running_machine &machine)
	: m_machine(machine)
	, m_uniqueid(12345)
{
	// add callbacks
	machine.add_notifier(MACHINE_NOTIFY_PAUSE, machine_notify_delegate(&output_manager::pause, this));
	machine.add_notifier(MACHINE_NOTIFY_RESUME, machine_notify_delegate(&output_manager::resume, this));
	machine.save().register_presave(save_prepost_delegate(FUNC(output_manager::presave), this));
	machine.save().register_postload(save_prepost_delegate(FUNC(output_manager::postload), this));
}


/*-------------------------------------------------
    register_save - register for save states
-------------------------------------------------*/

void output_manager::register_save()
{
	assert(m_save_order.empty());
	assert(!m_save_data);

	// make space for the data
	m_save_order.clear();
	m_save_order.reserve(m_itemtable.size());
	m_save_data = std::make_unique<s32 []>(m_itemtable.size());

	// sort existing outputs by name and register for save
	for (auto &item : m_itemtable)
		m_save_order.emplace_back(item.second);
	std::sort(m_save_order.begin(), m_save_order.end(), [] (auto const &l, auto const &r) { return l.get().name() < r.get().name(); });

	// register the reserved space for saving
	machine().save().save_pointer(nullptr, "output", nullptr, 0, NAME(m_save_data), m_itemtable.size());
	if (OUTPUT_VERBOSE)
		osd_printf_verbose("Registered %u outputs for save states\n", m_itemtable.size());

}


/*-------------------------------------------------
    find_item - find an item based on a string
-------------------------------------------------*/

output_manager::output_item *output_manager::find_item(char const *string)
{
	auto item = m_itemtable.find(std::string(string));
	if (item != m_itemtable.end())
		return &item->second;

	return nullptr;
}


/*-------------------------------------------------
    create_new_item - create a new item
-------------------------------------------------*/

output_manager::output_item &output_manager::create_new_item(char const *outname, s32 value)
{
	if (OUTPUT_VERBOSE)
		osd_printf_verbose("Creating output %s = %d%s\n", outname, value, m_save_data ? " (will not be saved)" : "");

	auto const ins(m_itemtable.emplace(
			std::piecewise_construct,
			std::forward_as_tuple(outname),
			std::forward_as_tuple(*this, outname, m_uniqueid++, value)));
	assert(ins.second);
	return ins.first->second;
}

output_manager::output_item &output_manager::find_or_create_item(char const *outname, s32 value)
{
	output_item *const item = find_item(outname);
	return item ? *item : create_new_item(outname, value);
}


/*-------------------------------------------------
    output_pause - send pause message
-------------------------------------------------*/

void output_manager::pause()
{
	set_value("pause", 1);
}

void output_manager::resume()
{
	set_value("pause", 0);
}


/*-------------------------------------------------
    presave - prepare data for save state
-------------------------------------------------*/

void output_manager::presave()
{
	for (size_t i = 0; m_save_order.size() > i; ++i)
		m_save_data[i] = m_save_order[i].get().get();
}


/*-------------------------------------------------
    postload - restore loaded data
-------------------------------------------------*/

void output_manager::postload()
{
	for (size_t i = 0; m_save_order.size() > i; ++i)
		 m_save_order[i].get().set(m_save_data[i]);
}


/*-------------------------------------------------
    output_set_value - set the value of an output
-------------------------------------------------*/

void output_manager::set_value(char const *outname, s32 value)
{
	output_item *const item = find_item(outname);

	// if no item of that name, create a new one and force notification
	if (!item)
		create_new_item(outname, value).notify(value);
	else
		item->set(value); // set the new value (notifies on change)
}


/*-------------------------------------------------
    output_get_value - return the value of an
    output
-------------------------------------------------*/

s32 output_manager::get_value(char const *outname)
{
	output_item const *const item = find_item(outname);

	// if no item, value is 0
	return item ? item->get() : 0;
}


/*-------------------------------------------------
    output_set_notifier - sets a notifier callback
    for a particular output, or for all outputs
    if nullptr is specified
-------------------------------------------------*/

void output_manager::set_notifier(char const *outname, notifier_func callback, void *param)
{
	// if an item is specified, find/create it
	if (outname)
	{
		output_item *const item = find_item(outname);
		(item ? *item : create_new_item(outname, 0)).set_notifier(callback, param);
	}
	else
	{
		m_global_notifylist.emplace_back(callback, param);
	}
}


/*-------------------------------------------------
    output_name_to_id - returns a unique ID for
    a given name
-------------------------------------------------*/

u32 output_manager::name_to_id(char const *outname)
{
	// if no item, ID is 0
	output_item const *const item = find_item(outname);
	return item ? item->id() : 0;
}


/*-------------------------------------------------
    output_id_to_name - returns a name that maps
    to a given unique ID
-------------------------------------------------*/

char const *output_manager::id_to_name(u32 id)
{
	for (auto &item : m_itemtable)
		if (item.second.id() == id)
			return item.second.name().c_str();

	// nothing found, return nullptr
	return nullptr;
}