summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/debug/dvdisasm.cpp
blob: 385a0c98a65b0cec4ee80dc2e752e79e26138f38 (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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// license:BSD-3-Clause
// copyright-holders:Aaron Giles, Olivier Galibert
/*********************************************************************

    dvdisasm.c

    Disassembly debugger view.

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

#include "emu.h"
#include "debugvw.h"
#include "dvdisasm.h"
#include "debugcpu.h"
#include "debugger.h"

//**************************************************************************
//  DEBUG VIEW DISASM SOURCE
//**************************************************************************

//-------------------------------------------------
//  debug_view_disasm_source - constructor
//-------------------------------------------------

debug_view_disasm_source::debug_view_disasm_source(std::string &&name, device_t &device)
	: debug_view_source(std::move(name), &device),
		m_space(device.memory().space(AS_PROGRAM)),
		m_decrypted_space(device.memory().has_space(AS_OPCODES) ? device.memory().space(AS_OPCODES) : device.memory().space(AS_PROGRAM))
{
}



//**************************************************************************
//  DEBUG VIEW DISASM
//**************************************************************************

const int debug_view_disasm::DEFAULT_DASM_LINES, debug_view_disasm::DEFAULT_DASM_WIDTH, debug_view_disasm::DASM_MAX_BYTES;


//-------------------------------------------------
//  debug_view_disasm - constructor
//-------------------------------------------------

debug_view_disasm::debug_view_disasm(running_machine &machine, debug_view_osd_update_func osdupdate, void *osdprivate)
	: debug_view(machine, DVT_DISASSEMBLY, osdupdate, osdprivate),
		m_right_column(DASM_RIGHTCOL_RAW),
		m_backwards_steps(3),
		m_dasm_width(DEFAULT_DASM_WIDTH),
		m_previous_pc(1),
		m_expression(machine)
{
	// fail if no available sources
	enumerate_sources();
	if(m_source_list.empty())
		throw std::bad_alloc();

	// count the number of comments
	int total_comments = 0;
	for(auto &source : m_source_list)
	{
		const debug_view_disasm_source &dasmsource = downcast<const debug_view_disasm_source &>(*source);
		total_comments += dasmsource.device()->debug()->comment_count();
	}

	// configure the view
	m_total.y = DEFAULT_DASM_LINES;
	m_supports_cursor = true;
}


//-------------------------------------------------
//  ~debug_view_disasm - destructor
//-------------------------------------------------

debug_view_disasm::~debug_view_disasm()
{
}


//-------------------------------------------------
//  enumerate_sources - enumerate all possible
//  sources for a disassembly view
//-------------------------------------------------

void debug_view_disasm::enumerate_sources()
{
	// start with an empty list
	m_source_list.clear();

	// iterate over devices with disassembly interfaces
	for (device_disasm_interface &dasm : disasm_interface_iterator(machine().root_device()))
	{
		if (dasm.device().memory().space_config(AS_PROGRAM))
		{
			m_source_list.emplace_back(
					std::make_unique<debug_view_disasm_source>(
						util::string_format("%s '%s'", dasm.device().name(), dasm.device().tag()),
						dasm.device()));
		}
	}

	// reset the source to a known good entry
	if (!m_source_list.empty())
		set_source(*m_source_list[0]);
}


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

void debug_view_disasm::view_notify(debug_view_notification type)
{
	if((type == VIEW_NOTIFY_CURSOR_CHANGED) && (m_cursor_visible == true))
		adjust_visible_y_for_cursor();

	else if(type == VIEW_NOTIFY_SOURCE_CHANGED)
	{
		const debug_view_disasm_source &source = downcast<const debug_view_disasm_source &>(*m_source);
		m_expression.set_context(&source.device()->debug()->symtable());
		m_expression.set_default_base(source.space().is_octal() ? 8 : 16);
	}
}


//-------------------------------------------------
//  view_char - handle a character typed within
//  the current view
//-------------------------------------------------

void debug_view_disasm::view_char(int chval)
{
	debug_view_xy origcursor = m_cursor;
	u8 end_buffer = 3;
	s32 temp;

	switch(chval)
	{
		case DCH_UP:
			if(m_cursor.y > 0)
				m_cursor.y--;
			break;

		case DCH_DOWN:
			if(m_cursor.y < m_total.y - 1)
				m_cursor.y++;
			break;

		case DCH_PUP:
			temp = m_cursor.y -(m_visible.y - end_buffer);
			if(temp < 0)
				m_cursor.y = 0;
			else
				m_cursor.y = temp;
			break;

		case DCH_PDOWN:
			temp = m_cursor.y +(m_visible.y - end_buffer);
			if(temp > m_total.y - 1)
				m_cursor.y = m_total.y - 1;
			else
				m_cursor.y = temp;
			break;

		case DCH_HOME:              // set the active column to the PC
		{
			const debug_view_disasm_source &source = downcast<const debug_view_disasm_source &>(*m_source);
			offs_t pc = source.device()->state().pcbase() & source.m_space.logaddrmask();

			// figure out which row the pc is on
			for(unsigned int curline = 0; curline < m_dasm.size(); curline++)
				if(m_dasm[curline].m_address == pc)
					m_cursor.y = curline;
			break;
		}

		case DCH_CTRLHOME:
			m_cursor.y = 0;
			break;

		case DCH_CTRLEND:
			m_cursor.y = m_total.y - 1;
			break;
	}

	/* send a cursor changed notification */
	if(m_cursor.y != origcursor.y)
	{
		begin_update();
		view_notify(VIEW_NOTIFY_CURSOR_CHANGED);
		m_update_pending = true;
		end_update();
	}
}


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

