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

  machine/stvcd.c - Sega Saturn and ST-V CD-ROM handling

  Another tilt at the windmill in 2011 by R. Belmont.

  Status: All known discs at least load their executable, and many load
          some data files successfully, but there are other problems.

  Information sources:
  - Tyranid's document
  - A commented disassembly I made of the Saturn BIOS's CD code
  - Yabuse's cs2.c
  - The ISO/IEC "Yellow Book" CD-ROM standard, 1995 version

  Address is mostly in terms of FAD (Frame ADdress).
  FAD is absolute number of frames from the start of the disc.
  In other words, FAD = LBA + 150; FAD is the same units as
  LBA except it counts starting at absolute zero instead of
  the first sector (00:02:00 in MSF format).

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

#include "emu.h"
#include "imagedev/chd_cd.h"
#include "includes/saturn.h"
#include "cdrom.h"
#include "sound/cdda.h"
#include "debugger.h"
#include "coreutil.h"

// super-verbose
#if 0
#define CDROM_LOG(x) printf x;
#else
#define CDROM_LOG(x)
#endif

// HIRQ definitions
#define CMOK 0x0001 // command dispatch possible
#define DRDY 0x0002 // data transfer preparations complete
#define CSCT 0x0004 // finished reading 1 sector
#define BFUL 0x0008 // CD buffer full
#define PEND 0x0010 // CD playback completed
#define DCHG 0x0020 // disc change / tray open
#define ESEL 0x0040 // selector settings processing complete
#define EHST 0x0080 // host input/output processing complete
#define ECPY 0x0100 // duplication/move processing complete
#define EFLS 0x0200 // file system processing complete
#define SCDQ 0x0400 // subcode Q update completed
#define MPED 0x0800 // MPEG-related processing complete
#define MPCM 0x1000 // MPEG action uncertain
#define MPST 0x2000 // MPEG interrupt status report

// CD status (hi byte of CR1) definitions:
// (these defines are shifted up 8)
#define CD_STAT_BUSY     0x0000     // status change in progress
#define CD_STAT_PAUSE    0x0100     // CD block paused (temporary stop)
#define CD_STAT_STANDBY  0x0200     // CD drive stopped
#define CD_STAT_PLAY     0x0300     // CD play in progress
#define CD_STAT_SEEK     0x0400     // drive seeking
#define CD_STAT_SCAN     0x0500     // drive scanning
#define CD_STAT_OPEN     0x0600     // tray is open
#define CD_STAT_NODISC   0x0700     // no disc present
#define CD_STAT_RETRY    0x0800     // read retry in progress
#define CD_STAT_ERROR    0x0900     // read data error occurred
#define CD_STAT_FATAL    0x0a00     // fatal error (hard reset required)
#define CD_STAT_PERI     0x2000     // periodic response if set, else command response
#define CD_STAT_TRANS    0x4000     // data transfer request if set
#define CD_STAT_WAIT     0x8000     // waiting for command if set, else executed immediately
#define CD_STAT_REJECT   0xff00     // ultra-fatal error.

int saturn_state::get_timing_command(void)
{
	/* TODO: calculate timings based off command params */
	return 16667;
}

/* FIXME: assume Saturn CD-ROMs to have a 2 secs pre-gap for now. */
int saturn_state::get_track_index(uint32_t fad)
{
	uint32_t rel_fad;
	uint8_t track;

	if(cdrom_get_track_type(cdrom, cdrom_get_track(cdrom, fad)) != CD_TRACK_AUDIO)
		return 1;

	track = cdrom_get_track( cdrom, fad );

	rel_fad = fad - cdrom_get_track_start( cdrom, track );

	if(rel_fad < 150)
		return 0;

	return 1;
}

int saturn_state::sega_cdrom_get_adr_control(cdrom_file *file, int track)
{
	return BITSWAP8(cdrom_get_adr_control(file, cur_track),3,2,1,0,7,6,5,4);
}

void saturn_state::cr_standard_return(uint16_t cur_status)
{
	if ((cd_stat & 0x0f00) == CD_STAT_SEEK)
	{
		/* During seek state, values returned are from the target position */
		uint8_t seek_track = cdrom_get_track(cdrom, cd_fad_seek-150);

		cr1 = cur_status | (playtype << 7) | 0x00 | (cdda_repeat_count & 0xf);
		cr2 =  (seek_track == 0xff) ? 0xffff : ((sega_cdrom_get_adr_control(cdrom, seek_track)<<8) | seek_track);
		cr3 = (get_track_index(cd_fad_seek)<<8) | (cd_fad_seek>>16); //index & 0xff00
		cr4 = cd_fad_seek;
	}
	else
	{
		/*
		TODO:
		- Whizz: wpset 0x608f030,4,w,wpdata==0x100&&pc!=0x6040006
		*/
		cr1 = cur_status | (playtype << 7) | 0x00 | (cdda_repeat_count & 0xf); //options << 4 | repeat & 0xf
		cr2 = (cur_track == 0xff) ? 0xffff : ((sega_cdrom_get_adr_control(cdrom, cur_track)<<8) | (cdrom_get_track(cdrom, cd_curfad-150)+1));
		cr3 = (get_track_index(cd_curfad)<<8) | (cd_curfad>>16); //index & 0xff00
		cr4 = cd_curfad;
	}
}

