summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/cpu/i960/i960.c
blob: 4280121ab3e60b34e726f35054b97566f04a581d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
#include "emu.h"
#include "debugger.h"
#include "i960.h"

CPU_DISASSEMBLE( i960  );

#ifdef _MSC_VER
/* logb prototype is different for MS Visual C */
#include <float.h>
#define logb _logb
#endif


// Warning, IP = Instruction Pointer, called PC outside of Intel
//          PC = Process Control

enum { RCACHE_SIZE = 4 };

typedef struct _i960_state_t i960_state_t;
struct _i960_state_t {
	UINT32 r[0x20];
	UINT32 rcache[RCACHE_SIZE][0x10];
	UINT32 rcache_frame_addr[RCACHE_SIZE];
	// rcache_pos = how deep in the stack we are.  0-(RCACHE_SIZE-1) means in-cache.
	// RCACHE_SIZE or greater means out of cache, must save to memory.
	INT32 rcache_pos;

	double fp[4];

	UINT32 SAT, PRCB, PC, AC;
	UINT32 IP, PIP, ICR;
	int bursting;

	int immediate_irq, immediate_vector, immediate_pri;

	device_irq_callback irq_cb;
	legacy_cpu_device *device;
	const address_space *program;

	int icount;
};

INLINE i960_state_t *get_safe_token(running_device *device)
{
	assert(device != NULL);
	assert(device->type() == CPU);
	assert(cpu_get_type(device) == CPU_I960);
	return (i960_state_t *)downcast<legacy_cpu_device *>(device)->token();
}

static void do_call(i960_state_t *i960, UINT32 adr, int type, UINT32 stack);

INLINE UINT32 i960_read_dword_unaligned(i960_state_t *i960, UINT32 address)
{
	if (address & 3)
		return memory_read_byte_32le(i960->program, address) | memory_read_byte_32le(i960->program, address+1)<<8 | memory_read_byte_32le(i960->program, address+2)<<16 | memory_read_byte_32le(i960->program, address+3)<<24;
	else
		return memory_read_dword_32le(i960->program, address);
}

INLINE UINT16 i960_read_word_unaligned(i960_state_t *i960, UINT32 address)
{
	if (address & 1)
		return memory_read_byte_32le(i960->program, address) | memory_read_byte_32le(i960->program, address+1)<<8;
	else
		return memory_read_word_32le(i960->program, address);
}

INLINE void i960_write_dword_unaligned(i960_state_t *i960, UINT32 address, UINT32 data)
{
	if (address & 3)
	{
		memory_write_byte_32le(i960->program, address, data & 0xff);
		memory_write_byte_32le(i960->program, address+1, (data>>8)&0xff);
		memory_write_byte_32le(i960->program, address+2, (data>>16)&0xff);
		memory_write_byte_32le(i960->program, address+3, (data>>24)&0xff);
	}
	else
	{
		memory_write_dword_32le(i960->program, address, data);
	}
}

INLINE void i960_write_word_unaligned(i960_state_t *i960, UINT32 address, UINT16 data)
{
	if (address & 1)
	{
		memory_write_byte_32le(i960->program, address, data & 0xff);
		memory_write_byte_32le(i960->program, address+1, (data>>8)&0xff);
	}
	else
	{
		memory_write_word_32le(i960->program, address, data);
	}
}

INLINE void send_iac(i960_state_t *i960, UINT32 adr)
{
	UINT32 iac[4];
	iac[0] = memory_read_dword_32le(i960->program, adr);
	iac[1] = memory_read_dword_32le(i960->program, adr+4);
	iac[2] = memory_read_dword_32le(i960->program, adr+8);
	iac[3] = memory_read_dword_32le(i960->program, adr+12);

	switch(iac[0]>>24) {
	case 0x93: // reinit
		i960->SAT  = iac[1];
		i960->PRCB = iac[2];
		i960->IP   = iac[3];
		break;
	default:
		fatalerror("I960: %x: IAC %08x %08x %08x %08x", i960->PIP, iac[0], iac[1], iac[2], iac[3]);
		break;
	}
}

INLINE UINT32 get_ea(i960_state_t *i960, UINT32 opcode)
{
	int abase = (opcode >> 14) & 0x1f;
	if(!(opcode & 0x00001000)) { // MEMA
		UINT32 offset = opcode & 0x1fff;
		if(!(opcode & 0x2000))
			return offset;
		else
			return i960->r[abase]+offset;
	} else {                     // MEMB
		int index = opcode & 0x1f;
		int scale = (opcode >> 7) & 0x7;
		int mode  = (opcode >> 10) & 0xf;
		UINT32 ret;

		switch(mode) {
		case 0x4:
			return i960->r[abase];

		case 0x7:
			return i960->r[abase] + (i960->r[index] << scale);

		case 0xc:
			ret = memory_decrypted_read_dword(i960->program, i960->IP);
			i960->IP += 4;
			return ret;

		case 0xd:
			ret = memory_decrypted_read_dword(i960->program, i960->IP) + i960->r[abase];
			i960->IP += 4;
			return ret;

		case 0xe:
			ret = memory_decrypted_read_dword(i960->program, i960->IP) + (i960->r[index] << scale);
			i960->IP += 4;
			return ret;

		case 0xf:
			ret = memory_decrypted_read_dword(i960->program, i960->IP) + i960->r[abase] + (i960->r[index] << scale);
			i960->IP += 4;
			return ret;

		default:
			fatalerror("I960: %x: unhandled MEMB mode %x", i960->PIP, mode);
			return 0;
		}
	}
}

INLINE UINT32 get_1_ri(i960_state_t *i960, UINT32 opcode)
{
	if(!(opcode & 0x00000800))
		return i960->r[opcode & 0x1f];
	else
		return opcode & 0x1f;
}

INLINE UINT32 get_2_ri(i960_state_t *i960, UINT32 opcode)
{
	if(!(opcode & 0x00001000))
		return i960->r[(opcode>>14) & 0x1f];
	else
		return (opcode>>14) & 0x1f;
}

INLINE UINT64 get_2_ri64(i960_state_t *i960, UINT32 opcode)
{
	if(!(opcode & 0x00001000))
		return i960->r[(opcode>>14) & 0x1f] | ((UINT64)i960->r[((opcode>>14) & 0x1f)+1]<<32);
	else
		return (opcode>>14) & 0x1f;
}

INLINE void set_ri(i960_state_t *i960, UINT32 opcode, UINT32 val)
{
	if(!(opcode & 0x00002000))
		i960->r[(opcode>>19) & 0x1f] = val;
	else {
		fatalerror("I960: %x: set_ri on literal?", i960->PIP);
	}
}

INLINE void set_ri2(i960_state_t *i960, UINT32 opcode, UINT32 val, UINT32 val2)
{
	if(!(opcode & 0x00002000))
	{
		i960->r[(opcode>>19) & 0x1f] = val;
		i960->r[((opcode>>19) & 0x1f)+1] = val2;
	}
	else {
		fatalerror("I960: %x: set_ri2 on literal?", i960->PIP);
	}
}

INLINE void set_ri64(i960_state_t *i960, UINT32 opcode, UINT64 val)
{
	if(!(opcode & 0x00002000)) {
		i960->r[(opcode>>19) & 0x1f] = val;
		i960->r[((opcode>>19) & 0x1f)+1] = val >> 32;
	} else
		fatalerror("I960: %x: set_ri64 on literal?", i960->PIP);
}

INLINE double get_1_rif(i960_state_t *i960, UINT32 opcode)
{
	if(!(opcode & 0x00000800))
		return u2f(i960->r[opcode & 0x1f]);
	else {
		int idx = opcode & 0x1f;
		if(idx < 4)
			return i960->fp[idx];
		if(idx == 0x16)
			return 1.0;
		return 0.0;
	}
}

INLINE double get_2_rif(i960_state_t *i960, UINT32 opcode)
{
	if(!(opcode & 0x00001000))
		return u2f(i960->r[(opcode>>14) & 0x1f]);
	else {
		int idx = (opcode>>14) & 0x1f;
		if(idx < 4)
			return i960->fp[idx];
		if(idx == 0x16)
			return 1.0;
		return 0.0;
	}
}

INLINE void set_rif(i960_state_t *i960, UINT32 opcode, double val)
{
	if(!(opcode & 0x00002000))
		i960->r[(opcode>>19) & 0x1f] = f2u(val);
	else if(!(opcode & 0x00e00000))
		i960->fp[(opcode>>19) & 3] = val;
	else
		fatalerror("I960: %x: set_rif on literal?", i960->PIP);
}

INLINE double get_1_rifl(i960_state_t *i960, UINT32 opcode)
{
	if(!(opcode & 0x00000800)) {
		UINT64 v = i960->r[opcode & 0x1e];
		v |= ((UINT64)(i960->r[(opcode & 0x1e)+1]))<<32;
		return u2d(v);
	} else {
		int idx = opcode & 0x1f;
		if(idx < 4)
			return i960->fp[idx];
		if(idx == 0x16)
			return 1.0;
		return 0.0;
	}
}

