-
Notifications
You must be signed in to change notification settings - Fork 90
/
blkdev.cpp
2471 lines (2340 loc) · 60.5 KB
/
blkdev.cpp
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
/*
* UAE - The Un*x Amiga Emulator
*
* lowlevel cd device glue, scsi emulator
*
* Copyright 2009-2013 Toni Wilen
*
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "memory.h"
#include "traps.h"
#include "blkdev.h"
#include "scsidev.h"
#include "savestate.h"
#include "crc32.h"
#include "threaddep/thread.h"
#include "execio.h"
#include "zfile.h"
#include "scsi.h"
#include "statusline.h"
#include "fsdb.h"
#ifdef RETROPLATFORM
#include "rp.h"
#endif
int log_scsiemu = 0;
#define PRE_INSERT_DELAY (3 * (currprefs.ntscmode ? 60 : 50))
struct blkdevstate
{
bool scsiemu;
int type;
struct device_functions *device_func;
int isopen;
int waspaused;
int delayed;
uae_sem_t sema;
int sema_cnt;
int current_pos;
int play_end_pos;
uae_u8 play_qcode[SUBQ_SIZE];
TCHAR newimagefile[256];
int imagechangetime;
bool cdimagefileinuse;
int wasopen;
bool mediawaschanged;
struct scsi_data_tape *tape;
bool showstatusline;
};
struct blkdevstate state[MAX_TOTAL_SCSI_DEVICES];
static bool dev_init;
/* convert minutes, seconds and frames -> logical sector number */
int msf2lsn (int msf)
{
int sector = (((msf >> 16) & 0xff) * 60 * 75 + ((msf >> 8) & 0xff) * 75 + ((msf >> 0) & 0xff));
sector -= 150;
return sector;
}
/* convert logical sector number -> minutes, seconds and frames */
int lsn2msf (int sectors)
{
int msf;
sectors += 150;
msf = (sectors / (75 * 60)) << 16;
msf |= ((sectors / 75) % 60) << 8;
msf |= (sectors % 75) << 0;
return msf;
}
uae_u8 frombcd (uae_u8 v)
{
return (v >> 4) * 10 + (v & 15);
}
uae_u8 tobcd (uae_u8 v)
{
return ((v / 10) << 4) | (v % 10);
}
int fromlongbcd (uae_u8 *p)
{
return (frombcd (p[0]) << 16) | (frombcd (p[1]) << 8) | (frombcd (p[2]) << 0);
}
void tolongbcd (uae_u8 *p, int v)
{
p[0] = tobcd ((v >> 16) & 0xff);
p[1] = tobcd ((v >> 8) & 0xff);
p[2] = tobcd ((v >> 0) & 0xff);
}
static struct cd_toc *gettoc (int unitnum, struct cd_toc_head *th, int block)
{
if (th->lastaddress == 0) {
if (unitnum < 0)
return NULL;
if (!sys_command_cd_toc(unitnum, th))
return NULL;
}
for (int i = th->first_track_offset + 1; i <= th->last_track_offset; i++) {
struct cd_toc *t = &th->toc[i];
if (block < t->paddress)
return t - 1;
}
return &th->toc[th->last_track_offset];
}
int cdtracknumber(struct cd_toc_head *th, int block)
{
struct cd_toc *t = gettoc(-1, th, block);
if (!t)
return -1;
return t->track;
}
int isaudiotrack (struct cd_toc_head *th, int block)
{
struct cd_toc *t = gettoc (-1, th, block);
if (!t)
return 0;
return (t->control & 0x0c) != 4;
}
int isdatatrack (struct cd_toc_head *th, int block)
{
return !isaudiotrack (th, block);
}
static int cdscsidevicetype[MAX_TOTAL_SCSI_DEVICES];
static struct device_functions *devicetable[] = {
NULL,
&devicefunc_cdimage,
#ifdef WITH_SCSI_IOCTL
&devicefunc_scsi_ioctl,
#else
NULL,
#endif
#ifdef WITH_SCSI_SPTI
&devicefunc_scsi_spti,
#else
NULL,
#endif
};
#define NUM_DEVICE_TABLE_ENTRIES 4
static int driver_installed[NUM_DEVICE_TABLE_ENTRIES];
static void install_driver (int flags)
{
for (int i = 0; i < MAX_TOTAL_SCSI_DEVICES; i++) {
struct blkdevstate *st = &state[i];
st->scsiemu = false;
st->type = -1;
st->device_func = NULL;
}
if (flags > 0) {
state[0].device_func = devicetable[flags];
state[0].scsiemu = true;
} else {
for (int i = 0; i < MAX_TOTAL_SCSI_DEVICES; i++) {
struct blkdevstate *st = &state[i];
st->scsiemu = false;
st->device_func = NULL;
switch (cdscsidevicetype[i])
{
case SCSI_UNIT_IMAGE:
st->device_func = devicetable[SCSI_UNIT_IMAGE];
st->scsiemu = true;
break;
case SCSI_UNIT_IOCTL:
st->device_func = devicetable[SCSI_UNIT_IOCTL];
st->scsiemu = true;
break;
case SCSI_UNIT_SPTI:
if (currprefs.win32_uaescsimode == UAESCSI_CDEMU) {
st->device_func = devicetable[SCSI_UNIT_IOCTL];
st->scsiemu = true;
} else {
st->device_func = devicetable[SCSI_UNIT_SPTI];
}
break;
}
// do not default to image mode if unit 1+ and automount
if (i == 0 || !currprefs.win32_automount_cddrives) {
// use image mode if driver disabled
for (int j = 1; j < NUM_DEVICE_TABLE_ENTRIES; j++) {
if (devicetable[j] == st->device_func && driver_installed[j] < 0) {
st->device_func = devicetable[SCSI_UNIT_IMAGE];
st->scsiemu = true;
}
}
}
}
}
for (int j = 1; j < NUM_DEVICE_TABLE_ENTRIES; j++) {
if (devicetable[j] == NULL) {
continue;
}
if (!driver_installed[j]) {
for (int i = 0; i < MAX_TOTAL_SCSI_DEVICES; i++) {
struct blkdevstate *st = &state[i];
if (st->device_func == devicetable[j]) {
int ok = st->device_func->openbus (0);
if (!ok && st->device_func != devicetable[SCSI_UNIT_IMAGE]) {
st->device_func = devicetable[SCSI_UNIT_IMAGE];
st->scsiemu = true;
write_log (_T("Fallback to image mode, unit %d.\n"), i);
driver_installed[j] = -1;
} else {
driver_installed[j] = 1;
}
write_log (_T("%s driver installed, ok=%d\n"), st->device_func->name, ok);
break;
}
}
}
}
}
void blkdev_default_prefs (struct uae_prefs *p)
{
for (int i = 0; i < MAX_TOTAL_SCSI_DEVICES; i++) {
p->cdslots[i].name[0] = 0;
p->cdslots[i].inuse = false;
p->cdslots[i].type = SCSI_UNIT_DEFAULT;
p->cdslots[i].temporary = false;
cdscsidevicetype[i] = SCSI_UNIT_DEFAULT;
}
}
void blkdev_fix_prefs (struct uae_prefs *p)
{
for (int i = 0; i < MAX_TOTAL_SCSI_DEVICES; i++) {
cdscsidevicetype[i] = p->cdslots[i].type;
if (p->cdslots[i].inuse == false && p->cdslots[i].name[0] && p->cdslots[i].type != SCSI_UNIT_DISABLED)
p->cdslots[i].inuse = true;
}
for (int i = 0; i < MAX_TOTAL_SCSI_DEVICES; i++) {
if (cdscsidevicetype[i] != SCSI_UNIT_DEFAULT && (currprefs.scsi == 0 || currprefs.win32_uaescsimode < UAESCSI_SPTI))
continue;
if (p->cdslots[i].inuse || p->cdslots[i].name[0]) {
TCHAR *name = p->cdslots[i].name;
if (_tcslen (name) == 3 && name[1] == ':' && name[2] == '\\') {
if (currprefs.scsi && (currprefs.win32_uaescsimode == UAESCSI_SPTI || currprefs.win32_uaescsimode == UAESCSI_SPTISCAN))
cdscsidevicetype[i] = SCSI_UNIT_SPTI;
else
cdscsidevicetype[i] = SCSI_UNIT_IOCTL;
} else {
cdscsidevicetype[i] = SCSI_UNIT_IMAGE;
}
} else if (currprefs.scsi) {
if (currprefs.win32_uaescsimode == UAESCSI_CDEMU)
cdscsidevicetype[i] = SCSI_UNIT_IOCTL;
else
cdscsidevicetype[i] = SCSI_UNIT_SPTI;
} else {
cdscsidevicetype[i] = SCSI_UNIT_IOCTL;
}
}
}
static bool getsem (int unitnum, bool dowait)
{
struct blkdevstate *st = &state[unitnum];
if (st->sema == NULL)
uae_sem_init (&st->sema, 0, 1);
bool gotit = false;
if (dowait) {
uae_sem_wait (&st->sema);
gotit = true;
} else {
gotit = uae_sem_trywait (&st->sema) == 0;
}
if (gotit)
st->sema_cnt++;
if (st->sema_cnt > 1)
write_log (_T("CD: unitsem%d acquire mismatch! cnt=%d\n"), unitnum, st->sema_cnt);
return gotit;
}
static bool getsem (int unitnum)
{
return getsem (unitnum, false);
}
static void freesem (int unitnum)
{
struct blkdevstate *st = &state[unitnum];
st->sema_cnt--;
if (st->sema_cnt < 0)
write_log (_T("CD: unitsem%d release mismatch! cnt=%d\n"), unitnum, st->sema_cnt);
uae_sem_post (&st->sema);
}
static void sys_command_close_internal (int unitnum)
{
struct blkdevstate *st = &state[unitnum];
getsem (unitnum, true);
st->waspaused = 0;
if (st->isopen <= 0)
write_log (_T("BUG unit %d close: opencnt=%d!\n"), unitnum, st->isopen);
if (st->device_func) {
state[unitnum].device_func->closedev (unitnum);
if (st->isopen > 0)
st->isopen--;
}
freesem (unitnum);
if (st->isopen == 0) {
uae_sem_destroy (&st->sema);
st->sema = NULL;
}
}
static int sys_command_open_internal (int unitnum, const TCHAR *ident, cd_standard_unit csu)
{
struct blkdevstate *st = &state[unitnum];
int ret = 0;
if (st->sema == NULL)
uae_sem_init (&st->sema, 0, 1);
getsem (unitnum, true);
if (st->isopen)
write_log (_T("BUG unit %d open: opencnt=%d!\n"), unitnum, st->isopen);
if (st->device_func) {
ret = state[unitnum].device_func->opendev (unitnum, ident, csu != CD_STANDARD_UNIT_DEFAULT);
if (ret)
st->isopen++;
}
freesem (unitnum);
return ret;
}
static int getunitinfo (int unitnum, int drive, cd_standard_unit csu, int *isaudio)
{
struct device_info di;
if (sys_command_info (unitnum, &di, 0)) {
write_log (_T("Scanning drive %s: "), di.label);
if (di.media_inserted) {
if (isaudiotrack (&di.toc, 0)) {
if (*isaudio == 0)
*isaudio = drive;
write_log (_T("CDA"));
}
uae_u8 buffer[2048];
if (sys_command_cd_read (unitnum, buffer, 16, 1)) {
if (!memcmp (buffer + 8, "CDTV", 4) || !memcmp (buffer + 8, "CD32", 4) || !memcmp (buffer + 8, "COMM", 4)) {
uae_u32 crc;
write_log (_T("CD32 or CDTV"));
if (sys_command_cd_read (unitnum, buffer, 21, 1)) {
crc = get_crc32 (buffer, sizeof buffer);
if (crc == 0xe56c340f) {
write_log (_T(" [CD32.TM]"));
if (csu == CD_STANDARD_UNIT_CD32) {
write_log (_T("\n"));
return 1;
}
}
}
if (csu == CD_STANDARD_UNIT_CDTV || csu == CD_STANDARD_UNIT_CD32) {
write_log (_T("\n"));
return 1;
}
}
}
} else {
write_log (_T("no media"));
}
}
write_log (_T("\n"));
return 0;
}
static int get_standard_cd_unit2 (struct uae_prefs *p, cd_standard_unit csu)
{
int unitnum = 0;
int isaudio = 0;
if (p->cdslots[unitnum].name[0] || p->cdslots[unitnum].inuse) {
if (p->cdslots[unitnum].name[0]) {
device_func_init (SCSI_UNIT_IOCTL);
if (!sys_command_open_internal (unitnum, p->cdslots[unitnum].name, csu)) {
device_func_init (SCSI_UNIT_IMAGE);
if (!sys_command_open_internal (unitnum, p->cdslots[unitnum].name, csu))
goto fallback;
}
} else {
goto fallback;
}
return unitnum;
}
device_func_init (SCSI_UNIT_IOCTL);
for (int drive = 'C'; drive <= 'Z'; ++drive) {
TCHAR vol[100];
_stprintf (vol, _T("%c:\\"), drive);
int drivetype = GetDriveType (vol);
if (drivetype == DRIVE_CDROM) {
if (sys_command_open_internal (unitnum, vol, csu)) {
if (getunitinfo (unitnum, drive, csu, &isaudio))
return unitnum;
sys_command_close (unitnum);
}
}
}
if (isaudio) {
TCHAR vol[100];
_stprintf (vol, _T("%c:\\"), isaudio);
if (sys_command_open_internal (unitnum, vol, csu))
return unitnum;
}
fallback:
device_func_init (SCSI_UNIT_IMAGE);
if (!sys_command_open_internal (unitnum, _T(""), csu)) {
write_log (_T("image mounter failed to open as empty!?\n"));
return -1;
}
return unitnum;
}
static void cd_statusline_label(int unitnum)
{
TCHAR *p = currprefs.cdslots[unitnum].name;
if (p[0]) {
TCHAR name[MAX_DPATH];
struct device_info di;
cfgfile_resolve_path_out_load(p, name, sizeof(name) / sizeof(TCHAR), PATH_CD);
const TCHAR *fname = my_getfilepart(name);
if (sys_command_info(unitnum, &di, 0) && di.volume_id[0])
statusline_add_message(STATUSTYPE_CD, _T("CD%d: [%s] %s"), unitnum, di.volume_id, fname);
else
statusline_add_message(STATUSTYPE_CD, _T("CD%d: %s"), unitnum, fname);
}
}
int get_standard_cd_unit (cd_standard_unit csu)
{
int unitnum = get_standard_cd_unit2 (&currprefs, csu);
if (unitnum < 0)
return -1;
struct blkdevstate *st = &state[unitnum];
#ifdef RETROPLATFORM
rp_cd_device_enable (unitnum, true);
#endif
st->delayed = 0;
if (currprefs.cdslots[unitnum].delayed) {
st->delayed = PRE_INSERT_DELAY;
}
st->showstatusline = true;
return unitnum;
}
void close_standard_cd_unit (int unitnum)
{
sys_command_close (unitnum);
}
int sys_command_isopen (int unitnum)
{
struct blkdevstate *st = &state[unitnum];
return st->isopen;
}
int sys_command_open_tape (int unitnum, const TCHAR *tape_directory, bool readonly)
{
struct blkdevstate *st = &state[unitnum];
if (st->isopen == 0) {
struct scsi_data_tape *tape = tape_alloc (unitnum, tape_directory, readonly);
if (!tape)
return 0;
st->tape = tape;
}
st->isopen++;
return 1;
}
int sys_command_open (int unitnum)
{
struct blkdevstate *st = &state[unitnum];
blkdev_fix_prefs (&currprefs);
if (!dev_init) {
device_func_init (0);
}
if (st->isopen) {
st->isopen++;
return -1;
}
st->waspaused = 0;
int v = sys_command_open_internal (unitnum, currprefs.cdslots[unitnum].name[0] ? currprefs.cdslots[unitnum].name : NULL, CD_STANDARD_UNIT_DEFAULT);
if (!v)
return 0;
#ifdef RETROPLATFORM
rp_cd_device_enable (unitnum, true);
#endif
return v;
}
void sys_command_close (int unitnum)
{
struct blkdevstate *st = &state[unitnum];
if (st->isopen > 1) {
st->isopen--;
return;
}
if (st->tape) {
tape_free (st->tape);
st->tape = NULL;
}
sys_command_close_internal (unitnum);
}
void blkdev_cd_change (int unitnum, const TCHAR *name)
{
struct device_info di;
sys_command_info (unitnum, &di, 1);
#ifdef RETROPLATFORM
rp_cd_image_change (unitnum, name);
#endif
}
void device_func_reset(void)
{
// if reset during delayed CD change, re-insert the CD immediately
for (int i = 0; i < MAX_TOTAL_SCSI_DEVICES; i++) {
struct blkdevstate *st = &state[i];
if (st->imagechangetime > 0 && st->newimagefile[0] && !currprefs.cdslots[i].name[0]) {
_tcscpy(changed_prefs.cdslots[i].name, st->newimagefile);
_tcscpy(currprefs.cdslots[i].name, st->newimagefile);
cd_statusline_label(i);
}
st->imagechangetime = 0;
st->newimagefile[0] = 0;
st->mediawaschanged = false;
st->waspaused = false;
}
}
void device_func_free(void)
{
for (int i = 0; i < MAX_TOTAL_SCSI_DEVICES; i++) {
struct blkdevstate *st = &state[i];
st->wasopen = 0;
st->waspaused = false;
st->mediawaschanged = false;
st->imagechangetime = 0;
st->cdimagefileinuse = false;
st->newimagefile[0] = 0;
}
dev_init = false;
}
int device_func_init (int flags)
{
blkdev_fix_prefs (&currprefs);
install_driver (flags);
dev_init = true;
return 1;
}
bool blkdev_get_info (struct uae_prefs *p, int unitnum, struct device_info *di)
{
bool open = true, opened = false, ok = false;
struct blkdevstate *st = &state[unitnum];
if (!st->isopen) {
blkdev_fix_prefs (p);
install_driver (0);
opened = true;
open = sys_command_open_internal (unitnum, p->cdslots[unitnum].name[0] ? p->cdslots[unitnum].name : NULL, CD_STANDARD_UNIT_DEFAULT) != 0;
}
if (open) {
ok = sys_command_info (unitnum, di, true) != 0;
}
if (open && opened)
sys_command_close_internal (unitnum);
return ok;
}
void blkdev_entergui (void)
{
for (int i = 0; i < MAX_TOTAL_SCSI_DEVICES; i++) {
struct blkdevstate *st = &state[i];
struct device_info di;
if (sys_command_info (i, &di, 1)) {
if (sys_command_cd_pause (i, 1) == 0)
st->waspaused = 1;
}
}
}
void blkdev_exitgui (void)
{
for (int i = 0; i < MAX_TOTAL_SCSI_DEVICES; i++) {
struct blkdevstate *st = &state[i];
if (st->waspaused) {
struct device_info di;
if (sys_command_info (i, &di, 1)) {
sys_command_cd_pause (i, 0);
}
}
st->waspaused = 0;
}
}
void check_prefs_changed_cd (void)
{
if (!config_changed)
return;
}
static void check_changes (int unitnum)
{
struct blkdevstate *st = &state[unitnum];
bool changed = false;
bool gotsem = false;
if (st->device_func == NULL)
return;
if (st->showstatusline) {
cd_statusline_label(unitnum);
st->showstatusline = 0;
}
if (st->delayed) {
st->delayed--;
if (st->delayed == 0)
write_log (_T("CD: startup delayed insert '%s'\n"), currprefs.cdslots[unitnum].name[0] ? currprefs.cdslots[unitnum].name : _T("<EMPTY>"));
return;
}
if (_tcscmp (changed_prefs.cdslots[unitnum].name, currprefs.cdslots[unitnum].name) != 0)
changed = true;
if (!changed && changed_prefs.cdslots[unitnum].name[0] == 0 && changed_prefs.cdslots[unitnum].inuse != currprefs.cdslots[unitnum].inuse)
changed = true;
if (changed) {
bool wasimage = currprefs.cdslots[unitnum].name[0] != 0;
if (st->sema)
gotsem = getsem (unitnum, true);
st->cdimagefileinuse = changed_prefs.cdslots[unitnum].inuse;
_tcscpy (st->newimagefile, changed_prefs.cdslots[unitnum].name);
changed_prefs.cdslots[unitnum].name[0] = currprefs.cdslots[unitnum].name[0] = 0;
currprefs.cdslots[unitnum].inuse = changed_prefs.cdslots[unitnum].inuse;
int pollmode = 0;
st->imagechangetime = 3 * 50;
struct device_info di;
st->device_func->info (unitnum, &di, 0, -1);
if (st->wasopen >= 0)
st->wasopen = di.open ? 1 : 0;
if (st->wasopen) {
st->device_func->closedev (unitnum);
st->wasopen = -1;
if (currprefs.scsi) {
scsi_do_disk_change (unitnum, 0, &pollmode);
if (pollmode)
st->imagechangetime = 8 * 50;
if (filesys_do_disk_change (unitnum, 0)) {
st->imagechangetime = st->newimagefile[0] ? 3 * 50 : 0;
pollmode = 0;
}
}
}
write_log (_T("CD: eject (%s) open=%d\n"), pollmode ? _T("slow") : _T("fast"), st->wasopen ? 1 : 0);
if (wasimage)
statusline_add_message(STATUSTYPE_CD, _T("CD%d: -"), unitnum);
#ifdef RETROPLATFORM
rp_cd_image_change (unitnum, NULL);
#endif
if (gotsem) {
freesem (unitnum);
gotsem = false;
}
}
if (st->imagechangetime == 0)
return;
st->imagechangetime--;
if (st->imagechangetime > 0)
return;
if (st->sema)
gotsem = getsem (unitnum, true);
_tcscpy (currprefs.cdslots[unitnum].name, st->newimagefile);
_tcscpy (changed_prefs.cdslots[unitnum].name, st->newimagefile);
currprefs.cdslots[unitnum].inuse = changed_prefs.cdslots[unitnum].inuse = st->cdimagefileinuse;
st->newimagefile[0] = 0;
write_log (_T("CD: delayed insert '%s' (open=%d,unit=%d)\n"), currprefs.cdslots[unitnum].name[0] ? currprefs.cdslots[unitnum].name : _T("<EMPTY>"), st->wasopen ? 1 : 0, unitnum);
device_func_init (0);
if (st->wasopen) {
if (!st->device_func->opendev (unitnum, currprefs.cdslots[unitnum].name, 0)) {
write_log (_T("-> device open failed\n"));
st->wasopen = 0;
} else {
st->wasopen = 1;
write_log (_T("-> device reopened\n"));
}
}
if (currprefs.scsi && st->wasopen) {
struct device_info di;
st->device_func->info (unitnum, &di, 0, -1);
int pollmode;
if (gotsem) {
freesem (unitnum);
gotsem = false;
}
scsi_do_disk_change (unitnum, 1, &pollmode);
filesys_do_disk_change (unitnum, 1);
}
st->mediawaschanged = true;
st->showstatusline = true;
if (gotsem) {
freesem (unitnum);
gotsem = false;
}
#ifdef RETROPLATFORM
rp_cd_image_change (unitnum, currprefs.cdslots[unitnum].name);
#endif
set_config_changed ();
}
void blkdev_vsync (void)
{
for (int i = 0; i < MAX_TOTAL_SCSI_DEVICES; i++)
check_changes (i);
}
static int do_scsi (int unitnum, uae_u8 *cmd, int cmdlen)
{
uae_u8 *p = state[unitnum].device_func->exec_out (unitnum, cmd, cmdlen);
return p != NULL;
}
static int do_scsi (int unitnum, uae_u8 *cmd, int cmdlen, uae_u8 *out, int outsize)
{
uae_u8 *p = state[unitnum].device_func->exec_in (unitnum, cmd, cmdlen, &outsize);
if (p)
memcpy (out, p, outsize);
return p != NULL;
}
static int failunit (int unitnum)
{
if (unitnum < 0 || unitnum >= MAX_TOTAL_SCSI_DEVICES)
return 1;
if (state[unitnum].device_func == NULL)
return 1;
return 0;
}
static int audiostatus (int unitnum)
{
if (!getsem (unitnum))
return 0;
uae_u8 cmd[10] = {0x42,2,0x40,1,0,0,0,(uae_u8)(DEVICE_SCSI_BUFSIZE>>8),(uae_u8)(DEVICE_SCSI_BUFSIZE&0xff),0};
uae_u8 *p = state[unitnum].device_func->exec_in (unitnum, cmd, sizeof (cmd), 0);
freesem (unitnum);
if (!p)
return 0;
return p[1];
}
/* pause/unpause CD audio */
int sys_command_cd_pause (int unitnum, int paused)
{
if (failunit (unitnum))
return -1;
if (!getsem (unitnum))
return 0;
int v;
if (state[unitnum].device_func->pause == NULL) {
int as = audiostatus (unitnum);
uae_u8 cmd[10] = {0x4b,0,0,0,0,0,0,0,(uae_u8)(paused?0:1),0};
do_scsi (unitnum, cmd, sizeof cmd);
v = as == AUDIO_STATUS_PAUSED;
} else {
v = state[unitnum].device_func->pause (unitnum, paused);
}
freesem (unitnum);
return v;
}
/* stop CD audio */
void sys_command_cd_stop (int unitnum)
{
if (failunit (unitnum))
return;
if (!getsem (unitnum))
return;
if (state[unitnum].device_func->stop == NULL) {
int as = audiostatus (unitnum);
uae_u8 cmd[6] = {0x4e,0,0,0,0,0};
do_scsi (unitnum, cmd, sizeof cmd);
} else {
state[unitnum].device_func->stop (unitnum);
}
freesem (unitnum);
}
/* play CD audio */
int sys_command_cd_play (int unitnum, int startlsn, int endlsn, int scan)
{
int v;
if (failunit (unitnum))
return 0;
if (!getsem (unitnum))
return 0;
state[unitnum].play_end_pos = endlsn;
if (state[unitnum].device_func->play == NULL) {
uae_u8 cmd[12] = {0,0,0,0,0,0,0,0,0,0,0,0};
int startmsf = lsn2msf (startlsn);
int endmsf = lsn2msf (endlsn);
cmd[0] = 0x47;
cmd[3] = (uae_u8)(startmsf >> 16);
cmd[4] = (uae_u8)(startmsf >> 8);
cmd[5] = (uae_u8)(startmsf >> 0);
cmd[6] = (uae_u8)(endmsf >> 16);
cmd[7] = (uae_u8)(endmsf >> 8);
cmd[8] = (uae_u8)(endmsf >> 0);
v = do_scsi (unitnum, cmd, sizeof cmd) ? 0 : 1;
} else {
v = state[unitnum].device_func->play (unitnum, startlsn, endlsn, scan, NULL, NULL);
}
freesem (unitnum);
return v;
}
/* play CD audio with subchannels */
int sys_command_cd_play (int unitnum, int startlsn, int endlsn, int scan, play_status_callback statusfunc, play_subchannel_callback subfunc)
{
int v;
if (failunit (unitnum))
return 0;
if (!getsem (unitnum))
return 0;
state[unitnum].play_end_pos = endlsn;
if (state[unitnum].device_func->play == NULL)
v = sys_command_cd_play (unitnum, startlsn, endlsn, scan);
else
v = state[unitnum].device_func->play (unitnum, startlsn, endlsn, scan, statusfunc, subfunc);
freesem (unitnum);
return v;
}
/* set CD audio volume */
uae_u32 sys_command_cd_volume (int unitnum, uae_u16 volume_left, uae_u16 volume_right)
{
int v;
if (failunit (unitnum))
return 0;
if (!getsem (unitnum))
return 0;
if (state[unitnum].device_func->volume == NULL)
v = -1;
else
v = state[unitnum].device_func->volume (unitnum, volume_left, volume_right);
freesem (unitnum);
return v;
}
/* read qcode */
int sys_command_cd_qcode (int unitnum, uae_u8 *buf, int sector, bool all)
{
int v;
if (failunit (unitnum))
return 0;
if (!getsem (unitnum))
return 0;
if (state[unitnum].device_func->qcode == NULL) {
if (all) {
v = 0;
} else {
uae_u8 cmd[10] = {0x42,2,0x40,1,0,0,0,(uae_u8)(SUBQ_SIZE>>8),(uae_u8)(SUBQ_SIZE&0xff),0};
v = do_scsi (unitnum, cmd, sizeof cmd, buf, SUBQ_SIZE);
}
} else {
v = state[unitnum].device_func->qcode (unitnum, buf, sector, all);
}
freesem (unitnum);
return v;
};
/* read table of contents */
int sys_command_cd_toc (int unitnum, struct cd_toc_head *th)
{
int v;
if (failunit (unitnum))
return 0;
if (!getsem (unitnum))
return 0;
if (state[unitnum].device_func->toc == NULL) {
uae_u8 buf[4 + 8 * 103];
int size = sizeof buf;
uae_u8 cmd [10] = { 0x43,0,0,0,0,0,0,(uae_u8)(size>>8),(uae_u8)(size&0xff),0};
v = do_scsi(unitnum, cmd, sizeof cmd, buf, size);
if (v > 0) {
th->first_track = buf[2];
th->last_track = buf[3];
th->tracks = th->last_track - th->first_track + 1;
th->firstaddress = 0;
th->points = th->tracks + 1;
int len = (buf[0] << 8) | buf[1];
uae_u8 *p = buf + 4;
if ((th->tracks + 1) * 8 > len) {
freesem(unitnum);
return 0;
}
struct cd_toc *t = &th->toc[th->first_track];
for (int i = th->first_track; i <= th->last_track; i++) {
t->adr = p[1] >> 4;
t->control = p[1] & 15;
t->point = t->track = p[2];
t->paddress = (p[5] << 16) | (p[6] << 8) | (p[7] << 0);
p += 8;
t++;
}
th->lastaddress = (p[5] << 16) | (p[6] << 8) | (p[7] << 0);
t->track = t->point = 0xaa;
th->first_track_offset = 0;
th->last_track_offset = th->last_track;
v = 1;
}
} else {
v = state[unitnum].device_func->toc (unitnum, th);
}
freesem (unitnum);
return v;
}
/* read one cd sector */
int sys_command_cd_read (int unitnum, uae_u8 *data, int block, int size)
{
int v;
if (failunit (unitnum))
return 0;
if (!getsem (unitnum))
return 0;
if (state[unitnum].device_func->read == NULL) {
uae_u8 cmd1[12] = { 0x28, 0, (uae_u8)(block >> 24), (uae_u8)(block >> 16), (uae_u8)(block >> 8), (uae_u8)(block >> 0), 0, (uae_u8)(size >> 8), (uae_u8)(size >> 0), 0, 0, 0 };
v = do_scsi (unitnum, cmd1, sizeof cmd1, data, size * 2048);
#if 0
if (!v) {
uae_u8 cmd2[12] = { 0xbe, 0, block >> 24, block >> 16, block >> 8, block >> 0, size >> 16, size >> 8, size >> 0, 0x10, 0, 0 };
v = do_scsi (unitnum, cmd2, sizeof cmd2, data, size * 2048);
}
#endif
} else {
v = state[unitnum].device_func->read (unitnum, data, block, size);
}
freesem (unitnum);
return v;
}
int sys_command_cd_rawread (int unitnum, uae_u8 *data, int block, int size, int sectorsize)
{
int v;
if (failunit (unitnum))
return -1;
if (!getsem (unitnum))
return 0;
if (state[unitnum].device_func->rawread == NULL) {
uae_u8 cmd[12] = { 0xbe, 0, (uae_u8)(block >> 24), (uae_u8)(block >> 16), (uae_u8)(block >> 8), (uae_u8)(block >> 0), (uae_u8)(size >> 16), (uae_u8)(size >> 8), (uae_u8)(size >> 0), 0x10, 0, 0 };
v = do_scsi (unitnum, cmd, sizeof cmd, data, size * sectorsize);
} else {
v = state[unitnum].device_func->rawread (unitnum, data, block, size, sectorsize, 0xffffffff);
}
freesem (unitnum);
return v;
}
int sys_command_cd_rawread (int unitnum, uae_u8 *data, int block, int size, int sectorsize, uae_u8 sectortype, uae_u8 scsicmd9, uae_u8 subs)
{
int v;
if (failunit (unitnum))
return -1;
if (!getsem (unitnum))
return 0;
if (state[unitnum].device_func->rawread == NULL) {
uae_u8 cmd[12] = { 0xbe, 0, (uae_u8)(block >> 24), (uae_u8)(block >> 16), (uae_u8)(block >> 8), (uae_u8)(block >> 0), (uae_u8)(size >> 16), (uae_u8)(size >> 8), (uae_u8)(size >> 0), 0x10, 0, 0 };
v = do_scsi (unitnum, cmd, sizeof cmd, data, size * sectorsize);
} else {
v = state[unitnum].device_func->rawread (unitnum, data, block, size, sectorsize, (sectortype << 16) | (scsicmd9 << 8) | subs);
}
freesem (unitnum);
return v;
}
/* read block */
int sys_command_read (int unitnum, uae_u8 *data, int block, int size)
{
int v;
if (failunit (unitnum))
return 0;
if (!getsem (unitnum))
return 0;
if (state[unitnum].device_func->read == NULL) {
uae_u8 cmd[12] = { 0xa8, 0, 0, 0, 0, 0, (uae_u8)(size >> 24), (uae_u8)(size >> 16), (uae_u8)(size >> 8), (uae_u8)(size >> 0), 0, 0 };
cmd[2] = (uae_u8)(block >> 24);
cmd[3] = (uae_u8)(block >> 16);
cmd[4] = (uae_u8)(block >> 8);
cmd[5] = (uae_u8)(block >> 0);
v = do_scsi (unitnum, cmd, sizeof cmd, data, size * 2048);