void saturn_state::cd_exec_command( void )
{
	uint32_t temp;

	if(cr1 != 0 &&
		((cr1 & 0xff00) != 0x5100) &&
		((cr1 & 0xff00) != 0x5200) &&
		((cr1 & 0xff00) != 0x5300) &&
		1)
		logerror("CD: command exec %04x %04x %04x %04x %04x (stat %04x)\n", hirqreg, cr1, cr2, cr3, cr4, cd_stat);

	switch ((cr1 >> 8) & 0xff)
	{
		case 0x00:
			//CDROM_LOG(("%s:CD: Get Status\n", machine().describe_context()))
			hirqreg |= CMOK;
			if(status_type == 0)
				cr_standard_return(cd_stat);
			else
			{
				cr1 = (cd_stat) | (prev_cr1 & 0xff);
				cr2 = prev_cr2;
				cr3 = prev_cr3;
				cr4 = prev_cr4;
				status_type = 0; /* Road Blaster and friends needs this otherwise they won't boot. */
			}
			//CDROM_LOG(("   = %04x %04x %04x %04x %04x\n", hirqreg, cr1, cr2, cr3, cr4))
			break;

		case 0x01:
			CDROM_LOG(("%s:CD: Get Hardware Info\n", machine().describe_context()))
			hirqreg |= CMOK;
			cr1 = cd_stat;
			cr2 = 0x0201;
			cr3 = 0x0000;
			cr4 = 0x0400;
			status_type = 0;
			break;

		case 0x02:    // Get TOC
			CDROM_LOG(("%s:CD: Get TOC\n", machine().describe_context()))
			cd_readTOC();
			cd_stat = CD_STAT_TRANS|CD_STAT_PAUSE;
			cr1 = cd_stat;
			cr2 = 102*2;    // TOC length in words (102 entries @ 2 words/4bytes each)
			cr3 = 0;
			cr4 = 0;
			xferdnum = 0;
			hirqreg |= (CMOK|DRDY);
			status_type = 0;
			break;

		case 0x03:    // get session info (lower byte = session # to get?)
						// bios is interested in returns in cr3 and cr4
						// cr3 should be data track #
						// cr4 must be > 1 and < 100 or bios gets angry.
			CDROM_LOG(("%s:CD: Get Session Info\n", machine().describe_context()))
			cd_readTOC();
			switch (cr1 & 0xff)
			{
				case 0: // get total session info / disc end
					cd_stat = CD_STAT_PAUSE;
					cr1 = cd_stat;
					cr2 = 0;
					cr3 = 0x0100 | tocbuf[(101*4)+1];
					cr4 = tocbuf[(101*4)+2]<<8 | tocbuf[(101*4)+3];
					break;

				case 1: // get total session info / disc start
					cd_stat = CD_STAT_PAUSE;
					cr1 = cd_stat;
					cr2 = 0;
					cr3 = 0x0100;   // sessions in high byte, session start in lower
					cr4 = 0;
					break;

				default:
					osd_printf_error("CD: Unknown request to Get Session Info %x\n", cr1 & 0xff);
					cr1 = cd_stat;
					cr2 = 0;
					cr3 = 0;
					cr4 = 0;
					break;
			}

			hirqreg |= (CMOK);
			status_type = 0;
			break;

		/* TODO: double check this */
		case 0x04:    // initialize CD system
				// CR1 & 1 = reset software
				// CR1 & 2 = decode RW subcode
				// CR1 & 4 = don't confirm mode 2 subheader
				// CR1 & 8 = retry reading mode 2 sectors
				// CR1 & 10 = force single-speed
				// CR1 & 80 = no change flag (done by Assault Suit Leynos 2)
			CDROM_LOG(("%s:CD: Initialize CD system\n", machine().describe_context()))
			//if((cr1 & 0x81) == 0x00) //guess TODO: nope, Choice Cuts doesn't like it, it crashes if you try to skip the FMV otherwise.
			{
				if(((cd_stat & 0x0f00) != CD_STAT_NODISC) && ((cd_stat & 0x0f00) != CD_STAT_OPEN))
				{
					cd_stat = CD_STAT_PAUSE;
					cd_curfad = 150;
					//cur_track = 1;
					fadstoplay = 0;
				}
				in_buffer = 0;
				buffull = 0;
				hirqreg &= 0xffe5;
				cd_speed = (cr1 & 0x10) ? 1 : 2;

				/* reset filter connections */
				/* Guess: X-Men COTA sequence is 0x48->0x48->0x04(01)->0x04(00)->0x30 then 0x10, without this game throws a FAD reject error */
				/* X-Men vs. SF is even fussier, sequence is  0x04 (1) 0x04 (0) 0x03 (0) 0x03 (1) 0x30 */
				#if 0
				for(int i=0;i<MAX_FILTERS;i++)
				{
					filters[i].fad = 0;
					filters[i].range = 0xffffffff;
					filters[i].mode = 0;
					filters[i].chan = 0;
					filters[i].smmask = 0;
					filters[i].cimask = 0;
					filters[i].fid = 0;
					filters[i].smval = 0;
					filters[i].cival = 0;
				}
				#endif

				/* reset CD device connection */
				//cddevice = (filterT *)nullptr;
			}

			hirqreg |= (CMOK|ESEL);
			cr_standard_return(cd_stat);
			status_type = 0;
			break;

		case 0x06:    // end data transfer (TODO: needs to be worked on!)
				// returns # of bytes transferred (24 bits) in
				// low byte of cr1 (MSB) and cr2 (middle byte, LSB)
			CDROM_LOG(("%s:CD: End data transfer (%d bytes xfer'd)\n", machine().describe_context(), xferdnum))

			// clear the "transfer" flag
			cd_stat &= ~CD_STAT_TRANS;

			if (xferdnum)
			{
				cr1 = (cd_stat) | ((xferdnum>>17) & 0xff);
				cr2 = (xferdnum>>1)&0xffff;
				cr3 = 0;
				cr4 = 0;
			}
			else
			{
				logerror("No xferdnum error\n");
				cr1 = (cd_stat) | (0xff);   // is this right?
				cr2 = 0xffff;
				cr3 = 0;
				cr4 = 0;
			}

			// try to clean up any transfers still in progress
			switch (xfertype32)
			{
				case XFERTYPE32_GETSECTOR:
				case XFERTYPE32_PUTSECTOR:
					hirqreg |= EHST;
					break;

				case XFERTYPE32_GETDELETESECTOR:
					if (transpart->size > 0)
					{
						int32_t i;

						xfertype32 = XFERTYPE32_INVALID;

						// deallocate the blocks
						for (i = xfersectpos; i < xfersectpos+xfersectnum; i++)
						{
							cd_free_block(transpart->blocks[i]);
							transpart->blocks[i] = (blockT *)nullptr;
							transpart->bnum[i] = 0xff;
						}

						// defrag what's left
						cd_defragblocks(transpart);

						// clean up our state
						transpart->size -= xferdnum;
						transpart->numblks -= xfersectnum;

						if (freeblocks == 200)
						{
							sectorstore = 0;
						}

						hirqreg |= EHST;
					}
					break;

				default:
					break;
			}


			xferdnum = 0;
			hirqreg |= CMOK;

			CDROM_LOG(("   = %04x %04x %04x %04x %04x\n", hirqreg, cr1, cr2, cr3, cr4))
			status_type = 1;
			break;

		case 0x10: // Play Disc.  FAD is in lowest 7 bits of cr1 and all of cr2.
			uint32_t start_pos,end_pos;
			uint8_t play_mode;

			CDROM_LOG(("%s:CD: Play Disc\n",   machine().describe_context()))
			cd_stat = CD_STAT_PLAY;

			play_mode = (cr3 >> 8) & 0x7f;

			if (!(cr3 & 0x8000))    // preserve current position if bit 7 set
			{
				start_pos = ((cr1&0xff)<<16) | cr2;
				end_pos = ((cr3&0xff)<<16) | cr4;

				if (start_pos & 0x800000)
				{
					if (start_pos != 0xffffff)
						cd_curfad = start_pos & 0xfffff;

					logerror("fad mode\n");
					cur_track = cdrom_get_track(cdrom, cd_curfad-150);
				}
				else
				{
					// track mode
					if(((start_pos)>>8) != 0)
					{
						cur_track = (start_pos)>>8;
						cd_fad_seek = cdrom_get_track_start(cdrom, cur_track-1);
						cd_stat = CD_STAT_SEEK;
						machine().device<cdda_device>("cdda")->pause_audio(0);
					}
					else
					{
						/* TODO: Waku Waku 7 sets up track 0, that basically doesn't make any sense. Just skip it for now. */
						popmessage("Warning: track mode == 0, contact MAMEdev");
						cr_standard_return(cd_stat);
						hirqreg |= (CMOK);
						return;
					}

					printf("track mode %d\n",cur_track);
				}

				if (end_pos & 0x800000)
				{
					if (end_pos != 0xffffff)
						fadstoplay = end_pos & 0xfffff;
				}
				else
				{
					uint8_t end_track;

					end_track = (end_pos)>>8;
					fadstoplay = cdrom_get_track_start(cdrom, end_track) - cd_fad_seek;
				}
			}
			else    // play until the end of the disc
			{
				start_pos = ((cr1&0xff)<<16) | cr2;
				end_pos = ((cr3&0xff)<<16) | cr4;

				if(start_pos != 0xffffff)
				{
					/* Madou Monogatari sets 0xff80xxxx as end position, needs investigation ... */
					if(end_pos & 0x800000)
						fadstoplay = end_pos & 0xfffff;
					else
					{
						if(end_pos == 0)
							fadstoplay = (cdrom_get_track_start(cdrom, 0xaa)) - cd_curfad;
						else
							fadstoplay = (cdrom_get_track_start(cdrom, (end_pos & 0xff00) >> 8)) - cd_curfad;
					}
					logerror("track mode %08x %08x\n",cd_curfad,fadstoplay);
				}
				else
				{
					/* resume from a pause state */
					/* TODO: Galaxy Fight calls 10ff ffff ffff ffff, but then it calls 0x04->0x02->0x06->0x11->0x04->0x02->0x06 command sequence
					   (and current implementation nukes start/end FAD addresses at 0x04). I'm sure that this doesn't work like this, but there could
					   be countless possible combinations ... */
					if(fadstoplay == 0)
					{
						cd_curfad = cdrom_get_track_start(cdrom, cur_track-1);
						fadstoplay = cdrom_get_track_start(cdrom, cur_track) - cd_curfad;
					}
					logerror("track resume %08x %08x\n",cd_curfad,fadstoplay);
				}
			}

			CDROM_LOG(("CD: Play Disc: start %x length %x\n", cd_curfad, fadstoplay))

			cr_standard_return(cd_stat);
			hirqreg |= (CMOK);
			oddframe = 0;
			in_buffer = 0;

			playtype = 0;

			// cdda
			if(cdrom_get_track_type(cdrom, cdrom_get_track(cdrom, cd_curfad)) == CD_TRACK_AUDIO)
			{
				machine().device<cdda_device>("cdda")->pause_audio(0);
				//machine().device<cdda_device>("cdda")->start_audio(cd_curfad, fadstoplay);
				//cdda_repeat_count = 0;
			}

			if(play_mode != 0x7f)
				cdda_maxrepeat = play_mode & 0xf;
			else
				cdda_maxrepeat = 0;

			cdda_repeat_count = 0;
			status_type = 0;
			break;

		case 0x11: // disc seek
			CDROM_LOG(("%s:CD: Disc seek\n",   machine().describe_context()))
			//printf("%08x %08x %08x %08x\n",cr1,cr2,cr3,cr4);
			if (cr1 & 0x80)
			{
				temp = (cr1&0xff)<<16;  // get FAD to seek to
				temp |= cr2;

				//cd_curfad = temp;

				if (temp == 0xffffff)
				{
					cd_stat = CD_STAT_PAUSE;
					machine().device<cdda_device>("cdda")->pause_audio(1);
				}
				else
				{
					cd_curfad = ((cr1&0x7f)<<16) | cr2;
					printf("disc seek with params %04x %04x\n",cr1,cr2); //Area 51 sets this up
				}
			}
			else
			{
				// is it a valid track?
				if (cr2 >> 8)
				{
					cd_stat = CD_STAT_PAUSE;
					cur_track = cr2>>8;;
					cd_curfad = cdrom_get_track_start(cdrom, cur_track-1);
					machine().device<cdda_device>("cdda")->pause_audio(1);
					// (index is cr2 low byte)
				}
				else // error!
				{
					cd_stat = CD_STAT_STANDBY;
					cd_curfad = 0xffffffff;
					cur_track = 0xff;
					machine().device<cdda_device>("cdda")->stop_audio(); //stop any pending CD-DA
				}
			}


			hirqreg |= CMOK;
			cr_standard_return(cd_stat);
			status_type = 0;
			break;

		case 0x12: // FFWD / REW
			//cr1 bit 0 determines if this is a Fast Forward (0) or a Rewind (1) command
			// ...
			break;

		case 0x20: // Get SubCode Q / RW Channel
			switch(cr1 & 0xff)
			{
				case 0: // Get Q
				{
					uint32_t msf_abs,msf_rel;
					uint8_t track;
					cr1 = cd_stat | 0;
					cr2 = 10/2;
					cr3 = 0;
					cr4 = 0;

					/*
					Subcode Q info should be:
					---- --x- S0
					---- ---x S1
					xxxx ---- [0] Control (bit 7 Pre-emphasis, bit 6: copy permitted, bit 5 undefined, bit 4 number of channels)
					---- xxxx [0] address (0x0001 Mode 1)
					xxxx xxxx [1] track number (1-99, AA lead-out), BCD format
					xxxx xxxx [2] index (01 lead-out), BCD format
					xxxx xxxx [3] Time within' track M
					xxxx xxxx [4] Time within' track S
					xxxx xxxx [5] Time within' track F
					xxxx xxxx [6] Zero
					xxxx xxxx [7] Absolute M
					xxxx xxxx [8] Absolute S
					xxxx xxxx [9] Absolute F
					xxxx xxxx [10] CRCC
					xxxx xxxx [11] CRCC
					*/

					msf_abs = lba_to_msf_alt( cd_curfad - 150 );
					track = cdrom_get_track( cdrom, cd_curfad );
					msf_rel = lba_to_msf_alt( cd_curfad - 150 - cdrom_get_track_start( cdrom, track ) );

					xfertype = XFERTYPE_SUBQ;
					xfercount = 0;
					subqbuf[0] = 0x01 | ((cdrom_get_track_type(cdrom, cdrom_get_track(cdrom, track+1)) == CD_TRACK_AUDIO) ? 0x00 : 0x40);
					subqbuf[1] = dec_2_bcd(track+1);
					subqbuf[2] = dec_2_bcd(get_track_index(cd_curfad));
					subqbuf[3] = dec_2_bcd((msf_rel >> 16) & 0xff);
					subqbuf[4] = dec_2_bcd((msf_rel >> 8) & 0xff);
					subqbuf[5] = dec_2_bcd((msf_rel >> 0) & 0xff);
					subqbuf[6] = 0;
					subqbuf[7] = dec_2_bcd((msf_abs >> 16) & 0xff);
					subqbuf[8] = dec_2_bcd((msf_abs >> 8) & 0xff);
					subqbuf[9] = dec_2_bcd((msf_abs >> 0) & 0xff);
				}
				break;

				case 1: // Get RW
					cr1 = cd_stat | 0;
					cr2 = 12;
					cr3 = 0;
					cr4 = 0;

					xfertype = XFERTYPE_SUBRW;
					xfercount = 0;

					/* return null data for now */
					{
						int i;

						for(i=0;i<12*2;i++)
							subrwbuf[i] = 0;
					}
					break;
			}
			hirqreg |= CMOK|DRDY;
			status_type = 0;
			break;

		case 0x30:    // Set CD Device connection
			{
				uint8_t parm;

				// get operation
				parm = cr3>>8;

				CDROM_LOG(("%s:CD: Set CD Device Connection filter # %x\n",   machine().describe_context(), parm))

				cddevicenum = parm;

				if (parm == 0xff)
				{
					cddevice = (filterT *)nullptr;
				}
				else
				{
					if (parm < 0x24)
					{
						cddevice = &filters[(cr3>>8)];
					}
				}


				hirqreg |= (CMOK|ESEL);
				cr_standard_return(cd_stat);
				status_type = 0;
			}
			break;

		case 0x31:
			popmessage("Get CD Device Connection, contact MAMEdev");
			hirqreg |= CMOK;
			break;

		case 0x32:    // Last Buffer Destination
			cr1 = cd_stat | 0;
			cr2 = 0;
			cr3 = lastbuf << 8;
			cr4 = 0;
			hirqreg |= (CMOK);
			status_type = 0;
			break;

		case 0x40:    // Set Filter Range
						// cr1 low + cr2 = FAD0, cr3 low + cr4 = FAD1
						// cr3 hi = filter num.
			{
				uint8_t fnum = (cr3>>8)&0xff;

				CDROM_LOG(("%s:CD: Set Filter Range\n",   machine().describe_context()))

				filters[fnum].fad = ((cr1 & 0xff)<<16) | cr2;
				filters[fnum].range = ((cr3 & 0xff)<<16) | cr4;

				printf("%08x %08x %d\n",filters[fnum].fad,filters[fnum].range,fnum);

				hirqreg |= (CMOK|ESEL);
				cr_standard_return(cd_stat);
				status_type = 0;
			}
			break;

		case 0x41:
			popmessage("Get Filter Range, contact MAMEdev");
			hirqreg |= CMOK;
			break;

		case 0x42:    // Set Filter Subheader conditions
			{
				uint8_t fnum = (cr3>>8)&0xff;

				CDROM_LOG(("%s:CD: Set Filter Subheader conditions %x => chan %x masks %x fid %x vals %x\n", machine().describe_context(), fnum, cr1&0xff, cr2, cr3&0xff, cr4))

				filters[fnum].chan = cr1 & 0xff;
				filters[fnum].smmask = (cr2>>8)&0xff;
				filters[fnum].cimask = cr2&0xff;
				filters[fnum].fid = cr3&0xff;
				filters[fnum].smval = (cr4>>8)&0xff;
				filters[fnum].cival = cr4&0xff;

				hirqreg |= (CMOK|ESEL);
				cr_standard_return(cd_stat);
				status_type = 0;
			}
			break;

		case 0x43:    // Get Filter Subheader conditions
			{
				uint8_t fnum = (cr3>>8)&0xff;

				CDROM_LOG(("%s:CD: Set Filter Subheader conditions %x => chan %x masks %x fid %x vals %x\n", machine().describe_context(), fnum, cr1&0xff, cr2, cr3&0xff, cr4))

				cr1 = cd_stat | (filters[fnum].chan & 0xff);
				cr2 = (filters[fnum].smmask << 8) | (filters[fnum].cimask & 0xff);
				cr3 = filters[fnum].fid;
				cr4 = (filters[fnum].smval << 8) | (filters[fnum].cival & 0xff);

				hirqreg |= (CMOK|ESEL);
				status_type = 0;
			}
			break;

		case 0x44:    // Set Filter Mode
			{
				uint8_t fnum = (cr3>>8)&0xff;
				uint8_t mode = (cr1 & 0xff);

				// initialize filter?
				if (mode & 0x80)
				{
					memset(&filters[fnum], 0, sizeof(filterT));
				}
				else
				{
					filters[fnum].mode = mode;
				}

				CDROM_LOG(("%s:CD: Set Filter Mode filt %x mode %x\n", machine().describe_context(), fnum, mode))
				hirqreg |= (CMOK|ESEL);
				cr_standard_return(cd_stat);
				status_type = 0;
			}
			break;

		case 0x45:    // Get Filter Mode
			{
				uint8_t fnum = (cr3>>8)&0xff;

				cr1 = cd_stat | (filters[fnum].mode & 0xff);
				cr2 = 0;
				cr3 = 0;
				cr4 = 0;

				hirqreg |= (CMOK|ESEL);
				status_type = 0;
			}
			break;

		case 0x46:    // Set Filter Connection
			{
				/* TODO: maybe condition false is cr3 low? */
				uint8_t fnum = (cr3>>8)&0xff;

				CDROM_LOG(("%s:CD: Set Filter Connection %x => mode %x parm %04x\n", machine().describe_context(), fnum, cr1 & 0xf, cr2))

				if (cr1 & 1)    // set true condition
					filters[fnum].condtrue = (cr2>>8)&0xff;

				if (cr1 & 2)    // set false condition
					filters[fnum].condfalse = cr2&0xff;

				hirqreg |= (CMOK|ESEL);
				cr_standard_return(cd_stat);
				status_type = 0;
			}
			break;

		case 0x48:    // Reset Selector
			{
			int i,j;

			CDROM_LOG(("%s:CD: Reset Selector\n",   machine().describe_context()))

			if((cr1 & 0xff) == 0x00)
			{
				uint8_t bufnum = cr3>>8;

				if(bufnum < MAX_FILTERS)
				{
					for (i = 0; i < MAX_BLOCKS; i++)
					{
						cd_free_block(partitions[bufnum].blocks[i]);
						partitions[bufnum].blocks[i] = (blockT *)nullptr;
						partitions[bufnum].bnum[i] = 0xff;
					}

					partitions[bufnum].size = -1;
					partitions[bufnum].numblks = 0;
				}

				// TODO: buffer full flag

				if (freeblocks == 200) { sectorstore = 0; }

				hirqreg |= (CMOK|ESEL);
				cr_standard_return(cd_stat);
				status_type = 0;
				return;
			}

			/* reset false filter output conditions */
			/* TODO: check these two. */
			if(cr1 & 0x80)
			{
				for(i=0;i<MAX_FILTERS;i++)
					filters[i].condfalse = 0;
			}

			/* reset true filter output conditions */
			if(cr1 & 0x40)
			{
				for(i=0;i<MAX_FILTERS;i++)
					filters[i].condtrue = 0;
			}

			/* reset filter conditions*/
			if(cr1 & 0x10)
			{
				for(i=0;i<MAX_FILTERS;i++)
				{
					filters[i].fad = 0;
					filters[i].range = 0xffffffff;
					filters[i].mode = 0;
					filters[i].chan = 0;
					filters[i].smmask = 0;
					filters[i].cimask = 0;
					filters[i].fid = 0;
					filters[i].smval = 0;
					filters[i].cival = 0;
				}
			}

			/* reset partition buffer data */
			if(cr1 & 0x4)
			{
				for(i=0;i<MAX_FILTERS;i++)
				{
					for (j = 0; j < MAX_BLOCKS; j++)
					{
						cd_free_block(partitions[i].blocks[j]);
						partitions[i].blocks[j] = (blockT *)nullptr;
						partitions[i].bnum[j] = 0xff;
					}

					partitions[i].size = -1;
					partitions[i].numblks = 0;
				}

				buffull = sectorstore = 0;
			}

			hirqreg |= (CMOK|ESEL);
			cr_standard_return(cd_stat);
			status_type = 0;
			}
			break;

		case 0x50:    // get Buffer Size
			cr1 = cd_stat;
			cr2 = (freeblocks > 200) ? 200 : freeblocks;
			cr3 = 0x1800;
			cr4 = 200;
			CDROM_LOG(("CD: Get Buffer Size = %d\n", cr2))
			hirqreg |= (CMOK);
			status_type = 0;
			break;

		case 0x51:    // get # sectors used in a buffer
			{
				uint32_t bufnum = cr3>>8;

				CDROM_LOG(("%s:CD: Get Sector Number (bufno %d) = %d blocks\n",   machine().describe_context(), bufnum, cr4))
				cr1 = cd_stat;
				cr2 = 0;
				cr3 = 0;
				if(cr1 & 0xff || cr2 || cr3 & 0xff || cr4)
					printf("Get # sectors used with params %04x %04x %04x %04x\n",cr1,cr2,cr3,cr4);

				// is the partition empty?
				if (partitions[bufnum].size == -1)
				{
					cr4 = 0;
				}
				else
				{
					cr4 = partitions[bufnum].numblks;
					//printf("Partition %08x %04x\n",bufnum,cr4);
				}

				//printf("%04x\n",cr4);
				if(cr4 == 0)
					hirqreg |= (CMOK);
				else
					hirqreg |= (CMOK|DRDY);
				status_type = 1;
			}
			break;

		case 0x52:    // calculate actual size
			{
				uint32_t bufnum = cr3>>8;
				uint32_t sectoffs = cr2;
				uint32_t numsect = cr4;

				CDROM_LOG(("%s:CD: Calculate actual size: buf %x offs %x numsect %x\n", machine().describe_context(), bufnum, sectoffs, numsect))

				calcsize = 0;
				if (partitions[bufnum].size != -1)
				{
					int32_t i;

					for (i = 0; i < numsect; i++)
					{
						if (partitions[bufnum].blocks[sectoffs+i])
						{
							calcsize += (partitions[bufnum].blocks[sectoffs+i]->size / 2);
						}
					}
				}

				hirqreg |= (CMOK|ESEL);
				cr_standard_return(cd_stat);
				status_type = 0;
			}
			break;

		case 0x53:    // get actual block size
			CDROM_LOG(("%s:CD: Get actual block size\n", machine().describe_context()))
			cr1 = cd_stat | ((calcsize>>16)&0xff);
			cr2 = (calcsize & 0xffff);
			cr3 = 0;
			cr4 = 0;
			hirqreg |= (CMOK|ESEL);
			status_type = 1;
			break;

		case 0x54:    // get sector info
			{
				uint32_t sectoffs = cr2 & 0xff;
				uint32_t bufnum = cr3>>8;

				if (bufnum >= MAX_FILTERS || !partitions[bufnum].blocks[sectoffs])
				{
					cr1 |= CD_STAT_REJECT & 0xff00;
					hirqreg |= (CMOK|ESEL);
					printf("Get sector info reject\n");
				}
				else
				{
					cr1 = cd_stat | ((partitions[bufnum].blocks[sectoffs]->FAD >> 16) & 0xff);
					cr2 = partitions[bufnum].blocks[sectoffs]->FAD & 0xffff;
					cr3 = ((partitions[bufnum].blocks[sectoffs]->fnum & 0xff) << 8) | (partitions[bufnum].blocks[sectoffs]->chan & 0xff);
					cr4 = ((partitions[bufnum].blocks[sectoffs]->subm & 0xff) << 8) | (partitions[bufnum].blocks[sectoffs]->cinf & 0xff);
					hirqreg |= (CMOK|ESEL);
				}

				status_type = 0;
			}
			break;

		case 0x60:    // set sector length
			CDROM_LOG(("%s:CD: Set sector length\n",   machine().describe_context()))

			switch (cr1 & 0xff)
			{
				case 0:
					sectlenin = 2048;
					break;
				case 1:
					sectlenin = 2336;
					break;
				case 2:
					sectlenin = 2340;
					break;
				case 3:
					sectlenin = 2352;
					break;
			}

			switch ((cr2>>8) & 0xff)
			{
				case 0:
					sectlenout = 2048;
					break;
				case 1:
					sectlenout = 2336;
					break;
				case 2:
					sectlenout = 2340;
					break;
				case 3:
					sectlenout = 2352;
					break;
			}
			hirqreg |= (CMOK|ESEL);
			cr_standard_return(cd_stat);
			status_type = 0;
			break;

		case 0x61:    // get sector data
			{
				uint32_t sectnum = cr4;
				uint32_t sectofs = cr2;
				uint32_t bufnum = cr3>>8;

				CDROM_LOG(("%s:CD: Get sector data (SN %d SO %d BN %d)\n",   machine().describe_context(), sectnum, sectofs, bufnum))

				if (bufnum >= MAX_FILTERS)
				{
					printf("CD: invalid buffer number\n");
					/* TODO: why this is happening? */
					cr_standard_return(CD_STAT_REJECT);
					hirqreg |= (CMOK|EHST);
					return;
				}

				if (partitions[bufnum].numblks < sectnum)
				{
					printf("CD: buffer is not full %08x %08x\n",partitions[bufnum].numblks,sectnum);
					cr_standard_return(CD_STAT_REJECT);
					hirqreg |= (CMOK|EHST);
					return;
				}

				cd_getsectoroffsetnum(bufnum, &sectofs, &sectnum);

				xfertype32 = XFERTYPE32_GETSECTOR;
				xferoffs = 0;
				xfersect = 0;
				xferdnum = 0;
				xfersectpos = sectofs;
				xfersectnum = sectnum;
				transpart = &partitions[bufnum];

				cd_stat |= CD_STAT_TRANS;
				cr_standard_return(cd_stat);
				hirqreg |= (CMOK|EHST|DRDY);
				status_type = 0;
			}
			break;

		case 0x62:    // delete sector data
			{
				uint32_t sectnum = cr4;
				uint32_t sectofs = cr2;
				uint32_t bufnum = cr3>>8;
				int32_t i;

				CDROM_LOG(("%s:CD: Delete sector data (SN %d SO %d BN %d)\n",   machine().describe_context(), sectnum, sectofs, bufnum))

				if (bufnum >= MAX_FILTERS)
				{
					printf("CD: invalid buffer number\n");
					/* TODO: why this is happening? */
					cr_standard_return(CD_STAT_REJECT);
					hirqreg |= (CMOK|EHST);
					return;
				}

				/* TODO: Phantasy Star 2 throws this one. */
				if (partitions[bufnum].numblks == 0)
				{
					printf("CD: buffer is already empty\n");
					cr_standard_return(CD_STAT_REJECT);
					hirqreg |= (CMOK|EHST);
					return;
				}

				cd_getsectoroffsetnum(bufnum, &sectofs, &sectnum);

				for (i = sectofs; i < (sectofs + sectnum); i++)
				{
					partitions[bufnum].size -= partitions[bufnum].blocks[i]->size;
					cd_free_block(partitions[bufnum].blocks[i]);
					partitions[bufnum].blocks[i] = (blockT *)nullptr;
					partitions[bufnum].bnum[i] = 0xff;
				}

				cd_defragblocks(&partitions[bufnum]);

				partitions[bufnum].numblks -= sectnum;

				if (freeblocks == 200)
				{
					sectorstore = 0;
				}

				cd_stat &= ~CD_STAT_TRANS;
				cr_standard_return(cd_stat);
				hirqreg |= (CMOK|EHST);
				status_type = 0;
			}
			break;

		case 0x63:    // get then delete sector data
			{
				uint32_t sectnum = cr4;
				uint32_t sectofs = cr2;
				uint32_t bufnum = cr3>>8;

				CDROM_LOG(("%s:CD: Get and delete sector data (SN %d SO %d BN %d)\n",   machine().describe_context(), sectnum, sectofs, bufnum))

				if (bufnum >= MAX_FILTERS)
				{
					printf("CD: invalid buffer number\n");
					/* TODO: why this is happening? */
					cr_standard_return(CD_STAT_REJECT);
					hirqreg |= (CMOK|EHST);
					return;
				}

				/* Yoshimoto Mahjong uses the REJECT status to verify when the data is ready. */
				if (partitions[bufnum].numblks < sectnum)
				{
					printf("CD: buffer is not full %08x %08x\n",partitions[bufnum].numblks,sectnum);
					cr_standard_return(CD_STAT_REJECT);
					hirqreg |= (CMOK|EHST);
					return;
				}

				cd_getsectoroffsetnum(bufnum, &sectofs, &sectnum);

				xfertype32 = XFERTYPE32_GETDELETESECTOR;
				xferoffs = 0;
				xfersect = 0;
				xferdnum = 0;
				xfersectpos = sectofs;
				xfersectnum = sectnum;
				transpart = &partitions[bufnum];

				cd_stat |= CD_STAT_TRANS;
				cr_standard_return(cd_stat);
				hirqreg |= (CMOK|EHST|DRDY);
				status_type = 0;
			}
			break;

		case 0x64:    // put sector data
			/* After Burner 2, Out Run, Fantasy Zone and Dungeon Master Nexus trips this */
			{
				uint32_t sectnum = cr4 & 0xff;
				uint32_t sectofs = cr2;
				uint32_t bufnum = cr3>>8;

				xfertype32 = XFERTYPE32_PUTSECTOR;

				/*TODO: eventual errors? */

				cd_getsectoroffsetnum(bufnum, &sectofs, &sectnum);

				cd_stat |= CD_STAT_TRANS;

				xferoffs = 0;
				xfersect = 0;
				xferdnum = 0;
				xfersectpos = sectofs;
				xfersectnum = sectnum;
				transpart = &partitions[bufnum];

				// allocate the blocks
				for (int i = xfersectpos; i < xfersectpos+xfersectnum; i++)
				{
					transpart->blocks[i] = cd_alloc_block(&transpart->bnum[i]);
					if(transpart->size == -1)
						transpart->size = 0;
					transpart->size += transpart->blocks[i]->size;
					transpart->numblks++;
				}
			}

			hirqreg |= (CMOK|DRDY);
			cr_standard_return(cd_stat);
			status_type = 0;
			break;

		case 0x65:
			popmessage("Move Sector data, contact MAMEdev");
			hirqreg |= (CMOK);
			break;

		case 0x66:    // copy sector data
			/* TODO: Sword & Sorcery / Riglord Saga 2 */
			{
				uint32_t src_filter = (cr3>>8)&0xff;
				uint32_t dst_filter = cr1&0xff;
				uint32_t sectnum = cr4 & 0xff;

				//cd_stat |= CD_STAT_TRANS;
				//transpart = &partitions[dst_filter];

				for (int i = 0; i < sectnum; i++)
				{
					// allocate the dst blocks
					partitions[dst_filter].blocks[i] = cd_alloc_block(&partitions[dst_filter].bnum[i]);
					if(partitions[dst_filter].size == -1)
						partitions[dst_filter].size = 0;
					partitions[dst_filter].size += partitions[dst_filter].blocks[i]->size;
					partitions[dst_filter].numblks++;

					//copy data
					for(int j = 0; j < sectlenin; j++)
						partitions[dst_filter].blocks[i]->data[j] = partitions[src_filter].blocks[i]->data[j];

					//deallocate the src blocks
					//partitions[src_filter].size -= partitions[src_filter].blocks[i]->size;
					//cd_free_block(partitions[src_filter].blocks[i]);
					//partitions[src_filter].blocks[i] = (blockT *)nullptr;
					//partitions[src_filter].bnum[i] = 0xff;
				}

			}

			hirqreg |= (CMOK|ECPY);
			cr_standard_return(cd_stat);
			status_type = 0;
			break;


		case 0x67:    // get copy error
			CDROM_LOG(("%s:CD: Get copy error\n",   machine().describe_context()))
			logerror("Get copy error\n");
			cr1 = cd_stat;
			cr2 = 0;
			cr3 = 0;
			cr4 = 0;
			hirqreg |= (CMOK);
			status_type = 0;
			break;

		case 0x70:    // change directory
			CDROM_LOG(("%s:CD: Change Directory\n",   machine().describe_context()))
			hirqreg |= (CMOK|EFLS);

			temp = (cr3&0xff)<<16;
			temp |= cr4;

			read_new_dir(temp);
			cr_standard_return(cd_stat);
			status_type = 0;
			break;

		case 0x71:    // Read directory entry
			CDROM_LOG(("%s:CD: Read Directory Entry\n",   machine().describe_context()))
//          uint32_t read_dir;

//          read_dir = ((cr3&0xff)<<16)|cr4;

			if((cr3 >> 8) < 0x24)
				cddevice = &filters[cr3 >> 8];
			else
				cddevice = (filterT *)nullptr;

			/* TODO:  */
			//read_new_dir(read_dir - 2);

			cr_standard_return(cd_stat);
			hirqreg |= (CMOK|EFLS);
			status_type = 0;
			break;

		case 0x72:    // Get file system scope
			CDROM_LOG(("CD: Get file system scope\n"))
			hirqreg |= (CMOK|EFLS);
			cr1 = cd_stat;
			cr2 = numfiles; // # of files in directory
			cr3 = 0x0100;   // report directory held
			cr4 = firstfile;    // first file id
			printf("%04x %04x %04x %04x\n",cr1,cr2,cr3,cr4);
			status_type = 0;
			break;

		case 0x73:    // Get File Info
			CDROM_LOG(("%s:CD: Get File Info\n",   machine().describe_context()))
			cd_stat |= CD_STAT_TRANS;
			cd_stat &= 0xff00;      // clear top byte of return value
			playtype = 0;
			cdda_repeat_count = 0;
			hirqreg |= (CMOK|DRDY);

			temp = (cr3&0xff)<<16;
			temp |= cr4;

			if (temp == 0xffffff)   // special
			{
				xfertype = XFERTYPE_FILEINFO_254;
				xfercount = 0;

				cr1 = cd_stat;
				cr2 = 0x5f4;
				cr3 = 0;
				cr4 = 0;
			}
			else
			{
				cr1 = cd_stat;
				cr2 = 6;    // 6 words for single file
							// first 4 bytes = FAD address
							// second 4 bytes = length
							// last 4 bytes:
							// - unit size
							// - gap size
							// - file #
							// attributes flags

				cr3 = 0;
				cr4 = 0;

				printf("%08x %08x\n",curdir[temp].firstfad,curdir[temp].length);
				// first 4 bytes = FAD
				finfbuf[0] = (curdir[temp].firstfad>>24)&0xff;
				finfbuf[1] = (curdir[temp].firstfad>>16)&0xff;
				finfbuf[2] = (curdir[temp].firstfad>>8)&0xff;
				finfbuf[3] = (curdir[temp].firstfad&0xff);
				// second 4 bytes = length of file
				finfbuf[4] = (curdir[temp].length>>24)&0xff;
				finfbuf[5] = (curdir[temp].length>>16)&0xff;
				finfbuf[6] = (curdir[temp].length>>8)&0xff;
				finfbuf[7] = (curdir[temp].length&0xff);
				finfbuf[8] = curdir[temp].interleave_gap_size;
				finfbuf[9] = curdir[temp].file_unit_size;
				finfbuf[10] = temp;
				finfbuf[11] = curdir[temp].flags;

				xfertype = XFERTYPE_FILEINFO_1;
				xfercount = 0;
			}
			CDROM_LOG(("   = %04x %04x %04x %04x %04x\n", hirqreg, cr1, cr2, cr3, cr4))
			status_type = 0;
			break;

		case 0x74:    // Read File
			CDROM_LOG(("%s:CD: Read File\n",   machine().describe_context()))
			uint16_t file_offset,file_filter,file_id,file_size;

			file_offset = ((cr1 & 0xff)<<8)|(cr2 & 0xff); /* correct? */
			file_filter = cr3 >> 8;
			file_id = ((cr3 & 0xff) << 16)|(cr4);
			file_size = ((curdir[file_id].length + sectlenin - 1) / sectlenin) - file_offset;

			cd_stat = CD_STAT_PLAY|0x80;    // set "cd-rom" bit
			cd_curfad = (curdir[file_id].firstfad + file_offset);
			fadstoplay = file_size;
			if(file_filter < 0x24)
				cddevice = &filters[file_filter];
			else
				cddevice = (filterT *)nullptr;

			printf("Read file %08x (%08x %08x) %02x %d\n",curdir[file_id].firstfad,cd_curfad,fadstoplay,file_filter,sectlenin);

			cr_standard_return(cd_stat);

			oddframe = 0;
			in_buffer = 0;

			playtype = 1;

			hirqreg |= (CMOK|EHST);
			status_type = 0;
			break;

		case 0x75:
			CDROM_LOG(("%s:CD: Abort File\n",   machine().describe_context()))
			// bios expects "2bc" mask to work against this
			hirqreg |= (CMOK|EFLS);
			sectorstore = 0;
			xfertype32 = XFERTYPE32_INVALID;
			xferdnum = 0;
			if(((cd_stat & 0x0f00) != CD_STAT_NODISC) && ((cd_stat & 0x0f00) != CD_STAT_OPEN))
				cd_stat = CD_STAT_PAUSE;    // force to pause
			cr_standard_return(cd_stat);
			status_type = 0;
			break;

		case 0xe0:    // appears to be copy protection check.  needs only to return OK.
			CDROM_LOG(("%s:CD: Verify copy protection\n",   machine().describe_context()))
			if(((cd_stat & 0x0f00) != CD_STAT_NODISC) && ((cd_stat & 0x0f00) != CD_STAT_OPEN))
				cd_stat = CD_STAT_PAUSE;
//          cr1 = cd_stat;  // necessary to pass
//          cr2 = 0x4;
//          hirqreg |= (CMOK|EFLS|CSCT);
			sectorstore = 1;
			hirqreg = 0xfc5;
			cr_standard_return(cd_stat);
			status_type = 0;
			break;

		case 0xe1:    // get disc region
			CDROM_LOG(("%s:CD: Get disc region\n",   machine().describe_context()))
			if(cd_stat != CD_STAT_NODISC && cd_stat != CD_STAT_OPEN)
				cd_stat = CD_STAT_PAUSE;
			cr1 = cd_stat;  // necessary to pass
			cr2 = 0x4;      // (must return this value to pass bios checks)
			cr3 = 0;
			cr4 = 0;
			hirqreg |= (CMOK);
//          cr_standard_return(cd_stat);
			status_type = 0;
			break;

		default:
			CDROM_LOG(("CD: Unknown command %04x\n", cr1>>8))
			popmessage("CD Block unknown command %02x, contact MAMEdev",cr1>>8);
			hirqreg |= (CMOK);
			break;
	}

	if(status_type == 1)
	{
		prev_cr1 = cr1;
		prev_cr2 = cr2;
		prev_cr3 = cr3;
		prev_cr4 = cr4;
	}
}

