aboutsummaryrefslogtreecommitdiff
path: root/SrcShared/Hardware/EmCPU68K.cpp
blob: e3f00e2c92dad1aa1203be1dfb1b36cd5e4c81f9 (plain)
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
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
	Copyright (c) 2000-2001 Palm, Inc. or its subsidiaries.
	All rights reserved.

	This file is part of the Palm OS Emulator.

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
\* ===================================================================== */

#include "EmCommon.h"
#include "EmCPU68K.h"

#include "Byteswapping.h"		// Canonical
#include "DebugMgr.h"			// gExceptionAddress, gExceptionSize, gExceptionForRead
#include "EmBankROM.h"			// EmBankROM::GetMemoryStart
#include "EmEventPlayback.h"	// EmEventPlayback::ReplayingEvents
#include "EmHAL.h"				// EmHAL::GetInterruptLevel
#include "EmMemory.h"			// CEnableFullAccess
#include "EmMinimize.h"			// IsOn
#include "EmSession.h"			// HandleInstructionBreak
#include "Logging.h"			// LogAppendMsg
#include "MetaMemory.h"			// IsCPUBreak
#include "Platform.h"			// GetMilliseconds
#include "SessionFile.h"		// WriteDBallRegs, etc.
#include "StringData.h"			// kExceptionNames
#include "UAE.h"				// cpuop_func, etc.

#include <algorithm>			// find

#if __profile__
#include <Profiler.h>
#endif

#if defined (macintosh) && defined (_DEBUG) && 0
	#define HAS_DEAD_MANS_SWITCH	1
#else
	#define HAS_DEAD_MANS_SWITCH	0
#endif

// !!! Why is this here?  Shouldn't it be in Profiling.h?
#if HAS_PROFILING
perfRec perftbl[65536];
#endif

// Define our own flags for regs.spcflag.  Please do not let these
// overlap with UAE-defined flags (should not fall below 0x0002000)
// and avoid using the high bit just for safety.

#define SPCFLAG_END_OF_CYCLE	(0x40000000)


// Data needed by UAE.

int	areg_byteinc[] = { 1,1,1,1,1,1,1,2 };	// (normally in newcpu.c)
int	imm8_table[] = { 8,1,2,3,4,5,6,7 }; 	// (normally in newcpu.c)

int	movem_index1[256];						// (normally in newcpu.c)
int	movem_index2[256];						// (normally in newcpu.c)
int	movem_next[256];						// (normally in newcpu.c)

cpuop_func*	cpufunctbl[65536];				// (normally in newcpu.c)

uint16	last_op_for_exception_3;			/* Opcode of faulting instruction */
emuptr	last_addr_for_exception_3;			/* PC at fault time */
emuptr	last_fault_for_exception_3; 		/* Address that generated the exception */

struct regstruct	regs;					// (normally in newcpu.c)
struct flag_struct	regflags;				// (normally in support.c)


// These variables should strictly be in a sub-system that implements
// the stack overflow checking, etc.  However, for performance reasons,
// we need to expose them to UAE (see the CHECK_STACK_POINTER_ASSIGNMENT,
// et al macros), so define them here.
//
// Similar comments for the CheckKernelStack function.

uae_u32	gStackHigh;
uae_u32	gStackLowWarn;
uae_u32	gStackLow;
uae_u32	gKernelStackOverflowed;


// Definitions of the stack frames used in EmCPU68K::ProcessException.

#include "PalmPack.h"

struct ExceptionStackFrame1
{
	uint16 statusRegister;
	uint32 programCounter;
};

struct ExceptionStackFrame2
{
	uint16 functionCode;
	uint32 accessAddress;
	uint16 instructionRegister;
	uint16 statusRegister;
	uint32 programCounter;
};

#include "PalmPackPop.h"


EmCPU68K*	gCPU68K;


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::Cycle
// ---------------------------------------------------------------------------

// this guy is a macro instead of an inline function so that "counter" can
// be declared as a variable local to the calling function.  The resulting
// code can be more efficient if "counter" can be cached in a register
// instead of being a static or global variable.

