summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/debug/debugcpu.c
blob: cc011a0c1972cbbb965137bf94a040c83b2a732f (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
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
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
/*********************************************************************

    debugcpu.c

    Debugger CPU/memory interface engine.

    Copyright Nicola Salmoria and the MAME Team.
    Visit http://mamedev.org for licensing and usage restrictions.

**********************************************************************

    Future work:

    - enable history to be enabled/disabled to improve performance

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

#include "osdepend.h"
#include "driver.h"
#include "debugcpu.h"
#include "debugcmd.h"
#include "debugcmt.h"
#include "debugcon.h"
#include "express.h"
#include "debugvw.h"
#include "debugger.h"
#include "deprecat.h"
#include "uiinput.h"
#include "machine/eeprom.h"
#include <ctype.h>



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

#define NUM_TEMP_VARIABLES	10

enum
{
	EXECUTION_STATE_STOPPED,
	EXECUTION_STATE_RUNNING
};



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

typedef struct _debugger_private debugger_private;
struct _debugger_private
{
	debug_cpu_info	cpuinfo[MAX_CPU];
	debug_cpu_info *livecpu;
	debug_cpu_info *visiblecpu;

	UINT8			within_instruction_hook;
	UINT8			vblank_occurred;
	UINT8			memory_modified;

	int				execution_state;

	UINT32			bpindex;
	UINT32			wpindex;

	UINT64			wpdata;
	UINT64			wpaddr;
	UINT64 			tempvar[NUM_TEMP_VARIABLES];

	osd_ticks_t 	last_periodic_update_time;
};



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

FILE *debug_source_file;
symbol_table *global_symtable;

static debugger_private global;



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

static void debug_cpu_exit(running_machine *machine);
static void perform_trace(debug_cpu_info *info);
static void prepare_for_step_overout(debug_cpu_info *info);
static void process_source_file(void);
static void breakpoint_check(debug_cpu_info *info, offs_t pc);
static void watchpoint_check(int cpunum, int spacenum, int type, offs_t address, UINT64 value_to_write, UINT64 mem_mask);
static void check_hotspots(int cpunum, int spacenum, offs_t address);

/* expression handlers */
static UINT64 expression_read_memory(int space, int index, UINT32 address, int size);
static UINT64 expression_read_address_space(int cpuindex, int space, offs_t address, int size);
static UINT64 expression_read_program_direct(int cpuindex, int opcode, offs_t address, int size);
static UINT64 expression_read_memory_region(int rgnindex, int rgntype, offs_t address, int size);
static UINT64 expression_read_eeprom(offs_t address, int size);
static void expression_write_memory(int space, int index, UINT32 address, int size, UINT64 data);
static void expression_write_address_space(int cpuindex, int space, offs_t address, int size, UINT64 data);
static void expression_write_program_direct(int cpuindex, int opcode, offs_t address, int size, UINT64 data);
static void expression_write_memory_region(int rgnindex, int rgntype, offs_t address, int size, UINT64 data);
static void expression_write_eeprom(offs_t address, int size, UINT64 data);

/* variable getters/setters */
static UINT64 get_wpaddr(UINT32 ref);
static UINT64 get_wpdata(UINT32 ref);
static UINT64 get_cycles(UINT32 ref);
static UINT64 get_cpunum(UINT32 ref);
static UINT64 get_tempvar(UINT32 ref);
static UINT64 get_logunmap(UINT32 ref);
static UINT64 get_beamx(UINT32 ref);
static UINT64 get_beamy(UINT32 ref);
static void set_tempvar(UINT32 ref, UINT64 value);
static void set_logunmap(UINT32 ref, UINT64 value);
static UINT64 get_current_pc(UINT32 ref);
static UINT64 get_cpu_reg(UINT32 ref);
static void set_cpu_reg(UINT32 ref, UINT64 value);


/***************************************************************************
    GLOBAL CONSTANTS
***************************************************************************/

const express_callbacks debug_expression_callbacks =
{
	expression_read_memory,
	expression_write_memory
};



/***************************************************************************
    FRONTENDS FOR OLDER FUNCTIONS
***************************************************************************/

/*-------------------------------------------------
    debug_cpu_within_instruction_hook - true if
    the debugger is currently live
-------------------------------------------------*/

int debug_cpu_within_instruction_hook(running_machine *machine)
{
	return global.within_instruction_hook;
}


/*-------------------------------------------------
    on_vblank - called when a VBLANK hits
-------------------------------------------------*/

static void on_vblank(const device_config *device, int vblank_state)
{
	/* just set a global flag to be consumed later */
	if (vblank_state)
		global.vblank_occurred = TRUE;
}


/***************************************************************************
    INITIALIZATION
***************************************************************************/

/*-------------------------------------------------
    debug_cpu_init - initialize the CPU
    information for debugging
-------------------------------------------------*/

void debug_cpu_init(running_machine *machine)
{
	int cpunum, spacenum, regnum;

	/* reset globals */
	global.execution_state = EXECUTION_STATE_STOPPED;
	global.bpindex = 1;
	global.wpindex = 1;
	global.within_instruction_hook = FALSE;
	global.visiblecpu = NULL;

	/* create a global symbol table */
	global_symtable = symtable_alloc(NULL);

	/* add "wpaddr", "wpdata", "cycles", "cpunum", "logunmap" to the global symbol table */
	symtable_add_register(global_symtable, "wpaddr", 0, get_wpaddr, NULL);
	symtable_add_register(global_symtable, "wpdata", 0, get_wpdata, NULL);
	symtable_add_register(global_symtable, "cycles", 0, get_cycles, NULL);
	symtable_add_register(global_symtable, "cpunum", 0, get_cpunum, NULL);
	symtable_add_register(global_symtable, "logunmap", ADDRESS_SPACE_PROGRAM, get_logunmap, set_logunmap);
	symtable_add_register(global_symtable, "logunmapd", ADDRESS_SPACE_DATA, get_logunmap, set_logunmap);
	symtable_add_register(global_symtable, "logunmapi", ADDRESS_SPACE_IO, get_logunmap, set_logunmap);
	symtable_add_register(global_symtable, "beamx", 0, get_beamx, NULL);
	symtable_add_register(global_symtable, "beamy", 0, get_beamy, NULL);

	/* add the temporary variables to the global symbol table */
	for (regnum = 0; regnum < NUM_TEMP_VARIABLES; regnum++)
	{
		char symname[10];
		sprintf(symname, "temp%d", regnum);
		symtable_add_register(global_symtable, symname, regnum, get_tempvar, set_tempvar);
	}

	/* loop over CPUs and build up their info */
	for (cpunum = 0; cpunum < MAX_CPU; cpunum++)
	{
		cpu_type cputype = machine->config->cpu[cpunum].type;
		debug_cpu_info *info = &global.cpuinfo[cpunum];

		/* if this is a dummy, stop looking */
		memset(info, 0, sizeof(*info));
		if (cputype == CPU_DUMMY)
			break;

		/* reset the PC data */
		info->valid = TRUE;
		info->flags = DEBUG_FLAG_OBSERVING | DEBUG_FLAG_HISTORY;
		info->endianness = cpunum_endianness(cpunum);
		info->opwidth = cpunum_min_instruction_bytes(cpunum);

		/* fetch the memory accessors */
		info->translate = (cpu_translate_func)cpunum_get_info_fct(cpunum, CPUINFO_PTR_TRANSLATE);
		info->read = (cpu_read_func)cpunum_get_info_fct(cpunum, CPUINFO_PTR_READ);
		info->write = (cpu_write_func)cpunum_get_info_fct(cpunum, CPUINFO_PTR_WRITE);
		info->readop = (cpu_readop_func)cpunum_get_info_fct(cpunum, CPUINFO_PTR_READOP);

		/* allocate a symbol table */
		info->symtable = symtable_alloc(global_symtable);

		/* add a global symbol for the current instruction pointer */
		symtable_add_register(info->symtable, "curpc", 0, get_current_pc, 0);

		/* add all registers into it */
		for (regnum = 0; regnum < MAX_REGS; regnum++)
		{
			const char *str = cpunum_reg_string(cpunum, regnum);
			const char *colon;
			char symname[256];
			int charnum;

			/* skip if we don't get a valid string, or one without a colon */
			if (str == NULL)
				continue;
			if (str[0] == '~')
				str++;
			colon = strchr(str, ':');
			if (colon == NULL)
				continue;

			/* strip all spaces from the name and convert to lowercase */
			for (charnum = 0; charnum < sizeof(symname) - 1 && str < colon; str++)
				if (!isspace(*str))
					symname[charnum++] = tolower(*str);
			symname[charnum] = 0;

			/* add the symbol to the table */
			symtable_add_register(info->symtable, symname, regnum, get_cpu_reg, set_cpu_reg);
		}

		/* loop over address spaces and get info */
		for (spacenum = 0; spacenum < ADDRESS_SPACES; spacenum++)
		{
			debug_space_info *spaceinfo = &info->space[spacenum];
			int datawidth = cpunum_databus_width(cpunum, spacenum);
			int logwidth = cpunum_logaddr_width(cpunum, spacenum);
			int physwidth = cpunum_addrbus_width(cpunum, spacenum);
			int addrshift = cpunum_addrbus_shift(cpunum, spacenum);
			int pageshift = cpunum_page_shift(cpunum, spacenum);

			if (logwidth == 0)
				logwidth = physwidth;

			spaceinfo->databytes = datawidth / 8;
			spaceinfo->pageshift = pageshift;

			/* left/right shifts to convert addresses to bytes */
			spaceinfo->addr2byte_lshift = (addrshift < 0) ? -addrshift : 0;
			spaceinfo->addr2byte_rshift = (addrshift > 0) ?  addrshift : 0;

			/* number of character used to display addresses */
			spaceinfo->physchars = (physwidth + 3) / 4;
			spaceinfo->logchars = (logwidth + 3) / 4;

			/* masks to apply to addresses */
			spaceinfo->physaddrmask = (0xfffffffful >> (32 - physwidth));
			spaceinfo->logaddrmask = (0xfffffffful >> (32 - logwidth));

			/* masks to apply to byte addresses */
			spaceinfo->physbytemask = ((spaceinfo->physaddrmask << spaceinfo->addr2byte_lshift) | ((1 << spaceinfo->addr2byte_lshift) - 1)) >> spaceinfo->addr2byte_rshift;
			spaceinfo->logbytemask = ((spaceinfo->logaddrmask << spaceinfo->addr2byte_lshift) | ((1 << spaceinfo->addr2byte_lshift) - 1)) >> spaceinfo->addr2byte_rshift;
		}
	}

	/* add callback for breaking on VBLANK */
	if (machine->primary_screen != NULL)
		video_screen_register_vblank_callback(machine->primary_screen, on_vblank);

	add_exit_callback(machine, debug_cpu_exit);
}


/*-------------------------------------------------
    debug_cpu_exit - free all memory
-------------------------------------------------*/

static void debug_cpu_exit(running_machine *machine)
{
	int cpunum, spacenum;

	/* loop over all watchpoints and breakpoints to free their memory */
	for (cpunum = 0; cpunum < MAX_CPU; cpunum++)
	{
		debug_cpu_info *info = &global.cpuinfo[cpunum];

		/* close any tracefiles */
		if (info->trace.file)
			fclose(info->trace.file);
		if (info->trace.action)
			free(info->trace.action);

		/* free the symbol table */
		if (info->symtable)
			symtable_free(info->symtable);

		/* free all breakpoints */
		while (info->bplist)
			debug_cpu_breakpoint_clear(info->bplist->index);

		/* loop over all address spaces */
		for (spacenum = 0; spacenum < ADDRESS_SPACES; spacenum++)
		{
			/* free all watchpoints */
			while (info->space[spacenum].wplist)
				debug_cpu_watchpoint_clear(info->space[spacenum].wplist->index);
		}
	}

	/* free the global symbol table */
	if (global_symtable)
		symtable_free(global_symtable);
}



/***************************************************************************
    MAIN CPU CALLBACK
***************************************************************************/

/*-------------------------------------------------
    compute_debug_flags - compute the global
    debug flags for optimal efficiency
-------------------------------------------------*/

static void compute_debug_flags(running_machine *machine, const debug_cpu_info *info)
{
	/* clear out all global flags by default */
	machine->debug_flags = DEBUG_FLAG_ENABLED;

	/* if we are ignoring this CPU, or if events are pending, we're done */
	if ((info->flags & DEBUG_FLAG_OBSERVING) == 0 || mame_is_scheduled_event_pending(machine) || mame_is_save_or_load_pending(machine))
		return;

	/* many of our states require us to be called on each instruction */
	if (global.execution_state == EXECUTION_STATE_STOPPED)
		machine->debug_flags |= DEBUG_FLAG_CALL_HOOK;
	if ((info->flags & (DEBUG_FLAG_HISTORY | DEBUG_FLAG_TRACING_ANY | DEBUG_FLAG_HOOKED |
						DEBUG_FLAG_STEPPING_ANY | DEBUG_FLAG_STOP_PC | DEBUG_FLAG_LIVE_BP)) != 0)
		machine->debug_flags |= DEBUG_FLAG_CALL_HOOK;

	/* if we are stopping at a particular time and that time is within the current timeslice, we need to be called */
	if ((info->flags & DEBUG_FLAG_STOP_TIME) && attotime_compare(info->endexectime, info->stoptime) <= 0)
		machine->debug_flags |= DEBUG_FLAG_CALL_HOOK;

	/* add in the watchpoint flags */
	machine->debug_flags |= (info->flags & DEBUG_FLAG_WATCHPOINT) >> (24 - 4);

	/* if any of the watchpoint flags are set and we're live, tell the memory system */
	if (global.livecpu != NULL && ((info->flags & DEBUG_FLAG_WATCHPOINT) != 0))
		memory_set_context(-1);
}


/*-------------------------------------------------
    reset_transient_flags - reset the transient
    flags on all CPUs
-------------------------------------------------*/

static void reset_transient_flags(running_machine *machine)
{
	int cpunum;

	/* loop over CPUs and reset the transient flags */
	for (cpunum = 0; cpunum < ARRAY_LENGTH(global.cpuinfo); cpunum++)
		global.cpuinfo[cpunum].flags &= ~DEBUG_FLAG_TRANSIENT;
}


/*-------------------------------------------------
    debug_cpu_start_hook - the CPU execution
    system calls this hook before beginning
    execution for the given CPU
-------------------------------------------------*/

void debug_cpu_start_hook(running_machine *machine, int cpunum, attotime endtime)
{
	debug_cpu_info *info = &global.cpuinfo[cpunum];

	assert((machine->debug_flags & DEBUG_FLAG_ENABLED) != 0);

	/* stash a pointer to the current live CPU */
	assert(global.livecpu == NULL);
	global.livecpu = info;

	/* update the target execution end time */
	info->endexectime = endtime;

	/* if a VBLANK occurred, check on things */
	if (global.vblank_occurred && global.execution_state != EXECUTION_STATE_STOPPED)
	{
		global.vblank_occurred = FALSE;

		/* if we were waiting for a VBLANK, signal it now */
		if ((info->flags & DEBUG_FLAG_STOP_VBLANK) != 0)
		{
			global.execution_state = EXECUTION_STATE_STOPPED;
			debug_console_printf("Stopped at VBLANK\n");
		}

		/* check for debug keypresses */
		else if (ui_input_pressed(machine, IPT_UI_DEBUG_BREAK))
		{
			global.execution_state = EXECUTION_STATE_STOPPED;
			debug_console_printf("User-initiated break\n");
		}

		/* while we're here, check for a periodic update */
		else if (info == global.visiblecpu && osd_ticks() > global.last_periodic_update_time + osd_ticks_per_second()/4)
		{
			debug_view_update_all();
			global.last_periodic_update_time = osd_ticks();
		}
	}

	/* recompute the debugging mode */
	compute_debug_flags(machine, info);
}


/*-------------------------------------------------
    debug_cpu_stop_hook - the CPU execution
    system calls this hook when ending execution
    for the given CPU
-------------------------------------------------*/

void debug_cpu_stop_hook(running_machine *machine, int cpunum)
{
	debug_cpu_info *info = &global.cpuinfo[cpunum];

	assert(global.livecpu == info);

	/* if we're stopping on a context switch, handle it now */
	if (info->flags & DEBUG_FLAG_STOP_CONTEXT)
	{
		global.execution_state = EXECUTION_STATE_STOPPED;
		reset_transient_flags(machine);
	}

	/* clear the live CPU */
	global.livecpu = NULL;
}


/*-------------------------------------------------
    debug_cpu_interrupt_hook - called when an
    interrupt is acknowledged
-------------------------------------------------*/

void debug_cpu_interrupt_hook(running_machine *machine, int cpunum, int irqline)
{
	debug_cpu_info *info = &global.cpuinfo[cpunum];

	/* see if this matches a pending interrupt request */
	if ((info->flags & DEBUG_FLAG_STOP_INTERRUPT) != 0 && (info->stopirq == -1 || info->stopirq == irqline))
	{
		global.execution_state = EXECUTION_STATE_STOPPED;
		debug_console_printf("Stopped on interrupt (CPU %d, IRQ %d)\n", cpunum, irqline);
		compute_debug_flags(machine, info);
	}
}


/*-------------------------------------------------
    debug_cpu_exception_hook - called when an
    exception is generated
-------------------------------------------------*/

void debug_cpu_exception_hook(running_machine *machine, int cpunum, int exception)
{
	debug_cpu_info *info = &global.cpuinfo[cpunum];

	/* see if this matches a pending interrupt request */
	if ((info->flags & DEBUG_FLAG_STOP_EXCEPTION) != 0 && (info->stopexception == -1 || info->stopexception == exception))
	{
		global.execution_state = EXECUTION_STATE_STOPPED;
		debug_console_printf("Stopped on exception (CPU %d, exception %d)\n", cpunum, exception);
		compute_debug_flags(machine, info);
	}
}


/*-------------------------------------------------
    debug_cpu_instruction_hook - called by the
    CPU cores before executing each instruction
-------------------------------------------------*/

void debug_cpu_instruction_hook(running_machine *machine, offs_t curpc)
{
	debug_cpu_info *info = global.livecpu;

	/* note that we are in the debugger code */
	global.within_instruction_hook = TRUE;

	/* update the history */
	info->pc_history[info->pc_history_index++ % DEBUG_HISTORY_SIZE] = curpc;

	/* are we tracing? */
	if (info->flags & DEBUG_FLAG_TRACING_ANY)
		perform_trace(info);

	/* per-instruction hook? */
	if (global.execution_state != EXECUTION_STATE_STOPPED && (info->flags & DEBUG_FLAG_HOOKED) != 0 && (*info->instrhook)(curpc))
		global.execution_state = EXECUTION_STATE_STOPPED;

	/* handle single stepping */
	if (global.execution_state != EXECUTION_STATE_STOPPED && (info->flags & DEBUG_FLAG_STEPPING_ANY) != 0)
	{
		/* is this an actual step? */
		if (info->stepaddr == ~0 || curpc == info->stepaddr)
		{
			/* decrement the count and reset the breakpoint */
			info->stepsleft--;
			info->stepaddr = ~0;

			/* if we hit 0, stop */
			if (info->stepsleft == 0)
				global.execution_state = EXECUTION_STATE_STOPPED;

			/* update every 100 steps until we are within 200 of the end */
			else if ((info->flags & DEBUG_FLAG_STEPPING_OUT) == 0 && (info->stepsleft < 200 || info->stepsleft % 100 == 0))
			{
				debug_view_update_all();
				debugger_refresh_display(machine);
			}
		}
	}

	/* handle breakpoints */
	if (global.execution_state != EXECUTION_STATE_STOPPED && (info->flags & (DEBUG_FLAG_STOP_TIME | DEBUG_FLAG_STOP_PC | DEBUG_FLAG_LIVE_BP)) != 0)
	{
		/* see if we hit a target time */
		if ((info->flags & DEBUG_FLAG_STOP_TIME) != 0 && attotime_compare(timer_get_time(), info->stoptime) >= 0)
		{
			debug_console_printf("Stopped at time interval %.1g\n", attotime_to_double(timer_get_time()));
			global.execution_state = EXECUTION_STATE_STOPPED;
		}

		/* check the temp running breakpoint and break if we hit it */
		else if ((info->flags & DEBUG_FLAG_STOP_PC) != 0 && info->stopaddr == curpc)
		{
			debug_console_printf("Stopped at temporary breakpoint %X on CPU %d\n", info->stopaddr, (int)(info - global.cpuinfo));
			global.execution_state = EXECUTION_STATE_STOPPED;
		}

		/* check for execution breakpoints */
		else if ((info->flags & DEBUG_FLAG_LIVE_BP) != 0)
			breakpoint_check(info, curpc);
	}

	/* if we are supposed to halt, do it now */
	if (global.execution_state == EXECUTION_STATE_STOPPED)
	{
		int firststop = TRUE;
		
		/* reset any transient state */
		reset_transient_flags(machine);

		/* update all views */
		debug_view_update_all();
		debugger_refresh_display(machine);

		/* wait for the debugger; during this time, disable sound output */
		sound_mute(TRUE);
		while (global.execution_state == EXECUTION_STATE_STOPPED)
		{
			/* clear the memory modified flag and wait */
			global.memory_modified = FALSE;
			osd_wait_for_debugger(machine, firststop);
			firststop = FALSE;

			/* if something modified memory, update the screen */
			if (global.memory_modified)
			{
				debug_disasm_update_all();
				debugger_refresh_display(machine);
			}

			/* check for commands in the source file */
			process_source_file();

			/* if an event got scheduled, resume */
			if (mame_is_scheduled_event_pending(machine))
				global.execution_state = EXECUTION_STATE_RUNNING;
		}
		sound_mute(FALSE);

		/* remember the last visible CPU in the debugger */
		global.visiblecpu = info;
	}

	/* handle step out/over on the instruction we are about to execute */
	if ((info->flags & (DEBUG_FLAG_STEPPING_OVER | DEBUG_FLAG_STEPPING_OUT)) != 0 && info->stepaddr == ~0)
		prepare_for_step_overout(info);

	/* no longer in debugger code */
	global.within_instruction_hook = FALSE;
}


/*-------------------------------------------------
    debug_cpu_memory_read_hook - the memory system
    calls this hook when watchpoints are enabled
    and a memory read happens
-------------------------------------------------*/

void debug_cpu_memory_read_hook(running_machine *machine, int cpunum, int spacenum, offs_t address, UINT64 mem_mask)
{
	debug_cpu_info *info = &global.cpuinfo[cpunum];

	/* check watchpoints */
	if ((info->flags & (DEBUG_FLAG_LIVE_WPR_PROGRAM << spacenum)) != 0)
		watchpoint_check(cpunum, spacenum, WATCHPOINT_READ, address, 0, mem_mask);

	/* check hotspots */
	if (info->hotspots != NULL)
		check_hotspots(cpunum, spacenum, address);
}


/*-------------------------------------------------
    debug_cpu_memory_write_hook - the memory
    system calls this hook when watchpoints are
    enabled and a memory write happens
-------------------------------------------------*/

void debug_cpu_memory_write_hook(running_machine *machine, int cpunum, int spacenum, offs_t address, UINT64 data, UINT64 mem_mask)
{
	debug_cpu_info *info = &global.cpuinfo[cpunum];

	/* check watchpoints */
	if ((info->flags & (DEBUG_FLAG_LIVE_WPW_PROGRAM << spacenum)) != 0)
		watchpoint_check(cpunum, spacenum, WATCHPOINT_WRITE, address, data, mem_mask);
}



/***************************************************************************
    EXECUTION CONTROL
***************************************************************************/

/*-------------------------------------------------
    debug_cpu_single_step - single step past the
    requested number of instructions
-------------------------------------------------*/

void debug_cpu_single_step(int numsteps)
{
	debug_cpu_info *info = global.livecpu;

	if (!global.within_instruction_hook)
		return;
	assert(info != NULL);

	info->stepsleft = numsteps;
	info->stepaddr = ~0;
	info->flags |= DEBUG_FLAG_STEPPING;
	global.execution_state = EXECUTION_STATE_RUNNING;
}


/*-------------------------------------------------
    debug_cpu_single_step_over - single step over
    a single instruction
-------------------------------------------------*/

void debug_cpu_single_step_over(int numsteps)
{
	debug_cpu_info *info = global.livecpu;

	if (!global.within_instruction_hook)
		return;
	assert(info != NULL);

	info->stepsleft = numsteps;
	info->stepaddr = ~0;
	info->flags |= DEBUG_FLAG_STEPPING_OVER;
	global.execution_state = EXECUTION_STATE_RUNNING;
}


/*-------------------------------------------------
    debug_cpu_single_step_out - single step out of
    the current function
-------------------------------------------------*/

void debug_cpu_single_step_out(void)
{
	debug_cpu_info *info = global.livecpu;

	if (!global.within_instruction_hook)
		return;
	assert(info != NULL);

	info->stepsleft = 100;
	info->stepaddr = ~0;
	info->flags |= DEBUG_FLAG_STEPPING_OUT;
	global.execution_state = EXECUTION_STATE_RUNNING;
}


/*-------------------------------------------------
    debug_cpu_go - resume execution
-------------------------------------------------*/

void debug_cpu_go(offs_t targetpc)
{
	debug_cpu_info *info = global.livecpu;

	if (!global.within_instruction_hook)
		return;
	assert(info != NULL);

	info->stopaddr = targetpc;
	info->flags |= DEBUG_FLAG_STOP_PC;
	global.execution_state = EXECUTION_STATE_RUNNING;
}


/*-------------------------------------------------
    debug_cpu_go_vblank - run until the next
    VBLANK
-------------------------------------------------*/

void debug_cpu_go_vblank(void)
{
	debug_cpu_info *info = global.livecpu;

	if (!global.within_instruction_hook)
		return;
	assert(info != NULL);

	global.vblank_occurred = FALSE;
	info->flags |= DEBUG_FLAG_STOP_VBLANK;
	global.execution_state = EXECUTION_STATE_RUNNING;
}


/*-------------------------------------------------
    debug_cpu_go_interrupt - run until the
    specified interrupt fires
-------------------------------------------------*/

void debug_cpu_go_interrupt(int irqline)
{
	debug_cpu_info *info = global.livecpu;

	if (!global.within_instruction_hook)
		return;
	assert(info != NULL);

	info->stopirq = irqline;
	info->flags |= DEBUG_FLAG_STOP_INTERRUPT;
	global.execution_state = EXECUTION_STATE_RUNNING;
}


/*-------------------------------------------------
    debug_cpu_go_exception - run until the
    specified exception fires
-------------------------------------------------*/

void debug_cpu_go_exception(int exception)
{
	debug_cpu_info *info = global.livecpu;

	if (!global.within_instruction_hook)
		return;
	assert(info != NULL);

	info->stopexception = exception;
	info->flags |= DEBUG_FLAG_STOP_EXCEPTION;
	global.execution_state = EXECUTION_STATE_RUNNING;
}


/*-------------------------------------------------
    debug_cpu_go_milliseconds - run until the
    specified delay elapses
-------------------------------------------------*/

void debug_cpu_go_milliseconds(UINT64 milliseconds)
{
	debug_cpu_info *info = global.livecpu;

	if (!global.within_instruction_hook)
		return;
	assert(info != NULL);

	info->stoptime = attotime_add(timer_get_time(), ATTOTIME_IN_MSEC(milliseconds));
	info->flags |= DEBUG_FLAG_STOP_TIME;
	global.execution_state = EXECUTION_STATE_RUNNING;
}


/*-------------------------------------------------
    debug_cpu_next_cpu - execute until we hit
    the next CPU
-------------------------------------------------*/

void debug_cpu_next_cpu(void)
{
	debug_cpu_info *info = global.livecpu;

	if (!global.within_instruction_hook)
		return;
	assert(info != NULL);

	info->flags |= DEBUG_FLAG_STOP_CONTEXT;
	global.execution_state = EXECUTION_STATE_RUNNING;
}


/*-------------------------------------------------
    debug_cpu_ignore_cpu - ignore/observe a given
    CPU
-------------------------------------------------*/

void debug_cpu_ignore_cpu(int cpunum, int ignore)
{
	debug_cpu_info *info = &global.cpuinfo[cpunum];

	if (!global.within_instruction_hook)
		return;

	if (ignore)
		info->flags &= ~DEBUG_FLAG_OBSERVING;
	else
		info->flags |= DEBUG_FLAG_OBSERVING;

	if (info == global.livecpu && ignore)
		debug_cpu_next_cpu();
}


/*-------------------------------------------------
    debug_cpu_trace - trace execution of a given
    CPU
-------------------------------------------------*/

void debug_cpu_trace(int cpunum, FILE *file, int trace_over, const char *action)
{
	debug_cpu_info *info = &global.cpuinfo[cpunum];

	/* close existing files and delete expressions */
	if (info->trace.file != NULL)
		fclose(info->trace.file);
	info->trace.file = NULL;

	if (info->trace.action != NULL)
		free(info->trace.action);
	info->trace.action = NULL;

	/* open any new files */
	info->trace.file = file;
	info->trace.action = NULL;
	info->trace.trace_over_target = ~0;
	if (action != NULL)
	{
		info->trace.action = malloc_or_die(strlen(action) + 1);
		if (info->trace.action != NULL)
			strcpy(info->trace.action, action);
	}

	/* update flags */
	if (info->trace.file != NULL)
		info->flags |= trace_over ? DEBUG_FLAG_TRACING_OVER : DEBUG_FLAG_TRACING;
	else
		info->flags &= ~DEBUG_FLAG_TRACING_ANY;
}



/***************************************************************************
    UTILITIES
***************************************************************************/

/*-------------------------------------------------
    debug_get_cpu_info - returns the cpu info
    block for a given CPU
-------------------------------------------------*/

const debug_cpu_info *debug_get_cpu_info(int cpunum)
{
	return &global.cpuinfo[cpunum];
}


/*-------------------------------------------------
    debug_cpu_halt_on_next_instruction - halt in
    the debugger on the next instruction
-------------------------------------------------*/

void debug_cpu_halt_on_next_instruction(running_machine *machine, const char *fmt, ...)
{
	va_list arg;

	va_start(arg, fmt);
	debug_console_vprintf(fmt, arg);
	va_end(arg);

	global.execution_state = EXECUTION_STATE_STOPPED;
	if (global.livecpu != NULL)
		compute_debug_flags(machine, global.livecpu);
}


/*-------------------------------------------------
    debug_cpu_is_stopped - return the
    current execution state
-------------------------------------------------*/

int debug_cpu_is_stopped(running_machine *machine)
{
	return global.execution_state == EXECUTION_STATE_STOPPED;
}


/*-------------------------------------------------
    perform_trace - log to the tracefile the
    data for a given instruction
-------------------------------------------------*/

static UINT32 dasm_wrapped(char *buffer, offs_t pc)
{
	const debug_cpu_info *cpuinfo = debug_get_cpu_info(cpu_getactivecpu());
	int maxbytes = activecpu_max_instruction_bytes();
	UINT8 opbuf[64], argbuf[64];
	offs_t pcbyte;
	int numbytes;

	/* fetch the bytes up to the maximum */
	pcbyte = ADDR2BYTE_MASKED(pc, cpuinfo, ADDRESS_SPACE_PROGRAM);
	for (numbytes = 0; numbytes < maxbytes; numbytes++)
	{
		opbuf[numbytes] = debug_read_opcode(pcbyte + numbytes, 1, FALSE);
		argbuf[numbytes] = debug_read_opcode(pcbyte + numbytes, 1, TRUE);
	}

	return activecpu_dasm(buffer, pc, opbuf, argbuf);
}


static void perform_trace(debug_cpu_info *info)
{
	offs_t pc = activecpu_get_pc();
	int offset, count, i;
	char buffer[100];
	offs_t dasmresult;

	/* are we in trace over mode and in a subroutine? */
	if ((info->flags & DEBUG_FLAG_TRACING_OVER) != 0 && info->trace.trace_over_target != ~0)
	{
		if (info->trace.trace_over_target != pc)
			return;
		info->trace.trace_over_target = ~0;
	}

	/* check for a loop condition */
	for (i = count = 0; i < TRACE_LOOPS; i++)
		if (info->trace.history[i] == pc)
			count++;

	/* if no more than 1 hit, process normally */
	if (count <= 1)
	{
		/* if we just finished looping, indicate as much */
		if (info->trace.loops != 0)
			fprintf(info->trace.file, "\n   (loops for %d instructions)\n\n", info->trace.loops);
		info->trace.loops = 0;

		/* execute any trace actions first */
		if (info->trace.action != NULL)
			debug_console_execute_command(info->trace.action, 0);

		/* print the address */
		offset = sprintf(buffer, "%0*X: ", info->space[ADDRESS_SPACE_PROGRAM].logchars, pc);

		/* print the disassembly */
		dasmresult = dasm_wrapped(&buffer[offset], pc);

		/* output the result */
		fprintf(info->trace.file, "%s\n", buffer);

		/* do we need to step the trace over this instruction? */
		if ((info->flags & DEBUG_FLAG_TRACING_OVER) != 0 && (dasmresult & DASMFLAG_SUPPORTED) != 0 && (dasmresult & DASMFLAG_STEP_OVER) != 0)
		{
			int extraskip = (dasmresult & DASMFLAG_OVERINSTMASK) >> DASMFLAG_OVERINSTSHIFT;
			offs_t trace_over_target = pc + (dasmresult & DASMFLAG_LENGTHMASK);

			/* if we need to skip additional instructions, advance as requested */
			while (extraskip-- > 0)
				trace_over_target += dasm_wrapped(buffer, trace_over_target) & DASMFLAG_LENGTHMASK;

			info->trace.trace_over_target = trace_over_target;
		}

		/* log this PC */
		info->trace.nextdex = (info->trace.nextdex + 1) % TRACE_LOOPS;
		info->trace.history[info->trace.nextdex] = pc;
	}

	/* else just count the loop */
	else
		info->trace.loops++;
}


/*-------------------------------------------------
    prepare_for_step_overout - prepare things for
    stepping over an instruction
-------------------------------------------------*/

static void prepare_for_step_overout(debug_cpu_info *info)
{
	offs_t pc = activecpu_get_pc();
	char dasmbuffer[100];
	offs_t dasmresult;

	/* disassemble the current instruction and get the flags */
	dasmresult = dasm_wrapped(dasmbuffer, pc);

	/* if flags are supported and it's a call-style opcode, set a temp breakpoint after that instruction */
	if ((dasmresult & DASMFLAG_SUPPORTED) != 0 && (dasmresult & DASMFLAG_STEP_OVER) != 0)
	{
		int extraskip = (dasmresult & DASMFLAG_OVERINSTMASK) >> DASMFLAG_OVERINSTSHIFT;
		pc += dasmresult & DASMFLAG_LENGTHMASK;

		/* if we need to skip additional instructions, advance as requested */
		while (extraskip-- > 0)
			pc += dasm_wrapped(dasmbuffer, pc) & DASMFLAG_LENGTHMASK;
		info->stepaddr = pc;
	}

	/* if we're stepping out and this isn't a step out instruction, reset the steps until stop to a high number */
	if ((info->flags & DEBUG_FLAG_STEPPING_OUT) != 0)
	{
		if ((dasmresult & DASMFLAG_SUPPORTED) != 0 && (dasmresult & DASMFLAG_STEP_OUT) == 0)
			info->stepsleft = 100;
		else
			info->stepsleft = 1;
	}
}


/*-------------------------------------------------
    process_source_file - executes commands from
    a source file
-------------------------------------------------*/

static void process_source_file(void)
{
	/* loop until the file is exhausted or until we are executing again */
	while (debug_source_file != NULL && global.execution_state == EXECUTION_STATE_STOPPED)
	{
		char buf[512];
		int i;
		char *s;

		/* stop at the end of file */
		if (feof(debug_source_file))
		{
			fclose(debug_source_file);
			debug_source_file = NULL;
			return;
		}

		/* fetch the next line */
		memset(buf, 0, sizeof(buf));
		fgets(buf, sizeof(buf), debug_source_file);

		/* strip out comments (text after '//') */
		s = strstr(buf, "//");
		if (s)
			*s = '\0';

		/* strip whitespace */
		i = (int)strlen(buf);
		while((i > 0) && (isspace(buf[i-1])))
			buf[--i] = '\0';

		/* execute the command */
		if (buf[0])
			debug_console_execute_command(buf, 1);
	}
}


/*-------------------------------------------------
    debug_cpu_set_instruction_hook - set a hook to
    be called on each instruction for a given CPU
-------------------------------------------------*/

void debug_cpu_set_instruction_hook(int cpunum, int (*hook)(offs_t pc))
{
	debug_cpu_info *info = &global.cpuinfo[cpunum];

	/* set the hook and also the CPU's flag for fast knowledge of the hook */
	info->instrhook = hook;
	if (hook != NULL)
		info->flags |= DEBUG_FLAG_HOOKED;
	else
		info->flags &= ~DEBUG_FLAG_HOOKED;
}



/***************************************************************************
    BREAKPOINTS
***************************************************************************/

/*-------------------------------------------------
    breakpoint_update_flags - update the CPU's
    breakpoint flags
-------------------------------------------------*/

static void breakpoint_update_flags(debug_cpu_info *info)
{
	debug_cpu_breakpoint *bp;

	/* see if there are any enabled breakpoints */
	info->flags &= ~DEBUG_FLAG_LIVE_BP;
	for (bp = info->bplist; bp != NULL; bp = bp->next)
		if (bp->enabled)
		{
			info->flags |= DEBUG_FLAG_LIVE_BP;
			break;
		}

	/* push the flags out globally */
	if (global.livecpu != NULL)
		compute_debug_flags(Machine, global.livecpu);
}


/*-------------------------------------------------
    breakpoint_check - check the breakpoints for
    a given CPU
-------------------------------------------------*/

static void breakpoint_check(debug_cpu_info *info, offs_t pc)
{
	debug_cpu_breakpoint *bp;
	UINT64 result;

	/* see if we match */
	for (bp = info->bplist; bp != NULL; bp = bp->next)
		if (bp->enabled && bp->address == pc)

			/* if we do, evaluate the condition */
			if (bp->condition == NULL || (expression_execute(bp->condition, &result) == EXPRERR_NONE && result != 0))
			{
				/* halt in the debugger by default */
				global.execution_state = EXECUTION_STATE_STOPPED;

				/* if we hit, evaluate the action */
				if (bp->action != NULL)
					debug_console_execute_command(bp->action, 0);

				/* print a notification, unless the action made us go again */
				if (global.execution_state == EXECUTION_STATE_STOPPED)
					debug_console_printf("Stopped at breakpoint %X\n", bp->index);
				break;
			}
}


/*-------------------------------------------------
    debug_cpu_breakpoint_set - set a new breakpoint
-------------------------------------------------*/

int debug_cpu_breakpoint_set(int cpunum, offs_t address, parsed_expression *condition, const char *action)
{
	debug_cpu_info *info = &global.cpuinfo[cpunum];
	debug_cpu_breakpoint *bp;

	assert_always(cpunum >= 0 && cpunum < cpu_gettotalcpu(), "debug_cpu_breakpoint_set() called with invalid cpunum!");

	/* allocate breakpoint */
	bp = malloc_or_die(sizeof(*bp));
	bp->index = global.bpindex++;
	bp->enabled = TRUE;
	bp->address = address;
	bp->condition = condition;
	bp->action = NULL;
	if (action != NULL)
	{
		bp->action = malloc_or_die(strlen(action) + 1);
		strcpy(bp->action, action);
	}

	/* hook us in */
	bp->next = info->bplist;
	info->bplist = bp;

	/* ensure the live breakpoint flag is set */
	breakpoint_update_flags(info);
	return bp->index;
}


/*-------------------------------------------------
    debug_cpu_breakpoint_clear - clear a breakpoint
-------------------------------------------------*/

int debug_cpu_breakpoint_clear(int bpnum)
{
	debug_cpu_breakpoint *bp, *pbp;
	int cpunum;

	/* loop over CPUs and find the requested breakpoint */
	for (cpunum = 0; cpunum < MAX_CPU; cpunum++)
	{
		debug_cpu_info *info = &global.cpuinfo[cpunum];
		for (pbp = NULL, bp = info->bplist; bp != NULL; pbp = bp, bp = bp->next)
			if (bp->index == bpnum)
			{
				/* unlink us from the list */
				if (pbp == NULL)
					info->bplist = bp->next;
				else
					pbp->next = bp->next;

				/* free the memory */
				if (bp->condition != NULL)
					expression_free(bp->condition);
				if (bp->action != NULL)
					free(bp->action);
				free(bp);

				/* update the flags */
				breakpoint_update_flags(info);
				return 1;
			}
	}

	/* we didn't find it; return an error */
	return 0;
}


/*-------------------------------------------------
    debug_cpu_breakpoint_enable - enable/disable a
    breakpoint
-------------------------------------------------*/

int debug_cpu_breakpoint_enable(int bpnum, int enable)
{
	debug_cpu_breakpoint *bp;
	int cpunum;

	/* loop over CPUs and find the requested breakpoint */
	for (cpunum = 0; cpunum < MAX_CPU; cpunum++)
	{
		debug_cpu_info *info = &global.cpuinfo[cpunum];
		for (bp = info->bplist; bp != NULL; bp = bp->next)
			if (bp->index == bpnum)
			{
				bp->enabled = (enable != 0);
				breakpoint_update_flags(info);
				return 1;
			}
	}

	return 0;
}



/***************************************************************************
    WATCHPOINTS
***************************************************************************/

/*-------------------------------------------------
    watchpoint_update_flags - update the CPU's
    watchpoint flags
-------------------------------------------------*/

static void watchpoint_update_flags(debug_cpu_info *info, int spacenum)
{
	UINT32 writeflag = DEBUG_FLAG_LIVE_WPW_PROGRAM << spacenum;
	UINT32 readflag = DEBUG_FLAG_LIVE_WPR_PROGRAM << spacenum;
	debug_cpu_watchpoint *wp;

	/* if hotspots are enabled, turn on all reads */
	if (info->hotspots != NULL)
	{
		info->flags |= DEBUG_FLAG_READ_WATCHPOINT;
		readflag = 0;
	}

	/* see if there are any enabled breakpoints */
	info->flags &= ~(readflag | writeflag);
	for (wp = info->space[spacenum].wplist; wp != NULL; wp = wp->next)
		if (wp->enabled)
		{
			if (wp->type & WATCHPOINT_READ)
			{
				info->flags |= readflag;
				readflag = 0;
			}
			if (wp->type & WATCHPOINT_WRITE)
			{
				info->flags |= writeflag;
				writeflag = 0;
			}
			if ((readflag | writeflag) == 0)
				break;
		}

	/* push the flags out globally */
	if (global.livecpu != NULL)
		compute_debug_flags(Machine, global.livecpu);
}


/*-------------------------------------------------
    watchpoint_check - check the watchpoints
    for a given CPU and address space
-------------------------------------------------*/

static void watchpoint_check(int cpunum, int spacenum, int type, offs_t address, UINT64 value_to_write, UINT64 mem_mask)
{
	const debug_cpu_info *info = &global.cpuinfo[cpunum];
	debug_cpu_watchpoint *wp;
	offs_t size = 0;
	UINT64 result;

	/* if we're within debugger code, don't stop */
	if (global.within_instruction_hook)
		return;

	global.within_instruction_hook = TRUE;

	/* adjust address, size & value_to_write based on mem_mask. */
	if (mem_mask != 0)
	{
		int bus_size = info->space[spacenum].databytes;
		int address_offset = 0;

		while (address_offset < bus_size && (mem_mask & 0xff) == 0)
		{
			address_offset++;
			value_to_write >>= 8;
			mem_mask >>= 8;
		}

		while (mem_mask != 0)
		{
			size++;
			mem_mask >>= 8;
		}

		if (info->endianness == CPU_IS_LE)
			address += address_offset;
		else
			address += bus_size - size - address_offset;
	}

	/* if we are a write watchpoint, stash the value that will be written */
	global.wpaddr = address;
	if (type & WATCHPOINT_WRITE)
		global.wpdata = value_to_write;

	/* see if we match */
	for (wp = info->space[spacenum].wplist; wp != NULL; wp = wp->next)
		if (wp->enabled && (wp->type & type) != 0 && address + size > wp->address && address < wp->address + wp->length)

			/* if we do, evaluate the condition */
			if (wp->condition == NULL || (expression_execute(wp->condition, &result) == EXPRERR_NONE && result != 0))
			{
				/* halt in the debugger by default */
				global.execution_state = EXECUTION_STATE_STOPPED;

				/* if we hit, evaluate the action */
				if (wp->action != NULL)
					debug_console_execute_command(wp->action, 0);

				/* print a notification, unless the action made us go again */
				if (global.execution_state == EXECUTION_STATE_STOPPED)
				{
					static const char *const sizes[] =
					{
						"0bytes", "byte", "word", "3bytes", "dword", "5bytes", "6bytes", "7bytes", "qword"
					};
					char buffer[100];

					if (type & WATCHPOINT_WRITE)
					{
						sprintf(buffer, "Stopped at watchpoint %X writing %s to %08X (PC=%X)", wp->index, sizes[size], BYTE2ADDR(address, &global.cpuinfo[cpunum], spacenum), activecpu_get_pc());
						if (value_to_write >> 32)
							sprintf(&buffer[strlen(buffer)], " (data=%X%08X)", (UINT32)(value_to_write >> 32), (UINT32)value_to_write);
						else
							sprintf(&buffer[strlen(buffer)], " (data=%X)", (UINT32)value_to_write);
					}
					else
						sprintf(buffer, "Stopped at watchpoint %X reading %s from %08X (PC=%X)", wp->index, sizes[size], BYTE2ADDR(address, &global.cpuinfo[cpunum], spacenum), activecpu_get_pc());
					debug_console_printf("%s\n", buffer);
					compute_debug_flags(Machine, info);
				}
				break;
			}

	global.within_instruction_hook = FALSE;
}


/*-------------------------------------------------
    debug_cpu_watchpoint_set - set a new watchpoint
-------------------------------------------------*/

int debug_cpu_watchpoint_set(int cpunum, int spacenum, int type, offs_t address, offs_t length, parsed_expression *condition, const char *action)
{
	debug_cpu_info *info = &global.cpuinfo[cpunum];
	debug_cpu_watchpoint *wp = malloc_or_die(sizeof(*wp));

	/* fill in the structure */
	wp->index = global.wpindex++;
	wp->enabled = TRUE;
	wp->type = type;
	wp->address = ADDR2BYTE_MASKED(address, &global.cpuinfo[cpunum], spacenum);
	wp->length = ADDR2BYTE(length, &global.cpuinfo[cpunum], spacenum);
	wp->condition = condition;
	wp->action = NULL;
	if (action != NULL)
	{
		wp->action = malloc_or_die(strlen(action) + 1);
		strcpy(wp->action, action);
	}

	/* hook us in */
	wp->next = info->space[spacenum].wplist;
	info->space[spacenum].wplist = wp;

	watchpoint_update_flags(info, spacenum);

	return wp->index;
}


/*-------------------------------------------------
    debug_cpu_watchpoint_clear - clear a watchpoint
-------------------------------------------------*/

int debug_cpu_watchpoint_clear(int wpnum)
{
	debug_cpu_watchpoint *wp, *pwp;
	int cpunum, spacenum;

	/* loop over CPUs and find the requested watchpoint */
	for (cpunum = 0; cpunum < MAX_CPU; cpunum++)
	{
		debug_cpu_info *info = &global.cpuinfo[cpunum];

		for (spacenum = 0; spacenum < ADDRESS_SPACES; spacenum++)
			for (pwp = NULL, wp = info->space[spacenum].wplist; wp != NULL; pwp = wp, wp = wp->next)
				if (wp->index == wpnum)
				{
					/* unlink us from the list */
					if (pwp == NULL)
						info->space[spacenum].wplist = wp->next;
					else
						pwp->next = wp->next;

					/* free the memory */
					if (wp->condition != NULL)
						expression_free(wp->condition);
					if (wp->action != NULL)
						free(wp->action);
					free(wp);

					watchpoint_update_flags(info, spacenum);
					return 1;
				}
	}

	/* we didn't find it; return an error */
	return 0;
}


/*-------------------------------------------------
    debug_cpu_watchpoint_enable - enable/disable a
    watchpoint
-------------------------------------------------*/

int debug_cpu_watchpoint_enable(int wpnum, int enable)
{
	debug_cpu_watchpoint *wp;
	int cpunum, spacenum;

	/* loop over CPUs and address spaces and find the requested watchpoint */
	for (cpunum = 0; cpunum < MAX_CPU; cpunum++)
	{
		debug_cpu_info *info = &global.cpuinfo[cpunum];

		for (spacenum = 0; spacenum < ADDRESS_SPACES; spacenum++)
			for (wp = info->space[spacenum].wplist; wp; wp = wp->next)
				if (wp->index == wpnum)
				{
					wp->enabled = (enable != 0);
					watchpoint_update_flags(info, spacenum);
					return 1;
				}
	}
	return 0;
}



/***************************************************************************
    HOTSPOTS
***************************************************************************/

/*-------------------------------------------------
    debug_cpu_hotspot_track - enable/disable tracking
    of hotspots
-------------------------------------------------*/

int debug_cpu_hotspot_track(int cpunum, int numspots, int threshhold)
{
	debug_cpu_info *info = &global.cpuinfo[cpunum];

	/* if we already have tracking info, kill it */
	if (info->hotspots)
		free(info->hotspots);
	info->hotspots = NULL;

	/* only start tracking if we have a non-zero count */
	if (numspots > 0)
	{
		/* allocate memory for hotspots */
		info->hotspots = malloc_or_die(sizeof(*info->hotspots) * numspots);
		memset(info->hotspots, 0xff, sizeof(*info->hotspots) * numspots);

		/* fill in the info */
		info->hotspot_count = numspots;
		info->hotspot_threshhold = threshhold;
	}

	watchpoint_update_flags(info, ADDRESS_SPACE_PROGRAM);
	return 1;
}


/*-------------------------------------------------
    check_hotspots - check for
    hotspots on a memory read access
-------------------------------------------------*/

static void check_hotspots(int cpunum, int spacenum, offs_t address)
{
	debug_cpu_info *info = &global.cpuinfo[cpunum];
	offs_t pc = activecpu_get_pc();
	int hotindex;

	/* see if we have a match in our list */
	for (hotindex = 0; hotindex < info->hotspot_count; hotindex++)
		if (info->hotspots[hotindex].access == address && info->hotspots[hotindex].pc == pc && info->hotspots[hotindex].spacenum == spacenum)
			break;

	/* if we didn't find any, make a new entry */
	if (hotindex == info->hotspot_count)
	{
		/* if the bottom of the list is over the threshhold, print it */
		debug_hotspot_entry *spot = &info->hotspots[info->hotspot_count - 1];
		if (spot->count > info->hotspot_threshhold)
			debug_console_printf("Hotspot @ %s %08X (PC=%08X) hit %d times (fell off bottom)\n", address_space_names[spot->spacenum], spot->access, spot->pc, spot->count);

		/* move everything else down and insert this one at the top */
		memmove(&info->hotspots[1], &info->hotspots[0], sizeof(info->hotspots[0]) * (info->hotspot_count - 1));
		info->hotspots[0].access = address;
		info->hotspots[0].pc = pc;
		info->hotspots[0].spacenum = spacenum;
		info->hotspots[0].count = 1;
	}

	/* if we did find one, increase the count and move it to the top */
	else
	{
		info->hotspots[hotindex].count++;
		if (hotindex != 0)
		{
			debug_hotspot_entry temp = info->hotspots[hotindex];
			memmove(&info->hotspots[1], &info->hotspots[0], sizeof(info->hotspots[0]) * hotindex);
			info->hotspots[0] = temp;
		}
	}
}


/***************************************************************************
    MEMORY ACCESSORS
***************************************************************************/

/*-------------------------------------------------
    debug_read_byte - return a byte from the
    current cpu in the specified memory space
-------------------------------------------------*/

UINT8 debug_read_byte(int spacenum, offs_t address, int apply_translation)
{
	const debug_cpu_info *info = &global.cpuinfo[cpu_getactivecpu()];
	UINT64 custom;
	UINT8 result;

	/* mask against the logical byte mask */
	address &= info->space[spacenum].logbytemask;

	/* all accesses from this point on are for the debugger */
	memory_set_debugger_access(1);

	/* translate if necessary; if not mapped, return 0xff */
	if (apply_translation && info->translate != NULL && !(*info->translate)(spacenum, TRANSLATE_READ_DEBUG, &address))
		result = 0xff;

	/* if there is a custom read handler, and it returns TRUE, use that value */
	else if (info->read && (*info->read)(spacenum, address, 1, &custom))
		result = custom;

	/* otherwise, call the byte reading function for the translated address */
	else
		result = (*active_address_space[spacenum].accessors->read_byte)(address);

	/* no longer accessing via the debugger */
	memory_set_debugger_access(0);
	return result;
}


/*-------------------------------------------------
    debug_read_word - return a word from the
    current cpu in the specified memory space
-------------------------------------------------*/

UINT16 debug_read_word(int spacenum, offs_t address, int apply_translation)
{
	const debug_cpu_info *info = &global.cpuinfo[cpu_getactivecpu()];
	UINT64 custom;
	UINT16 result;

	/* mask against the logical byte mask */
	address &= info->space[spacenum].logbytemask;

	/* if this is misaligned read, or if there are no word readers, just read two bytes */
	if ((address & 1) || !active_address_space[spacenum].accessors->read_word)
	{
		UINT8 byte0 = debug_read_byte(spacenum, address + 0, apply_translation);
		UINT8 byte1 = debug_read_byte(spacenum, address + 1, apply_translation);

		/* based on the endianness, the result is assembled differently */
		if (global.cpuinfo[cpu_getactivecpu()].endianness == CPU_IS_LE)
			result = byte0 | (byte1 << 8);
		else
			result = byte1 | (byte0 << 8);
	}

	/* otherwise, this proceeds like the byte case */
	else
	{
		/* all accesses from this point on are for the debugger */
		memory_set_debugger_access(1);

		/* translate if necessary; if not mapped, return 0xffff */
		if (apply_translation && info->translate != NULL && !(*info->translate)(spacenum, TRANSLATE_READ_DEBUG, &address))
			result = 0xffff;

		/* if there is a custom read handler, and it returns TRUE, use that value */
		else if (info->read && (*info->read)(spacenum, address, 2, &custom))
			result = custom;

		/* otherwise, call the byte reading function for the translated address */
		else
			result = (*active_address_space[spacenum].accessors->read_word)(address);

		/* no longer accessing via the debugger */
		memory_set_debugger_access(0);
	}

	return result;
}


/*-------------------------------------------------
    debug_read_dword - return a dword from the
    current cpu in the specified memory space
-------------------------------------------------*/

UINT32 debug_read_dword(int spacenum, offs_t address, int apply_translation)
{
	const debug_cpu_info *info = &global.cpuinfo[cpu_getactivecpu()];
	UINT64 custom;
	UINT32 result;

	/* mask against the logical byte mask */
	address &= info->space[spacenum].logbytemask;

	/* if this is misaligned read, or if there are no dword readers, just read two words */
	if ((address & 3) || !active_address_space[spacenum].accessors->read_dword)
	{
		UINT16 word0 = debug_read_word(spacenum, address + 0, apply_translation);
		UINT16 word1 = debug_read_word(spacenum, address + 2, apply_translation);

		/* based on the endianness, the result is assembled differently */
		if (global.cpuinfo[cpu_getactivecpu()].endianness == CPU_IS_LE)
			result = word0 | (word1 << 16);
		else
			result = word1 | (word0 << 16);
	}

	/* otherwise, this proceeds like the byte case */
	else
	{
		/* all accesses from this point on are for the debugger */
		memory_set_debugger_access(1);

		/* translate if necessary; if not mapped, return 0xffffffff */
		if (apply_translation && info->translate != NULL && !(*info->translate)(spacenum, TRANSLATE_READ_DEBUG, &address))
			result = 0xffffffff;

		/* if there is a custom read handler, and it returns TRUE, use that value */
		else if (info->read && (*info->read)(spacenum, address, 4, &custom))
			result = custom;

		/* otherwise, call the byte reading function for the translated address */
		else
			result = (*active_address_space[spacenum].accessors->read_dword)(address);

		/* no longer accessing via the debugger */
		memory_set_debugger_access(0);
	}

	return result;
}


/*-------------------------------------------------
    debug_read_qword - return a qword from the
    current cpu in the specified memory space
-------------------------------------------------*/

UINT64 debug_read_qword(int spacenum, offs_t address, int apply_translation)
{
	const debug_cpu_info *info = &global.cpuinfo[cpu_getactivecpu()];
	UINT64 custom;
	UINT64 result;

	/* mask against the logical byte mask */
	address &= info->space[spacenum].logbytemask;

	/* if this is misaligned read, or if there are no qword readers, just read two dwords */
	if ((address & 7) || !active_address_space[spacenum].accessors->read_qword)
	{
		UINT32 dword0 = debug_read_dword(spacenum, address + 0, apply_translation);
		UINT32 dword1 = debug_read_dword(spacenum, address + 4, apply_translation);

		/* based on the endianness, the result is assembled differently */
		if (global.cpuinfo[cpu_getactivecpu()].endianness == CPU_IS_LE)
			result = dword0 | ((UINT64)dword1 << 32);
		else
			result = dword1 | ((UINT64)dword0 << 32);
	}

	/* otherwise, this proceeds like the byte case */
	else
	{
		/* all accesses from this point on are for the debugger */
		memory_set_debugger_access(1);

		/* translate if necessary; if not mapped, return 0xffffffffffffffff */
		if (apply_translation && info->translate != NULL && !(*info->translate)(spacenum, TRANSLATE_READ_DEBUG, &address))
			result = ~(UINT64)0;

		/* if there is a custom read handler, and it returns TRUE, use that value */
		else if (info->read && (*info->read)(spacenum, address, 8, &custom))
			result = custom;

		/* otherwise, call the byte reading function for the translated address */
		else
			result = (*active_address_space[spacenum].accessors->read_qword)(address);

		/* no longer accessing via the debugger */
		memory_set_debugger_access(0);
	}

	return result;
}


/*-------------------------------------------------
    debug_write_byte - write a byte to the
    current cpu in the specified memory space
-------------------------------------------------*/

void debug_write_byte(int spacenum, offs_t address, UINT8 data, int apply_translation)
{
	const debug_cpu_info *info = &global.cpuinfo[cpu_getactivecpu()];

	/* mask against the logical byte mask */
	address &= info->space[spacenum].logbytemask;

	/* all accesses from this point on are for the debugger */
	memory_set_debugger_access(1);

	/* translate if necessary; if not mapped, we're done */
	if (apply_translation && info->translate != NULL && !(*info->translate)(spacenum, TRANSLATE_WRITE_DEBUG, &address))
		;

	/* if there is a custom write handler, and it returns TRUE, use that */
	else if (info->write && (*info->write)(spacenum, address, 1, data))
		;

	/* otherwise, call the byte reading function for the translated address */
	else
		(*active_address_space[spacenum].accessors->write_byte)(address, data);

	/* no longer accessing via the debugger */
	memory_set_debugger_access(0);
	global.memory_modified = TRUE;
}


/*-------------------------------------------------
    debug_write_word - write a word to the
    current cpu in the specified memory space
-------------------------------------------------*/

void debug_write_word(int spacenum, offs_t address, UINT16 data, int apply_translation)
{
	const debug_cpu_info *info = &global.cpuinfo[cpu_getactivecpu()];

	/* mask against the logical byte mask */
	address &= info->space[spacenum].logbytemask;

	/* if this is a misaligned write, or if there are no word writers, just read two bytes */
	if ((address & 1) || !active_address_space[spacenum].accessors->write_word)
	{
		if (global.cpuinfo[cpu_getactivecpu()].endianness == CPU_IS_LE)
		{
			debug_write_byte(spacenum, address + 0, data >> 0, apply_translation);
			debug_write_byte(spacenum, address + 1, data >> 8, apply_translation);
		}
		else
		{
			debug_write_byte(spacenum, address + 0, data >> 8, apply_translation);
			debug_write_byte(spacenum, address + 1, data >> 0, apply_translation);
		}
	}

	/* otherwise, this proceeds like the byte case */
	else
	{
		/* all accesses from this point on are for the debugger */
		memory_set_debugger_access(1);

		/* translate if necessary; if not mapped, we're done */
		if (apply_translation && info->translate && !(*info->translate)(spacenum, TRANSLATE_WRITE_DEBUG, &address))
			;

		/* if there is a custom write handler, and it returns TRUE, use that */
		else if (info->write && (*info->write)(spacenum, address, 2, data))
			;

		/* otherwise, call the byte reading function for the translated address */
		else
			(*active_address_space[spacenum].accessors->write_word)(address, data);

		/* no longer accessing via the debugger */
		memory_set_debugger_access(0);
		global.memory_modified = TRUE;
	}
}


/*-------------------------------------------------
    debug_write_dword - write a dword to the
    current cpu in the specified memory space
-------------------------------------------------*/

void debug_write_dword(int spacenum, offs_t address, UINT32 data, int apply_translation)
{
	const debug_cpu_info *info = &global.cpuinfo[cpu_getactivecpu()];

	/* mask against the logical byte mask */
	address &= info->space[spacenum].logbytemask;

	/* if this is a misaligned write, or if there are no dword writers, just read two words */
	if ((address & 3) || !active_address_space[spacenum].accessors->write_dword)
	{
		if (global.cpuinfo[cpu_getactivecpu()].endianness == CPU_IS_LE)
		{
			debug_write_word(spacenum, address + 0, data >> 0, apply_translation);
			debug_write_word(spacenum, address + 2, data >> 16, apply_translation);
		}
		else
		{
			debug_write_word(spacenum, address + 0, data >> 16, apply_translation);
			debug_write_word(spacenum, address + 2, data >> 0, apply_translation);
		}
	}

	/* otherwise, this proceeds like the byte case */
	else
	{
		/* all accesses from this point on are for the debugger */
		memory_set_debugger_access(1);

		/* translate if necessary; if not mapped, we're done */
		if (apply_translation && info->translate && !(*info->translate)(spacenum, TRANSLATE_WRITE_DEBUG, &address))
			;

		/* if there is a custom write handler, and it returns TRUE, use that */
		else if (info->write && (*info->write)(spacenum, address, 4, data))
			;

		/* otherwise, call the byte reading function for the translated address */
		else
			(*active_address_space[spacenum].accessors->write_dword)(address, data);

		/* no longer accessing via the debugger */
		memory_set_debugger_access(0);
		global.memory_modified = TRUE;
	}
}


/*-------------------------------------------------
    debug_write_qword - write a qword to the
    current cpu in the specified memory space
-------------------------------------------------*/

void debug_write_qword(int spacenum, offs_t address, UINT64 data, int apply_translation)
{
	const debug_cpu_info *info = &global.cpuinfo[cpu_getactivecpu()];

	/* mask against the logical byte mask */
	address &= info->space[spacenum].logbytemask;

	/* if this is a misaligned write, or if there are no qword writers, just read two dwords */
	if ((address & 7) || !active_address_space[spacenum].accessors->write_qword)
	{
		if (global.cpuinfo[cpu_getactivecpu()].endianness == CPU_IS_LE)
		{
			debug_write_dword(spacenum, address + 0, data >> 0, apply_translation);
			debug_write_dword(spacenum, address + 4, data >> 32, apply_translation);
		}
		else
		{
			debug_write_dword(spacenum, address + 0, data >> 32, apply_translation);
			debug_write_dword(spacenum, address + 4, data >> 0, apply_translation);
		}
	}
	/* otherwise, this proceeds like the byte case */
	else
	{
		/* all accesses from this point on are for the debugger */
		memory_set_debugger_access(1);

		/* translate if necessary; if not mapped, we're done */
		if (apply_translation && info->translate && !(*info->translate)(spacenum, TRANSLATE_WRITE_DEBUG, &address))
			;

		/* if there is a custom write handler, and it returns TRUE, use that */
		else if (info->write && (*info->write)(spacenum, address, 8, data))
			;

		/* otherwise, call the byte reading function for the translated address */
		else
			(*active_address_space[spacenum].accessors->write_qword)(address, data);

		/* no longer accessing via the debugger */
		memory_set_debugger_access(0);
		global.memory_modified = TRUE;
	}
}


/*-------------------------------------------------
    debug_read_opcode - read 1,2,4 or 8 bytes at
    the given offset from opcode space
-------------------------------------------------*/

UINT64 debug_read_opcode(offs_t address, int size, int arg)
{
	const debug_cpu_info *info = &global.cpuinfo[cpu_getactivecpu()];
	offs_t lowbits_mask;
	const void *ptr;

	/* keep in logical range */
	address &= info->space[ADDRESS_SPACE_PROGRAM].logbytemask;

	/* shortcut if we have a custom routine */
	if (info->readop)
	{
		UINT64 result;
		if ((*info->readop)(address, size, &result))
			return result;
	}

	/* if we're bigger than the address bus, break into smaller pieces */
	if (size > info->space[ADDRESS_SPACE_PROGRAM].databytes)
	{
		int halfsize = size / 2;
		UINT64 r0 = debug_read_opcode(address + 0, halfsize, arg);
		UINT64 r1 = debug_read_opcode(address + halfsize, halfsize, arg);

		if (info->endianness == CPU_IS_LE)
			return r0 | (r1 << (8 * halfsize));
		else
			return r1 | (r0 << (8 * halfsize));
	}

	/* translate to physical first */
	if (info->translate && !(*info->translate)(ADDRESS_SPACE_PROGRAM, TRANSLATE_FETCH_DEBUG, &address))
		return ~(UINT64)0 & (~(UINT64)0 >> (64 - 8*size));

	/* keep in physical range */
	address &= info->space[ADDRESS_SPACE_PROGRAM].physbytemask;

	/* adjust the address */
	memory_set_opbase(address);

	switch (info->space[ADDRESS_SPACE_PROGRAM].databytes * 10 + size)
	{
		/* dump opcodes in bytes from a byte-sized bus */
		case 11:
			break;

		/* dump opcodes in bytes from a word-sized bus */
		case 21:
			address ^= (info->endianness == CPU_IS_LE) ? BYTE_XOR_LE(0) : BYTE_XOR_BE(0);
			break;

		/* dump opcodes in words from a word-sized bus */
		case 22:
			break;

		/* dump opcodes in bytes from a dword-sized bus */
		case 41:
			address ^= (info->endianness == CPU_IS_LE) ? BYTE4_XOR_LE(0) : BYTE4_XOR_BE(0);
			break;

		/* dump opcodes in words from a dword-sized bus */
		case 42:
			address ^= (info->endianness == CPU_IS_LE) ? WORD_XOR_LE(0) : WORD_XOR_BE(0);
			break;

		/* dump opcodes in dwords from a dword-sized bus */
		case 44:
			break;

		/* dump opcodes in bytes from a qword-sized bus */
		case 81:
			address ^= (info->endianness == CPU_IS_LE) ? BYTE8_XOR_LE(0) : BYTE8_XOR_BE(0);
			break;

		/* dump opcodes in words from a qword-sized bus */
		case 82:
			address ^= (info->endianness == CPU_IS_LE) ? WORD2_XOR_LE(0) : WORD2_XOR_BE(0);
			break;

		/* dump opcodes in dwords from a qword-sized bus */
		case 84:
			address ^= (info->endianness == CPU_IS_LE) ? DWORD_XOR_LE(0) : DWORD_XOR_BE(0);
			break;

		/* dump opcodes in qwords from a qword-sized bus */
		case 88:
			break;

		default:
			fatalerror("debug_read_opcode: unknown type = %d", info->space[ADDRESS_SPACE_PROGRAM].databytes * 10 + size);
			break;
	}

	/* get pointer to data */
	/* note that we query aligned to the bus width, and then add back the low bits */
	lowbits_mask = info->space[ADDRESS_SPACE_PROGRAM].databytes - 1;
	ptr = memory_get_op_ptr(cpu_getactivecpu(), address & ~lowbits_mask, arg);
	if (!ptr)
		return ~(UINT64)0 & (~(UINT64)0 >> (64 - 8*size));
	ptr = (UINT8 *)ptr + (address & lowbits_mask);

	/* gross! */
//  if (osd_is_bad_read_ptr(ptr, size))
//      fatalerror("debug_read_opcode: cpu %d address %x mapped to invalid memory %p", cpu_getactivecpu(), address, ptr);

	/* return based on the size */
	switch (size)
	{
		case 1:	return *(UINT8 *) ptr;
		case 2:	return *(UINT16 *)ptr;
		case 4:	return *(UINT32 *)ptr;
		case 8:	return *(UINT64 *)ptr;
	}

	return 0;	/* appease compiler */
}


/*-------------------------------------------------
    expression_read_memory - read 1,2,4 or 8 bytes
    at the given offset in the given address
    space
-------------------------------------------------*/

static UINT64 expression_read_memory(int space, int index, UINT32 address, int size)
{
	int cpuindex;

	switch (space)
	{
		case EXPSPACE_PROGRAM:
		case EXPSPACE_DATA:
		case EXPSPACE_IO:
			cpuindex = (index == -1) ? cpu_getactivecpu() : index;
			space = ADDRESS_SPACE_PROGRAM + (space - EXPSPACE_PROGRAM);
			return expression_read_address_space(cpuindex, space, address, size);

		case EXPSPACE_OPCODE:
		case EXPSPACE_RAMWRITE:
			cpuindex = (index == -1) ? cpu_getactivecpu() : index;
			return expression_read_program_direct(cpuindex, (space == EXPSPACE_OPCODE), address, size);

		case EXPSPACE_EEPROM:
			return expression_read_eeprom(address, size);

		case EXPSPACE_CPU:
		case EXPSPACE_USER:
		case EXPSPACE_GFX:
		case EXPSPACE_SOUND:
			if (index < 1)
				break;
			space = ADDRESS_SPACE_PROGRAM + (space - EXPSPACE_PROGRAM);
			return expression_read_memory_region(index, space, address, size);
	}
	return ~(UINT64)0 >> (64 - 8*size);
}


/*-------------------------------------------------
    expression_read_address_space - read memory
    from a specific CPU's address space
-------------------------------------------------*/

static UINT64 expression_read_address_space(int cpuindex, int space, offs_t address, int size)
{
	const debug_cpu_info *info = &global.cpuinfo[cpuindex];
	UINT64 result = ~(UINT64)0 >> (64 - 8*size);

	/* only process if in of range and we have a bus */
	if (cpuindex < ARRAY_LENGTH(global.cpuinfo) && info->space[space].databytes != 0)
	{
		/* adjust the address into a byte address */
		address = ADDR2BYTE(address, info, space);

		/* switch contexts and do the read */
		cpuintrf_push_context(cpuindex);
		switch (size)
		{
			case 1:		result = debug_read_byte(space, address, TRUE);		break;
			case 2:		result = debug_read_word(space, address, TRUE);		break;
			case 4:		result = debug_read_dword(space, address, TRUE);	break;
			case 8:		result = debug_read_qword(space, address, TRUE);	break;
		}
		cpuintrf_pop_context();
	}
	return result;
}


/*-------------------------------------------------
    expression_read_program_direct - read memory
    directly from an opcode or RAM pointer
-------------------------------------------------*/

static UINT64 expression_read_program_direct(int cpuindex, int opcode, offs_t address, int size)
{
	const debug_cpu_info *info = &global.cpuinfo[cpuindex];
	UINT64 result = ~(UINT64)0 >> (64 - 8*size);
	UINT8 *base;

	/* only process if in of range and we have a bus */
	if (cpuindex < ARRAY_LENGTH(global.cpuinfo) && info->space[ADDRESS_SPACE_PROGRAM].databytes != 0)
	{
		/* adjust the address into a byte address, but not if being called recursively */
		if ((opcode & 2) == 0)
			address = ADDR2BYTE(address, info, ADDRESS_SPACE_PROGRAM);

		/* call ourself recursively until we are byte-sized */
		if (size > 1)
		{
			int halfsize = size / 2;
			UINT64 r0, r1;

			/* read each half, from lower address to upper address */
			r0 = expression_read_program_direct(cpuindex, opcode | 2, address + 0, halfsize);
			r1 = expression_read_program_direct(cpuindex, opcode | 2, address + halfsize, halfsize);

			/* assemble based on the target endianness */
			if (info->endianness == CPU_IS_LE)
				result = r0 | (r1 << (8 * halfsize));
			else
				result = r1 | (r0 << (8 * halfsize));
		}

		/* handle the byte-sized final requests */
		else
		{
			/* lowmask specified which address bits are within the databus width */
			offs_t lowmask = info->space[ADDRESS_SPACE_PROGRAM].databytes - 1;

			/* get the base of memory, aligned to the address minus the lowbits */
			if (opcode & 1)
				base = memory_get_op_ptr(cpuindex, address & ~lowmask, FALSE);
			else
				base = memory_get_read_ptr(cpuindex, ADDRESS_SPACE_PROGRAM, address & ~lowmask);

			/* if we have a valid base, return the appropriate byte */
			if (base != NULL)
			{
				if (info->endianness == CPU_IS_LE)
					result = base[BYTE8_XOR_LE(address) & lowmask];
				else
					result = base[BYTE8_XOR_BE(address) & lowmask];
			}
		}
	}
	return result;
}


/*-------------------------------------------------
    expression_read_memory_region - read memory
    from a memory region
-------------------------------------------------*/

static UINT64 expression_read_memory_region(int rgnindex, int rgntype, offs_t address, int size)
{
	UINT64 result = ~(UINT64)0 >> (64 - 8*size);
	int rgnnum = -1;

	/* convert to a region number */
	switch (rgntype)
	{
		case EXPSPACE_CPU:		rgnnum = REGION_CPU1 + (rgnindex - 1);		break;
		case EXPSPACE_USER:		rgnnum = REGION_USER1 + (rgnindex - 1);		break;
		case EXPSPACE_GFX:		rgnnum = REGION_GFX1 + (rgnindex - 1);		break;
		case EXPSPACE_SOUND:	rgnnum = REGION_SOUND1 + (rgnindex - 1);	break;
	}

	/* process if it exists */
	if (rgnnum != -1)
	{
		UINT8 *base = memory_region(Machine, rgnnum);

		/* make sure we get a valid base before proceeding */
		if (base != NULL)
		{
			UINT32 length = memory_region_length(Machine, rgnnum);
			UINT32 flags = memory_region_flags(Machine, rgnnum);

			/* call ourself recursively until we are byte-sized */
			if (size > 1)
			{
				int halfsize = size / 2;
				UINT64 r0, r1;

				/* read each half, from lower address to upper address */
				r0 = expression_read_memory_region(rgnindex, rgntype, address + 0, halfsize);
				r1 = expression_read_memory_region(rgnindex, rgntype, address + halfsize, halfsize);

				/* assemble based on the target endianness */
				if ((flags & ROMREGION_ENDIANMASK) == ROMREGION_LE)
					result = r0 | (r1 << (8 * halfsize));
				else
					result = r1 | (r0 << (8 * halfsize));
			}

			/* only process if we're within range */
			else if (address < length)
			{
				/* lowmask specified which address bits are within the databus width */
				UINT32 lowmask = (1 << (flags & ROMREGION_WIDTHMASK)) - 1;
				base += address & ~lowmask;

				/* if we have a valid base, return the appropriate byte */
				if ((flags & ROMREGION_ENDIANMASK) == ROMREGION_LE)
					result = base[BYTE8_XOR_LE(address) & lowmask];
				else
					result = base[BYTE8_XOR_BE(address) & lowmask];
			}
		}
	}
	return result;
}


/*-------------------------------------------------
    expression_read_eeprom - read EEPROM data
-------------------------------------------------*/

static UINT64 expression_read_eeprom(offs_t address, int size)
{
	UINT64 result = ~(UINT64)0 >> (64 - 8*size);
	UINT32 eelength, eesize;
	void *base;

	/* make sure we get a valid base before proceeding */
	base = eeprom_get_data_pointer(&eelength, &eesize);
	if (base != NULL && address < eelength)
	{
		/* switch off the size */
		switch (eesize)
		{
			case 1:		result &= ((UINT8 *)base)[address];							break;
			case 2:		result &= BIG_ENDIANIZE_INT16(((UINT16 *)base)[address]);	break;
		}
	}
	return result;
}


/*-------------------------------------------------
    expression_write_memory - write 1,2,4 or 8
    bytes at the given offset in the given address
    space
-------------------------------------------------*/

static void expression_write_memory(int space, int index, UINT32 address, int size, UINT64 data)
{
	int cpuindex;

	switch (space)
	{
		case EXPSPACE_PROGRAM:
		case EXPSPACE_DATA:
		case EXPSPACE_IO:
			cpuindex = (index == -1) ? cpu_getactivecpu() : index;
			space = ADDRESS_SPACE_PROGRAM + (space - EXPSPACE_PROGRAM);
			expression_write_address_space(cpuindex, space, address, size, data);
			break;

		case EXPSPACE_OPCODE:
		case EXPSPACE_RAMWRITE:
			cpuindex = (index == -1) ? cpu_getactivecpu() : index;
			expression_write_program_direct(cpuindex, (space == EXPSPACE_OPCODE), address, size, data);
			break;

		case EXPSPACE_EEPROM:
			expression_write_eeprom(address, size, data);
			break;

		case EXPSPACE_CPU:
		case EXPSPACE_USER:
		case EXPSPACE_GFX:
		case EXPSPACE_SOUND:
			if (index < 1)
				break;
			space = ADDRESS_SPACE_PROGRAM + (space - EXPSPACE_PROGRAM);
			expression_write_memory_region(index, space, address, size, data);
			break;
	}
}


/*-------------------------------------------------
    expression_write_address_space - write memory
    to a specific CPU's address space
-------------------------------------------------*/

static void expression_write_address_space(int cpuindex, int space, offs_t address, int size, UINT64 data)
{
	const debug_cpu_info *info = &global.cpuinfo[cpuindex];

	/* only process if in of range and we have a bus */
	if (cpuindex < ARRAY_LENGTH(global.cpuinfo) && info->space[space].databytes != 0)
	{
		/* adjust the address into a byte address */
		address = ADDR2BYTE(address, info, space);

		/* switch contexts and do the write */
		cpuintrf_push_context(cpuindex);
		switch (size)
		{
			case 1:		debug_write_byte(space, address, data, TRUE);	break;
			case 2:		debug_write_word(space, address, data, TRUE);	break;
			case 4:		debug_write_dword(space, address, data, TRUE);	break;
			case 8:		debug_write_qword(space, address, data, TRUE);	break;
		}
		cpuintrf_pop_context();
	}
}


/*-------------------------------------------------
    expression_write_program_direct - write memory
    directly to an opcode or RAM pointer
-------------------------------------------------*/

static void expression_write_program_direct(int cpuindex, int opcode, offs_t address, int size, UINT64 data)
{
	const debug_cpu_info *info = &global.cpuinfo[cpuindex];
	UINT8 *base;

	/* only process if in of range and we have a bus */
	if (cpuindex < ARRAY_LENGTH(global.cpuinfo) && info->space[ADDRESS_SPACE_PROGRAM].databytes != 0)
	{
		/* adjust the address into a byte address, but not if being called recursively */
		if ((opcode & 2) == 0)
			address = ADDR2BYTE(address, info, ADDRESS_SPACE_PROGRAM);

		/* call ourself recursively until we are byte-sized */
		if (size > 1)
		{
			int halfsize = size / 2;
			UINT64 r0, r1, halfmask;

			/* break apart based on the target endianness */
			halfmask = ~(UINT64)0 >> (64 - 8 * halfsize);
			if (info->endianness == CPU_IS_LE)
			{
				r0 = data & halfmask;
				r1 = (data >> (8 * halfsize)) & halfmask;
			}
			else
			{
				r0 = (data >> (8 * halfsize)) & halfmask;
				r1 = data & halfmask;
			}

			/* write each half, from lower address to upper address */
			expression_write_program_direct(cpuindex, opcode | 2, address + 0, halfsize, r0);
			expression_write_program_direct(cpuindex, opcode | 2, address + halfsize, halfsize, r1);
		}

		/* handle the byte-sized final case */
		else
		{
			/* lowmask specified which address bits are within the databus width */
			offs_t lowmask = info->space[ADDRESS_SPACE_PROGRAM].databytes - 1;

			/* get the base of memory, aligned to the address minus the lowbits */
			if (opcode & 1)
				base = memory_get_op_ptr(cpuindex, address & ~lowmask, FALSE);
			else
				base = memory_get_read_ptr(cpuindex, ADDRESS_SPACE_PROGRAM, address & ~lowmask);

			/* if we have a valid base, write the appropriate byte */
			if (base != NULL)
			{
				if (info->endianness == CPU_IS_LE)
					base[BYTE8_XOR_LE(address) & lowmask] = data;
				else
					base[BYTE8_XOR_BE(address) & lowmask] = data;
				global.memory_modified = TRUE;
			}
		}
	}
}


/*-------------------------------------------------
    expression_write_memory_region - write memory
    from a memory region
-------------------------------------------------*/

static void expression_write_memory_region(int rgnindex, int rgntype, offs_t address, int size, UINT64 data)
{
	int rgnnum = -1;

	/* convert to a region number */
	switch (rgntype)
	{
		case EXPSPACE_CPU:		rgnnum = REGION_CPU1 + (rgnindex - 1);		break;
		case EXPSPACE_USER:		rgnnum = REGION_USER1 + (rgnindex - 1);		break;
		case EXPSPACE_GFX:		rgnnum = REGION_GFX1 + (rgnindex - 1);		break;
		case EXPSPACE_SOUND:	rgnnum = REGION_SOUND1 + (rgnindex - 1);	break;
	}

	/* process if it exists */
	if (rgnnum != -1)
	{
		UINT8 *base = memory_region(Machine, rgnnum);

		/* make sure we get a valid base before proceeding */
		if (base != NULL)
		{
			UINT32 length = memory_region_length(Machine, rgnnum);
			UINT32 flags = memory_region_flags(Machine, rgnnum);

			/* call ourself recursively until we are byte-sized */
			if (size > 1)
			{
				int halfsize = size / 2;
				UINT64 r0, r1, halfmask;

				/* break apart based on the target endianness */
				halfmask = ~(UINT64)0 >> (64 - 8 * halfsize);
				if ((flags & ROMREGION_ENDIANMASK) == ROMREGION_LE)
				{
					r0 = data & halfmask;
					r1 = (data >> (8 * halfsize)) & halfmask;
				}
				else
				{
					r0 = (data >> (8 * halfsize)) & halfmask;
					r1 = data & halfmask;
				}

				/* write each half, from lower address to upper address */
				expression_write_memory_region(rgnindex, rgntype, address + 0, halfsize, r0);
				expression_write_memory_region(rgnindex, rgntype, address + halfsize, halfsize, r1);
			}

			/* only process if we're within range */
			else if (address < length)
			{
				/* lowmask specified which address bits are within the databus width */
				UINT32 lowmask = (1 << (flags & ROMREGION_WIDTHMASK)) - 1;
				base += address & ~lowmask;

				/* if we have a valid base, set the appropriate byte */
				if ((flags & ROMREGION_ENDIANMASK) == ROMREGION_LE)
					base[BYTE8_XOR_LE(address) & lowmask] = data;
				else
					base[BYTE8_XOR_BE(address) & lowmask] = data;
				global.memory_modified = TRUE;
			}
		}
	}
}


/*-------------------------------------------------
    expression_write_eeprom - write EEPROM data
-------------------------------------------------*/

static void expression_write_eeprom(offs_t address, int size, UINT64 data)
{
	UINT32 eelength, eesize;
	void *vbase = eeprom_get_data_pointer(&eelength, &eesize);

	/* make sure we get a valid base before proceeding */
	if (vbase != NULL && address < eelength)
	{
		UINT64 mask = ~(UINT64)0 >> (64 - 8*size);

		/* switch off the size */
		switch (eesize)
		{
			case 1:
			{
				UINT8 *base = (UINT8 *)vbase + address;
				*base = (*base & ~mask) | (data & mask);
				break;
			}

			case 2:
			{
				UINT16 *base = (UINT16 *)vbase + address;
				UINT16 value = BIG_ENDIANIZE_INT16(*base);
				value = (value & ~mask) | (data & mask);
				*base = BIG_ENDIANIZE_INT16(value);
				break;
			}
		}
		global.memory_modified = TRUE;
	}
}


/*-------------------------------------------------
    debug_cpu_trace_printf - writes text to a given
    CPU's trace file
-------------------------------------------------*/

void debug_cpu_trace_printf(int cpunum, const char *fmt, ...)
{
	va_list va;

	debug_cpu_info *info = &global.cpuinfo[cpunum];

	if (info->trace.file)
	{
		va_start(va, fmt);
		vfprintf(info->trace.file, fmt, va);
		va_end(va);
	}
}


/*-------------------------------------------------
    debug_cpu_source_script - specifies a debug command
    script to use
-------------------------------------------------*/

void debug_cpu_source_script(const char *file)
{
	if (debug_source_file)
	{
		fclose(debug_source_file);
		debug_source_file = NULL;
	}

	if (file)
	{
		debug_source_file = fopen(file, "r");
		if (!debug_source_file)
		{
			if (mame_get_phase(Machine) == MAME_PHASE_RUNNING)
				debug_console_printf("Cannot open command file '%s'\n", file);
			else
				fatalerror("Cannot open command file '%s'", file);
		}
	}
}


/*-------------------------------------------------
    debug_cpu_flush_traces - flushes all traces; this is
    useful if a trace is going on when we fatalerror
-------------------------------------------------*/

void debug_cpu_flush_traces(void)
{
	int cpunum;

	for (cpunum = 0; cpunum < cpu_gettotalcpu(); cpunum++)
	{
		if (global.cpuinfo[cpunum].trace.file)
			fflush(global.cpuinfo[cpunum].trace.file);
	}
}



/***************************************************************************
    VARIABLE GETTERS/SETTERS
***************************************************************************/

/*-------------------------------------------------
    get_wpaddr - getter callback for the
    'wpaddr' symbol
-------------------------------------------------*/

static UINT64 get_wpaddr(UINT32 ref)
{
	return global.wpaddr;
}


/*-------------------------------------------------
    get_wpdata - getter callback for the
    'wpdata' symbol
-------------------------------------------------*/

static UINT64 get_wpdata(UINT32 ref)
{
	return global.wpdata;
}


/*-------------------------------------------------
    get_cycles - getter callback for the
    'cycles' symbol
-------------------------------------------------*/

static UINT64 get_cycles(UINT32 ref)
{
	return activecpu_get_icount();
}


/*-------------------------------------------------
    get_cpunum - getter callback for the
    'cpunum' symbol
-------------------------------------------------*/

static UINT64 get_cpunum(UINT32 ref)
{
	return cpu_getactivecpu();
}


/*-------------------------------------------------
    get_tempvar - getter callback for the
    'tempX' symbols
-------------------------------------------------*/

static UINT64 get_tempvar(UINT32 ref)
{
	return global.tempvar[ref];
}


/*-------------------------------------------------
    set_tempvar - setter callback for the
    'tempX' symbols
-------------------------------------------------*/

static void set_tempvar(UINT32 ref, UINT64 value)
{
	global.tempvar[ref] = value;
}


/*-------------------------------------------------
    get_logunmap - getter callback for the logumap
    symbols
-------------------------------------------------*/

static UINT64 get_logunmap(UINT32 ref)
{
	return memory_get_log_unmap(ref);
}


/*-------------------------------------------------
    get_beamx - get beam horizontal position
-------------------------------------------------*/

static UINT64 get_beamx(UINT32 ref)
{
	UINT64 ret = 0;
	const device_config *screen = device_list_find_by_index(Machine->config->devicelist, VIDEO_SCREEN, ref);

	if (screen != NULL)
		ret = video_screen_get_hpos(screen);

	return ret;
}


/*-------------------------------------------------
    get_beamy - get beam vertical position
-------------------------------------------------*/

static UINT64 get_beamy(UINT32 ref)
{
	UINT64 ret = 0;
	const device_config *screen = device_list_find_by_index(Machine->config->devicelist, VIDEO_SCREEN, ref);

	if (screen != NULL)
		ret = video_screen_get_vpos(screen);

	return ret;
}


/*-------------------------------------------------
    set_logunmap - setter callback for the logumap
    symbols
-------------------------------------------------*/

static void set_logunmap(UINT32 ref, UINT64 value)
{
	memory_set_log_unmap(ref, value ? 1 : 0);
}


/*-------------------------------------------------
    get_current_pc - getter callback for a CPU's
    current instruction pointer
-------------------------------------------------*/

static UINT64 get_current_pc(UINT32 ref)
{
	return activecpu_get_pc();
}


/*-------------------------------------------------
    get_cpu_reg - getter callback for a CPU's
    register symbols
-------------------------------------------------*/

static UINT64 get_cpu_reg(UINT32 ref)
{
	return activecpu_get_reg(ref);
}


/*-------------------------------------------------
    set_cpu_reg - setter callback for a CPU's
    register symbols
-------------------------------------------------*/

static void set_cpu_reg(UINT32 ref, UINT64 value)
{
	activecpu_set_reg(ref, value);
}