TIMER_DEVICE_CALLBACK_MEMBER( saturn_state::stv_sh1_sim )
{
	if((cmd_pending == 0xf) && (!(hirqreg & CMOK)))
		cd_exec_command();
}

TIMER_DEVICE_CALLBACK_MEMBER( saturn_state::stv_sector_cb )
{
	//sector_timer->reset();

	//popmessage("%08x %08x %d %d",cd_curfad,fadstoplay,cmd_pending,cd_speed);

	cd_playdata();

	if(cdrom_get_track_type(cdrom, cdrom_get_track(cdrom, cd_curfad)) == CD_TRACK_AUDIO)
		sector_timer->adjust(attotime::from_hz(75));    // 75 sectors / second = 150kBytes/second (cdda track ignores cd_speed setting)
	else
		sector_timer->adjust(attotime::from_hz(75*cd_speed));   // 75 / 150 sectors / second = 150 / 300kBytes/second

	/* TODO: doesn't boot if a disk isn't in? */
	/* TODO: Check out when this really happens. (Daytona USA original version definitely wants it to be on).*/
	//if(((cd_stat & 0x0f00) != CD_STAT_NODISC) && ((cd_stat & 0x0f00) != CD_STAT_OPEN))
		hirqreg |= SCDQ;

	if(cd_stat & CD_STAT_PERI)
	{
		cr_standard_return(cd_stat);
	}
}

