File: uc_machine.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (2422 lines) | stat: -rw-r--r-- 67,168 bytes parent folder | download | duplicates (2)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "common/memstream.h"

#include "ultima/ultima8/usecode/uc_machine.h"
#include "ultima/ultima8/usecode/uc_process.h"
#include "ultima/ultima8/usecode/usecode.h"
#include "ultima/ultima8/kernel/kernel.h"
#include "ultima/ultima8/kernel/delay_process.h"
#include "ultima/ultima8/ultima8.h"
#include "ultima/ultima8/world/current_map.h"
#include "ultima/ultima8/world/world.h"
#include "ultima/ultima8/usecode/bit_set.h"
#include "ultima/ultima8/usecode/byte_set.h"
#include "ultima/ultima8/usecode/uc_list.h"
#include "ultima/ultima8/misc/id_man.h"
#include "ultima/ultima8/world/get_object.h"

#include "ultima/ultima8/convert/u8/convert_usecode_u8.h"
#include "ultima/ultima8/convert/crusader/convert_usecode_regret.h"
#include "ultima/ultima8/world/actors/main_actor.h"

namespace Ultima {
namespace Ultima8 {

//#define DEBUG_USECODE

#ifdef DEBUG_USECODE
#define TRACE_OP(format, ...) if (trace) debug(format, __VA_ARGS__)
#else
#define TRACE_OP(format, ...)
#endif

#ifdef DEBUG_USECODE
static const char *print_bp(const int16 offset) {
	static char str[32];
	snprintf(str, 32, "[BP%c%02Xh]", offset < 0 ? '-' : '+',
	         offset < 0 ? -offset : offset);
	return str;
}

static const char *print_sp(const int16 offset) {
	static char str[32];
	snprintf(str, 32, "[SP%c%02Xh]", offset < 0 ? '-' : '+',
	         offset < 0 ? -offset : offset);
	return str;
}
#endif


//#define DUMPHEAP

enum UCSegments {
	SEG_STACK      = 0x0000,
	SEG_STACK_FIRST = 0x0001,
	SEG_STACK_LAST = 0x7FFE,
	SEG_STRING     = 0x8000,
	SEG_LIST       = 0x8001, // I don't think this is used
	SEG_OBJ        = 0x8002,
	SEG_GLOBAL     = 0x8003
};

UCMachine *UCMachine::_ucMachine = nullptr;

UCMachine::UCMachine(const Intrinsic *iset, unsigned int icount) {
	debug(1, "Creating UCMachine...");

	_ucMachine = this;

	// zero _globals

	if (GAME_IS_U8) {
		_globals = new BitSet(0x1000);
		_convUse = new ConvertUsecodeU8();
	} else if (GAME_IS_REMORSE) {
		_globals = new ByteSet(0x1000);
		// slight hack: set global 003C to start as avatar number.
		_globals->setEntries(0x3C, 2, 1);
		_convUse = new ConvertUsecodeCrusader();
	} else {
		_globals = new ByteSet(0x1000);
		// slight hack: set global 001E to start as avatar number.
		_globals->setEntries(0x1E, 2, 1);
		_convUse = new ConvertUsecodeRegret();
	}

	loadIntrinsics(iset, icount); //!...

	_listIDs = new idMan(1, 65534, 128);
	_stringIDs = new idMan(1, 65534, 256);

	_tracingEnabled = false;
	_traceAll = false;
}


UCMachine::~UCMachine() {
	debug(1, "Destroying UCMachine...");
	_ucMachine = nullptr;

	delete _globals;
	delete _convUse;
	delete _listIDs;
	delete _stringIDs;
}

void UCMachine::reset() {
	debug(1, "Resetting UCMachine");

	// clear _globals
	_globals->setSize(0x1000);

	// slight HACK: set global 003C (remorse) / 001E (regret)
	// to start as avatar number.
	if (GAME_IS_REMORSE) {
		_globals->setEntries(0x3C, 2, 1);
	} else if (GAME_IS_REGRET) {
		_globals->setEntries(0x1E, 2, 1);
	}

	// clear strings, lists
	Common::HashMap<uint16, UCList *>::iterator iter;
	for (iter = _listHeap.begin(); iter != _listHeap.end(); ++iter)
		delete(iter->_value);
	_listHeap.clear();
	_stringHeap.clear();
}

void UCMachine::loadIntrinsics(const Intrinsic *i, unsigned int icount) {
	_intrinsics = i;
	_intrinsicCount = icount;
}

void UCMachine::execProcess(UCProcess *p) {
	assert(p);

	uint32 base = p->_usecode->get_class_base_offset(p->_classId);
	Common::SeekableReadStream *cs = new Common::MemoryReadStream(p->_usecode->get_class(p->_classId) + base,
																  p->_usecode->get_class_size(p->_classId) - base);
	cs->seek(p->_ip);

	bool trace = trace_show(p->_pid, p->_itemNum, p->_classId);
	if (trace) {
		debug("tick %u running process %u, item %u, type %u, class %u, offset %u",
			  Kernel::get_instance()->getTickNum(), p->_pid, p->_itemNum, p->_type, p->_classId, p->_ip);
	}

	bool cede = false;
	bool error = false;
	bool go_until_cede = false;

	while (!cede && !error && !p->is_terminated()) {
		//! guard against reading past end of class
		//! guard against other error conditions

		uint8 opcode = cs->readByte();

#ifdef DEBUG_USECODE
		char op_info[32];
		if (trace) {
			snprintf(op_info, 32, "sp = %02X; %04X:%04X: %02X",
					 p->_stack.stacksize(), p->_classId, p->_ip, opcode);
		}
#endif
		int8 si8a, si8b;
		uint8 ui8a;
		uint16 ui16a, ui16b;
		uint32 ui32a, ui32b;
		int16 si16a, si16b;
		int32 si32a, si32b;

		switch (opcode) {

		// POP opcodes
		case 0x00:
			// 00 xx
			// pop 16 bit int, and assign LS 8 bit int into bp+xx
			si8a = cs->readSByte();
			ui16a = p->_stack.pop2();
			p->_stack.assign1(p->_bp + si8a, static_cast<uint8>(ui16a));
			TRACE_OP("%s\tpop byte\t%s = %02Xh", op_info, print_bp(si8a), ui16a);
			break;

		case 0x01:
			// 01 xx
			// pop 16 bit int into bp+xx
			si8a = cs->readSByte();
			ui16a = p->_stack.pop2();
			p->_stack.assign2(p->_bp + si8a, ui16a);
			TRACE_OP("%s\tpop\t\t%s = %04Xh", op_info, print_bp(si8a), ui16a);
			break;

		case 0x02:
			// 02 xx
			// pop 32 bit int into bp+xx
			si8a = cs->readSByte();
			ui32a = p->_stack.pop4();
			p->_stack.assign4(p->_bp + si8a, ui32a);
			TRACE_OP("%s\tpop dword\t%s = %08Xh", op_info, print_bp(si8a), ui32a);
			break;

		case 0x03: {
			// 03 xx yy
			// pop yy bytes into bp+xx
			si8a = cs->readSByte();
			uint8 size = cs->readByte();
			uint8 buf[256];
			p->_stack.pop(buf, size);
			p->_stack.assign(p->_bp + si8a, buf, size);
			TRACE_OP("%s\tpop huge\t%s %i", op_info, print_bp(si8a), size);
			break;
		}

		// 0x04 ASSIGN_MEMBER_CHAR (Unused)
		// 0x05 ASSIGN_MEMBER_INT (Unused)
		// 0x06 ASSIGN_MEMBER_LONG (Unused)
		// 0x07 ASSIGN_MEMBER_HUGE (Unused)

		case 0x08:
			// 08
			// pop 32bits into process result register
			TRACE_OP("%s\tpop dword\tprocess result", op_info);
			p->_result = p->_stack.pop4();
			break;

		case 0x09: {
			// 09 xx yy zz
			// pop yy bytes into an element of list bp+xx (or slist if zz set)
			si8a = cs->readSByte();
			ui32a = cs->readByte();
			si8b = cs->readSByte();
			TRACE_OP("%s\tassign element\t%s (%02X) (slist==%02X)",
				  op_info, print_bp(si8a), ui32a, si8b);
			ui16a = p->_stack.pop2() - 1; // index
			ui16b = p->_stack.access2(p->_bp + si8a);
			UCList *l = getList(ui16b);
			if (!l) {
				warning("assign element to an invalid list (%u)", ui16b);
				error = true;
				break;
			}
			if (si8b) { // slist?
				// what special behaviour do we need here?
				// probably just that the overwritten element has to be freed?
				if (ui32a != 2) {
					warning("Unhandled operand %u to pop slist", ui32a);
					error = true; // um?
				}
				l->assign(ui16a, p->_stack.access());
				p->_stack.pop2(); // advance SP
			} else {
				l->assign(ui16a, p->_stack.access());
				p->_stack.addSP(ui32a);
			}
			break;
		}

		// PUSH opcodes

		case 0x0A:
			// 0A xx
			// push sign-extended 8 bit xx onto the stack as 16 bit
			ui16a = cs->readSByte();
			p->_stack.push2(ui16a);
			TRACE_OP("%s\tpush sbyte\t%04Xh", op_info, ui16a);
			break;

		case 0x0B:
			// 0B xx xx
			// push 16 bit xxxx onto the stack
			ui16a = cs->readUint16LE();
			p->_stack.push2(ui16a);
			TRACE_OP("%s\tpush\t\t%04Xh", op_info, ui16a);
			break;

		case 0x0C:
			// 0C xx xx xx xx
			// push 32 bit xxxxxxxx onto the stack
			ui32a = cs->readUint32LE();
			p->_stack.push4(ui32a);
			TRACE_OP("%s\tpush dword\t%08Xh", op_info, ui32a);
			break;

		case 0x0D: {
			// 0D xx xx yy ... yy 00
			// push string (yy ... yy) of length xx xx onto the stack
			ui16a = cs->readUint16LE();
			char *str = new char[ui16a + 1];
			cs->read(str, ui16a);
			str[ui16a] = 0;

			// WORKAROUND: German U8: When the candles are not in the right positions
			// for a sorcery spell, the string does not match, causing a crash.
			// Original bug: https://sourceforge.net/p/pentagram/bugs/196/
			if (GAME_IS_U8 && p->_classId == 0x7C) {
				if (!strcmp(str, " Irgendetwas stimmt nicht!")) {
					str[25] = '.'; // ! to .
				}
			}

			TRACE_OP("%s\tpush string\t\"%s\"", op_info, str);
			ui16b = cs->readByte();
			if (ui16b != 0) {
				warning("Zero terminator missing in push string");
				error = true;
			}
			p->_stack.push2(assignString(str));
			delete[] str;
			break;
		}

		case 0x0E: {
			// 0E xx yy
			// pop yy values of size xx and push the resulting list
			// (list is created in reverse order)
			ui16a = cs->readByte();
			ui16b = cs->readByte();
			UCList *l = new UCList(ui16a, ui16b);
			p->_stack.addSP(ui16a * (ui16b - 1));
			for (unsigned int i = 0; i < ui16b; i++) {
				l->append(p->_stack.access());
				p->_stack.addSP(-ui16a);
			}
			p->_stack.addSP(ui16a * (ui16b + 1));
			p->_stack.push2(assignList(l));
			TRACE_OP("%s\tcreate list\t%02X (%02X)", op_info, ui16b, ui16a);
			break;
		}

		// Usecode function and intrinsic calls

		case 0x0F: {
			// 0F xx yyyy
			// intrinsic call. xx is number of argument bytes
			// (includes this pointer, if present)
			// NB: do not actually pop these argument bytes
			uint16 arg_bytes = cs->readByte();
			uint16 func = cs->readUint16LE();
			TRACE_OP("%s\tcalli\t\t%04Xh (%02Xh arg bytes) %s",
				  op_info, func, arg_bytes, _convUse->intrinsics()[func]);

			// !constants
			if (func >= _intrinsicCount || _intrinsics[func] == 0) {
				Item *testItem = nullptr;
				p->_temp32 = 0;

				if (arg_bytes >= 4) {
					// HACKHACKHACK to check what the argument is.
					uint8 *argmem = new uint8[arg_bytes];
					uint8 *args = argmem;
					p->_stack.pop(args, 4);
					p->_stack.addSP(-4); // don't really pop the args
					ARG_UC_PTR(iptr);
					uint16 testItemId = ptrToObject(iptr);
					testItem = getItem(testItemId);
					delete [] argmem;
				}

				Common::String info;
				if (testItem) {
					info = Common::String::format("item %u", testItem->getObjId());
					if (arg_bytes > 4)
						info += Common::String::format(" + %u bytes", arg_bytes - 4);
				} else {
					info = Common::String::format("%u bytes", arg_bytes);
				}
				warning("Unhandled intrinsic %u \'%s\'? (%s) called", func, _convUse->intrinsics()[func], info.c_str());
				if (testItem) {
					warning("%s", testItem->dumpInfo().c_str());
				}
			} else {
				//!! hackish
				if (_intrinsics[func] == UCMachine::I_dummyProcess ||
				        _intrinsics[func] == UCMachine::I_true) {
					warning("Unhandled intrinsic %u \'%s\'? called", func, _convUse->intrinsics()[func]);
				}
				uint8 *argbuf = new uint8[arg_bytes];
				p->_stack.pop(argbuf, arg_bytes);
				p->_stack.addSP(-arg_bytes); // don't really pop the args

				p->_temp32 = _intrinsics[func](argbuf, arg_bytes);

				delete[] argbuf;
			}

			// WORKAROUND: In U8, the flag 'startedConvo' [0000 01] which acts
			// as a mutex is set too late in the script, allowing two copies of
			// of the Ancient Ones script (each spawned by a different egg) to
			// run simultaneously. Set the flag when the avatar is put in stasis
			// to avoid this.
			// Original bug: https://sourceforge.net/p/pentagram/feature-requests/6/
			if (GAME_IS_U8 && p->_classId == 0x48B && func == 0xD0) {
				// 0xD0 = setAvatarInStasis
				_globals->setEntries(0, 1, 1);
			}
			break;
		}

		// 0x10 NEAR_ROUTINE_CALL (Unused in U8 and Crusader)

		case 0x11: {
			// 11 xx xx yy yy
			// Ultima 8:
			// call the function at offset yy yy of class xx xx
			// Crusader:
			// call function number yy yy of class xx xx
			uint16 new_classid = cs->readUint16LE();
			uint16 new_offset = cs->readUint16LE();
			TRACE_OP("%s\tcall\t\t%04X:%04X", op_info, new_classid, new_offset);
			if (GAME_IS_CRUSADER) {
				new_offset = p->_usecode->get_class_event(new_classid,
				             new_offset);
			}

			p->_ip = static_cast<uint16>(cs->pos());   // Truncates!!
			p->call(new_classid, new_offset);

			// Update the code segment
			uint32 base_ = p->_usecode->get_class_base_offset(p->_classId);
			delete cs;
			cs = new Common::MemoryReadStream(p->_usecode->get_class(p->_classId) + base_,
											  p->_usecode->get_class_size(p->_classId) - base_);
			cs->seek(p->_ip);

			// Resume execution
			break;
		}

		case 0x12:
			// 12
			// pop 16bits into temp register
			p->_temp32 = p->_stack.pop2();
			TRACE_OP("%s\tpop\t\ttemp = %04X", op_info, (p->_temp32 & 0xFFFF));
			break;

		case 0x13:
			// 13
			// pop 32bits into temp register. (Not actually used in U8 or Crusader)
			p->_temp32 = p->_stack.pop4();
			TRACE_OP("%s\tpop long\t\ttemp = %08X", op_info, p->_temp32);
			break;

		// Arithmetic

		case 0x14:
			// 14
			// 16 bit add
			si16a = static_cast<int16>(p->_stack.pop2());
			si16b = static_cast<int16>(p->_stack.pop2());
			p->_stack.push2(static_cast<uint16>(si16a + si16b));
			TRACE_OP("%s\tadd", op_info);
			break;

		case 0x15:
			// 15
			// 32 bit add
			si32a = static_cast<int32>(p->_stack.pop4());
			si32b = static_cast<int32>(p->_stack.pop4());
			p->_stack.push4(static_cast<uint32>(si32a + si32b));
			TRACE_OP("%s\tadd long", op_info);
			break;

		case 0x16:
			// 16
			// pop two strings from the stack and push the concatenation
			// (free the originals? order?)
			ui16a = p->_stack.pop2();
			ui16b = p->_stack.pop2();
			if (ui16b == 0) {
				warning("Trying to append to string 0.");
				error = true;
				break;
			}
			_stringHeap[ui16b] += getString(ui16a);
			freeString(ui16a);
			p->_stack.push2(ui16b);
			TRACE_OP("%s\tconcat\t\t= %s", op_info, _stringHeap[ui16b].c_str());
			break;

		case 0x17: {
			// 17
			// pop two lists from the stack and push the 'sum' of the lists
			// (freeing the originals)
			ui16a = p->_stack.pop2();
			ui16b = p->_stack.pop2();
			UCList *listA = getList(ui16a);
			UCList *listB = getList(ui16b);

			if (listB && listA) {
				if (listA->getElementSize() != listB->getElementSize()) {
					warning("Trying to append lists with different element sizes (%u != %u)",
						listB->getElementSize(), listA->getElementSize());
					error = true;
				} else {
					listB->appendList(*listA);
				}
				// CHECKME: do we allow appending a list to itself?
				assert(ui16a != ui16b);
				freeList(ui16a);
				p->_stack.push2(ui16b);
			} else {
				// at least one of the lists didn't exist. Error or not?
				// for now: if one exists, push that one.
				// if neither exists, push 0.

				if (listA) {
					p->_stack.push2(ui16a);
				} else if (listB) {
					p->_stack.push2(ui16b);
				} else {
					p->_stack.push2(0);
				}
			}
			TRACE_OP("%s\tappend", op_info);
			break;
		}
		// 0x18 EXCLUSIVE_ADD_LIST (Unused in U8 and Crusader)

		case 0x19: {
			// 19 02
			// add two stringlists, removing duplicates
			ui32a = cs->readByte();
			if (ui32a != 2) {
				warning("Unhandled operand %u to union slist", ui32a);
				error = true;
			}
			ui16a = p->_stack.pop2();
			ui16b = p->_stack.pop2();
			UCList *srclist = getList(ui16a);
			UCList *dstlist = getList(ui16b);
			if (!srclist || !dstlist) {
				warning("Invalid list param to union slist");
				error = true;
			} else {
				dstlist->unionStringList(*srclist);
				freeStringList(ui16a); // contents are actually freed in unionSL
			}
			p->_stack.push2(ui16b);
			TRACE_OP("%s\tunion slist\t(%02X)", op_info, ui32a);
			break;
		}
		case 0x1A: {
			// 1A 02
			// subtract string list
			ui32a = cs->readByte(); // elementsize (always 02)
			ui32a = 2;
			ui16a = p->_stack.pop2();
			ui16b = p->_stack.pop2();
			UCList *srclist = getList(ui16a);
			UCList *dstlist = getList(ui16b);
			if (!srclist || !dstlist) {
				warning("Invalid list param to subtract slist");
				error = true;
			} else {
				dstlist->subtractStringList(*srclist);
				freeStringList(ui16a);
			}
			p->_stack.push2(ui16b);
			TRACE_OP("%s\tremove slist\t(%02X)", op_info, ui32a);
			break;
		}
		case 0x1B: {
			// 1B xx
			// pop two lists from the stack of element size xx and
			// remove the 2nd from the 1st
			// (free the originals? order?)
			ui32a = cs->readByte(); // elementsize
			ui16a = p->_stack.pop2();
			ui16b = p->_stack.pop2();
			UCList *srclist = getList(ui16a);
			UCList *dstlist = getList(ui16b);
			if (!srclist || !dstlist) {
				warning("Invalid list param to remove from slist");
				error = true;
			} else {
				dstlist->subtractList(*srclist);
				freeList(ui16a);
			}
			p->_stack.push2(ui16b);
			TRACE_OP("%s\tremove list\t(%02X)", op_info, ui32a);
			break;
		}
		case 0x1C:
			// 1C
			// subtract two 16 bit integers
			si16a = static_cast<int16>(p->_stack.pop2());
			si16b = static_cast<int16>(p->_stack.pop2());
			p->_stack.push2(static_cast<uint16>(si16b - si16a));
			TRACE_OP("%s\tsub", op_info);
			break;

		case 0x1D:
			// 1D
			// subtract two 32 bit integers
			si32a = static_cast<int32>(p->_stack.pop4());
			si32b = static_cast<int32>(p->_stack.pop4());
			p->_stack.push4(static_cast<uint32>(si32b - si32a));
			TRACE_OP("%s\tsub long", op_info);
			break;

		case 0x1E:
			// 1E
			// multiply two 16 bit integers
			si16a = static_cast<int16>(p->_stack.pop2());
			si16b = static_cast<int16>(p->_stack.pop2());
			p->_stack.push2(static_cast<uint16>(si16a * si16b));
			TRACE_OP("%s\tmul", op_info);
			break;

		case 0x1F:
			// 1F
			// multiply two 32 bit integers
			si32a = static_cast<int32>(p->_stack.pop4());
			si32b = static_cast<int32>(p->_stack.pop4());
			p->_stack.push4(static_cast<uint32>(si32a * si32b));
			TRACE_OP("%s\tmul long", op_info);
			break;

		case 0x20:
			// 20
			// divide two 16 bit integers
			si16a = static_cast<int16>(p->_stack.pop2());
			si16b = static_cast<int16>(p->_stack.pop2());
			if (si16a != 0) {
				p->_stack.push2(static_cast<uint16>(si16b / si16a));
			} else {
				warning("0x20 division by zero.");
				p->_stack.push2(0);
			}
			TRACE_OP("%s\tdiv", op_info);
			break;

		case 0x21:
			// 21
			// divide two 32 bit integers
			si32a = static_cast<int32>(p->_stack.pop4());
			si32b = static_cast<int32>(p->_stack.pop4());
			if (si32a != 0) {
				p->_stack.push4(static_cast<uint32>(si32b / si32a));
			} else {
				warning("0x21 division by zero.");
				p->_stack.push4(0);
			}
			TRACE_OP("%s\tdiv", op_info);
			break;

		case 0x22:
			// 22
			// 16 bit mod, b % a
			// Appears to be C-style %
			si16a = static_cast<int16>(p->_stack.pop2());
			si16b = static_cast<int16>(p->_stack.pop2());
			if (si16a != 0) {
				p->_stack.push2(static_cast<uint16>(si16b % si16a));
			} else {
				warning("0x22 division by zero.");
				p->_stack.push2(0);
			}
			TRACE_OP("%s\tmod", op_info);
			break;

		case 0x23:
			// 23
			// 32 bit mod
			si32a = static_cast<int32>(p->_stack.pop4());
			si32b = static_cast<int32>(p->_stack.pop4());
			if (si32a != 0) {
				p->_stack.push4(static_cast<uint32>(si32b % si32a));
			} else {
				warning("0x23 division by zero.");
				p->_stack.push4(0);
			}
			TRACE_OP("%s\tmod long", op_info);
			break;

		case 0x24:
			// 24
			// 16 bit cmp
			si16a = static_cast<int16>(p->_stack.pop2());
			si16b = static_cast<int16>(p->_stack.pop2());
			if (si16a == si16b) {
				p->_stack.push2(1);
			} else {
				p->_stack.push2(0);
			}
			TRACE_OP("%s\tcmp", op_info);
			break;

		case 0x25:
			// 25
			// 32 bit cmp
			si32a = static_cast<int32>(p->_stack.pop4());
			si32b = static_cast<int32>(p->_stack.pop4());
			if (si32a == si32b) {
				p->_stack.push2(1);
			} else {
				p->_stack.push2(0);
			}
			TRACE_OP("%s\tcmp long", op_info);
			break;

		case 0x26:
			// 26
			// compare two strings
			// (delete strings)
			ui16a = p->_stack.pop2();
			ui16b = p->_stack.pop2();
			if (getString(ui16b) == getString(ui16a))
				p->_stack.push2(1);
			else
				p->_stack.push2(0);
			freeString(ui16a);
			freeString(ui16b);
			TRACE_OP("%s\tstrcmp", op_info);
			break;

		// 0x27 EQUALS_HUGE (Unused in U8 and Crusader)

		case 0x28:
			// 28
			// 16 bit less-than
			si16a = static_cast<int16>(p->_stack.pop2());
			si16b = static_cast<int16>(p->_stack.pop2());
			if (si16b < si16a) {
				p->_stack.push2(1);
			} else {
				p->_stack.push2(0);
			}
			TRACE_OP("%s\tlt", op_info);
			break;

		case 0x29:
			// 29
			// 32 bit less-than
			si32a = static_cast<int32>(p->_stack.pop4());
			si32b = static_cast<int32>(p->_stack.pop4());
			if (si32b < si32a) {
				p->_stack.push2(1);
			} else {
				p->_stack.push2(0);
			}
			TRACE_OP("%s\tlt long", op_info);
			break;

		case 0x2A:
			// 2A
			// 16 bit less-or-equal
			si16a = static_cast<int16>(p->_stack.pop2());
			si16b = static_cast<int16>(p->_stack.pop2());
			if (si16b <= si16a) {
				p->_stack.push2(1);
			} else {
				p->_stack.push2(0);
			}
			TRACE_OP("%s\tle", op_info);
			break;

		case 0x2B:
			// 2B
			// 32 bit less-or-equal
			si32a = static_cast<int32>(p->_stack.pop4());
			si32b = static_cast<int32>(p->_stack.pop4());
			if (si32b <= si32a) {
				p->_stack.push2(1);
			} else {
				p->_stack.push2(0);
			}
			TRACE_OP("%s\tle long", op_info);
			break;

		case 0x2C:
			// 2C
			// 16 bit greater-than
			si16a = static_cast<int16>(p->_stack.pop2());
			si16b = static_cast<int16>(p->_stack.pop2());
			if (si16b > si16a) {
				p->_stack.push2(1);
			} else {
				p->_stack.push2(0);
			}
			TRACE_OP("%s\tgt", op_info);
			break;

		case 0x2D:
			// 2D
			// 32 bit greater-than
			si32a = static_cast<int32>(p->_stack.pop4());
			si32b = static_cast<int32>(p->_stack.pop4());
			if (si32b > si32a) {
				p->_stack.push2(1);
			} else {
				p->_stack.push2(0);
			}
			TRACE_OP("%s\tgt long", op_info);
			break;

		case 0x2E:
			// 2E
			// 16 bit greater-or-equal
			si16a = static_cast<int16>(p->_stack.pop2());
			si16b = static_cast<int16>(p->_stack.pop2());
			if (si16b >= si16a) {
				p->_stack.push2(1);
			} else {
				p->_stack.push2(0);
			}
			TRACE_OP("%s\tge", op_info);
			break;

		case 0x2F:
			// 2F
			// 32 bit greater-or-equal
			si32a = static_cast<int32>(p->_stack.pop4());
			si32b = static_cast<int32>(p->_stack.pop4());
			if (si32b >= si32a) {
				p->_stack.push2(1);
			} else {
				p->_stack.push2(0);
			}
			TRACE_OP("%s\tge long", op_info);
			break;

		case 0x30:
			// 30
			// 16 bit boolean not
			ui16a = p->_stack.pop2();
			if (!ui16a) {
				p->_stack.push2(1);
			} else {
				p->_stack.push2(0);
			}
			TRACE_OP("%s\tnot", op_info);
			break;

		case 0x31:
			// 31
			// 32 bit boolean not (not used in U8 or Crusader)
			ui32a = p->_stack.pop4();
			if (!ui32a) {
				p->_stack.push2(1);
			} else {
				p->_stack.push2(0);
			}
			TRACE_OP("%s\tnot long", op_info);
			break;

		case 0x32:
			// 32
			// 16 bit logical and
			ui16a = p->_stack.pop2();
			ui16b = p->_stack.pop2();
			if (ui16a && ui16b) {
				p->_stack.push2(1);
			} else {
				p->_stack.push2(0);
			}
			TRACE_OP("%s\tand", op_info);
			break;

		case 0x33:
			// 33
			// 32 bit logical and (not used in U8 or Crusader)
			ui32a = p->_stack.pop4();
			ui32b = p->_stack.pop4();
			if (ui32a && ui32b) {
				p->_stack.push4(1);
			} else {
				p->_stack.push4(0);
			}
			TRACE_OP("%s\tand long", op_info);
			break;

		case 0x34:
			// 34
			// 16 bit logical or
			ui16a = p->_stack.pop2();
			ui16b = p->_stack.pop2();
			if (ui16a || ui16b) {
				p->_stack.push2(1);
			} else {
				p->_stack.push2(0);
			}
			TRACE_OP("%s\tor", op_info);
			break;

		case 0x35:
			// 35
			// 32 bit logical or (not used in U8 or Crusader)
			ui32a = p->_stack.pop4();
			ui32b = p->_stack.pop4();
			if (ui32a || ui32b) {
				p->_stack.push4(1);
			} else {
				p->_stack.push4(0);
			}
			TRACE_OP("%s\tor long", op_info);
			break;

		case 0x36:
			// 36
			// 16 bit not-equal
			si16a = static_cast<int16>(p->_stack.pop2());
			si16b = static_cast<int16>(p->_stack.pop2());
			if (si16a != si16b) {
				p->_stack.push2(1);
			} else {
				p->_stack.push2(0);
			}
			TRACE_OP("%s\tne", op_info);
			break;

		case 0x37:
			// 37
			// 32 bit not-equal (only used in Crusader)
			si32a = static_cast<int32>(p->_stack.pop4());
			si32b = static_cast<int32>(p->_stack.pop4());
			if (si32a != si32b) {
				p->_stack.push2(1);
			} else {
				p->_stack.push2(0);
			}
			TRACE_OP("%s\tne long", op_info);
			break;

		case 0x38: {
			// 38 xx yy
			// is element (size xx) in list? (or slist if yy is true)
			// free list/slist afterwards

			ui16a = cs->readByte();
			ui32a = cs->readByte();
			ui16b = p->_stack.pop2();
			UCList *l = getList(ui16b);
			if (!l) {
				warning("Invalid list id %u", ui16b);
				error = true;
			} else if (ui32a) { // stringlist
				if (ui16a != 2) {
					warning("Unhandled operand %u to in slist", ui16a);
					error = true;
				}
				if (l->stringInList(p->_stack.pop2()))
					p->_stack.push2(1);
				else
					p->_stack.push2(0);
				freeStringList(ui16b);
			} else {
				bool found = l->inList(p->_stack.access());
				p->_stack.addSP(ui16a);
				if (found)
					p->_stack.push2(1);
				else
					p->_stack.push2(0);

				freeList(ui16b);
			}
			TRACE_OP("%s\tin list\t\t%s slist==%02X", op_info, print_bp(ui16a), ui32a);
			break;
		}
		case 0x39:
			// 39
			// 16 bit bitwise and
			ui16a = p->_stack.pop2();
			ui16b = p->_stack.pop2();
			p->_stack.push2(ui16a & ui16b);
			TRACE_OP("%s\tbit_and", op_info);
			break;

		case 0x3A:
			// 3A
			// 16 bit bitwise or
			ui16a = p->_stack.pop2();
			ui16b = p->_stack.pop2();
			p->_stack.push2(ui16a | ui16b);
			TRACE_OP("%s\tbit_or", op_info);
			break;

		case 0x3B:
			// 3B
			// 16 bit bitwise not
			ui16a = p->_stack.pop2();
			p->_stack.push2(~ui16a);
			TRACE_OP("%s\tbit_not", op_info);
			break;

		case 0x3C:
			// 3C
			// 16 bit left shift
			// operand order is different between U8 and crusader!
			if (GAME_IS_U8) {
				si16a = static_cast<int16>(p->_stack.pop2());
				ui16b = static_cast<int16>(p->_stack.pop2());
			} else {
				ui16b = static_cast<int16>(p->_stack.pop2());
				si16a = static_cast<int16>(p->_stack.pop2());
			}
			p->_stack.push2(static_cast<uint16>(si16a << ui16b));
			TRACE_OP("%s\tlsh\t%04Xh >> %xh = %xh", op_info, si16a, ui16b, si16a << ui16b);
			break;

		case 0x3D:
			// 3D
			// 16 bit right shift (sign-extended - game uses SAR opcode)
			// operand order is different between U8 and crusader!
			if (GAME_IS_U8) {
				si16a = static_cast<int16>(p->_stack.pop2());
				ui16b = static_cast<int16>(p->_stack.pop2());
			} else {
				ui16b = static_cast<int16>(p->_stack.pop2());
				si16a = static_cast<int16>(p->_stack.pop2());
			}
			p->_stack.push2(static_cast<uint16>(si16a >> ui16b));
			TRACE_OP("%s\trsh\t%04Xh >> %xh = %xh", op_info, si16a, ui16b, si16a >> ui16b);
			break;

		case 0x3E:
			// 3E xx
			// push the value of the sign-extended 8 bit local var xx as 16 bit int
			si8a = cs->readSByte();
			ui16a = static_cast<uint16>(static_cast<int8>(p->_stack.access1(p->_bp + si8a)));
			p->_stack.push2(ui16a);
			TRACE_OP("%s\tpush byte\t%s = %02Xh", op_info, print_bp(si8a), ui16a);
			break;

		case 0x3F:
			// 3F xx
			// push the value of the 16 bit local var xx
			si8a = cs->readSByte();
			ui16a = p->_stack.access2(p->_bp + si8a);
			p->_stack.push2(ui16a);
			TRACE_OP("%s\tpush\t\t%s = %04Xh", op_info, print_bp(si8a), ui16a);
			break;

		case 0x40:
			// 40 xx
			// push the value of the 32 bit local var xx
			si8a = cs->readSByte();
			ui32a = p->_stack.access4(p->_bp + si8a);
			p->_stack.push4(ui32a);
			TRACE_OP("%s\tpush dword\t%s = %08Xh", op_info, print_bp(si8a), ui32a);
			break;

		case 0x41: {
			// 41 xx
			// push the string local var xx
			// duplicating the string?
			si8a = cs->readSByte();
			ui16a = p->_stack.access2(p->_bp + si8a);
			p->_stack.push2(duplicateString(ui16a));
			TRACE_OP("%s\tpush string\t%s", op_info, print_bp(si8a));
			break;
		}
		case 0x42: {
			// 42 xx yy
			// push the list (with yy size elements) at BP+xx
			// duplicating the list?
			si8a = cs->readSByte();
			ui16a = cs->readByte();
			ui16b = p->_stack.access2(p->_bp + si8a);
			UCList *l = new UCList(ui16a);
			if (getList(ui16b)) {
				l->copyList(*getList(ui16b));
			} else {
				// trying to push non-existent list. Error or not?
				// Not: for example, function 01E3::0080, offset 0112
				// warning("Pushing non-existent list");
				// error = true;
			}
			uint16 newlistid = assignList(l);
			p->_stack.push2(newlistid);
			TRACE_OP("%s\tpush list\t%s (%04X, copy %04X, %d elements)",
				  op_info, print_bp(si8a), ui16b, newlistid, l->getSize());
			break;
		}
		case 0x43: {
			// 43 xx
			// push the stringlist local var xx
			// duplicating the list, duplicating the strings in the list
			si8a = cs->readSByte();
			ui16a = 2;
			ui16b = p->_stack.access2(p->_bp + si8a);
			UCList *l = new UCList(ui16a);
			if (getList(ui16b)) {
				l->copyStringList(*getList(ui16b));
			} else {
				// trying to push non-existent list. Error or not?
				// (Devon's talk code seems to use it; so no error for now)
				// warning("Pushing non-existent slist");
				// error = true;
			}
			p->_stack.push2(assignList(l));
			TRACE_OP("%s\tpush slist\t%s", op_info, print_bp(si8a));
			break;
		}
		case 0x44: {
			// 44 xx yy
			// push element from the second last var pushed onto the stack
			// (a list/slist), indexed by the last element pushed onto the list
			// (a byte/word). XX is the size of the types contained in the list
			// YY is true if it's a slist (for garbage collection)

			// duplicate string if YY? yy = 1 only occurs
			// in two places in U8: once it pops into temp afterwards,
			// once it is indeed freed. So, guessing we should duplicate.
			ui32a = cs->readByte();
			ui32b = cs->readByte();
			ui16a = p->_stack.pop2() - 1; // index
			ui16b = p->_stack.pop2(); // list
			UCList *l = getList(ui16b);
			if (!l) {
//				warning("push element from invalid list (%u)", ui16b);
				// This is necessary for closing the backpack to work
				p->_stack.push0(ui32a);
//				error = true;
			} else {
				if (ui32b) {
					uint16 s = l->getStringIndex(ui16a);
					p->_stack.push2(duplicateString(s));
				} else {
					if (ui16a < l->getSize()) {
						p->_stack.push((*l)[ui16a], ui32a);
					} else {
						// WORKAROUND
						warning("ignore 0x44 request to push %d from list len %d", ui16a, l->getSize());
					}
				}
			}
			TRACE_OP("%s\tpush element\t%02X slist==%02X", op_info, ui32a, ui32b);
			break;
		}
		case 0x45:
			// 45 xx yy
			// push huge of size yy from BP+xx
			si8a = cs->readSByte();
			ui16b = cs->readByte();
			p->_stack.push(p->_stack.access(p->_bp + si8a), ui16b);
			TRACE_OP("%s\tpush huge\t%s %02X", op_info, print_bp(si8a), ui16b);
			break;

		// 0x46 BYTE_MEMBER_REFERENCE (Unused)
		// 0x47 INT_MEMBER_REFERENCE (Unused)
		// 0x48 LONG_MEMBER_REFERENCE (Unused)
		// 0x49 HUGE_MEMBER_REFERENCE (Unused)
		// 0x4a THIS_REFERENCE (Unused)

		case 0x4B:
			// 4B xx
			// push 32 bit pointer address of BP+XX
			si8a = cs->readSByte();
			p->_stack.push4(stackToPtr(p->_pid, p->_bp + si8a));
			TRACE_OP("%s\tpush addr\t%s", op_info, print_bp(si8a));
			break;

		case 0x4C: {
			// 4C xx
			// indirect push,
			// pops a 32 bit pointer off the stack and pushes xx bytes
			// from the location referenced by the pointer
			ui16a = cs->readByte();
			ui32a = p->_stack.pop4();

			p->_stack.addSP(-ui16a);
			if (!dereferencePointer(ui32a, p->_stack.access(), ui16a)) {
				error = true;
			}

			if (!error && ui16a == 2) {
				TRACE_OP("%s\tpush indirect\t%02Xh bytes = %04Xh", op_info, ui16a, p->_stack.access2(p->_stack.getSP()));
			} else {
				TRACE_OP("%s\tpush indirect\t%02Xh bytes", op_info, ui16a);
			}
			break;
		}

		case 0x4D: {
			// 4D xx
			// indirect pop
			// pops a 32 bit pointer off the stack and pushes xx bytes
			// from the location referenced by the pointer
			ui16a = cs->readByte();
			ui32a = p->_stack.pop4();

			if (assignPointer(ui32a, p->_stack.access(), ui16a)) {
				p->_stack.addSP(ui16a);
			} else {
				error = true;
			}

			TRACE_OP("%s\tpop indirect\t%02Xh bytes", op_info, ui16a);
			break;
		}

		case 0x4E:
			// 4E xx xx yy
			// push global xxxx size yy bits
			ui16a = cs->readUint16LE();
			ui16b = cs->readByte();
			ui32a = _globals->getEntries(ui16a, ui16b);
			p->_stack.push2(static_cast<uint16>(ui32a));
			TRACE_OP("%s\tpush\t\tglobal [%04X %02X] = %02X", op_info, ui16a, ui16b, ui32a);
			break;

		case 0x4F:
			// 4F xx xx yy
			// pop value into global xxxx size yy bits
			ui16a = cs->readUint16LE();	// pos
			ui16b = cs->readByte();		// len
			ui32a = p->_stack.pop2();	// val
			_globals->setEntries(ui16a, ui16b, ui32a);

			if ((GAME_IS_U8 && (ui32a & ~(((1 << ui16b) - 1)))) || (GAME_IS_CRUSADER && (ui16b > 2))) {
				warning("Value popped into a flag it doesn't fit in (%04X %04X %04X)", ui16a, ui16b, ui32a);
			}

			// paranoid :-)
			if (GAME_IS_U8) {
				assert(_globals->getEntries(ui16a, ui16b) == (ui32a & ((1 << ui16b) - 1)));
			} else {
				assert(_globals->getEntries(ui16a, ui16b) == (ui32a & ((1 << (ui16b * 8)) - 1)));
		    }

			TRACE_OP("%s\tpop\t\tglobal [%04X %02X] = %02X", op_info, ui16a, ui16b, ui32a);
			break;

		case 0x50:
			// 50
			// return from function
			if (p->ret()) { // returning from process
				TRACE_OP("%s\tret\t\tfrom process", op_info);
				p->terminateDeferred();

				// return value is going to be stored somewhere,
				// and some other process is probably waiting for it.
				// So, we can't delete ourselves just yet.
			} else {
				TRACE_OP("%s\tret\t\tto %04X:%04X", op_info, p->_classId, p->_ip);

				// return value is stored in _temp32 register

				// Update the code segment
				uint32 base_ = p->_usecode->get_class_base_offset(p->_classId);

				delete cs;
				cs = new Common::MemoryReadStream(p->_usecode->get_class(p->_classId) + base_,
												  p->_usecode->get_class_size(p->_classId) - base_);
				cs->seek(p->_ip);
			}

			// Resume execution
			break;

		case 0x51:
			// 51 xx xx
			// relative jump to xxxx if false
			si16a = static_cast<int16>(cs->readUint16LE());
			ui16b = p->_stack.pop2();
			if (!ui16b) {
				ui16a = cs->pos() + si16a;
				cs->seek(ui16a);
				TRACE_OP("%s\tjne\t\t%04hXh\t(to %04X) (taken)", op_info, si16a, cs->pos());
			} else {
				TRACE_OP("%s\tjne\t\t%04hXh\t(to %04X) (not taken)", op_info, si16a, cs->pos());
			}
			break;

		case 0x52:
			// 52 xx xx
			// relative jump to xxxx
			si16a = static_cast<int16>(cs->readUint16LE());
			ui16a = cs->pos() + si16a;
			cs->seek(ui16a);
			TRACE_OP("%s\tjmp\t\t%04hXh\t(to %04X)", op_info, si16a, cs->pos());
			break;

		case 0x53:
			// 53
			// suspend
			TRACE_OP("%s\tsuspend", op_info);
			go_until_cede = false;
			cede = true;
			break;

		case 0x54: {
			// 54 01 01
			// implies
			// Links two processes (two pids are popped) a and b, meaning
			// b->waitfor(a)
			//
			// In the disassembly, '01 01' is the number of processes to
			// pop, but in practice only ever appears as 01 01.
			//
			// pid a is often the current pid in U8

			// 'implies' seems to push a value too, although it is very
			// often ignored. It looks like it's a pid, but which one?

			// additionally, it is possible that 'implies' puts the result
			// of a process in the 'process result' variable,
			// or more likely, when a process finishes, it sets the result
			// value of the processes that were waiting for it.
			// 0x6D (push process result) only seems to occur soon after
			// an 'implies'

			cs->readUint16LE(); // skip the 01 01
			ui16a = p->_stack.pop2();
			ui16b = p->_stack.pop2();
			p->_stack.push2(ui16a); //!! which pid do we need to push!?
			TRACE_OP("%s\timplies", op_info);

			Process *proc = Kernel::get_instance()->getProcess(ui16b);
			Process *proc2 = Kernel::get_instance()->getProcess(ui16a);
			if (proc && proc2) {
				proc->waitFor(ui16a);
				// The proc is now marked suspended, but finish this execution
				// until we hit a suspend or return.
				go_until_cede = true;
			} else {
				if (!proc && !proc2) {
					warning("Non-existent process PID (%u, %u) in implies.", ui16a, ui16b);
				} else if (!proc) {
					warning("Non-existent process PID (%u) in implies.", ui16b);
				} else {
					warning("Non-existent process PID (%u) in implies.", ui16a);
				}
				// This condition triggers in 057C:1090 when talking
				// to a child (class 02C4), directly after the conversation
				// Specifically, it occurs because there is no
				// leaveFastArea usecode for class 02C4.
				// So currently we only regard this as an error when the
				// missing process wasn't PID 0.
				if ((ui16a && !proc2) || (ui16b && !proc))
					error = true;
			}
			break;
		}

		// 0x55: AND_IMPLIES (only does push 0x402 in disasm, unused in U8 and Crusader)
		// 0x56: OR_IMPLIES (only does push 0x404 in disasm, unused in U8 and Crusader)

		case 0x57: {
			// 57 aa tt xx xx yy yy
			// spawn process function yyyy in class xxxx
			// aa = number of arg bytes pushed (not including this pointer which is 4 bytes)
			// tt = sizeof this pointer object
			// only remove the this pointer from stack (4 bytes)
			// put PID of spawned process in temp
			int arg_bytes = cs->readByte();
			int this_size = cs->readByte();
			uint16 classid = cs->readUint16LE();
			uint16 offset = cs->readUint16LE();

			uint32 thisptr = p->_stack.pop4();

			TRACE_OP("%s\tspawn\t\t%02X %02X %04X:%04X",
				  op_info, arg_bytes, this_size, classid, offset);

			if (GAME_IS_CRUSADER) {
				offset = p->_usecode->get_class_event(classid, offset);
			}

			UCProcess *newproc = new UCProcess(classid, offset,
			                                   thisptr,
			                                   this_size,
			                                   p->_stack.access(),
			                                   arg_bytes);
			// Note: order of execution of this process and the new one is
			// relevant. Currently, the spawned processes is executed once
			// immediately, after which the current process resumes
			p->_temp32 = Kernel::get_instance()->addProcessExec(newproc);

			if (trace) {
				debug("tick %u (still) running process %u, item %u, type %u, class %u, offset %u",
					  Kernel::get_instance()->getTickNum(), p->_pid, p->_itemNum, p->_type, p->_classId, p->_ip);
			}
			break;
		}

		case 0x58: {
			// 58 xx xx yy yy zz zz tt uu
			// spawn inline process function yyyy in class xxxx at offset zzzz
			// tt = size of this pointer
			// uu = unknown (occurring values: 00, 02, 05) - seems unused in original
			uint16 classid = cs->readUint16LE();
			uint16 offset = cs->readUint16LE();
			uint16 delta = cs->readUint16LE();
			int this_size = cs->readByte();
			int unknown = cs->readByte(); // ??

			// This only gets used in U8.  If it were used in Crusader it would
			// need the offset translation done in 0x57.
			assert(GAME_IS_U8);

			TRACE_OP("%s\tspawn inline\t%04X:%04X+%04X=%04X %02X %02X",
				  op_info, classid, offset, delta, offset + delta, this_size, unknown);

			 // This also ensures that unknown variable is used when TRACE_OP is empty
			if (unknown != 0 && unknown != 2 && unknown != 5) {
				debug(10, "unknown unknown value: %02X", unknown);
			}

			uint32 thisptr = 0;
			if (this_size > 0)
				thisptr = p->_stack.access4(p->_bp + 6);
			UCProcess *newproc = new UCProcess(classid, offset + delta,
			                                   thisptr, this_size);

			// as with 'spawn', run the spawned process once immediately
			uint16 newpid = Kernel::get_instance()->addProcessExec(newproc);

			if (trace) {
				debug("tick %u (still) running process %u, item %u, type %u, class %u, offset %u",
					  Kernel::get_instance()->getTickNum(), p->_pid, p->_itemNum, p->_type, p->_classId, p->_ip);
			}

			p->_stack.push2(newpid); //! push pid of new proc
			break;
		}

		case 0x59:
			// 59
			// push current process id
			p->_stack.push2(p->_pid);
			TRACE_OP("%s\tpush\t\tpid = %04Xh", op_info, p->_pid);
			break;

		case 0x5A:
			// 5A xx
			// init function. xx = local var size
			// sets xx bytes on stack to 0, moving sp
			ui16a = cs->readByte();
			TRACE_OP("%s\tinit\t\t%02X", op_info, ui16a);

			if (ui16a & 1) ui16a++; // 16-bit align
			if (ui16a > 0) {
				p->_stack.push0(ui16a);
			}
			break;

		case 0x5B:
			// 5B xx xx
			// debug line no xx xx
			ui16a = cs->readUint16LE(); // source line number
			TRACE_OP("%s\tdebug\tline number %d", op_info, ui16a);
			break;

		case 0x5C: {
			// 5C xx xx char[9]
			// debug line no xx xx in class str
			ui16a = cs->readUint16LE(); // source line number
			char name[10] = {0};
			for (int x = 0; x < 9; x++) {
				// skip over class name and null terminator
				name[x] = cs->readByte();
			}
			TRACE_OP("%s\tdebug\tline number %d\t\"%s\"", op_info, ui16a, name);
			debug(10, "name: \"%s\"", name); // Ensures that name variable is used when TRACE_OP is empty
			break;
		}

		case 0x5D:
			// 5D
			// push 8 bit value returned from function call
			// (push temp8 as 16 bit value)
			p->_stack.push2(static_cast<uint8>(p->_temp32 & 0xFF));
			TRACE_OP("%s\tpush byte\tretval = %02Xh", op_info, (p->_temp32 & 0xFF));
			break;

		case 0x5E:
			// 5E
			// push 16 bit value returned from function call
			// (push temp16)
			p->_stack.push2(static_cast<uint16>(p->_temp32 & 0xFFFF));
			TRACE_OP("%s\tpush\t\tretval = %04Xh", op_info, (p->_temp32 & 0xFFFF));
			break;

		case 0x5F:
			// 5F
			// push 32 bit value returned from function call
			// (push _temp32)
			p->_stack.push4(p->_temp32);
			TRACE_OP("%s\tpush long\t\tretval = %08Xh", op_info, p->_temp32);
			break;

		case 0x60:
			// 60
			// convert 16-bit to 32-bit int (sign extend)
			si32a = static_cast<int16>(p->_stack.pop2());
			p->_stack.push4(si32a);
			TRACE_OP("%s\tint to long", op_info);
			break;

		case 0x61:
			// 61
			// convert 32-bit to 16-bit int
			si16a = static_cast<int16>(p->_stack.pop4());
			p->_stack.push2(si16a);
			TRACE_OP("%s\tlong to int", op_info);
			break;

		case 0x62:
			// 62 xx
			// free the string in var BP+xx
			si8a = cs->readSByte();
			ui16a = p->_stack.access2(p->_bp + si8a);
			freeString(ui16a);
			TRACE_OP("%s\tfree string\t%s = %04X", op_info, print_bp(si8a), ui16a);
			break;

		case 0x63:
			// 63 xx
			// free the stringlist in var BP+xx
			si8a = cs->readSByte();
			ui16a = p->_stack.access2(p->_bp + si8a);
			freeStringList(ui16a);
			TRACE_OP("%s\tfree slist\t%s = %04X", op_info, print_bp(si8a), ui16a);
			break;

		case 0x64:
			// 64 xx
			// free the list in var BP+xx
			si8a = cs->readSByte();
			ui16a = p->_stack.access2(p->_bp + si8a);
			freeList(ui16a);
			TRACE_OP("%s\tfree list\t%s = %04X", op_info, print_bp(si8a), ui16a);
			break;

		case 0x65:
			// 65 xx
			// free the string at SP+xx
			// NB: sometimes there's a 32-bit string pointer at SP+xx
			//     However, the low word of this is exactly the 16bit ref
			si8a = cs->readSByte();
			ui16a = p->_stack.access2(p->_stack.getSP() + si8a);
			freeString(ui16a);
			TRACE_OP("%s\tfree string\t%s = %04X", op_info, print_sp(si8a), ui16a);
			break;

		case 0x66:
			// 66 xx
			// free the list at SP+xx
			si8a = cs->readSByte();
			ui16a = p->_stack.access2(p->_stack.getSP() + si8a);
			freeList(ui16a);
			TRACE_OP("%s\tfree list\t%s = %04X", op_info, print_sp(si8a), ui16a);
			break;

		case 0x67:
			// 67 xx
			// free the string list at SP+xx
			si8a = cs->readSByte();
			ui16a = p->_stack.access2(p->_stack.getSP() + si8a);
			freeStringList(ui16a);
			TRACE_OP("%s\tfree slist\t%s = %04x", op_info, print_sp(si8a), ui16a);
			break;

		// 0x68 COPY_STRING (unused in U8 and Crusader)

		case 0x69:
			// 69 xx
			// push the string in var BP+xx as 32 bit pointer
			si8a = cs->readSByte();
			ui16a = p->_stack.access2(p->_bp + si8a);
			p->_stack.push4(stringToPtr(ui16a));
			TRACE_OP("%s\tstr to ptr\t%s", op_info, print_bp(si8a));
			break;

		// 0x6A Convert pointer to string (unused in U8 and Crusader)

		case 0x6B:
			// 6B
			// pop a string and push 32 bit pointer to string
			ui16a = p->_stack.pop2();
			p->_stack.push4(stringToPtr(ui16a));
			TRACE_OP("%s\tstr to ptr", op_info);
			break;

		case 0x6C:
			// 6C xx yy
			// yy = type (01 = string, 02 = slist, 03 = list)
			// copy the (string/slist/list) in BP+xx to the current process,
			// and add it to the "Free Me" list of the process
			si8a = cs->readByte(); // index
			ui8a = cs->readByte(); // type
			TRACE_OP("%s\tparam _pid chg\t%s, type=%u", op_info, print_bp(si8a), ui8a);

			ui16a = p->_stack.access2(p->_bp + si8a);
			switch (ui8a) {
			case 1: // string
				// copy string
				ui16b = duplicateString(ui16a);
				break;
			case 2: { // slist
				UCList *l = new UCList(2);
				const UCList *srclist = getList(ui16a);
				if (!srclist) {
					warning("Invalid src list passed to slist copy");
					ui16b = 0;
					delete l;
					break;
				}
				l->copyStringList(*srclist);
				ui16b = assignList(l);
			}
			break;
			case 3: { // list
				const UCList *l = getList(ui16a);
				if (!l) {
					warning("Invalid src list passed to list copy");
					ui16b = 0;
					break;
				}
				int elementsize = l->getElementSize();
				UCList *l2 = new UCList(elementsize);
				l2->copyList(*l);
				ui16b = assignList(l2);
			}
			break;
			default:
				ui16b = 0;
				warning("Error: invalid param _pid change type (%u)", ui8a);
				error = true;
			}
			p->_stack.assign2(p->_bp + si8a, ui16b); // assign new index
			p->freeOnTerminate(ui16b, ui8a); // free new var when terminating
			break;

		case 0x6D:
			// 6D
			// push 32bit result of current process
			TRACE_OP("%s\tpush dword\tprocess result", op_info);
			p->_stack.push4(p->_result);
			break;

		case 0x6E:
			// 6E xx
			// subtract xx from stack pointer
			// (effect on SP is the same as popping xx bytes)
			si8a = cs->readSByte();
			p->_stack.addSP(-si8a);
			TRACE_OP("%s\tmove sp\t\t%s%02Xh", op_info, si8a < 0 ? "-" : "", si8a < 0 ? -si8a : si8a);
			break;

		case 0x6F:
			// 6F xx
			// push 32 pointer address of SP-xx
			si8a = cs->readSByte();
			p->_stack.push4(stackToPtr(p->_pid, static_cast<uint16>(p->_stack.getSP() - si8a)));
			TRACE_OP("%s\tpush addr\t%s", op_info, print_sp(-si8a));
			break;

		// loop-related opcodes
		// 0x70 has different types:
		//    02: search the area around an object
		//    03: search the area around an object, recursing into containers
		//    04: search a container
		//    05: search a container, recursing into containers
		//    06: something about looking for items on top of another (??)
		// each of these types allocate a rather large area on the stack
		// we expect SP to be at the end of that area when 0x73 is executed
		// a 'loop script' (created by 0x74) is used to select items

		case 0x70: {
			// 70 xx yy zz
			// loop something. Stores 'current object' in var xx
			// yy == num bytes in string
			// zz == type
			si16a = cs->readSByte();
			uint32 scriptsize = cs->readByte();
			uint32 searchtype = cs->readByte();

			ui16a = p->_stack.pop2();
			ui16b = p->_stack.pop2();

			//!! This may not be the way the original did things...

			// We'll first create a list of all matching items.
			// Store the id of this list in the last two bytes
			// of our stack area.
			// Behind that we'll store an index into this list.
			// This is followed by the variable in which to store the item
			// After that we store the loopscript length followed by
			// the loopscript itself.
			//   (Note that this puts a limit on the max. size of the
			//    loopscript of 0x20 bytes)

			if (scriptsize > 0x20) {
				warning("Loopscript too long");
				error = true;
				break;
			}

			uint8 *script = new uint8[scriptsize];
			p->_stack.pop(script, scriptsize);

			uint32 stacksize = 0;
			bool recurse = false;
			// we'll put everything on the stack after stacksize is set

			UCList *itemlist = new UCList(2);

			World *world = World::get_instance();

			switch (searchtype) {
			case 2:
			case 3: {
				// area search (3 = recursive)
				stacksize = GAME_IS_U8 ? 0x34 : 0x3A;
				if (searchtype == 3) recurse = true;

				// ui16a = item, ui16b = range
				const Item *item = getItem(ui16a);
				const uint16 range = GAME_IS_CRUSADER ? ui16b * 2 : ui16b;

				if (item) {
					Point3 pt = item->getLocationAbsolute();
					world->getCurrentMap()->areaSearch(itemlist, script,
					                                   scriptsize, nullptr,
					                                   range, recurse, pt.x, pt.y);
				} else {
					// return error or return empty list?
					warning("Invalid item %u passed to area search", ui16a);
				}
				break;
			}
			case 4:
			case 5: {
				// container search (5 = recursive)
				stacksize = GAME_IS_U8 ? 0x28 : 0x2A;
				if (searchtype == 5) {
					stacksize += 2;
					recurse = true;
				}

				// ui16a = 0xFFFF (?), ui16b = container
				Container *container = getContainer(ui16b);

				if (ui16a != 0xFFFF) {
					warning("non-FFFF value passed to container search");
				}

				if (container) {
					container->containerSearch(itemlist, script,
					                           scriptsize, recurse);
				} else {
					// return error or return empty list?
					warning("Invalid container %u passed to container search", ui16b);
				}
				break;
			}
			case 6: {
				// Surface search
				stacksize = GAME_IS_U8 ? 0x3D : 0x43;

				bool above = ui16a != 0xFFFF;
				bool below = ui16b != 0xFFFF;
				Item *item = getItem(below ? ui16b : ui16a);

				if (item) {
					world->getCurrentMap()->surfaceSearch(itemlist, script,
					                                      scriptsize, item,
					                                      above, below);
				} else {
					// return error or return empty list?
					warning("Invalid item passed to surface search");
				}
				break;
			}
			default:
				warning("Unhandled search type %u", searchtype);
				error = true;
				delete[] script;
				script = nullptr;
				break;
			}

			if (script != nullptr) {
				p->_stack.push0(stacksize - scriptsize - 8); // filler
				p->_stack.push(script, scriptsize);
				p->_stack.push2(scriptsize);
				p->_stack.push2(static_cast<uint16>(si16a));
				p->_stack.push2(0);
				uint16 itemlistID = assignList(itemlist);
				p->_stack.push2(itemlistID);

				delete[] script;

				TRACE_OP("%s\tloop\t\t%s %02X %02X", op_info, print_bp(si16a),
						 scriptsize, searchtype);
			}
		}
		// Intentional fall-through

		// 0x71 SEARCH_RECURSIVE (Unused)
		// 0x72 SEARCH_SURFACE (Unused)

		case 0x73: {
			// 73
			// next loop object. pushes false if end reached
			unsigned int sp = p->_stack.getSP();
			uint16 itemlistID = p->_stack.access2(sp);
			UCList *itemlist = getList(itemlistID);
			uint16 index = p->_stack.access2(sp + 2);
			si16a = static_cast<int16>(p->_stack.access2(sp + 4));

			if (!itemlist) {
				warning("Invalid item list in loopnext");
				error = true;
				break;
			}

			// see if there are still valid items left
			bool valid = false;
			do {
				if (index >= itemlist->getSize()) {
					break;
				}

				p->_stack.assign(p->_bp + si16a, (*itemlist)[index], 2);
				uint16 objid = p->_stack.access2(p->_bp + si16a);
				Item *item = getItem(objid);
				if (item) {
					valid = true;
				}

				if (!valid) index++;

			} while (!valid);

			if (!valid) {
				p->_stack.push2(0); // end of loop
				freeList(itemlistID);
			} else {
				p->_stack.push2(1);
				// increment index
				p->_stack.assign2(sp + 2, index + 1);
			}

			if (opcode == 0x73) { // because of the fall-through
				TRACE_OP("%s\tloopnext", op_info);
			}
			break;
		}

		case 0x74:
			// 74 xx
			// add xx to the current 'loopscript'
			ui8a = cs->readByte();
			p->_stack.push1(ui8a);
			TRACE_OP("%s\tloopscr\t\t%02X \"%c\"", op_info, ui8a, static_cast<char>(ui8a));
			break;

		case 0x75:
		case 0x76:
			// 75 xx yy zz zz  (foreach list)
			// 76 xx yy zz zz  (foreach string list)
			// xx is the stack offset to store 'current' value from the list
			//   (BP+xx)
			// yy is the 'datasize' of the list, identical to the second parameter
			//   of the create list/slist opcodes
			// zzzz is the offset to jump to after it's finished iteration
			//	 (the opcode before is always a 'jmp' to the start of the loop)
			// 2 16 bit values are on the stack and left there during each
			//   iteration:
			//   - loop index (always starts at 0xffff), updated each iteration
			//   - list id

			// 75 is for lists, 76 for slists
			// The only difference should be in the freeing afterwards.
			// Strings are _not_ duplicated when putting them in the loopvar
			// Lists _are_ freed afterwards

			si8a = cs->readByte();  // loop variable
			ui32a = cs->readByte(); // list size
			si16a = cs->readUint16LE(); // jump offset

			ui16a = p->_stack.access2(p->_stack.getSP());     // Loop index
			ui16b = p->_stack.access2(p->_stack.getSP() + 2); // Loop list

			if (opcode == 0x76 && ui32a != 2) {
				error = true;
			}

			if (opcode == 0x75) {
				TRACE_OP("%s\tfor each\t%s (%02X) %04hX",
						 op_info, print_bp(si8a), ui32a, si16a);
			} else {
				TRACE_OP("%s\tfor each str\t%s (%02X) %04hX",
						 op_info, print_bp(si8a), ui32a, si16a);
			}

			// Increment the counter
			if (ui16a == 0xFFFF) ui16a = 0;
			else ui16a++;

			if (ui16a >= getList(ui16b)->getSize()) {
				// loop done

				// free loop list
				if (opcode == 0x75) {
					freeList(ui16b);
				} else {
					freeStringList(ui16b);
				}

				p->_stack.addSP(4);  // Pop list and counter

				// jump out
				ui16a = cs->pos() + si16a;
				cs->seek(ui16a);
			} else {
				// loop iteration
				// (not duplicating any strings)

				// updated loop index
				p->_stack.assign2(p->_stack.getSP(), ui16a);

				// place next element from list in [bp+si8a]
				p->_stack.assign(p->_bp + si8a, (*getList(ui16b))[ui16a], ui32a);
			}
			break;

		case 0x77:
			// 77
			// set info
			// assigns item number and ProcessType
			p->setItemNum(p->_stack.pop2());
			p->setType(p->_stack.pop2());
			TRACE_OP("%s\tset info itemno: %d type: %d", op_info, p->getItemNum(), p->getType());
			break;

		case 0x78:
			// 78
			// process exclude
			// process gets 'exclusive access' to this (object,type)

			// Educated guess:
			// Check if any other processes have the same (object,type) info
			// set. If so, return from process.

			if (Kernel::get_instance()->
			        getNumProcesses(p->_itemNum, p->_type) > 1) {
				// another process with this (object,type) is already running
				p->terminateDeferred();
				TRACE_OP("%s\tprocess exclude\t(terminating)", op_info);
			} else {
				TRACE_OP("%s\tprocess exclude", op_info);
			}

			break;

		case 0x79:
			// 79
			// push address of global (Crusader only)
			ui16a = cs->readUint16LE(); // global address
			ui32a = globalToPtr(ui16a);
			p->_stack.push4(ui32a);
			TRACE_OP("%s\tpush global 0x%x (value: %x)", op_info, ui16a, ui32a);
			break;

		case 0x7A:
			// 7A
			// end of function
			// shouldn't happen
			TRACE_OP("%s\tend", op_info);
			warning("end of function opcode %02X reached", opcode);
			error = true;
			break;

		// 0x7B REGRESS (Unused)

		default:
			warning("unhandled opcode %02X", opcode);

		} // switch(opcode)

		// write back IP (but preserve IP if there was an error)
		if (!error)
			p->_ip = static_cast<uint16>(cs->pos());   // TRUNCATES!

		// check if we suspended ourselves
		if ((p->_flags & Process::PROC_SUSPENDED) != 0 && !go_until_cede)
			cede = true;
	} // while(!cede && !error && !p->terminated && !p->terminate_deferred)

	delete cs;

	if (error) {
		warning("Process %d caused an error at %04X:%04X (item %d). Killing process.",
			p->_pid, p->_classId, p->_ip, p->_itemNum);
		p->terminateDeferred();
	}
}


const Std::string &UCMachine::getString(uint16 str) const {
	static const Std::string emptystring("");

	Common::HashMap<uint16, Std::string>::const_iterator iter =
			_stringHeap.find(str);

	if (iter != _stringHeap.end())
		return iter->_value;

	return emptystring;
}

UCList *UCMachine::getList(uint16 l) {
	Common::HashMap<uint16, UCList *>::iterator iter = _listHeap.find(l);

	if (iter != _listHeap.end())
		return iter->_value;

	return nullptr;
}



uint16 UCMachine::assignString(const char *str) {
	uint16 id = _stringIDs->getNewID();
	if (id == 0) return 0;

	_stringHeap[id] = str;

	return id;
}

uint16 UCMachine::duplicateString(uint16 str) {
	return assignString(_stringHeap[str].c_str());
}


uint16 UCMachine::assignList(UCList *l) {
	uint16 id = _listIDs->getNewID();
	if (id == 0) return 0;
	assert(_listHeap.find(id) == _listHeap.end());

	_listHeap[id] = l;

	return id;
}

void UCMachine::freeString(uint16 s) {
	//! There's still a semi-bug in some places that string 0 can be assigned
	//! (when something accesses _stringHeap[0])
	//! This may not be desirable, but OTOH the created string will be
	//! empty, so not too much of a problem.
	Common::HashMap<uint16, Std::string>::iterator iter = _stringHeap.find(s);
	if (iter != _stringHeap.end()) {
		_stringHeap.erase(iter);
		_stringIDs->clearID(s);
	}
}

void UCMachine::freeList(uint16 l) {
	Common::HashMap<uint16, UCList *>::iterator iter = _listHeap.find(l);
	if (iter != _listHeap.end() && iter->_value) {
		iter->_value->free();
		delete iter->_value;
		_listHeap.erase(iter);
		_listIDs->clearID(l);
	}
}

void UCMachine::freeStringList(uint16 l) {
	Common::HashMap<uint16, UCList *>::iterator iter = _listHeap.find(l);
	if (iter != _listHeap.end() && iter->_value) {
		iter->_value->freeStrings();
		delete iter->_value;
		_listHeap.erase(iter);
		_listIDs->clearID(l);
	}
}

//static
uint32 UCMachine::listToPtr(uint16 l) {
	uint32 ptr = SEG_LIST;
	ptr <<= 16;
	ptr += l;
	return ptr;
}

//static
uint32 UCMachine::stringToPtr(uint16 s) {
	uint32 ptr = SEG_STRING;
	ptr <<= 16;
	ptr += s;
	return ptr;
}

//static
uint32 UCMachine::stackToPtr(uint16 _pid, uint16 offset) {
	uint32 ptr = SEG_STACK + _pid;
	ptr <<= 16;
	ptr += offset;
	return ptr;
}

//static
uint32 UCMachine::globalToPtr(uint16 offset) {
	uint32 ptr = SEG_GLOBAL;
	ptr <<= 16;
	ptr += offset;
	return ptr;
}

//static
uint32 UCMachine::objectToPtr(uint16 objID) {
	uint32 ptr = SEG_OBJ;
	ptr <<= 16;
	ptr += objID;
	return ptr;
}

bool UCMachine::assignPointer(uint32 ptr, const uint8 *data, uint32 size) {
	// Only implemented the following:
	// * stack pointers
	// * global pointers


	//! range checking...

	uint16 segment = static_cast<uint16>(ptr >> 16);
	uint16 offset = static_cast<uint16>(ptr & 0xFFFF);

	if (segment >= SEG_STACK_FIRST && segment <= SEG_STACK_LAST) {
		UCProcess *proc = dynamic_cast<UCProcess *>
		                  (Kernel::get_instance()->getProcess(segment));

		// reference to the stack of _pid 'segment'
		if (!proc) {
			// segfault :-)
			warning("Trying to access stack of non-existent process (pid: %u)", segment);
			return false;
		} else {
			proc->_stack.assign(offset, data, size);
		}
	} else if (segment == SEG_GLOBAL) {
		if (!GAME_IS_CRUSADER)
			warning("Global pointers not supported in U8");

		if (size == 1) {
			_globals->setEntries(offset, 1, data[0]);
		} else if (size == 2) {
			uint16 val = ((data[1] << 8) | data[0]);
			_globals->setEntries(offset, 2, val);
		} else {
			warning("Global pointers must be size 1 or 2");
		}
	} else {
		warning("Trying to access segment %04X", segment);
		return false;
	}

	return true;
}

bool UCMachine::dereferencePointer(uint32 ptr, uint8 *data, uint32 size) {
	// this one is a bit tricky. There's no way we can support
	// all possible pointers, so we're just going to do a few:
	// * stack pointers
	// * object pointers, as long as xx == 02. (i.e., get objref)
	// * global pointers


	//! range checking...

	uint16 segment = static_cast<uint16>(ptr >> 16);
	uint16 offset = static_cast<uint16>(ptr & 0xFFFF);

	if (segment >= SEG_STACK_FIRST && segment <= SEG_STACK_LAST) {
		UCProcess *proc = dynamic_cast<UCProcess *>
		                  (Kernel::get_instance()->getProcess(segment));

		// reference to the stack of _pid 'segment'
		if (!proc) {
			// segfault :-)
			warning("Trying to access stack of non-existent process (pid: %u)", segment);
			return false;
		} else {
			memcpy(data, proc->_stack.access(offset), size);
		}
	} else if (segment == SEG_OBJ) {
		if (size != 2) {
			warning("Trying to read other than 2 bytes from objptr");
			return false;
		} else {
			// push objref
			data[0] = static_cast<uint8>(offset);
			data[1] = static_cast<uint8>(offset >> 8);
		}
	} else if (segment == SEG_GLOBAL) {
		if (!GAME_IS_CRUSADER)
			warning("Global pointers not supported in U8");

		if (size == 1) {
			data[0] = static_cast<uint8>(_globals->getEntries(offset, 1));
		} else if (size == 2) {
			uint16 val = _globals->getEntries(offset, 2);
			data[0] = static_cast<uint8>(val);
			data[1] = static_cast<uint8>(val >> 8);
		} else {
			warning("Global pointers must be size 1 or 2");
		}
	} else {
		warning("Trying to access segment %04X", segment);
		return false;
	}
	return true;
}

//static
uint16 UCMachine::ptrToObject(uint32 ptr) {
	//! This function is a bit of a misnomer, since it's more general than this

	uint16 segment = static_cast<uint16>(ptr >> 16);
	uint16 offset = static_cast<uint16>(ptr);
	if (segment >= SEG_STACK_FIRST && segment <= SEG_STACK_LAST) {
		UCProcess *proc = dynamic_cast<UCProcess *>
		                  (Kernel::get_instance()->getProcess(segment));

		// reference to the stack of _pid 'segment'
		if (!proc) {
			// segfault :-)
			warning("Trying to access stack of non-existent process (pid: %u)", segment);
			return 0;
		} else if (proc->_stack.getSize() < (uint32)offset + 2) {
			warning("Trying to access past end of stack offset %u (size: %u) process (pid: %u)",
				offset, proc->_stack.getSize(), segment);
			return 0;
		} else {
			return proc->_stack.access2(offset);
		}
	} else if (segment == SEG_OBJ || segment == SEG_STRING) {
		return offset;
	} else if (segment == SEG_GLOBAL) {
		return get_instance()->_globals->getEntries(offset, 2);
	} else {
		warning("Trying to access segment %04X", segment);
		return 0;
	}
}

void UCMachine::usecodeStats() const {
	g_debugger->debugPrintf("Usecode Machine memory stats:\n");
	g_debugger->debugPrintf("Strings    : %u/65534\n", _stringHeap.size());
#ifdef DUMPHEAP
	Common::HashMap<uint16, Std::string>::const_iterator iter;
	for (iter = _stringHeap.begin(); iter != _stringHeap.end(); ++iter)
		g_debugger->debugPrintf("%d:%s\n", iter->_key << ":" << iter->_value.c_str());
#endif
	g_debugger->debugPrintf("Lists      : %u/65534\n", _listHeap.size());
#ifdef DUMPHEAP
	Common::HashMap<uint16, UCList *>::const_iterator iterl;
	for (iterl = _listHeap.begin(); iterl != _listHeap.end(); ++iterl) {
		if (!iterl->_value) {
			g_debugger->debugPrintf("%d: <null>\n", iterl->_key);
			continue;
		}
		if (iterl->_value->getElementSize() == 2) {
			g_debugger->debugPrintf("%d:", iterl->_key);

			for (unsigned int i = 0; i < iterl->_value->getSize(); ++i) {
				if (i > 0) g_debugger->debugPrintf(",");
				g_debugger->debugPrintf("%d", iterl->_value->getuint16(i));
			}
			g_debugger->debugPrintf("\n");
		} else {
			g_debugger->debugPrintf("%d: %u elements of size %u\n",
				iterl->_key, iterl->_value->getSize(), iterl->_value->getElementSize());
		}
	}
#endif
}

void UCMachine::saveGlobals(Common::WriteStream *ws) const {
	_globals->save(ws);
}

void UCMachine::saveStrings(Common::WriteStream *ws) const {
	_stringIDs->save(ws);
	ws->writeUint32LE(static_cast<uint32>(_stringHeap.size()));

	Common::HashMap<uint16, Std::string>::const_iterator iter;
	for (iter = _stringHeap.begin(); iter != _stringHeap.end(); ++iter) {
		ws->writeUint16LE((*iter)._key);
		ws->writeUint32LE((*iter)._value.size());
		ws->write((*iter)._value.c_str(), (*iter)._value.size());
	}
}

void UCMachine::saveLists(Common::WriteStream *ws) const {
	_listIDs->save(ws);
	ws->writeUint32LE(_listHeap.size());

	Common::HashMap<uint16, UCList *>::const_iterator iter;
	for (iter = _listHeap.begin(); iter != _listHeap.end(); ++iter) {
		ws->writeUint16LE((*iter)._key);
		(*iter)._value->save(ws);
	}
}

bool UCMachine::loadGlobals(Common::ReadStream *rs, uint32 version) {
	return _globals->load(rs, version);
}

bool UCMachine::loadStrings(Common::ReadStream *rs, uint32 version) {
	if (!_stringIDs->load(rs, version)) return false;

	uint32 stringcount = rs->readUint32LE();
	for (unsigned int i = 0; i < stringcount; ++i) {
		uint16 sid = rs->readUint16LE();
		uint32 len = rs->readUint32LE();
		if (len) {
			char *buf = new char[len + 1];
			rs->read(buf, len);
			buf[len] = 0;
			_stringHeap[sid] = buf;
			delete[] buf;
		} else {
			_stringHeap[sid] = "";
		}
	}

	return true;
}

bool UCMachine::loadLists(Common::ReadStream *rs, uint32 version) {
	if (!_listIDs->load(rs, version)) return false;

	uint32 listcount = rs->readUint32LE();

	if (listcount > 65536) {
		warning("Improbable number of UC lists %d in save, corrupt save?", listcount);
		return false;
	}

	for (unsigned int i = 0; i < listcount; ++i) {
		uint16 lid = rs->readUint16LE();
		UCList *l = new UCList(2); // the "2" will be ignored by load()
		bool ret = l->load(rs, version);
		if (!ret) {
			delete l;
			return false;
		}

		_listHeap[lid] = l;
	}

	return true;
}


uint32 UCMachine::I_true(const uint8 * /*args*/, unsigned int /*argsize*/) {
	return 1;
}

uint32 UCMachine::I_false(const uint8 * /*args*/, unsigned int /*argsize*/) {
	return 1;
}

uint32 UCMachine::I_dummyProcess(const uint8 * /*args*/, unsigned int /*argsize*/) {
	return Kernel::get_instance()->addProcess(new DelayProcess(4));
}

uint32 UCMachine::I_getName(const uint8 * /*args*/, unsigned int /*argsize*/) {
	UCMachine *uc = UCMachine::get_instance();
	MainActor *av = getMainActor();
	// Note: assignString takes a copy
	return uc->assignString(av->getName().c_str());
}

uint32 UCMachine::I_numToStr(const uint8 *args, unsigned int /*argsize*/) {
	ARG_SINT16(num);

	char buf[16]; // a 16 bit int should easily fit
	snprintf(buf, 16, "%d", num);

	return UCMachine::get_instance()->assignString(buf);
}

uint32 UCMachine::I_urandom(const uint8 *args, unsigned int /*argsize*/) {
	ARG_UINT16(num);
	if (num <= 1) return 0;

	// return random integer between 0 (incl.) to num (excl.)
	Common::RandomSource &rs = Ultima8Engine::get_instance()->getRandomSource();
	return rs.getRandomNumber(num - 1);
}

uint32 UCMachine::I_rndRange(const uint8 *args, unsigned int /*argsize*/) {
	ARG_SINT16(lo);
	ARG_SINT16(hi);

	// return random integer between lo (incl.) to hi (incl.)
	if (hi <= lo)
		return lo;

	Common::RandomSource &rs = Ultima8Engine::get_instance()->getRandomSource();
	return rs.getRandomNumberRng(lo, hi);
}

} // End of namespace Ultima8
} // End of namespace Ultima