void debug_view_disasm::view_click(const int button, const debug_view_xy& pos)
{
	const debug_view_xy origcursor = m_cursor;
	m_cursor = pos;

	/* cursor popup|toggle */
	bool cursorVisible = true;
	if(m_cursor.y == origcursor.y)
	{
		cursorVisible = !m_cursor_visible;
	}

	/* send a cursor changed notification */
	begin_update();
	m_cursor_visible = cursorVisible;
	view_notify(VIEW_NOTIFY_CURSOR_CHANGED);
	m_update_pending = true;
	end_update();
}

void debug_view_disasm::generate_from_address(debug_disasm_buffer &buffer, offs_t address)
{
	m_dasm.clear();
	for(int i=0; i != m_total.y; i++) {
		std::string dasm;
		offs_t size;
		offs_t next_address;
		u32 info;
		buffer.disassemble(address, dasm, next_address, size, info);
		m_dasm.emplace_back(address, size, dasm);
		address = next_address;
	}
}

bool debug_view_disasm::generate_with_pc(debug_disasm_buffer &buffer, offs_t pc)
{
	// Consider that instructions are 64 bytes max
	const debug_view_disasm_source &source = downcast<const debug_view_disasm_source &>(*m_source);
	int shift = source.m_space.addr_shift();

	offs_t backwards_offset;
	if(shift < 0)
		backwards_offset = 64 >> -shift;
	else if(shift == 0)
		backwards_offset = 64;
	else
		backwards_offset = 64 << shift;

	m_dasm.clear();
	offs_t address = (pc - m_backwards_steps*backwards_offset) & source.m_space.logaddrmask();
	// Handle wrap at 0
	if(address > pc)
		address = 0;

	util::disasm_interface &intf(dynamic_cast<device_disasm_interface &>(*source.device()).get_disassembler());
	if(intf.interface_flags() & util::disasm_interface::NONLINEAR_PC) {
		offs_t lpc = intf.pc_real_to_linear(pc);
		while(intf.pc_real_to_linear(address) < lpc) {
			std::string dasm;
			offs_t size;
			offs_t next_address;
			u32 info;
			buffer.disassemble(address, dasm, next_address, size, info);
			m_dasm.emplace_back(address, size, dasm);
			if(intf.pc_real_to_linear(address) > intf.pc_real_to_linear(next_address))
				return false;
			address = next_address;
		}

	} else {
		while(address < pc) {
			std::string dasm;
			offs_t size;
			offs_t next_address;
			u32 info;
			buffer.disassemble(address, dasm, next_address, size, info);
			m_dasm.emplace_back(address, size, dasm);
			if(address > next_address)
				return false;
			address = next_address;
		}
	}

	if(address != pc)
		return false;

	if(m_dasm.size() > m_backwards_steps)
		m_dasm.erase(m_dasm.begin(), m_dasm.begin() + (m_dasm.size() - m_backwards_steps));

	while(m_dasm.size() < m_total.y) {
		std::string dasm;
		offs_t size;
		offs_t next_address;
		u32 info;
		buffer.disassemble(address, dasm, next_address, size, info);
		m_dasm.emplace_back(address, size, dasm);
		address = next_address;
	}
	return true;
}