// global functions
void saturn_state::stvcd_reset( void )
{
	int32_t i, j;

	hirqmask = 0xffff;
	hirqreg = 0xffff;
	cr1 = 'C';
	cr2 = ('D'<<8) | 'B';
	cr3 = ('L'<<8) | 'O';
	cr4 = ('C'<<8) | 'K';
	cd_stat = CD_STAT_PAUSE;
	cd_stat |= CD_STAT_PERI;
	cur_track = 0xff;

	curdir.clear();

	xfertype = XFERTYPE_INVALID;
	xfertype32 = XFERTYPE32_INVALID;

	// reset flag vars
	buffull = sectorstore = 0;

	freeblocks = 200;

	sectlenin = sectlenout = 2048;

	lastbuf = 0xff;

	// reset buffer partitions
	for (i = 0; i < MAX_FILTERS; i++)
	{
		partitions[i].size = -1;
		partitions[i].numblks = 0;

		for (j = 0; j < MAX_BLOCKS; j++)
		{
			partitions[i].blocks[j] = (blockT *)nullptr;
			partitions[i].bnum[j] = 0xff;
		}
	}

	// reset blocks
	for (i = 0; i < MAX_BLOCKS; i++)
	{
		blocks[i].size = -1;
		memset(&blocks[i].data, 0, CD_MAX_SECTOR_DATA);
	}

	// open device
	if (cdrom)
	{
		cdrom_close(cdrom);
		cdrom = (cdrom_file *)nullptr;
	}

	cdrom_image_device *cddevice = machine().device<cdrom_image_device>("cdrom");
	if (cddevice!=nullptr)
	{
		// MESS case
		cdrom = cddevice->get_cdrom_file();
	}
	else
	{
		// MAME case
		cdrom = cdrom_open(machine().rom_load().get_disk_handle("cdrom"));
	}

	machine().device<cdda_device>("cdda")->set_cdrom(cdrom);

	if (cdrom)
	{
		CDROM_LOG(("Opened CD-ROM successfully, reading root directory\n"))
		read_new_dir(0xffffff);    // read root directory
	}
	else
	{
		cd_stat = CD_STAT_NODISC;
	}

	cd_speed = 2;
	cdda_repeat_count = 0;
	tray_is_closed = 1;

	sector_timer = machine().device<timer_device>("sector_timer");
	sector_timer->adjust(attotime::from_hz(150));   // 150 sectors / second = 300kBytes/second
	sh1_timer = machine().device<timer_device>("sh1_cmd");
}

