summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/debug/dvsave.cpp
blob: eda3fb59235894189c31b5374fd043ec74acb3fb (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/*********************************************************************

    dvsave.cpp

    Save debugger view.

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

#include "emu.h"
#include "dvsave.h"

#include "debugvw.h"

#include "screen.h"


//**************************************************************************
//  DEBUG VIEW STATE
//**************************************************************************

//-------------------------------------------------
//  debug_view_save - constructor
//-------------------------------------------------

debug_view_save::debug_view_save(running_machine &machine, debug_view_osd_update_func osdupdate, void *osdprivate)
	: debug_view(machine, DVT_SAVE, osdupdate, osdprivate)
	, m_divider(40)
{
}


//-------------------------------------------------
//  ~debug_view_save - destructor
//-------------------------------------------------

debug_view_save::~debug_view_save()
{
	reset();
}


//-------------------------------------------------
//  reset - delete all of our state items
//-------------------------------------------------

void debug_view_save::reset()
{
	// free all items in the state list
	m_save_list.clear();
}


//-------------------------------------------------
//  recompute - recompute all info for the
//  registers view
//-------------------------------------------------

void debug_view_save::build_list_recursive(save_registered_item &item, uintptr_t objbase, int depth, int count)
{
	// update the base pointer and forward if a trivial unwrap
	if (item.unwrap_and_update_base(objbase))
		return build_list_recursive(item.subitems().front(), objbase, depth, count);

	// switch off the type
	switch (item.type())
	{
		// boolean types save as a single byte
		case save_registered_item::TYPE_BOOL:
		case save_registered_item::TYPE_INT:
		case save_registered_item::TYPE_UINT:
		case save_registered_item::TYPE_FLOAT:
			m_save_list.push_back(save_item(item, objbase, depth, count));
			break;

		// structs and containers iterate over owned items
		case save_registered_item::TYPE_CONTAINER:
		case save_registered_item::TYPE_STRUCT:
			if (item.subitems().size() == 0)
				break;
			m_save_list.push_back(save_item(item, objbase, depth, count));
			for (auto &subitem : item.subitems())
				build_list_recursive(subitem, objbase, depth + 1);
			break;

		// arrays are multiples of a single item
		case save_registered_item::TYPE_STATIC_ARRAY:
		case save_registered_item::TYPE_VECTOR_ARRAY:
		case save_registered_item::TYPE_RAW_ARRAY:
		{
			m_save_list.push_back(save_item(item, objbase, depth, count));
			auto subitem = item.subitems().begin();
			auto last = std::prev(item.subitems().end());
			int items_per_row = 1;
			if (subitem == last)
			{
				if (subitem->type() == save_registered_item::TYPE_BOOL)
					items_per_row = 32;
				else if (subitem->is_int())
				{
					if (subitem->native_size() <= 2)
						items_per_row = 16 / subitem->native_size();
					else
						items_per_row = 32 / subitem->native_size();
				}
				else if (subitem->type() == save_registered_item::TYPE_FLOAT)
					items_per_row = 4;
			}
			for (uint32_t rep = 0; rep < item.count(); rep += items_per_row)
			{
				build_list_recursive(*subitem, objbase + rep * item.native_size(), depth + 1, std::min<int>(items_per_row, item.count() - rep));
				if (subitem != last)
					++subitem;
			}
			break;
		}
	}
}

void debug_view_save::recompute()
{
	// start with a blank list
	reset();

	// build the list of items
	build_list_recursive(machine().save().root_registrar().item(), 0, 0);
	m_save_list[0].set_collapse(false);
	for (auto &item : m_save_list)
		item.update_value();

	// no longer need to recompute
	m_recompute = false;
}


//-------------------------------------------------
//  view_notify - handle notification of updates
//  to cursor changes
//-------------------------------------------------

void debug_view_save::view_notify(debug_view_notification type)
{
	if (type == VIEW_NOTIFY_SOURCE_CHANGED)
		m_recompute = true;
}


//-------------------------------------------------
//  view_update - update the contents of the
//  register view
//-------------------------------------------------

void debug_view_save::view_update()
{
	// if our assumptions changed, revisit them
	if (m_recompute)
		recompute();

	// update the console info
	m_total.x = m_divider + 90;
	m_total.y = m_save_list.size();

	// loop over rows
	m_total.y = 0;
	int collapse_depth = 100;
	int depth_index[100];
	debug_view_char *dest(&m_viewdata[0]);
	std::string tempstr;
	for (auto &item : m_save_list)
	{
		if (item.depth() > collapse_depth)
			continue;
		if (item.depth() == collapse_depth)
			collapse_depth = 100;
		if (item.depth() < collapse_depth)
		{
			if (m_total.y >= m_topleft.y && m_total.y < m_topleft.y + m_visible.y)
			{
				// get the name
				char const *name = item.name();
				if (name[0] == 0)
				{
					if (item.depth() == 0)
						tempstr = "(root)";
					else if (item.count() > 1)
						tempstr = string_format("[%d-%d]", depth_index[item.depth()], depth_index[item.depth()] + item.count() - 1);
					else
						tempstr = string_format("[%d]", depth_index[item.depth()]);
					name = tempstr.c_str();
				}
				std::string value = item.value();

				// see if we changed
				const u8 attrib(item.changed() ? DCA_CHANGED : DCA_NORMAL);

				// build up a string
				char temp[256];
				int len = 0;
				for (int index = 0; index < item.depth(); index++)
					temp[len++] = temp[len++] = ' ';
				temp[len++] = item.collapsible() ? (item.collapsed() ? '+' : '-') : ' ';
				temp[len++] = ' ';

				int namelen = std::min<int>(strlen(name), m_divider - 1 - len);
				memcpy(&temp[len], name, namelen);
				len += namelen;
				while (len < m_divider)
					temp[len++] = ' ';
				temp[len++] = ' ';

				int valuelen = std::min(value.length(), sizeof(temp) - m_divider - 4);
				memcpy(&temp[len], value.c_str(), valuelen);
				len += valuelen;

				if (len < sizeof(temp) - 1)
					memset(&temp[len], ' ', sizeof(temp) - 1 - len);
				temp[sizeof(temp) - 1] = 0;

				// copy data
				int col = 0;
				for (u32 effcol = m_topleft.x; (col < m_visible.x) && (effcol < len); ++dest, ++col)
				{
					dest->byte = temp[effcol++];
					dest->attrib = attrib | ((effcol <= m_divider) ? DCA_ANCILLARY : DCA_NORMAL);
				}

				// fill the rest with blanks
				while (col < m_visible.x)
				{
					dest->byte = ' ';
					dest->attrib = DCA_NORMAL;
					dest++;
					col++;
				}
			}
			m_total.y++;
			if (item.collapsed())
				collapse_depth = item.depth();

			// reset the depth index if this is an array
			depth_index[item.depth() + 1] = 0;
			depth_index[item.depth()] += item.count();
		}
	}
	for (int y = m_total.y; y < m_topleft.y + m_visible.y; y++)
	{
		for (int col = 0; col < m_visible.x; col++)
		{
			dest->byte = ' ';
			dest->attrib = DCA_NORMAL;
			dest++;
		}
	}
}


//-------------------------------------------------
//  view_click - handle a mouse click within the
//  current view
//-------------------------------------------------

void debug_view_save::view_click(const int button, const debug_view_xy& pos)
{
	int cury = 0;
	int collapse_depth = 100;
	for (auto &item : m_save_list)
	{
		if (item.depth() > collapse_depth)
			continue;
		if (item.depth() == collapse_depth)
			collapse_depth = 100;
		if (item.depth() < collapse_depth)
		{
			if (pos.y == cury)
			{
				if (!item.collapsible())
					break;
				item.set_collapse(!item.collapsed());
				begin_update();
				view_notify(VIEW_NOTIFY_VISIBLE_CHANGED);
				m_update_pending = true;
				end_update();
				break;
			}
			cury++;
			if (item.collapsed())
				collapse_depth = item.depth();
		}
	}
}



std::string debug_view_save::save_item::value()
{
	// pre-subtract the item's ptr_offset as it will get re-applied at unwrap time
	return value(m_item, m_count, m_objbase - m_item.ptr_offset(), m_collapsed);
}


std::string debug_view_save::save_item::value(save_registered_item &item, int count, uintptr_t objbase, bool collapsed)
{
	// update the base pointer and forward if a trivial unwrap
	if (item.unwrap_and_update_base(objbase))
		return value(item.subitems().front(), 0, objbase, collapsed);

	char tempbuf[256];
	tempbuf[0] = 0;
	int pos = 0;

	switch (item.type())
	{
		// boolean types
		case save_registered_item::TYPE_BOOL:
			if (count == 0)
				return *reinterpret_cast<bool const *>(objbase) ? "true" : "false";
			else
			{
				bool const *ptr = reinterpret_cast<bool const *>(objbase);
				for (int index = 0; index < count; index++)
					catprintf(tempbuf, pos, "%c ", *ptr++ ? 'T' : 'F');
				return tempbuf;
			}

		// signed integral types
		case save_registered_item::TYPE_INT:
			if (count == 0)
				catprintf(tempbuf, pos, "%lld", item.read_int_signed(objbase, item.native_size()));
			else
			{
				static u8 const s_width[] = { 4,4,6,6,11,11,11,11,20,20,20,20,20,20,20,20 };
				int size = item.native_size() & 15;
				for (int index = 0; index < count; index++)
					catprintf(tempbuf, pos, "%*lld ", collapsed ? 0 : s_width[size], item.read_int_signed(objbase + size * index, size));
			}
			return tempbuf;

		// unsigned integral types
		case save_registered_item::TYPE_UINT:
			if (count == 0)
				catprintf(tempbuf, pos, "0x%0*llX", collapsed ? 0 : 2 * item.native_size(), item.read_int_unsigned(objbase, item.native_size()));
			else
			{
				int size = item.native_size() & 15;
				for (int index = 0; index < count; index++)
					catprintf(tempbuf, pos, "0x%0*llX ", collapsed ? 0 : 2 * size, item.read_int_unsigned(objbase + size * index, size));
			}
			return tempbuf;

		// float types
		case save_registered_item::TYPE_FLOAT:
			if (count == 0)
				catprintf(tempbuf, pos, "%g", item.read_float(objbase, item.native_size()));
			else
			{
				int size = item.native_size() & 15;
				for (int index = 0; index < count; index++)
					catprintf(tempbuf, pos, "%*g ", collapsed ? 0 : 20, item.read_float(objbase + size * index, size));
			}
			return tempbuf;

		// structs and containers iterate over owned items
		case save_registered_item::TYPE_CONTAINER:
		case save_registered_item::TYPE_STRUCT:
			if (!collapsed || item.subitems().size() == 0)
				return "";
			else
			{
				catprintf(tempbuf, pos, "{");
				bool first = true;
				for (auto &subitem : item.subitems())
				{
					std::string val = value(subitem, 0, objbase, true);
					if (val[0] == 0)
						continue;
					char const *name = subitem.name();
					if (name[0] == 0)
						name = subitem.subitems().front().name();
					if (!catprintf(tempbuf, pos, "%s%s:%s", first ? "" : ",", name, val.c_str()))
						break;
					first = false;
				}
				catprintf(tempbuf, pos, "}");
				return tempbuf;
			}

		// arrays are multiples of a single item
		case save_registered_item::TYPE_STATIC_ARRAY:
		case save_registered_item::TYPE_VECTOR_ARRAY:
		case save_registered_item::TYPE_RAW_ARRAY:
		{
			if (!collapsed)
				catprintf(tempbuf, pos, "[%d]", item.count());
			else
			{
				catprintf(tempbuf, pos, "[");
				auto subitem = item.subitems().begin();
				auto last = std::prev(item.subitems().end());
				for (int num = 0; num < item.count(); num++)
				{
					std::string val = value(*subitem, 0, objbase + num * item.native_size(), true);
					if (!catprintf(tempbuf, pos, "%s%s", (num == 0) ? "" : ",", val.c_str()))
						break;
					if (subitem != last)
						++subitem;
				}
				catprintf(tempbuf, pos, "]");
			}
			return tempbuf;
		}

		default:
			return "(unknown)";
	}
}


bool debug_view_save::save_item::changed() const
{
	switch (m_item.type())
	{
		case save_registered_item::TYPE_BOOL:
		case save_registered_item::TYPE_INT:
		case save_registered_item::TYPE_UINT:
		case save_registered_item::TYPE_FLOAT:
			return (memcmp(reinterpret_cast<void const *>(m_objbase), m_prev_value, m_item.native_size()) != 0);

		// structs and containers iterate over owned items
		case save_registered_item::TYPE_CONTAINER:
		case save_registered_item::TYPE_STRUCT:
			return false;

		// arrays are multiples of a single item
		default:
			return false;
	}
}


void debug_view_save::save_item::update_value()
{
	switch (m_item.type())
	{
		case save_registered_item::TYPE_BOOL:
		case save_registered_item::TYPE_INT:
		case save_registered_item::TYPE_UINT:
		case save_registered_item::TYPE_FLOAT:
			memcpy(m_prev_value, reinterpret_cast<void const *>(m_objbase), m_item.native_size());
			break;

		// structs and containers iterate over owned items
		case save_registered_item::TYPE_CONTAINER:
		case save_registered_item::TYPE_STRUCT:
			return;

		// arrays are multiples of a single item
		default:
			return;
	}
}