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

  spec_snqk.cpp

  TODO:

    - Implement the following snapshot formats
      .89C (Tezxas)
      .SLT (Various emulators)
      .SZX (Spectaculator)
      .ZLS (ZX-Live!)
      .ZX (ZXSpectr)
      .ZX82 (Speculator '97)
      .ZXS (ZX32)

    - Implement the following quickload formats
      .$? (HoBeta)
      .__? (SpecEm)
      .BAS (BASin)
      .H/.B (Roman ZX)
      .RAW (Various emulators)
      .SCR variants

    - Cleanup of the .Z80 format

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

#include "emu.h"
#include "ui/uimain.h"
#include "cpu/z80/z80.h"
#include "includes/spectrum.h"
#include "includes/spec128.h"
#include "includes/timex.h"
#include "includes/specpls3.h"
#include "sound/ay8910.h"
#include "machine/spec_snqk.h"

#define EXEC_NA "N/A"

//-------------------------------------------------
//  log_quickload - logs and displays useful
//  data for the end user
//-------------------------------------------------

void spectrum_state::log_quickload(const char *type, uint32_t start, uint32_t length, uint32_t exec, const char *exec_format)
{
	std::ostringstream tempstring;

	logerror("Loading %04X bytes of RAM at %04X\n", length, start);

	util::stream_format(tempstring, "Quickload type: %s   Length: %d bytes\n", type, length);
	util::stream_format(tempstring, "Start: 0x%04X   End: 0x%04X   Exec: ", start, start + length - 1);

	logerror("Quickload loaded.\n");
	if (!core_stricmp(exec_format, EXEC_NA))
		tempstring << "N/A";
	else
	{
		logerror("Execution can resume with ");
		logerror(exec_format, exec);
		logerror("\n");
		util::stream_format(tempstring, exec_format, exec);
	}

	machine().ui().popup_time(10, "%s", tempstring.str());
}

/*******************************************************************
 *
 *      Update the memory and paging of the spectrum being emulated
 *
 *      if port_7ffd_data is -1 then machine is 48K - no paging
 *      if port_1ffd_data is -1 then machine is 128K
 *      if neither port is -1 then machine is +2a/+3
 *
 *      Note: the 128K .SNA and .Z80 file formats do not store the
 *      port 1FFD setting so it is necessary to calculate the appropriate
 *      value for the ROM paging.
 *
 *******************************************************************/
void spectrum_state::update_paging()
{
	if (m_port_7ffd_data == -1)
		return;
	if (m_port_1ffd_data == -1)
		spectrum_128_update_memory();

	else
	{
		if (BIT(m_port_7ffd_data, 4))
			// Page in Spec 48K basic ROM
			m_port_1ffd_data = 0x04;
		else
			m_port_1ffd_data = 0x00;
		plus3_update_memory();
	}
}

/* Page in the 48K Basic ROM. Used when running 48K snapshots on a 128K machine. */
void spectrum_state::page_basicrom()
{
	if (m_port_7ffd_data == -1)
		return;
	m_port_7ffd_data |= 0x10;
	update_paging();
}


SNAPSHOT_LOAD_MEMBER(spectrum_state::snapshot_cb)
{
	std::vector<uint8_t> snapshot_data(snapshot_size);

	image.fread(&snapshot_data[0], snapshot_size);

	if (!core_stricmp(file_type, "sna"))
	{
		if ((snapshot_size != SNA48_SIZE) && (snapshot_size != SNA128_SIZE_1) && (snapshot_size != SNA128_SIZE_2))
		{
			logerror("Invalid .SNA file size.\n");
			goto error;
		}
		setup_sna(&snapshot_data[0], snapshot_size);
	}
	else if (!core_stricmp(file_type, "sp"))
	{
		if ((snapshot_data[0] != 'S' && snapshot_data[1] != 'P') && (snapshot_size != SP_NEW_SIZE_16K && snapshot_size != SP_NEW_SIZE_48K))
		{
			if (snapshot_size != SP_OLD_SIZE)
			{
				logerror("Invalid .SP file size.\n");
				goto error;
			}
		}
		setup_sp(&snapshot_data[0], snapshot_size);
	}
	else if (!core_stricmp(file_type, "ach"))
	{
		if (snapshot_size != ACH_SIZE)
		{
			logerror("Invalid .ACH file size.\n");
			goto error;
		}
		setup_ach(&snapshot_data[0], snapshot_size);
	}
	else if (!core_stricmp(file_type, "prg"))
	{
		if (snapshot_size != PRG_SIZE)
		{
			logerror("Invalid .PRG file size.\n");
			goto error;
		}
		setup_prg(&snapshot_data[0], snapshot_size);
	}
	else if (!core_stricmp(file_type, "plusd"))
	{
		if ((snapshot_size != PLUSD48_SIZE) && (snapshot_size != PLUSD128_SIZE))
		{
			logerror("Invalid .PLUSD file size.\n");
			goto error;
		}
		setup_plusd(&snapshot_data[0], snapshot_size);
	}
	else if (!core_stricmp(file_type, "sem"))
	{
		if (snapshot_data[0] != 0x05 && snapshot_data[1] != 'S' && \
			snapshot_data[2] != 'P' && snapshot_data[3] != 'E' && \
			snapshot_data[4] != 'C' && snapshot_data[5] != '1')
		{
			if (snapshot_size != SEM_SIZE)
			{
				logerror("Invalid .SEM file size.\n");
				goto error;
			}
		}
		setup_sem(&snapshot_data[0], snapshot_size);
	}
	else if (!core_stricmp(file_type, "sit"))
	{
		if (snapshot_size != SIT_SIZE)
		{
			logerror("Invalid .SIT file size.\n");
			goto error;
		}
		setup_sit(&snapshot_data[0], snapshot_size);
	}
	else if (!core_stricmp(file_type, "zx"))
	{
		if (snapshot_size != ZX_SIZE)
		{
			logerror("Invalid .ZX file size.\n");
			goto error;
		}
		setup_zx(&snapshot_data[0], snapshot_size);
	}
	else if (!core_stricmp(file_type, "snp"))
	{
		if (snapshot_size != SNP_SIZE)
		{
			logerror("Invalid .SNP file size.\n");
			goto error;
		}
		setup_snp(&snapshot_data[0], snapshot_size);
	}
	else if (!core_stricmp(file_type, "snx"))
	{
		if (snapshot_data[0] != 'X' && snapshot_data[1] != 'S' && \
			snapshot_data[2] != 'N' && snapshot_data[3] != 'A')
		{
			logerror("Invalid .SNX file size.\n");
			goto error;
		}
		setup_snx(&snapshot_data[0], snapshot_size);
	}
	else if (!core_stricmp(file_type, "frz"))
	{
		if (snapshot_size != FRZ_SIZE)
		{
			logerror("Invalid .FRZ file size.\n");
			goto error;
		}
		setup_frz(&snapshot_data[0], snapshot_size);
	}
	else
	{
		setup_z80(&snapshot_data[0], snapshot_size);
	}

	return image_init_result::PASS;

error:
	return image_init_result::FAIL;
}

/*******************************************************************
 *
 *      Load a .SP file.
 *
 *      There are two kinds of .SP files: "old" and "new".
 *
 *      The old version is always 49184 bytes long and is created
 *      by a leaked copy of the VGASpec emulator.
 *
 *      Subsequently Pedro Gimeno (the author of VGASpec) renamed it
 *      to "Spectrum" (but it's colloquially known as the "Spanish
 *      Spectrum emulator") and extended the header to break backward
 *      compatibility: the new format supports both 16K and 48K
 *      images and it's 16422 or 49190 bytes long.
 *
 *      http://www.formauri.es/personal/pgimeno/spec/spec.html
 *
 *      The formats are defined as follows:
 *
 *      Offset              Size    Description (all registers stored with LSB first)
 *      ------------------- ------- -------------------------------------------------
 *      VGASPEC SPECTRUM
 *      ------------------- ------- -------------------------------------------------
 *      N/A     0           2       "SP" (signature)
 *      N/A     2           2       Program length
 *      N/A     4           2       Program location
 *      0       6           2       BC
 *      2       8           2       DE
 *      4       10          2       HL
 *      6       12          2       AF
 *      8       14          2       IX
 *      10      16          2       IY
 *      12      18          2       BC'
 *      14      20          2       DE'
 *      16      22          2       HL'
 *      18      24          2       AF'
 *      20      26          1       R
 *      21      27          1       I
 *      22      28          2       SP
 *      24      30          2       PC
 *      26      32          2       0x00 (reserved for future use)
 *      28      34          1       Border color
 *      29      35          1       0x00 (reserved for future use)
 *      30      36          2       Status word
 *      32      38          16384/  RAM dump
 *                          49152
 *
 *  Status word:
 *  Bit     Description
 *  ------- ---------------------------------------------------
 *  15-8    Reserved for future use
 *  7-6     Reserved for internal use (0)
 *  5       Status of the flash attribute: 0=normal/1=inverse
 *  4       Interrupt pending for execution
 *  3       If 1, IM 0; if 0, bit 1 determines interrupt mode
 *              (Spectrum v0.99e had this behaviour reversed,
 *              and this bit was not used in versions previous
 *              to v 0.99e)
 *  2       IFF2: 0=DI/1=EI
 *  1       Interrupt mode (if bit 3 reset): 0=IM1/1=IM2
 *  0       IFF1: 0=DI/1=EI
 *
 *******************************************************************/
void spectrum_state::border_update(int data)
{
#if 0
	spectrum_EventList_Reset(machine());
	spectrum_border_set_last_color(machine(), data);
	spectrum_border_force_redraw(machine());
#endif
}

void spectrum_state::setup_sp(uint8_t *snapdata, uint32_t snapsize)
{
	int i, SP_OFFSET;
	uint8_t intr;
	uint16_t start, size, data, status;
	address_space &space = m_maincpu->space(AS_PROGRAM);

	if (snapsize == SP_NEW_SIZE_16K || snapsize == SP_NEW_SIZE_48K)
	{
		SP_OFFSET = 0;
		start = (snapdata[SP_OFFSET +  5] << 8) | snapdata[SP_OFFSET +  4];
		size = (snapdata[SP_OFFSET +  3] << 8) | snapdata[SP_OFFSET +  2];
	}
	else
	{
		SP_OFFSET = SP_OLD_HDR - SP_NEW_HDR;
		start = BASE_RAM;
		size = 3*SPECTRUM_BANK;
	}

	data = (snapdata[SP_OFFSET + 13] << 8) | snapdata[SP_OFFSET + 12];
	m_maincpu->set_state_int(Z80_AF, data);

	data = (snapdata[SP_OFFSET +  7] << 8) | snapdata[SP_OFFSET +  6];
	m_maincpu->set_state_int(Z80_BC, data);

	data = (snapdata[SP_OFFSET +  9] << 8) | snapdata[SP_OFFSET +  8];
	m_maincpu->set_state_int(Z80_DE, data);

	data = (snapdata[SP_OFFSET + 11] << 8) | snapdata[SP_OFFSET + 10];
	m_maincpu->set_state_int(Z80_HL, data);


	data = (snapdata[SP_OFFSET + 25] << 8) | snapdata[SP_OFFSET + 24];
	m_maincpu->set_state_int(Z80_AF2, data);

	data = (snapdata[SP_OFFSET + 19] << 8) | snapdata[SP_OFFSET + 18];
	m_maincpu->set_state_int(Z80_BC2, data);

	data = (snapdata[SP_OFFSET + 21] << 8) | snapdata[SP_OFFSET + 20];
	m_maincpu->set_state_int(Z80_DE2, data);

	data = (snapdata[SP_OFFSET + 23] << 8) | snapdata[SP_OFFSET + 22];
	m_maincpu->set_state_int(Z80_HL2, data);


	data = (snapdata[SP_OFFSET + 15] << 8) | snapdata[SP_OFFSET + 14];
	m_maincpu->set_state_int(Z80_IX, data);

	data = (snapdata[SP_OFFSET + 17] << 8) | snapdata[SP_OFFSET + 16];
	m_maincpu->set_state_int(Z80_IY, data);


	data = snapdata[SP_OFFSET + 26];
	m_maincpu->set_state_int(Z80_R, data);

	data = snapdata[SP_OFFSET + 27];
	m_maincpu->set_state_int(Z80_I, data);


	data = (snapdata[SP_OFFSET + 29] << 8) | snapdata[SP_OFFSET + 28];
	m_maincpu->set_state_int(Z80_SP, data);

	data = (snapdata[SP_OFFSET + 31] << 8) | snapdata[SP_OFFSET + 30];
	m_maincpu->set_state_int(Z80_PC, data);


	status = (snapdata[SP_OFFSET + 37] << 8) | snapdata[SP_OFFSET + 36];

	data = (BIT(status, 3) << 1) | BIT(status, 1);
	switch(data)
	{
		case 0: // case 2: in version 0.99e of the emulator
		m_maincpu->set_state_int(Z80_IM, 1);
		break;
		case 1: // case 3: in version 0.99e of the emulator
		m_maincpu->set_state_int(Z80_IM, 2);
		break;
		case 2: // case 0: in version 0.99e of the emulator
		case 3: // case 1: in version 0.99e of the emulator
		m_maincpu->set_state_int(Z80_IM, (uint64_t)0);
	}

	data = BIT(status, 0);
	m_maincpu->set_state_int(Z80_IFF1, data);

	data = BIT(status, 2);
	m_maincpu->set_state_int(Z80_IFF2, data);

	intr = BIT(status, 4) ? ASSERT_LINE : CLEAR_LINE;
	m_maincpu->set_input_line(INPUT_LINE_IRQ0, intr);
	m_maincpu->set_input_line(INPUT_LINE_HALT, CLEAR_LINE);

	data = BIT(status, 5);
	m_flash_invert = data;
	logerror("FLASH state: %s\n", data ? "PAPER on INK" : "INK on PAPER");

	// Memory dump
	logerror("Loading %04X bytes of RAM at %04X\n", size, start);
	for (i = 0; i < size; i++)
		space.write_byte(start + i, snapdata[SP_OFFSET + SP_NEW_HDR + i]);

	// Set border color
	data = snapdata[SP_OFFSET + 34] & 0x07;
	m_port_fe_data = (m_port_fe_data & 0xf8) | data;
	border_update(data);
	logerror("Border color:%02X\n", data);

	page_basicrom();

	//logerror("Snapshot loaded.\nExecution resuming at %s\n", m_maincpu->state_string(Z80_PC).c_str());
}

/*******************************************************************
 *
 *      Load a .SNA file.
 *
 *      This format was used by Arnt Gulbrandsen for its JPP
 *      emulator, and it's based on the format used by the Mirage
 *      Microdriver, a Microdrive backup accessory.
 *
 *      http://www.worldofspectrum.org/infoseekid.cgi?id=1000266
 *
 *      The 48K image is always 49179 bytes long.
 *
 *      .SNA snapshots save the PC on the top of the stack, thus
 *      deleting the original bytes: not a good feature for a snapshot...
 *
 *      It's been reported that snapshots saved at critical moments would
 *      subsequently crash on restore due to the fact that the stack was full
 *      or pointed to a ROM address. A workaround for this situation is to
 *      store 0x0000 into the word where the PC was located.
 *
 *      For sake of fidelity, the code that zeroes the top of the stack has
 *      been written but commented out.
 *
 *      Later, the format has been extended to store the whole Spectrum 128 status.
 *
 *      48K format as follows:
 *
 *      Offset  Size    Description (all registers stored with LSB first)
 *      ------- ------- -------------------------------------------------
 *      0       1       I
 *      1       2       HL'
 *      3       2       DE'
 *      5       2       BC'
 *      7       2       AF'
 *      9       2       HL
 *      11      2       DE
 *      13      2       BC
 *      15      2       IY
 *      17      2       IX
 *      19      1       IFF1/2: bit 0 contains IFF1, bit 2 contains IFF2 (0=DI/1=EI)
 *      20      1       R
 *      21      2       AF
 *      23      2       SP
 *      25      1       Interrupt mode (0=IM0/1=IM1/2=IM2)
 *      26      1       Border color
 *      27      49152   RAM dump
 *
 *      PC is stored on stack.
 *
 *      128K format as follows:
 *
 *      Offset  Size    Description (all registers stored with LSB first)
 *      ------- ------- -------------------------------------------------
 *      0       1       I
 *      1       2       HL'
 *      3       2       DE'
 *      5       2       BC'
 *      7       2       AF'
 *      9       2       HL
 *      11      2       DE
 *      13      2       BC
 *      15      2       IY
 *      17      2       IX
 *      19      1       IFF1/2: bit 0 contains IFF1, bit 2 contains IFF2 (0=DI/1=EI)
 *      20      1       R
 *      21      2       AF
 *      23      2       SP
 *      25      1       Interrupt mode (0=IM0/1=IM1/2=IM2)
 *      26      1       Border color
 *      27      16384   RAM bank 5 (0x4000-0x7fff)
 *      16411   16384   RAM bank 2 (0x8000-0xbfff)
 *      32795   16384   RAM bank n (0xc000-0xffff - currently paged bank)
 *      49179   2       PC
 *      49181   1       Port 0x7FFD data
 *      49182   1       TR-DOS rom paged (0=no/1=yes)
 *      49183   81920/  Remaining RAM banks in ascending order
 *              98304
 *
 *      PC is NOT stored on stack.
 *
 *      The bank in 0xc000 is always included even if it is page 2 or 5
 *      in which case it is included twice.
 *
 *******************************************************************/
void spectrum_state::setup_sna(uint8_t *snapdata, uint32_t snapsize)
{
	int i, j, usedbanks[8];
	long bank_offset;
	uint8_t intr;
	uint16_t data, addr;
	address_space &space = m_maincpu->space(AS_PROGRAM);

	if ((snapsize != SNA48_SIZE) && (m_port_7ffd_data == -1))
	{
		logerror("Can't load 128K .SNA file into 48K machine\n");
		return;
	}


	data = (snapdata[SNA48_OFFSET + 22] << 8) | snapdata[SNA48_OFFSET + 21];
	m_maincpu->set_state_int(Z80_AF, data);

	data = (snapdata[SNA48_OFFSET + 14] << 8) | snapdata[SNA48_OFFSET + 13];
	m_maincpu->set_state_int(Z80_BC, data);

	data = (snapdata[SNA48_OFFSET + 12] << 8) | snapdata[SNA48_OFFSET + 11];
	m_maincpu->set_state_int(Z80_DE, data);

	data = (snapdata[SNA48_OFFSET + 10] << 8) | snapdata[SNA48_OFFSET +  9];
	m_maincpu->set_state_int(Z80_HL, data);


	data = (snapdata[SNA48_OFFSET +  8] << 8) | snapdata[SNA48_OFFSET +  7];
	m_maincpu->set_state_int(Z80_AF2, data);

	data = (snapdata[SNA48_OFFSET +  6] << 8) | snapdata[SNA48_OFFSET +  5];
	m_maincpu->set_state_int(Z80_BC2, data);

	data = (snapdata[SNA48_OFFSET +  4] << 8) | snapdata[SNA48_OFFSET +  3];
	m_maincpu->set_state_int(Z80_DE2, data);

	data = (snapdata[SNA48_OFFSET +  2] << 8) | snapdata[SNA48_OFFSET +  1];
	m_maincpu->set_state_int(Z80_HL2, data);


	data = (snapdata[SNA48_OFFSET + 18] << 8) | snapdata[SNA48_OFFSET + 17];
	m_maincpu->set_state_int(Z80_IX, data);

	data = (snapdata[SNA48_OFFSET + 16] << 8) | snapdata[SNA48_OFFSET + 15];
	m_maincpu->set_state_int(Z80_IY, data);


	data = snapdata[SNA48_OFFSET + 20];
	m_maincpu->set_state_int(Z80_R, data);

	data = snapdata[SNA48_OFFSET +  0];
	m_maincpu->set_state_int(Z80_I, data);


	data = (snapdata[SNA48_OFFSET + 24] << 8) | snapdata[SNA48_OFFSET + 23];
	m_maincpu->set_state_int(Z80_SP, data);


	data = snapdata[SNA48_OFFSET + 25] & 0x03;
	if (data == 3)
		data = 2;
	m_maincpu->set_state_int(Z80_IM, data);

	data = snapdata[SNA48_OFFSET + 19];
	m_maincpu->set_state_int(Z80_IFF1, BIT(data, 0));
	m_maincpu->set_state_int(Z80_IFF2, BIT(data, 2));

	intr = BIT(data, 0) ? CLEAR_LINE : ASSERT_LINE;
	m_maincpu->set_input_line(INPUT_LINE_IRQ0, intr);
	m_maincpu->set_input_line(INPUT_LINE_HALT, CLEAR_LINE);

	if (snapsize == SNA48_SIZE)
		// 48K Snapshot
		page_basicrom();
	else
	{
		// 128K Snapshot
		m_port_7ffd_data = snapdata[SNA128_OFFSET + 2];
		logerror ("Port 7FFD:%02X\n", m_port_7ffd_data);
		if (snapdata[SNA128_OFFSET + 3])
		{
			/* TODO: page TR-DOS ROM when supported */
		}
		update_paging();
	}

	if (snapsize == SNA48_SIZE)
	{
		// Memory dump
		logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
		for (i = 0; i < 3*SPECTRUM_BANK; i++)
			space.write_byte(BASE_RAM + i, snapdata[SNA48_HDR + i]);

		// Get PC from stack
		addr = m_maincpu->state_int(Z80_SP);

		if (addr < BASE_RAM || addr > 4*SPECTRUM_BANK - 2)
			logerror("Corrupted SP out of range:%04X", addr);
		else
			logerror("Fetching PC from the stack at SP:%04X\n", addr);

		data = (space.read_byte(addr + 1) << 8) | space.read_byte(addr + 0);
		m_maincpu->set_state_int(Z80_PC, data);

#if 0
		space.write_byte(addr + 0, 0); // It's been reported that zeroing these locations fixes the loading
		space.write_byte(addr + 1, 0); // of a few images that were snapshot at a "wrong" instant
#endif

		addr += 2;
		logerror("Fixing SP:%04X\n", addr);
		m_maincpu->set_state_int(Z80_SP, addr);

		// Set border color
		data = snapdata[SNA48_OFFSET + 26] & 0x07;
		m_port_fe_data = (m_port_fe_data & 0xf8) | data;
		border_update(data);
		logerror("Border color:%02X\n", data);

		//logerror("Snapshot loaded.\nExecution resuming at %s\n", m_maincpu->state_string(Z80_PC).c_str());
	}
	else
	{
		// Set up other RAM banks
		for (i = 0; i < 8; i++)
			usedbanks[i] = 0;

		usedbanks[5] = 1;                               // 0x4000-0x7fff
		usedbanks[2] = 1;                               // 0x8000-0xbfff
		usedbanks[m_port_7ffd_data & 0x07] = 1;    // Banked memory

		logerror("Loading %05X bytes of RAM at %04X\n", 8*SPECTRUM_BANK, BASE_RAM);
		logerror("Loading bank 5 from offset:0001B\n");
		logerror("Loading bank 2 from offset:0401B\n");
		logerror("Loading bank %d from offset:0801B\n", snapdata[SNA128_OFFSET + 2] & 0x07);
		for (i = 0; i < 3*SPECTRUM_BANK; i++)
			space.write_byte(BASE_RAM + i, snapdata[SNA48_HDR + i]);

		bank_offset = SNA48_SIZE + SNA128_HDR;
		for (i = 0; i < 8; i++)
		{
			if (!usedbanks[i])
			{
				logerror("Loading bank %d from offset:%05lX\n", i, bank_offset);
				m_port_7ffd_data &= 0xf8;
				m_port_7ffd_data += i;
				update_paging();
				for (j = 0; j < SPECTRUM_BANK; j++)
					space.write_byte(j + 3*SPECTRUM_BANK, snapdata[bank_offset + j]);
				bank_offset += SPECTRUM_BANK;
			}
		}

		// program counter
		data = (snapdata[SNA128_OFFSET + 1] << 8) | snapdata[SNA128_OFFSET + 0];
		m_maincpu->set_state_int(Z80_PC, data);

		// Set border color
		data = snapdata[SNA48_OFFSET + 26] & 0x07;
		m_port_fe_data = (m_port_fe_data & 0xf8) | data;
		border_update(data);
		logerror("Border color:%02X\n", data);
		data = m_port_7ffd_data & 0x07;

		// Reset paging
		m_port_7ffd_data = snapdata[SNA128_OFFSET + 2];
		update_paging();

		//logerror("Snapshot loaded.\nExecution resuming at bank:%d %s\n", data, m_maincpu->state_string(Z80_PC).c_str());
	}
}

/*******************************************************************
 *
 *      Load a .ACH file.
 *
 *      .ACH files were produced by a commercial Archimedes
 *      (hence the name) emulator called !Speccy.
 *
 *      The files are always 65792 bytes long and store both
 *      a copy of the ROM and the whole 48K RAM.
 *
 *
 *      Offset  Size    Description (all registers stored with LSB first
 *                      except when noted with *BE*)
 *      ------- ------- -------------------------------------------------
 *      0       4       A
 *      4       4       F
 *      8       4       B
 *      12      4       C
 *      16      4       D
 *      20      4       E
 *      24      4       H
 *      28      4       L
 *      32      4       PC
 *      36      4       0x00 (reserved for future use)
 *      40      4       SP
 *      44      104     0x00 (reserved for future use)
 *      148     4       R
 *      152     4       0x00 (reserved for future use)
 *      156     4       Border color
 *      160     4       0x00 (reserved for future use)
 *      164     2       Interrupt mode (0=IM0/1=IM1/2=IM2)
 *      166     24      0x00 (reserved for future use)
 *      190     1       I
 *      191     1       IFF1/2: (0=DI/1=EI)
 *      192     44      0x00 (reserved for future use)
 *      236     4       AF' *BE*
 *      240     4       BC' *BE*
 *      244     2       DE' *BE*
 *      246     2       HL' *BE*
 *      248     4       IX
 *      252     4       IY
 *      256     16384   ROM dump
 *      16640   49152   RAM dump
 *
 *******************************************************************/
void spectrum_state::setup_ach(uint8_t *snapdata, uint32_t snapsize)
{
	int i;
	uint8_t intr;
	uint16_t data;
	address_space &space = m_maincpu->space(AS_PROGRAM);

	data = (snapdata[ACH_OFFSET +   0] << 8) | snapdata[ACH_OFFSET +   4];
	m_maincpu->set_state_int(Z80_AF, data);

	data = (snapdata[ACH_OFFSET +   8] << 8) | snapdata[ACH_OFFSET +  12];
	m_maincpu->set_state_int(Z80_BC, data);

	data = (snapdata[ACH_OFFSET +  16] << 8) | snapdata[ACH_OFFSET +  20];
	m_maincpu->set_state_int(Z80_DE, data);

	data = (snapdata[ACH_OFFSET +  24] << 8) | snapdata[ACH_OFFSET +  28];
	m_maincpu->set_state_int(Z80_HL, data);


	data = (snapdata[ACH_OFFSET + 236] << 8) | snapdata[ACH_OFFSET + 237];
	m_maincpu->set_state_int(Z80_AF2, data);

	data = (snapdata[ACH_OFFSET + 240] << 8) | snapdata[ACH_OFFSET + 241];
	m_maincpu->set_state_int(Z80_BC2, data);

	data = (snapdata[ACH_OFFSET + 244] << 8) | snapdata[ACH_OFFSET + 245];
	m_maincpu->set_state_int(Z80_DE2, data);

	data = (snapdata[ACH_OFFSET + 246] << 8) | snapdata[ACH_OFFSET + 247];
	m_maincpu->set_state_int(Z80_HL2, data);


	data = (snapdata[ACH_OFFSET + 249] << 8) | snapdata[ACH_OFFSET + 248];
	m_maincpu->set_state_int(Z80_IX, data);

	data = (snapdata[ACH_OFFSET + 253] << 8) | snapdata[ACH_OFFSET + 252];
	m_maincpu->set_state_int(Z80_IY, data);

	data = snapdata[ACH_OFFSET + 148];
	m_maincpu->set_state_int(Z80_R, data);

	data = snapdata[ACH_OFFSET + 190];
	m_maincpu->set_state_int(Z80_I, data);


	data = (snapdata[ACH_OFFSET +  41] << 8) | snapdata[ACH_OFFSET +  40];
	m_maincpu->set_state_int(Z80_SP, data);

	data = (snapdata[ACH_OFFSET +  33] << 8) | snapdata[ACH_OFFSET +  32];
	m_maincpu->set_state_int(Z80_PC, data);

	data = snapdata[ACH_OFFSET + 164] & 0x03;
	if (data == 3)
		data = 0;
	m_maincpu->set_state_int(Z80_IM, data);

	data = snapdata[ACH_OFFSET + 191] ? 1 : 0;
	m_maincpu->set_state_int(Z80_IFF1, data);
	m_maincpu->set_state_int(Z80_IFF2, data);

	intr = snapdata[ACH_OFFSET + 191] ? CLEAR_LINE : ASSERT_LINE;
	m_maincpu->set_input_line(INPUT_LINE_IRQ0, intr);
	m_maincpu->set_input_line(INPUT_LINE_HALT, CLEAR_LINE);

	logerror("Skipping the 16K ROM dump at offset:%04X\n", ACH_OFFSET + 256);

	// Memory dump
	logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
	for (i = 0; i < 3*SPECTRUM_BANK; i++)
		space.write_byte(BASE_RAM + i, snapdata[ACH_HDR + SPECTRUM_BANK + i]);

	// Set border color
	data = snapdata[ACH_OFFSET + 156] & 0x07;
	m_port_fe_data = (m_port_fe_data & 0xf8) | data;
	border_update(data);
	logerror("Border color:%02X\n", data);

	page_basicrom();

	//logerror("Snapshot loaded.\nExecution resuming at %s\n", m_maincpu->state_string(Z80_PC).c_str());
}

/*******************************************************************
 *
 *      Load a .PRG file.
 *
 *      .PRG files were produced by an irish emulator called SpecEm
 *      written by Kevin Phair (thanks for the help, Kevin!)
 *
 *      http://code.google.com/p/specem/
 *
 *      The files are always 49408 bytes long and use some stack space
 *      to store registers.
 *
 *      The header actually mimics the file descriptor format used by
 *      the DISCiPLE/+D interface.
 *
 *      Offset  Size    Description (all registers stored with LSB first
 *                      except when noted with *BE*)
 *      ------- ------- -------------------------------------------------
 *      0       1       File type (always 0x05, corresponding to "48K Snapshot"
 *      1       10      Program name (padded with 0x20)
 *      11      2       Number of sectors occupied by the file *BE*
 *      13      1       Track number of the first sector of the file
 *      14      1       Sector number of the first sector of the file
 *      15      195     Sector allocation bitmap
 *      210     10      0x00 (reserved for future use)
 *      220     2       IY
 *      222     2       IX
 *      224     2       DE'
 *      226     2       BC'
 *      228     2       HL'
 *      230     2       AF'
 *      232     2       DE
 *      234     2       BC
 *      236     2       HL
 *      238     1       Junk created when saving I via LD A,I/PUSH AF
 *      239     1       I
 *      240     2       SP
 *      242     14      0x00 (reserved for future use)
 *      256     49152   RAM dump
 *
 *      IFF1/2, R, AF and PC are stored on the stack. Due to a bug in the
 *      BIOS, the snapshots created with the DISCiPLE have the AF' register
 *      replaced with the AF register.
 *
 *      It's unknown (but likely possible) that the snapshots could
 *      suffer from the same "top of the stack" bug as well as .SNA images.
 *
 *******************************************************************/
void spectrum_state::setup_prg(uint8_t *snapdata, uint32_t snapsize)
{
	int i;
	uint8_t intr;
	uint16_t addr, data;
	address_space &space = m_maincpu->space(AS_PROGRAM);

	data = snapdata[PRG_OFFSET +   0];
	if (data != 0x05)
		logerror("Wrong DISCiPLE/+D file type: %02X instead of 05\n", data);

	data = (snapdata[PRG_OFFSET + 235] << 8) | snapdata[PRG_OFFSET + 234];
	m_maincpu->set_state_int(Z80_BC, data);

	data = (snapdata[PRG_OFFSET + 233] << 8) | snapdata[PRG_OFFSET + 232];
	m_maincpu->set_state_int(Z80_DE, data);

	data = (snapdata[PRG_OFFSET + 237] << 8) | snapdata[PRG_OFFSET + 236];
	m_maincpu->set_state_int(Z80_HL, data);


	data = (snapdata[PRG_OFFSET + 231] << 8) | snapdata[PRG_OFFSET + 230];
	m_maincpu->set_state_int(Z80_AF2, data);

	data = (snapdata[PRG_OFFSET + 227] << 8) | snapdata[PRG_OFFSET + 226];
	m_maincpu->set_state_int(Z80_BC2, data);

	data = (snapdata[PRG_OFFSET + 225] << 8) | snapdata[PRG_OFFSET + 224];
	m_maincpu->set_state_int(Z80_DE2, data);

	data = (snapdata[PRG_OFFSET + 229] << 8) | snapdata[PRG_OFFSET + 228];
	m_maincpu->set_state_int(Z80_HL2, data);


	data = (snapdata[PRG_OFFSET + 223] << 8) | snapdata[PRG_OFFSET + 222];
	m_maincpu->set_state_int(Z80_IX, data);

	data = (snapdata[PRG_OFFSET + 221] << 8) | snapdata[PRG_OFFSET + 220];
	m_maincpu->set_state_int(Z80_IY, data);

	data = snapdata[PRG_OFFSET + 239];
	m_maincpu->set_state_int(Z80_I, data);

	m_maincpu->set_state_int(Z80_IM, (data == 0x00 || data == 0x3f) ? 1 : 2);

	// Memory dump
	logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
	for (i = 0; i < 3*SPECTRUM_BANK; i++)
		space.write_byte(BASE_RAM + i, snapdata[PRG_HDR + i]);

	addr = (snapdata[PRG_OFFSET + 241] << 8) | snapdata[PRG_OFFSET + 240];
	if (addr < BASE_RAM || addr > 4*SPECTRUM_BANK - 6)
		logerror("Corrupted SP out of range:%04X", addr);
	else
		logerror("Fetching registers IFF1/2, R, AF and PC from the stack at SP:%04X\n", addr);

	data = space.read_byte(addr + 0); // IFF1/2: (bit 2, 0=DI/1=EI)
	m_maincpu->set_state_int(Z80_IFF1, BIT(data, 2));
	m_maincpu->set_state_int(Z80_IFF2, BIT(data, 2));

	intr = BIT(data, 2) ? CLEAR_LINE : ASSERT_LINE;
	m_maincpu->set_input_line(INPUT_LINE_IRQ0, intr);
	m_maincpu->set_input_line(INPUT_LINE_HALT, CLEAR_LINE);

	data = space.read_byte(addr + 1);
	m_maincpu->set_state_int(Z80_R, data);

	data = (space.read_byte(addr + 3) << 8) | space.read_byte(addr + 2);
	m_maincpu->set_state_int(Z80_AF, data);

	data = (space.read_byte(addr + 5) << 8) | space.read_byte(addr + 4);
	m_maincpu->set_state_int(Z80_PC, data);

#if 0
	space.write_byte(addr + 0, 0); // It's been reported that zeroing these locations fixes the loading
	space.write_byte(addr + 1, 0); // of a few images that were snapshot at a "wrong" instant
	space.write_byte(addr + 2, 0);
	space.write_byte(addr + 3, 0);
	space.write_byte(addr + 4, 0);
	space.write_byte(addr + 5, 0);
#endif

	addr += 6;
	logerror("Fixing SP:%04X\n", addr);
	m_maincpu->set_state_int(Z80_SP, addr);

	// Set border color
	data = (space.read_byte(0x5c48) >> 3) & 0x07; // Get the current border color from BORDCR system variable.
	m_port_fe_data = (m_port_fe_data & 0xf8) | data;
	border_update(data);
	logerror("Border color:%02X\n", data);

	page_basicrom();

	//logerror("Snapshot loaded.\nExecution resuming at %s\n", m_maincpu->state_string(Z80_PC).c_str());
}

/*******************************************************************
 *
 *      Load a .PLUSD file.
 *
 *      .PLUSD files are the snapshots produced by the DISCiPLE or
 *      the +D disk interface, thus share the same internal organization
 *      of the .PRG files, with a couple of notable exceptions:
 *
 *      1) The filesystem metadata is missing
 *      2) Spectrum 128 snapshots (file type 9) are allowed
 *
 *      A commented disassembly of both BIOSes is avilable at:
 *
 *      http://www.biehold.nl/rudy/
 *
 *      48K snapshots are 49174 bytes long, 128K snapshots are 131095
 *      bytes long.
 *
 *      Philip Kendall's FUSE emulator at http://fuse-emulator.sourceforge.net
 *      supports this format, but no extension is officially provided.
 *      Therefore I have used .PLUSD - corrections are welcome!
 *
 *      48K snapshot format
 *      -------------------
 *
 *      Offset  Size    Description (all registers stored with LSB first
 *      ------- ------- -------------------------------------------------
 *      0       2       IY
 *      2       2       IX
 *      4       2       DE'
 *      6       2       BC'
 *      8       2       HL'
 *      10      2       AF'
 *      12      2       DE
 *      14      2       BC
 *      16      2       HL
 *      18      1       Junk created when saving I via LD A,I/PUSH AF
 *      19      1       I
 *      20      2       SP
 *      22      49152   RAM dump
 *
 *      128K snapshot format
 *      --------------------
 *
 *      Offset  Size    Description (all registers stored with LSB first
 *      ------- ------- -------------------------------------------------
 *      0       2       IY
 *      2       2       IX
 *      4       2       DE'
 *      6       2       BC'
 *      8       2       HL'
 *      10      2       AF'
 *      12      2       DE
 *      14      2       BC
 *      16      2       HL
 *      18      1       Junk created when saving I via LD A,I/PUSH AF
 *      19      1       I
 *      20      2       SP
 *      22      1       Port 0x7FFD data
 *      23      131072  RAM dump
 *
 *      The 8 RAM banks are stored in the order 0-7.
 *
 *      IFF1/2, R, AF and PC are stored on the stack. Due to a bug in the
 *      BIOS, the snapshots created with the DISCiPLE have the AF' register
 *      replaced with the AF register.
 *
 *      It's unknown (but likely possible) that the snapshots could
 *      suffer from the same "top of the stack" bug as well as .SNA images.
 *
 *******************************************************************/
void spectrum_state::setup_plusd(uint8_t *snapdata, uint32_t snapsize)
{
	int i, j;
	uint8_t intr;
	uint16_t addr = 0, data;
	address_space &space = m_maincpu->space(AS_PROGRAM);

	data = (snapdata[PLUSD_OFFSET + 15] << 8) | snapdata[PLUSD_OFFSET + 14];
	m_maincpu->set_state_int(Z80_BC, data);

	data = (snapdata[PLUSD_OFFSET + 13] << 8) | snapdata[PLUSD_OFFSET + 12];
	m_maincpu->set_state_int(Z80_DE, data);

	data = (snapdata[PLUSD_OFFSET + 17] << 8) | snapdata[PLUSD_OFFSET + 16];
	m_maincpu->set_state_int(Z80_HL, data);


	data = (snapdata[PLUSD_OFFSET + 11] << 8) | snapdata[PLUSD_OFFSET + 10];
	m_maincpu->set_state_int(Z80_AF2, data);

	data = (snapdata[PLUSD_OFFSET +  7] << 8) | snapdata[PLUSD_OFFSET +  6];
	m_maincpu->set_state_int(Z80_BC2, data);

	data = (snapdata[PLUSD_OFFSET +  5] << 8) | snapdata[PLUSD_OFFSET +  4];
	m_maincpu->set_state_int(Z80_DE2, data);

	data = (snapdata[PLUSD_OFFSET +  9] << 8) | snapdata[PLUSD_OFFSET +  8];
	m_maincpu->set_state_int(Z80_HL2, data);


	data = (snapdata[PLUSD_OFFSET +  3] << 8) | snapdata[PLUSD_OFFSET +  2];
	m_maincpu->set_state_int(Z80_IX, data);

	data = (snapdata[PLUSD_OFFSET +  1] << 8) | snapdata[PLUSD_OFFSET +  0];
	m_maincpu->set_state_int(Z80_IY, data);

	data = snapdata[PLUSD_OFFSET + 19];
	m_maincpu->set_state_int(Z80_I, data);

	m_maincpu->set_state_int(Z80_IM, (data == 0x00 || data == 0x3f) ? 1 : 2);

	if (snapsize == PLUSD48_SIZE)
	{
		page_basicrom();

		// Memory dump
		logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
		for (i = 0; i < 3*SPECTRUM_BANK; i++)
			space.write_byte(BASE_RAM + i, snapdata[PLUSD48_HDR + i]);
	}
	else
	{
		logerror("Loading %05X bytes of RAM at %04X\n", 8*SPECTRUM_BANK, BASE_RAM);
		for (i = 0; i < 8; i++)
		{
			switch (i)
			{
				case 5: addr = SPECTRUM_BANK;
						break;
				case 2: addr = 2*SPECTRUM_BANK;
						break;
				case 0:
				case 1:
				case 3:
				case 4:
				case 6:
				case 7: addr = 3*SPECTRUM_BANK;
						m_port_7ffd_data &= 0xf8;
						m_port_7ffd_data += i;
						update_paging();
						break;
			};
			logerror("Loading bank %d from offset:%05X\n", i, PLUSD128_HDR + i*SPECTRUM_BANK);
			for (j = 0; j < SPECTRUM_BANK; j++)
				space.write_byte(j + addr, snapdata[j + PLUSD128_HDR + i*SPECTRUM_BANK]);
		}
		m_port_7ffd_data = snapdata[PLUSD_OFFSET + 22];
		logerror("Port 7FFD:%02X\n", m_port_7ffd_data);
		logerror("Paging bank:%d\n", m_port_7ffd_data & 0x07);
		update_paging();
	}

	addr = (snapdata[PLUSD_OFFSET + 21] << 8) | snapdata[PLUSD_OFFSET + 20];
	if (addr < BASE_RAM || addr > 4*SPECTRUM_BANK - 6)
		logerror("Corrupted SP out of range:%04X", addr);
	else
		logerror("Fetching registers IFF1/2, R, AF and PC from the stack at SP:%04X\n", addr);

	data = space.read_byte(addr + 0); // IFF1/2: (bit 2, 0=DI/1=EI)
	m_maincpu->set_state_int(Z80_IFF1, BIT(data, 2));
	m_maincpu->set_state_int(Z80_IFF2, BIT(data, 2));

	intr = BIT(data, 2) ? CLEAR_LINE : ASSERT_LINE;
	m_maincpu->set_input_line(INPUT_LINE_IRQ0, intr);
	m_maincpu->set_input_line(INPUT_LINE_HALT, CLEAR_LINE);

	data = space.read_byte(addr + 1);
	m_maincpu->set_state_int(Z80_R, data);

	data = (space.read_byte(addr + 3) << 8) | space.read_byte(addr + 2);
	m_maincpu->set_state_int(Z80_AF, data);

	data = (space.read_byte(addr + 5) << 8) | space.read_byte(addr + 4);
	m_maincpu->set_state_int(Z80_PC, data);

#if 0
	space.write_byte(addr + 0, 0); // It's been reported that zeroing these locations fixes the loading
	space.write_byte(addr + 1, 0); // of a few images that were snapshot at a "wrong" instant
	space.write_byte(addr + 2, 0);
	space.write_byte(addr + 3, 0);
	space.write_byte(addr + 4, 0);
	space.write_byte(addr + 5, 0);
#endif

	addr += 6;
	logerror("Fixing SP:%04X\n", addr);
	m_maincpu->set_state_int(Z80_SP, addr);

	// Set border color
	data = (space.read_byte(0x5c48) >> 3) & 0x07; // Get the current border color from BORDCR system variable.
	m_port_fe_data = (m_port_fe_data & 0xf8) | data;
	border_update(data);
	logerror("Border color:%02X\n", data);

	//if (snapsize == PLUSD48_SIZE)
		//logerror("Snapshot loaded.\nExecution resuming at %s\n", m_maincpu->state_string(Z80_PC).c_str());
	//else
		//logerror("Snapshot loaded.\nExecution resuming at bank:%d %s\n", m_port_7ffd_data & 0x07, m_maincpu->state_string(Z80_PC).c_str());
}

/*******************************************************************
 *
 *      Load a .SEM file.
 *
 *      .SEM files were produced by a german emulator called SpecEmu
 *      written by Bernd Waschke.
 *
 *      The files are usually 49192 bytes long unless optional POKE
 *      blocks are stored at the end of the file.
 *
 *      Offset  Size    Description (all registers stored with LSB first)
 *      ------- ------- -------------------------------------------------
 *      0       1       0x05 (signature length)
 *      1       5       "SPEC1" (signature)
 *      6       49152   RAM dump
 *      49158   2       AF
 *      49160   2       BC
 *      49162   2       DE
 *      49164   2       HL
 *      49166   2       AF'
 *      49168   2       BC'
 *      49170   2       DE'
 *      49172   2       HL'
 *      49174   2       PC
 *      49176   2       SP
 *      49178   2       IX
 *      49180   2       IY
 *      49182   2       I
 *      49184   2       R
 *      49186   2       IFF1 (0=DI/1=EI)
 *      49188   2       IFF2 (0=DI/1=EI)
 *      49190   2       Interrupt mode (0=IM0/1=IM1/2=IM2)
 *
 *      Following these data, there are optional POKE blocks
 *
 *******************************************************************/
void spectrum_state::setup_sem(uint8_t *snapdata, uint32_t snapsize)
{
	int i;
	uint8_t intr;
	uint16_t data;
	address_space &space = m_maincpu->space(AS_PROGRAM);

	data = (snapdata[SEM_OFFSET +  1] << 8) | snapdata[SEM_OFFSET +  0];
	m_maincpu->set_state_int(Z80_AF, data);

	data = (snapdata[SEM_OFFSET +  3] << 8) | snapdata[SEM_OFFSET +  2];
	m_maincpu->set_state_int(Z80_BC, data);

	data = (snapdata[SEM_OFFSET +  5] << 8) | snapdata[SEM_OFFSET +  4];
	m_maincpu->set_state_int(Z80_DE, data);

	data = (snapdata[SEM_OFFSET +  7] << 8) | snapdata[SEM_OFFSET +  6];
	m_maincpu->set_state_int(Z80_HL, data);


	data = (snapdata[SEM_OFFSET +  9] << 8) | snapdata[SEM_OFFSET +  8];
	m_maincpu->set_state_int(Z80_AF2, data);

	data = (snapdata[SEM_OFFSET + 11] << 8) | snapdata[SEM_OFFSET + 10];
	m_maincpu->set_state_int(Z80_BC2, data);

	data = (snapdata[SEM_OFFSET + 13] << 8) | snapdata[SEM_OFFSET + 12];
	m_maincpu->set_state_int(Z80_DE2, data);

	data = (snapdata[SEM_OFFSET + 15] << 8) | snapdata[SEM_OFFSET + 14];
	m_maincpu->set_state_int(Z80_HL2, data);


	data = (snapdata[SEM_OFFSET + 21] << 8) | snapdata[SEM_OFFSET + 20];
	m_maincpu->set_state_int(Z80_IX, data);

	data = (snapdata[SEM_OFFSET + 23] << 8) | snapdata[SEM_OFFSET + 22];
	m_maincpu->set_state_int(Z80_IY, data);


	data = snapdata[SEM_OFFSET + 26];
	m_maincpu->set_state_int(Z80_R, data);

	data = snapdata[SEM_OFFSET + 24];
	m_maincpu->set_state_int(Z80_I, data);


	data = (snapdata[SEM_OFFSET + 19] << 8) | snapdata[SEM_OFFSET + 18];
	m_maincpu->set_state_int(Z80_SP, data);

	data = (snapdata[SEM_OFFSET + 17] << 8) | snapdata[SEM_OFFSET + 16];
	m_maincpu->set_state_int(Z80_PC, data);

	data = snapdata[SEM_OFFSET + 32];
	m_maincpu->set_state_int(Z80_IM, data);

	data = snapdata[SEM_OFFSET + 28];
	m_maincpu->set_state_int(Z80_IFF1, BIT(data, 0));
	data = snapdata[SEM_OFFSET + 30];
	m_maincpu->set_state_int(Z80_IFF2, BIT(data, 0));

	intr = BIT(snapdata[SEM_OFFSET + 30], 0) ? CLEAR_LINE : ASSERT_LINE;
	m_maincpu->set_input_line(INPUT_LINE_IRQ0, intr);
	m_maincpu->set_input_line(INPUT_LINE_HALT, CLEAR_LINE);

	// Memory dump
	logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
	for (i = 0; i < 3*SPECTRUM_BANK; i++)
		space.write_byte(BASE_RAM + i, snapdata[SEM_SIGNATURE + i]);

	// Set border color
	data = (space.read_byte(0x5c48) >> 3) & 0x07; // Get the current border color from BORDCR system variable.
	m_port_fe_data = (m_port_fe_data & 0xf8) | data;
	border_update(data);
	logerror("Border color:%02X\n", data);

	page_basicrom();

	//logerror("Snapshot loaded.\nExecution resuming at %s\n", m_maincpu->state_string(Z80_PC).c_str());

/* TODO: Decode the optional POKE bank at the end of the image */

}

/*******************************************************************
 *
 *      Load a .SIT file.
 *
 *      .SIT files were produced by a spanish emulator called Sinclair
 *      written by Pedro M. R. Salas.
 *
 *      http://www.ugr.es/~pedrom/sinclair.htm
 *
 *      The files are always 65564 bytes long and store both ROM and RAM.
 *
 *      Offset  Size    Description (all registers stored with LSB first)
 *      ------- ------- -------------------------------------------------
 *      0       2       BC
 *      2       2       DE
 *      4       2       HL
 *      6       2       AF
 *      8       2       IX
 *      10      2       IY
 *      12      2       SP
 *      14      2       PC
 *      16      1       R
 *      17      1       I
 *      18      2       BC'
 *      20      2       DE'
 *      22      2       HL'
 *      24      2       AF'
 *      26      1       Interrupt mode (0=IM0/1=IM1/2=IM2)
 *      27      1       Border color
 *      28      16384   ROM dump
 *      16412   49152   RAM dump
 *
 *******************************************************************/
void spectrum_state::setup_sit(uint8_t *snapdata, uint32_t snapsize)
{
	int i;
	uint8_t intr;
	uint16_t data;
	address_space &space = m_maincpu->space(AS_PROGRAM);

	data = (snapdata[SIT_OFFSET +  7] << 8) | snapdata[SIT_OFFSET +  6];
	m_maincpu->set_state_int(Z80_AF, data);

	data = (snapdata[SIT_OFFSET +  1] << 8) | snapdata[SIT_OFFSET +  0];
	m_maincpu->set_state_int(Z80_BC, data);

	data = (snapdata[SIT_OFFSET +  3] << 8) | snapdata[SIT_OFFSET +  2];
	m_maincpu->set_state_int(Z80_DE, data);

	data = (snapdata[SIT_OFFSET +  5] << 8) | snapdata[SIT_OFFSET +  4];
	m_maincpu->set_state_int(Z80_HL, data);


	data = (snapdata[SIT_OFFSET + 25] << 8) | snapdata[SIT_OFFSET + 24];
	m_maincpu->set_state_int(Z80_AF2, data);

	data = (snapdata[SIT_OFFSET + 19] << 8) | snapdata[SIT_OFFSET + 18];
	m_maincpu->set_state_int(Z80_BC2, data);

	data = (snapdata[SIT_OFFSET + 21] << 8) | snapdata[SIT_OFFSET + 20];
	m_maincpu->set_state_int(Z80_DE2, data);

	data = (snapdata[SIT_OFFSET + 23] << 8) | snapdata[SIT_OFFSET + 22];
	m_maincpu->set_state_int(Z80_HL2, data);


	data = (snapdata[SIT_OFFSET +  9] << 8) | snapdata[SIT_OFFSET +  8];
	m_maincpu->set_state_int(Z80_IX, data);

	data = (snapdata[SIT_OFFSET + 11] << 8) | snapdata[SIT_OFFSET + 10];
	m_maincpu->set_state_int(Z80_IY, data);


	data = snapdata[SIT_OFFSET + 16];
	m_maincpu->set_state_int(Z80_R, data);

	data = snapdata[SIT_OFFSET + 17];
	m_maincpu->set_state_int(Z80_I, data);


	data = (snapdata[SIT_OFFSET + 13] << 8) | snapdata[SIT_OFFSET + 12];
	m_maincpu->set_state_int(Z80_SP, data);

	data = (snapdata[SIT_OFFSET + 15] << 8) | snapdata[SIT_OFFSET + 14];
	m_maincpu->set_state_int(Z80_PC, data);

	data = snapdata[SIT_OFFSET + 26];
	m_maincpu->set_state_int(Z80_IM, data);

	data = 0x01; // .SIT snapshots don't specify whether interrupts are enabled or not, so I assume they are.
	m_maincpu->set_state_int(Z80_IFF1, data);
	m_maincpu->set_state_int(Z80_IFF2, data);

	intr = data ? CLEAR_LINE : ASSERT_LINE;
	m_maincpu->set_input_line(INPUT_LINE_IRQ0, intr);
	m_maincpu->set_input_line(INPUT_LINE_HALT, CLEAR_LINE);

	// Memory dump
	logerror("Skipping the 16K ROM dump at offset:%04X\n", SIT_OFFSET + 28);
	logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
	for (i = 0; i < 3*SPECTRUM_BANK; i++)
		space.write_byte(BASE_RAM + i, snapdata[SIT_HDR + SPECTRUM_BANK + i]);

	// Set border color
	data = snapdata[SIT_OFFSET + 27] & 0x07;
	m_port_fe_data = (m_port_fe_data & 0xf8) | data;
	border_update(data);
	logerror("Border color:%02X\n", data);

	page_basicrom();

	//logerror("Snapshot loaded.\nExecution resuming at %s\n", m_maincpu->state_string(Z80_PC).c_str());
}

/*******************************************************************
 *
 *      Load a .ZX file.
 *
 *      .ZX files were produced by the Amiga emulator called KGB
 *
 *      The files are usually 49486 bytes and store at the beginning
 *      of the file the last 132 bytes of the ROM.
 *
 *      Offset  Size    Description (all registers stored with MSB first)
 *      ------- ------- -------------------------------------------------
 *      0       132     Last 132 bytes of ROM dump
 *      132     49152   RAM dump
 *      49284   132     0x00 (reserved for future use)
 *      49416   10      Various KGB settings
 *      49426   1       IFF1/2: (0=DI/1=EI)
 *      49427   2       Reserved (must be 0x0003)
 *      49429   1       KGB ColorMode (0=BW/1=Color)
 *      49430   4       0x00 (reserved for future use)
 *      49434   2       BC
 *      49436   2       BC'
 *      49438   2       DE
 *      49440   2       DE'
 *      49442   2       HL
 *      49444   2       HL'
 *      49446   2       IX
 *      49448   2       IY
 *      49450   1       I
 *      49451   1       R
 *      49452   3       0x00 (reserved for future use)
 *      49455   1       A'
 *      49456   1       0x00 (reserved for future use)
 *      49457   1       A
 *      49458   1       0x00 (reserved for future use)
 *      49459   1       F'
 *      49460   1       0x00 (reserved for future use)
 *      49461   1       F
 *      49462   2       0x00 (reserved for future use)
 *      49464   2       PC
 *      49466   2       0x00 (reserved for future use)
 *      49468   2       SP
 *      49470   2       KGB Soundmode (0=Simple/1=Pitch/2=RomOnly)
 *      49472   2       KGB HaltMode (0=NoHalt/1=Halt)
 *      49474   2       Interrupt mode (-1=IM0/0=IM1/1=IM2)
 *      49476   10      0x00 (reserved for future use)
 *
 *******************************************************************/
void spectrum_state::setup_zx(uint8_t *snapdata, uint32_t snapsize)
{
	int i;
	uint8_t intr;
	uint16_t data, mode;
	address_space &space = m_maincpu->space(AS_PROGRAM);

	logerror("Skipping last 132 bytes of the 16K ROM dump at offset:0000\n");

	data = (snapdata[ZX_OFFSET + 173] << 8) | snapdata[ZX_OFFSET + 177];
	m_maincpu->set_state_int(Z80_AF, data);

	data = (snapdata[ZX_OFFSET + 150] << 8) | snapdata[ZX_OFFSET + 151];
	m_maincpu->set_state_int(Z80_BC, data);

	data = (snapdata[ZX_OFFSET + 154] << 8) | snapdata[ZX_OFFSET + 155];
	m_maincpu->set_state_int(Z80_DE, data);

	data = (snapdata[ZX_OFFSET + 158] << 8) | snapdata[ZX_OFFSET + 159];
	m_maincpu->set_state_int(Z80_HL, data);


	data = (snapdata[ZX_OFFSET + 171] << 8) | snapdata[ZX_OFFSET + 175];
	m_maincpu->set_state_int(Z80_AF2, data);

	data = (snapdata[ZX_OFFSET + 152] << 8) | snapdata[ZX_OFFSET + 153];
	m_maincpu->set_state_int(Z80_BC2, data);

	data = (snapdata[ZX_OFFSET + 156] << 8) | snapdata[ZX_OFFSET + 157];
	m_maincpu->set_state_int(Z80_DE2, data);

	data = (snapdata[ZX_OFFSET + 160] << 8) | snapdata[ZX_OFFSET + 161];
	m_maincpu->set_state_int(Z80_HL2, data);


	data = (snapdata[ZX_OFFSET + 162] << 8) | snapdata[ZX_OFFSET + 163];
	m_maincpu->set_state_int(Z80_IX, data);

	data = (snapdata[ZX_OFFSET + 164] << 8) | snapdata[ZX_OFFSET + 165];
	m_maincpu->set_state_int(Z80_IY, data);


	data = snapdata[ZX_OFFSET + 167];
	m_maincpu->set_state_int(Z80_R, data);

	data = snapdata[ZX_OFFSET + 166];
	m_maincpu->set_state_int(Z80_I, data);


	data = (snapdata[ZX_OFFSET + 184] << 8) | snapdata[ZX_OFFSET + 185];
	m_maincpu->set_state_int(Z80_SP, data);

	data = (snapdata[ZX_OFFSET + 180] << 8) | snapdata[ZX_OFFSET + 181];
	m_maincpu->set_state_int(Z80_PC, data);

	mode = (snapdata[ZX_OFFSET + 190] << 8) | snapdata[ZX_OFFSET + 191];
	switch (mode)
	{
		case 0xffff:
		m_maincpu->set_state_int(Z80_IM, (uint64_t)0);
		break;
		case 0x00:
		m_maincpu->set_state_int(Z80_IM, 1);
		break;
		case 0x01:
		m_maincpu->set_state_int(Z80_IM, 2);
		break;
		default:
		logerror("Invalid IM:%04X\n", mode);
	}

	data = BIT(snapdata[ZX_OFFSET + 142], 0);
	m_maincpu->set_state_int(Z80_IFF1, data);
	m_maincpu->set_state_int(Z80_IFF2, data);

	intr = BIT(snapdata[ZX_OFFSET + 142], 0) ? CLEAR_LINE : ASSERT_LINE;
	m_maincpu->set_input_line(INPUT_LINE_IRQ0, intr);
	m_maincpu->set_input_line(INPUT_LINE_HALT, CLEAR_LINE);

	// Memory dump
	logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
	for (i = 0; i < 3*SPECTRUM_BANK; i++)
		space.write_byte(BASE_RAM + i, snapdata[132 + i]);

	// Set border color
	data = (space.read_byte(0x5c48) >> 3) & 0x07; // Get the current border color from BORDCR system variable.
	m_port_fe_data = (m_port_fe_data & 0xf8) | data;
	border_update(data);
	logerror("Border color:%02X\n", data);

	page_basicrom();

	//logerror("Snapshot loaded.\nExecution resuming at %s\n", m_maincpu->state_string(Z80_PC).c_str());
}

/*******************************************************************
 *
 *      Load a .SNP file.
 *
 *      .SNP files were produced by a polish emulator called Nuclear ZX
 *      written by Radovan Garabik and Lubomir Salanci.
 *
 *      http://korpus.juls.savba.sk/~garabik/old/zx.html
 *
 *      The files are always 49183 bytes long.
 *
 *      Offset  Size    Description (all registers stored with LSB first)
 *      ------- ------- -------------------------------------------------
 *      0       49152   RAM dump
 *      49152   2       AF
 *      49154   1       Border color
 *      49155   1       0x00 (reserved for future use)
 *      49156   2       BC
 *      49158   2       DE
 *      49160   2       HL
 *      49162   2       PC
 *      49164   2       SP
 *      49166   2       IX
 *      49168   2       IY
 *      49170   1       0x00 (reserved for IFF2 but not implemented)
 *      49171   1       IFF1 (0=DI/other=EI)
 *      49172   1       Interrupt mode (0=IM0/1=IM1/2=IM2)
 *      49173   1       R
 *      49174   1       I
 *      49175   2       AF'
 *      49177   2       BC'
 *      49179   2       DE'
 *      49181   2       HL'
 *
 *******************************************************************/
void spectrum_state::setup_snp(uint8_t *snapdata, uint32_t snapsize)
{
	int i;
	uint8_t intr;
	uint16_t data;
	address_space &space = m_maincpu->space(AS_PROGRAM);

	data = (snapdata[SNP_OFFSET +  1] << 8) | snapdata[SNP_OFFSET +  0];
	m_maincpu->set_state_int(Z80_AF, data);

	data = (snapdata[SNP_OFFSET +  5] << 8) | snapdata[SNP_OFFSET +  4];
	m_maincpu->set_state_int(Z80_BC, data);

	data = (snapdata[SNP_OFFSET +  7] << 8) | snapdata[SNP_OFFSET +  6];
	m_maincpu->set_state_int(Z80_DE, data);

	data = (snapdata[SNP_OFFSET +  9] << 8) | snapdata[SNP_OFFSET +  8];
	m_maincpu->set_state_int(Z80_HL, data);


	data = (snapdata[SNP_OFFSET + 24] << 8) | snapdata[SNP_OFFSET + 23];
	m_maincpu->set_state_int(Z80_AF2, data);

	data = (snapdata[SNP_OFFSET + 26] << 8) | snapdata[SNP_OFFSET + 25];
	m_maincpu->set_state_int(Z80_BC2, data);

	data = (snapdata[SNP_OFFSET + 28] << 8) | snapdata[SNP_OFFSET + 27];
	m_maincpu->set_state_int(Z80_DE2, data);

	data = (snapdata[SNP_OFFSET + 30] << 8) | snapdata[SNP_OFFSET + 29];
	m_maincpu->set_state_int(Z80_HL2, data);


	data = (snapdata[SNP_OFFSET + 15] << 8) | snapdata[SNP_OFFSET + 14];
	m_maincpu->set_state_int(Z80_IX, data);

	data = (snapdata[SNP_OFFSET + 17] << 8) | snapdata[SNP_OFFSET + 16];
	m_maincpu->set_state_int(Z80_IY, data);


	data = snapdata[SNP_OFFSET + 21];
	m_maincpu->set_state_int(Z80_R, data);

	data = snapdata[SNP_OFFSET + 22];
	m_maincpu->set_state_int(Z80_I, data);


	data = (snapdata[SNP_OFFSET + 13] << 8) | snapdata[SNP_OFFSET + 12];
	m_maincpu->set_state_int(Z80_SP, data);

	data = (snapdata[SNP_OFFSET + 11] << 8) | snapdata[SNP_OFFSET + 10];
	m_maincpu->set_state_int(Z80_PC, data);


	data = snapdata[SNP_OFFSET + 20] & 0x03;
	m_maincpu->set_state_int(Z80_IM, data);

	data = BIT(snapdata[SNP_OFFSET + 19], 0);
	m_maincpu->set_state_int(Z80_IFF1, data);
	m_maincpu->set_state_int(Z80_IFF2, data);

	intr = BIT(snapdata[SNP_OFFSET + 19], 0) ? CLEAR_LINE : ASSERT_LINE;
	m_maincpu->set_input_line(INPUT_LINE_IRQ0, intr);
	m_maincpu->set_input_line(INPUT_LINE_HALT, CLEAR_LINE);

	// Memory dump
	logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
	for (i = 0; i < 3*SPECTRUM_BANK; i++)
		space.write_byte(BASE_RAM + i, snapdata[i]);

	// Set border color
	data = snapdata[SNP_OFFSET +  2] & 0x07;
	m_port_fe_data = (m_port_fe_data & 0xf8) | data;
	border_update(data);
	logerror("Border color:%02X\n", data);

	page_basicrom();

	//logerror("Snapshot loaded.\nExecution resuming at %s\n", m_maincpu->state_string(Z80_PC).c_str());
}

/*******************************************************************
 *
 *      Load a .SNX file.
 *
 *      .SNX files were produced by an Atari ST emulator called Specci
 *      written by Christian Gandler
 *
 *      http://cd.textfiles.com/crawlycrypt1/apps/misc/zx_sp207/
 *
 *      The header is an extension of the .SNA format and the RAM dump
 *      is compressed with a simple run-length algorithm.
 *
 *      Offset  Size    Description (all registers stored with LSB first
 *                      except when noted with *BE*)
 *      ------- ------- -------------------------------------------------
 *      0       4       "XSNA" (signature)
 *      4       2       Header length *BE*
 *      6       1       I
 *      7       2       HL'
 *      9       2       DE'
 *      11      2       BC'
 *      13      2       AF'
 *      15      2       HL
 *      17      2       DE
 *      19      2       BC
 *      21      2       IY
 *      23      2       IX
 *      25      1       IFF1/2: bit 0 contains IFF1, bit 2 contains IFF2 (0=DI/1=EI)
 *      26      1       R
 *      27      2       AF
 *      29      2       SP
 *      31      1       Interrupt Mode (0=IM0/1=IM1/2=IM2)
 *      32      1       Border color
 *      33      1       Specci settings: Interface 1 (0=no/1=yes)
 *      34      1       Specci settings: FLASH emulation (0=no/1=yes)
 *      35      1       Specci settings: Attributes emulation (0=no/1=yes)
 *      36      1       Specci settings: Bit 7 - Keyboard (0=QWERTY/1=QWERTZ)
 *                                       Bit 6 - ULA emulation (0=no/1=yes)
 *                                       Bit 0,1 - Joystick interface
 *                                            00 = Kempston
 *                                            01 = IF2 Left
 *                                            10 = IF2 Right
 *                                            11 = Cursor/AGF/Protek
 *      37      1       Specci settings: R register emulation (0=no/1=yes)
 *                                       Bit 7 - EAR bit (0=Issue 3/1=Issue 2)
 *      38      1       Specci settings: Interrupt frequency (0=50 Hz/1=100 Hz)
 *      39      1       Specci settings: RS232 redirection (0=RS232/1=Centronics)
 *      40      1       Specci settings: Sound emulation
 *                                       Bit 4,7 - Frequency (0..4) for mode 2
 *                                       Bit 0,3 - Mode (0=off/1=direct/2=interrupt)
 *      41      1       Specci settings: Border emulation (0=off/1=direct/2=interrupt)
 *      42      1       Specci settings: IM2 hardware vector (0..255)
 *      43      ?????   RAM dump
 *
 *      PC is stored on stack.
 *
 *      The RAM dump is divided into short or long datablocks:
 *      if the block is composed of the same byte, it is replaced by
 *      a compressed version. The format of the various blocks follows:
 *
 *      Uncompressed short block
 *      ------------------------
 *      Offset  Size    Description
 *      ------- ------- -------------------------------------------------
 *      0       1       Marker byte. Values between 0xe0 and 0xef.
 *                      Low nibble is (block length - 1).
 *      1       ?????   Data block
 *
 *      Compressed short block
 *      ----------------------
 *      Offset  Size    Description
 *      ------- ------- -------------------------------------------------
 *      0       1       Marker byte. Values between 0xf0 and 0xff.
 *                      Low nibble is (block length - 1).
 *      1       1       Filler byte. This byte is repeated for the whole
 *                      length of the block.
 *
 *      Uncompressed long block
 *      -----------------------
 *      Offset  Size    Description
 *      ------- ------- -------------------------------------------------
 *      0       2       Block length in big endian format.
 *                      Values between 0x0000 and 0xdfff.
 *      2       1       Uncompressed block flag: 0xff.
 *      3       ?????   Data block
 *
 *      Compressed long block
 *      ---------------------
 *      Offset  Size    Description
 *      ------- ------- -------------------------------------------------
 *      0       2       Block length in big endian format.
 *                      Values between 0x0000 and 0xdfff.
 *      2       1       Compressed block flag: 0x00.
 *      3       1       Filler byte. This byte is repeated for the whole
 *                      length of the block.
 *
 *******************************************************************/
void spectrum_state::snx_decompress_block(address_space &space, uint8_t *source, uint16_t dest, uint16_t size)
{
	uint8_t counthi, countlo, compress, fill;
	uint16_t block = 0, count, i, j, numbytes;
	i = SNX_HDR - 1;
	numbytes = 0;

	while (numbytes < size)
	{
		counthi = source[++i];
		if (counthi >= 0xe0)
		{
			count = (counthi & 0x0f) + 1;     // Short block
			if ((counthi & 0xf0) == 0xf0)
				compress = SNX_COMPRESSED;
			else
				compress = SNX_UNCOMPRESSED;
			logerror("Block:%05d  Type:Short  Compr:%s  Offset:%04X  Len:%04X  ", block++, compress == SNX_COMPRESSED ? "Y" : "N", i-1, count);
		}
		else
		{
			countlo = source[++i];
			count = (counthi << 8) | countlo; // Long block
			compress = source[++i];
			logerror("Block:%05d  Type:Long   Compr:%s  Offset:%04X  Len:%04X  ", block++, compress == SNX_COMPRESSED ? "Y" : "N", i-3, count);
		}

		if (compress == SNX_COMPRESSED)
		{
			fill = source[++i];
			logerror("Dest:%04X  Filler:%02X\n", BASE_RAM + numbytes, fill);
			for(j = 0; j < count; j++)
				space.write_byte(BASE_RAM + numbytes + j, fill);
			numbytes += count;
		}
		else
		{
			logerror("Dest:%04X\n", BASE_RAM + numbytes);
			j = 0;
			while (j < count)
				space.write_byte(BASE_RAM + numbytes + j++, source[++i]);
			numbytes += count;
		}
	}
}

void spectrum_state::setup_snx(uint8_t *snapdata, uint32_t snapsize)
{
	uint8_t intr;
	uint16_t data, addr;
	address_space &space = m_maincpu->space(AS_PROGRAM);

	data = (snapdata[SNX_OFFSET +  4] << 8) | snapdata[SNX_OFFSET +  5];
	if (data != 0x25)
		logerror("Corrupted header length: %02X instead of 0x25\n", data);

	data = (snapdata[SNX_OFFSET + 28] << 8) | snapdata[SNX_OFFSET + 27];
	m_maincpu->set_state_int(Z80_AF, data);

	data = (snapdata[SNX_OFFSET + 20] << 8) | snapdata[SNX_OFFSET + 19];
	m_maincpu->set_state_int(Z80_BC, data);

	data = (snapdata[SNX_OFFSET + 18] << 8) | snapdata[SNX_OFFSET + 17];
	m_maincpu->set_state_int(Z80_DE, data);

	data = (snapdata[SNX_OFFSET + 16] << 8) | snapdata[SNX_OFFSET + 15];
	m_maincpu->set_state_int(Z80_HL, data);


	data = (snapdata[SNX_OFFSET + 14] << 8) | snapdata[SNX_OFFSET + 13];
	m_maincpu->set_state_int(Z80_AF2, data);

	data = (snapdata[SNX_OFFSET + 12] << 8) | snapdata[SNX_OFFSET + 11];
	m_maincpu->set_state_int(Z80_BC2, data);

	data = (snapdata[SNX_OFFSET + 10] << 8) | snapdata[SNX_OFFSET +  9];
	m_maincpu->set_state_int(Z80_DE2, data);

	data = (snapdata[SNX_OFFSET +  8] << 8) | snapdata[SNX_OFFSET +  7];
	m_maincpu->set_state_int(Z80_HL2, data);


	data = (snapdata[SNX_OFFSET + 24] << 8) | snapdata[SNX_OFFSET + 23];
	m_maincpu->set_state_int(Z80_IX, data);

	data = (snapdata[SNX_OFFSET + 22] << 8) | snapdata[SNX_OFFSET + 21];
	m_maincpu->set_state_int(Z80_IY, data);


	data = snapdata[SNX_OFFSET + 26];
	m_maincpu->set_state_int(Z80_R, data);

	data = snapdata[SNX_OFFSET +  6];
	m_maincpu->set_state_int(Z80_I, data);


	data = (snapdata[SNX_OFFSET + 30] << 8) | snapdata[SNX_OFFSET + 29];
	m_maincpu->set_state_int(Z80_SP, data);


	data = snapdata[SNX_OFFSET + 31] & 0x03;
	if (data == 3)
		data = 2;
	m_maincpu->set_state_int(Z80_IM, data);

	data = snapdata[SNX_OFFSET + 25];
	m_maincpu->set_state_int(Z80_IFF1, BIT(data, 0));
	m_maincpu->set_state_int(Z80_IFF2, BIT(data, 2));

	intr = BIT(data, 0) ? CLEAR_LINE : ASSERT_LINE;
	m_maincpu->set_input_line(INPUT_LINE_IRQ0, intr);
	m_maincpu->set_input_line(INPUT_LINE_HALT, CLEAR_LINE);

	// Memory dump
	logerror("Uncompressing %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
	snx_decompress_block(space, snapdata, BASE_RAM, 3*SPECTRUM_BANK);

	// get pc from stack
	addr = m_maincpu->state_int(Z80_SP);

	if (addr < BASE_RAM || addr > 4*SPECTRUM_BANK - 2)
		logerror("Corrupted SP out of range:%04X", addr);
	else
		logerror("Fetching PC from the stack at SP:%04X\n", addr);

	m_maincpu->set_state_int(Z80_PC, (space.read_byte(addr + 1) << 8) | space.read_byte(addr + 0));

#if 0
	space.write_byte(addr + 0, 0); // It's been reported that zeroing these locations fixes the loading
	space.write_byte(addr + 1, 0); // of a few images that were snapshot at a "wrong" instant
#endif

	addr += 2;
	logerror("Fixed the stack at SP:%04X\n", addr);
	m_maincpu->set_state_int(Z80_SP, addr);

	// Set border color
	data = snapdata[SNX_OFFSET + 32] & 0x07;
	m_port_fe_data = (m_port_fe_data & 0xf8) | data;
	border_update(data);
	logerror("Border color:%02X\n", data);

	page_basicrom();

/* TODO: Enable/disable IF1 as per snapdata[SNX_OFFSET + 33] */

/* TODO: Enable/disable joysticks as per snapdata[SNX_OFFSET + 36] */

/* TODO: Enable selection of Issue 2/3 config switch as per snapdata[SNX_OFFSET + 37] */

	//logerror("Snapshot loaded.\nExecution resuming at %s\n", m_maincpu->state_string(Z80_PC).c_str());
}

/*******************************************************************
 *
 *      Load a .FRZ file.
 *
 *      .FRZ files were produced by the czech Amiga emulator CBSpeccy
 *      written by the CodeBusters. Kudos to Dmitriy Zhivilov and
 *      Ian Greenway for having reverse-engineered and shared the
 *      description of this format.
 *
 *      The original specs of this format were published on a
 *      russian electronics magazine.
 *
 *      The format is always 131114 bytes long and supports only
 *      Spectrum 128K images.
 *
 *      Offset  Size    Description (all registers stored with MSB first)
 *      ------- ------- -------------------------------------------------
 *      0       1       0x00 (reserved for future use)
 *      1       1       Port 0x7FFD data
 *      2       2       HL'
 *      4       2       HL
 *      6       2       DE'
 *      8       2       DE
 *      10      2       BC'
 *      12      2       BC
 *      14      2       AF'
 *      16      2       AF
 *      18      7       Emulation disk registers and T-states
 *      25      1       R
 *      26      2       PC
 *      28      2       SP
 *      30      1       I
 *      31      1       0xFF (reserved for future use)
 *      32      1       0x00 (reserved for future use)
 *      33      1       Interrupt mode (0=IM0/1=IM1/2=IM2)
 *      34      3       0x00 (reserved for future use)
 *      37      1       IFF1: bit 2 contains IFF1 (0=DI/1=EI)
 *      38      2       IY
 *      40      2       IX
 *      42      131072  RAM dump
 *
 *      The 8 16K banks are stored in the order 5, 2, 0, 1, 3, 4, 6, 7
 *
 *******************************************************************/
void spectrum_state::setup_frz(uint8_t *snapdata, uint32_t snapsize)
{
	int i, j;
	uint8_t intr;
	uint16_t addr, data;
	address_space &space = m_maincpu->space(AS_PROGRAM);

	if (m_port_7ffd_data == -1)
	{
		logerror("Can't load 128K .FRZ file into 48K machine\n");
		return;
	}

	data = (snapdata[FRZ_OFFSET + 16] << 8) | snapdata[FRZ_OFFSET + 17];
	m_maincpu->set_state_int(Z80_AF, data);

	data = (snapdata[FRZ_OFFSET + 12] << 8) | snapdata[FRZ_OFFSET + 13];
	m_maincpu->set_state_int(Z80_BC, data);

	data = (snapdata[FRZ_OFFSET +  8] << 8) | snapdata[FRZ_OFFSET +  9];
	m_maincpu->set_state_int(Z80_DE, data);

	data = (snapdata[FRZ_OFFSET +  4] << 8) | snapdata[FRZ_OFFSET +  5];
	m_maincpu->set_state_int(Z80_HL, data);


	data = (snapdata[FRZ_OFFSET + 14] << 8) | snapdata[FRZ_OFFSET + 15];
	m_maincpu->set_state_int(Z80_AF2, data);

	data = (snapdata[FRZ_OFFSET + 10] << 8) | snapdata[FRZ_OFFSET + 11];
	m_maincpu->set_state_int(Z80_BC2, data);

	data = (snapdata[FRZ_OFFSET +  6] << 8) | snapdata[FRZ_OFFSET +  7];
	m_maincpu->set_state_int(Z80_DE2, data);

	data = (snapdata[FRZ_OFFSET +  2] << 8) | snapdata[FRZ_OFFSET +  3];
	m_maincpu->set_state_int(Z80_HL2, data);


	data = (snapdata[FRZ_OFFSET + 40] << 8) | snapdata[FRZ_OFFSET + 41];
	m_maincpu->set_state_int(Z80_IX, data);

	data = (snapdata[FRZ_OFFSET + 38] << 8) | snapdata[FRZ_OFFSET + 39];
	m_maincpu->set_state_int(Z80_IY, data);


	data = snapdata[FRZ_OFFSET + 25];
	m_maincpu->set_state_int(Z80_R, data);

	data = snapdata[FRZ_OFFSET + 30];
	m_maincpu->set_state_int(Z80_I, data);


	data = (snapdata[FRZ_OFFSET + 28] << 8) | snapdata[FRZ_OFFSET + 29];
	m_maincpu->set_state_int(Z80_SP, data);

	data = (snapdata[FRZ_OFFSET + 26] << 8) | snapdata[FRZ_OFFSET + 27];
	m_maincpu->set_state_int(Z80_PC, data);


	data = snapdata[FRZ_OFFSET + 33];
	m_maincpu->set_state_int(Z80_IM, data);

	data = snapdata[FRZ_OFFSET + 37];
	m_maincpu->set_state_int(Z80_IFF1, BIT(data, 2));
	m_maincpu->set_state_int(Z80_IFF2, BIT(data, 2));

	intr = BIT(data, 2) ? CLEAR_LINE : ASSERT_LINE;
	m_maincpu->set_input_line(INPUT_LINE_IRQ0, intr);
	m_maincpu->set_input_line(INPUT_LINE_HALT, CLEAR_LINE);

	// Memory dump
	addr = 0;
	static const uint8_t banks[] = { 5, 2, 0, 1, 3, 4, 6, 7 };
	logerror("Loading %05X bytes of RAM at %04X\n", 8*SPECTRUM_BANK, BASE_RAM);
	for (i = 0; i < 8; i++)
	{
		switch (banks[i])
		{
			case 5: addr = SPECTRUM_BANK;
					break;
			case 2: addr = 2*SPECTRUM_BANK;
					break;
			case 0:
			case 1:
			case 3:
			case 4:
			case 6:
			case 7: addr = 3*SPECTRUM_BANK;
					m_port_7ffd_data &= 0xf8;
					m_port_7ffd_data += banks[i];
					update_paging();
					break;
		};
		logerror("Loading bank %d from offset:%05X\n", banks[i], FRZ_HDR + i*SPECTRUM_BANK);
		for (j = 0; j < SPECTRUM_BANK; j++)
			space.write_byte(j + addr, snapdata[j + FRZ_HDR + i*SPECTRUM_BANK]);
	}
	m_port_7ffd_data = snapdata[FRZ_OFFSET +  1];
	logerror("Port 7FFD:%02X\n", m_port_7ffd_data);
	logerror("Paging bank:%d\n", m_port_7ffd_data & 0x07);
	update_paging();

	// Set border color
	data = (space.read_byte(0x5c48) >> 3) & 0x07; // Get the current border color from BORDCR system variable.
	m_port_fe_data = (m_port_fe_data & 0xf8) | data;
	border_update(data);
	logerror("Border color:%02X\n", data);

	//logerror("Snapshot loaded.\nExecution resuming at bank:%d %s\n", m_port_7ffd_data & 0x07, m_maincpu->state_string(Z80_PC).c_str());
}

void spectrum_state::z80_decompress_block(address_space &space, uint8_t *source, uint16_t dest, uint16_t size)
{
	uint8_t ch;
	int i;

	do
	{
		/* get byte */
		ch = source[0];

		/* either start 0f 0x0ed, 0x0ed, xx yy or single 0x0ed */
		if (ch == 0x0ed)
		{
			if (source[1] == 0x0ed)
			{
				/* 0x0ed, 0x0ed, xx yy - repetition */
				uint8_t count;
				uint8_t data;

				count = source[2];

				if (count == 0)
					return;

				data = source[3];

				source += 4;

				if (count > size)
					count = size;

				size -= count;

				for (i = 0; i < count; i++)
				{
					space.write_byte(dest, data);
					dest++;
				}
			}
			else
			{
				/* single 0x0ed */
				space.write_byte(dest, ch);
				dest++;
				source++;
				size--;
			}
		}
		else
		{
			/* not 0x0ed */
			space.write_byte(dest, ch);
			dest++;
			source++;
			size--;
		}
	}
	while (size > 0);
}

static SPECTRUM_Z80_SNAPSHOT_TYPE spectrum_identify_z80 (uint8_t *snapdata, uint32_t snapsize)
{
	uint8_t lo, hi, data;

	if (snapsize < 30)
		return SPECTRUM_Z80_SNAPSHOT_INVALID;   /* Invalid file */

	lo = snapdata[6] & 0x0ff;
	hi = snapdata[7] & 0x0ff;
	if (hi || lo)
		return SPECTRUM_Z80_SNAPSHOT_48K_OLD;   /* V1.45 - 48K only */

	lo = snapdata[30] & 0x0ff;
	hi = snapdata[31] & 0x0ff;
	data = snapdata[34] & 0x0ff;            /* Hardware mode */

	if ((hi == 0) && (lo == 23))
	{                       /* V2.01 */                         /* V2.01 format */
		switch (data)
		{
			case 0:
			case 1: return SPECTRUM_Z80_SNAPSHOT_48K;
			case 2: return SPECTRUM_Z80_SNAPSHOT_SAMRAM;
			case 3:
			case 4: return SPECTRUM_Z80_SNAPSHOT_128K;
			case 128: return SPECTRUM_Z80_SNAPSHOT_TS2068;
		}
	}

	if ((hi == 0) && (lo == 54))
	{                       /* V3.0x */                         /* V2.01 format */
		switch (data)
		{
			case 0:
			case 1:
			case 3: return SPECTRUM_Z80_SNAPSHOT_48K;
			case 2: return SPECTRUM_Z80_SNAPSHOT_SAMRAM;
			case 4:
			case 5:
			case 6: return SPECTRUM_Z80_SNAPSHOT_128K;
			case 128: return SPECTRUM_Z80_SNAPSHOT_TS2068;
		}
	}

	return SPECTRUM_Z80_SNAPSHOT_INVALID;
}

// supports 48k & 128k .Z80 files
void spectrum_state::setup_z80(uint8_t *snapdata, uint32_t snapsize)
{
	int i;
	uint8_t lo, hi, data;
	SPECTRUM_Z80_SNAPSHOT_TYPE z80_type;
	address_space &space = m_maincpu->space(AS_PROGRAM);

	z80_type = spectrum_identify_z80(snapdata, snapsize);

	switch (z80_type)
	{
		case SPECTRUM_Z80_SNAPSHOT_INVALID:
				logerror("Invalid .Z80 file\n");
				return;
		case SPECTRUM_Z80_SNAPSHOT_48K_OLD:
		case SPECTRUM_Z80_SNAPSHOT_48K:
				logerror("48K .Z80 file\n");
				if (!strcmp(machine().system().name,"ts2068"))
					logerror("48K .Z80 file in TS2068\n");
				break;
		case SPECTRUM_Z80_SNAPSHOT_128K:
				logerror("128K .Z80 file\n");
				if (m_port_7ffd_data == -1)
				{
					logerror("Not a 48K .Z80 file\n");
					return;
				}
				if (!strcmp(machine().system().name,"ts2068"))
				{
					logerror("Not a TS2068 .Z80 file\n");
					return;
				}
				break;
		case SPECTRUM_Z80_SNAPSHOT_TS2068:
				logerror("TS2068 .Z80 file\n");
				if (strcmp(machine().system().name,"ts2068"))
					logerror("Not a TS2068 machine\n");
				break;
		case SPECTRUM_Z80_SNAPSHOT_SAMRAM:
				logerror("Hardware not supported - .Z80 file\n");
				return;
	}

	// AF
	hi = snapdata[0] & 0x0ff;
	lo = snapdata[1] & 0x0ff;
	m_maincpu->set_state_int(Z80_AF, (hi << 8) | lo);
	// BC
	lo = snapdata[2] & 0x0ff;
	hi = snapdata[3] & 0x0ff;
	m_maincpu->set_state_int(Z80_BC, (hi << 8) | lo);
	// HL
	lo = snapdata[4] & 0x0ff;
	hi = snapdata[5] & 0x0ff;
	m_maincpu->set_state_int(Z80_HL, (hi << 8) | lo);

	// SP
	lo = snapdata[8] & 0x0ff;
	hi = snapdata[9] & 0x0ff;
	m_maincpu->set_state_int(Z80_SP, (hi << 8) | lo);

	// I
	m_maincpu->set_state_int(Z80_I, (snapdata[10] & 0x0ff));

	// R
	data = (snapdata[11] & 0x07f) | ((snapdata[12] & 0x01) << 7);
	m_maincpu->set_state_int(Z80_R, data);

	// Set border color
	m_port_fe_data = (m_port_fe_data & 0xf8) | ((snapdata[12] & 0x0e) >> 1);
	border_update((snapdata[12] & 0x0e) >> 1);

	lo = snapdata[13] & 0x0ff;
	hi = snapdata[14] & 0x0ff;
	m_maincpu->set_state_int(Z80_DE, (hi << 8) | lo);

	lo = snapdata[15] & 0x0ff;
	hi = snapdata[16] & 0x0ff;
	m_maincpu->set_state_int(Z80_BC2, (hi << 8) | lo);

	lo = snapdata[17] & 0x0ff;
	hi = snapdata[18] & 0x0ff;
	m_maincpu->set_state_int(Z80_DE2, (hi << 8) | lo);

	lo = snapdata[19] & 0x0ff;
	hi = snapdata[20] & 0x0ff;
	m_maincpu->set_state_int(Z80_HL2, (hi << 8) | lo);

	hi = snapdata[21] & 0x0ff;
	lo = snapdata[22] & 0x0ff;
	m_maincpu->set_state_int(Z80_AF2, (hi << 8) | lo);

	lo = snapdata[23] & 0x0ff;
	hi = snapdata[24] & 0x0ff;
	m_maincpu->set_state_int(Z80_IY, (hi << 8) | lo);

	lo = snapdata[25] & 0x0ff;
	hi = snapdata[26] & 0x0ff;
	m_maincpu->set_state_int(Z80_IX, (hi << 8) | lo);

	// Interrupt Flip/Flop
	if (snapdata[27] == 0)
	{
		m_maincpu->set_state_int(Z80_IFF1, (uint64_t)0);
		// m_maincpu->set_state_int(Z80_IRQ_STATE, 0);
	}
	else
	{
		m_maincpu->set_state_int(Z80_IFF1, 1);
		// m_maincpu->set_state_int(Z80_IRQ_STATE, 1);
	}

	m_maincpu->set_input_line(INPUT_LINE_IRQ0, CLEAR_LINE);
//  m_maincpu->set_input_line(INPUT_LINE_NMI, data);
	m_maincpu->set_input_line(INPUT_LINE_HALT, CLEAR_LINE);

	// IFF2
	if (snapdata[28] != 0)
	{
		data = 1;
	}
	else
	{
		data = 0;
	}
	m_maincpu->set_state_int(Z80_IFF2, data);

	// Interrupt Mode
	m_maincpu->set_state_int(Z80_IM, (snapdata[29] & 0x03));

	if (z80_type == SPECTRUM_Z80_SNAPSHOT_48K_OLD)
	{
		lo = snapdata[6] & 0x0ff;
		hi = snapdata[7] & 0x0ff;
		m_maincpu->set_state_int(Z80_PC, (hi << 8) | lo);

		page_basicrom();

		if ((snapdata[12] & 0x020) == 0)
		{
			logerror("Not compressed\n");   // not compressed
			for (i = 0; i < 49152; i++)
				space.write_byte(i + 16384, snapdata[30 + i]);
		}
		else
		{
			logerror("Compressed\n");   // compressed
			z80_decompress_block(space, snapdata + 30, 16384, 49152);
		}
	}
	else
	{
		uint8_t *pSource;
		int header_size;

		header_size = 30 + 2 + ((snapdata[30] & 0x0ff) | ((snapdata[31] & 0x0ff) << 8));

		lo = snapdata[32] & 0x0ff;
		hi = snapdata[33] & 0x0ff;
		m_maincpu->set_state_int(Z80_PC, (hi << 8) | lo);

		if ((z80_type == SPECTRUM_Z80_SNAPSHOT_128K) || ((z80_type == SPECTRUM_Z80_SNAPSHOT_TS2068) && !strcmp(machine().system().name,"ts2068")))
		{
			ay8910_device *ay8912 = subdevice<ay8910_device>("ay8912");

			// Only set up sound registers for 128K machine or TS2068!
			for (i = 0; i < 16; i++)
			{
				ay8912->address_w(i);
				ay8912->data_w(snapdata[39 + i]);
			}
			ay8912->address_w(snapdata[38]);
		}

		pSource = snapdata + header_size;

		if (z80_type == SPECTRUM_Z80_SNAPSHOT_48K)
			// Ensure 48K Basic ROM is used
			page_basicrom();

		do
		{
			unsigned short length;
			uint8_t page;
			int Dest = 0;

			length = (pSource[0] & 0x0ff) | ((pSource[1] & 0x0ff) << 8);
			page = pSource[2];

			if (z80_type == SPECTRUM_Z80_SNAPSHOT_48K || z80_type == SPECTRUM_Z80_SNAPSHOT_TS2068)
			{
				switch (page)
				{
					case 4: Dest = 0x08000; break;
					case 5: Dest = 0x0c000; break;
					case 8: Dest = 0x04000; break;
					default: Dest = 0; break;
				}
			}
			else
			{
				// 3 = bank 0, 4 = bank 1 ... 10 = bank 7
				if ((page >= 3) && (page <= 10))
				{
					// Page the appropriate bank into 0xc000 - 0xfff
					m_port_7ffd_data = page - 3;
					update_paging();
					Dest = 0x0c000;
				}
				else
					// Other values correspond to ROM pages
					Dest = 0x0;
			}

			if (Dest != 0)
			{
				if (length == 0x0ffff)
				{
					// block is uncompressed
					logerror("Not compressed\n");

					// not compressed
					for (i = 0; i < 16384; i++)
						space.write_byte(i + Dest, pSource[i]);
				}
				else
				{
					logerror("Compressed\n");

					// block is compressed
					z80_decompress_block(space, &pSource[3], Dest, 16384);
				}
			}

			// go to next block
			pSource += (3 + length);
		}
		while ((pSource - snapdata) < snapsize);

		if ((m_port_7ffd_data != -1) && (z80_type != SPECTRUM_Z80_SNAPSHOT_48K))
		{
			// Set up paging
			m_port_7ffd_data = (snapdata[35] & 0x0ff);
			update_paging();
		}
		if ((z80_type == SPECTRUM_Z80_SNAPSHOT_48K) && !strcmp(machine().system().name,"ts2068"))
		{
			m_port_f4_data = 0x03;
			m_port_ff_data = 0x00;
			ts2068_update_memory();
		}
		if (z80_type == SPECTRUM_Z80_SNAPSHOT_TS2068 && !strcmp(machine().system().name,"ts2068"))
		{
			m_port_f4_data = snapdata[35];
			m_port_ff_data = snapdata[36];
			ts2068_update_memory();
		}
	}
}

QUICKLOAD_LOAD_MEMBER(spectrum_state::quickload_cb)
{
	std::vector<uint8_t> quickload_data(quickload_size);

	image.fread(&quickload_data[0], quickload_size);

	if (!core_stricmp(file_type, "scr"))
	{
		if ((quickload_size != SCR_SIZE) && (quickload_size != SCR_BITMAP))
		{
			logerror("Invalid .SCR file size.\n");
			goto error;
		}
		setup_scr(&quickload_data[0], quickload_size);
	}
	else if (!core_stricmp(file_type, "raw"))
	{
		if (quickload_size != RAW_SIZE)
		{
			logerror("Invalid .RAW file size.\n");
			goto error;
		}
		setup_raw(&quickload_data[0], quickload_size);
	}

	return image_init_result::PASS;

error:
	return image_init_result::FAIL;
}

/*******************************************************************
 *
 *      Load a .SCR file.
 *
 *      .SCR files are just a binary dump of the ZX Spectrum's
 *      display file, which is 256x192 pixels large.
 *
 *      These file are created using the Sinclair BASIC command
 *
 *      SAVE "filename" SCREEN$
 *
 *      where the keyword "SCREEN$" is a shortcut for "CODE 16384,6912"
 *      i.e. the loading address and the length of the data.
 *
 *      The format is organized as follows:
 *
 *      Offset   Size    Description
 *      -------- ------- -------------------------------------------------
 *      0        6144    Screen bitmap
 *      6144     768     Screen attributes
 *
 *      Some utilities and emulators support a B&W version of this
 *      format, which stores only the bitmap. These files can't be
 *      produced using the SAVE SCREEN$ command - rather they're
 *      created by the command
 *
 *      SAVE "filename" CODE 16384,6144
 *
 *******************************************************************/
void spectrum_state::setup_scr(uint8_t *quickdata, uint32_t quicksize)
{
	int i;
	address_space &space = m_maincpu->space(AS_PROGRAM);

	for (i = 0; i < quicksize; i++)
		space.write_byte(i + BASE_RAM, quickdata[i]);

	log_quickload(quicksize == SCR_SIZE ? "SCREEN$" : "SCREEN$ (Mono)", BASE_RAM, quicksize, 0, EXEC_NA);
}

/*******************************************************************
 *
 *      Load a .RAW file.
 *
 *      .RAW files are a binary dump of the ZX Spectrum RAM
 *      and are created using the Sinclair BASIC command
 *
 *      SAVE *"b" CODE 16384,49152
 *
 *      which copies the whole RAM to the Interface 1's RS232 interface.
 *      The resulting file is always 49161 bytes long.
 *
 *      The format is organized as follows:
 *
 *      Offset  Size    Description (all registers stored with LSB first)
 *      ------- ------- -------------------------------------------------
 *      0       1       0x03 (BYTES file type)
 *      1       2       Image size
 *      3       2       Image start address
 *      5       4       0xFF
 *
 *      Since the size and the start address are encoded in the header,
 *      it would be possible to create .RAW images of any part of the RAM.
 *      However, no image of such type has ever surfaced.
 *
 *******************************************************************/
void spectrum_state::setup_raw(uint8_t *quickdata, uint32_t quicksize)
{
	int i;
	uint8_t data;
	uint16_t start, len;
	address_space &space = m_maincpu->space(AS_PROGRAM);

	start = (quickdata[RAW_OFFSET + 4] << 8) | quickdata[RAW_OFFSET + 3];
	len   = (quickdata[RAW_OFFSET + 2] << 8) | quickdata[RAW_OFFSET + 1];

	for (i = 0; i < len; i++)
		space.write_byte(i + start, quickdata[i + RAW_HDR]);

	// Set border color
	data = (space.read_byte(0x5c48) >> 3) & 0x07; // Get the current border color from BORDCR system variable.
	m_port_fe_data = (m_port_fe_data & 0xf8) | data;
	border_update(data);
	logerror("Border color:%02X\n", data);

	log_quickload("BYTES", start, len, 0, EXEC_NA);
}
pan>/2] = 0x0060;//adda.w $60,A0 jm_mcu_code[0x0014/2] = 0x92fc; jm_mcu_code[0x0016/2] = 0x0200;//suba.w $200,A1 jm_mcu_code[0x0018/2] = 0x32d8;//move.w (A0)+,(A1)+ jm_mcu_code[0x001a/2] = 0x51c9; jm_mcu_code[0x001c/2] = 0xfffc;//dbra D1,f00ca jm_mcu_code[0x001e/2] = 0x4e75;//rts /******************************************************* 2nd M68k code uploaded by the MCU (tile upload) *******************************************************/ jm_shared_ram[0x03ca/2] = 0x4ef9; jm_shared_ram[0x03cc/2] = 0x0010; jm_shared_ram[0x03ce/2] = 0x0800;//jmp $100800 jm_mcu_code[0x0800/2] = 0x32da; jm_mcu_code[0x0802/2] = 0x51c8; jm_mcu_code[0x0804/2] = 0xfffc; jm_mcu_code[0x0806/2] = 0x4e75; /******************************************************* 3rd M68k code uploaded by the MCU (palette upload) *******************************************************/ jm_shared_ram[0x03c0/2] = 0x4ef9; jm_shared_ram[0x03c2/2] = 0x0010; jm_shared_ram[0x03c4/2] = 0x1000;//jmp $101000 jm_mcu_code[0x1000/2] = 0x4e71;//0x92fc; jm_mcu_code[0x1002/2] = 0x4e71;//0x0200;//suba.w $200,A1 jm_mcu_code[0x1004/2] = 0xb3fc; jm_mcu_code[0x1006/2] = 0x0008; jm_mcu_code[0x1008/2] = 0x8200; jm_mcu_code[0x100a/2] = 0x673c;//beq :x//0x6d00; jm_mcu_code[0x100c/2] = 0x4e71;//nop//0x003c; jm_mcu_code[0x100e/2] = 0x33c2; jm_mcu_code[0x1010/2] = 0x0010; jm_mcu_code[0x1012/2] = 0x17fe; //move.w D2,$1017fe jm_mcu_code[0x1014/2] = 0x33c1; jm_mcu_code[0x1016/2] = 0x0010; jm_mcu_code[0x1018/2] = 0x17fc; //move.w D1,$1017fc jm_mcu_code[0x101a/2] = 0x720f; jm_mcu_code[0x101c/2] = 0x740f; //moveq $07,D2 jm_mcu_code[0x101e/2] = 0x23c8; jm_mcu_code[0x1020/2] = 0x0010; jm_mcu_code[0x1022/2] = 0x17f0; jm_mcu_code[0x1024/2] = 0x2050; //movea (A0),A0 jm_mcu_code[0x1026/2] = 0x32d8; jm_mcu_code[0x1028/2] = 0x51ca; jm_mcu_code[0x102a/2] = 0xfffc; jm_mcu_code[0x102c/2] = 0x2079; jm_mcu_code[0x102e/2] = 0x0010; jm_mcu_code[0x1030/2] = 0x17f0; jm_mcu_code[0x1032/2] = 0xd0fc; jm_mcu_code[0x1034/2] = 0x0004;//adda.w $4,A0 jm_mcu_code[0x1036/2] = 0x51c9; jm_mcu_code[0x1038/2] = 0xffe4; jm_mcu_code[0x103a/2] = 0x3439; jm_mcu_code[0x103c/2] = 0x0010; jm_mcu_code[0x103e/2] = 0x17fe; jm_mcu_code[0x1040/2] = 0x3239; jm_mcu_code[0x1042/2] = 0x0010; jm_mcu_code[0x1044/2] = 0x17fc; jm_mcu_code[0x1046/2] = 0x4e75; jm_mcu_code[0x1048/2] = 0x23CD; jm_mcu_code[0x104a/2] = 0x0010; jm_mcu_code[0x104c/2] = 0x17C0; jm_mcu_code[0x104e/2] = 0x2A7C; jm_mcu_code[0x1050/2] = 0x0010; jm_mcu_code[0x1052/2] = 0x17E0; jm_mcu_code[0x1054/2] = 0x33C6; jm_mcu_code[0x1056/2] = 0x0010; jm_mcu_code[0x1058/2] = 0x17B0; jm_mcu_code[0x105a/2] = 0x3C39; jm_mcu_code[0x105c/2] = 0x000F; jm_mcu_code[0x105e/2] = 0x201A; jm_mcu_code[0x1060/2] = 0x3A86; jm_mcu_code[0x1062/2] = 0x6700; jm_mcu_code[0x1064/2] = 0x0074; jm_mcu_code[0x1066/2] = 0x33C2; jm_mcu_code[0x1068/2] = 0x0010; jm_mcu_code[0x106a/2] = 0x17FE; jm_mcu_code[0x106c/2] = 0x33C1; jm_mcu_code[0x106e/2] = 0x0010; jm_mcu_code[0x1070/2] = 0x17FC; jm_mcu_code[0x1072/2] = 0x23C8; jm_mcu_code[0x1074/2] = 0x0010; jm_mcu_code[0x1076/2] = 0x17F0; jm_mcu_code[0x1078/2] = 0x23C9; jm_mcu_code[0x107a/2] = 0x0010; jm_mcu_code[0x107c/2] = 0x17D0; jm_mcu_code[0x107e/2] = 0x41F9; jm_mcu_code[0x1080/2] = 0x0002; jm_mcu_code[0x1082/2] = 0xA2C0; jm_mcu_code[0x1084/2] = 0x3c15; jm_mcu_code[0x1086/2] = 0xd1fc; jm_mcu_code[0x1088/2] = 0x0000; jm_mcu_code[0x108a/2] = 0x0040; jm_mcu_code[0x108c/2] = 0x5346; jm_mcu_code[0x108e/2] = 0x6704; jm_mcu_code[0x1090/2] = 0x4e71; jm_mcu_code[0x1092/2] = 0x60F2; jm_mcu_code[0x1094/2] = 0x23c8; jm_mcu_code[0x1096/2] = 0x0010; jm_mcu_code[0x1098/2] = 0x17a0;//store here the A0 reg,1017a0 jm_mcu_code[0x109a/2] = 0xd3fc; jm_mcu_code[0x109c/2] = 0x0000; jm_mcu_code[0x109e/2] = 0x0200; jm_mcu_code[0x10a0/2] = 0x720F; jm_mcu_code[0x10a2/2] = 0x740F; jm_mcu_code[0x10a4/2] = 0x2050; jm_mcu_code[0x10a6/2] = 0x32D8; jm_mcu_code[0x10a8/2] = 0x51CA; jm_mcu_code[0x10aa/2] = 0xFFFC; jm_mcu_code[0x10ac/2] = 0x2079;//1017a0,not 1017f0 jm_mcu_code[0x10ae/2] = 0x0010; jm_mcu_code[0x10b0/2] = 0x17a0; jm_mcu_code[0x10b2/2] = 0xD0FC; jm_mcu_code[0x10b4/2] = 0x0004; jm_mcu_code[0x10b6/2] = 0x23c8; jm_mcu_code[0x10b8/2] = 0x0010; jm_mcu_code[0x10ba/2] = 0x17a0; jm_mcu_code[0x10bc/2] = 0x51C9; jm_mcu_code[0x10be/2] = 0xffe4; jm_mcu_code[0x10c0/2] = 0x3439; jm_mcu_code[0x10c2/2] = 0x0010; jm_mcu_code[0x10c4/2] = 0x17FE; jm_mcu_code[0x10c6/2] = 0x3239; jm_mcu_code[0x10c8/2] = 0x0010; jm_mcu_code[0x10ca/2] = 0x17FC; jm_mcu_code[0x10cc/2] = 0x2079; jm_mcu_code[0x10ce/2] = 0x0010; jm_mcu_code[0x10d0/2] = 0x17F0; jm_mcu_code[0x10d2/2] = 0x2279; jm_mcu_code[0x10d4/2] = 0x0010; jm_mcu_code[0x10d6/2] = 0x17D0; jm_mcu_code[0x10d8/2] = 0x2A79; jm_mcu_code[0x10da/2] = 0x0010; jm_mcu_code[0x10dc/2] = 0x17c0; jm_mcu_code[0x10de/2] = 0x3C39; jm_mcu_code[0x10e0/2] = 0x0010; jm_mcu_code[0x10e2/2] = 0x17B0; jm_mcu_code[0x10e4/2] = 0x6000; jm_mcu_code[0x10e6/2] = 0xFF26; } } READ16_MEMBER(jalmah_state::daireika_mcu_r) { static const int resp[] = { 0x99, 0xd8, 0x00, 0x2a, 0x6a, 0x00, 0x9c, 0xd8, 0x00, 0x2f, 0x6f, 0x00, 0x22, 0x62, 0x00, 0x25, 0x65, 0x00, 0x23, 0x63, 0x00, 0x3e, 0x7e, 0x00, 0x35, 0x75, 0x00, 0x21, 0x61, 0x00 }; int res; res = resp[m_respcount++]; if (m_respcount >= sizeof(resp)/sizeof(resp[0])) m_respcount = 0; // logerror("%04x: mcu_r %02x\n",cpu_get_pc(&space.device()),res); return res; } /* data value is REQ under mjzoomin video test menu.It is related to the MCU? */ static const UINT16 dai_mcu_code[0x11] = { 0x33c5, 0x0010, 0x07fe, 0x3a39,0x000f,0x000c,0xda86,0x0245, 0x003f, 0x33c5, 0x000f, 0x000c,0x3a39,0x0010,0x07fe,0x4e75 }; WRITE16_MEMBER(jalmah_state::daireika_mcu_w) { UINT16 *jm_shared_ram = m_jm_shared_ram; UINT16 *jm_mcu_code = m_jm_mcu_code; UINT16 i; if(ACCESSING_BITS_0_7 && data) { /******************************************************* 1st M68k code uploaded by the MCU. random number generator (guess) *******************************************************/ jm_shared_ram[0x0140/2] = 0x4ef9; jm_shared_ram[0x0142/2] = 0x0010; jm_shared_ram[0x0144/2] = 0x0800; /* 33c5 0010 07fe move.w D5, $1007fe.l 3A39 000F 000C move.w $f000c, D5 da86 add.w D6,D5 0245 003f and.w $3f,D5 33C5 000F 000C move.w D5, $f000c.l 3a39 0010 07fe move.w $1007fe, D5 4e75 rts */ for(i=0;i<0x11;i++) jm_mcu_code[(0x0800/2)+i] = dai_mcu_code[i]; /******************************************************* 2nd M68k code uploaded by the MCU. *******************************************************/ jm_shared_ram[0x0020/2] = 0x4ef9; jm_shared_ram[0x0022/2] = 0x0010; jm_shared_ram[0x0024/2] = 0x2000;//jmp $102000 jm_mcu_code[0x2000/2] = 0x0040; jm_mcu_code[0x2002/2] = 0x0080;//ori $80,D0 jm_mcu_code[0x2004/2] = 0x33c0; jm_mcu_code[0x2006/2] = 0x0008; jm_mcu_code[0x2008/2] = 0x0040; jm_mcu_code[0x200a/2] = 0x6100; jm_mcu_code[0x200c/2] = 0x000c; jm_mcu_code[0x200e/2] = 0x33fc; jm_mcu_code[0x2010/2] = 0x0010; jm_mcu_code[0x2012/2] = 0x0008; jm_mcu_code[0x2014/2] = 0x0040; jm_mcu_code[0x2016/2] = 0x4e75; jm_mcu_code[0x2018/2] = 0x3239; jm_mcu_code[0x201a/2] = 0x0008; jm_mcu_code[0x201c/2] = 0x0040; jm_mcu_code[0x201e/2] = 0x0241; jm_mcu_code[0x2020/2] = 0x0001; jm_mcu_code[0x2022/2] = 0x66f4; jm_mcu_code[0x2024/2] = 0x4e75; /******************************************************* 3rd M68k code uploaded by the MCU. see mjzoomin_mcu_w *******************************************************/ jm_shared_ram[0x00c6/2] = 0x6000; jm_shared_ram[0x00c8/2] = 0x0008;//bra +$8,needed because we have only two bytes here //and we need three... jm_shared_ram[0x00d0/2] = 0x4ef9; jm_shared_ram[0x00d2/2] = 0x0010; jm_shared_ram[0x00d4/2] = 0x0000;//jmp $100000 //jm_shared_ram[0x00cc/2] = 0x4e75;//rts //needed? we can use jmp instead of jsr... jm_mcu_code[0x0000/2] = 0x2050;//movea.l (A0),A0 jm_mcu_code[0x0002/2] = 0x32d8;//move.w (A0)+,(A1)+ jm_mcu_code[0x0004/2] = 0x51c9; jm_mcu_code[0x0006/2] = 0xfffc;//dbra D1,f00ca jm_mcu_code[0x0008/2] = 0x4e75;//rts /******************************************************* 4th M68k code uploaded by the MCU They seem video code cleaning functions *******************************************************/ //108800 jm_shared_ram[0x0100/2] = 0x4ef9; jm_shared_ram[0x0102/2] = 0x0010; jm_shared_ram[0x0104/2] = 0x8800; jm_mcu_code[0x8800/2] = 0x4df9; jm_mcu_code[0x8802/2] = 0x0009; jm_mcu_code[0x8804/2] = 0x0000; //lea.w #90000,A6 jm_mcu_code[0x8806/2] = 0x323c; jm_mcu_code[0x8808/2] = 0x1fff; //move.w #$1fff,D1 jm_mcu_code[0x880a/2] = 0x3cbc; jm_mcu_code[0x880c/2] = 0x00ff; //move.w #$0000,(A6) jm_mcu_code[0x880e/2] = 0xdcfc; jm_mcu_code[0x8810/2] = 0x0002; //adda.w #$0002,A6 jm_mcu_code[0x8812/2] = 0x51c9; jm_mcu_code[0x8814/2] = 0xfff6; //dbra D1 jm_mcu_code[0x8816/2] = 0x4df9; jm_mcu_code[0x8818/2] = 0x0000; jm_mcu_code[0x881a/2] = 0x0000; //lea.w #0,A6 jm_mcu_code[0x881c/2] = 0x323c; jm_mcu_code[0x881e/2] = 0x0000; //move.w #$0,D1 jm_mcu_code[0x8820/2] = 0x4e75; //rts //108880 jm_shared_ram[0x0108/2] = 0x4ef9; jm_shared_ram[0x010a/2] = 0x0010; jm_shared_ram[0x010c/2] = 0x8880; jm_mcu_code[0x8880/2] = 0x4df9; jm_mcu_code[0x8882/2] = 0x0009; jm_mcu_code[0x8884/2] = 0x4000; //lea.w #94000,A6 jm_mcu_code[0x8886/2] = 0x323c; jm_mcu_code[0x8888/2] = 0x1fff; //move.w #$1fff,D1 jm_mcu_code[0x888a/2] = 0x3cbc; jm_mcu_code[0x888c/2] = 0x00ff; //move.w #$0000,(A6) jm_mcu_code[0x888e/2] = 0xdcfc; jm_mcu_code[0x8890/2] = 0x0002; //adda.w #$0002,A6 jm_mcu_code[0x8892/2] = 0x51c9; jm_mcu_code[0x8894/2] = 0xfff6; //dbra D1 jm_mcu_code[0x8896/2] = 0x4df9; jm_mcu_code[0x8898/2] = 0x0000; jm_mcu_code[0x889a/2] = 0x0000; //lea.w #0,A6 jm_mcu_code[0x889c/2] = 0x323c; jm_mcu_code[0x889e/2] = 0x0000; //move.w #$0,D1 jm_mcu_code[0x88a0/2] = 0x4e75; //rts //108900 jm_shared_ram[0x0110/2] = 0x4ef9; jm_shared_ram[0x0112/2] = 0x0010; jm_shared_ram[0x0114/2] = 0x8900; jm_mcu_code[0x8900/2] = 0x4df9; jm_mcu_code[0x8902/2] = 0x0009; jm_mcu_code[0x8904/2] = 0x8000; //lea.w #98000,A6 jm_mcu_code[0x8906/2] = 0x323c; jm_mcu_code[0x8908/2] = 0x1fff; //move.w #$1fff,D1 jm_mcu_code[0x890a/2] = 0x3cbc; jm_mcu_code[0x890c/2] = 0xf0ff; //move.w #$f0ff,(A6) jm_mcu_code[0x890e/2] = 0xdcfc; jm_mcu_code[0x8910/2] = 0x0002; //adda.w #$0002,A6 jm_mcu_code[0x8912/2] = 0x51c9; jm_mcu_code[0x8914/2] = 0xfff6; //dbra D1 jm_mcu_code[0x8916/2] = 0x4df9; jm_mcu_code[0x8918/2] = 0x0000; jm_mcu_code[0x891a/2] = 0x0000; //lea.w #0,A6 jm_mcu_code[0x891c/2] = 0x323c; jm_mcu_code[0x891e/2] = 0x0000; //move.w #$0,D1 jm_mcu_code[0x8920/2] = 0x4e75; //rts /*TX function?*/ jm_shared_ram[0x0126/2] = 0x4ef9; jm_shared_ram[0x0128/2] = 0x0010; jm_shared_ram[0x012a/2] = 0x8980; //m_pri $f0590 jm_mcu_code[0x8980/2] = 0x33fc; jm_mcu_code[0x8982/2] = 0x0006; jm_mcu_code[0x8984/2] = 0x000f; jm_mcu_code[0x8986/2] = 0x0590; //move.w #$6,$f0590 (m_pri n) jm_mcu_code[0x8988/2] = 0x4df9; jm_mcu_code[0x898a/2] = 0x0009; jm_mcu_code[0x898c/2] = 0xc000; //lea.w #9c000,A6 jm_mcu_code[0x898e/2] = 0x323c; jm_mcu_code[0x8990/2] = 0x1fff; //move.w #$1fff,D1 jm_mcu_code[0x8992/2] = 0x3cbc; jm_mcu_code[0x8994/2] = 0x0020; //move.w #$0020,(A6) jm_mcu_code[0x8996/2] = 0xdcfc; jm_mcu_code[0x8998/2] = 0x0002; //adda.w #$0002,A6 jm_mcu_code[0x899a/2] = 0x51c9; jm_mcu_code[0x899c/2] = 0xfff6; //dbra D1 jm_mcu_code[0x899e/2] = 0x4df9; jm_mcu_code[0x89a0/2] = 0x0000; jm_mcu_code[0x89a2/2] = 0x0000; //lea.w #0,A6 jm_mcu_code[0x89a4/2] = 0x323c; jm_mcu_code[0x89a6/2] = 0x0000; //move.w #$0,D1 jm_mcu_code[0x89a8/2] = 0x4e75; //rts /* jm_shared_ram[0x0100/2] = 0x4ef9; jm_shared_ram[0x0102/2] = 0x0010; jm_shared_ram[0x0104/2] = 0x1000;//jmp $101000 //jm_shared_ram[0x00c6/2] = 0x4e75;//rts jm_mcu_code[0x1000/2] = 0x33c2; jm_mcu_code[0x1002/2] = 0x0010; jm_mcu_code[0x1004/2] = 0x17fe; //move.w D2,$1017fe jm_mcu_code[0x1006/2] = 0x23c8; jm_mcu_code[0x1008/2] = 0x0010; jm_mcu_code[0x100a/2] = 0x17f0; jm_mcu_code[0x100c/2] = 0x2050; //movea (A0),A0 jm_mcu_code[0x100e/2] = 0x22d8; jm_mcu_code[0x1010/2] = 0x51ca; jm_mcu_code[0x1012/2] = 0xfffc; jm_mcu_code[0x1014/2] = 0x3439; jm_mcu_code[0x1016/2] = 0x0010; jm_mcu_code[0x1018/2] = 0x17fe; jm_mcu_code[0x101a/2] = 0x2079; jm_mcu_code[0x101c/2] = 0x0010; jm_mcu_code[0x101e/2] = 0x17f0; jm_mcu_code[0x1020/2] = 0xd0fc; jm_mcu_code[0x1022/2] = 0x0004;//adda.w $4,A0 jm_mcu_code[0x1024/2] = 0x4e75;*/ /******************************************************* 5th M68k code uploaded by the MCU (palette upload) *******************************************************/ jm_shared_ram[0x00c0/2] = 0x4ef9; jm_shared_ram[0x00c2/2] = 0x0010; jm_shared_ram[0x00c4/2] = 0x1000;//jmp $101000 //jm_shared_ram[0x00c6/2] = 0x4e75;//rts jm_mcu_code[0x1000/2] = 0x33c2; jm_mcu_code[0x1002/2] = 0x0010; jm_mcu_code[0x1004/2] = 0x17fe; //move.w D2,$1017fe jm_mcu_code[0x1006/2] = 0x33c1; jm_mcu_code[0x1008/2] = 0x0010; jm_mcu_code[0x100a/2] = 0x17fc; //move.w D1,$1017fc jm_mcu_code[0x100c/2] = 0x720f; jm_mcu_code[0x100e/2] = 0x740f; //moveq $0f,D2 jm_mcu_code[0x1010/2] = 0x23c8; jm_mcu_code[0x1012/2] = 0x0010; jm_mcu_code[0x1014/2] = 0x17f0; jm_mcu_code[0x1016/2] = 0x2050; //movea (A0),A0 jm_mcu_code[0x1018/2] = 0x32d8; jm_mcu_code[0x101a/2] = 0x51ca; jm_mcu_code[0x101c/2] = 0xfffc; jm_mcu_code[0x101e/2] = 0x2079; jm_mcu_code[0x1020/2] = 0x0010; jm_mcu_code[0x1022/2] = 0x17f0; jm_mcu_code[0x1024/2] = 0xd0fc; jm_mcu_code[0x1026/2] = 0x0004;//adda.w $4,A0 jm_mcu_code[0x1028/2] = 0x51c9; jm_mcu_code[0x102a/2] = 0xffe4; jm_mcu_code[0x102c/2] = 0x3439; jm_mcu_code[0x102e/2] = 0x0010; jm_mcu_code[0x1030/2] = 0x17fe; jm_mcu_code[0x1032/2] = 0x3239; jm_mcu_code[0x1034/2] = 0x0010; jm_mcu_code[0x1036/2] = 0x17fc; jm_mcu_code[0x1038/2] = 0x4e75; /******************************************************* 6th M68k code uploaded by the MCU (tile upload) *******************************************************/ jm_shared_ram[0x00ca/2] = 0x4ef9; jm_shared_ram[0x00cc/2] = 0x0010; jm_shared_ram[0x00ce/2] = 0x1800;//jmp $101800 //jm_shared_ram[0x00c6/2] = 0x4e75;//rts jm_mcu_code[0x1800/2] = 0x22da;//move.l (A2)+,(A1)+ jm_mcu_code[0x1802/2] = 0xb5fc; jm_mcu_code[0x1804/2] = 0x0002; jm_mcu_code[0x1806/2] = 0x6600; jm_mcu_code[0x1808/2] = 0x6706; jm_mcu_code[0x180a/2] = 0x51c8; jm_mcu_code[0x180c/2] = 0xfff4;//dbra D0,f00ca jm_mcu_code[0x180e/2] = 0x4e75;//rts jm_mcu_code[0x1810/2] = 0xd4fc; jm_mcu_code[0x1812/2] = 0x0a00; jm_mcu_code[0x1814/2] = 0x60ea; } } READ16_MEMBER(jalmah_state::mjzoomin_mcu_r) { static const int resp[] = { 0x9c, 0xd8, 0x00, 0x2a, 0x6a, 0x00, 0x99, 0xd8, 0x00, 0x2f, 0x6f, 0x00, 0x22, 0x62, 0x00, 0x25, 0x65, 0x00, 0x35, 0x75, 0x00, 0x36, 0x36, 0x00, 0x21, 0x61, 0x00 }; int res; res = resp[m_respcount++]; if (m_respcount >= sizeof(resp)/sizeof(resp[0])) m_respcount = 0; // logerror("%04x: mcu_r %02x\n",cpu_get_pc(&space.device()),res); return res; } /* data value is REQ under mjzoomin video test menu.It is related to the MCU? */ WRITE16_MEMBER(jalmah_state::mjzoomin_mcu_w) { UINT16 *jm_shared_ram = m_jm_shared_ram; UINT16 *jm_mcu_code = m_jm_mcu_code; if(ACCESSING_BITS_0_7 && data) { /****************************************************** 1st M68k code uploaded by the MCU(Service Mode PC=2a56). Program passes some parameters before entering into the sub-routine (jsr) D1 = 0xf A0 = 1026e A1 = 88600 (A0) is the vector number for take the real palette address. ******************************************************/ jm_shared_ram[0x00c6/2] = 0x4ef9; jm_shared_ram[0x00c8/2] = 0x0010; jm_shared_ram[0x00ca/2] = 0x0000;//jsr $100000 //jm_shared_ram[0x00cc/2] = 0x4e75;//rts //needed? we can use jmp instead of jsr... jm_mcu_code[0x0000/2] = 0x2050;//movea.l (A0),A0 jm_mcu_code[0x0002/2] = 0x32d8;//move.w (A0)+,(A1)+ jm_mcu_code[0x0004/2] = 0x51c9; jm_mcu_code[0x0006/2] = 0xfffc;//dbra D1,f00ca jm_mcu_code[0x0008/2] = 0x4e75;//rts /******************************************************* 2nd M68k code uploaded by the MCU (Sound read/write) (Note:copied from suchipi,check here the sound banking) *******************************************************/ jm_shared_ram[0x0020/2] = 0x4ef9; jm_shared_ram[0x0022/2] = 0x0010; jm_shared_ram[0x0024/2] = 0x1800;//jmp $101800 /*Wrong,it reads from the rom...*/ //0x33c2 0011 80fe move.w D2,$1180fe //0x0642 addi.w $1,D2 //0x0001 //0x0242 andi.w $3,D2 ;might be not needed //0x0003 //0x33c2 move.w D2,$80018 ;sound bank //0x0008 //0x0018 //0x3439 0011 80fe move.w $1180fe,D2 jm_mcu_code[0x1800/2] = 0x33c2; jm_mcu_code[0x1802/2] = 0x0011; jm_mcu_code[0x1804/2] = 0x80fe; jm_mcu_code[0x1806/2] = 0x0642; jm_mcu_code[0x1808/2] = 0x0001; jm_mcu_code[0x180a/2] = 0x0242; jm_mcu_code[0x180c/2] = 0x0003; jm_mcu_code[0x180e/2] = 0x33c2; jm_mcu_code[0x1810/2] = 0x0008; jm_mcu_code[0x1812/2] = 0x0018; jm_mcu_code[0x1814/2] = 0x0040; jm_mcu_code[0x1816/2] = 0x0080;//ori $80,D0 jm_mcu_code[0x1818/2] = 0x33c0;//move.w D0,$80040 jm_mcu_code[0x181a/2] = 0x0008; jm_mcu_code[0x181c/2] = 0x0040; jm_mcu_code[0x181e/2] = 0x33fc;//move.w $10,$80040 jm_mcu_code[0x1820/2] = 0x0010; jm_mcu_code[0x1822/2] = 0x0008; jm_mcu_code[0x1824/2] = 0x0040; jm_mcu_code[0x1826/2] = 0x3439; jm_mcu_code[0x1828/2] = 0x0011; jm_mcu_code[0x182a/2] = 0x80fe; jm_mcu_code[0x182c/2] = 0x4e75; //jm_mcu_code[0x1818/2] = 0x3239; //jm_mcu_code[0x181a/2] = 0x0008; //jm_mcu_code[0x181c/2] = 0x0040; //jm_mcu_code[0x181e/2] = 0x0241; //jm_mcu_code[0x1820/2] = 0x0001; //jm_mcu_code[0x1822/2] = 0x66f4; //jm_mcu_code[0x1824/2] = 0x4e75; /******************************************************* 3rd M68k code uploaded by the MCU(palette upload) *******************************************************/ jm_shared_ram[0x00c0/2] = 0x4ef9; jm_shared_ram[0x00c2/2] = 0x0010; jm_shared_ram[0x00c4/2] = 0x1000;//jmp $101000 //jm_shared_ram[0x00c6/2] = 0x4e75;//rts jm_mcu_code[0x1000/2] = 0x33c2; jm_mcu_code[0x1002/2] = 0x0010; jm_mcu_code[0x1004/2] = 0x17fe; //move.w D2,$1017fe jm_mcu_code[0x1006/2] = 0x33c1; jm_mcu_code[0x1008/2] = 0x0010; jm_mcu_code[0x100a/2] = 0x17fc; //move.w D1,$1017fc jm_mcu_code[0x100c/2] = 0x720f; jm_mcu_code[0x100e/2] = 0x740f; //moveq $07,D2 jm_mcu_code[0x1010/2] = 0x23c8; jm_mcu_code[0x1012/2] = 0x0010; jm_mcu_code[0x1014/2] = 0x17f0; jm_mcu_code[0x1016/2] = 0x2050; //movea (A0),A0 jm_mcu_code[0x1018/2] = 0x32d8; jm_mcu_code[0x101a/2] = 0x51ca; jm_mcu_code[0x101c/2] = 0xfffc; jm_mcu_code[0x101e/2] = 0x2079; jm_mcu_code[0x1020/2] = 0x0010; jm_mcu_code[0x1022/2] = 0x17f0; jm_mcu_code[0x1024/2] = 0xd0fc; jm_mcu_code[0x1026/2] = 0x0004;//adda.w $4,A0 jm_mcu_code[0x1028/2] = 0x51c9; jm_mcu_code[0x102a/2] = 0xffe4; jm_mcu_code[0x102c/2] = 0x3439; jm_mcu_code[0x102e/2] = 0x0010; jm_mcu_code[0x1030/2] = 0x17fe; jm_mcu_code[0x1032/2] = 0x3239; jm_mcu_code[0x1034/2] = 0x0010; jm_mcu_code[0x1036/2] = 0x17fc; jm_mcu_code[0x1038/2] = 0x4e75; } } READ16_MEMBER(jalmah_state::kakumei_mcu_r) { static const int resp[] = { 0x8a, 0xd8, 0x00, 0x3c, 0x7c, 0x00, 0x99, 0xd8, 0x00, 0x25, 0x65, 0x00, 0x36, 0x76, 0x00, 0x35, 0x75, 0x00, 0x2f, 0x6f, 0x00, 0x31, 0x71, 0x00, 0x3e, 0x7e, 0x00 }; int res; res = resp[m_respcount++]; if (m_respcount >= sizeof(resp)/sizeof(resp[0])) m_respcount = 0; // popmessage("%04x: mcu_r %02x",cpu_get_pc(&space.device()),res); return res; } READ16_MEMBER(jalmah_state::suchipi_mcu_r) { static const int resp[] = { 0x8a, 0xd8, 0x00, 0x3c, 0x7c, 0x00, 0x99, 0xd8, 0x00, 0x25, 0x65, 0x00, 0x36, 0x76, 0x00, 0x35, 0x75, 0x00, 0x2f, 0x6f, 0x00, 0x31, 0x71, 0x00, 0x3e, 0x7e, 0x00 }; int res; res = resp[m_respcount++]; if (m_respcount >= sizeof(resp)/sizeof(resp[0])) m_respcount = 0; // popmessage("%04x: mcu_r %02x",cpu_get_pc(&space.device()),res); return res; } static DRIVER_INIT( urashima ) { jalmah_state *state = machine.driver_data<jalmah_state>(); machine.device("maincpu")->memory().space(AS_PROGRAM)->install_read_handler(0x80004, 0x80005, read16_delegate(FUNC(jalmah_state::urashima_mcu_r), state)); machine.device("maincpu")->memory().space(AS_PROGRAM)->install_write_handler(0x80012, 0x80013, write16_delegate(FUNC(jalmah_state::urashima_mcu_w), state)); state->m_mcu_prg = 0x12; } static DRIVER_INIT( daireika ) { jalmah_state *state = machine.driver_data<jalmah_state>(); machine.device("maincpu")->memory().space(AS_PROGRAM)->install_read_handler(0x80004, 0x80005, read16_delegate(FUNC(jalmah_state::daireika_mcu_r), state)); machine.device("maincpu")->memory().space(AS_PROGRAM)->install_write_handler(0x80012, 0x80013, write16_delegate(FUNC(jalmah_state::daireika_mcu_w), state)); state->m_mcu_prg = 0x11; } static DRIVER_INIT( mjzoomin ) { jalmah_state *state = machine.driver_data<jalmah_state>(); machine.device("maincpu")->memory().space(AS_PROGRAM)->install_read_handler(0x80004, 0x80005, read16_delegate(FUNC(jalmah_state::mjzoomin_mcu_r), state)); machine.device("maincpu")->memory().space(AS_PROGRAM)->install_write_handler(0x80012, 0x80013, write16_delegate(FUNC(jalmah_state::mjzoomin_mcu_w), state)); state->m_mcu_prg = 0x13; } static DRIVER_INIT( kakumei ) { jalmah_state *state = machine.driver_data<jalmah_state>(); machine.device("maincpu")->memory().space(AS_PROGRAM)->install_read_handler(0x80004, 0x80005, read16_delegate(FUNC(jalmah_state::kakumei_mcu_r), state)); state->m_mcu_prg = 0x21; } static DRIVER_INIT( kakumei2 ) { jalmah_state *state = machine.driver_data<jalmah_state>(); machine.device("maincpu")->memory().space(AS_PROGRAM)->install_read_handler(0x80004, 0x80005, read16_delegate(FUNC(jalmah_state::kakumei_mcu_r), state)); state->m_mcu_prg = 0x22; } static DRIVER_INIT( suchipi ) { jalmah_state *state = machine.driver_data<jalmah_state>(); machine.device("maincpu")->memory().space(AS_PROGRAM)->install_read_handler(0x80004, 0x80005, read16_delegate(FUNC(jalmah_state::suchipi_mcu_r), state)); state->m_mcu_prg = 0x23; } /*First version of the MCU*/ GAME( 1989, urashima, 0, urashima, urashima, urashima, ROT0, "UPL", "Otogizoushi Urashima Mahjong (Japan)", GAME_IMPERFECT_GRAPHICS | GAME_IMPERFECT_SOUND ) GAME( 1989, daireika, 0, jalmah, daireika, daireika, ROT0, "Jaleco / NMK", "Mahjong Daireikai (Japan)", GAME_IMPERFECT_GRAPHICS | GAME_IMPERFECT_SOUND ) GAME( 1990, mjzoomin, 0, jalmah, mjzoomin, mjzoomin, ROT0, "Jaleco", "Mahjong Channel Zoom In (Japan)", GAME_IMPERFECT_GRAPHICS | GAME_IMPERFECT_SOUND ) /*Second version of the MCU*/ GAME( 1990, kakumei, 0, jalmah, kakumei, kakumei, ROT0, "Jaleco", "Mahjong Kakumei (Japan)", GAME_IMPERFECT_GRAPHICS ) GAME( 1992, kakumei2, 0, jalmah, kakumei2, kakumei2, ROT0, "Jaleco", "Mahjong Kakumei 2 - Princess League (Japan)", GAME_IMPERFECT_GRAPHICS ) GAME( 1993, suchipi, 0, jalmah, suchipi, suchipi, ROT0, "Jaleco", "Idol Janshi Suchie-Pai Special (Japan)", GAME_IMPERFECT_GRAPHICS )