saturn_state::blockT *saturn_state::cd_alloc_block(uint8_t *blknum)
{
	int32_t i;

	// search the 200 available blocks for a free one
	for (i = 0; i < 200; i++)
	{
		if (blocks[i].size == -1)
		{
			freeblocks--;
			if (freeblocks <= 0)
			{
				buffull = 1;
				printf("buffull in cd_alloc_block\n");
			}

			blocks[i].size = sectlenin;
			*blknum = i;

			CDROM_LOG(("CD: allocating block %d, size %x\n", i, sectlenin))

			return &blocks[i];
		}
	}

	buffull = 1;
	return (blockT *)nullptr;
}

void saturn_state::cd_free_block(blockT *blktofree)
{
	int32_t i;

	CDROM_LOG(("cd_free_block: %x\n", (uint32_t)(uintptr_t)blktofree))

	if(blktofree == nullptr)
	{
		return;
	}

	for (i = 0; i < 200; i++)
	{
		if (&blocks[i] == blktofree)
		{
			CDROM_LOG(("CD: freeing block %d\n", i))
		}
	}

	blktofree->size = -1;
	freeblocks++;
	buffull = 0;
	hirqreg &= ~BFUL;
}

void saturn_state::cd_getsectoroffsetnum(uint32_t bufnum, uint32_t *sectoffs, uint32_t *sectnum)
{
	if (*sectoffs == 0xffff)
	{
		// last sector
		printf("CD: Don't know how to handle offset ffff\n");
	}
	else if (*sectnum == 0xffff)
	{
		*sectnum = partitions[bufnum].numblks - *sectoffs;
	}
}

void saturn_state::cd_defragblocks(partitionT *part)
{
	uint32_t i, j;
	blockT *temp;
	uint8_t temp2;

	for (i = 0; i < (MAX_BLOCKS-1); i++)
	{
		for (j = i+1; j < MAX_BLOCKS; j++)
		{
			if ((part->blocks[i] == (blockT *)nullptr) && (part->blocks[j] != (blockT *)nullptr))
			{
				temp = part->blocks[i];
				part->blocks[i] = part->blocks[j];
				part->blocks[j] = temp;

				temp2 = part->bnum[i];
				part->bnum[i] = part->bnum[j];
				part->bnum[j] = temp2;
			}
		}
	}
}