#define CYCLE(sleeping)															\
{																				\
	/* Don't do anything if we're in the middle of an ATrap call.  We don't */	\
	/* need interrupts firing or tmr counters incrementing. */					\
																				\
	EmAssert (session);															\
	if (!session->IsNested ())													\
	{																			\
		/* Perform CPU-specific idling. */										\
																				\
		EmHAL::Cycle (sleeping);												\
																				\
		/* Perform expensive operations. */										\
																				\
		if (sleeping || ((++counter & 0x7FFF) == 0))							\
		{																		\
			this->CycleSlowly (sleeping);										\
		}																		\
	}																			\
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::EmCPU68K
// ---------------------------------------------------------------------------

EmCPU68K::EmCPU68K (EmSession* session) :
	EmCPU (session),
	fLastTraceAddress (EmMemNULL),
	fCycleCount (0),
//	fExceptionHandlers (),
	fHookJSR (),
	fHookJSR_Ind (),
	fHookLINK (),
	fHookRTE (),
	fHookRTS (),
	fHookNewPC (),
	fHookNewSP ()
#if REGISTER_HISTORY
	, fRegHistoryIndex (0)
//	, fRegHistory ()
#endif
#if EXCEPTION_HISTORY
	, fExceptionHistoryIndex (0)
//	, fExceptionHistory ()
#endif
{
	this->InitializeUAETables ();

	EmAssert (gCPU68K == NULL);
	gCPU68K = this;
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::~EmCPU68K
// ---------------------------------------------------------------------------

EmCPU68K::~EmCPU68K (void)
{
	EmAssert (gCPU68K == this);
	gCPU68K = NULL;
}


#pragma mark -

// ---------------------------------------------------------------------------
//		¥ EmCPU68K::Reset
// ---------------------------------------------------------------------------

void EmCPU68K::Reset (Bool hardwareReset)
{
	fLastTraceAddress		= EmMemNULL;
	fCycleCount				= 0;

#if REGISTER_HISTORY
	fRegHistoryIndex		= 0;
#endif

#if EXCEPTION_HISTORY
	fExceptionHistoryIndex	= 0;
#endif

	gStackHigh				= EmMemEOM;
	gStackLowWarn			= EmMemNULL;
	gStackLow				= EmMemNULL;
	gKernelStackOverflowed	= false;

	if (hardwareReset)
	{
		// (taken from m68k_reset in newcpu.c)

		// !!! I think that we really need to emulate ROM appearing at NULL.
		// That would break our dependency on EmBankROM.

		emuptr romStart = EmBankROM::GetMemoryStart ();
		m68k_areg (regs, 7) = EmMemGet32 (romStart);
		m68k_setpc (EmMemGet32 (romStart + 4));

		// Note, on the 68K, the t0 and m flags must always be zero.

		regs.prefetch	= 0x0000;
		regs.kick_mask	= 0xF80000;	// (not a 68K register)
		regs.s			= 1;		// supervisor/user state
		regs.m			= 0;		// master/interrupt state
		regs.stopped	= 0;		// (not a 68K register)
		regs.t1 		= 0;		// 1 = trace on any instruction
		regs.t0 		= 0;		// 1 = trace on change of flow
		SET_ZFLG (0);
		SET_XFLG (0);
		SET_CFLG (0);
		SET_VFLG (0);
		SET_NFLG (0);
		regs.spcflags	= 0;		// (not a 68K register)
		regs.intmask	= 7;		// disable all interrupts
		regs.vbr = regs.sfc = regs.dfc = 0;
		regs.fpcr = regs.fpsr = regs.fpiar = 0;
	}

	Memory::CheckNewPC (m68k_getpc ());
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::Save
// ---------------------------------------------------------------------------

void EmCPU68K::Save (SessionFile& f)
{
	// Write out the CPU Registers

	regstruct	tempRegs;
	this->GetRegisters (tempRegs);

	Canonical (tempRegs);
	f.WriteDBallRegs (tempRegs);
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::Load
// ---------------------------------------------------------------------------

void EmCPU68K::Load (SessionFile& f)
{
	// Read in the CPU Registers.

	regstruct	tempRegs;
	f.ReadDBallRegs (tempRegs);

	Canonical (tempRegs);
	this->SetRegisters (tempRegs);
}


#pragma mark -

// ---------------------------------------------------------------------------
//		¥ EmCPU68K::Execute
// ---------------------------------------------------------------------------

void EmCPU68K::Execute (void)
{
	// This function is the bottleneck for all 68K emulation.  It's
	// important that it run as quickly as possible.  To that end,
	// fine tune register allocation as much as we can by hand.

#if defined(__powerc) || defined(powerc) || \
	defined(__powerpc) || defined(powerpc) || \
	defined(__ppc__) || defined(ppc)

	register int			counter			= 0;
	register cpuop_func**	functable		= cpufunctbl;

	register uint8**		pc_p_p			= &regs.pc_p;
	register uint8**		pc_meta_oldp_p	= &regs.pc_meta_oldp;
	register uint8**		pc_oldp_p		= &regs.pc_oldp;
	register uint32*		spcflags_p		= &regs.spcflags;
	register EmSession*		session			= fSession;

	#define pc_p			(*pc_p_p)
	#define pc_meta_oldp	(*pc_meta_oldp_p)
	#define pc_oldp			(*pc_oldp_p)
	#define spcflags		(*spcflags_p)

#elif defined(_MSC_VER) && defined(_M_IX86)

	register int			counter			= 0;
	register cpuop_func**	functable		= cpufunctbl;

	#define pc_p			(regs.pc_p)
	#define pc_meta_oldp	(regs.pc_meta_oldp)
	#define pc_oldp			(regs.pc_oldp)
	#define spcflags		(regs.spcflags)
	#define session			(fSession)

#else

	register int			counter			= 0;
	register cpuop_func**	functable		= cpufunctbl;

	#define pc_p			(regs.pc_p)
	#define pc_meta_oldp	(regs.pc_meta_oldp)
	#define pc_oldp			(regs.pc_oldp)
	#define spcflags		(regs.spcflags)
	#define session			(fSession)

#endif

#if HAS_PROFILING_DEBUG
	UInt64	readCyclesSaved		= 0;
	UInt64	writeCyclesSaved	= 0;
	UInt64	clockCyclesSaved	= 0;
#endif

#if HAS_DEAD_MANS_SWITCH
	// -----------------------------------------------------------------------
	// Put in a little dead-man's switch. If this function doesn't exit for a
	// long time, let us get into the debugger.
	// -----------------------------------------------------------------------

	uint32	deadManNow;
	uint32	deadManStart = Platform::GetMilliseconds ();
#endif

	// -----------------------------------------------------------------------
	// Check for the stopped flag before entering the "execute an opcode"
	// section.  It could be that we last exited the loop while still in stop
	// mode, and we need to wind our way back down to that spot.
	// -----------------------------------------------------------------------

	if ((spcflags & SPCFLAG_STOP) != 0)
		goto StoppedLoop;

	while (1)
	{
#if REGISTER_HISTORY
		// -----------------------------------------------------------------------
		// Save the registers for the post-mortem, but don't record the
		// instructions we generate when calling the ROM as a subroutine.  We want
		// those to be as transparent as possible.	In particular, we don't want
		// any functions that we call as part of figuring out why a problem
		// occurred to knock the problem-causing registers off of our array.
		// -----------------------------------------------------------------------

//		if (!session->IsNested ())
		{
			++fRegHistoryIndex;
			fRegHistory[fRegHistoryIndex & (kRegHistorySize - 1)] = regs;
		}
#endif

#if HAS_DEAD_MANS_SWITCH
		// -----------------------------------------------------------------------
		// Put in a little dead-man's switch. If this function doesn't exit for a
		// long time, let us get into the debugger.
		// -----------------------------------------------------------------------

		deadManNow = Platform::GetMilliseconds ();
		if ((deadManNow - deadManStart) > 5000)
		{
			Platform::Debugger ();
		}
#endif

		// -----------------------------------------------------------------------
		// See if we need to halt CPU execution at this location.  We could need
		// to do this for several reasons, including hitting soft breakpoints or
		// needing to execute tailpatches.
		// -----------------------------------------------------------------------

		if (MetaMemory::IsCPUBreak (pc_meta_oldp + (pc_p - pc_oldp)))
		{
			EmAssert (session);
			session->HandleInstructionBreak ();
		}

#if HAS_PROFILING
		emuptr	pcStart;
		pcStart = m68k_getpc ();

		if (gProfilingEnabled)
		{
			// If detailed, log instruction here

			if (gProfilingDetailed)
			{
				ProfileInstructionEnter (pcStart);
			}

#if HAS_PROFILING_DEBUG
			readCyclesSaved		= gReadCycles;
			writeCyclesSaved	= gWriteCycles;
			clockCyclesSaved	= gClockCycles;
#endif

			// Turn gProfilingCounted on here so the opcode fetch below is counted.

			gProfilingCounted = true;
		}
#endif

		// =======================================================================
		// Execute the opcode.
		// -----------------------------------------------------------------------
		//	Interestingly, defining "opcode" as an EmOpcode68K (which is a uint32)
		//	instead of a uint16 (which is all you need to hold the opcode), makes
		//	a big difference in performance (at least, on Intel).  The former
		//	generates:
		//
		//		mov		edx, DWORD PTR _regs+92
		//		xor		eax, eax
		//		mov		ax, WORD PTR [edx]
		//		push	eax
		//		call	DWORD PTR [edi+eax*4]
		//
		//	while the latter generates:
		//
		//		mov		edx, DWORD PTR _regs+92
		//		mov		ax, WORD PTR [edx]
		//		and		eax, 65535		; 0000ffffH
		//		push	eax
		//		call	DWORD PTR [edi+eax*4]
		//
		//	This results in an amazing 5% performance difference.  What makes it
		//	even more amazing is that similar optimizations elsewhere don't seem
		//	to help.  For instance, in EmCPU68K::Cycle, there's the expression:
		//
		//		((++gCounter) & 0x7FFF == 0)
		//
		//	Changing gCounter to a uint16, or changing the expression to:
		//
		//		((uint16) ++gCounter) == 0)
		//
		//	both result in the period doubling, but also in better code
		//	generation.  However, neither gives better performance.  Additionally,
		//	I tried like heck to optimize the call to IsCPUBreak above.  I added
		//	a preflight check that would skip 70% of the calls to IsCPUBreak (as
		//	well as the calculation of the parameter passed to it) and re-
		//	organized the code so that branch prediction would work.  However,
		//	none of that resulted in better performance.
		// -----------------------------------------------------------------------
		EmOpcode68K	opcode;
	//	opcode = get_iword (0);
#if HAS_PROFILING
		if (gProfilingEnabled)
			get_word(regs.pc + ((char*) pc_p - (char*) pc_oldp));
#endif
		opcode = do_get_mem_word (pc_p);
		fCycleCount += (functable[opcode]) (opcode);
		// =======================================================================

#if HAS_PROFILING
		if (gProfilingEnabled)
		{
			// Add in the extra time taken to execute the instruction.

			ProfileIncrementClock (perftbl[opcode].extraCycles);

			// Detail (instruction level) profiling support

			if (gProfilingDetailed)
			{
				ProfileInstructionExit (pcStart);
			}

#if HAS_PROFILING_DEBUG
			// Validity check on EmMemory stuff.

			Boolean tryAgain = false;

			if (perftbl[opcode].readCycles != 0xFF &&
				gReadCycles - readCyclesSaved != perftbl[opcode].readCycles)
			{
				gReadMismatch += gReadCycles - readCyclesSaved - perftbl[opcode].readCycles;
			}

			if (perftbl[opcode].writeCycles != 0xFF &&
				gWriteCycles - writeCyclesSaved != perftbl[opcode].writeCycles)
			{
				gWriteMismatch += gWriteCycles - writeCyclesSaved - perftbl[opcode].writeCycles;
			}

			if (tryAgain)
			{
				(functable[opcode]) (opcode);
			}
#endif
		}
#endif

		// Perform periodic tasks.

		CYCLE (false);

StoppedLoop:

		// -----------------------------------------------------------------------
		// Handle special conditions.  NB: the code reached by calling
		// EmCPU68K::ExecuteSpecial used to be inline in this function.  Moving it
		// out (thus simplifying both EmCPU68K::Execute and EmCPU68K::ExecuteSpecial)
		// sped up the CPU loop by 9%!
		// -----------------------------------------------------------------------

		if (spcflags)
		{
			if (this->ExecuteSpecial ())
				break;
		}

#if HAS_PROFILING
		gProfilingCounted = false;
#endif
	}	// while (1)
	
#undef pc_p
#undef pc_meta_oldp
#undef pc_oldp
#undef spcflags
#undef session
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::ExecuteSimple
// ---------------------------------------------------------------------------

/*
	This is the same loop with the following removed:
	
	* Comments
	* Profiling code
	* Profiling debugging code
	* Dead mans's check on Mac
	* Register history recording

	It's essentially what we get in Release/Non-profile builds.
*/

#if 0

void EmCPU68K::ExecuteSimple (void)
{
	if ((regs.spcflags & SPCFLAG_STOP) != 0)
		goto StoppedLoop;

	while (1)
	{
		if (MetaMemory::IsCPUBreak (regs.pc_meta_oldp + (regs.pc_p - regs.pc_oldp)))
		{
			this->HandleInstructionBreak ();
		}

		EmOpcode68K	opcode;
		opcode = get_iword (0);
		fCycleCount += cpufunctbl[opcode] (opcode);

		this->Cycle (false);

StoppedLoop:
		if (regs.spcflags)
		{
			if (this->ExecuteSpecial ())
				break;
		}
	}
}

#endif


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::ExecuteSpecial
// ---------------------------------------------------------------------------

Bool EmCPU68K::ExecuteSpecial (void)
{
	// If we're making subroutine calls, then all we process are requests
	// to break from the CPU loop.  We don't want interrupts, tracing, etc.
	// getting in the way.

	EmAssert (fSession);
	if (fSession->IsNested () != 0)
	{
		return this->CheckForBreak ();
	}

	// Check for Reset first.  If this is set, don't do anything else.

	if ((regs.spcflags & SPCFLAG_END_OF_CYCLE))
	{		
		if (fSession->ExecuteSpecial (true))
			return true;
	}

	// Execute UAE spcflags-handling code (from do_specialties in newcpu.c).

	// If we're tracing, execute the trace vector.
	//
	// The check for SPCFLAG_STOP was added in Poser.  It's needed
	// if we're re-entering ExecuteStopped loop on the Mac after
	// exiting in order to handle events.

	if ((regs.spcflags & SPCFLAG_DOTRACE) && !(regs.spcflags & SPCFLAG_STOP))
	{
		this->ProcessException (kException_Trace);
	}

	if (regs.spcflags & SPCFLAG_STOP)
	{
		if (this->ExecuteStoppedLoop ())
		{
			regs.spcflags &= ~SPCFLAG_BRK;
			return true;
		}
	}

	// Do trace-mode stuff (do_trace from newcpu.c does more,
	// but it's only needed for CPU_LEVEL > 0)

	if (regs.spcflags & SPCFLAG_TRACE)
	{
		if (regs.t1)
		{
			fLastTraceAddress = m68k_getpc ();
			regs.spcflags &= ~SPCFLAG_TRACE;
			regs.spcflags |= SPCFLAG_DOTRACE;
		}
	}

	if ((regs.spcflags & SPCFLAG_DOINT) && !gKernelStackOverflowed)
	{
		int32 interruptLevel = EmHAL::GetInterruptLevel ();
		regs.spcflags &= ~SPCFLAG_DOINT;	// was ~(SPCFLAG_INT | SPCFLAG_DOINT) in Greg and Craig, but the latest UAE has this
		if ((interruptLevel != -1) && (interruptLevel > regs.intmask))
		{
			this->ProcessInterrupt (interruptLevel);
			regs.stopped = 0;
		}
	}

	if (regs.spcflags & SPCFLAG_INT)
	{
		regs.spcflags &= ~SPCFLAG_INT;
		regs.spcflags |= SPCFLAG_DOINT;
	}

	// Check for Poser end-of-cycle operations.  This is inserted
	// before the standard UAE check of the SPCFLAG_BRK flag.

	if ((regs.spcflags & SPCFLAG_END_OF_CYCLE))
	{
		regs.spcflags &= ~SPCFLAG_END_OF_CYCLE;
		
		if (fSession->ExecuteSpecial (false))
			return true;
	}

	return this->CheckForBreak ();
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::ExecuteStoppedLoop
// ---------------------------------------------------------------------------

Bool EmCPU68K::ExecuteStoppedLoop (void)
{
	register EmSession*		session			= fSession;

	EmAssert (session);
	EmAssert (!session->IsNested ());
	EmAssert (regs.intmask < 7);

	// If we're running Gremlins and the device goes to sleep (as opposed
	// to just dozing), pretend the user clicked on the Power button).

	if (EmHAL::GetAsleep () && !session->HasButtonEvent ())
	{
		// Turns Hordes off for a moment, as pen events are rejected
		// when it's running!

		Bool	hordesWasOn = false;
		Bool	playbackWasOn = false;
		Bool	minimizationWasOn = false;

		if (Hordes::IsOn ())
		{
			Hordes::TurnOn (false);
			hordesWasOn = true;
		}

		if (EmEventPlayback::ReplayingEvents ())
		{
			EmEventPlayback::ReplayEvents (false);
			playbackWasOn = true;
		}

		if (EmMinimize::IsOn ())
		{
			EmMinimize::TurnOn (false);
			minimizationWasOn = true;
		}

		/*
			When posting the button down event for the Power button, include
			a flag that causes the event to be posted to the hardware NOW.
			Normally, EmSession employs a throttling mechanism that prevents
			button events from showing up in the hardware too quickly.  This
			throttling allows the time for the OS to wakeup and respond to
			the hardware interrupt.
			
			However, the throttling also leads to an incredibly subtle bug
			if it's used unconditionally.  Consider the following sequence
			of events:
			
			*	Gremlins creates a vchrAutoOff event
			*	The system responds to it and goes to sleep.
			*	Poser notices the device is asleep while running a Gremlin,
				and posts Power button down/up events.
			*	It's been more than 100msec since the last button event, so
				the button event is given to the hardware immediately.
			*	The system wakes up, notices that the Power key is down, and
				sets the flag that says it should stay awake.
			*	Gremlins starts posting events again.
			*	Within 100msecs, Gremlins generates another vchrAutoOff event.
			*	The devices goes to sleep again.  Somewhere in the process of
				dealing with the  vchrAutoOff event, the Power-button up event
				is popped off and handled.
			*	Poser posts Power button down/up events again.
			*	Because it's been within 100msecs of the last time a button
				event was popped off the queue, these events are held in the
				queue for a while.
			*	An RTC interrupt occurs.
			*	The device wakes up, notices that the Power is not down, and
				does NOT set the bit that says to stay awake.
			*	The devices goes back to sleep after processing the RTC
				interrupt.  Note that there are still two Power-button events
				in EmSession's button queue.
			*	Poser notices that the device is off, and posts *another*
				pair of Power-button down/up events.
			*	At this point, Poser is in a weird state, where there are too
				many events in the queue.  In fact, the situation will get
				worse, with more and more events piling up and the system
				deals with turning itself on and off in response to the pending
				button events.  The result is that the user sees the device
				flashing on and off.

			In order to avoid this problem, have the Power button posted to
			the hardware registers immediately, before an interrupt can get
			in the way.
			
			Note that there is another potential bug here.  We avoid it for
			now, but be careful not to introduce it later.  By passing "true"
			to PostButtonEvent, we can make the button event available to
			the hardware immediately.  However, the event is not forced onto
			the hardware.  Instead, the hardware polls for any pending button
			events at some reasonable rate.  That is, even if EmSession is
			prepared to say that there is a pending button event, EmCPU may
			not pick up on it until some time later based on its checking
			frequency.
			
			We avoid this problem for now because the hardware checks for
			button events in EmHAL::CycleSlowly, which is called
			unconditionally when the processor is moved out of the "stopped"
			state.
		*/

		if (hordesWasOn || playbackWasOn || minimizationWasOn)
		{
			EmButtonEvent	event (kElement_PowerButton, true);
			session->PostButtonEvent (event, true);

			event.fButtonIsDown = false;
			session->PostButtonEvent (event);
		}

		if (hordesWasOn)
		{
			Hordes::TurnOn (true);
		}

		if (playbackWasOn)
		{
			EmEventPlayback::ReplayEvents (true);
		}

		if (minimizationWasOn)
		{
			EmMinimize::TurnOn (true);
		}
	}

	int	counter = 0;

	// While the CPU is stopped (because a STOP instruction was
	// executed) do some idle tasks.

#if HAS_DEAD_MANS_SWITCH
	// -----------------------------------------------------------------------
	// Put in a little dead-man's switch. If this function doesn't
	// exit for a long time, let us get into the debugger.
	// -----------------------------------------------------------------------

	uint32 deadManStart = Platform::GetMilliseconds ();
#endif

	do {
#if HAS_DEAD_MANS_SWITCH
		// -----------------------------------------------------------------------
		// Put in a little dead-man's switch. If this function doesn't
		// exit for a long time, let us get into the debugger.
		// -----------------------------------------------------------------------

		uint32 deadManNow = Platform::GetMilliseconds ();
		if ((deadManNow - deadManStart) > 5000)
		{
			Platform::Debugger ();
		}
#endif

		// -----------------------------------------------------------------------
		// Slow down processing so that the timer used
		// to increment the tickcount doesn't run too quickly.
		// -----------------------------------------------------------------------

#if __profile__
	short	oldStatus = ProfilerGetStatus ();
	ProfilerSetStatus (false);
#endif

		Platform::Delay ();

#if __profile__
	ProfilerSetStatus (oldStatus);
#endif

		// Perform periodic tasks.

		CYCLE (true);

		// Process an interrupt (see if it's time to wake up).

		if (regs.spcflags & (SPCFLAG_INT | SPCFLAG_DOINT))
		{
			int32 interruptLevel = EmHAL::GetInterruptLevel ();

			regs.spcflags &= ~(SPCFLAG_INT | SPCFLAG_DOINT);

			if ((interruptLevel != -1) && (interruptLevel > regs.intmask))
			{
				this->ProcessInterrupt (interruptLevel);
				regs.stopped = 0;
				regs.spcflags &= ~SPCFLAG_STOP;
			}
		}

		if (this->CheckForBreak ())
		{
			return true;
		}
	} while (regs.spcflags & SPCFLAG_STOP);

	return false;
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::CycleSlowly
// ---------------------------------------------------------------------------

void EmCPU68K::CycleSlowly (Bool sleeping)
{
	EmHAL::CycleSlowly (sleeping);

	// Do some platform-specific stuff.

	Platform::CycleSlowly ();

#if HAS_OMNI_THREAD
	// Check to see if some external thread has asked us to quit.

	EmAssert (fSession);
	omni_mutex_lock	lock (fSession->fSharedLock);

	if (fSession->fSuspendState.fAllCounters)
	{
		this->CheckAfterCycle ();
	}
#endif
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::CheckAfterCycle
// ---------------------------------------------------------------------------

void EmCPU68K::CheckAfterCycle (void)
{
	regs.spcflags |= SPCFLAG_END_OF_CYCLE;
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::GetPC
// ---------------------------------------------------------------------------

emuptr EmCPU68K::GetPC (void)
{
//	return this->GetRegister (e68KRegID_PC);
	return m68k_getpc ();
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::GetSP
// ---------------------------------------------------------------------------

emuptr EmCPU68K::GetSP (void)
{
//	return this->GetRegister (e68KRegID_SSP);
	return m68k_areg (regs, 7);
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::GetRegister
// ---------------------------------------------------------------------------

uint32 EmCPU68K::GetRegister (int index)
{
	uint32	result = 0;

	switch (index)
	{
		case e68KRegID_D0:	result = m68k_dreg (regs, 0);	break;
		case e68KRegID_D1:	result = m68k_dreg (regs, 1);	break;
		case e68KRegID_D2:	result = m68k_dreg (regs, 2);	break;
		case e68KRegID_D3:	result = m68k_dreg (regs, 3);	break;
		case e68KRegID_D4:	result = m68k_dreg (regs, 4);	break;
		case e68KRegID_D5:	result = m68k_dreg (regs, 5);	break;
		case e68KRegID_D6:	result = m68k_dreg (regs, 6);	break;
		case e68KRegID_D7:	result = m68k_dreg (regs, 7);	break;
		case e68KRegID_A0:	result = m68k_areg (regs, 0);	break;
		case e68KRegID_A1:	result = m68k_areg (regs, 1);	break;
		case e68KRegID_A2:	result = m68k_areg (regs, 2);	break;
		case e68KRegID_A3:	result = m68k_areg (regs, 3);	break;
		case e68KRegID_A4:	result = m68k_areg (regs, 4);	break;
		case e68KRegID_A5:	result = m68k_areg (regs, 5);	break;
		case e68KRegID_A6:	result = m68k_areg (regs, 6);	break;
		case e68KRegID_USP:	result = m68k_areg (regs, 7);	break;
		case e68KRegID_SSP:	result = m68k_areg (regs, 7);	break;
		case e68KRegID_PC:	result = m68k_getpc ();			break;
		case e68KRegID_SR:
			this->UpdateSRFromRegisters ();
			result = regs.sr;
			break;

		default:
			EmAssert (false);
	}

	return result;
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::SetPC
// ---------------------------------------------------------------------------

void EmCPU68K::SetPC (emuptr newPC)
{
	this->SetRegister (e68KRegID_PC, newPC);
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::SetSP
// ---------------------------------------------------------------------------

void EmCPU68K::SetSP (emuptr newPC)
{
	this->SetRegister (e68KRegID_SSP, newPC);
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::SetRegister
// ---------------------------------------------------------------------------

void EmCPU68K::SetRegister (int index, uint32 val)
{
	switch (index)
	{
		case e68KRegID_D0:	m68k_dreg (regs, 0) = val;	break;
		case e68KRegID_D1:	m68k_dreg (regs, 1) = val;	break;
		case e68KRegID_D2:	m68k_dreg (regs, 2) = val;	break;
		case e68KRegID_D3:	m68k_dreg (regs, 3) = val;	break;
		case e68KRegID_D4:	m68k_dreg (regs, 4) = val;	break;
		case e68KRegID_D5:	m68k_dreg (regs, 5) = val;	break;
		case e68KRegID_D6:	m68k_dreg (regs, 6) = val;	break;
		case e68KRegID_D7:	m68k_dreg (regs, 7) = val;	break;
		case e68KRegID_A0:	m68k_areg (regs, 0) = val;	break;
		case e68KRegID_A1:	m68k_areg (regs, 1) = val;	break;
		case e68KRegID_A2:	m68k_areg (regs, 2) = val;	break;
		case e68KRegID_A3:	m68k_areg (regs, 3) = val;	break;
		case e68KRegID_A4:	m68k_areg (regs, 4) = val;	break;
		case e68KRegID_A5:	m68k_areg (regs, 5) = val;	break;
		case e68KRegID_A6:	m68k_areg (regs, 6) = val;	break;
		case e68KRegID_USP:	m68k_areg (regs, 7) = val;	break;
		case e68KRegID_SSP:	m68k_areg (regs, 7) = val;	break;
		case e68KRegID_PC:	m68k_setpc (val);			break;
		case e68KRegID_SR:
			regs.sr = val;
			this->UpdateRegistersFromSR ();
			break;

		default:
			EmAssert (false);
	}
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::Stopped
// ---------------------------------------------------------------------------
// Return whether or not the CPU itself is halted.  This is seperate from
// whether or not the session (that is, the thread emulating the CPU) is
// halted.

Bool EmCPU68K::Stopped (void)
{
	return regs.stopped;
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::CheckForBreak
// ---------------------------------------------------------------------------
// Check to see if the conditions tell us to break from the CPU Execute loop.

Bool EmCPU68K::CheckForBreak (void)
{
	if ((regs.spcflags & SPCFLAG_BRK) != 0)
	{
		regs.spcflags &= ~SPCFLAG_BRK;
		return true;
	}

	EmAssert (fSession);
	return fSession->CheckForBreak ();
}


#pragma mark -

// ---------------------------------------------------------------------------
//		¥ EmCPU68K::ProcessInterrupt
// ---------------------------------------------------------------------------

void EmCPU68K::ProcessInterrupt (int32 interrupt)
{
	this->ProcessException ((ExceptionNumber) (EmHAL::GetInterruptBase () + interrupt));

	regs.intmask = interrupt;
	regs.spcflags |= SPCFLAG_INT;	// Check for higher-level interrupts
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::ProcessException
// ---------------------------------------------------------------------------

void EmCPU68K::ProcessException (ExceptionNumber exception)
{
	// Make sure the Status Register is up-to-date.

	this->UpdateSRFromRegisters ();

#if EXCEPTION_HISTORY
	// Save the exception for the post-mortem

	fExceptionHistoryIndex++;

	long	index = fExceptionHistoryIndex & (kExceptionHistorySize - 1);
	fExceptionHistory[index].name = kExceptionNames[exception];

	fExceptionHistory[index].pc = m68k_getpc ();
	fExceptionHistory[index].sp = m68k_areg (regs, 7);
#endif

#if HAS_PROFILING
	// Don't count cycles spent in exception handlers against functions.

	// Get and remember the current PC here.  This is important to make the
	// profiling routines come out right.  If Poser decides to completely
	// handle the exception and profiling is on, then the handler will return
	// true (setting "handled" to true), and adjust the program counter.
	// Since "handled" is true, Poser calls ProfileInterruptExit, this time
	// with the current PC.  ProfileInterruptExit sees the adjusted PC, and
	// determines that the funky TRAP $F thing is going on, resulting in it
	// pushing a new function-call record on its stack.  However, since Poser
	// has completely handled the interrupt, we don't want that to happen.
	// All we want is to remove the entry recorded by ProfileInterruptEnter.
	// By saving the current PC value here and passing it to ProfileInterruptExit
	// later, we achieve that affect.  ProfileInterruptExit will record an
	// "interrupt mismatch", but I can live with that...

	emuptr curpc = m68k_getpc ();

	if (gProfilingEnabled)
	{
		ProfileInterruptEnter (exception, curpc);
	}
#endif

	// Let any custom exception handler have a go at it.  If it returns
	// true, it handled it completely, and we don't have anything else to do.
	//
	// By "handled it completely", we could mean one of two things.  (1)
	// that Poser completely replaced the system function and the ROM
	// function was skipped, or (2) Poser dealt with the trap dispatch
	// process, setting the PC to the function entry point.
	//
	// If profiling is on, (2) never occurs because we want to profile the
	// trap dispatcher itself.  Therefore, only (1) is possible.

	Bool							handled = false;
	Hook68KExceptionList&			fns = fExceptionHandlers[exception];
	Hook68KExceptionList::iterator	iter = fns.begin ();
	while (iter != fns.end ())
	{
		if ((*iter) (exception))
		{
			handled = true;
		}

		++iter;
	}

	if (handled)
	{
#if HAS_PROFILING
		if (gProfilingEnabled)
		{
			ProfileInterruptExit (curpc);
		}
#endif
		return;
	}

	// The following is vaguely modelled after Exception() from newcpu.c
	// (The call to MakeSR appears at the start of this method).

	// If not in supervisor mode, set the usp, restore A7 from the isp,
	// and transfer to supervisor mode.

	if (!regs.s)
	{
		regs.usp = m68k_areg (regs, 7);
		m68k_areg (regs, 7) = regs.isp;
		regs.s = 1;
	}

	// Set up the stack frame.
	// !!! If we're handling a trace exception, I think that fLastTraceAddress
	// comes into play here instead of m68k_getpc.
	// !!! Manage this with EmPalmStructs...

	if (exception == kException_BusErr || exception == kException_AddressErr)
	{
		COMPILE_TIME_ASSERT (sizeof (ExceptionStackFrame2) == 14);
		m68k_areg (regs, 7) -= sizeof (ExceptionStackFrame2);
		CHECK_STACK_POINTER_DECREMENT ();

		emuptr frame = m68k_areg (regs, 7);

		// Eh...Palm OS doesn't use these 3 anyway...
		EmMemPut16 (frame + offsetof (ExceptionStackFrame2, functionCode), 0);
		EmMemPut32 (frame + offsetof (ExceptionStackFrame2, accessAddress), 0);
		EmMemPut16 (frame + offsetof (ExceptionStackFrame2, instructionRegister), 0);

		EmMemPut16 (frame + offsetof (ExceptionStackFrame2, statusRegister), regs.sr);
		EmMemPut32 (frame + offsetof (ExceptionStackFrame2, programCounter), m68k_getpc ());
	}
	else
	{
		COMPILE_TIME_ASSERT (sizeof (ExceptionStackFrame1) == 6);
		m68k_areg (regs, 7) -= sizeof (ExceptionStackFrame1);
		CHECK_STACK_POINTER_DECREMENT ();

		emuptr frame = m68k_areg (regs, 7);

		EmMemPut16 (frame + offsetof (ExceptionStackFrame1, statusRegister), regs.sr);
		EmMemPut32 (frame + offsetof (ExceptionStackFrame1, programCounter), m68k_getpc ());
	}

	emuptr newpc;
	{
		CEnableFullAccess	munge;	// Remove blocks on memory access.

		// Get the exception handler address.
		newpc = EmMemGet32 (regs.vbr + 4 * exception);
	}

	// Check the exception handler address and jam it into the PC.

	this->CheckNewPC (newpc);
	m68k_setpc (newpc);

	// Turn tracing off.

	regs.t1 = regs.t0 = regs.m = 0;
	regs.spcflags &= ~(SPCFLAG_TRACE | SPCFLAG_DOTRACE);
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::ProcessIllegalInstruction
// ---------------------------------------------------------------------------

void EmCPU68K::ProcessIllegalInstruction (EmOpcode68K opcode)
{
	// This function is loosely based on op_illg in newcpu.c

	// Process an FTrap.

	if ((opcode & 0xF000) == 0xF000)
	{
		this->ProcessException (kException_FTrap);
	}

	// Process an ATrap.

	else if ((opcode & 0xF000) == 0xA000)
	{
		this->ProcessException (kException_ATrap);
	}

	// Process all other opcodes.

	else
	{
		this->ProcessException (kException_IllegalInstr);
	}
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::ProcessJSR
// ---------------------------------------------------------------------------

int EmCPU68K::ProcessJSR (emuptr oldPC, emuptr dest)
{
	int	handled = false;

	Hook68KJSRList::iterator	iter = fHookJSR.begin ();
	while (iter != fHookJSR.end ())
	{
		if ((*iter) (oldPC, dest))
		{
			handled = true;
		}

		++iter;
	}

	return handled;
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::ProcessJSR_Ind
// ---------------------------------------------------------------------------

int EmCPU68K::ProcessJSR_Ind (emuptr oldPC, emuptr dest)
{
	int	handled = false;

	Hook68KJSR_IndList::iterator	iter = fHookJSR_Ind.begin ();
	while (iter != fHookJSR_Ind.end ())
	{
		if ((*iter) (oldPC, dest))
		{
			handled = true;
		}

		++iter;
	}

	return handled;
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::ProcessLINK
// ---------------------------------------------------------------------------

void EmCPU68K::ProcessLINK (int linkSize)
{
	Hook68KLINKList::iterator	iter = fHookLINK.begin ();
	while (iter != fHookLINK.end ())
	{
		(*iter) (linkSize);

		++iter;
	}
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::ProcessRTE
// ---------------------------------------------------------------------------

int EmCPU68K::ProcessRTE (emuptr dest)
{
	int	handled = false;

	Hook68KRTEList::iterator	iter = fHookRTE.begin ();
	while (iter != fHookRTE.end ())
	{
		if ((*iter) (dest))
		{
			handled = true;
		}

		++iter;
	}

	return handled;
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::ProcessRTS
// ---------------------------------------------------------------------------

int EmCPU68K::ProcessRTS (emuptr dest)
{
	int	handled = false;

	Hook68KRTSList::iterator	iter = fHookRTS.begin ();
	while (iter != fHookRTS.end ())
	{
		if ((*iter) (dest))
		{
			handled = true;
		}

		++iter;
	}

	return handled;
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::CheckNewPC
// ---------------------------------------------------------------------------

void EmCPU68K::CheckNewPC (emuptr dest)
{
	Hook68KNewPCList::iterator	iter = fHookNewPC.begin ();
	while (iter != fHookNewPC.end ())
	{
		(*iter) (dest);

		++iter;
	}

	EmMemory::CheckNewPC (dest);
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::CheckNewSP
// ---------------------------------------------------------------------------

void EmCPU68K::CheckNewSP (EmStackChangeType type)
{
	Hook68KNewSPList::iterator	iter = fHookNewSP.begin ();
	while (iter != fHookNewSP.end ())
	{
		(*iter) (type);

		++iter;
	}
}


#pragma mark -

// ---------------------------------------------------------------------------
//		¥ EmCPU68K::InstallHookException
//		¥ EmCPU68K::InstallHookJSR
//		¥ EmCPU68K::InstallHookJSR_Ind
//		¥ EmCPU68K::InstallHookLINK
//		¥ EmCPU68K::InstallHookRTE
//		¥ EmCPU68K::InstallHookRTS
//		¥ EmCPU68K::InstallHookNewPC
//		¥ EmCPU68K::InstallHookNewSP
// ---------------------------------------------------------------------------

void EmCPU68K::InstallHookException (ExceptionNumber exceptionNumber,
										Hook68KException fn)
{
	fExceptionHandlers[exceptionNumber].push_back (fn);
}


void EmCPU68K::InstallHookJSR (Hook68KJSR fn)
{
	fHookJSR.push_back (fn);
}


void EmCPU68K::InstallHookJSR_Ind (Hook68KJSR_Ind fn)
{
	fHookJSR_Ind.push_back (fn);
}


void EmCPU68K::InstallHookLINK (Hook68KLINK fn)
{
	fHookLINK.push_back (fn);
}


void EmCPU68K::InstallHookRTE (Hook68KRTE fn)
{
	fHookRTE.push_back (fn);
}


void EmCPU68K::InstallHookRTS (Hook68KRTS fn)
{
	fHookRTS.push_back (fn);
}


void EmCPU68K::InstallHookNewPC (Hook68KNewPC fn)
{
	fHookNewPC.push_back (fn);
}


void EmCPU68K::InstallHookNewSP (Hook68KNewSP fn)
{
	fHookNewSP.push_back (fn);
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::RemoveHookException
//		¥ EmCPU68K::RemoveHookJSR
//		¥ EmCPU68K::RemoveHookJSR_Ind
//		¥ EmCPU68K::RemoveHookLINK
//		¥ EmCPU68K::RemoveHookRTE
//		¥ EmCPU68K::RemoveHookRTS
//		¥ EmCPU68K::RemoveHookNewPC
//		¥ EmCPU68K::RemoveHookNewSP
// ---------------------------------------------------------------------------

void EmCPU68K::RemoveHookException (ExceptionNumber exceptionNumber,
										Hook68KException fn)
{
	Hook68KExceptionList::iterator	iter = find (
							fExceptionHandlers[exceptionNumber].begin (),
							fExceptionHandlers[exceptionNumber].end (),
							fn);

	if (iter != fExceptionHandlers[exceptionNumber].end ())
	{
		fExceptionHandlers[exceptionNumber].erase (iter);
	}
}


void EmCPU68K::RemoveHookJSR (Hook68KJSR fn)
{
	Hook68KJSRList::iterator	iter = find (fHookJSR.begin (), fHookJSR.end (), fn);

	if (iter != fHookJSR.end ())
	{
		fHookJSR.erase (iter);
	}
}


void EmCPU68K::RemoveHookJSR_Ind (Hook68KJSR_Ind fn)
{
	Hook68KJSR_IndList::iterator	iter = find (fHookJSR_Ind.begin (), fHookJSR_Ind.end (), fn);

	if (iter != fHookJSR_Ind.end ())
	{
		fHookJSR_Ind.erase (iter);
	}
}


void EmCPU68K::RemoveHookLINK (Hook68KLINK fn)
{
	Hook68KLINKList::iterator	iter = find (fHookLINK.begin (), fHookLINK.end (), fn);

	if (iter != fHookLINK.end ())
	{
		fHookLINK.erase (iter);
	}
}


void EmCPU68K::RemoveHookRTE (Hook68KRTE fn)
{
	Hook68KRTEList::iterator	iter = find (fHookRTE.begin (), fHookRTE.end (), fn);

	if (iter != fHookRTE.end ())
	{
		fHookRTE.erase (iter);
	}
}


void EmCPU68K::RemoveHookRTS (Hook68KRTS fn)
{
	Hook68KRTSList::iterator	iter = find (fHookRTS.begin (), fHookRTS.end (), fn);

	if (iter != fHookRTS.end ())
	{
		fHookRTS.erase (iter);
	}
}


void EmCPU68K::RemoveHookNewPC (Hook68KNewPC fn)
{
	Hook68KNewPCList::iterator	iter = find (fHookNewPC.begin (), fHookNewPC.end (), fn);

	if (iter != fHookNewPC.end ())
	{
		fHookNewPC.erase (iter);
	}
}


void EmCPU68K::RemoveHookNewSP (Hook68KNewSP fn)
{
	Hook68KNewSPList::iterator	iter = find (fHookNewSP.begin (), fHookNewSP.end (), fn);

	if (iter != fHookNewSP.end ())
	{
		fHookNewSP.erase (iter);
	}
}


#pragma mark -

// ---------------------------------------------------------------------------
//		¥ EmCPU68K::GetRegisters
// ---------------------------------------------------------------------------

void EmCPU68K::GetRegisters (regstruct& registers)
{
	this->UpdateSRFromRegisters ();

	registers = regs;
	registers.pc = m68k_getpc ();
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::SetRegisters
// ---------------------------------------------------------------------------

void EmCPU68K::SetRegisters (regstruct& registers)
{
	regs = registers;
	this->UpdateRegistersFromSR ();

	m68k_setpc (registers.pc);

	this->CheckNewSP (kStackPointerChanged);
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::UpdateSRFromRegisters
// ---------------------------------------------------------------------------
// Create a 16-bit status register value from the broken out fields.  In
// general, we keep the various fields separate for speed of access.  However,
// there are times when we need the packed 16-bit field.
//
// This function is called any time the 16-bit representation of the SR is
// needed:
//
//		on EORSR
//		on ORSR
//		on ANDSR
//		on MVSR2
//		on MV2SR
//		on RTR
//		when processing exceptions (EmCPU68K::ProcessException)
//
//		SystemPacket::GetRegs
//		EmCPU68K::GetRegisters

void EmCPU68K::UpdateSRFromRegisters (void)
{
	// (taken from MakeSR in newcpu.c)

	regs.sr = ((regs.t1 << 15) | (regs.t0 << 14)
		| (regs.s << 13) | (regs.m << 12) | (regs.intmask << 8)
		| (GET_XFLG << 4) | (GET_NFLG << 3) | (GET_ZFLG << 2) | (GET_VFLG << 1)
		| GET_CFLG);
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::UpdateRegistersFromSR
// ---------------------------------------------------------------------------
// Break out all of the fields from the 16-bit status register into their own
// separate variables.
//
// This function is called any time the SR has been update and needs to be
// re-expanded:
//
//		on EORSR
//		on ORSR
//		on ANDSR
//		on MV2SR
//		on STOP
//		on RTE
//		on RTR
//
//		SystemPacket::SetRegs
//		EmCPU68K::SetRegisters

void EmCPU68K::UpdateRegistersFromSR (void)
{
	// (taken from MakeFromSR in newcpu.c)
	
//	int oldm = regs.m;
	int olds = regs.s;
	
	regs.t1 = (regs.sr >> 15) & 1;
	regs.t0 = (regs.sr >> 14) & 1;
	regs.s = (regs.sr >> 13) & 1;
	regs.m = (regs.sr >> 12) & 1;
	regs.intmask = (regs.sr >> 8) & 7;

	SET_XFLG ((regs.sr >> 4) & 1);
	SET_NFLG ((regs.sr >> 3) & 1);
	SET_ZFLG ((regs.sr >> 2) & 1);
	SET_VFLG ((regs.sr >> 1) & 1);
	SET_CFLG (regs.sr & 1);

	if (olds != regs.s)
	{
		if (olds)
		{
			regs.isp = m68k_areg(regs, 7);
			m68k_areg(regs, 7) = regs.usp;
		}
		else
		{
			regs.usp = m68k_areg(regs, 7);
			m68k_areg(regs, 7) = regs.isp;
		}
	}
	
	regs.spcflags |= SPCFLAG_INT;

	if (regs.t1 || regs.t0)
	{
		regs.spcflags |= SPCFLAG_TRACE;
	}
	else
	{
		regs.spcflags &= ~(SPCFLAG_TRACE | SPCFLAG_DOTRACE);
	}
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::GetCycleCount
// ---------------------------------------------------------------------------

uint32 EmCPU68K::GetCycleCount (void)
{
	return fCycleCount;
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::BusError
// ---------------------------------------------------------------------------

void EmCPU68K::BusError (emuptr address, long size, Bool forRead)
{
	gExceptionAddress	= address;
	gExceptionSize		= size;
	gExceptionForRead	= forRead;

	this->ProcessException (kException_BusErr);

	EmAssert (false);	// Should never get this far.
}


// ---------------------------------------------------------------------------
//		¥ EmCPU68K::AddressError
// ---------------------------------------------------------------------------

void EmCPU68K::AddressError (emuptr address, long size, Bool forRead)
{
	gExceptionAddress	= address;
	gExceptionSize		= size;
	gExceptionForRead	= forRead;

	this->ProcessException (kException_AddressErr);

	EmAssert (false);	// Should never get this far.
}


#pragma mark -

// ---------------------------------------------------------------------------
//		¥ EmCPU68K::InitializeUAETables
// ---------------------------------------------------------------------------

void EmCPU68K::InitializeUAETables (void)
{
	static int	initialized;

	// All of the stuff in this function needs to be done only once;
	// it doesn't need to be executed every time we create a new CPU.

	if (initialized)
		return;

	initialized = true;

	// Initialize some CPU-related tables
	// (This initialization code is taken from init_m68k in newcpu.c)

	int 	i, j;

	for (i = 0 ; i < 256 ; i++)
	{
		for (j = 0 ; j < 8 ; j++)
		{
			if (i & (1 << j))
			{
				break;
			}
		}

		movem_index1[i] = j;
		movem_index2[i] = 7-j;
		movem_next[i] = i & (~(1 << j));
	}

	read_table68k ();
	do_merges ();

	// The rest of this code is based on build_cpufunctbl in newcpu.c.

	unsigned long	opcode;
	struct cputbl*	tbl = op_smalltbl_3;

	for (opcode = 0; opcode < 65536; opcode++)
	{
		cpufunctbl[opcode] = op_illg;
	}

	for (i = 0; tbl[i].handler != NULL; i++)
	{
		if (!tbl[i].specific)
		{
			cpufunctbl[tbl[i].opcode] = tbl[i].handler;
#if HAS_PROFILING
			perftbl[tbl[i].opcode] = tbl[i].perf;
#endif
		}
	}

	for (opcode = 0; opcode < 65536; opcode++)
	{
		cpuop_func* f;

		if (table68k[opcode].mnemo == i_ILLG || table68k[opcode].clev > 0)
		{
			continue;
		}

		if (table68k[opcode].handler != -1)
		{
			f = cpufunctbl[table68k[opcode].handler];
			if (f == op_illg)
			{
				abort ();
			}

			cpufunctbl[opcode] = f;
#if HAS_PROFILING
			perftbl[opcode] = perftbl[table68k[opcode].handler];
#endif

		}
	}

	for (i = 0; tbl[i].handler != NULL; i++)
	{
		if (tbl[i].specific)
		{
			cpufunctbl[tbl[i].opcode] = tbl[i].handler;
#if HAS_PROFILING
			perftbl[tbl[i].opcode] = tbl[i].perf;
#endif
		}
	}

	// (hey readcpu doesn't free this guy!)

	Platform::DisposeMemory (table68k);
}