forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathakiko.cpp
1859 lines (1704 loc) · 43.6 KB
/
akiko.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
*
* CD32 Akiko emulation
*
* - C2P
* - NVRAM
* - CDROM
*
* Copyright 2001-2010 Toni Wilen
*
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "memory.h"
#include "events.h"
#include "savestate.h"
#include "blkdev.h"
#include "zfile.h"
#include "threaddep/thread.h"
#include "akiko.h"
#include "gui.h"
#include "crc32.h"
#include "uae.h"
#include "custom.h"
#include "newcpu.h"
#define AKIKO_DEBUG_NVRAM 0
#define AKIKO_DEBUG_IO 0
#define AKIKO_DEBUG_IO_CMD 0
// 43 48 49 4E 4F 4E 20 20 4F 2D 36 35 38 2D 32 20 32 34
#define FIRMWAREVERSION "CHINON O-658-2 24"
static void irq (void)
{
#if AKIKO_DEBUG_IO > 1
write_log (L"Akiko Interrupt\n");
#endif
if (!(intreq & 8)) {
INTREQ_0 (0x8000 | 0x0008);
}
}
/*
* CD32 1Kb NVRAM (EEPROM) emulation
*
* NVRAM chip is 24C08 CMOS EEPROM (1024x8 bits = 1Kb)
* Chip interface is I2C (2 wire serial)
* Akiko addresses used:
* 0xb80030: bit 7 = SCL (clock), 6 = SDA (data)
* 0xb80032: 0xb80030 data direction register (0 = input, 1 = output)
*
* Because I don't have any experience on I2C, following code may be
* unnecessarily complex and not 100% correct..
*/
enum i2c { I2C_WAIT, I2C_START, I2C_DEVICEADDR, I2C_WORDADDR, I2C_DATA };
/* size of EEPROM, don't try to change,
* (hardcoded in Kickstart)
*/
#define NVRAM_SIZE 1024
/* max size of one write request */
#define NVRAM_PAGE_SIZE 16
static uae_u8 cd32_nvram[NVRAM_SIZE], nvram_writetmp[NVRAM_PAGE_SIZE];
static int nvram_address, nvram_writeaddr;
static int nvram_rw;
static int bitcounter = -1, direction = -1;
static uae_u8 nvram_byte;
static int scl_out, scl_in, scl_dir, oscl, sda_out, sda_in, sda_dir, osda;
static int sda_dir_nvram;
static int state = I2C_WAIT;
static void nvram_write (int offset, int len)
{
struct zfile *f;
if (!currprefs.cs_cd32nvram)
return;
f = zfile_fopen (currprefs.flashfile, L"rb+", ZFD_NORMAL);
if (!f) {
f = zfile_fopen (currprefs.flashfile, L"wb", 0);
if (!f) return;
zfile_fwrite (cd32_nvram, NVRAM_SIZE, 1, f);
}
zfile_fseek (f, offset, SEEK_SET);
zfile_fwrite (cd32_nvram + offset, len, 1, f);
zfile_fclose (f);
}
static void nvram_read (void)
{
struct zfile *f;
if (!currprefs.cs_cd32nvram)
return;
f = zfile_fopen (currprefs.flashfile, L"rb", ZFD_NORMAL);
memset (cd32_nvram, 0, NVRAM_SIZE);
if (!f) return;
zfile_fread (cd32_nvram, NVRAM_SIZE, 1, f);
zfile_fclose (f);
}
static void i2c_do (void)
{
#if AKIKO_DEBUG_NVRAM
int i;
#endif
sda_in = 1;
if (!sda_dir_nvram && scl_out && oscl) {
if (!sda_out && osda) { /* START-condition? */
state = I2C_DEVICEADDR;
bitcounter = 0;
direction = -1;
#if AKIKO_DEBUG_NVRAM
write_log (L"START\n");
#endif
return;
} else if(sda_out && !osda) { /* STOP-condition? */
state = I2C_WAIT;
bitcounter = -1;
#if AKIKO_DEBUG_NVRAM
write_log (L"STOP\n");
#endif
if (direction > 0) {
memcpy (cd32_nvram + (nvram_address & ~(NVRAM_PAGE_SIZE - 1)), nvram_writetmp, NVRAM_PAGE_SIZE);
nvram_write (nvram_address & ~(NVRAM_PAGE_SIZE - 1), NVRAM_PAGE_SIZE);
direction = -1;
gui_flicker_led (LED_MD, 0, 2);
#if AKIKO_DEBUG_NVRAM
write_log (L"NVRAM write address %04X:", nvram_address & ~(NVRAM_PAGE_SIZE - 1));
for (i = 0; i < NVRAM_PAGE_SIZE; i++)
write_log (L"%02X", nvram_writetmp[i]);
write_log (L"\n");
#endif
}
return;
}
}
if (bitcounter >= 0) {
if (direction) {
/* Amiga -> NVRAM */
if (scl_out && !oscl) {
if (bitcounter == 8) {
#if AKIKO_DEBUG_NVRAM
write_log (L"RB %02X ", nvram_byte, M68K_GETPC);
#endif
sda_in = 0; /* ACK */
if (direction > 0) {
nvram_writetmp[nvram_writeaddr++] = nvram_byte;
nvram_writeaddr &= 15;
bitcounter = 0;
} else {
bitcounter = -1;
}
} else {
//write_log (L"NVRAM received bit %d, offset %d\n", sda_out, bitcounter);
nvram_byte <<= 1;
nvram_byte |= sda_out;
bitcounter++;
}
}
} else {
/* NVRAM -> Amiga */
if (scl_out && !oscl && bitcounter < 8) {
if (bitcounter == 0)
nvram_byte = cd32_nvram[nvram_address];
sda_dir_nvram = 1;
sda_in = (nvram_byte & 0x80) ? 1 : 0;
//write_log (L"NVRAM sent bit %d, offset %d\n", sda_in, bitcounter);
nvram_byte <<= 1;
bitcounter++;
if (bitcounter == 8) {
#if AKIKO_DEBUG_NVRAM
write_log (L"NVRAM sent byte %02X address %04X PC=%08X\n", cd32_nvram[nvram_address], nvram_address, M68K_GETPC);
#endif
nvram_address++;
nvram_address &= NVRAM_SIZE - 1;
sda_dir_nvram = 0;
}
}
if(!sda_out && sda_dir && !scl_out) /* ACK from Amiga */
bitcounter = 0;
}
if (bitcounter >= 0) return;
}
switch (state)
{
case I2C_DEVICEADDR:
if ((nvram_byte & 0xf0) != 0xa0) {
write_log (L"WARNING: I2C_DEVICEADDR: device address != 0xA0\n");
state = I2C_WAIT;
return;
}
nvram_rw = (nvram_byte & 1) ? 0 : 1;
if (nvram_rw) {
/* 2 high address bits, only fetched if WRITE = 1 */
nvram_address &= 0xff;
nvram_address |= ((nvram_byte >> 1) & 3) << 8;
state = I2C_WORDADDR;
direction = -1;
} else {
state = I2C_DATA;
direction = 0;
sda_dir_nvram = 1;
}
bitcounter = 0;
#if AKIKO_DEBUG_NVRAM
write_log (L"I2C_DEVICEADDR: rw %d, address %02Xxx PC=%08X\n", nvram_rw, nvram_address >> 8, M68K_GETPC);
#endif
break;
case I2C_WORDADDR:
nvram_address &= 0x300;
nvram_address |= nvram_byte;
#if AKIKO_DEBUG_NVRAM
write_log (L"I2C_WORDADDR: address %04X PC=%08X\n", nvram_address, M68K_GETPC);
#endif
if (direction < 0) {
memcpy (nvram_writetmp, cd32_nvram + (nvram_address & ~(NVRAM_PAGE_SIZE - 1)), NVRAM_PAGE_SIZE);
nvram_writeaddr = nvram_address & (NVRAM_PAGE_SIZE - 1);
gui_flicker_led (LED_MD, 0, 1);
}
state = I2C_DATA;
bitcounter = 0;
direction = 1;
break;
}
}
static void akiko_nvram_write (int offset, uae_u32 v)
{
int sda;
switch (offset)
{
case 0:
oscl = scl_out;
scl_out = (v & 0x80) ? 1 : 0;
osda = sda_out;
sda_out = (v & 0x40) ? 1 : 0;
break;
case 2:
scl_dir = (v & 0x80) ? 1 : 0;
sda_dir = (v & 0x40) ? 1 : 0;
break;
default:
return;
}
sda = sda_out;
if (oscl != scl_out || osda != sda) {
i2c_do ();
oscl = scl_out;
osda = sda;
}
}
static uae_u32 akiko_nvram_read (int offset)
{
uae_u32 v = 0;
switch (offset)
{
case 0:
if (!scl_dir)
v |= scl_in ? 0x80 : 0x00;
else
v |= scl_out ? 0x80 : 0x00;
if (!sda_dir)
v |= sda_in ? 0x40 : 0x00;
else
v |= sda_out ? 0x40 : 0x00;
break;
case 2:
v |= scl_dir ? 0x80 : 0x00;
v |= sda_dir ? 0x40 : 0x00;
break;
}
return v;
}
/* CD32 Chunky to Planar hardware emulation
* Akiko addresses used:
* 0xb80038-0xb8003b
*/
static uae_u32 akiko_buffer[8];
static int akiko_read_offset, akiko_write_offset;
static uae_u32 akiko_result[8];
static void akiko_c2p_do (void)
{
int i;
for (i = 0; i < 8; i++)
akiko_result[i] = 0;
/* FIXME: better c2p algoritm than this piece of crap.... */
for (i = 0; i < 8 * 32; i++) {
if (akiko_buffer[7 - (i >> 5)] & (1 << (i & 31)))
akiko_result[i & 7] |= 1 << (i >> 3);
}
}
static void akiko_c2p_write (int offset, uae_u32 v)
{
if (offset == 3)
akiko_buffer[akiko_write_offset] = 0;
akiko_buffer[akiko_write_offset] |= v << ( 8 * (3 - offset));
if (offset == 0) {
akiko_write_offset++;
akiko_write_offset &= 7;
}
akiko_read_offset = 0;
}
static uae_u32 akiko_c2p_read (int offset)
{
uae_u32 v;
if (akiko_read_offset == 0 && offset == 3)
akiko_c2p_do ();
akiko_write_offset = 0;
v = akiko_result[akiko_read_offset];
if (offset == 0) {
akiko_read_offset++;
akiko_read_offset &= 7;
}
return v >> (8 * (3 - offset));
}
/* CD32 CDROM hardware emulation
* Akiko addresses used:
* 0xb80004-0xb80028
*/
#define CDINTERRUPT_SUBCODE 0x80000000
#define CDINTERRUPT_DRIVEXMIT 0x40000000 /* not used by ROM */
#define CDINTERRUPT_DRIVERECV 0x20000000 /* not used by ROM */
#define CDINTERRUPT_RXDMADONE 0x10000000
#define CDINTERRUPT_TXDMADONE 0x08000000
#define CDINTERRUPT_PBX 0x04000000
#define CDINTERRUPT_OVERFLOW 0x02000000
#define CDFLAG_SUBCODE 0x80000000
#define CDFLAG_TXD 0x40000000
#define CDFLAG_RXD 0x20000000
#define CDFLAG_CAS 0x10000000
#define CDFLAG_PBX 0x08000000
#define CDFLAG_ENABLE 0x04000000
#define CDFLAG_RAW 0x02000000
#define CDFLAG_MSB 0x01000000
#define CDS_ERROR 0x80
#define CDS_PLAYING 0x08
#define CDS_PLAYEND 0x00
#define CH_ERR_BADCOMMAND 0x80 // %10000000
#define CH_ERR_CHECKSUM 0x88 // %10001000
#define CH_ERR_DRAWERSTUCK 0x90 // %10010000
#define CH_ERR_DISKUNREADABLE 0x98 // %10011000
#define CH_ERR_INVALIDADDRESS 0xa0 // %10100000
#define CH_ERR_WRONGDATA 0xa8 // %10101000
#define CH_ERR_FOCUSERROR 0xc8 // %11001000
#define CH_ERR_SPINDLEERROR 0xd0 // %11010000
#define CH_ERR_TRACKINGERROR 0xd8 // %11011000
#define CH_ERR_SLEDERROR 0xe0 // %11100000
#define CH_ERR_TRACKJUMP 0xe8 // %11101000
#define CH_ERR_ABNORMALSEEK 0xf0 // %11110000
#define CH_ERR_NODISK 0xf8 // %11111000
static int subcodecounter;
#define MAX_SUBCODEBUFFER 20
static volatile int subcodebufferoffset, subcodebufferoffsetw;
static uae_u8 subcodebufferinuse[MAX_SUBCODEBUFFER];
static uae_u8 subcodebuffer[MAX_SUBCODEBUFFER * SUB_CHANNEL_SIZE];
static uae_u32 cdrom_intreq, cdrom_intena;
static uae_u8 cdrom_subcodeoffset;
static uae_u32 cdrom_addressdata, cdrom_addressmisc;
static uae_u32 subcode_address, cdrx_address, cdtx_address;
static uae_u32 cdrom_flags;
static uae_u32 cdrom_pbx;
static uae_u8 cdcomtxinx; /* 0x19 */
static uae_u8 cdcomrxinx; /* 0x1a */
static uae_u8 cdcomtxcmp; /* 0x1d */
static uae_u8 cdrom_result_buffer[32];
static uae_u8 cdrom_command_buffer[32];
static uae_u8 cdrom_command;
static int cdrom_toc_counter;
static uae_u32 cdrom_toc_crc;
static uae_u8 cdrom_toc_buffer[MAX_TOC_ENTRIES * 13];
static struct cd_toc_head cdrom_toc_cd_buffer;
static uae_u8 qcode_buf[SUBQ_SIZE];
static int qcode_valid;
static int cdrom_disk, cdrom_paused, cdrom_playing, cdrom_audiostatus;
static int cdrom_command_active;
static int cdrom_command_length;
static int cdrom_checksum_error, cdrom_unknown_command;
static int cdrom_data_offset, cdrom_speed, cdrom_sector_counter;
static int cdrom_current_sector, cdrom_seek_delay;
static int cdrom_data_end;
static int cdrom_audiotimeout;
static int cdrom_led;
static int cdrom_dosomething;
static int cdrom_receive_started;
static int cdrom_muted;
static int cd_initialized;
static uae_u8 *sector_buffer_1, *sector_buffer_2;
static int sector_buffer_sector_1, sector_buffer_sector_2;
#define SECTOR_BUFFER_SIZE 64
static uae_u8 *sector_buffer_info_1, *sector_buffer_info_2;
static int unitnum = -1;
static bool akiko_inited;
static volatile int mediachanged, mediacheckcounter;
static volatile int frame2counter;
static smp_comm_pipe requests;
static volatile int akiko_thread_running;
static uae_sem_t akiko_sem, sub_sem;
static void checkint (void)
{
if (cdrom_intreq & cdrom_intena)
irq ();
}
static void set_status (uae_u32 status)
{
cdrom_intreq |= status;
checkint ();
cdrom_led ^= LED_CD_ACTIVE2;
}
void rethink_akiko (void)
{
checkint ();
}
static void cdaudiostop_do (void)
{
qcode_valid = 0;
if (unitnum < 0)
return;
sys_command_cd_pause (unitnum, 0);
sys_command_cd_stop (unitnum);
}
static void cdaudiostop (void)
{
cdrom_audiostatus = 0;
cdrom_audiotimeout = 0;
cdrom_paused = 0;
cdrom_playing = 0;
write_comm_pipe_u32 (&requests, 0x0104, 1);
}
static void subfunc (uae_u8 *data, int cnt)
{
if (!(cdrom_flags & CDFLAG_SUBCODE))
return;
uae_sem_wait (&sub_sem);
#if 0
int total = 0;
for (int i = 0; i < MAX_SUBCODEBUFFER; i++) {
if (subcodebufferinuse[i])
total++;
}
write_log (L"%d ", total);
#endif
if (subcodebufferinuse[subcodebufferoffsetw]) {
memset (subcodebufferinuse, 0,sizeof (subcodebufferinuse));
subcodebufferoffsetw = subcodebufferoffset = 0;
uae_sem_post (&sub_sem);
write_log (L"CD32: subcode buffer overflow 1\n");
return;
}
int offset = subcodebufferoffsetw;
while (cnt > 0) {
if (subcodebufferinuse[offset]) {
write_log (L"CD32: subcode buffer overflow 2\n");
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)
{
if (status == -1)
return 0;
if (status == -2)
return 150;
if (cdrom_audiostatus != status) {
if (status == AUDIO_STATUS_IN_PROGRESS) {
cdrom_playing = 1;
cdrom_audiotimeout = 1;
}
if (cdrom_playing && status != AUDIO_STATUS_IN_PROGRESS && status != AUDIO_STATUS_PAUSED) {
cdrom_audiotimeout = -1;
}
}
cdrom_audiostatus = status;
return 0;
}
static void cdaudioplay_do (void)
{
uae_u32 startlsn = read_comm_pipe_u32_blocking (&requests);
uae_u32 endlsn = read_comm_pipe_u32_blocking (&requests);
uae_u32 scan = read_comm_pipe_u32_blocking (&requests);
qcode_valid = 0;
if (unitnum < 0)
return;
sys_command_cd_pause (unitnum, 0);
sys_command_cd_play (unitnum, startlsn, endlsn, scan, statusfunc, subfunc);
}
static bool isaudiotrack (int startlsn)
{
struct cd_toc *s = NULL;
uae_u32 addr;
int i;
if (!cdrom_toc_cd_buffer.points)
return false;
for (i = 0; i < cdrom_toc_cd_buffer.points; i++) {
s = &cdrom_toc_cd_buffer.toc[i];
addr = s->paddress;
if (s->track > 0 && s->track < 100 && addr >= startlsn)
break;
s++;
}
if (s && (s->control & 0x0c) == 0x04) {
write_log (L"tried to play data track %d!\n", s->track);
return false;
}
return true;
}
static struct cd_toc *get_track (int startlsn)
{
for (int i = cdrom_toc_cd_buffer.first_track_offset + 1; i <= cdrom_toc_cd_buffer.last_track_offset; i++) {
struct cd_toc *s = &cdrom_toc_cd_buffer.toc[i];
uae_u32 addr = s->paddress;
if (startlsn < addr)
return s - 1;
}
return NULL;
}
static int last_play_end;
static int cd_play_audio (int startlsn, int endlsn, int scan)
{
struct cd_toc *s = NULL;
if (!cdrom_toc_cd_buffer.points)
return 0;
s = get_track (startlsn);
if (s && (s->control & 0x0c) == 0x04) {
s = get_track (startlsn + 150);
if (s && (s->control & 0x0c) == 0x04) {
write_log (L"tried to play data track %d!\n", s->track);
s++;
startlsn = s->paddress;
s++;
endlsn = s->paddress;
}
startlsn = s->paddress;
}
qcode_valid = 0;
last_play_end = endlsn;
cdrom_audiotimeout = 0;
cdrom_paused = 0;
write_comm_pipe_u32 (&requests, 0x0110, 0);
write_comm_pipe_u32 (&requests, startlsn, 0);
write_comm_pipe_u32 (&requests, endlsn, 0);
write_comm_pipe_u32 (&requests, scan, 1);
return 1;
}
/* read qcode */
static int last_play_pos;
static int cd_qcode (uae_u8 *d)
{
uae_u8 *buf, *s, as;
if (d)
memset (d, 0, 11);
last_play_pos = 0;
buf = qcode_buf;
as = buf[1];
buf[2] = 0x80;
if (!qcode_valid)
return 0;
if (cdrom_audiostatus != AUDIO_STATUS_IN_PROGRESS && cdrom_audiostatus != AUDIO_STATUS_PAUSED)
return 0;
s = buf + 4;
last_play_pos = msf2lsn (fromlongbcd (s + 7));
if (!d)
return 0;
buf[2] = 0;
/* ??? */
d[0] = 0;
/* CtlAdr */
d[1] = s[0];
/* Track */
d[2] = s[1];
/* Index */
d[3] = s[2];
/* TrackPos */
d[4] = s[3];
d[5] = s[4];
d[6] = s[5];
/* DiskPos */
d[7] = 0;
d[8] = s[7];
d[9] = s[8];
d[10] = s[9];
if (as == AUDIO_STATUS_IN_PROGRESS) {
/* Make sure end of disc position is not missed.
*/
if (last_play_pos >= cdrom_toc_cd_buffer.lastaddress || cdrom_toc_cd_buffer.lastaddress - last_play_pos < 10) {
int msf = lsn2msf (cdrom_toc_cd_buffer.lastaddress);
d[8] = tobcd ((uae_u8)(msf >> 16));
d[9] = tobcd ((uae_u8)(msf >> 8));
d[10] = tobcd ((uae_u8)(msf >> 0));
}
}
// write_log (L"%02X.%02X.%02X.%02X.%02X.%02X.%02X.%02X.%02X.%02X.%02X.%02X\n",
// d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], d[8], d[9], d[10], d[11]);
return 0;
}
/* read toc */
static int get_cdrom_toc (void)
{
int j;
int datatrack = 0, secondtrack = 0;
cdrom_toc_counter = -1;
if (!sys_command_cd_toc (unitnum, &cdrom_toc_cd_buffer))
return 1;
memset (cdrom_toc_buffer, 0, MAX_TOC_ENTRIES * 13);
cdrom_data_end = -1;
for (j = 0; j < cdrom_toc_cd_buffer.points; j++) {
struct cd_toc *s = &cdrom_toc_cd_buffer.toc[j];
uae_u8 *d = &cdrom_toc_buffer[j * 13];
int addr = s->paddress;
int msf = lsn2msf (addr);
if (s->point == 0xa0 || s->point == 0xa1)
msf = s->track << 16;
d[1] = (s->adr << 0) | (s->control << 4);
d[3] = s->point < 100 ? tobcd (s->point) : s->point;
d[8] = tobcd ((msf >> 16) & 0xff);
d[9] = tobcd ((msf >> 8) & 0xff);
d[10] = tobcd ((msf >> 0) & 0xff);
if (s->point == 1 && (s->control & 0x0c) == 0x04)
datatrack = 1;
if (s->point == 2)
secondtrack = addr;
}
cdrom_toc_crc = get_crc32 (cdrom_toc_buffer, cdrom_toc_cd_buffer.points * 13);
if (datatrack) {
if (secondtrack)
cdrom_data_end = secondtrack;
else
cdrom_data_end = cdrom_toc_cd_buffer.lastaddress;
}
return 0;
}
/* open device */
static int sys_cddev_open (void)
{
struct device_info di;
unitnum = get_standard_cd_unit (CD_STANDARD_UNIT_CD32);
sys_command_info (unitnum, &di, 0);
write_log (L"using drive %s (unit %d, media %d)\n", di.label, unitnum, di.media_inserted);
/* make sure CD audio is not playing */
cdaudiostop_do ();
return 0;
}
/* close device */
static void sys_cddev_close (void)
{
if (unitnum >= 0) {
cdaudiostop_do ();
sys_command_close (unitnum);
}
unitnum = -1;
}
static int command_lengths[] = { 1,2,1,1,12,2,1,1,4,1,-1,-1,-1,-1,-1,-1 };
static void cdrom_start_return_data (int len)
{
if (len <= 0 || cdrom_receive_started > 0)
return;
cdrom_receive_started = len;
}
static void cdrom_return_data (void)
{
uae_u32 cmd_buf = cdrx_address;
int i;
uae_u8 checksum;
int len = cdrom_receive_started;
if (!len)
return;
if (!(cdrom_flags & CDFLAG_RXD))
return;
#if AKIKO_DEBUG_IO_CMD
write_log (L"OUT:");
#endif
checksum = 0xff;
for (i = 0; i < len; i++) {
checksum -= cdrom_result_buffer[i];
put_byte (cmd_buf + ((cdcomrxinx + i) & 0xff), cdrom_result_buffer[i]);
#if AKIKO_DEBUG_IO_CMD
write_log (L"%02X ", cdrom_result_buffer[i]);
#endif
}
put_byte (cmd_buf + ((cdcomrxinx + len) & 0xff), checksum);
#if AKIKO_DEBUG_IO_CMD
write_log (L"(%02X)\n", checksum);
#endif
cdcomrxinx += len + 1;
cdcomrxinx &= 0xff;
set_status (CDINTERRUPT_RXDMADONE);
cdrom_receive_started = 0;
}
static int cdrom_command_led (void)
{
int v = cdrom_command_buffer[1];
int old = cdrom_led;
cdrom_led &= ~LED_CD_ACTIVE;
cdrom_led |= (v & 1) ? LED_CD_ACTIVE : 0;
if (cdrom_led != old)
gui_flicker_led (LED_CD, 0, cdrom_led);
if (v & 0x80) { // result wanted?
cdrom_result_buffer[0] = cdrom_command;
cdrom_result_buffer[1] = (cdrom_led & LED_CD_ACTIVE) ? 1 : 0;
return 2;
}
return 0;
}
static int cdrom_command_media_status (void)
{
cdrom_result_buffer[0] = 0x0a;
cdrom_result_buffer[1] = sys_command_ismedia (unitnum, 0) > 0 ? 0x83: 0x80;
return 2;
}
/* check if cd drive door is open or closed, return firmware info */
static int cdrom_command_status (void)
{
cdrom_result_buffer[1] = 0x01;
//cdrom_result_buffer[1] = 0x80; door open
if (unitnum >= 0)
get_cdrom_toc ();
/* firmware info */
memcpy (cdrom_result_buffer + 2, FIRMWAREVERSION, sizeof FIRMWAREVERSION);
cdrom_result_buffer[0] = cdrom_command;
cd_initialized = 1;
return 20;
}
/* return one TOC entry */
static int cdrom_return_toc_entry (void)
{
cdrom_result_buffer[0] = 6;
if (cdrom_toc_cd_buffer.points == 0) {
cdrom_result_buffer[1] = CDS_ERROR;
return 15;
}
cdrom_result_buffer[1] = 0;
memcpy (cdrom_result_buffer + 2, cdrom_toc_buffer + cdrom_toc_counter * 13, 13);
cdrom_toc_counter++;
if (cdrom_toc_counter >= cdrom_toc_cd_buffer.points)
cdrom_toc_counter = -1;
return 15;
}
static int checkerr (void)
{
if (!cdrom_disk) {
cdrom_result_buffer[1] = CH_ERR_NODISK;
return 1;
}
return 0;
}
static int cdrom_command_stop (void)
{
cdrom_audiotimeout = 0;
cdrom_result_buffer[0] = cdrom_command;
if (checkerr ())
return 2;
cdrom_result_buffer[1] = 0;
cdaudiostop ();
return 2;
}
/* pause CD audio */
static int cdrom_command_pause (void)
{
cdrom_audiotimeout = 0;
cdrom_toc_counter = -1;
cdrom_result_buffer[0] = cdrom_command;
if (checkerr ())
return 2;
cdrom_result_buffer[1] = cdrom_playing ? CDS_PLAYING : 0;
if (cdrom_paused)
return 2;
cdrom_paused = 1;
if (!cdrom_playing)
return 2;
write_comm_pipe_u32 (&requests, 0x0102, 1);
return 2;
}
/* unpause CD audio */
static int cdrom_command_unpause (void)
{
cdrom_result_buffer[0] = cdrom_command;
if (checkerr ())
return 2;
cdrom_result_buffer[1] = cdrom_playing ? CDS_PLAYING : 0;
if (!cdrom_paused)
return 2;
cdrom_paused = 0;
if (!cdrom_playing)
return 2;
write_comm_pipe_u32 (&requests, 0x0103, 1);
return 2;
}
/* seek head/play CD audio/read data sectors */
static int cdrom_command_multi (void)
{
int seekpos = msf2lsn (fromlongbcd (cdrom_command_buffer + 1));
int endpos = msf2lsn (fromlongbcd (cdrom_command_buffer + 4));
if (cdrom_playing)
cdaudiostop ();
cdrom_speed = (cdrom_command_buffer[8] & 0x40) ? 2 : 1;
cdrom_result_buffer[0] = cdrom_command;
cdrom_result_buffer[1] = 0;
if (!cdrom_disk) {
cdrom_result_buffer[1] = 1; // no disk
return 2;
}
if (cdrom_command_buffer[7] == 0x80) { /* data read */
int cdrom_data_offset_end = endpos;
cdrom_data_offset = seekpos;
cdrom_seek_delay = abs (cdrom_current_sector - cdrom_data_offset);
if (cdrom_seek_delay < 100) {
cdrom_seek_delay = 1;
} else {
cdrom_seek_delay /= 1000;
cdrom_seek_delay += 10;
if (cdrom_seek_delay > 100)
cdrom_seek_delay = 100;
}
#if AKIKO_DEBUG_IO_CMD
write_log (L"READ DATA %06X (%d) - %06X (%d) SPD=%dx PC=%08X\n",
seekpos, cdrom_data_offset, endpos, cdrom_data_offset_end, cdrom_speed, M68K_GETPC);
#endif
cdrom_result_buffer[1] |= 0x02;
} else if (cdrom_command_buffer[10] & 4) { /* play audio */
int scan = 0;
if (cdrom_command_buffer[7] & 0x04)
scan = 1;
else if (cdrom_command_buffer[7] & 0x08)
scan = -1;
#if AKIKO_DEBUG_IO_CMD
write_log (L"PLAY FROM %06X (%d) to %06X (%d) SCAN=%d\n",
seekpos, msf2lsn (seekpos), endpos, msf2lsn (endpos), scan);
#endif
cdrom_playing = 1;
if (!cd_play_audio (seekpos, endpos, 0)) {
// play didn't start, report it in next status packet
cdrom_audiotimeout = -3;
}
cdrom_result_buffer[1] |= CDS_PLAYING;
} else {
#if AKIKO_DEBUG_IO_CMD
write_log (L"SEEKTO %06X\n",seekpos);
#endif
if (seekpos < 150)
cdrom_toc_counter = 0;
else
cdrom_toc_counter = -1;
}
return 2;
}
static int cdrom_playend_notify (int status)
{
cdrom_result_buffer[0] = 4;
if (status < 0)
cdrom_result_buffer[1] = 0x80; // error
else if (status == 0)
cdrom_result_buffer[1] = 0x08; // play started
else
cdrom_result_buffer[1] = 0x00; // play ended
return 2;
}
/* return subq entry */
static int cdrom_command_subq (void)
{
cdrom_result_buffer[0] = cdrom_command;
cdrom_result_buffer[1] = 0;
cd_qcode (cdrom_result_buffer + 2);
return 15;
}
static void cdrom_run_command (void)
{
int i, cmd_len;
uae_u8 checksum;
uae_u8 *pp = get_real_address (cdtx_address);
if (!(cdrom_flags & CDFLAG_TXD))
return;
for (;;) {
if (cdrom_command_active)
return;
if (cdcomtxinx == cdcomtxcmp)
return;
cdrom_command = get_byte (cdtx_address + cdcomtxinx);
if ((cdrom_command & 0xf0) == 0) {
cdcomtxinx = (cdcomtxinx + 1) & 0xff;
return;
}
cdrom_checksum_error = 0;
cdrom_unknown_command = 0;
cmd_len = command_lengths[cdrom_command & 0x0f];
if (cmd_len < 0) {
#if AKIKO_DEBUG_IO_CMD
write_log (L"unknown command %x\n", cdrom_command & 0x0f);
#endif
cdrom_unknown_command = 1;
cdrom_command_active = 1;
cdrom_command_length = 1;
set_status (CDINTERRUPT_TXDMADONE);
return;
}
#if AKIKO_DEBUG_IO_CMD
write_log (L"IN:");
#endif
checksum = 0;
for (i = 0; i < cmd_len + 1; i++) {
cdrom_command_buffer[i] = get_byte (cdtx_address + ((cdcomtxinx + i) & 0xff));
checksum += cdrom_command_buffer[i];
#if AKIKO_DEBUG_IO_CMD
if (i == cmd_len)
write_log (L"(%02X) ", cdrom_command_buffer[i]); // checksum
else
write_log (L"%02X ", cdrom_command_buffer[i]);
#endif
}
if (checksum != 0xff) {
#if AKIKO_DEBUG_IO_CMD
write_log (L" checksum error");
#endif
cdrom_checksum_error = 1;
}
#if AKIKO_DEBUG_IO_CMD
write_log (L"\n");
#endif
cdrom_command_active = 1;
cdrom_command_length = cmd_len;