uint16_t saturn_state::cd_readWord(uint32_t addr)
{
	uint16_t rv;

	switch (addr & 0xffff)
	{
		case 0x0008:    // read HIRQ register
		case 0x000a:
		case 0x8008:
		case 0x800a:
			rv = hirqreg;

			rv &= ~DCHG;    // always clear bit 6 (tray open)

			if (buffull) rv |= BFUL; else rv &= ~BFUL;
			if (sectorstore) rv |= CSCT; else rv &= ~CSCT;

			hirqreg = rv;

//          CDROM_LOG(("RW HIRQ: %04x\n", rv))

			return rv;

		case 0x000c:
		case 0x000e:
		case 0x800c:
		case 0x800e:
//          CDROM_LOG(("RW HIRM: %04x\n", hirqmask))
			printf("RW HIRM: %04x\n", hirqmask);
			return hirqmask;

		case 0x0018:
		case 0x001a:
		case 0x8018:
		case 0x801a:
//          CDROM_LOG(("RW CR1: %04x\n", cr1))
			return cr1;

		case 0x001c:
		case 0x001e:
		case 0x801c:
		case 0x801e:
//          CDROM_LOG(("RW CR2: %04x\n", cr2))
			return cr2;

		case 0x0020:
		case 0x0022:
		case 0x8020:
		case 0x8022:
//          CDROM_LOG(("RW CR3: %04x\n", cr3))
			return cr3;

		case 0x0024:
		case 0x0026:
		case 0x8024:
		case 0x8026:
//          CDROM_LOG(("RW CR4: %04x\n", cr4))
			//popmessage("%04x %04x %04x %04x",cr1,cr2,cr3,cr4);
			cmd_pending = 0;
			cd_stat |= CD_STAT_PERI;
			return cr4;

		case 0x8000:
			rv = 0xffff;
			switch (xfertype)
			{
				case XFERTYPE_TOC:
					rv = tocbuf[xfercount]<<8 | tocbuf[xfercount+1];

					xfercount += 2;
					xferdnum += 2;

					if (xfercount > 102*4)
					{
						xfercount = 0;
						xfertype = XFERTYPE_INVALID;
					}
					break;

				case XFERTYPE_FILEINFO_1:
					rv = finfbuf[xfercount]<<8 | finfbuf[xfercount+1];
					xfercount += 2;
					xferdnum += 2;

					if (xfercount > 6*2)
					{
						xfercount = 0;
						xfertype = XFERTYPE_INVALID;
					}
					break;

				case XFERTYPE_FILEINFO_254: // Lunar 2
					if((xfercount % (6 * 2)) == 0)
					{
						uint32_t temp = 2 + (xfercount / (0x6 * 2));

						// first 4 bytes = FAD
						finfbuf[0] = (curdir[temp].firstfad>>24)&0xff;
						finfbuf[1] = (curdir[temp].firstfad>>16)&0xff;
						finfbuf[2] = (curdir[temp].firstfad>>8)&0xff;
						finfbuf[3] = (curdir[temp].firstfad&0xff);
						// second 4 bytes = length of file
						finfbuf[4] = (curdir[temp].length>>24)&0xff;
						finfbuf[5] = (curdir[temp].length>>16)&0xff;
						finfbuf[6] = (curdir[temp].length>>8)&0xff;
						finfbuf[7] = (curdir[temp].length&0xff);
						finfbuf[8] = curdir[temp].interleave_gap_size;
						finfbuf[9] = curdir[temp].file_unit_size;
						finfbuf[10] = temp;
						finfbuf[11] = curdir[temp].flags;
					}

					rv = finfbuf[xfercount % (6 * 2)]<<8 | finfbuf[(xfercount % (6 * 2)) +1];

					xfercount += 2;
					xferdnum += 2;

					if (xfercount > (254 * 6 * 2))
					{
						xfercount = 0;
						xfertype = XFERTYPE_INVALID;
					}
					break;

				case XFERTYPE_SUBQ:
					rv = subqbuf[xfercount]<<8 | subqbuf[xfercount+1];

					xfercount += 2;
					xferdnum += 2;

					if (xfercount > 5*2)
					{
						xfercount = 0;
						xfertype = XFERTYPE_INVALID;
					}
					break;


				case XFERTYPE_SUBRW:
					rv = subrwbuf[xfercount]<<8 | subrwbuf[xfercount+1];

					xfercount += 2;
					xferdnum += 2;

					if (xfercount > 12*2)
					{
						xfercount = 0;
						xfertype = XFERTYPE_INVALID;
					}
					break;

				default:
					printf("STVCD: Unhandled xfer type %d\n", (int)xfertype);
					rv = 0;
					break;
			}

			return rv;

		default:
			CDROM_LOG(("CD: RW %08x\n", addr))
			return 0xffff;
	}

}

uint32_t saturn_state::cd_readLong(uint32_t addr)
{
	uint32_t rv = 0;

	switch (addr & 0xffff)
	{
		case 0x8000:
			switch (xfertype32)
			{
				case XFERTYPE32_GETSECTOR:
				case XFERTYPE32_GETDELETESECTOR:
					// make sure we have sectors left
					if (xfersect < xfersectnum)
					{
						// get next longword
						rv = (transpart->blocks[xfersectpos+xfersect]->data[xferoffs + 0]<<24) |
								(transpart->blocks[xfersectpos+xfersect]->data[xferoffs + 1]<<16) |
								(transpart->blocks[xfersectpos+xfersect]->data[xferoffs + 2]<<8)  |
								(transpart->blocks[xfersectpos+xfersect]->data[xferoffs + 3]<<0);

						xferdnum += 4;
						xferoffs += 4;

						// did we run out of sector?
						if (xferoffs >= transpart->blocks[xfersect]->size)
						{
							CDROM_LOG(("CD: finished xfer of block %d of %d\n", xfersect+1, xfersectnum))

							xferoffs = 0;
							xfersect++;
						}
					}
					else    // sectors are done, kill 'em all if we can
					{
						if (xfertype32 == XFERTYPE32_GETDELETESECTOR)
						{
							int32_t i;

							CDROM_LOG(("Killing sectors in done\n"))

							// deallocate the blocks
							for (i = xfersectpos; i < xfersectpos+xfersectnum; i++)
							{
								cd_free_block(transpart->blocks[i]);
								transpart->blocks[i] = (blockT *)nullptr;
								transpart->bnum[i] = 0xff;
							}

							// defrag what's left
							cd_defragblocks(transpart);

							// clean up our state
							transpart->size -= xferdnum;
							transpart->numblks -= xfersectnum;

							/* TODO: is this correct? */
							xfertype32 = XFERTYPE32_INVALID;
						}
					}
					break;

				default:
					printf("CD: unhandled 32-bit transfer type\n");
					break;
			}

			return rv;

		default:
			CDROM_LOG(("CD: RL %08x\n", addr))
			return 0xffff;
	}
}

void saturn_state::cd_writeLong(uint32_t addr, uint32_t data)
{
	switch (addr & 0xffff)
	{
		case 0x8000:
			switch (xfertype32)
			{
				case XFERTYPE32_PUTSECTOR:
					// make sure we have sectors left
					if (xfersect < xfersectnum)
					{
						// get next longword
						transpart->blocks[xfersectpos+xfersect]->data[xferoffs + 0] = (data >> 24) & 0xff;
						transpart->blocks[xfersectpos+xfersect]->data[xferoffs + 1] = (data >> 16) & 0xff;
						transpart->blocks[xfersectpos+xfersect]->data[xferoffs + 2] = (data >> 8) & 0xff;
						transpart->blocks[xfersectpos+xfersect]->data[xferoffs + 3] = (data >> 0) & 0xff;

						xferdnum += 4;
						xferoffs += 4;

						// did we run out of sector?
						if (xferoffs >= transpart->blocks[xfersectpos+xfersect]->size)
						{
							CDROM_LOG(("CD: finished xfer of block %d of %d\n", xfersect+1, xfersectnum))

							xferoffs = 0;
							xfersect++;
						}
					}
					else    // sectors are done
					{
						/* Virtual On doesnt want this to be resetted. */
						//xfertype32 = XFERTYPE32_INVALID;
					}
					break;

				default:
					printf("CD: unhandled 32-bit transfer type write\n");
					break;
			}
			break;

		default:
			break;
	}
}

void saturn_state::cd_writeWord(uint32_t addr, uint16_t data)
{
	switch(addr & 0xffff)
	{
	case 0x0008:
	case 0x000a:
	case 0x8008:
	case 0x800a:
//      CDROM_LOG(("%s:WW HIRQ: %04x & %04x => %04x\n", machine().describe_context(), hirqreg, data, hirqreg & data))
		hirqreg &= data;
		return;
	case 0x000c:
	case 0x000e:
	case 0x800c:
	case 0x800e:
//      CDROM_LOG(("WW HIRM: %04x => %04x\n", hirqmask, data))
		printf("WW HIRM: %04x => %04x\n", hirqmask, data);
		hirqmask = data;
		return;
	case 0x0018:
	case 0x001a:
	case 0x8018:
	case 0x801a:
//      CDROM_LOG(("WW CR1: %04x\n", data))
		cr1 = data;
		cd_stat &= ~CD_STAT_PERI;
		cmd_pending |= 1;
		sh1_timer->adjust(attotime::never);
		break;
	case 0x001c:
	case 0x001e:
	case 0x801c:
	case 0x801e:
//      CDROM_LOG(("WW CR2: %04x\n", data))
		cr2 = data;
		cmd_pending |= 2;
		break;
	case 0x0020:
	case 0x0022:
	case 0x8020:
	case 0x8022:
//      CDROM_LOG(("WW CR3: %04x\n", data))
		cr3 = data;
		cmd_pending |= 4;
		break;
	case 0x0024:
	case 0x0026:
	case 0x8024:
	case 0x8026:
//      CDROM_LOG(("WW CR4: %04x\n", data))
		cr4 = data;
		cmd_pending |= 8;
		sh1_timer->adjust(attotime::from_hz(get_timing_command()));
		break;
	default:
		CDROM_LOG(("CD: WW %08x %04x\n", addr, data))
		break;
	}
}

