summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/modules/debugger/debugint.cpp
blob: 0f77f08f8028521bbd3b9d7150c14105ca2f028b (plain) (generated by cgit v1.2.3 (git 2.39.1) at 2026-05-22 21:21:43 +0000
href='#n221'>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 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
// license:BSD-3-Clause
// copyright-holders:Couriersud
/*********************************************************************

    debugint.c

    Internal debugger frontend using render interface.

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

#include "emu.h"
#include "ui/ui.h"
#include "ui/menu.h"
#include "rendfont.h"
#include "uiinput.h"

#include "debug/debugvw.h"
#include "debug/dvdisasm.h"
#include "debug/dvmemory.h"
#include "debug/dvbpoints.h"
#include "debug/dvwpoints.h"
#include "debug/debugcon.h"
#include "debug/debugcpu.h"

#include "config.h"
#include "debugger.h"
#include "modules/lib/osdobj_common.h"
#include "debug_module.h"
#include "modules/osdmodule.h"

class debug_internal : public osd_module, public debug_module
{
public:
	debug_internal()
	: osd_module(OSD_DEBUG_PROVIDER, "internal"), debug_module(),
		m_machine(nullptr)
	{
	}

	virtual ~debug_internal() { }

	virtual int init(const osd_options &options) override { return 0; }
	virtual void exit() override;

	virtual void init_debugger(running_machine &machine) override;
	virtual void wait_for_debugger(device_t &device, bool firststop) override;
	virtual void debugger_update() override;

private:
	running_machine* m_machine;
	const char*      font_name;
	float            font_size;
};



/***************************************************************************
    CONSTANTS
***************************************************************************/

#define BORDER_YTHICKNESS 1
#define BORDER_XTHICKNESS 1
#define HSB_HEIGHT 20
#define VSB_WIDTH 20
#define TITLE_HEIGHT (debug_font_height + 3*BORDER_YTHICKNESS)

enum
{
	RECT_DVIEW,
	RECT_DVIEW_CLIENT,
	RECT_DVIEW_TITLE,
	RECT_DVIEW_HSB,
	RECT_DVIEW_VSB,
	RECT_DVIEW_SIZE
};

enum
{
	VIEW_STATE_BUTTON           = 0x01,
	VIEW_STATE_MOVING           = 0x02,
	VIEW_STATE_SIZING           = 0x04,
	VIEW_STATE_NEEDS_UPDATE     = 0x08,
	VIEW_STATE_FOLLOW_CPU       = 0x10,
	VIEW_STATE_VISIBLE          = 0x20
};

/***************************************************************************
    MACROS
***************************************************************************/

//#define NX(_dv, _x) ((float) (_x)/(float)(_dv)->bounds.width())
//#define NY(_dv, _y) ((float) (_y)/(float)(_dv)->bounds.height())
#define NX(_dv, _x) ((float) (_x)/(float) (dv)->rt_width)
#define NY(_dv, _y) ((float) (_y)/(float) (dv)->rt_height)


#define LIST_ADD_FRONT(_list, _elem, _type) \
	do { \
		(_elem)->next = _list; \
		_list = _elem; \
	} while (0)

#define LIST_GET_PREVIOUS(_list, _elem, _prev) \
	do { \
		_prev = nullptr; \
		if (_list != _elem) \
			for (_prev = _list; _prev != nullptr; _prev = _prev->next) \
				if ((_prev)->next == _elem) \
					break; \
	} while (0)

#define LIST_GET_LAST(_list, _last) \
	do { \
		for (_last = _list; _last != nullptr; _last = _last->next) \
			if ((_last)->next == nullptr) \
				break; \
	} while (0)

#define LIST_REMOVE(_list, _elem, _type) \
	do { \
		_type *_hlp; \
		LIST_GET_PREVIOUS(_list, _elem, _hlp); \
		if (_hlp != nullptr) \
			(_hlp)->next = (_elem)->next; \
		else \
			_list = (_elem)->next; \
	} while (0)

#define LIST_ADD_BACK(_list, _elem, _type) \
	do { \
		_type *_hlp; \
		LIST_GET_LAST(_list, _hlp); \
		if (_hlp != nullptr) \
			(_hlp)->next = _elem; \
		else \
			_list = _elem; \
	} while (0)

/***************************************************************************
    TYPE DEFINITIONS
***************************************************************************/

struct adjustment
{
	int     visible;
	int     lower;
	int     upper;
	int     value;
	int     step_increment;
	int     page_increment;
	int     page_size;
};

class DView;

class DView_edit
{
	DISABLE_COPYING(DView_edit);

public:
	DView_edit(DView* owner): active(0), container(nullptr), owner(owner) { }
	~DView_edit() { }
	int                 active;
	render_container *  container;
	std::string         str;
	DView*              owner;
};

/***************************************************************************
    FUNCTION PROTOTYPES
***************************************************************************/

static void dview_update(debug_view &dw, void *osdprivate);
static int map_point(DView *dv, INT32 target_x, INT32 target_y, INT32 *mapped_x, INT32 *mapped_y);

class DView
{
	DISABLE_COPYING(DView);

public:
	DView(render_target *target, running_machine &machine, debug_view_type type, int flags)
		: next(nullptr),
			type(0),
			state(0),
			ofs_x(0),
			ofs_y(0),
			editor(this)
		{
		this->target = target;
		//dv->container = render_target_get_component_container(target, name, &pos);
		this->container = target->debug_alloc();
		this->view = machine.debug_view().alloc_view(type, dview_update, this);
		this->type = type;
		this->m_machine = &machine;
		this->state = flags | VIEW_STATE_NEEDS_UPDATE | VIEW_STATE_VISIBLE;
		// initial size
		this->bounds.set(0, 300, 0, 300);

		/* specials */
		switch (type)
		{
		case DVT_DISASSEMBLY:
			/* set up disasm view */
			downcast<debug_view_disasm *>(this->view)->set_expression("curpc");
			//debug_view_  property_UINT32(dv->view, DVP_DASM_TRACK_LIVE, 1);
			break;
		default:
			break;
		}
		}
	~DView()
	{
		//this->target->debug_free(*this->container);
		machine().debug_view().free_view(*this->view);
	}

	running_machine &machine() const { assert(m_machine != nullptr); return *m_machine; }

	DView *             next;

	int                 type;
	debug_view *        view;
	render_container *  container;
	render_target *     target;
	running_machine *   m_machine;
	int                 state;
	// drawing
	rectangle           bounds;
	int                 ofs_x;
	int                 ofs_y;
	std::string         title;
	int                 last_x;
	int                 last_y;
	// Scrollbars
	adjustment          hsb;
	adjustment          vsb;
	// render target tracking
	INT32               rt_width;
	INT32               rt_height;
	//optional
	DView_edit          editor;
};


/***************************************************************************
    INLINE FUNCTIONS
***************************************************************************/

static inline int dview_is_state(DView *dv, int state)
{
	return ((dv->state & state) ? TRUE : FALSE);
}

static inline int dview_is_state_all(DView *dv, int state)
{
	return ((dv->state & state) == state ? TRUE : FALSE);
}

static inline void dview_set_state(DView *dv, int state, int onoff)
{
	if (onoff)
		dv->state |= state;
	else
		dv->state &= ~state;
}

/***************************************************************************
    LOCAL VARIABLES
***************************************************************************/

static render_font *    debug_font = nullptr;
static int              debug_font_width;
static int              debug_font_height;
static float            debug_font_aspect;
static DView *          list = nullptr;
static DView *          focus_view;

static ui_menu *        menu;
static DView_edit *     cur_editor;
static int              win_count;
static void*            menu_sel;  // selected item in the menu

static void set_focus_view(DView *dv)
{
	if (focus_view != nullptr)
		dview_set_state(focus_view, VIEW_STATE_NEEDS_UPDATE, TRUE);

	if (dv != nullptr)
		dview_set_state(dv, VIEW_STATE_NEEDS_UPDATE, TRUE);

	if (focus_view != dv)
	{
		focus_view = dv;
		LIST_REMOVE(list, dv, DView);
		LIST_ADD_FRONT(list, dv, DView);
		dv->target->debug_append(*dv->container);
	}
}

static DView *dview_alloc(render_target *target, running_machine &machine, debug_view_type type, int flags)
{
	DView *dv;

	dv = global_alloc(DView(target, machine, type, flags));

	/* add to list */

	LIST_ADD_BACK(list, dv, DView);

	return dv;
}

static void dview_free(DView *dv)
{
	dv->container->empty();
	LIST_REMOVE(list, dv, DView);
	global_free(dv);
}

static void dview_get_rect(DView *dv, int type, rectangle &rect)
{
	rect = dv->bounds;
	switch (type)
	{
	case RECT_DVIEW:
		break;
	case RECT_DVIEW_CLIENT:
		rect.min_x += BORDER_XTHICKNESS;
		rect.max_x -= (BORDER_XTHICKNESS + dv->vsb.visible * VSB_WIDTH);
		rect.min_y += 2 * BORDER_YTHICKNESS + TITLE_HEIGHT;
		rect.max_y -= (BORDER_YTHICKNESS + dv->hsb.visible * HSB_HEIGHT);
		break;
	case RECT_DVIEW_HSB:
		rect.min_x += 0;
		rect.max_x -= /* dv->vsb.visible * */ VSB_WIDTH;
		rect.min_y = dv->bounds.max_y - HSB_HEIGHT;
		rect.max_y -= 0;
		break;
	case RECT_DVIEW_VSB:
		rect.min_x = dv->bounds.max_x - VSB_WIDTH;
		rect.max_x -= 0;
		rect.min_y += TITLE_HEIGHT;
		rect.max_y -= /* dv->hsb.visible * */ HSB_HEIGHT;
		break;
	case RECT_DVIEW_SIZE:
		rect.min_x = dv->bounds.max_x - VSB_WIDTH;
		rect.max_x -= 0;
		rect.min_y = dv->bounds.max_y - HSB_HEIGHT;
		rect.max_y -= 0;
		break;
	case RECT_DVIEW_TITLE:
		rect.min_x += 0;
		rect.max_x -= 0;
		rect.min_y += 0;
		rect.max_y = rect.min_y + TITLE_HEIGHT - 1;
		break;
	default:
		assert_always(FALSE, "unknown rectangle type");
	}
}


static void dview_clear(DView *dv)
{
	dv->container->empty();
}

static void dview_draw_outlined_box(DView *dv, int rtype, int x, int y, int w, int h, rgb_t bg)
{
	rectangle r;

	dview_get_rect(dv, rtype, r);
	dv->container->manager().machine().ui().draw_outlined_box(dv->container, NX(dv, x + r.min_x), NY(dv, y + r.min_y),
			NX(dv, x + r.min_x + w), NY(dv, y + r.min_y + h), bg);
}

static void dview_draw_box(DView *dv, int rtype, int x, int y, int w, int h, rgb_t col)
{
	rectangle r;

	dview_get_rect(dv, rtype, r);
	dv->container->add_rect(NX(dv, x + r.min_x), NY(dv, y + r.min_y),
			NX(dv, x + r.min_x + w), NY(dv, y + r.min_y + h), col,
			PRIMFLAG_BLENDMODE(BLENDMODE_ALPHA));
}

static void dview_draw_line(DView *dv, int rtype, int x1, int y1, int x2, int y2, rgb_t col)
{
	rectangle r;

	dview_get_rect(dv, rtype, r);
	dv->container->add_line(NX(dv, x1 + r.min_x), NY(dv, y1 + r.min_y),
			NX(dv, x2 + r.min_x), NY(dv, y2 + r.min_y), UI_LINE_WIDTH, col,
			PRIMFLAG_BLENDMODE(BLENDMODE_ALPHA));
}

static void dview_draw_char(DView *dv, int rtype, int x, int y, int h, rgb_t col, UINT16 ch)
{
	rectangle r;

	dview_get_rect(dv, rtype, r);
	dv->container->add_char(
			NX(dv, x + r.min_x),
			NY(dv, y + r.min_y),
			NY(dv, h),
			debug_font_aspect,
			//(float) dv->bounds.height() / (float) dv->bounds->width(), //render_get_ui_aspect(),
			col,
			*debug_font,
			ch);
}

static int dview_xy_in_rect(DView *dv, int type, int x, int y)
{
	rectangle r;

	dview_get_rect(dv, type, r);
	if (r.contains(x, y))
		return TRUE;
	return FALSE;
}

static void dview_draw_hsb(DView *dv)
{
	int vt;
	int ts;
	//int sz = SLIDER_SIZE;
	int sz;
	rectangle r;
	adjustment *sb = &dv->hsb;

	dview_get_rect(dv, RECT_DVIEW_HSB, r);

	dview_draw_box(dv, RECT_DVIEW_HSB, 0, 0, r.width(), r.height(), rgb_t(0xff, 0x60, 0x60, 0x60));
	dview_draw_outlined_box(dv, RECT_DVIEW_HSB, 0, 0, VSB_WIDTH,HSB_HEIGHT, rgb_t(0xff, 0xc0, 0xc0, 0xc0));
	dview_draw_outlined_box(dv, RECT_DVIEW_HSB, r.width() - VSB_WIDTH, 0, VSB_WIDTH, HSB_HEIGHT, rgb_t(0xff, 0xc0, 0xc0, 0xc0));

	// draw arrows
	dview_draw_line(dv, RECT_DVIEW_HSB, (VSB_WIDTH/3)*2, HSB_HEIGHT/4, VSB_WIDTH/3, HSB_HEIGHT/2, rgb_t(0xff, 0x00, 0x00, 0x00));
	dview_draw_line(dv, RECT_DVIEW_HSB, VSB_WIDTH/3, HSB_HEIGHT/2, (VSB_WIDTH/3)*2, (HSB_HEIGHT/4)*3, rgb_t(0xff, 0x00, 0x00, 0x00));
	dview_draw_line(dv, RECT_DVIEW_HSB, r.width() - (VSB_WIDTH/3)*2, HSB_HEIGHT/4, r.width() - VSB_WIDTH/3, HSB_HEIGHT/2, rgb_t(0xff, 0x00, 0x00, 0x00));
	dview_draw_line(dv, RECT_DVIEW_HSB, r.width() - VSB_WIDTH/3, HSB_HEIGHT/2, r.width() - (VSB_WIDTH/3)*2, (HSB_HEIGHT/4)*3, rgb_t(0xff, 0x00, 0x00, 0x00));

	ts = (r.width()) - 2 * VSB_WIDTH;

	sz = (ts * (sb->page_size)) / (sb->upper - sb->lower);
	ts = ts - sz;

	vt = (ts * (sb->value - sb->lower)) / (sb->upper - sb->lower - sb->page_size) + sz / 2 + r.min_x + VSB_WIDTH;

	dview_draw_outlined_box(dv, RECT_DVIEW_HSB, vt - sz / 2, 0, sz, HSB_HEIGHT, rgb_t(0xff, 0xc0, 0xc0, 0xc0));
}

static void dview_draw_vsb(DView *dv)
{
	INT64 vt;
	INT64 ts;
	//int sz = SLIDER_SIZE;
	INT64 sz;
	rectangle r;
	adjustment *sb = &dv->vsb;

	dview_get_rect(dv, RECT_DVIEW_VSB, r);

	dview_draw_box(dv, RECT_DVIEW_VSB, 0, 0, r.width(), r.height(), rgb_t(0xff, 0x60, 0x60, 0x60));
	dview_draw_outlined_box(dv, RECT_DVIEW_VSB, 0, r.height() - HSB_HEIGHT, VSB_WIDTH, HSB_HEIGHT, rgb_t(0xff, 0xc0, 0xc0, 0xc0));
	dview_draw_outlined_box(dv, RECT_DVIEW_VSB, 0, 0,                       VSB_WIDTH, HSB_HEIGHT, rgb_t(0xff, 0xc0, 0xc0, 0xc0));

	// draw arrows
	dview_draw_line(dv, RECT_DVIEW_VSB, VSB_WIDTH/4, (HSB_HEIGHT/3)*2, VSB_WIDTH/2, HSB_HEIGHT/3, rgb_t(0xff, 0x00, 0x00, 0x00));
	dview_draw_line(dv, RECT_DVIEW_VSB, VSB_WIDTH/2, HSB_HEIGHT/3, (VSB_WIDTH/4)*3, (HSB_HEIGHT/3)*2, rgb_t(0xff, 0x00, 0x00, 0x00));
	dview_draw_line(dv, RECT_DVIEW_VSB, VSB_WIDTH/4, r.height() - (HSB_HEIGHT/3)*2, VSB_WIDTH/2, r.height() - HSB_HEIGHT/3, rgb_t(0xff, 0x00, 0x00, 0x00));
	dview_draw_line(dv, RECT_DVIEW_VSB, VSB_WIDTH/2, r.height() - HSB_HEIGHT/3, (VSB_WIDTH/4)*3, r.height() - (HSB_HEIGHT/3)*2, rgb_t(0xff, 0x00, 0x00, 0x00));

	ts = r.height() - 2 * HSB_HEIGHT;

	sz = (ts * (sb->page_size)) / (sb->upper - sb->lower);
	ts = ts - sz;

	vt = (ts * (sb->value - sb->lower)) / (sb->upper - sb->lower - sb->page_size) + sz / 2 + HSB_HEIGHT;

	dview_draw_outlined_box(dv, RECT_DVIEW_VSB, 0, vt - sz / 2, VSB_WIDTH, sz, rgb_t(0xff, 0xc0, 0xc0, 0xc0));
}

static void dview_draw_size(DView *dv)
{
	rectangle r;

	dview_get_rect(dv, RECT_DVIEW_SIZE, r);

	dview_draw_outlined_box(dv, RECT_DVIEW_SIZE, 0, 0,
			r.width(),r.height(), rgb_t(0xff, 0x80, 0x80, 0x80));
	dview_draw_line(dv, RECT_DVIEW_SIZE, 11, r.height()-1, r.width()-1, 11, rgb_t(0xff, 0xc0, 0xc0, 0xc0));
	dview_draw_line(dv, RECT_DVIEW_SIZE, 12, r.height()-1, r.width()-1, 12, rgb_t(0xff, 0x60, 0x60, 0x60));
	dview_draw_line(dv, RECT_DVIEW_SIZE, 13, r.height()-1, r.width()-1, 13, rgb_t(0xff, 0xc0, 0xc0, 0xc0));
	dview_draw_line(dv, RECT_DVIEW_SIZE, 14, r.height()-1, r.width()-1, 14, rgb_t(0xff, 0x60, 0x60, 0x60));
	dview_draw_line(dv, RECT_DVIEW_SIZE, 15, r.height()-1, r.width()-1, 15, rgb_t(0xff, 0xc0, 0xc0, 0xc0));
	dview_draw_line(dv, RECT_DVIEW_SIZE, 16, r.height()-1, r.width()-1, 16, rgb_t(0xff, 0x60, 0x60, 0x60));
	dview_draw_line(dv, RECT_DVIEW_SIZE, 17, r.height()-1, r.width()-1, 17, rgb_t(0xff, 0xc0, 0xc0, 0xc0));
	dview_draw_line(dv, RECT_DVIEW_SIZE, 18, r.height()-1, r.width()-1, 18, rgb_t(0xff, 0x60, 0x60, 0x60));
}

static void dview_set_title(DView *dv, std::string title)
{
	if (dv->title.compare(title) != 0)
	{
		dv->title = title;
		dview_set_state(dv, VIEW_STATE_NEEDS_UPDATE, TRUE);
	}
}

static void dview_draw_title(DView *dv)
{
	int i;
	rgb_t col = rgb_t(0xff,0x00,0x00,0xff);
	rectangle r;
	int str_x = 2;

	dview_get_rect(dv, RECT_DVIEW_TITLE, r);

	if (dv == focus_view)
		col = rgb_t(0xff,0x00,0x7f,0x00);

	dview_draw_outlined_box(dv, RECT_DVIEW_TITLE, 0, 0, dv->bounds.width(), TITLE_HEIGHT, col);

	if (dv->title.empty())
		return;

	for (i = 0; i<strlen(dv->title.c_str()); i++)
	{
		if(str_x < r.width() - debug_font_width)
		{
			dview_draw_char(dv, RECT_DVIEW_TITLE, str_x,
				BORDER_YTHICKNESS, debug_font_height, //r.max_y - 2 * BORDER_YTHICKNESS,
				rgb_t(0xff,0xff,0xff,0xff), (UINT16) dv->title[i] );
		}
		str_x += debug_font->char_width(debug_font_height, debug_font_aspect,(UINT16) dv->title[i]) + 2*BORDER_XTHICKNESS;
	}
}

static int dview_on_mouse(DView *dv, int mx, int my, bool button)
{
	int clicked = (button && !dview_is_state(dv, VIEW_STATE_BUTTON));
	int handled = TRUE;
	int x,y;

	if (button && dview_is_state_all(dv, VIEW_STATE_BUTTON | VIEW_STATE_MOVING))
	{
		int dx = mx - dv->last_x;
		int dy = my - dv->last_y;

		dv->ofs_x += dx;
		dv->ofs_y += dy;
		dv->last_x = mx;
		dv->last_y = my;
		dview_set_state(dv, VIEW_STATE_NEEDS_UPDATE, TRUE);
		return TRUE;
	}
	else if (button && dview_is_state_all(dv, VIEW_STATE_BUTTON | VIEW_STATE_SIZING))
	{
		int dx = mx - dv->last_x;
		int dy = my - dv->last_y;

		dv->bounds.max_x += dx;
		dv->bounds.max_y += dy;
		dv->last_x = mx;
		dv->last_y = my;
		dview_set_state(dv, VIEW_STATE_NEEDS_UPDATE, TRUE);
		return TRUE;
	}
	else
		dview_set_state(dv, VIEW_STATE_MOVING | VIEW_STATE_SIZING, FALSE);

	if (!map_point(dv, mx, my, &x, &y))
		return FALSE;

	if (dview_xy_in_rect(dv, RECT_DVIEW_TITLE, x, y))
	{
		/* on title, do nothing */
		if (clicked) {
			dv->last_x = mx;
			dv->last_y = my;
			set_focus_view(dv);
			dview_set_state(dv, VIEW_STATE_MOVING, TRUE);
		}
	}
	else if (dview_xy_in_rect(dv, RECT_DVIEW_HSB, x, y))
	{
		/* on horizontal scrollbar */
		debug_view_xy pos;
		adjustment *sb = &dv->hsb;

		if (clicked)
		{
			rectangle r;
			int xt;

			dview_get_rect(dv, RECT_DVIEW_HSB, r);
			x -= r.min_x;

			xt = (x - VSB_WIDTH) * (sb->upper - sb->lower) / (r.width() - 2 * dv->vsb.visible * VSB_WIDTH) + sb->lower;
			if (x < VSB_WIDTH)
				sb->value -= sb->step_increment;
			else if (x > r.width() - VSB_WIDTH)
				sb->value += sb->step_increment;
			else if (xt < sb->value)
				sb->value -= sb->page_increment;
			else if (xt > sb->value)
				sb->value += sb->page_increment;

			if (sb->value < sb->lower)
				sb->value = sb->lower;
			if (sb->value > sb->upper)
				sb->value = sb->upper;
		}

		pos = dv->view->visible_position();

		if (sb->value != pos.x)
		{
			pos.x = sb->value;
			dv->view->set_visible_position(pos);
			dview_set_state(dv, VIEW_STATE_NEEDS_UPDATE, TRUE);
		}
	}
	else if (dview_xy_in_rect(dv, RECT_DVIEW_VSB, x, y) )
	{
		/* on vertical scrollbar */
		debug_view_xy pos;
		adjustment *sb = &dv->vsb;

		if (clicked)
		{
			rectangle r;
			int yt;

			dview_get_rect(dv, RECT_DVIEW_VSB, r);
			y -= r.min_y;
			yt = (y - HSB_HEIGHT) * (sb->upper - sb->lower) / (r.height() - 2 * HSB_HEIGHT) + sb->lower;

			if (y < HSB_HEIGHT)
				sb->value -= sb->step_increment;
			else if (y > r.height() - HSB_HEIGHT)
				sb->value += sb->step_increment;
			else if (yt < sb->value)
				sb->value -= sb->page_increment;
			else if (yt > sb->value)
				sb->value += sb->page_increment;

			if (sb->value < sb->lower)
				sb->value = sb->lower;
			if (sb->value > sb->upper)
				sb->value = sb->upper;
		}

		pos = dv->view->visible_position();

		if (sb->value != pos.y)
		{
			pos.y = sb->value;
			dv->view->set_visible_position(pos);
			dview_set_state(dv, VIEW_STATE_NEEDS_UPDATE, TRUE);
		}
	}
	else if (dview_xy_in_rect(dv, RECT_DVIEW_SIZE, x, y))
	{
		/* on sizing area */
		if (clicked)
		{
			dv->last_x = mx;
			dv->last_y = my;
			set_focus_view(dv);
			dview_set_state(dv, VIEW_STATE_SIZING, TRUE);
		}
	}
	else if (dview_xy_in_rect(dv, RECT_DVIEW_CLIENT, x, y))
	{
		y -= TITLE_HEIGHT;
		if (dv->view->cursor_supported() && clicked && y >= 0)
		{
			debug_view_xy topleft = dv->view->visible_position();
			debug_view_xy newpos;
			newpos.x = topleft.x + x / debug_font_width;
			newpos.y = topleft.y + y / debug_font_height;
			dv->view->set_cursor_position(newpos);
			dv->view->set_cursor_visible(true);
		}
		if (clicked)
			set_focus_view(dv);
	}
	else
	{
		handled = FALSE;
	}
	dview_set_state(dv, VIEW_STATE_BUTTON, button);
	return handled;

}

static inline void map_attr_to_fg_bg(unsigned char attr, rgb_t *fg, rgb_t *bg)
{
	*bg = rgb_t(0xff,0xff,0xff,0xff);
	*fg = rgb_t(0xff,0x00,0x00,0x00);

	if(attr & DCA_ANCILLARY)
		*bg = rgb_t(0xff,0xe0,0xe0,0xe0);
	if(attr & DCA_SELECTED) {
		*bg = rgb_t(0xff,0xff,0x80,0x80);
	}
	if(attr & DCA_CURRENT) {
		*bg = rgb_t(0xff,0xff,0xff,0x00);
	}
	if(attr & DCA_CHANGED) {
		*fg = rgb_t(0xff,0xff,0x00,0x00);
	}
	if(attr & DCA_INVALID) {
		*fg = rgb_t(0xff,0x00,0x00,0xff);
	}
	if(attr & DCA_DISABLED) {
		*fg = rgb_t(fg->a(), (fg->r() + bg->r()) >> 1, (fg->g() + bg->g()) >> 1, (fg->b() + bg->b()) >> 1);
	}
	if(attr & DCA_COMMENT) {
		*fg = rgb_t(0xff,0x00,0x80,0x00);
	}
}

static void dview_draw(DView *dv)
{
	const debug_view_char *viewdata;
	debug_view_xy vsize;
	UINT32 i, j, xx, yy;
	rgb_t bg_base, bg, fg;
	rectangle r;

	vsize = dv->view->visible_size();

	bg_base = rgb_t(0xff,0xff,0xff,0xff);

	/* always start clean */
	dview_clear(dv);

	dview_draw_title(dv);

	dview_get_rect(dv, RECT_DVIEW_CLIENT, r);

	dview_draw_outlined_box(dv, RECT_DVIEW_CLIENT, 0, 0,
			r.width() /*- (dv->vs ? VSB_WIDTH : 0)*/,
			r.height() /*- (dv->hsb.visible ? HSB_HEIGHT : 0)*/, bg_base);

	/* draw background and text */
	viewdata = dv->view->viewdata();
	yy = BORDER_YTHICKNESS;
	for(j=0; j<vsize.y; j++)
	{
		xx = BORDER_XTHICKNESS;
		for(i=0; i<vsize.x; i++)
		{
			UINT16 s = ' ';
			unsigned char v = viewdata->byte;

			if(v < 128) {
				s = v;
			} else {
				s = 0xc0 | (v>>6);
				s |= (0x80 | (v & 0x3f));
			}
			map_attr_to_fg_bg(viewdata->attrib, &fg, &bg);
			if (bg != bg_base)
				dview_draw_box(dv, RECT_DVIEW_CLIENT, xx, yy,
						debug_font_width, debug_font_height, bg);
			if (v != ' ')
				dview_draw_char(dv, RECT_DVIEW_CLIENT, xx, yy, debug_font_height, fg, s);
			xx += debug_font_width;
			viewdata++;
		}
		yy += debug_font_height;
	}

	if(dv->hsb.visible)
		dview_draw_hsb(dv);
	if(dv->vsb.visible)
		dview_draw_vsb(dv);
	dview_draw_size(dv);
}


static void dview_size_allocate(DView *dv)
{
	debug_view_xy size, pos, col, vsize;
	render_container::user_settings rcus;
	rectangle r;
	std::string title;

	dv->container->get_user_settings(rcus);
	rcus.m_xoffset = (float) dv->ofs_x / (float) dv->rt_width;
	rcus.m_yoffset = (float) dv->ofs_y / (float) dv->rt_height;
	rcus.m_xscale = 1.0; //(float) dv->bounds.width() / (float) dv->rt_width;
	rcus.m_yscale = 1.0; //(float) dv->bounds.height() / (float) dv->rt_height;
	dv->container->set_user_settings(rcus);
	//printf("%d %d %d %d\n", wpos.min_x, wpos.max_x, wpos.min_y, wpos.max_y);

	pos = dv->view->visible_position();
	size = dv->view->total_size();

	dv->hsb.visible = 0;
	dv->vsb.visible = 0;
	dview_get_rect(dv, RECT_DVIEW_CLIENT, r);

	dv->hsb.visible = (size.x * debug_font_width > r.width() ? 1 : 0);
	dv->vsb.visible = ((INT64)size.y * (INT64)debug_font_height > r.height() ? 1 : 0);
	dview_get_rect(dv, RECT_DVIEW_CLIENT, r);

//  dv->hsb.visible = (size.x * debug_font_width > r.width() ? 1 : 0);
//  dv->vsb.visible = (size.y * debug_font_height > r.height() ? 1 : 0);
//  dview_get_rect(dv, RECT_DVIEW_CLIENT, r);

	col.y = (r.height() - 2 * BORDER_YTHICKNESS /*+ debug_font_height  - 1*/) / debug_font_height;
	col.x = (r.width() - 2 * BORDER_XTHICKNESS /*+ debug_font_width - 1*/) / debug_font_width;

	vsize.y = size.y - pos.y;
	vsize.x = size.x - pos.x;
	if(vsize.y > col.y)
		vsize.y = col.y;
	else if(vsize.y < col.y) {
		pos.y = size.y-col.y;
		if(pos.y < 0)
			pos.y = 0;
		vsize.y = size.y-pos.y;
	}
	if(vsize.x > col.x)
		vsize.x = col.x;
	else if(vsize.x < col.x) {
		pos.x = size.x-col.x;
		if(pos.x < 0)
			pos.x = 0;
		vsize.x = size.x-pos.x;
	}

	dv->view->set_visible_position(pos);
	dv->view->set_visible_size(vsize);

	if(dv->hsb.visible) {
		int span = (r.width() - 2 * BORDER_XTHICKNESS) / debug_font_width;

		if(pos.x + span > size.x)
			pos.x = size.x - span;
		if(pos.x < 0)
			pos.x = 0;
		dv->hsb.lower = 0;
		dv->hsb.upper = size.x;
		dv->hsb.value = pos.x;
		dv->hsb.step_increment = 1;
		dv->hsb.page_increment = span;
		dv->hsb.page_size = span;

		dv->view->set_visible_position(pos);
	}

	if(dv->vsb.visible) {
		int span = (r.height() - 2 * BORDER_YTHICKNESS) / debug_font_height;

		if(pos.y + span > size.y)
			pos.y = size.y - span;
		if(pos.y < 0)
			pos.y = 0;
		dv->vsb.lower = 0;
		dv->vsb.upper = size.y;
		dv->vsb.value = pos.y;
		dv->vsb.step_increment = 1;
		dv->vsb.page_increment = span;
		dv->vsb.page_size = span;

		dv->view->set_visible_position(pos);
	}
}

static void dview_update(debug_view &dw, void *osdprivate)
{
	DView *dv = (DView *) osdprivate;

	dview_set_state(dv, VIEW_STATE_NEEDS_UPDATE, TRUE);

#if 0
	debug_view_xy size = dw.total_size();

	if((dv->tr != size.y) || (dv->tc != size.x))
		gtk_widget_queue_resize(GTK_WIDGET(dv));
	else
		gtk_widget_queue_draw(GTK_WIDGET(dv));
#endif
}

void debug_internal::exit()
{
	for (DView *ndv = list; ndv != nullptr; )
	{
		DView *temp = ndv;
		ndv = ndv->next;
		dview_free(temp);
	}
	if (debug_font != nullptr)
	{
		m_machine->render().font_free(debug_font);
		debug_font = nullptr;
	}
	if (menu)
		global_free(menu);
}

void debug_internal::init_debugger(running_machine &machine)
{
	unicode_char ch;
	int chw;

	m_machine = &machine;
	font_name = (downcast<osd_options &>(m_machine->options()).debugger_font());
	font_size = (downcast<osd_options &>(m_machine->options()).debugger_font_size());

	if(!strcmp(font_name, OSDOPTVAL_AUTO))
		debug_font = m_machine->render().font_alloc("Courier New");
	else
		debug_font = m_machine->render().font_alloc(font_name);

	debug_font_width = 0;
	if(font_size < 8)
		debug_font_height = 16;  // default
	else
		debug_font_height = font_size;
	menu = nullptr;
	cur_editor = nullptr;
	list = nullptr;
	focus_view = nullptr;

	debug_font_aspect = m_machine->render().ui_aspect();

	for (ch=0;ch<=127;ch++)
	{
		chw = debug_font->char_width(debug_font_height, debug_font_aspect, ch);
		if (chw>debug_font_width)
			debug_font_width = chw;
	}
	debug_font_width += 2*BORDER_XTHICKNESS;
}

#if 0
static void set_view_by_name(render_target *target, const char *name)
{
	int i = 0;
	const char *s;

	for (i = 0; ; i++ )
	{
		s = target->view_name(i);
		if (s == nullptr)
			return;
		//printf("%d %s\n", i, s);
		if (strcmp(name, s) == 0)
		{
			target->set_view(i);
			//printf("%d\n", target->view() );
			return;
		}
	}
}
#endif

/*-------------------------------------------------
    Menu Callbacks
  -------------------------------------------------*/

static void process_string(DView *dv, const char *str)
{
	switch (dv->type)
	{
	case DVT_DISASSEMBLY:
		downcast<debug_view_disasm *>(dv->view)->set_expression(str);
		break;
	case DVT_CONSOLE:
		if(!dv->editor.str[(int)0])
			debug_cpu_get_visible_cpu(dv->machine())->debug()->single_step();
		else
			debug_console_execute_command(dv->machine(), str, 1);
		break;
	case DVT_MEMORY:
		downcast<debug_view_memory *>(dv->view)->set_expression(str);
		break;
	}
}

static void debug_hide_all()
{
	for (DView *dv = list; dv != nullptr; dv = dv->next)
	{
		dv->container->empty();
		dview_set_state(dv,VIEW_STATE_VISIBLE,false);
	}
}

static void debug_show_all()
{
	for (DView *dv = list; dv != nullptr; dv = dv->next)
		dview_set_state(dv,VIEW_STATE_VISIBLE,true);
}

static void on_memory_window_activate(DView *dv, const ui_menu_event *event)
{
	DView *ndv;
	render_target *target;
	const debug_view_source *source;

	target = &dv->machine().render().ui_target();

	ndv = dview_alloc(target, dv->machine(), DVT_MEMORY, 0);
	ndv->editor.active = TRUE;
	ndv->editor.container = &dv->machine().render().ui_container();
	source = ndv->view->source();
	dview_set_title(ndv, source->name());
	ndv->ofs_x = ndv->ofs_y = win_count * TITLE_HEIGHT;
	ndv->bounds.setx(0,500);
	win_count++;
	dview_set_state(ndv,VIEW_STATE_VISIBLE,true);
	set_focus_view(ndv);
}

static void on_disassembly_window_activate(DView *dv, const ui_menu_event *event)
{
	DView *ndv;
	render_target *target;
	const debug_view_source *source;

	target = &dv->machine().render().ui_target();

	ndv = dview_alloc(target, dv->machine(), DVT_DISASSEMBLY, 0);
	ndv->editor.active = TRUE;
	ndv->editor.container = &dv->machine().render().ui_container();
	source = ndv->view->source();
	dview_set_title(ndv, source->name());
	ndv->ofs_x = ndv->ofs_y = win_count * TITLE_HEIGHT;
	win_count++;
	dview_set_state(ndv,VIEW_STATE_VISIBLE,true);
	set_focus_view(ndv);
}

static void on_disasm_cpu_activate(DView *dv, const ui_menu_event *event)
{
	const debug_view_source *current = dv->view->source();

	if (event->iptkey == IPT_UI_RIGHT)
	{
		current = current->next();
		if (current == nullptr)
			current = dv->view->first_source();
		dv->view->set_source(*current);
		dview_set_state(dv, VIEW_STATE_NEEDS_UPDATE, TRUE);
		dview_set_title(dv, current->name());
	}
}

static void on_log_window_activate(DView *dv, const ui_menu_event *event)
{
	DView *ndv;
	render_target *target;

	target = &dv->machine().render().ui_target();
	ndv = dview_alloc(target, dv->machine(), DVT_LOG, 0);
	dview_set_title(ndv, "Log");
	ndv->ofs_x = ndv->ofs_y = win_count * TITLE_HEIGHT;
	ndv->bounds.setx(0,600);
	win_count++;
	dview_set_state(ndv,VIEW_STATE_VISIBLE,true);
	set_focus_view(ndv);
}

static void on_bp_window_activate(DView *dv, const ui_menu_event *event)
{
	DView *ndv;
	render_target *target;

	target = &dv->machine().render().ui_target();
	ndv = dview_alloc(target, dv->machine(), DVT_BREAK_POINTS, 0);
	dview_set_title(ndv, "Breakpoints");
	ndv->ofs_x = ndv->ofs_y = win_count * TITLE_HEIGHT;
	ndv->bounds.setx(0,600);
	win_count++;
	dview_set_state(ndv,VIEW_STATE_VISIBLE,true);
	set_focus_view(ndv);
}

static void on_wp_window_activate(DView *dv, const ui_menu_event *event)
{
	DView *ndv;
	render_target *target;

	target = &dv->machine().render().ui_target();
	ndv = dview_alloc(target, dv->machine(), DVT_WATCH_POINTS, 0);
	dview_set_title(ndv, "Watchpoints");
	ndv->ofs_x = ndv->ofs_y = win_count * TITLE_HEIGHT;
	ndv->bounds.setx(0,600);
	win_count++;
	dview_set_state(ndv,VIEW_STATE_VISIBLE,true);
	set_focus_view(ndv);
}

static void on_close_activate(DView *dv, const ui_menu_event *event)
{
	if (focus_view == dv)
		set_focus_view(dv->next);
	win_count--;
	dview_free(dv);
}

static void on_run_activate(DView *dv, const ui_menu_event *event)
{
	debug_cpu_get_visible_cpu(dv->machine())->debug()->go();
}

static void on_run_h_activate(DView *dv, const ui_menu_event *event)
{
	debug_hide_all();
	debug_cpu_get_visible_cpu(dv->machine())->debug()->go();
}

static void on_run_cpu_activate(DView *dv, const ui_menu_event *event)
{
	debug_cpu_get_visible_cpu(dv->machine())->debug()->go_next_device();
}

static void on_run_irq_activate(DView *dv, const ui_menu_event *event)
{
	debug_cpu_get_visible_cpu(dv->machine())->debug()->go_interrupt();
}

static void on_run_vbl_activate(DView *dv, const ui_menu_event *event)
{
	debug_cpu_get_visible_cpu(dv->machine())->debug()->go_vblank();
}

static void on_step_into_activate(DView *dv, const ui_menu_event *event)
{
	debug_cpu_get_visible_cpu(dv->machine())->debug()->single_step();
}

static void on_step_over_activate(DView *dv, const ui_menu_event *event)
{
	debug_cpu_get_visible_cpu(dv->machine())->debug()->single_step_over();
}

#ifdef UNUSED_CODE
static void on_step_out_activate(DView *dv, const ui_menu_event *event)
{
	debug_cpu_get_visible_cpu(dv->machine())->debug()->single_step_out();
}
#endif

static void on_hard_reset_activate(DView *dv, const ui_menu_event *event)
{
	dv->machine().schedule_hard_reset();
}

static void on_soft_reset_activate(DView *dv, const ui_menu_event *event)
{
	dv->machine().schedule_soft_reset();
	debug_cpu_get_visible_cpu(dv->machine())->debug()->go();
}

static void on_exit_activate(DView *dv, const ui_menu_event *event)
{
	dv->machine().schedule_exit();
}

static void on_view_opcodes_activate(DView *dv, const ui_menu_event *event)
{
	debug_view_disasm *dasmview = downcast<debug_view_disasm *>(focus_view->view);
	disasm_right_column rc = dasmview->right_column();
	disasm_right_column new_rc = DASM_RIGHTCOL_NONE;

	if (event->iptkey == IPT_UI_RIGHT)
	{
		switch (rc)
		{
		case DASM_RIGHTCOL_RAW:         new_rc = DASM_RIGHTCOL_ENCRYPTED; break;
		case DASM_RIGHTCOL_ENCRYPTED:   new_rc = DASM_RIGHTCOL_COMMENTS; break;
		case DASM_RIGHTCOL_COMMENTS:    new_rc = DASM_RIGHTCOL_RAW; break;
		default:                        break;
		}
		dasmview->set_right_column(new_rc);
		dview_set_state(dv, VIEW_STATE_NEEDS_UPDATE, TRUE);
	}
}

static void on_run_to_cursor_activate(DView *dv, const ui_menu_event *event)
{
	char command[64];

	if (dv->view->cursor_visible() && debug_cpu_get_visible_cpu(dv->machine()) == dv->view->source()->device())
	{
		offs_t address = downcast<debug_view_disasm *>(dv->view)->selected_address();
		sprintf(command, "go %X", address);
		debug_console_execute_command(dv->machine(), command, 1);
	}
}

static void on_memory_address_type(DView *dv, const ui_menu_event *event)
{
	debug_view_memory *memview = downcast<debug_view_memory *>(focus_view->view);
	bool phys = memview->physical();

	if (event->iptkey == IPT_UI_RIGHT)
	{
		memview->set_physical(!phys);
		dview_set_state(dv, VIEW_STATE_NEEDS_UPDATE, TRUE);
	}
}

static void on_memory_data_format(DView *dv, const ui_menu_event *event)
{
	debug_view_memory *memview = downcast<debug_view_memory *>(focus_view->view);
	int format = memview->get_data_format();
	int idx = 0;
	int order[7] = { 1, 2, 4, 8, 9, 10, 11 };

	for(int x=0; x<7; x++)
	{
		if(order[x] == format)
			idx = x;
	}

	if (event->iptkey == IPT_UI_RIGHT)
	{
		idx++;
		if(idx >= 7)
			idx = 0;
		memview->set_data_format(order[idx]);
		dview_set_state(dv, VIEW_STATE_NEEDS_UPDATE, TRUE);
	}
	if (event->iptkey == IPT_UI_LEFT)
	{
		idx--;
		if(idx < 0)
			idx = 6;
		memview->set_data_format(order[idx]);
		dview_set_state(dv, VIEW_STATE_NEEDS_UPDATE, TRUE);
	}
}

static void on_memory_region(DView *dv, const ui_menu_event *event)
{
	debug_view_memory *memview = downcast<debug_view_memory *>(focus_view->view);
	const debug_view_source *source = memview->source();

	if (event->iptkey == IPT_UI_LEFT)
	{
		int idx = memview->source_list().indexof(*source);
		if(idx > 0)
			memview->set_source(*memview->source_list().find(idx-1));
		else
			memview->set_source(*memview->source_list().find(memview->source_list().count()-1));
		dview_set_state(dv, VIEW_STATE_NEEDS_UPDATE, TRUE);
		dview_set_title(dv, memview->source()->name());
	}
	if (event->iptkey == IPT_UI_RIGHT)
	{
		if(source->next() != nullptr)
			memview->set_source(*source->next());
		else
			memview->set_source(*memview->first_source());
		dview_set_state(dv, VIEW_STATE_NEEDS_UPDATE, TRUE);
		dview_set_title(dv, memview->source()->name());
	}
}

/*-------------------------------------------------
    editor
  -------------------------------------------------*/

static void render_editor(DView_edit *editor)
{
	DView* dv = editor->owner;
	rectangle r;
	const char* str = editor->str.c_str();
	int str_x = 2 * BORDER_XTHICKNESS;
	int start;
//  int editor_width;

	dview_get_rect(dv,RECT_DVIEW_HSB,r);

//  editor_width = debug_font->string_width(debug_font_height, debug_font_aspect, editor->str.c_str());
	// figure out which character to start drawing, so that you can always see the end of the string you're typing
	start = strlen(str) - (r.width() / (debug_font_width + 2*BORDER_XTHICKNESS));
	if(start < 0)
		start = 0;

	dview_draw_box(dv,RECT_DVIEW_HSB,0,0,r.width(),r.height(),rgb_t(0xff,0xff,0xff,0xff));
	dview_draw_line(dv,RECT_DVIEW_HSB,0,0,r.width(),0,rgb_t(0xff,0xc0,0xc0,0xc0));
	dview_draw_line(dv,RECT_DVIEW_HSB,r.width(),0,r.width(),r.height(),rgb_t(0xff,0x60,0x60,0x60));
	dview_draw_line(dv,RECT_DVIEW_HSB,r.width(),r.height(),0,r.height(),rgb_t(0xff,0x60,0x60,0x60));
	dview_draw_line(dv,RECT_DVIEW_HSB,0,r.height(),0,0,rgb_t(0xff,0xc0,0xc0,0xc0));

	for(int x=start;x<strlen(str);x++)
	{
		if(str_x < r.width() - debug_font_width)
			dview_draw_char(dv,RECT_DVIEW_HSB,str_x,BORDER_YTHICKNESS,r.height(),rgb_t(0xff,0x00,0x00,0x00),(UINT16)str[x]);
		str_x += debug_font->char_width(r.height(),debug_font_aspect,(UINT16)str[x]) + 2*BORDER_XTHICKNESS;
	}
}

/*-------------------------------------------------
    menu_main_populate - populate the main menu
  -------------------------------------------------*/

class ui_menu_debug : public ui_menu {
public:
	ui_menu_debug(running_machine &machine, render_container *container) : ui_menu(machine, container) {}
	virtual ~ui_menu_debug() {}
	virtual void populate() override {}
	virtual void handle() override {}
};

static void CreateMainMenu(running_machine &machine)
{
	const char *subtext = "";
	int rc;
	std::string title;

	if (menu)
	{
		menu_sel = menu->get_selection();
		global_free( menu);
	}
	menu = global_alloc_clear<ui_menu_debug>(machine, &machine.render().ui_container());

	switch (focus_view->type)
	{
	case DVT_DISASSEMBLY:
		title = "Disassembly:";
		break;
	case DVT_CONSOLE:
		title = "Console:";
		break;
	case DVT_LOG:
		title = "Log:";
		break;
	case DVT_MEMORY:
		title = "Memory:";
		break;
	case DVT_STATE:
		title = "State:";
		break;
	case DVT_BREAK_POINTS:
		title = "Breakpoints:";
		break;
	case DVT_WATCH_POINTS:
		title = "Watchpoints:";
		break;
	}

	menu->item_append(title.append(focus_view->title).c_str(), nullptr, MENU_FLAG_DISABLE, nullptr);
	menu->item_append(MENU_SEPARATOR_ITEM, nullptr, 0, nullptr);

	switch (focus_view->type)
	{
	case DVT_DISASSEMBLY:
	{
		rc = downcast<debug_view_disasm *>(focus_view->view)->right_column();
		switch(rc)
		{
		case DASM_RIGHTCOL_RAW:         subtext = "Raw Opcodes"; break;
		case DASM_RIGHTCOL_ENCRYPTED:   subtext = "Enc Opcodes"; break;
		case DASM_RIGHTCOL_COMMENTS:        subtext = "Comments"; break;
		}
		menu->item_append("View", subtext, MENU_FLAG_RIGHT_ARROW, (void *)on_view_opcodes_activate);
		menu->item_append("Run to cursor", nullptr, 0, (void *)on_run_to_cursor_activate);

		if (!dview_is_state(focus_view, VIEW_STATE_FOLLOW_CPU))
		{
			menu->item_append("CPU", focus_view->view->source()->name(), MENU_FLAG_RIGHT_ARROW, (void *)on_disasm_cpu_activate);
		}
		menu->item_append(MENU_SEPARATOR_ITEM, nullptr, 0, nullptr);
		break;
	}
	case DVT_MEMORY:
	{
		bool phys = downcast<debug_view_memory *>(focus_view->view)->physical();
		int format = downcast<debug_view_memory *>(focus_view->view)->get_data_format();
		const debug_view_source* source = downcast<debug_view_memory *>(focus_view->view)->source();
		menu->item_append("Region", source->name(), MENU_FLAG_LEFT_ARROW | MENU_FLAG_RIGHT_ARROW, (void *)on_memory_region);
		if(phys)
			menu->item_append("Address type", "Physical", MENU_FLAG_RIGHT_ARROW, (void *)on_memory_address_type);
		else
			menu->item_append("Address type", "Logical", MENU_FLAG_RIGHT_ARROW, (void *)on_memory_address_type);
		switch(format)
		{
		case 1: subtext = "1 byte chunks"; break;
		case 2: subtext = "2 byte chunks"; break;
		case 4: subtext = "4 byte chunks"; break;
		case 8: subtext = "8 byte chunks"; break;
		case 9: subtext = "32-bit floating point"; break;
		case 10: subtext = "64-bit floating point"; break;
		case 11: subtext = "80-bit floating point"; break;
		}
		menu->item_append("Format", subtext, MENU_FLAG_LEFT_ARROW | MENU_FLAG_RIGHT_ARROW, (void *)on_memory_data_format);
		menu->item_append(MENU_SEPARATOR_ITEM, nullptr, 0, nullptr);
		break;
	}
	}

	/* add input menu items */

	menu->item_append("New Memory Window", "[Ctrl+M]", 0, (void *)on_memory_window_activate);
	menu->item_append("New Disassembly Window", "[Ctrl+D]", 0, (void *)on_disassembly_window_activate);
	menu->item_append("New Error Log Window", "[Ctrl+L]", 0, (void *)on_log_window_activate);
	menu->item_append("New Breakpoints Window", "[Ctrl+B]", 0, (void *)on_bp_window_activate);
	menu->item_append("New Watchpoints Window", "[Ctrl+W]", 0, (void *)on_wp_window_activate);
	menu->item_append(MENU_SEPARATOR_ITEM, nullptr, 0, nullptr);
	menu->item_append("Run", "[F5]", 0, (void *)on_run_activate);
	menu->item_append("Run and Hide Debugger", "[F12]", 0, (void *)on_run_h_activate);
	menu->item_append("Run to Next CPU", "[F6]", 0, (void *)on_run_cpu_activate);
	menu->item_append("Run until Next Interrupt on This CPU", "[F7]", 0, (void *)on_run_irq_activate);
	menu->item_append("Run until Next VBLANK", "[F8]", 0, (void *)on_run_vbl_activate);
	menu->item_append(MENU_SEPARATOR_ITEM, nullptr, 0, nullptr);
	menu->item_append("Step Into", "[F11]", 0, (void *)on_step_into_activate);
	menu->item_append("Step Over", "[F10]", 0, (void *)on_step_over_activate);
	menu->item_append(MENU_SEPARATOR_ITEM, nullptr, 0, nullptr);
	menu->item_append("Soft Reset", "[F3]", 0, (void *)on_soft_reset_activate);
	menu->item_append("Hard Reset", "[Shift+F3]", 0, (void *)on_hard_reset_activate);
	menu->item_append(MENU_SEPARATOR_ITEM, nullptr, 0, nullptr);
	if (!dview_is_state(focus_view, VIEW_STATE_FOLLOW_CPU))
		menu->item_append("Close Window", "[Shift+F4]", 0, (void *)on_close_activate);
	menu->item_append("Exit", nullptr, 0, (void *)on_exit_activate);
	menu->set_selection(menu_sel);
}

static int map_point(DView *dv, INT32 target_x, INT32 target_y, INT32 *mapped_x, INT32 *mapped_y)
{
	rectangle pos;

	/* default to point not mapped */
	*mapped_x = -1;
	*mapped_y = -1;

	pos = dv->bounds;
	pos.min_x += dv->ofs_x;
	pos.max_x += dv->ofs_x;
	pos.min_y += dv->ofs_y;
	pos.max_y += dv->ofs_y;
	//render_target_get_component_container(target, name, &pos);

	if (target_x >= pos.min_x && target_x <= pos.max_x && target_y >= pos.min_y && target_y <= pos.max_y)
	{
		*mapped_x = target_x - pos.min_x;
		*mapped_y = target_y - pos.min_y;
		return TRUE;
	}
	return FALSE;
}

static void handle_mouse(running_machine &machine)
{
	render_target * mouse_target;
	INT32           x,y;
	bool            button;

	if (menu != nullptr)
		return;

	mouse_target = machine.ui_input().find_mouse(&x, &y, &button);

	if (mouse_target == nullptr)
		return;
	//printf("mouse %d %d %d\n", x, y, button);

	for (DView *dv = list; dv != nullptr; dv = dv->next)
	{
		if (mouse_target == dv->target)
		{
			if (dview_on_mouse(dv, x, y, button))
				break;
		}
	}
}

static void handle_keys(running_machine &machine)
{
	if (menu != nullptr)
		return;

	// global keys
	if(machine.input().code_pressed_once(KEYCODE_F3))
	{
		if(machine.input().code_pressed(KEYCODE_LSHIFT))
			machine.schedule_hard_reset();
		else
		{
			machine.schedule_soft_reset();
			debug_cpu_get_visible_cpu(machine)->debug()->go();
		}
	}
	
	if(machine.input().code_pressed_once(KEYCODE_F5))
		debug_cpu_get_visible_cpu(machine)->debug()->go();
	if(machine.input().code_pressed_once(KEYCODE_F6))
		debug_cpu_get_visible_cpu(machine)->debug()->go_next_device();
	if(machine.input().code_pressed_once(KEYCODE_F7))
		debug_cpu_get_visible_cpu(machine)->debug()->go_interrupt();
	if(machine.input().code_pressed_once(KEYCODE_F8))
		debug_cpu_get_visible_cpu(machine)->debug()->go_vblank();
	if(machine.input().code_pressed_once(KEYCODE_F10))
		debug_cpu_get_visible_cpu(machine)->debug()->single_step_over();
	if(machine.input().code_pressed_once(KEYCODE_F11))
		debug_cpu_get_visible_cpu(machine)->debug()->single_step();
	if(machine.input().code_pressed_once(KEYCODE_F12))
	{
		debug_hide_all();
		debug_cpu_get_visible_cpu(machine)->debug()->go();
	}

	// TODO: make common functions to be shared here and with the menu callbacks
	if(machine.input().code_pressed_once(KEYCODE_D))
	{
		if(machine.input().code_pressed(KEYCODE_LCONTROL))
		{
			DView *ndv;
			render_target *target;
			const debug_view_source *source;

			target = &machine.render().ui_target();

			ndv = dview_alloc(target, machine, DVT_DISASSEMBLY, 0);
			ndv->editor.active = TRUE;
			ndv->editor.container = &machine.render().ui_container();
			source = ndv->view->source();
			dview_set_title(ndv, source->name());
			ndv->ofs_x = ndv->ofs_y = win_count * TITLE_HEIGHT;
			win_count++;
			set_focus_view(ndv);
		}
	}
	if(machine.input().code_pressed_once(KEYCODE_M))
	{
		if(machine.input().code_pressed(KEYCODE_LCONTROL))
		{
			DView *ndv;
			render_target *target;
			const debug_view_source *source;

			target = &machine.render().ui_target();

			ndv = dview_alloc(target, machine, DVT_MEMORY, 0);
			ndv->editor.active = TRUE;
			ndv->editor.container = &machine.render().ui_container();
			source = ndv->view->source();
			dview_set_title(ndv, source->name());
			ndv->ofs_x = ndv->ofs_y = win_count * TITLE_HEIGHT;
			ndv->bounds.setx(0,500);
			win_count++;

			set_focus_view(ndv);
		}
	}	
	if(machine.input().code_pressed_once(KEYCODE_L))
	{
		if(machine.input().code_pressed(KEYCODE_LCONTROL))
		{
			DView *ndv;
			render_target *target;

			target = &machine.render().ui_target();
			ndv = dview_alloc(target, machine, DVT_LOG, 0);
			dview_set_title(ndv, "Log");
			ndv->ofs_x = ndv->ofs_y = win_count * TITLE_HEIGHT;
			ndv->bounds.setx(0,600);
			win_count++;
			set_focus_view(ndv);
		}
	}
	if(machine.input().code_pressed_once(KEYCODE_B))
	{
		if(machine.input().code_pressed(KEYCODE_LCONTROL))
		{
			DView *ndv;
			render_target *target;

			target = &machine.render().ui_target();
			ndv = dview_alloc(target, machine, DVT_BREAK_POINTS, 0);
			dview_set_title(ndv, "Breakpoints");
			ndv->ofs_x = ndv->ofs_y = win_count * TITLE_HEIGHT;
			ndv->bounds.setx(0,600);
			win_count++;
			set_focus_view(ndv);
		}
	}
	if(machine.input().code_pressed_once(KEYCODE_W))
	{
		if(machine.input().code_pressed(KEYCODE_LCONTROL))
		{
			DView *ndv;
			render_target *target;

			target = &machine.render().ui_target();
			ndv = dview_alloc(target, machine, DVT_WATCH_POINTS, 0);
			dview_set_title(ndv, "Watchpoints");
			ndv->ofs_x = ndv->ofs_y = win_count * TITLE_HEIGHT;
			ndv->bounds.setx(0,600);
			win_count++;
			set_focus_view(ndv);
		}
	}
	if (!dview_is_state(focus_view, VIEW_STATE_FOLLOW_CPU))
	{
		if(machine.input().code_pressed_once(KEYCODE_F4))
		{
			if(machine.input().code_pressed(KEYCODE_LSHIFT))  // use shift+F4, as ctrl+F4 is used to toggle keepaspect.
			{
				DView* dv = focus_view;
				set_focus_view(focus_view->next);
				win_count--;
				dview_free(dv);
			}
		}
	}

	
	// pass keypresses to debug view with focus
	if(machine.input().code_pressed_once(KEYCODE_UP))
		focus_view->view->process_char(DCH_UP);
	if(machine.input().code_pressed_once(KEYCODE_DOWN))
		focus_view->view->process_char(DCH_DOWN);
	if(machine.input().code_pressed_once(KEYCODE_LEFT))
	{
		if(machine.input().code_pressed(KEYCODE_LCONTROL))
			focus_view->view->process_char(DCH_CTRLLEFT);
		else
			focus_view->view->process_char(DCH_LEFT);
	}
	if(machine.input().code_pressed_once(KEYCODE_RIGHT))
	{
		if(machine.input().code_pressed(KEYCODE_LCONTROL))
			focus_view->view->process_char(DCH_CTRLRIGHT);
		else
			focus_view->view->process_char(DCH_RIGHT);
	}
	if(machine.input().code_pressed_once(KEYCODE_PGUP))
		focus_view->view->process_char(DCH_PUP);
	if(machine.input().code_pressed_once(KEYCODE_PGDN))
		focus_view->view->process_char(DCH_PDOWN);
	if(machine.input().code_pressed_once(KEYCODE_HOME))
	{
		if(machine.input().code_pressed(KEYCODE_LCONTROL))
			focus_view->view->process_char(DCH_CTRLHOME);
		else
			focus_view->view->process_char(DCH_HOME);
	}
	if(machine.input().code_pressed_once(KEYCODE_END))
	{
		if(machine.input().code_pressed(KEYCODE_LCONTROL))
			focus_view->view->process_char(DCH_CTRLEND);
		else
			focus_view->view->process_char(DCH_END);
	}
}


/*-------------------------------------------------
    handle_editor - handle the editor
-------------------------------------------------*/

static void handle_editor(running_machine &machine)
{
	if (focus_view->editor.active && dview_is_state(focus_view, VIEW_STATE_VISIBLE))
	{
		ui_event event;

		/* loop while we have interesting events */
		while (machine.ui_input().pop_event(&event))
		{
			switch (event.event_type)
			{
			case UI_EVENT_CHAR:
				/* if it's a backspace and we can handle it, do so */
				if ((event.ch == 8 || event.ch == 0x7f) && focus_view->editor.str.length() > 0)
				{
					/* autoschow */
					cur_editor = &focus_view->editor;
					cur_editor->str = cur_editor->str.substr(0, cur_editor->str.length()-1);
				}
				/* if it's any other key and we're not maxed out, update */
				else if (event.ch >= ' ' && event.ch < 0x7f)
				{
					char buf[10];
					int ret;
					/* autoschow */
					cur_editor = &focus_view->editor;
					ret = utf8_from_uchar(buf, 10, event.ch);
					buf[ret] = 0;
					cur_editor->str = cur_editor->str.append(buf);
				}
				break;
			default:
				break;
			}
		}
		if (cur_editor != nullptr)
		{
			render_editor(cur_editor);
			if (machine.ui_input().pressed(IPT_UI_SELECT))
			{
				process_string(focus_view, focus_view->editor.str.c_str());
				focus_view->editor.str = "";
				cur_editor = nullptr;
			}
			if (machine.ui_input().pressed(IPT_UI_CANCEL))
				cur_editor = nullptr;
		}
	}

}


/*-------------------------------------------------
    menu_main - handle the main menu
-------------------------------------------------*/

static void handle_menus(running_machine &machine)
{
	const ui_menu_event *event;

	machine.render().ui_container().empty();
	machine.ui_input().frame_update();
	if (menu != nullptr)
	{
		/* process the menu */
		event = menu->process(0);
		if (event != nullptr && (event->iptkey == IPT_UI_SELECT || (event->iptkey == IPT_UI_LEFT) || (event->iptkey == IPT_UI_RIGHT)))
		{
			//global_free(menu);
			//menu = nullptr;
			((void (*)(DView *, const ui_menu_event *)) event->itemref)(focus_view, event);
			//ui_menu_stack_push(ui_menu_alloc(machine, menu->container, (ui_menu_handler_func)event->itemref,nullptr));
			CreateMainMenu(machine);
		}
		else if (machine.ui_input().pressed(IPT_UI_CONFIGURE))
		{
			menu_sel = menu->get_selection();
			global_free(menu);
			menu = nullptr;
		}
	}
	else
	{
		/* turn on menus if requested */
		if (machine.ui_input().pressed(IPT_UI_CONFIGURE))
			CreateMainMenu(machine);
		/* turn on editor if requested */
		//if (ui_input_pressed(machine, IPT_UI_UP) && focus_view->editor.active)
		//  cur_editor = &focus_view->editor;
		handle_editor(machine);
	}
}

//============================================================
//  followers_set_cpu
//============================================================

static void followers_set_cpu(device_t *device)
{
	std::string title;

	for (DView *dv = list; dv != nullptr; dv = dv->next)
	{
		if (dview_is_state(dv, VIEW_STATE_FOLLOW_CPU))
		{
			const debug_view_source *source = dv->view->source_for_device(device);
			switch (dv->type)
			{
			case DVT_DISASSEMBLY:
			case DVT_STATE:
				dv->view->set_source(*source);
				strprintf(title, "%s", source->name());
				dview_set_title(dv, title);
				break;
			}
		}
	}
	// and recompute the children
	//console_recompute_children(main_console);
}


static void dview_update_view(DView *dv)
{
	INT32 old_rt_width = dv->rt_width;
	INT32 old_rt_height = dv->rt_height;

	dv->rt_width = dv->target->width();
	dv->rt_height = dv->target->height();
	if (dview_is_state(dv, VIEW_STATE_NEEDS_UPDATE) || dv->rt_width != old_rt_width || dv->rt_height != old_rt_height)
	{
		dview_size_allocate(dv);
		if(dview_is_state(dv, VIEW_STATE_VISIBLE))
			dview_draw(dv);
		dview_set_state(dv, VIEW_STATE_NEEDS_UPDATE, FALSE);
	}
}


static void update_views(void)
{
	DView *dv;

	for(dv=list;dv!=nullptr;dv=dv->next)
		dview_update_view(dv);
}


void debug_internal::wait_for_debugger(device_t &device, bool firststop)
{
	if (firststop && list == nullptr)
	{
		render_target *target = &device.machine().render().ui_target();

		//set_view_by_name(target, "Debug");
		win_count = 0;

		DView *disassembly = dview_alloc(target, device.machine(), DVT_DISASSEMBLY, VIEW_STATE_FOLLOW_CPU);
		disassembly->editor.active = TRUE;
		disassembly->editor.container = &device.machine().render().ui_container();
		disassembly->ofs_x = 500;
		disassembly->bounds.setx(0,600);
		win_count++;

		DView *statewin = dview_alloc(target, device.machine(), DVT_STATE, VIEW_STATE_FOLLOW_CPU);
		statewin->ofs_x = 300;
		statewin->bounds.set(0,200,0,600);
		win_count++;

		DView *console = dview_alloc(target, device.machine(), DVT_CONSOLE, VIEW_STATE_FOLLOW_CPU);
		dview_set_title(console, "Console");
		console->editor.active = TRUE;
		console->editor.container = &device.machine().render().ui_container();
		console->bounds.setx(0,600);
		console->ofs_x = 500;
		console->ofs_y = 300;
		win_count++;
		set_focus_view(console);
	}

	followers_set_cpu(&device);
	if(firststop)
	{
		debug_show_all();
		device.machine().ui_input().reset();
	}
	//ui_update_and_render(device.machine(), device.machine().render().ui_container()());
	update_views();
	device.machine().osd().update(false);
	handle_menus(device.machine());
	handle_mouse(device.machine());
	handle_keys(device.machine());
	//osd_sleep(osd_ticks_per_second()/60);

}

void debug_internal::debugger_update()
{
	if ((m_machine != nullptr) && (!debug_cpu_is_stopped(*m_machine)) && (m_machine->phase() == MACHINE_PHASE_RUNNING))
	{
		update_views();
	}
}

MODULE_DEFINITION(DEBUG_INTERNAL, debug_internal)