INLINE double get_2_rifl(i960_state_t *i960, UINT32 opcode)
{
	if(!(opcode & 0x00001000)) {
		UINT64 v = i960->r[(opcode >> 14) & 0x1e];
		v |= ((UINT64)(i960->r[((opcode>>14) & 0x1e)+1]))<<32;
		return u2d(v);
	} else {
		int idx = (opcode>>14) & 0x1f;
		if(idx < 4)
			return i960->fp[idx];
		if(idx == 0x16)
			return 1.0;
		return 0.0;
	}
}

INLINE void set_rifl(i960_state_t *i960, UINT32 opcode, double val)
{
	if(!(opcode & 0x00002000)) {
		UINT64 v = d2u(val);
		i960->r[(opcode>>19) & 0x1e] = v;
		i960->r[((opcode>>19) & 0x1e)+1] = v>>32;
	} else if(!(opcode & 0x00e00000))
		i960->fp[(opcode>>19) & 3] = val;
	else
		fatalerror("I960: %x: set_rifl on literal?", i960->PIP);
}

INLINE UINT32 get_1_ci(i960_state_t *i960, UINT32 opcode)
{
	if(!(opcode & 0x00002000))
		return i960->r[(opcode >> 19) & 0x1f];
	else
		return (opcode >> 19) & 0x1f;
}

INLINE UINT32 get_2_ci(i960_state_t *i960, UINT32 opcode)
{
	return i960->r[(opcode >> 14) & 0x1f];
}

INLINE UINT32 get_disp(i960_state_t *i960, UINT32 opcode)
{
	UINT32 disp;
	disp = opcode & 0xffffff;
	if(disp & 0x00800000)
		disp |= 0xff000000;
	return disp-4;
}

INLINE UINT32 get_disp_s(i960_state_t *i960, UINT32 opcode)
{
	UINT32 disp;
	disp = opcode & 0x1fff;
	if(disp & 0x00001000)
		disp |= 0xffffe000;
	return disp-4;
}

INLINE void cmp_s(i960_state_t *i960, INT32 v1, INT32 v2)
{
	i960->AC &= ~7;
	if(v1<v2)
		i960->AC |= 4;
	else if(v1 == v2)
		i960->AC |= 2;
	else
		i960->AC |= 1;
}

INLINE void cmp_u(i960_state_t *i960, UINT32 v1, UINT32 v2)
{
	i960->AC &= ~7;
	if(v1<v2)
		i960->AC |= 4;
	else if(v1 == v2)
		i960->AC |= 2;
	else
		i960->AC |= 1;
}

INLINE void concmp_s(i960_state_t *i960, INT32 v1, INT32 v2)
{
	i960->AC &= ~7;
	if(v1 <= v2)
		i960->AC |= 2;
	else
		i960->AC |= 1;
}

INLINE void concmp_u(i960_state_t *i960, UINT32 v1, UINT32 v2)
{
	i960->AC &= ~7;
	if(v1 <= v2)
		i960->AC |= 2;
	else
		i960->AC |= 1;
}

INLINE void cmp_d(i960_state_t *i960, double v1, double v2)
{
	i960->AC &= ~7;
	if(v1<v2)
		i960->AC |= 4;
	else if(v1 == v2)
		i960->AC |= 2;
	else if(v1 > v2)
		i960->AC |= 1;
}

INLINE void bxx(i960_state_t *i960, UINT32 opcode, int mask)
{
	if(i960->AC & mask) {
		i960->IP += get_disp(i960, opcode);
	}
}

INLINE void bxx_s(i960_state_t *i960, UINT32 opcode, int mask)
{
	if(i960->AC & mask) {
		i960->IP += get_disp_s(i960, opcode);
	}
}

INLINE void test(i960_state_t *i960, UINT32 opcode, int mask)
{
	if(i960->AC & mask)
		i960->r[(opcode>>19) & 0x1f] = 1;
	else
		i960->r[(opcode>>19) & 0x1f] = 0;
}

INLINE const char *i960_get_strflags(i960_state_t *i960)
{
	static const char *const conditions[8] =
	{
		"no", "g", "e", "ge", "l", "ne", "le", "o"
	};

	return (conditions[i960->AC & 7]);
}

// interrupt dispatch
static void take_interrupt(i960_state_t *i960, int vector, int lvl)
{
	int int_tab =  memory_read_dword_32le(i960->program, i960->PRCB+20);	// interrupt table
	int int_SP  =  memory_read_dword_32le(i960->program, i960->PRCB+24);	// interrupt stack
	int SP;
	UINT32 IRQV;

	IRQV = memory_read_dword_32le(i960->program, int_tab + 36 + (vector-8)*4);

	// start the process
	if(!(i960->PC & 0x2000))	// if this is a nested interrupt, don't re-get int_SP
	{
		SP = int_SP;
	}
	else
	{
		SP = i960->r[I960_SP];
	}

	SP = (SP + 63) & ~63;
	SP += 128;	// emulate ElSemi's core, this fixes the crash in sonic the fighters

	do_call(i960, IRQV, 7, SP);

	// save the processor state
	memory_write_dword_32le(i960->program, i960->r[I960_FP]-16, i960->PC);
	memory_write_dword_32le(i960->program, i960->r[I960_FP]-12, i960->AC);
	// store the vector
	memory_write_dword_32le(i960->program, i960->r[I960_FP]-8, vector-8);

	i960->PC &= ~0x1f00;	// clear priority, state, trace-fault pending, and trace enable
	i960->PC |= (lvl<<16);	// set CPU level to current IRQ level
	i960->PC |= 0x2002;	// set supervisor mode & interrupt flag
}

static void check_irqs(i960_state_t *i960)
{
	int int_tab =  memory_read_dword_32le(i960->program, i960->PRCB+20);	// interrupt table
	int cpu_pri = (i960->PC>>16)&0x1f;
	int pending_pri;
	int lvl, irq, take = -1;
	int vword;
	static const UINT32 lvlmask[4] = { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 };

	pending_pri = memory_read_dword_32le(i960->program, int_tab);		// read pending priorities

	if ((i960->immediate_irq) && ((cpu_pri < i960->immediate_pri) || (i960->immediate_pri == 31)))
	{
		take_interrupt(i960, i960->immediate_vector, i960->immediate_pri);
		i960->immediate_irq = 0;
	}
	else
	{
		for(lvl = 31; lvl >= 0; lvl--) {
			if((pending_pri & (1 << lvl)) && ((cpu_pri < lvl) || (lvl == 31))) {
				int word, wordl, wordh;

				// figure out which word contains this level's priorities
				word = ((lvl / 4) * 4) + 4;	// (lvl/4) = word address, *4 for byte address, +4 to skip pending priorities
				wordl = (lvl % 4) * 8;
				wordh = (wordl + 8) - 1;

				vword = memory_read_dword_32le(i960->program, int_tab + word);

				// take the first vector we find for this level
				for (irq = wordh; irq >= wordl; irq--) {
					if(vword & (1 << irq)) {
						// clear pending bit
						vword &= ~(1 << irq);
						memory_write_dword_32le(i960->program, int_tab + word, vword);
						take = irq;
						break;
					}
				}

				// if no vectors were found at our level, it's an error
				if(take == -1) {
					logerror("i960: ERROR! no vector found for pending level %d\n", lvl);

					// try to recover...
					pending_pri &= ~(1 << lvl);
					memory_write_dword_32le(i960->program, int_tab, pending_pri);
					return;
				}

				// if no vectors are waiting for this level, clear the level bit
				if(!(vword & lvlmask[lvl % 4])) {
					pending_pri &= ~(1 << lvl);
					memory_write_dword_32le(i960->program, int_tab, pending_pri);
				}

				take += ((lvl/4) * 32);

				take_interrupt(i960, take, lvl);
				return;
			}
		}
	}
}