READ32_MEMBER( saturn_state::stvcd_r )
{
	uint32_t rv = 0;

	offset <<= 2;

	switch (offset)
	{
		case 0x88008:
		case 0x8800a:
		case 0x8800c:
		case 0x8800e:
		case 0x88018:
		case 0x8801a:
		case 0x8801c:
		case 0x8801e:
		case 0x88020:
		case 0x88022:
		case 0x88024:
		case 0x88026:
		case 0x90008:
		case 0x9000a:
		case 0x9000c:
		case 0x9000e:
		case 0x90018:
		case 0x9001a:
		case 0x9001c:
		case 0x9001e:
		case 0x90020:
		case 0x90022:
		case 0x90024:
		case 0x90026:
			rv = cd_readWord(offset);
			return rv<<16;

		case 0x98000:
		case 0x18000:
			if (mem_mask == 0xffffffff)
			{
				rv = cd_readLong(offset);
			}
			else if (mem_mask == 0xffff0000)
			{
				rv = cd_readWord(offset)<<16;
			}
			else if (mem_mask == 0x0000ffff)
			{
				rv = cd_readWord(offset);
			}
			else
			{
				osd_printf_error("CD: Unknown data buffer read @ mask = %08x\n", mem_mask);
			}

			break;

		default:
			printf("Unknown CD read %x\n", offset);
			break;
	}

	return rv;
}

WRITE32_MEMBER( saturn_state::stvcd_w )
{
	offset <<= 2;

	switch (offset)
	{
		case 0x18000:
			if (mem_mask == 0xffffffff)
				cd_writeLong(offset, data);
			else
				printf("CD: Unknown data buffer write @ mask = %08x\n", mem_mask);
			break;

		case 0x88008:
		case 0x8800a:
		case 0x8800c:
		case 0x8800e:
		case 0x88018:
		case 0x8801a:
		case 0x8801c:
		case 0x8801e:
		case 0x88020:
		case 0x88022:
		case 0x88024:
		case 0x88026:
		case 0x90008:
		case 0x9000a:
		case 0x9000c:
		case 0x9000e:
		case 0x90018:
		case 0x9001a:
		case 0x9001c:
		case 0x9001e:
		case 0x90020:
		case 0x90022:
		case 0x90024:
		case 0x90026:
			cd_writeWord(offset, data>>16);
			break;

		default:
			printf("Unknown CD write %x @ %x\n", data, offset);
			//xferdnum = 0x8c00;
			break;
	}
}

// iso9660 parsing
void saturn_state::read_new_dir(uint32_t fileno)
{
	int foundpd, i;
	uint32_t cfad;//, dirfad;
	uint8_t sect[2048];

	if (fileno == 0xffffff)
	{
		cfad = 166;     // first sector of directory as per iso9660 specs

		foundpd = 0;    // search for primary vol. desc
		while ((!foundpd) && (cfad < 200))
		{
			if(sectlenin != 2048)
				popmessage("Sector Length %d, contact MAMEdev (0)",sectlenin);

			memset(sect, 0, 2048);
			cd_readblock(cfad++, sect);

			if ((sect[1] == 'C') && (sect[2] == 'D') && (sect[3] == '0') && (sect[4] == '0') && (sect[5] == '1'))
			{
				switch (sect[0])
				{
					case 0: // boot record
						break;

					case 1: // primary vol. desc
						foundpd = 1;
						break;

					case 2: // secondary vol desc
						break;

					case 3: // vol. section descriptor
						break;

					case 0xff:
						cfad = 200;
						break;
				}
			}
		}

		// got primary vol. desc.
		if (foundpd)
		{
			//dirfad = sect[140] | (sect[141]<<8) | (sect[142]<<16) | (sect[143]<<24);
			//dirfad += 150;

			// parse root entry
			curroot.firstfad = sect[158] | (sect[159]<<8) | (sect[160]<<16) | (sect[161]<<24);
			curroot.firstfad += 150;
			curroot.length = sect[166] | (sect[167]<<8) | (sect[168]<<16) | (sect[169]<<24);
			curroot.flags = sect[181];
			for (i = 0; i < sect[188]; i++)
			{
				curroot.name[i] = sect[189+i];
			}
			curroot.name[i] = '\0'; // terminate

			// easy to fix, but make sure we *need* to first
			if (curroot.length > MAX_DIR_SIZE)
			{
				osd_printf_error("ERROR: root directory too big (%d)\n", curroot.length);
			}

			// done with all that, read the root directory now
			make_dir_current(curroot.firstfad);
		}
	}
	else
	{
		if (curdir[fileno].length > MAX_DIR_SIZE)
		{
			osd_printf_error("ERROR: new directory too big (%d)!\n", curdir[fileno].length);
		}
		make_dir_current(curdir[fileno].firstfad);
	}
}

// makes the directory pointed to by FAD current
void saturn_state::make_dir_current(uint32_t fad)
{
	uint32_t i;
	uint32_t nextent, numentries;
	std::vector<uint8_t> sect(MAX_DIR_SIZE);
	direntryT *curentry;

	memset(&sect[0], 0, MAX_DIR_SIZE);
	if(sectlenin != 2048)
		popmessage("Sector Length %d, contact MAMEdev (1)",sectlenin);

	for (i = 0; i < (curroot.length/2048); i++)
	{
		cd_readblock(fad+i, &sect[2048*i]);
	}

	nextent = 0;
	numentries = 0;
	while (nextent < MAX_DIR_SIZE)
	{
		if (sect[nextent])
		{
			nextent += sect[nextent];
			numentries++;
		}
		else
		{
			nextent = MAX_DIR_SIZE;
		}
	}

	curdir.resize(numentries);
	curentry = &curdir[0];
	numfiles = numentries;

	nextent = 0;
	while (numentries)
	{
		// [0] record size
		// [1] xa record size
		// [2-5] lba
		// [6-9] (lba?)
		// [10-13] size
		// [14-17] (size?)
		// [18] year
		// [19] month
		// [20] day
		// [21] hour
		// [22] minute
		// [23] second
		// [24] gmt offset
		// [25] flags
		// [26] file unit size
		// [27] interleave gap size
		// [28-29] volume sequencer number
		// [30-31] (volume sequencer number?)
		// [32] name character size
		// [33+ ...] file name

		curentry->record_size = sect[nextent+0];
		curentry->xa_record_size = sect[nextent+1];
		curentry->firstfad = sect[nextent+2] | (sect[nextent+3]<<8) | (sect[nextent+4]<<16) | (sect[nextent+5]<<24);
		curentry->firstfad += 150;
		curentry->length = sect[nextent+10] | (sect[nextent+11]<<8) | (sect[nextent+12]<<16) | (sect[nextent+13]<<24);
		curentry->year = sect[nextent+18];
		curentry->month = sect[nextent+19];
		curentry->day = sect[nextent+20];
		curentry->hour = sect[nextent+21];
		curentry->minute = sect[nextent+22];
		curentry->second = sect[nextent+23];
		curentry->gmt_offset = sect[nextent+24];
		curentry->flags = sect[nextent+25];
		curentry->file_unit_size = sect[nextent+26];
		curentry->interleave_gap_size = sect[nextent+27];
		curentry->volume_sequencer_number = sect[nextent+28] | (sect[nextent+29] << 8);

		for (i = 0; i < sect[nextent+32]; i++)
		{
			curentry->name[i] = sect[nextent+33+i];
		}
		curentry->name[i] = '\0';   // terminate
		//printf("%08x %08x %s %d/%d/%d\n",curentry->firstfad,curentry->length,curentry->name,curentry->year,curentry->month,curentry->day);

		nextent += sect[nextent];
		curentry++;
		numentries--;
	}

	for (i = 0; i < numfiles; i++)
	{
		if (!(curdir[i].flags & 0x02))
		{
			firstfile = i;
			i = numfiles;
		}
	}
}

void saturn_state::stvcd_exit( void )
{
	curdir.clear();

	if (cdrom)
	{
		cdrom_image_device *cddevice = machine().device<cdrom_image_device>("cdrom");
		if (cddevice==nullptr)
		{
			cdrom_close(cdrom);
		}
		cdrom = (cdrom_file *)nullptr;
	}
}

void saturn_state::cd_readTOC(void)
{
	int i, ntrks, tocptr, fad;

	xfertype = XFERTYPE_TOC;
	xfercount = 0;

	if (cdrom)
	{
		ntrks = cdrom_get_last_track(cdrom);
	}
	else
	{
		ntrks = 0;
	}

	// data format for Saturn TOC:
	// no header.
	// 4 bytes per track
	// top nibble of first byte is CTRL info
	// low nibble is ADR
	// next 3 bytes are FAD address (LBA + 150)
	// there are always 99 track entries (0-98)
	// unused tracks are ffffffff.
	// entries 99-101 are metadata

	tocptr = 0; // starting point of toc entries

	for (i = 0; i < ntrks; i++)
	{
		if (cdrom)
		{
			//tocbuf[tocptr] = sega_cdrom_get_adr_control(cdrom, i);
			//HACK: ddsom does not enter ingame with the line above!
			tocbuf[tocptr] = cdrom_get_adr_control(cdrom, i)<<4 | 0x01;
		}
		else
		{
			tocbuf[tocptr] = 0xff;
		}

		if (cdrom)
		{
			fad = cdrom_get_track_start(cdrom, i) + 150;

			tocbuf[tocptr+1] = (fad>>16)&0xff;
			tocbuf[tocptr+2] = (fad>>8)&0xff;
			tocbuf[tocptr+3] = fad&0xff;
		}
		else
		{
			tocbuf[tocptr+1] = 0xff;
			tocbuf[tocptr+2] = 0xff;
			tocbuf[tocptr+3] = 0xff;
		}

		tocptr += 4;
	}

	// fill in the rest
	for ( ; i < 99; i++)
	{
		tocbuf[tocptr] = 0xff;
		tocbuf[tocptr+1] = 0xff;
		tocbuf[tocptr+2] = 0xff;
		tocbuf[tocptr+3] = 0xff;

		tocptr += 4;
	}

	// tracks 99-101 are special metadata
	// $$$FIXME: what to do with the address info for these?
	tocptr = 99 * 4;
	tocbuf[tocptr] = tocbuf[0]; // get ctrl/adr from first track
	tocbuf[tocptr+1] = 1;   // first track's track #
	tocbuf[tocptr+2] = 0;
	tocbuf[tocptr+3] = 0;

	tocbuf[tocptr+4] = tocbuf[(ntrks-1)*4]; // ditto for last track
	tocbuf[tocptr+5] = ntrks;   // last track's track #
	tocbuf[tocptr+6] = 0;
	tocbuf[tocptr+7] = 0;

	// get total disc length (start of lead-out)
	fad = cdrom_get_track_start(cdrom, 0xaa) + 150;

	tocbuf[tocptr+8] = tocbuf[0];
	tocbuf[tocptr+9]  = (fad>>16)&0xff;
	tocbuf[tocptr+10] = (fad>>8)&0xff;
	tocbuf[tocptr+11] = fad&0xff;
}