int debug_view_disasm::address_position(offs_t pc) const
{
	for(int i=0; i != int(m_dasm.size()); i++)
		if(m_dasm[i].m_address == pc)
			return i;
	return -1;
}

void debug_view_disasm::generate_dasm(debug_disasm_buffer &buffer, offs_t pc)
{
	bool pc_changed = pc != m_previous_pc;
	m_previous_pc = pc;
	if(strcmp(m_expression.string(), "curpc")) {
		if(m_expression.dirty()) {
			m_topleft.x = 0;
			m_topleft.y = 0;
		}
		const debug_view_disasm_source &source = downcast<const debug_view_disasm_source &>(*m_source);
		generate_from_address(buffer, m_expression.value() & source.m_space.logaddrmask());
		return;
	}

	if(address_position(pc) != -1) {
		generate_from_address(buffer, m_dasm[0].m_address);
		int pos = address_position(pc);
		if(pos != -1) {
			if(!pc_changed)
				return;
			if(pos >= m_topleft.y && pos < m_topleft.y + m_visible.y - 2)
				return;
			if(pos < m_total.y - m_visible.y) {
				m_topleft.x = 0;
				m_topleft.y = pos - m_backwards_steps;
				return;
			}
		}
	}

	m_topleft.x = 0;
	m_topleft.y = 0;

	if(generate_with_pc(buffer, pc))
		return;

	generate_from_address(buffer, pc);
}

void debug_view_disasm::complete_information(const debug_view_disasm_source &source, debug_disasm_buffer &buffer, offs_t pc)
{
	for(auto &dasm : m_dasm) {
		offs_t adr = dasm.m_address;

		dasm.m_tadr = buffer.pc_to_string(adr);
		dasm.m_topcodes = buffer.data_to_string(adr, dasm.m_size, true);
		dasm.m_tparams  = buffer.data_to_string(adr, dasm.m_size, false);

		dasm.m_is_pc = adr == pc;

		dasm.m_is_bp = source.device()->debug()->breakpoint_find(adr) != nullptr;
		dasm.m_is_visited = source.device()->debug()->track_pc_visited(adr);

		const char *comment = source.device()->debug()->comment_text(adr);
		if(comment)
			dasm.m_comment = comment;
	}
}

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

void debug_view_disasm::view_update()
{
	const debug_view_disasm_source &source = downcast<const debug_view_disasm_source &>(*m_source);
	debug_disasm_buffer buffer(*source.device());
	offs_t pc = source.device()->state().pcbase() & source.m_space.logaddrmask();

	generate_dasm(buffer, pc);

	complete_information(source, buffer, pc);
	redraw();
}


//-------------------------------------------------
//  print - print a string in the disassembly view
//-------------------------------------------------

void debug_view_disasm::print(int row, std::string text, int start, int end, u8 attrib)
{
	int view_end = end - m_topleft.x;
	if(view_end < 0)
		return;

	int string_0 = start - m_topleft.x;
	if(string_0 >= m_visible.x)
		return;

	int view_start = string_0 > 0 ? string_0 : 0;
	debug_view_char *dest = &m_viewdata[row * m_visible.x + view_start];

	if(view_end >= m_visible.x)
		view_end = m_visible.x;

	for(int pos = view_start; pos < view_end; pos++) {
		int spos = pos - string_0;
		if(spos >= int(text.size()))
			*dest++ = { ' ', attrib };
		else
			*dest++ = { u8(text[spos]), attrib };
	}
}


//-------------------------------------------------
//  redraw - update the view from the data
//-------------------------------------------------