static void do_call(i960_state_t *i960, UINT32 adr, int type, UINT32 stack)
{
	int i;
	UINT32 FP;

	// call and callx take 9 cycles base
	i960->icount -= 9;

	// set the new RIP
	i960->r[I960_RIP] = i960->IP;
//  mame_printf_debug("CALL (type %d): FP %x, %x => %x, stack %x, rcache_pos %d\n", type, i960->r[I960_FP], i960->r[I960_RIP], adr, stack, i960->rcache_pos);

	// are we out of cache entries?
	if (i960->rcache_pos >= RCACHE_SIZE) {
		// flush the current register set to the current frame
		FP = i960->r[I960_FP] & ~0x3f;
		for (i = 0; i < 16; i++) {
			memory_write_dword_32le(i960->program, FP + (i*4), i960->r[i]);
		}
	}
	else	// a cache entry is available, use it
	{
		memcpy(&i960->rcache[i960->rcache_pos][0], i960->r, 0x10 * sizeof(UINT32));
		i960->rcache_frame_addr[i960->rcache_pos] = i960->r[I960_FP] & ~0x3f;
	}
	i960->rcache_pos++;

	i960->IP = adr;
	i960->r[I960_PFP] = i960->r[I960_FP] & ~7;
	i960->r[I960_PFP] |= type;

	if(type == 7) {	// interrupts need special handling
		// set the stack to the passed-in value to properly handle nested interrupts
		// (can't set it externally or the original program's SP will be lost)
		i960->r[I960_SP] = stack;
	}

	i960->r[I960_FP]  = (i960->r[I960_SP] + 63) & ~63;
	i960->r[I960_SP]  = i960->r[I960_FP] + 64;
}

static void do_ret_0(i960_state_t *i960)
{
//  int type = i960->r[I960_PFP] & 7;

	i960->r[I960_FP] = i960->r[I960_PFP] & ~0x3f;

	i960->rcache_pos--;

	// normal situation: if we're still above rcache size, we're not in cache.
	// abnormal situation (after the app does a FLUSHREG): rcache_pos will be 0
	// coming in, but we must still treat it as a not-in-cache situation.
	if ((i960->rcache_pos >= RCACHE_SIZE) || (i960->rcache_pos < 0))
	{
		int i;
		for(i=0; i<0x10; i++)
			i960->r[i] = memory_read_dword_32le(i960->program, i960->r[I960_FP]+4*i);

		if (i960->rcache_pos < 0)
		{
			i960->rcache_pos = 0;
		}
	}
	else
	{
		memcpy(i960->r, i960->rcache[i960->rcache_pos], 0x10*sizeof(UINT32));
	}

//  mame_printf_debug("RET (type %d): FP %x, %x => %x, rcache_pos %d\n", type, i960->r[I960_FP], i960->IP, i960->r[I960_RIP], i960->rcache_pos);
	i960->IP = i960->r[I960_RIP];
}

static void do_ret(i960_state_t *i960)
{
	UINT32 x, y;
	i960->icount -= 7;
	switch(i960->r[I960_PFP] & 7) {
	case 0:
		do_ret_0(i960);
		break;

	case 7:
		x = memory_read_dword(i960->program, i960->r[I960_FP]-16);
		y = memory_read_dword(i960->program, i960->r[I960_FP]-12);
		do_ret_0(i960);
		i960->AC = y;
		// #### test supervisor
		i960->PC = x;

		// check for another IRQ now that we're back
		check_irqs(i960);
		break;

	default:
		fatalerror("I960: %x: Unsupported return mode %d", i960->PIP, i960->r[I960_PFP] & 7);
	}
}

