-
Notifications
You must be signed in to change notification settings - Fork 90
/
cdtv.cpp
1956 lines (1793 loc) · 43.2 KB
/
cdtv.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
*
* CDTV DMAC/CDROM controller emulation
*
* Copyright 2004/2007-2010 Toni Wilen
*
* Thanks to Mark Knibbs <[email protected]> for CDTV Technical information
*
*/
//#define CDTV_SUB_DEBUG
//#define CDTV_DEBUG
//#define CDTV_DEBUG_CMD
//#define CDTV_DEBUG_6525
#include "sysconfig.h"
#include "sysdeps.h"
#ifdef CDTV
#include "options.h"
#include "memory.h"
#include "custom.h"
#include "newcpu.h"
#include "debug.h"
#include "cdtv.h"
#include "blkdev.h"
#include "gui.h"
#include "zfile.h"
#include "threaddep/thread.h"
#include "a2091.h"
#include "uae.h"
#include "savestate.h"
#include "scsi.h"
#include "devices.h"
#include "rommgr.h"
/* DMAC CNTR bits. */
#define CNTR_TCEN (1<<7)
#define CNTR_PREST (1<<6)
#define CNTR_PDMD (1<<5)
#define CNTR_INTEN (1<<4)
#define CNTR_DDIR (1<<3)
/* ISTR bits. */
#define ISTR_INTX (1<<8)
#define ISTR_INT_F (1<<7)
#define ISTR_INTS (1<<6)
#define ISTR_E_INT (1<<5)
#define ISTR_INT_P (1<<4)
#define ISTR_UE_INT (1<<3)
#define ISTR_OE_INT (1<<2)
#define ISTR_FF_FLG (1<<1)
#define ISTR_FE_FLG (1<<0)
#define MODEL_NAME "MATSHITA0.96"
/* also MATSHITA0.97 exists but is apparently rare */
#define MAX_SUBCODEBUFFER 36
static volatile int subcodebufferoffset, subcodebufferoffsetw, subcodeoffset;
static uae_u8 subcodebufferinuse[MAX_SUBCODEBUFFER];
static uae_u8 subcodebuffer[MAX_SUBCODEBUFFER * SUB_CHANNEL_SIZE];
static uae_sem_t sub_sem, cda_sem;
static smp_comm_pipe requests;
static volatile int thread_alive;
static int configured;
static int cdtvscsi;
static uae_u8 dmacmemory[128];
static struct cd_toc_head toc;
static uae_u32 last_cd_position, play_start, play_end;
static uae_u8 cdrom_qcode[4 + 12], cd_audio_status;
static int datatrack;
static volatile uae_u8 dmac_istr, dmac_cntr;
static volatile uae_u16 dmac_dawr;
static volatile uae_u32 dmac_acr;
static volatile int dmac_wtc;
static volatile int dmac_dma;
static uae_u32 dma_mask;
static volatile int activate_stch, cdrom_command_done;
static volatile int cdrom_sector, cdrom_sectors, cdrom_length, cdrom_offset;
static volatile int cd_playing, cd_paused, cd_motor, cd_media, cd_error, cd_finished, cd_isready, cd_audio_finished;
static uae_u32 last_play_pos, last_play_end;
static volatile int cdtv_hsync, dma_finished, cdtv_sectorsize;
static volatile uae_u64 dma_wait;
static int cd_volume, cd_volume_stored;
static uae_u16 dac_control_data_format;
static int cd_led;
static int frontpanel;
static uae_u8 cdrom_command_input[16];
static int cdrom_command_cnt_in;
static uae_u8 tp_a, tp_b, tp_c, tp_ad, tp_bd, tp_cd;
static uae_u8 tp_imask, tp_cr, tp_air, tp_ilatch, tp_ilatch2;
static int volstrobe1, volstrobe2;
static void do_stch (void);
static void INT2 (void)
{
safe_interrupt_set(IRQ_SOURCE_CD32CDTV, 0, false);
cd_led ^= LED_CD_ACTIVE2;
}
static volatile int cdrom_command_cnt_out, cdrom_command_size_out;
static uae_u8 cdrom_command_output[16];
static volatile int stch, sten, scor, sbcp;
static volatile int cmd, enable, xaen, dten;
static int unitnum = -1;
static void subreset (void)
{
uae_sem_wait (&sub_sem);
memset (subcodebufferinuse, 0, sizeof subcodebufferinuse);
subcodebufferoffsetw = subcodebufferoffset = 0;
subcodeoffset = -1;
sbcp = 0;
scor = 0;
uae_sem_post (&sub_sem);
}
static int get_toc (void)
{
datatrack = 0;
if (!sys_command_cd_toc (unitnum, &toc))
return 0;
last_cd_position = toc.lastaddress;
if (toc.first_track == 1 && (toc.toc[toc.first_track_offset].control & 0x0c) == 0x04)
datatrack = 1;
return 1;
}
static int get_qcode (void)
{
if (!sys_command_cd_qcode (unitnum, cdrom_qcode, -1, false))
return 0;
cdrom_qcode[1] = cd_audio_status;
return 1;
}
static void cdaudiostop_do (void)
{
if (unitnum < 0)
return;
sys_command_cd_pause (unitnum, 0);
sys_command_cd_stop (unitnum);
}
static void cdaudiostop (void)
{
cd_finished = 0;
cd_audio_status = AUDIO_STATUS_NO_STATUS;
if (cd_playing) {
cd_audio_status = AUDIO_STATUS_PLAY_COMPLETE;
cd_finished = 1;
}
cd_playing = 0;
cd_paused = 0;
cd_motor = 0;
cd_audio_finished = 0;
write_comm_pipe_u32 (&requests, 0x0104, 1);
}
static void cdaudiostopfp (void)
{
cdaudiostop_do ();
cd_audio_status = AUDIO_STATUS_NO_STATUS;
activate_stch = 1;
cd_finished = 0;
cd_playing = 0;
cd_paused = 0;
cd_motor = 0;
}
static int pause_audio (int pause)
{
sys_command_cd_pause (unitnum, pause);
if (!cd_playing) {
cd_paused = 0;
cd_audio_status = AUDIO_STATUS_NO_STATUS;
return 0;
}
cd_paused = pause;
cd_audio_status = pause ? AUDIO_STATUS_PAUSED : AUDIO_STATUS_IN_PROGRESS;
subreset ();
return 1;
}
static int read_sectors (int start, int length)
{
if (cd_playing)
cdaudiostop ();
#ifdef CDTV_DEBUG_CMD
write_log (_T("READ DATA sector %d, %d sectors (blocksize=%d)\n"), start, length, cdtv_sectorsize);
#endif
cdrom_sector = start;
cdrom_sectors = length;
cdrom_offset = start * cdtv_sectorsize;
cdrom_length = length * cdtv_sectorsize;
cd_motor = 1;
cd_audio_status = AUDIO_STATUS_NOT_SUPPORTED;
return 0;
}
static int ismedia (void)
{
if (unitnum < 0)
return 0;
return sys_command_ismedia (unitnum, 0);
}
static int issub (void)
{
return 1;
}
static void subfunc (uae_u8 *data, int cnt)
{
if (!issub ())
return;
uae_sem_wait (&sub_sem);
#ifdef CDTV_SUB_DEBUG
int total = 0;
for (int i = 0; i < MAX_SUBCODEBUFFER; i++) {
if (subcodebufferinuse[i])
total++;
}
write_log (_T("%d "), total);
#endif
if (subcodebufferinuse[subcodebufferoffsetw]) {
memset (subcodebufferinuse, 0, sizeof subcodebufferinuse);
subcodebufferoffsetw = subcodebufferoffset = 0;
subcodeoffset = -1;
uae_sem_post (&sub_sem);
#ifdef CDTV_SUB_DEBUG
write_log (_T("CDTV: subcode buffer overflow 1\n"));
#endif
return;
}
int offset = subcodebufferoffsetw;
while (cnt > 0) {
if (subcodebufferinuse[offset]) {
#ifdef CDTV_SUB_DEBUG
write_log (_T("CDTV: subcode buffer overflow 2\n"));
#endif
break;
}
subcodebufferinuse[offset] = 1;
memcpy (&subcodebuffer[offset * SUB_CHANNEL_SIZE], data, SUB_CHANNEL_SIZE);
data += SUB_CHANNEL_SIZE;
offset++;
if (offset >= MAX_SUBCODEBUFFER)
offset = 0;
cnt--;
}
subcodebufferoffsetw = offset;
uae_sem_post (&sub_sem);
}
static int statusfunc (int status, int playpos)
{
if (status == -1)
return 150;
if (status == -2)
return 20;
if (status < 0)
return 0;
if (cd_audio_status != status) {
if (status == AUDIO_STATUS_PLAY_COMPLETE || status == AUDIO_STATUS_PLAY_ERROR) {
cd_audio_finished = 1;
} else {
if (status == AUDIO_STATUS_IN_PROGRESS)
cd_playing = 1;
activate_stch = 1;
}
}
if (status == AUDIO_STATUS_IN_PROGRESS)
last_play_pos = playpos;
cd_audio_status = status;
return 0;
}
static int statusfunc_imm(int status, int playpos)
{
if (status == -3 || status > AUDIO_STATUS_IN_PROGRESS)
uae_sem_post(&cda_sem);
if (status < 0)
return 0;
if (status == AUDIO_STATUS_IN_PROGRESS)
cd_audio_status = status;
return statusfunc(status, playpos);
}
static void do_play(bool immediate)
{
uae_u32 start = read_comm_pipe_u32_blocking (&requests);
uae_u32 end = read_comm_pipe_u32_blocking (&requests);
uae_u32 scan = read_comm_pipe_u32_blocking (&requests);
subreset ();
sys_command_cd_pause (unitnum, 0);
sys_command_cd_volume (unitnum, (cd_volume_stored << 5) | (cd_volume_stored >> 5), (cd_volume_stored << 5) | (cd_volume_stored >> 5));
sys_command_cd_play (unitnum, start, end, 0, immediate ? statusfunc_imm : statusfunc, subfunc);
}
static void startplay (void)
{
subreset ();
write_comm_pipe_u32 (&requests, 0x0110, 0);
write_comm_pipe_u32 (&requests, play_start, 0);
write_comm_pipe_u32 (&requests, play_end, 0);
write_comm_pipe_u32 (&requests, 0, 1);
if (!cd_motor) {
cd_motor = 1;
activate_stch = 1;
}
}
static int play_cdtrack (uae_u8 *p)
{
int track_start = p[1];
int index_start = p[2];
int track_end = p[3];
int index_end = p[4];
int start_found, end_found;
uae_u32 start, end;
int j;
if (track_start == 0 && track_end == 0)
return 0;
end = last_cd_position;
start_found = end_found = 0;
for (j = toc.first_track_offset; j <= toc.last_track_offset; j++) {
struct cd_toc *s = &toc.toc[j];
if (track_start == s->track) {
start_found++;
start = s->paddress;
}
if (track_end == s->track) {
end = s->paddress;
end_found++;
}
}
if (start_found == 0) {
cdaudiostop ();
cd_error = 1;
activate_stch = 1;
write_log (_T("PLAY CD AUDIO: illegal start track %d\n"), track_start);
return 0;
}
play_end = end;
play_start = start;
last_play_pos = start;
last_play_end = end;
#ifdef CDTV_DEBUG_CMD
write_log (_T("PLAY CD AUDIO from %d-%d, %06X (%d) to %06X (%d)\n"),
track_start, track_end, start, start, end, end);
#endif
startplay ();
return 0;
}
static int play_cd (uae_u8 *p)
{
uae_u32 start, end;
start = (p[1] << 16) | (p[2] << 8) | p[3];
end = (p[4] << 16) | (p[5] << 8) | p[6];
if (p[0] == 0x09) /* end is length in lsn-mode */
end += start;
if (start == 0 && end == 0) {
cd_finished = 0;
if (cd_playing)
cd_finished = 1;
cd_playing = 0;
cd_paused = 0;
cd_motor = 0;
write_comm_pipe_u32 (&requests, 0x0104, 1);
cd_audio_status = AUDIO_STATUS_NO_STATUS;
cd_error = 1;
activate_stch = 1;
return 0;
}
if (p[0] != 0x09) { /* msf */
start = msf2lsn (start);
if (end < 0x00ffffff)
end = msf2lsn (end);
}
if (end >= 0x00ffffff || end > last_cd_position)
end = last_cd_position;
play_end = end;
play_start = start;
last_play_pos = start;
last_play_end = end;
#ifdef CDTV_DEBUG_CMD
write_log (_T("PLAY CD AUDIO from %06X (%d) to %06X (%d)\n"),
lsn2msf (start), start, lsn2msf (end), end);
#endif
startplay ();
return 0;
}
static int cdrom_subq (uae_u8 *out, int msflsn)
{
uae_u8 *s = cdrom_qcode;
uae_u32 trackposlsn, trackposmsf;
uae_u32 diskposlsn, diskposmsf;
out[0] = cd_audio_status;
s += 4;
out[1] = (s[0] >> 4) | (s[0] << 4);
out[2] = frombcd (s[1]); // track
out[3] = frombcd (s[2]); // index
trackposmsf = fromlongbcd (s + 3);
diskposmsf = fromlongbcd (s + 7);
trackposlsn = msf2lsn (trackposmsf);
diskposlsn = msf2lsn (diskposmsf);
out[4] = 0;
out[5] = (msflsn ? diskposmsf : diskposlsn) >> 16;
out[6] = (msflsn ? diskposmsf : diskposlsn) >> 8;
out[7] = (msflsn ? diskposmsf : diskposlsn) >> 0;
out[8] = 0;
out[9] = (msflsn ? trackposmsf : trackposlsn) >> 16;
out[10] = (msflsn ? trackposmsf : trackposlsn) >> 8;
out[11] = (msflsn ? trackposmsf : trackposlsn) >> 0;
out[12] = 0;
if (cd_audio_status == AUDIO_STATUS_IN_PROGRESS)
last_play_pos = diskposlsn;
return 13;
}
static int cdrom_info (uae_u8 *out)
{
uae_u32 size;
if (ismedia () <= 0)
return -1;
cd_motor = 1;
out[0] = toc.first_track;
out[1] = toc.last_track;
size = lsn2msf (toc.lastaddress);
out[2] = size >> 16;
out[3] = size >> 8;
out[4] = size >> 0;
cd_finished = 1;
return 5;
}
static int read_toc (int track, int msflsn, uae_u8 *out)
{
int j;
if (ismedia () <= 0)
return -1;
if (!out)
return 0;
cd_motor = 1;
for (j = 0; j < toc.points; j++) {
if (track == toc.toc[j].point) {
int lsn = toc.toc[j].paddress;
int msf = lsn2msf (lsn);
out[0] = 0;
out[1] = (toc.toc[j].adr << 4) | (toc.toc[j].control << 0);
out[2] = toc.toc[j].point;
out[3] = toc.tracks;
out[4] = 0;
out[5] = (msflsn ? msf : lsn) >> 16;
out[6] = (msflsn ? msf : lsn) >> 8;
out[7] = (msflsn ? msf : lsn) >> 0;
cd_finished = 1;
return 8;
}
}
return -1;
}
static int cdrom_modeset (uae_u8 *cmd)
{
cdtv_sectorsize = (cmd[2] << 8) | cmd[3];
if (cdtv_sectorsize != 2048 && cdtv_sectorsize != 2336 && cdtv_sectorsize != 2352 && cdtv_sectorsize != 2328) {
write_log (_T("CDTV: tried to set unknown sector size %d\n"), cdtv_sectorsize);
cdtv_sectorsize = 2048;
}
return 0;
}
static void cdrom_command_accepted (int size, uae_u8 *cdrom_command_input, int *cdrom_command_cnt_in)
{
#ifdef CDTV_DEBUG_CMD
TCHAR tmp[200];
int i;
#endif
cdrom_command_size_out = size;
#ifdef CDTV_DEBUG_CMD
tmp[0] = 0;
for (i = 0; i < *cdrom_command_cnt_in; i++)
_stprintf (tmp + i * 3, _T("%02X%c"), cdrom_command_input[i], i < *cdrom_command_cnt_in - 1 ? '.' : ' ');
write_log (_T("CD<-: %s\n"), tmp);
if (size > 0) {
tmp[0] = 0;
for (i = 0; i < size; i++)
_stprintf (tmp + i * 3, _T("%02X%c"), cdrom_command_output[i], i < size - 1 ? '.' : ' ');
write_log (_T("CD->: %s\n"), tmp);
}
#endif
*cdrom_command_cnt_in = 0;
cdrom_command_cnt_out = 0;
cdrom_command_done = 1;
}
static void cdrom_command_thread (uae_u8 b)
{
uae_u8 *s;
cdrom_command_input[cdrom_command_cnt_in] = b;
cdrom_command_cnt_in++;
s = cdrom_command_input;
switch (cdrom_command_input[0])
{
case 0x01: /* seek */
if (cdrom_command_cnt_in == 7) {
cdrom_command_accepted (0, s, &cdrom_command_cnt_in);
cd_finished = 1;
if (currprefs.cd_speed)
sleep_millis (500);
activate_stch = 1;
}
break;
case 0x02: /* read */
if (cdrom_command_cnt_in == 7) {
read_sectors ((s[1] << 16) | (s[2] << 8) | (s[3] << 0), (s[4] << 8) | (s[5] << 0));
cdrom_command_accepted (0, s, &cdrom_command_cnt_in);
}
break;
case 0x04: /* motor on */
if (cdrom_command_cnt_in == 7) {
cd_motor = 1;
cdrom_command_accepted (0, s, &cdrom_command_cnt_in);
cd_finished = 1;
}
break;
case 0x05: /* motor off */
if (cdrom_command_cnt_in == 7) {
cd_motor = 0;
cdrom_command_accepted (0, s, &cdrom_command_cnt_in);
cd_finished = 1;
}
break;
case 0x09: /* play (lsn) */
case 0x0a: /* play (msf) */
if (cdrom_command_cnt_in == 7) {
cdrom_command_accepted (play_cd (cdrom_command_input), s, &cdrom_command_cnt_in);
}
break;
case 0x0b:
if (cdrom_command_cnt_in == 7) {
cdrom_command_accepted (play_cdtrack (cdrom_command_input), s, &cdrom_command_cnt_in);
}
break;
case 0x81:
if (cdrom_command_cnt_in == 1) {
uae_u8 flag = 0;
if (!cd_isready)
flag |= 1 << 0; // 01
if (cd_playing)
flag |= 1 << 2; // 04
if (cd_finished)
flag |= 1 << 3; // 08
if (cd_error)
flag |= 1 << 4; // 10
if (cd_motor)
flag |= 1 << 5; // 20
if (cd_media)
flag |= 1 << 6; // 40
cdrom_command_output[0] = flag;
cdrom_command_accepted (1, s, &cdrom_command_cnt_in);
cd_finished = 0;
}
break;
case 0x82:
if (cdrom_command_cnt_in == 7) {
if (cd_error)
cdrom_command_output[2] |= 1 << 4;
cd_error = 0;
cd_isready = 0;
cdrom_command_accepted (6, s, &cdrom_command_cnt_in);
cd_finished = 1;
}
break;
case 0x83:
if (cdrom_command_cnt_in == 7) {
memcpy (cdrom_command_output, MODEL_NAME, uaestrlen(MODEL_NAME));
cdrom_command_accepted (uaestrlen(MODEL_NAME), s, &cdrom_command_cnt_in);
cd_finished = 1;
}
case 0x84:
if (cdrom_command_cnt_in == 7) {
cdrom_command_accepted (cdrom_modeset (cdrom_command_input), s, &cdrom_command_cnt_in);
cd_finished = 1;
}
break;
case 0x87: /* subq */
if (cdrom_command_cnt_in == 7) {
cdrom_command_accepted (cdrom_subq (cdrom_command_output, cdrom_command_input[1] & 2), s, &cdrom_command_cnt_in);
}
break;
case 0x89:
if (cdrom_command_cnt_in == 7) {
cdrom_command_accepted (cdrom_info (cdrom_command_output), s, &cdrom_command_cnt_in);
}
break;
case 0x8a: /* read toc */
if (cdrom_command_cnt_in == 7) {
cdrom_command_accepted (read_toc (cdrom_command_input[2], cdrom_command_input[1] & 2, cdrom_command_output), s, &cdrom_command_cnt_in);
}
break;
case 0x8b:
if (cdrom_command_cnt_in == 7) {
pause_audio (s[1] == 0x00 ? 1 : 0);
cdrom_command_accepted (0, s, &cdrom_command_cnt_in);
cd_finished = 1;
}
break;
case 0xa3: /* front panel */
if (cdrom_command_cnt_in == 7) {
frontpanel = s[1] ? 1 : 0;
cdrom_command_accepted (0, s, &cdrom_command_cnt_in);
cd_finished = 1;
}
break;
default:
write_log (_T("unknown CDROM command %02X!\n"), s[0]);
cd_error = 1;
cdrom_command_accepted (0, s, &cdrom_command_cnt_in);
break;
}
}
static void dma_do_thread (void)
{
static int readsector;
int didread = 0;
int cnt;
while (dma_finished)
sleep_millis (2);
if (!cdtv_sectorsize)
return;
cnt = dmac_wtc;
#ifdef CDTV_DEBUG_CMD
write_log (_T("DMAC DMA: sector=%d, addr=%08X, words=%d (of %d)\n"),
cdrom_offset / cdtv_sectorsize, dmac_acr, cnt, cdrom_length / 2);
#endif
dma_wait += cnt * (uae_u64)312 * 50 / 75 + 1;
if (currprefs.cd_speed == 0)
dma_wait = 1;
while (cnt > 0 && dmac_dma) {
uae_u8 buffer[2352];
if (!didread || readsector != (cdrom_offset / cdtv_sectorsize)) {
readsector = cdrom_offset / cdtv_sectorsize;
if (cdtv_sectorsize != 2048)
didread = sys_command_cd_rawread (unitnum, buffer, readsector, 1, cdtv_sectorsize);
else
didread = sys_command_cd_read (unitnum, buffer, readsector, 1);
if (!didread) {
cd_error = 1;
activate_stch = 1;
write_log (_T("CDTV: CD read error!\n"));
break;
}
}
dma_put_byte(dmac_acr & dma_mask, buffer[(cdrom_offset % cdtv_sectorsize) + 0]);
dma_put_byte((dmac_acr + 1) & dma_mask, buffer[(cdrom_offset % cdtv_sectorsize) + 1]);
cnt--;
dmac_acr += 2;
cdrom_length -= 2;
cdrom_offset += 2;
}
dmac_wtc = 0;
dmac_dma = 0;
dma_finished = 1;
cd_finished = 1;
}
static void dev_thread (void *p)
{
write_log (_T("CDTV: CD thread started\n"));
thread_alive = 1;
for (;;) {
uae_u32 b = read_comm_pipe_u32_blocking (&requests);
if (b == 0xffff) {
thread_alive = -1;
return;
}
if (unitnum < 0)
continue;
switch (b)
{
case 0x0100:
dma_do_thread ();
break;
case 0x0101:
{
int m = ismedia ();
if (m < 0) {
write_log (_T("CDTV: device %d lost\n"), unitnum);
activate_stch = 1;
cd_media = 0;
} else if (m != cd_media) {
cd_media = m;
get_toc ();
activate_stch = 1;
if (cd_playing)
cd_error = 1;
}
if (cd_media)
get_qcode ();
}
break;
case 0x0102: // pause
sys_command_cd_pause (unitnum, 1);
break;
case 0x0103: // unpause
sys_command_cd_pause (unitnum, 0);
break;
case 0x0104: // stop
cdaudiostop_do ();
break;
case 0x0105: // pause
pause_audio (1);
break;
case 0x0106: // unpause
pause_audio (0);
break;
case 0x0107: // frontpanel stop
cdaudiostopfp ();
break;
case 0x0110: // do_play!
do_play(false);
break;
case 0x0111: // do_play immediate
do_play(true);
break;
default:
cdrom_command_thread (b);
break;
}
}
}
static void cdrom_command (uae_u8 b)
{
write_comm_pipe_u32 (&requests, b, 1);
}
static void init_play (int start, int end)
{
play_end = end;
play_start = start;
last_play_pos = start;
last_play_end = end;
#ifdef CDTV_DEBUG_CMD
write_log (_T("PLAY CD AUDIO from %06X (%d) to %06X (%d)\n"),
lsn2msf (start), start, lsn2msf (end), end);
#endif
startplay ();
}
bool cdtv_front_panel (int button)
{
if (!frontpanel || configured <= 0)
return false;
if (button < 0)
return true;
switch (button)
{
case 0: // stop
if (cd_paused)
write_comm_pipe_u32 (&requests, 0x0106, 1);
write_comm_pipe_u32 (&requests, 0x0107, 1);
break;
case 1: // playpause
if (cd_playing) {
write_comm_pipe_u32 (&requests, cd_paused ? 0x0106 : 0x0105, 1);
} else if (cd_media) {
init_play (0, last_cd_position);
}
break;
case 2: // prev
case 3: // next
if (!cd_playing)
return true;
uae_u8 *sq = cdrom_qcode + 4;
int track = frombcd (sq[1]);
int pos = 0;
for (int j = 0; j < toc.points; j++) {
int t = toc.toc[j].track;
pos = toc.toc[j].paddress;
if (t == 1 && track == 1 && button == 2)
break;
else if (j == toc.points - 1 && t == track && button == 3)
break;
else if (t == track - 1 && track > 1 && button == 2)
break;
else if (t == track + 1 && track < 99 && button == 3)
break;
}
init_play (pos - 150, last_cd_position);
break;
}
return true;
}
static uae_u8 get_tp_c (void)
{
uae_u8 v = (sbcp ? 0 : (1 << 0)) | (scor ? 0 : (1 << 1)) |
(stch ? 0 : (1 << 2)) | (sten ? 0 : (1 << 3) | (1 << 4));
return v;
}
static uae_u8 get_tp_c_level (void)
{
uae_u8 v = (sbcp == 1 ? 0 : (1 << 0)) | (scor == 1 ? 0 : (1 << 1)) |
(stch == 1 ? 0 : (1 << 2)) | (sten == 1 ? 0 : (1 << 3)) | (1 << 4);
if (sten == 1)
sten = -1;
if (scor == 1)
scor = -1;
if (sbcp == 1)
sbcp = -1;
return v;
}
static void tp_check_interrupts (void)
{
/* MC = 1 ? */
if ((tp_cr & 1) != 1) {
get_tp_c_level ();
return;
}
tp_ilatch |= get_tp_c_level () ^ 0x1f;
stch = 0;
if (!(tp_ilatch & (1 << 5)) && (tp_ilatch & tp_imask)) {
tp_air = 0;
int mask = 0x10;
while (((tp_ilatch & tp_imask) & mask) == 0)
mask >>= 1;
tp_air |= tp_ilatch & mask;
tp_ilatch |= 1 << 5; // IRQ
tp_ilatch2 = tp_ilatch & mask;
tp_ilatch &= ~mask;
}
if (tp_ilatch & (1 << 5))
INT2 ();
}
// MC=1, C lines 0-4 = input irq lines, 5 = irq out, 6-7 IO
static void tp_bput (int addr, uae_u8 v)
{
#ifdef CDTV_DEBUG_6525
if (addr != 1)
write_log (_T("6525 write %x=%02X PC=%x %d\n"), addr, v, M68K_GETPC, regs.s);
#endif
switch (addr)
{
case 0:
tp_a = v;
break;
case 1:
tp_b = v;
break;
case 2:
if (tp_cr & 1) {
// 0 = clear, 1 = ignored
tp_ilatch &= 0xe0 | v;
} else {
tp_c = get_tp_c () & ~tp_cd;
tp_c |= v & tp_cd;
if (tp_c & (1 << 5))
INT2 ();
}
break;
case 3:
tp_ad = v;
break;
case 4:
tp_bd = v;
break;
case 5:
// data direction (mode=0), interrupt mask (mode=1)
if (tp_cr & 1) {
tp_imask = v & 0x1f;
} else {
tp_cd = v;
}
break;
case 6:
tp_cr = v;
break;
case 7:
tp_air = v;
break;
}
cmd = (tp_b >> 0) & 1;
enable = (tp_b >> 1) & 1;
xaen = (tp_b >> 2) & 1;
dten = (tp_b >> 3) & 1;
if (!volstrobe1 && ((tp_b >> 6) & 1)) {
dac_control_data_format >>= 1;
dac_control_data_format |= ((tp_b >> 5) & 1) << 11;
volstrobe1 = 1;
} else if (volstrobe1 && !((tp_b >> 6) & 1)) {
volstrobe1 = 0;
}
if (!volstrobe2 && ((tp_b >> 7) & 1)) {
#ifdef CDTV_DEBUG_CMD
write_log (_T("CDTV CD volume = %d\n"), cd_volume);
#endif
cd_volume = dac_control_data_format & 1023;
if (unitnum >= 0)
sys_command_cd_volume (unitnum, (cd_volume << 5) | (cd_volume >> 5), (cd_volume << 5) | (cd_volume >> 5));
cd_volume_stored = cd_volume;
volstrobe2 = 1;
} else if (volstrobe2 && !((tp_b >> 7) & 1)) {
volstrobe2 = 0;
}
tp_check_interrupts ();
}
static uae_u8 subtransferbuf[SUB_CHANNEL_SIZE];
#define SUBCODE_CYCLES (2 * maxhpos)
static int subcode_activecnt;
static void subcode_interrupt (uae_u32 v)
{
subcode_activecnt--;
if (subcode_activecnt > 0) {
if (subcode_activecnt > 1)
subcode_activecnt = 1;
return;
}
if (subcodeoffset < -1)
return;
if (sbcp && scor == 0) {
sbcp = 0;
// CD+G interrupt didn't read data fast enough, just abort until next packet
return;
}
if (scor < 0) {
scor = 0;
if (issub ()) {
subcodeoffset = 0;
}
tp_check_interrupts ();
}
if (subcodeoffset >= SUB_CHANNEL_SIZE)
return;
sbcp = 1;
tp_check_interrupts ();
subcode_activecnt++;
event2_newevent2 (SUBCODE_CYCLES, 0, subcode_interrupt);
}
static uae_u8 tp_bget (int addr)
{
uae_u8 v = 0;
switch (addr)
{
case 0:
// A = subchannel byte input from serial to parallel converter
if (subcodeoffset < 0 || subcodeoffset >= SUB_CHANNEL_SIZE) {
#ifdef CDTV_SUB_DEBUG
write_log (_T("CDTV: requested non-existing subchannel data!? %d\n"), subcodeoffset);
#endif
v = 0;
} else {
v = subtransferbuf[subcodeoffset];
tp_a = 0;
tp_a |= (v >> 7) & 1;
tp_a |= (v >> 5) & 2;
tp_a |= (v >> 3) & 4;
tp_a |= (v >> 1) & 8;