saturn_state::partitionT *saturn_state::cd_filterdata(filterT *flt, int trktype, uint8_t *p_ok)
{
	int match, keepgoing;
	partitionT *filterprt;

	CDROM_LOG(("cd_filterdata, trktype %d\n", trktype))
	match = 1;
	keepgoing = 2;
	lastbuf = flt->condtrue;

	// loop on the filters
	do
	{
		// FAD range check?
		/* according to an obscure document note, this switches the filter connector to be false if the range fails ... I think ... */
		if (flt->mode & 0x40)
		{
			if ((cd_curfad < flt->fad) || (cd_curfad > (flt->fad + flt->range)))
			{
				printf("curfad reject %08x %08x %08x %08x\n",cd_curfad,fadstoplay,flt->fad,flt->fad+flt->range);
				match = 0;
				//lastbuf = flt->condfalse;
				//flt = &filters[lastbuf];
			}
		}

		if ((trktype != CD_TRACK_AUDIO) && (curblock.data[15] == 2))
		{
			if (flt->mode & 1)  // file number
			{
				if (curblock.fnum != flt->fid)
				{
					printf("fnum reject\n");
					match = 0;
				}
			}

			if (flt->mode & 2)  // channel number
			{
				if (curblock.chan != flt->chan)
				{
					printf("channel number reject\n");
					match = 0;
				}
			}

			if (flt->mode & 4)  // sub mode
			{
				if((curblock.subm & flt->smmask) != flt->smval)
				{
					printf("sub mode reject\n");
					match = 0;
				}
			}

			if (flt->mode & 8)  // coding information
			{
				if((curblock.cinf & flt->cimask) != flt->cival)
				{
					printf("coding information reject\n");
					match = 0;
				}
			}

			if (flt->mode & 0x10)   // reverse subheader conditions
			{
				match ^= 1;
			}
		}

		if (match)
		{
			//lastbuf = flt->condtrue;
			//filterprt = &partitions[lastbuf];
			// we're done
			keepgoing = 0;
		}
		else
		{
			lastbuf = flt->condfalse;

			// reject sector if no match on either connector
			if ((lastbuf == 0xff) || (keepgoing == 0))
			{
				*p_ok = 0;
				return (partitionT *)nullptr;
			}

			// try again using the filter that was on the "false" connector
			flt = &filters[lastbuf];
			match = 1;

			// and exit if we fail
			keepgoing--;
		}
	} while (keepgoing);

	filterprt = &partitions[lastbuf];

	// try to allocate a block
	filterprt->blocks[filterprt->numblks] = cd_alloc_block(&filterprt->bnum[filterprt->numblks]);

	// did the allocation succeed?
	if (filterprt->blocks[filterprt->numblks] == (blockT *)nullptr)
	{
		*p_ok = 0;
		return (partitionT *)nullptr;
	}

	// copy working block to the newly allocated one
	memcpy(filterprt->blocks[filterprt->numblks], &curblock, sizeof(blockT));

	// and massage the data format a bit
	switch  (curblock.size)
	{
		case 2048:  // user data
			if (curblock.data[15] == 2)
			{
				// mode 2
				memcpy(&filterprt->blocks[filterprt->numblks]->data[0], &curblock.data[24], curblock.size);
			}
			else
			{
				// mode 1
				memcpy(&filterprt->blocks[filterprt->numblks]->data[0], &curblock.data[16], curblock.size);
			}
			break;

		case 2324:  // Mode 2 Form 2 data
			memcpy(&filterprt->blocks[filterprt->numblks]->data[0], &curblock.data[24], curblock.size);
			break;

		case 2336:  // Mode 2 Form 2 skip sync/header
			memcpy(&filterprt->blocks[filterprt->numblks]->data[0], &curblock.data[16], curblock.size);
			break;

		case 2340:  // Mode 2 Form 2 skip sync only
			memcpy(&filterprt->blocks[filterprt->numblks]->data[0], &curblock.data[12], curblock.size);
			break;

		case 2352:  // want all data, it's already done, so don't do it again :)
			break;
	}

	// update the status of the partition
	if (filterprt->size == -1)
		filterprt->size = 0;

	filterprt->size += filterprt->blocks[filterprt->numblks]->size;
	filterprt->numblks++;

	*p_ok = 1;
	return filterprt;
}

// read a single sector off the CD, applying the current filter(s) as necessary
saturn_state::partitionT *saturn_state::cd_read_filtered_sector(int32_t fad, uint8_t *p_ok)
{
	int trktype;

	if ((cddevice != nullptr) && (!buffull))
	{
		// find out the track's type
		trktype = cdrom_get_track_type(cdrom, cdrom_get_track(cdrom, fad-150));

		// now get a raw 2352 byte sector - if it's mode 1, get mode1_raw
		if ((trktype == CD_TRACK_MODE1) || (trktype == CD_TRACK_MODE1_RAW))
		{
			cdrom_read_data(cdrom, fad-150, curblock.data, CD_TRACK_MODE1_RAW);
		}
		else if (trktype != CD_TRACK_AUDIO) // if not audio it must be mode 2 so get mode2_raw
		{
			cdrom_read_data(cdrom, fad-150, curblock.data, CD_TRACK_MODE2_RAW);
		}
		else
		{
			cdrom_read_data(cdrom, fad-150, curblock.data, CD_TRACK_AUDIO);
		}

		curblock.size = sectlenin;
		curblock.FAD = fad;

		// if track is Mode 2, get the subheader values
		if ((trktype != CD_TRACK_AUDIO) && (curblock.data[15] == 2))
		{
			curblock.chan = curblock.data[17];
			curblock.fnum = curblock.data[16];
			curblock.subm = curblock.data[18];
			curblock.cinf = curblock.data[19];

			// if it's Form 2, the length is actually 2324 bytes
			if (curblock.subm & 0x20)
			{
				curblock.size = 2324;
			}
		}

		return cd_filterdata(cddevice, trktype, &*p_ok);
	}

	*p_ok = 0;
	return (partitionT *)nullptr;
}

// loads in data set up by a CD-block PLAY command
void saturn_state::cd_playdata( void )
{
	if ((cd_stat & 0x0f00) == CD_STAT_SEEK)
	{
		int32_t fad_diff;
		//printf("PRE %08x %08x %08x %d\n",cd_curfad,cd_fad_seek,cd_stat,cd_fad_seek - cd_curfad);

		fad_diff = (cd_fad_seek - cd_curfad);

		/* Zero Divide wants this TODO: timings. */
		if(fad_diff > (750*cd_speed))
		{
			//printf("PRE FFWD %08x %08x %08x %d %d\n",cd_curfad,cd_fad_seek,cd_stat,cd_fad_seek - cd_curfad,750*cd_speed);
			cd_curfad += (750*cd_speed);
			//printf("POST FFWD %08x %08x %08x %d %d\n",cd_curfad,cd_fad_seek,cd_stat,cd_fad_seek - cd_curfad, 750*cd_speed);
		}
		else if(fad_diff < (-750*cd_speed))
		{
			//printf("PRE REW %08x %08x %08x %d %d\n",cd_curfad,cd_fad_seek,cd_stat,cd_fad_seek - cd_curfad, -750*cd_speed);
			cd_curfad -= (750*cd_speed);
			//printf("POST REW %08x %08x %08x %d %d\n",cd_curfad,cd_fad_seek,cd_stat,cd_fad_seek - cd_curfad, -750*cd_speed);
		}
		else
		{
			//printf("Ready\n");
			cd_curfad = cd_fad_seek;
			cd_stat = CD_STAT_PLAY;
		}

		return;
	}

	if ((cd_stat & 0x0f00) == CD_STAT_PLAY)
	{
		if (fadstoplay)
		{
			logerror("STVCD: Reading FAD %d\n", cd_curfad);

			if (cdrom)
			{
				uint8_t p_ok;

				if(cdrom_get_track_type(cdrom, cdrom_get_track(cdrom, cd_curfad)) != CD_TRACK_AUDIO)
				{
					cd_read_filtered_sector(cd_curfad,&p_ok);
					machine().device<cdda_device>("cdda")->stop_audio(); //stop any pending CD-DA
				}
				else
				{
					p_ok = 1; // TODO
					machine().device<cdda_device>("cdda")->start_audio(cd_curfad, 1);
				}

				if(p_ok)
				{
					cd_curfad++;
					fadstoplay--;
					hirqreg |= CSCT;
					sectorstore = 1;

					if (!fadstoplay)
					{
						if(cdda_repeat_count >= cdda_maxrepeat)
						{
							CDROM_LOG(("cd_playdata: playback ended\n"))
							cd_stat = CD_STAT_PAUSE;

							hirqreg |= PEND;

							if (playtype == 1)
							{
								CDROM_LOG(("cd_playdata: setting EFLS\n"))
								hirqreg |= EFLS;
							}
						}
						else
						{
							if(cdda_repeat_count < 0xe)
								cdda_repeat_count++;

							cd_curfad = cdrom_get_track_start(cdrom, cur_track-1) + 150;
							fadstoplay = cdrom_get_track_start(cdrom, cur_track) - cd_curfad;
						}
					}
				}
			}
		}
	}
}

// loads a single sector off the CD, anywhere from FAD 150 on up
void saturn_state::cd_readblock(uint32_t fad, uint8_t *dat)
{
	if (cdrom)
	{
		cdrom_read_data(cdrom, fad-150, dat, CD_TRACK_MODE1);
	}
}

void saturn_state::stvcd_set_tray_open( void )
{
	if(!tray_is_closed)
		return;

	hirqreg |= DCHG;
	cd_stat = CD_STAT_OPEN;

	cdrom = (cdrom_file *)nullptr;
	tray_is_closed = 0;

	popmessage("Tray Open");
}

void saturn_state::stvcd_set_tray_close( void )
{
	/* avoid user attempts to load a CD-ROM without opening the tray first (emulation asserts anyway with current framework) */
	if(tray_is_closed)
		return;

	hirqreg |= DCHG;

	cdrom_image_device *cddevice = machine().device<cdrom_image_device>("cdrom");
	if (cddevice!=nullptr)
	{
		// MESS case
		cdrom = cddevice->get_cdrom_file();
	}
	else
	{
		// MAME case
		cdrom = cdrom_open(machine().rom_load().get_disk_handle("cdrom"));
	}

	machine().device<cdda_device>("cdda")->set_cdrom(cdrom);

	if (cdrom)
	{
		CDROM_LOG(("Opened CD-ROM successfully, reading root directory\n"))
		//read_new_dir(0xffffff);  // read root directory
		cd_stat = CD_STAT_PAUSE;
	}
	else
	{
		cd_stat = CD_STAT_NODISC;
	}

	cd_speed = 2;
	cdda_repeat_count = 0;
	tray_is_closed = 1;

	popmessage("Tray Close");
}