INLINE void execute_op(i960_state_t *i960, UINT32 opcode)
{
	UINT32 t1, t2;
	double t1f, t2f;

	switch(opcode >> 24) {
		case 0x08: // b
			i960->icount--;
			i960->IP += get_disp(i960, opcode);
			break;

		case 0x09: // call
			do_call(i960, i960->IP+get_disp(i960, opcode), 0, i960->r[I960_SP]);
			break;

		case 0x0a: // ret
			do_ret(i960);
			break;

		case 0x0b: // bal
			i960->icount -= 5;
			i960->r[0x1e] = i960->IP;
			i960->IP += get_disp(i960, opcode);
			break;

		case 0x10: // bno
			i960->icount--;
			if(!(i960->AC & 7)) {
				i960->IP += get_disp(i960, opcode);
			}
			break;

		case 0x11: // bg
			i960->icount--;
			bxx(i960, opcode, 1);
			break;

		case 0x12: // be
			i960->icount--;
			bxx(i960, opcode, 2);
			break;

		case 0x13: // bge
			i960->icount--;
			bxx(i960, opcode, 3);
			break;

		case 0x14: // bl
			i960->icount--;
			bxx(i960, opcode, 4);
			break;

		case 0x15: // bne
			i960->icount--;
			bxx(i960, opcode, 5);
			break;

		case 0x16: // ble
			i960->icount--;
			bxx(i960, opcode, 6);
			break;

		case 0x17: // bo
			i960->icount--;
			bxx(i960, opcode, 7);
			break;

		case 0x20: // testno
			i960->icount--;
			if(!(i960->AC & 7))
				i960->r[(opcode>>19) & 0x1f] = 1;
			else
				i960->r[(opcode>>19) & 0x1f] = 0;
			break;

		case 0x21: // testg
			i960->icount--;
			test(i960, opcode, 1);
			break;

		case 0x22: // teste
			i960->icount--;
			test(i960, opcode, 2);
			break;

		case 0x23: // testge
			i960->icount--;
			test(i960, opcode, 3);
			break;

		case 0x24: // testl
			i960->icount--;
			test(i960, opcode, 4);
			break;

		case 0x25: // testne
			i960->icount--;
			test(i960, opcode, 5);
			break;

		case 0x26: // testle
			i960->icount--;
			test(i960, opcode, 6);
			break;

		case 0x27: // testo
			i960->icount--;
			test(i960, opcode, 7);
			break;

		case 0x30: // bbc
			i960->icount -= 4;
			t1 = get_1_ci(i960, opcode) & 0x1f;
			t2 = get_2_ci(i960, opcode);
			if(!(t2 & (1<<t1))) {
				i960->AC = (i960->AC & ~7) | 2;
				i960->IP += get_disp_s(i960, opcode);
			} else
				i960->AC &= ~7;
			break;

		case 0x31: // cmp0bg
			i960->icount -= 4;
			t1 = get_1_ci(i960, opcode);
			t2 = get_2_ci(i960, opcode);
			cmp_u(i960, t1, t2);
			bxx_s(i960, opcode, 1);
			break;

		case 0x32: // cmpobe
			i960->icount -= 4;
			t1 = get_1_ci(i960, opcode);
			t2 = get_2_ci(i960, opcode);
			cmp_u(i960, t1, t2);
			bxx_s(i960, opcode, 2);
			break;

		case 0x33: // cmpobge
			i960->icount -= 4;
			t1 = get_1_ci(i960, opcode);
			t2 = get_2_ci(i960, opcode);
			cmp_u(i960, t1, t2);
			bxx_s(i960, opcode, 3);
			break;

		case 0x34: // cmpobl
			i960->icount -= 4;
			t1 = get_1_ci(i960, opcode);
			t2 = get_2_ci(i960, opcode);
			cmp_u(i960, t1, t2);
			bxx_s(i960, opcode, 4);
			break;

		case 0x35: // cmpobne
			i960->icount -= 4;
			t1 = get_1_ci(i960, opcode);
			t2 = get_2_ci(i960, opcode);
			cmp_u(i960, t1, t2);
			bxx_s(i960, opcode, 5);
			break;

		case 0x36: // cmpoble
			i960->icount -= 4;
			t1 = get_1_ci(i960, opcode);
			t2 = get_2_ci(i960, opcode);
			cmp_u(i960, t1, t2);
			bxx_s(i960, opcode, 6);
			break;

		case 0x37: // bbs
			i960->icount -= 4;
			t1 = get_1_ci(i960, opcode) & 0x1f;
			t2 = get_2_ci(i960, opcode);
			if(t2 & (1<<t1)) {
				i960->AC = (i960->AC & ~7) | 2;
				i960->IP += get_disp_s(i960, opcode);
			} else
				i960->AC &= ~7;
			break;

		case 0x39: // cmpibg
			i960->icount -= 4;
			t1 = get_1_ci(i960, opcode);
			t2 = get_2_ci(i960, opcode);
			cmp_s(i960, t1, t2);
			bxx_s(i960, opcode, 1);
			break;

		case 0x3a: // cmpibe
			i960->icount -= 4;
			t1 = get_1_ci(i960, opcode);
			t2 = get_2_ci(i960, opcode);
			cmp_s(i960, t1, t2);
			bxx_s(i960, opcode, 2);
			break;

		case 0x3b: // cmpibge
			i960->icount -= 4;
			t1 = get_1_ci(i960, opcode);
			t2 = get_2_ci(i960, opcode);
			cmp_s(i960, t1, t2);
			bxx_s(i960, opcode, 3);
			break;

		case 0x3c: // cmpibl
			i960->icount -= 4;
			t1 = get_1_ci(i960, opcode);
			t2 = get_2_ci(i960, opcode);
			cmp_s(i960, t1, t2);
			bxx_s(i960, opcode, 4);
			break;

		case 0x3d: // cmpibne
			i960->icount -= 4;
			t1 = get_1_ci(i960, opcode);
			t2 = get_2_ci(i960, opcode);
			cmp_s(i960, t1, t2);
			bxx_s(i960, opcode, 5);
			break;

		case 0x3e: // cmpible
			i960->icount -= 4;
			t1 = get_1_ci(i960, opcode);
			t2 = get_2_ci(i960, opcode);
			cmp_s(i960, t1, t2);
			bxx_s(i960, opcode, 6);
			break;

		case 0x58:
			switch((opcode >> 7) & 0xf) {
			case 0x0: // notbit
				i960->icount -= 2;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, t2 ^ (1<<(t1 & 31)));
				break;

			case 0x1: // and
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, t2 & t1);
				break;

			case 0x2: // andnot
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, t2 & ~t1);
				break;

			case 0x3: // setbit
				i960->icount -= 2;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, t2 | (1<<(t1 & 31)));
				break;

			case 0x4: // notand
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, (~t2) & t1);
				break;

			case 0x6: // xor
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, t2 ^ t1);
				break;

			case 0x7: // or
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, t2 | t1);
				break;

			case 0x8: // nor
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, ((~t2) & (~t1)));
				break;

			case 0x9: // xnor
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, ~(t2 ^ t1));
				break;

			case 0xa: // not
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				set_ri(i960, opcode, ~t1);
				break;

			case 0xb: // ornot
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, t2 | ~t1);
				break;

			case 0xc: // clrbit
				i960->icount -= 2;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, t2 & ~(1<<(t1 & 31)));
				break;

			case 0xd: // notor
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, (~t2) | t1);
				break;

			case 0xe: // nand
				i960->icount -= 2;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, ~t2 | ~t1);
				break;

			case 0xf: // alterbit
				i960->icount -= 2;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				if(i960->AC & 2)
					set_ri(i960, opcode, t2 | (1<<(t1 & 31)));
				else
					set_ri(i960, opcode, t2 & ~(1<<(t1 & 31)));
				break;

			default:
				fatalerror("I960: %x: Unhandled 58.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x59:
			switch((opcode >> 7) & 0xf) {
			case 0x0: // addo
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, t2+t1);
				break;

			case 0x1: // addi
				// #### overflow
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, t2+t1);
				break;

			case 0x2: // subo
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, t2-t1);
				break;

			case 0x3: // subi
				// #### overflow
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, t2-t1);
				break;

			case 0x8: // shro
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, t2>>t1);
				break;

			case 0xa: // shrdi
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				if(((INT32)t2) < 0) {
					if(t2 & ((1<<t1)-1))
						set_ri(i960, opcode, (((INT32)t2)>>t1)+1);
					else
						set_ri(i960, opcode, ((INT32)t2)>>t1);
				} else
					set_ri(i960, opcode, t2>>t1);
				break;

			case 0xb: // shri
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, ((INT32)t2)>>t1);
				break;

			case 0xc: // shlo
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, t2<<t1);
				break;

			case 0xd: // rotate
				i960->icount--;
				t1 = get_1_ri(i960, opcode) & 0x1f;
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, (t2<<t1)|(t2>>(32-t1)));
				break;

			case 0xe: // shli
				// missing overflow
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, t2<<t1);
				break;

			default:
				fatalerror("I960: %x: Unhandled 59.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x5a:
			switch((opcode >> 7) & 0xf) {
			case 0x0: // cmpo
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				cmp_u(i960, t1, t2);
				break;

			case 0x1: // cmpi
				i960->icount--;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				cmp_s(i960, t1, t2);
				break;

			case 0x2: // concmpo
				i960->icount--;
				if(!(i960->AC & 0x4)) {
					t1 = get_1_ri(i960, opcode);
					t2 = get_2_ri(i960, opcode);
					concmp_u(i960, t1, t2);
				}
				break;

			case 0x3: // concmpi
				i960->icount--;
				if(!(i960->AC & 0x4)) {
					t1 = get_1_ri(i960, opcode);
					t2 = get_2_ri(i960, opcode);
					concmp_s(i960, t1, t2);
				}
				break;

			case 0x4: // cmpinco
				i960->icount -= 2;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				cmp_u(i960, t1, t2);
				set_ri(i960, opcode, t2+1);
				break;

			case 0x5: // cmpinci
				i960->icount -= 2;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				cmp_s(i960, t1, t2);
				set_ri(i960, opcode, t2+1);
				break;

			case 0x6: // cmpdeco
				i960->icount -= 2;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				cmp_u(i960, t1, t2);
				set_ri(i960, opcode, t2-1);
				break;

			case 0x7: // cmpdeci
				i960->icount -= 2;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				cmp_s(i960, t1, t2);
				set_ri(i960, opcode, t2-1);
				break;

			case 0xe: // chkbit
				i960->icount -= 2;
				t1 = get_1_ri(i960, opcode) & 0x1f;
				t2 = get_2_ri(i960, opcode);
				if(t2 & (1<<t1))
					i960->AC = (i960->AC & ~7) | 2;
				else
					i960->AC &= ~7;
				break;

			default:
				fatalerror("I960: %x: Unhandled 5a.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x5b:
			switch((opcode >> 7) & 0xf) {
			case 0x0:	// addc
				{
					UINT64 res;

					i960->icount -= 2;
					t1 = get_1_ri(i960, opcode);
					t2 = get_2_ri(i960, opcode);
					res = t2+(t1+((i960->AC>>1)&1));
					set_ri(i960, opcode, res&0xffffffff);

					i960->AC &= ~0x3;	// clear C and V
					// set carry
					i960->AC |= ((res) & (((UINT64)1) << 32)) ? 0x2 : 0;
					// set overflow
					i960->AC |= (((res) ^ (t1)) & ((res) ^ (t2)) & 0x80000000) ? 1: 0;
				}
				break;

			case 0x2:	// subc
				{
					UINT64 res;

					i960->icount -= 2;
					t1 = get_1_ri(i960, opcode);
					t2 = get_2_ri(i960, opcode);
					res = t2-(t1+((i960->AC>>1)&1));
					set_ri(i960, opcode, res&0xffffffff);

					i960->AC &= ~0x3;	// clear C and V
					// set carry
					i960->AC |= ((res) & (((UINT64)1) << 32)) ? 0x2 : 0;
					// set overflow
					i960->AC |= (((t2) ^ (t1)) & ((t2) ^ (res)) & 0x80000000) ? 1 : 0;
				}
				break;

			default:
				fatalerror("I960: %x: Unhandled 5b.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x5c:
			switch((opcode >> 7) & 0xf) {
			case 0xc: // mov
				i960->icount -= 2;
				t1 = get_1_ri(i960, opcode);
				set_ri(i960, opcode, t1);
				break;

			default:
				fatalerror("I960: %x: Unhandled 5c.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x5d:
			switch((opcode >> 7) & 0xf) {
			case 0xc: // movl
				i960->icount -= 2;
				t2 = (opcode>>19) & 0x1e;
				if(opcode & 0x00000800) { // litteral
					t1 = opcode & 0x1f;
					i960->r[t2] = i960->r[t2+1] = t1;
				} else
					memcpy(i960->r+t2, i960->r+(opcode & 0x1f), 2*sizeof(UINT32));
				break;

			default:
				fatalerror("I960: %x: Unhandled 5d.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x5e:
			switch((opcode >> 7) & 0xf) {
			case 0xc: // movt
				i960->icount -= 3;
				t2 = (opcode>>19) & 0x1c;
				if(opcode & 0x00000800) { // litteral
					t1 = opcode & 0x1f;
					i960->r[t2] = i960->r[t2+1] = i960->r[t2+2]= t1;
				} else
					memcpy(i960->r+t2, i960->r+(opcode & 0x1f), 3*sizeof(UINT32));
				break;

			default:
				fatalerror("I960: %x: Unhandled 5e.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x5f:
			switch((opcode >> 7) & 0xf) {
			case 0xc: // movq
				i960->icount -= 4;
				t2 = (opcode>>19) & 0x1c;
				if(opcode & 0x00000800) { // litteral
					t1 = opcode & 0x1f;
					i960->r[t2] = i960->r[t2+1] = i960->r[t2+2] = i960->r[t2+3] = t1;
				} else
					memcpy(i960->r+t2, i960->r+(opcode & 0x1f), 4*sizeof(UINT32));
				break;

			default:
				fatalerror("I960: %x: Unhandled 5f.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x60:
			switch((opcode >> 7) & 0xf) {
			case 0x0: // synmov
				i960->icount -= 6;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				// interrupt control register
				if(t1 == 0xff000004)
					i960->ICR = memory_read_dword_32le(i960->program, t2);
				else
					memory_write_dword_32le(i960->program, t1,    memory_read_dword_32le(i960->program, t2));
				i960->AC = (i960->AC & ~7) | 2;
				break;

			case 0x2: // synmovq
				i960->icount -= 12;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				if(t1 == 0xff000010)
					send_iac(i960, t2);
				else {
					memory_write_dword_32le(i960->program, t1,    memory_read_dword_32le(i960->program, t2));
					memory_write_dword_32le(i960->program, t1+4,  memory_read_dword_32le(i960->program, t2+4));
					memory_write_dword_32le(i960->program, t1+8,  memory_read_dword_32le(i960->program, t2+8));
					memory_write_dword_32le(i960->program, t1+12, memory_read_dword_32le(i960->program, t2+12));
				}
				i960->AC = (i960->AC & ~7) | 2;
				break;

			default:
				fatalerror("I960: %x: Unhandled 60.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x64:
			switch((opcode >> 7) & 0xf) {
			case 0x0: // spanbit
				{
					UINT32 res = 0xffffffff;
					int i;

					i960->icount -= 10;

					t1 = get_1_ri(i960, opcode);
					i960->AC &= ~7;

					for (i = 31; i >= 0; i--)
					{
						if (!(t1 & (1<<i)))
						{
							i960->AC |= 2;
							res = i;
							break;
						}
					}

					set_ri(i960, opcode, res);
				}
				break;

			case 0x1: // scanbit
				{
					UINT32 res = 0xffffffff;
					int i;

					i960->icount -= 10;

					t1 = get_1_ri(i960, opcode);
					i960->AC &= ~7;

					for (i = 31; i >= 0; i--)
					{
						if (t1 & (1<<i))
						{
							i960->AC |= 2;
							res = i;
							break;
						}
					}

					set_ri(i960, opcode, res);
				}
				break;

			case 0x5: // modac
				i960->icount -= 10;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, i960->AC);
				i960->AC = (i960->AC & ~t1) | (t2 & t1);
				break;

			default:
				fatalerror("I960: %x: Unhandled 64.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x65:
			switch((opcode >> 7) & 0xf) {
			case 0x5: // modpc
				i960->icount -= 10;
				t1 = i960->PC;
				t2 = get_2_ri(i960, opcode);
				i960->PC = (i960->PC & ~t2) | (i960->r[(opcode>>19) & 0x1f] & t2);
				set_ri(i960, opcode, t1);
				break;

			default:
				fatalerror("I960: %x: Unhandled 65.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x66:
			switch((opcode >> 7) & 0xf) {
			case 0xd: // flushreg
				if (i960->rcache_pos > 4)
				{
					i960->rcache_pos = 4;
				}
				for(t1=0; t1 < i960->rcache_pos; t1++)
				{
					int i;

					for (i = 0; i < 0x10; i++)
					{
						memory_write_dword_32le(i960->program, i960->rcache_frame_addr[t1] + (i * sizeof(UINT32)), i960->rcache[t1][i]);
					}
				}
				i960->rcache_pos = 0;
				break;

			default:
				fatalerror("I960: %x: Unhandled 66.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x67:
			switch((opcode >> 7) & 0xf) {
			case 0x0: // emul
				i960->icount -= 37;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);

				set_ri64(i960, opcode, (INT64)t1 * (INT64)t2);
				break;

			case 0x1: // ediv
				i960->icount -= 37;
				{
					UINT64 src1, src2;

					src1 = get_1_ri(i960, opcode);
					src2 = get_2_ri64(i960, opcode);

					set_ri2(i960, opcode, src2 % src1, src2 / src1);
				}
				break;

			case 0x4: // cvtir
				i960->icount -= 30;
				t1 = get_1_ri(i960, opcode);
				set_rif(i960, opcode, (double)(INT32)t1);
				break;

			case 0x6: // scalerl
				i960->icount -= 30;
				t1 = get_1_ri(i960, opcode);
				t2f = get_2_rifl(i960, opcode);
				set_rifl(i960, opcode, t2f * pow(2.0, (double)(INT32)t1));
				break;

			case 0x7: // scaler
				i960->icount -= 30;
				t1 = get_1_ri(i960, opcode);
				t2f = get_2_rif(i960, opcode);
			set_rif(i960, opcode, t2f * pow(2.0, (double)(INT32)t1));
				break;

			default:
				fatalerror("I960: %x: Unhandled 67.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x68:
			switch((opcode >> 7) & 0xf) {
			case 0x0: // atanr
				i960->icount -= 267;
				t1f = get_1_rif(i960, opcode);
				t2f = get_2_rif(i960, opcode);
				set_rif(i960, opcode, atan2(t2f, t1f));
				break;

			case 0x1: // logepr
				i960->icount -= 400;
				t1f = get_1_rif(i960, opcode);
				t2f = get_2_rif(i960, opcode);
				set_rif(i960, opcode, t2f*log(t1f+1.0)/log(2.0));
				break;

			case 0x3: // remr
				i960->icount -= 67;	// (67 to 75878 depending on opcodes!!!)
				t1f = get_1_rif(i960, opcode);
				t2f = get_2_rif(i960, opcode);
				set_rif(i960, opcode, fmod(t2f, t1f));
				break;

			case 0x5: // cmpr
				i960->icount -= 10;
				t1f = get_1_rif(i960, opcode);
				t2f = get_2_rif(i960, opcode);
				cmp_d(i960, t1f, t2f);
				break;

			case 0x8: // sqrtr
				i960->icount -= 104;
				t1f = get_1_rif(i960, opcode);
				set_rif(i960, opcode, sqrt(t1f));
				break;

			case 0xa: // logbnr
				i960->icount -= 37;
				t1f = get_1_rif(i960, opcode);
				set_rif(i960, opcode, logb(t1f));
				break;

			case 0xb: // roundr
				{
					INT32 st1 = get_1_rif(i960, opcode);
					i960->icount -= 69;
					set_rif(i960, opcode, (double)st1);
				}
				break;

			case 0xc: // sinr
				i960->icount -= 406;
				t1f = get_1_rif(i960, opcode);
				set_rif(i960, opcode, sin(t1f));
				break;

			case 0xd: // cosr
				i960->icount -= 406;
				t1f = get_1_rif(i960, opcode);
				set_rif(i960, opcode, sin(t1f));
				break;

			case 0xe: // tanr
				i960->icount -= 293;
				t1f = get_1_rif(i960, opcode);
				set_rif(i960, opcode, tan(t1f));
				break;

			default:
				fatalerror("I960: %x: Unhandled 68.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x69:
			switch((opcode >> 7) & 0xf) {
			case 0x0: // atanrl
				i960->icount -= 350;
				t1f = get_1_rifl(i960, opcode);
				t2f = get_2_rifl(i960, opcode);
				set_rifl(i960, opcode, atan2(t2f, t1f));
				break;

			case 0x2: // logrl
				i960->icount -= 438;
				t1f = get_1_rifl(i960, opcode);
				set_rifl(i960, opcode, log(t1f));
				break;

			case 0x5: // cmprl
				i960->icount -= 12;
				t1f = get_1_rifl(i960, opcode);
				t2f = get_2_rifl(i960, opcode);
				cmp_d(i960, t1f, t2f);
				break;

			case 0x8: // sqrtrl
				i960->icount -= 104;
				t1f = get_1_rifl(i960, opcode);
				set_rifl(i960, opcode, sqrt(t1f));
				break;

			case 0x9: // exprl
				i960->icount -= 334;
				t1f = get_1_rifl(i960, opcode);
				set_rifl(i960, opcode, pow(2.0, t1f)-1.0);
				break;

			case 0xa: // logbnrl
				i960->icount -= 37;
				t1f = get_1_rifl(i960, opcode);
				set_rifl(i960, opcode, logb(t1f));
				break;

			case 0xb: // roundrl
				{
					INT32 st1 = get_1_rifl(i960, opcode);
					i960->icount -= 70;
					set_rifl(i960, opcode, (double)st1);
				}
				break;

			case 0xc: // sinrl
				i960->icount -= 441;
				t1f = get_1_rifl(i960, opcode);
				set_rifl(i960, opcode, sin(t1f));
				break;

			case 0xd: // cosrl
				i960->icount -= 441;
				t1f = get_1_rifl(i960, opcode);
				set_rifl(i960, opcode, cos(t1f));
				break;

			case 0xe: // tanrl
				i960->icount -= 323;
				t1f = get_1_rifl(i960, opcode);
				set_rifl(i960, opcode, tan(t1f));
				break;

			default:
				fatalerror("I960: %x: Unhandled 69.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x6c:
			switch((opcode >> 7) & 0xf) {
			case 0x0: // cvtri
				i960->icount -= 33;
				t1f = get_1_rif(i960, opcode);
				// apply rounding mode
				// we do this a little indirectly to avoid some odd GCC warnings
				t2f = 0.0;
				switch((i960->AC>>30)&3)
				{
					case 0: t2f = floor(t1f+0.5); break;
					case 1: t2f = floor(t1f); break;
					case 2: t2f = ceil(t1f); break;
					case 3: t2f = t1f; break;
				}
				set_ri(i960, opcode, (INT32)t2f);
				break;

			case 0x2: // cvtzri
				i960->icount -= 43;
				t1f = get_1_rif(i960, opcode);
				set_ri(i960, opcode, (INT32)t1f);
				break;

			case 0x3: // cvtzril
				i960->icount -= 44;
				t1f = get_1_rif(i960, opcode);
				set_ri64(i960, opcode, (INT64)t1f);
				break;

			case 0x9: // movr
				i960->icount -= 5;
				t1f = get_1_rif(i960, opcode);
				set_rif(i960, opcode, t1f);
				break;

			default:
				fatalerror("I960: %x: Unhandled 6c.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x6d:
			switch((opcode >> 7) & 0xf) {
			case 0x9: // movrl
				i960->icount -= 6;
				t1f = get_1_rifl(i960, opcode);
				set_rifl(i960, opcode, t1f);
				break;

			default:
				fatalerror("I960: %x: Unhandled 6d.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x6e:
			switch((opcode >> 7) & 0xf) {
			case 0x1: // movre
				{
					UINT32 *src=0, *dst=0;

					i960->icount -= 8;

					if(!(opcode & 0x00000800)) {
						src = (UINT32 *)&i960->r[opcode & 0x1e];
					} else {
						int idx = opcode & 0x1f;
						if(idx < 4)
							src = (UINT32 *)&i960->fp[idx];
					}

					if(!(opcode & 0x00002000)) {
						dst = (UINT32 *)&i960->r[(opcode>>19) & 0x1e];
					} else if(!(opcode & 0x00e00000))
						dst = (UINT32 *)&i960->fp[(opcode>>19) & 3];

					dst[0] = src[0];
					dst[1] = src[1];
					dst[2] = src[2]&0xffff;
				}
				break;
			case 0x2: // cpysre
				i960->icount -= 8;
				t1f = get_1_rifl(i960, opcode);
				t2f = get_2_rifl(i960, opcode);

				if (t2f >= 0.0)
					set_rifl(i960, opcode, fabs(t1f));
				else
					set_rifl(i960, opcode, -fabs(t1f));
				break;
			default:
				fatalerror("I960: %x: Unhandled 6e.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x70:
			switch((opcode >> 7) & 0xf) {
			case 0x1: // mulo
				i960->icount -= 18;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, t2*t1);
				break;

			case 0x8: // remo
				i960->icount -= 37;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, t2%t1);
				break;

			case 0xb: // divo
				i960->icount -= 37;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				if (t1 == 0)	// HACK!
					set_ri(i960, opcode, 0);
				else
					set_ri(i960, opcode, t2/t1);
				break;

			default:
				fatalerror("I960: %x: Unhandled 70.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x74:
			switch((opcode >> 7) & 0xf) {
			case 0x1: // muli
				i960->icount -= 18;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, ((INT32)t2)*((INT32)t1));
				break;

			case 0x8: // remi
				i960->icount -= 37;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, ((INT32)t2)%((INT32)t1));
				break;

			case 0x9:{// modi
				INT32 src1, src2, dst;
				i960->icount -= 37;
				src1 = (INT32)get_1_ri(i960, opcode);
				src2 = (INT32)get_2_ri(i960, opcode);
				dst = src2 - ((src2/src1)*src1);
				if(((src2*src1) < 0) && (dst != 0))
					dst += src1;
				set_ri(i960, opcode, dst);
				break;
			}

			case 0xb: // divi
				i960->icount -= 37;
				t1 = get_1_ri(i960, opcode);
				t2 = get_2_ri(i960, opcode);
				set_ri(i960, opcode, ((INT32)t2)/((INT32)t1));
				break;

			default:
				fatalerror("I960: %x: Unhandled 74.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x78:
			switch((opcode >> 7) & 0xf) {
			case 0xb: // divr
				i960->icount -= 35;
				t1f = get_1_rif(i960, opcode);
				t2f = get_2_rif(i960, opcode);
				set_rif(i960, opcode, t2f/t1f);
				break;

			case 0xc: // mulr
				i960->icount -= 18;
				t1f = get_1_rif(i960, opcode);
				t2f = get_2_rif(i960, opcode);
				set_rif(i960, opcode, t2f*t1f);
				break;

			case 0xd: // subr
				i960->icount -= 10;
				t1f = get_1_rif(i960, opcode);
				t2f = get_2_rif(i960, opcode);
				set_rif(i960, opcode, t2f-t1f);
				break;

			case 0xf: // addr
				i960->icount -= 10;
				t1f = get_1_rif(i960, opcode);
				t2f = get_2_rif(i960, opcode);
				set_rif(i960, opcode, t2f+t1f);
				break;

			default:
				fatalerror("I960: %x: Unhandled 78.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x79:
			switch((opcode >> 7) & 0xf) {
			case 0xb: // divrl
				i960->icount -= 77;
				t1f = get_1_rifl(i960, opcode);
				t2f = get_2_rifl(i960, opcode);
				set_rifl(i960, opcode, t2f/t1f);
				break;

			case 0xc: // mulrl
				i960->icount -= 36;
				t1f = get_1_rifl(i960, opcode);
				t2f = get_2_rifl(i960, opcode);
				set_rifl(i960, opcode, t2f*t1f);
				break;

			case 0xd: // subrl
				i960->icount -= 13;
				t1f = get_1_rifl(i960, opcode);
				t2f = get_2_rifl(i960, opcode);
				set_rifl(i960, opcode, t2f-t1f);
				break;

			case 0xf: // addrl
				i960->icount -= 13;
				t1f = get_1_rifl(i960, opcode);
				t2f = get_2_rifl(i960, opcode);
				set_rifl(i960, opcode, t2f+t1f);
				break;

			default:
				fatalerror("I960: %x: Unhandled 79.%x", i960->PIP, (opcode >> 7) & 0xf);
			}
			break;

		case 0x80: // ldob
			i960->icount -= 4;
			i960->r[(opcode>>19)&0x1f] = memory_read_byte_32le(i960->program, get_ea(i960, opcode));
			break;

		case 0x82: // stob
			i960->icount -= 2;
			memory_write_byte_32le(i960->program, get_ea(i960, opcode), i960->r[(opcode>>19)&0x1f]);
			break;

		case 0x84: // bx
			i960->icount -= 3;
			i960->IP = get_ea(i960, opcode);
			break;

		case 0x85: // balx
			i960->icount -= 5;
			t1 = get_ea(i960, opcode);
			i960->r[(opcode>>19)&0x1f] = i960->IP;
			i960->IP = t1;
			break;

		case 0x86: // callx
			t1 = get_ea(i960, opcode);
			do_call(i960, t1, 0, i960->r[I960_SP]);
			break;

		case 0x88: // ldos
			i960->icount -= 4;
			i960->r[(opcode>>19)&0x1f] = i960_read_word_unaligned(i960, get_ea(i960, opcode));
			break;

		case 0x8a: // stos
			i960->icount -= 2;
			i960_write_word_unaligned(i960, get_ea(i960, opcode), i960->r[(opcode>>19)&0x1f]);
			break;

		case 0x8c: // lda
			i960->icount--;
			i960->r[(opcode>>19)&0x1f] = get_ea(i960, opcode);
			break;

		case 0x90: // ld
			i960->icount -= 4;
			i960->r[(opcode>>19)&0x1f] = i960_read_dword_unaligned(i960, get_ea(i960, opcode));
			break;

		case 0x92: // st
			i960->icount -= 2;
			i960_write_dword_unaligned(i960, get_ea(i960, opcode), i960->r[(opcode>>19)&0x1f]);
			break;

		case 0x98:{// ldl
			int i;
			i960->icount -= 5;
			t1 = get_ea(i960, opcode);
			t2 = (opcode>>19)&0x1e;
			i960->bursting = 1;
			for(i=0; i<2; i++) {
				i960->r[t2+i] = i960_read_dword_unaligned(i960, t1);
				if(i960->bursting)
					t1 += 4;
			}
			break;
		}

		case 0x9a:{// stl
			int i;
			i960->icount -= 3;
			t1 = get_ea(i960, opcode);
			t2 = (opcode>>19)&0x1e;
			i960->bursting = 1;
			for(i=0; i<2; i++) {
				i960_write_dword_unaligned(i960, t1, i960->r[t2+i]);
				if(i960->bursting)
					t1 += 4;
			}
			break;
		}

		case 0xa0:{// ldt
			int i;
			i960->icount -= 6;
			t1 = get_ea(i960, opcode);
			t2 = (opcode>>19)&0x1c;
			i960->bursting = 1;
			for(i=0; i<3; i++) {
				i960->r[t2+i] = i960_read_dword_unaligned(i960, t1);
				if(i960->bursting)
					t1 += 4;
			}
			break;
		}

		case 0xa2:{// stt
			int i;
			i960->icount -= 4;
			t1 = get_ea(i960, opcode);
			t2 = (opcode>>19)&0x1c;
			i960->bursting = 1;
			for(i=0; i<3; i++) {
				i960_write_dword_unaligned(i960, t1, i960->r[t2+i]);
				if(i960->bursting)
					t1 += 4;
			}
			break;
		}

		case 0xb0:{// ldq
			int i;
			i960->icount -= 7;
			t1 = get_ea(i960, opcode);
			t2 = (opcode>>19)&0x1c;
			i960->bursting = 1;
			for(i=0; i<4; i++) {
				i960->r[t2+i] = i960_read_dword_unaligned(i960, t1);
				if(i960->bursting)
					t1 += 4;
			}
			break;
		}

		case 0xb2:{// stq
			int i;
			i960->icount -= 5;
			t1 = get_ea(i960, opcode);
			t2 = (opcode>>19)&0x1c;
			i960->bursting = 1;
			for(i=0; i<4; i++) {
				i960_write_dword_unaligned(i960, t1, i960->r[t2+i]);
				if(i960->bursting)
					t1 += 4;
			}
			break;
		}

		case 0xc0: // ldib
			i960->icount -= 4;
			i960->r[(opcode>>19)&0x1f] = (INT8)memory_read_byte_32le(i960->program, get_ea(i960, opcode));
			break;

		case 0xc2: // stib
			i960->icount -= 2;
			memory_write_byte_32le(i960->program, get_ea(i960, opcode), i960->r[(opcode>>19)&0x1f]);
			break;

		case 0xc8: // ldis
			i960->icount -= 4;
			i960->r[(opcode>>19)&0x1f] = (INT16)i960_read_word_unaligned(i960, get_ea(i960, opcode));
			break;

		case 0xca: // stis
			i960->icount -= 2;
			i960_write_word_unaligned(i960, get_ea(i960, opcode), i960->r[(opcode>>19)&0x1f]);
			break;

		default:
			fatalerror("I960: %x: Unhandled %02x", i960->PIP, opcode >> 24);
	}

}

static CPU_EXECUTE( i960 )
{
	i960_state_t *i960 = get_safe_token(device);
	UINT32 opcode;

	check_irqs(i960);
	while(i960->icount > 0) {
		i960->PIP = i960->IP;
		debugger_instruction_hook(device, i960->IP);

		i960->bursting = 0;

		opcode = memory_decrypted_read_dword(i960->program, i960->IP);
		i960->IP += 4;

		execute_op(i960, opcode);
	}
}

static void set_irq_line(i960_state_t *i960, int irqline, int state)
{
	int int_tab =  memory_read_dword_32le(i960->program, i960->PRCB+20);	// interrupt table
	int cpu_pri = (i960->PC>>16)&0x1f;
	int vector =0;
	int priority;
	UINT32 pend, word, wordofs;

	// We support the 4 external IRQ lines in "normal" mode only.
	// The i960's interrupt support is a bit more complete than that,
	// but Namco and Sega both went for the cheapest solution.

	switch (irqline)
	{
		case I960_IRQ0:
			vector = i960->ICR & 0xff;
			break;

		case I960_IRQ1:
			vector = (i960->ICR>>8)&0xff;
			break;

		case I960_IRQ2:
			vector = (i960->ICR>>16)&0xff;
			break;

		case I960_IRQ3:
			vector = (i960->ICR>>24)&0xff;
			break;
	}

	if(!vector)
	{
		logerror("i960: interrupt line %d in IAC mode, unsupported!\n", irqline);
		return;
	}


	priority = vector / 8;

	if(state) {
		// check if we can take this "right now"
		if (((cpu_pri < priority) || (priority == 31)) && (i960->immediate_irq == 0))
		{
			i960->immediate_irq = 1;
			i960->immediate_vector = vector;
			i960->immediate_pri = priority;
		}
		else
		{
			// store the interrupt in the "pending" table
			pend = memory_read_dword_32le(i960->program, int_tab);
			pend |= (1 << priority);
			memory_write_dword_32le(i960->program, int_tab, pend);

			// now bitfield-ize the vector
			word = ((vector / 32) * 4) + 4;
			wordofs = vector % 32;
			pend = memory_read_dword_32le(i960->program, int_tab + word);
			pend |= (1 << wordofs);
			memory_write_dword_32le(i960->program, int_tab + word, pend);
		}

		// and ack it to the core now that it's queued
		(*i960->irq_cb)(i960->device, irqline);
	}
}

static CPU_SET_INFO( i960 )
{
	i960_state_t *i960 = get_safe_token(device);

	if(state >= CPUINFO_INT_REGISTER+I960_R0 && state <= CPUINFO_INT_REGISTER + I960_G15) {
		i960->r[state - (CPUINFO_INT_REGISTER + I960_R0)] = info->i;
		return;
	}

	switch(state) {
		// Interfacing
	case CPUINFO_INT_REGISTER + I960_IP:		i960->IP = info->i; 						break;
	case CPUINFO_INT_INPUT_STATE + I960_IRQ0:	set_irq_line(i960, I960_IRQ0, info->i);		break;
	case CPUINFO_INT_INPUT_STATE + I960_IRQ1:	set_irq_line(i960, I960_IRQ1, info->i);		break;
	case CPUINFO_INT_INPUT_STATE + I960_IRQ2:	set_irq_line(i960, I960_IRQ2, info->i);		break;
	case CPUINFO_INT_INPUT_STATE + I960_IRQ3:	set_irq_line(i960, I960_IRQ3, info->i);		break;

	default:
		fatalerror("i960_set_info %x", state);
	}
}

static CPU_INIT( i960 )
{
	i960_state_t *i960 = get_safe_token(device);

	i960->irq_cb = irqcallback;
	i960->device = device;
	i960->program = device->space(AS_PROGRAM);

	state_save_register_device_item(device, 0, i960->PIP);
	state_save_register_device_item(device, 0, i960->SAT);
	state_save_register_device_item(device, 0, i960->PRCB);
	state_save_register_device_item(device, 0, i960->PC);
	state_save_register_device_item(device, 0, i960->AC);
	state_save_register_device_item(device, 0, i960->ICR);
	state_save_register_device_item_array(device, 0, i960->r);
	state_save_register_device_item_array(device, 0, i960->fp);
	state_save_register_device_item_2d_array(device, 0, i960->rcache);
	state_save_register_device_item_array(device, 0, i960->rcache_frame_addr);
}

static CPU_RESET( i960 )
{
	i960_state_t *i960 = get_safe_token(device);

	i960->SAT        = memory_read_dword_32le(i960->program, 0);
	i960->PRCB       = memory_read_dword_32le(i960->program, 4);
	i960->IP         = memory_read_dword_32le(i960->program, 12);
	i960->PC         = 0x001f2002;
	i960->AC         = 0;
	i960->ICR	    = 0xff000000;
	i960->bursting   = 0;
	i960->immediate_irq = 0;

	memset(i960->r, 0, sizeof(i960->r));
	memset(i960->rcache, 0, sizeof(i960->rcache));

	i960->r[I960_FP] = memory_read_dword_32le(i960->program, i960->PRCB+24);
	i960->r[I960_SP] = i960->r[I960_FP] + 64;
	i960->rcache_pos = 0;
}

CPU_GET_INFO( i960 )
{
	i960_state_t *i960 = (device != NULL && device->token() != NULL) ? get_safe_token(device) : NULL;

	if(state >= CPUINFO_INT_REGISTER+I960_R0 && state <= CPUINFO_INT_REGISTER + I960_G15) {
		info->i = i960->r[state - (CPUINFO_INT_REGISTER + I960_R0)];
		return;
	}

	switch(state) {
		// Interface functions and variables
	case CPUINFO_FCT_SET_INFO:					info->setinfo     = CPU_SET_INFO_NAME(i960);	break;
	case CPUINFO_FCT_INIT:						info->init        = CPU_INIT_NAME(i960);		break;
	case CPUINFO_FCT_RESET:						info->reset       = CPU_RESET_NAME(i960);		break;
	case CPUINFO_FCT_EXIT:						info->exit        = 0;							break;
	case CPUINFO_FCT_EXECUTE:					info->execute     = CPU_EXECUTE_NAME(i960);		break;
	case CPUINFO_FCT_BURN:						info->burn        = 0;							break;
	case CPUINFO_FCT_DISASSEMBLE:				info->disassemble = CPU_DISASSEMBLE_NAME(i960);	break;
	case CPUINFO_PTR_INSTRUCTION_COUNTER:		info->icount      = &i960->icount;				break;
	case CPUINFO_INT_CONTEXT_SIZE:				info->i           = sizeof(i960_state_t);		break;
	case CPUINFO_INT_MIN_INSTRUCTION_BYTES:		info->i           = 4;							break;
	case CPUINFO_INT_MAX_INSTRUCTION_BYTES:		info->i           = 8;							break;

		// Bus sizes
	case DEVINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_PROGRAM:	info->i = 32;						break;
	case DEVINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_PROGRAM:	info->i = 32;						break;
	case DEVINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_PROGRAM:	info->i = 0;						break;
	case CPUINFO_INT_LOGADDR_WIDTH_PROGRAM:	info->i = 0;						break;
	case DEVINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_DATA:	info->i = 0;						break;
	case DEVINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_DATA:	info->i = 0;						break;
	case DEVINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_DATA:	info->i = 0;						break;
	case CPUINFO_INT_LOGADDR_WIDTH_DATA:	info->i = 0;						break;
	case DEVINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_IO:		info->i = 0;						break;
	case DEVINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_IO:		info->i = 0;						break;
	case DEVINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_IO:		info->i = 0;						break;
	case CPUINFO_INT_LOGADDR_WIDTH_IO:		info->i = 0;						break;

		// Internal maps
	case DEVINFO_PTR_INTERNAL_MEMORY_MAP + ADDRESS_SPACE_PROGRAM:	info->internal_map32 = NULL;break;
	case DEVINFO_PTR_INTERNAL_MEMORY_MAP + ADDRESS_SPACE_DATA:		info->internal_map32 = NULL;break;
	case DEVINFO_PTR_INTERNAL_MEMORY_MAP + ADDRESS_SPACE_IO:		info->internal_map32 = NULL;break;

		// CPU misc parameters
	case DEVINFO_STR_NAME:					strcpy(info->s, "i960KB");							break;
	case DEVINFO_STR_SOURCE_FILE:				strcpy(info->s, __FILE__);							break;
	case CPUINFO_STR_FLAGS:					strcpy(info->s, i960_get_strflags(i960));			break;
	case DEVINFO_INT_ENDIANNESS:			info->i = ENDIANNESS_LITTLE;								break;
	case CPUINFO_INT_INPUT_LINES:			info->i = 4;										break;
	case CPUINFO_INT_DEFAULT_IRQ_VECTOR:	info->i = -1;										break;
	case CPUINFO_INT_CLOCK_MULTIPLIER:		info->i = 1;										break;
	case CPUINFO_INT_CLOCK_DIVIDER:			info->i = 1;										break;

		// CPU main state
	case CPUINFO_INT_PC:					info->i = i960->IP;									break;
	case CPUINFO_INT_SP:					info->i = i960->r[I960_SP];							break;
	case CPUINFO_INT_PREVIOUSPC:			info->i = i960->PIP;								break;

	case CPUINFO_INT_REGISTER + I960_SAT:	info->i = i960->SAT;								break;
	case CPUINFO_INT_REGISTER + I960_PRCB:	info->i = i960->PRCB;								break;
	case CPUINFO_INT_REGISTER + I960_PC:	info->i = i960->PC;									break;
	case CPUINFO_INT_REGISTER + I960_AC:	info->i = i960->AC;									break;
	case CPUINFO_INT_REGISTER + I960_IP:	info->i = i960->IP;									break;
	case CPUINFO_INT_REGISTER + I960_PIP:	info->i = i960->PIP;								break;

		// CPU debug stuff
	case CPUINFO_STR_REGISTER + I960_SAT:	sprintf(info->s, "sat  :%08x", i960->SAT);			break;
	case CPUINFO_STR_REGISTER + I960_PRCB:	sprintf(info->s, "prcb :%08x", i960->PRCB);			break;
	case CPUINFO_STR_REGISTER + I960_PC:	sprintf(info->s, "pc   :%08x", i960->PC);			break;
	case CPUINFO_STR_REGISTER + I960_AC:	sprintf(info->s, "ac   :%08x", i960->AC);			break;
	case CPUINFO_STR_REGISTER + I960_IP:	sprintf(info->s, "ip   :%08x", i960->IP);			break;
	case CPUINFO_STR_REGISTER + I960_PIP:	sprintf(info->s, "pip  :%08x", i960->PIP);			break;

	case CPUINFO_STR_REGISTER + I960_R0:	sprintf(info->s, "pfp  :%08x", i960->r[ 0]);		break;
	case CPUINFO_STR_REGISTER + I960_R1:	sprintf(info->s, "sp   :%08x", i960->r[ 1]);		break;
	case CPUINFO_STR_REGISTER + I960_R2:	sprintf(info->s, "rip  :%08x", i960->r[ 2]);		break;
	case CPUINFO_STR_REGISTER + I960_R3:	sprintf(info->s, "r3   :%08x", i960->r[ 3]);		break;
	case CPUINFO_STR_REGISTER + I960_R4:	sprintf(info->s, "r4   :%08x", i960->r[ 4]);		break;
	case CPUINFO_STR_REGISTER + I960_R5:	sprintf(info->s, "r5   :%08x", i960->r[ 5]);		break;
	case CPUINFO_STR_REGISTER + I960_R6:	sprintf(info->s, "r6   :%08x", i960->r[ 6]);		break;
	case CPUINFO_STR_REGISTER + I960_R7:	sprintf(info->s, "r7   :%08x", i960->r[ 7]);		break;
	case CPUINFO_STR_REGISTER + I960_R8:	sprintf(info->s, "r8   :%08x", i960->r[ 8]);		break;
	case CPUINFO_STR_REGISTER + I960_R9:	sprintf(info->s, "r9   :%08x", i960->r[ 9]);		break;
	case CPUINFO_STR_REGISTER + I960_R10:	sprintf(info->s, "r10  :%08x", i960->r[10]);		break;
	case CPUINFO_STR_REGISTER + I960_R11:	sprintf(info->s, "r11  :%08x", i960->r[11]);		break;
	case CPUINFO_STR_REGISTER + I960_R12:	sprintf(info->s, "r12  :%08x", i960->r[12]);		break;
	case CPUINFO_STR_REGISTER + I960_R13:	sprintf(info->s, "r13  :%08x", i960->r[13]);		break;
	case CPUINFO_STR_REGISTER + I960_R14:	sprintf(info->s, "r14  :%08x", i960->r[14]);		break;
	case CPUINFO_STR_REGISTER + I960_R15:	sprintf(info->s, "r15  :%08x", i960->r[15]);		break;

	case CPUINFO_STR_REGISTER + I960_G0:	sprintf(info->s, "g0   :%08x", i960->r[16]);		break;
	case CPUINFO_STR_REGISTER + I960_G1:	sprintf(info->s, "g1   :%08x", i960->r[17]);		break;
	case CPUINFO_STR_REGISTER + I960_G2:	sprintf(info->s, "g2   :%08x", i960->r[18]);		break;
	case CPUINFO_STR_REGISTER + I960_G3:	sprintf(info->s, "g3   :%08x", i960->r[19]);		break;
	case CPUINFO_STR_REGISTER + I960_G4:	sprintf(info->s, "g4   :%08x", i960->r[20]);		break;
	case CPUINFO_STR_REGISTER + I960_G5:	sprintf(info->s, "g5   :%08x", i960->r[21]);		break;
	case CPUINFO_STR_REGISTER + I960_G6:	sprintf(info->s, "g6   :%08x", i960->r[22]);		break;
	case CPUINFO_STR_REGISTER + I960_G7:	sprintf(info->s, "g7   :%08x", i960->r[23]);		break;
	case CPUINFO_STR_REGISTER + I960_G8:	sprintf(info->s, "g8   :%08x", i960->r[24]);		break;
	case CPUINFO_STR_REGISTER + I960_G9:	sprintf(info->s, "g9   :%08x", i960->r[25]);		break;
	case CPUINFO_STR_REGISTER + I960_G10:	sprintf(info->s, "g10  :%08x", i960->r[26]);		break;
	case CPUINFO_STR_REGISTER + I960_G11:	sprintf(info->s, "g11  :%08x", i960->r[27]);		break;
	case CPUINFO_STR_REGISTER + I960_G12:	sprintf(info->s, "g12  :%08x", i960->r[28]);		break;
	case CPUINFO_STR_REGISTER + I960_G13:	sprintf(info->s, "g13  :%08x", i960->r[29]);		break;
	case CPUINFO_STR_REGISTER + I960_G14:	sprintf(info->s, "g14  :%08x", i960->r[30]);		break;
	case CPUINFO_STR_REGISTER + I960_G15:	sprintf(info->s, "fp   :%08x", i960->r[31]);		break;

//  default:
//      fatalerror("i960_get_info %x          ", state);
	}
}

// call from any read/write handler for a memory area that can't be bursted
// on the real hardware (e.g. Model 2's interrupt control registers)
void i960_noburst(running_device *device)
{
	i960_state_t *i960 = get_safe_token(device);
	i960->bursting = 0;
}

void i960_stall(running_device *device)
{
	i960_state_t *i960 = get_safe_token(device);
	i960->IP = i960->PIP;
}