void debug_view_disasm::redraw()
{
	// determine how many characters we need for an address and set the divider
	int m_divider1 = 1 + m_dasm[0].m_tadr.size() + 1;

	// assume a fixed number of characters for the disassembly
	int m_divider2 = m_divider1 + 1 + m_dasm_width + 1;

	// set the width of the third column to max comment length
	m_total.x = m_divider2 + 1 + 50;        // DEBUG_COMMENT_MAX_LINE_LENGTH

	// loop over visible rows
	for(u32 row = 0; row < m_visible.y; row++)
	{
		u32 effrow = m_topleft.y + row;

		// if this visible row is valid, add it to the buffer
		u8 attrib = DCA_NORMAL;
		if(effrow < m_dasm.size())
		{
			// if we're on the line with the PC, hilight it
			if(m_dasm[effrow].m_is_pc)
				attrib = DCA_CURRENT;

			// if we're on a line with a breakpoint, tag it changed
			else if(m_dasm[effrow].m_is_bp)
				attrib = DCA_CHANGED;

			// if we're on the active column and everything is couth, highlight it
			if(m_cursor_visible && effrow == m_cursor.y)
				attrib |= DCA_SELECTED;

			// if we've visited this pc, mark it as such
			if(m_dasm[effrow].m_is_visited)
				attrib |= DCA_VISITED;

			print(row, ' ' + m_dasm[effrow].m_tadr, 0, m_divider1, attrib | DCA_ANCILLARY);
			print(row, ' ' + m_dasm[effrow].m_dasm, m_divider1, m_divider2, attrib);

			if(m_right_column == DASM_RIGHTCOL_RAW || m_right_column == DASM_RIGHTCOL_ENCRYPTED) {
				std::string text = ' ' +(m_right_column == DASM_RIGHTCOL_RAW ? m_dasm[effrow].m_topcodes : m_dasm[effrow].m_tparams);
				print(row, text, m_divider2, m_visible.x, attrib | DCA_ANCILLARY);
				if(int(text.size()) > m_visible.x - m_divider2) {
					int base = m_total.x - 3;
					if(base < m_divider2)
						base = m_divider2;
					print(row, "...", base, m_visible.x, attrib | DCA_ANCILLARY);
				}
			} else if(!m_dasm[effrow].m_comment.empty())
				print(row, " // " + m_dasm[effrow].m_comment, m_divider2, m_visible.x, attrib | DCA_COMMENT | DCA_ANCILLARY);
			else
				print(row, "", m_divider2, m_visible.x, attrib | DCA_COMMENT | DCA_ANCILLARY);
		}
	}
}


//-------------------------------------------------
//  selected_address - return the PC of the
//  currently selected address in the view
//-------------------------------------------------

offs_t debug_view_disasm::selected_address()
{
	flush_updates();
	return m_dasm[m_cursor.y].m_address;
}


//-------------------------------------------------
//  set_sexpression - set the expression string
//  describing the home address
//-------------------------------------------------

void debug_view_disasm::set_expression(const std::string &expression)
{
	begin_update();
	m_expression.set_string(expression);
	m_recompute = m_update_pending = true;
	end_update();
}


//-------------------------------------------------
//  set_right_column - set the contents of the
//  right column
//-------------------------------------------------

void debug_view_disasm::set_right_column(disasm_right_column contents)
{
	begin_update();
	m_right_column = contents;
	m_recompute = m_update_pending = true;
	end_update();
}


//-------------------------------------------------
//  set_backward_steps - set the number of
//  instructions displayed before the home address
//-------------------------------------------------

void debug_view_disasm::set_backward_steps(u32 steps)
{
	begin_update();
	m_backwards_steps = steps;
	m_recompute = m_update_pending = true;
	end_update();
}


//-------------------------------------------------
//  set_disasm_width - set the width in characters
//  of the main disassembly section
//-------------------------------------------------

void debug_view_disasm::set_disasm_width(u32 width)
{
	begin_update();
	m_dasm_width = width;
	m_update_pending = true;
	end_update();
}


//-------------------------------------------------
//  set_selected_address - set the PC of the
//  currently selected address in the view
//-------------------------------------------------

void debug_view_disasm::set_selected_address(offs_t address)
{
	const debug_view_disasm_source &source = downcast<const debug_view_disasm_source &>(*m_source);
	address = address & source.m_space.logaddrmask();
	for(int line = 0; line < m_total.y; line++)
		if(m_dasm[line].m_address == address) {
			m_cursor.y = line;
			set_cursor_position(m_cursor);
			break;
		}
}

//-------------------------------------------------
//  set_source - set the current subview
//-------------------------------------------------

void debug_view_disasm::set_source(const debug_view_source &source)
{
	if(&source != m_source) {
		debug_view::set_source(source);
		m_dasm.clear();
	}
}