-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
ide_cdrom.cpp
1636 lines (1391 loc) · 42.4 KB
/
ide_cdrom.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <stdbool.h>
#include <fcntl.h>
#include <time.h>
#include <ios>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <sys/stat.h>
#include <cmath>
#include <libchdr/chd.h>
#include <byteswap.h>
#include "spi.h"
#include "user_io.h"
#include "file_io.h"
#include "hardware.h"
#include "cd.h"
#include "ide.h"
#if 0
#define dbg_printf printf
#define dbg_print_regs ide_print_regs
#define dbg_hexdump hexdump
#else
#define dbg_printf(...) void()
#define dbg_print_regs void
#define dbg_hexdump(...) void()
#endif
#define ide_send_data(databuf, size) ide_sendbuf(ide, 255, (size), (uint16_t*)(databuf))
#define ide_recv_data(databuf, size) ide_recvbuf(ide, 255, (size), (uint16_t*)(databuf))
#define ide_reset_buf() ide_reg_set(ide, 7, 0)
#define BYTES_PER_RAW_REDBOOK_FRAME 2352
#define BYTES_PER_COOKED_REDBOOK_FRAME 2048
#define REDBOOK_FRAMES_PER_SECOND 75
#define REDBOOK_FRAME_PADDING 150
#define CD_FPS 75
#define MSF_TO_FRAMES(M, S, F) ((M)*60*CD_FPS+(S)*CD_FPS+(F))
#define CD_ERR_NO_DISK 2
#define CD_ERR_ILLEGAL_REQUEST 5
#define CD_ERR_UNIT_ATTENTION 6
#define CD_ASC_CODE_COMMAND_SEQUENCE_ERR 0x2C
#define CD_ASC_CODE_ILLEGAL_OPCODE 0x20
typedef struct
{
unsigned char min;
unsigned char sec;
unsigned char fr;
} TMSF;
static int check_magic(fileTYPE *file, int sectorSize, int mode2)
{
// Initialize our array in the event file->read() doesn't fully write it
static uint8_t pvd[BYTES_PER_COOKED_REDBOOK_FRAME];
memset(pvd, 0, sizeof(pvd));
uint32_t seek = 16 * sectorSize; // first vd is located at sector 16
if (sectorSize == BYTES_PER_RAW_REDBOOK_FRAME && !mode2) seek += 16;
if (mode2) seek += 24;
FileSeek(file, seek, SEEK_SET);
if (!FileReadAdv(file, pvd, BYTES_PER_COOKED_REDBOOK_FRAME)) return 0;
// pvd[0] = descriptor type, pvd[1..5] = standard identifier,
// pvd[6] = iso version (+8 for High Sierra)
return ((pvd[0] == 1 && !strncmp((char*)(&pvd[1]), "CD001", 5) && pvd[6] == 1) ||
(pvd[8] == 1 && !strncmp((char*)(&pvd[9]), "CDROM", 5) && pvd[14] == 1));
}
static int check_iso_file(fileTYPE *f, uint8_t *mode2, uint16_t *sectorSize)
{
if (check_magic(f, BYTES_PER_COOKED_REDBOOK_FRAME, false))
{
if (sectorSize) *sectorSize = BYTES_PER_COOKED_REDBOOK_FRAME;
if (mode2) *mode2 = 0;
return 1;
}
else if (check_magic(f, BYTES_PER_RAW_REDBOOK_FRAME, false))
{
if (sectorSize) *sectorSize = BYTES_PER_RAW_REDBOOK_FRAME;
if (mode2) *mode2 = 0;
return 1;
}
else if (check_magic(f, 2336, true))
{
if (sectorSize) *sectorSize = 2336;
if (mode2) *mode2 = 1;
return 1;
}
else if (check_magic(f, BYTES_PER_RAW_REDBOOK_FRAME, true))
{
if (sectorSize) *sectorSize = BYTES_PER_RAW_REDBOOK_FRAME;
if (mode2) *mode2 = 1;
return 1;
}
if (sectorSize) *sectorSize = 0;
if (mode2) *mode2 = 0;
return 0;
}
static const char * load_iso_file(drive_t *drv, const char* filename)
{
fileTYPE f;
memset(drv->track, 0, sizeof(drv->track));
drv->track_cnt = 0;
strcpy(drv->track[0].filename, filename);
if (!FileOpen(&f, filename))
{
printf("Cannot open ISO file!\n");
return 0;
}
if (!check_iso_file(&f, &drv->track[0].mode2, &drv->track[0].sectorSize))
{
printf("Fail to parse ISO!\n");
FileClose(&f);
return 0;
}
drv->track[0].attr = 0x40; //data track
drv->track[0].length = f.size / drv->track[0].sectorSize;
drv->track[0].number = 1;
FileClose(&f);
// lead-out track (track 2)
drv->track[1].start = drv->track[0].length;
drv->track[2].number = 2;
printf("ISO: mode2 = %d, sectorSize = %d, sectors = %d\n", drv->track[0].mode2, drv->track[0].sectorSize, drv->track[0].length);
drv->track_cnt = 2;
drv->data_num = 0;
if (!FileOpen(&drv->track[0].f, drv->track[0].filename))
{
printf("Cannot open ISO file! (First track)\n");
return 0;
}
return drv->track[0].filename;
}
static int get_word(std::string &keyword, std::istream &in)
{
in >> keyword;
for (uint32_t i = 0; i < keyword.size(); i++) keyword[i] = (char)toupper(keyword[i]);
return keyword.size();
}
static int get_timecode(uint32_t &frames, std::istream &in)
{
std::string msf;
in >> msf;
TMSF tmp = { 0, 0, 0 };
int success = sscanf(msf.c_str(), "%hhu:%hhu:%hhu", &tmp.min, &tmp.sec, &tmp.fr) == 3;
frames = (int)MSF_TO_FRAMES(tmp.min, tmp.sec, tmp.fr);
return success;
}
static track_t *get_track_from_lba(drive_t *drive, uint32_t lba, bool &index0)
{
track_t *ret = NULL;
index0 = false;
for (int i = 0; i < drive->track_cnt; i++)
{
uint32_t start_lba = i ? drive->track[i-1].start + drive->track[i-1].length : 0;
uint32_t end_lba = drive->track[i].start + drive->track[i].length;
if (lba >= start_lba && lba <= end_lba)
{
ret = &drive->track[i];
//In the "pregap" section
if (lba < drive->track[i].start) index0 = true;
break;
}
}
return ret;
}
static int add_track(drive_t *drv, track_t *curr, uint32_t &shift, const int32_t prestart, uint32_t &totalPregap, uint32_t currPregap)
{
uint32_t skip = 0;
if (drv->track_cnt >= sizeof(drv->track) / sizeof(drv->track[0]))
{
printf("CDROM: too many tracks(%d)\n", drv->track_cnt);
return 0;
}
// frames between index 0(prestart) and 1(curr.start) must be skipped
if (prestart >= 0)
{
if (prestart > static_cast<int>(curr->start))
{
printf("CDROM: add_track => prestart %d cannot be > curr.start %u\n", prestart, curr->start);
return 0;
}
skip = static_cast<uint32_t>(static_cast<int>(curr->start) - prestart);
}
// Add the first track, if our vector is empty
if (!drv->track_cnt)
{
//assertm(curr.number == 1, "The first track must be labelled number 1 [BUG!]");
curr->skip = skip * curr->sectorSize;
curr->start += currPregap;
totalPregap = currPregap;
memcpy(&drv->track[drv->track_cnt], curr, sizeof(track_t));
FileOpenEx(&drv->track[drv->track_cnt].f, curr->filename, O_RDONLY);
drv->track_cnt++;
return 1;
}
// Guard against undefined behavior in subsequent tracks.back() call
//assert(!tracks.empty());
track_t *prev = &drv->track[drv->track_cnt - 1];
// current track consumes data from the same file as the previous
if (!strcmp(prev->filename, curr->filename))
{
curr->start += shift;
if (!prev->length)
{
prev->length = curr->start + totalPregap - prev->start - skip;
}
curr->skip += prev->skip + prev->length * prev->sectorSize + skip * curr->sectorSize;
totalPregap += currPregap;
curr->start += totalPregap;
// current track uses a different file as the previous track
}
else
{
uint32_t size = FileLoad(prev->filename, 0, 0);
const uint32_t tmp = size - prev->skip;
prev->length = tmp / prev->sectorSize;
if (tmp % prev->sectorSize != 0) prev->length++; // padding
curr->start += prev->start + prev->length + currPregap;
curr->skip = skip * curr->sectorSize;
shift += prev->start + prev->length;
totalPregap = currPregap;
}
// error checks
if (curr->number <= 1
|| prev->number + 1 != curr->number
|| curr->start < prev->start + prev->length) {
printf("add_track: failed consistency checks\n"
"\tcurr.number (%d) <= 1\n"
"\tprev.number (%d) + 1 != curr.number (%d)\n"
"\tcurr.start (%d) < prev.start (%d) + prev.length (%d)\n",
curr->number, prev->number, curr->number,
curr->start, prev->start, prev->length);
return 0;
}
memcpy(&drv->track[drv->track_cnt], curr, sizeof(track_t));
FileOpenEx(&drv->track[drv->track_cnt].f, drv->track[drv->track_cnt].filename, O_RDONLY);
drv->track_cnt++;
return 1;
}
static const char* load_chd_file(drive_t *drv, const char *chdfile)
{
//Borrow the cd.h "toc_t" and mister_chd* parse function. Then translate the toc_t to drive_t+track_t.
//TODO: abstract all the bin/cue+chd+iso parsing and reading into a shared class
//
const char *ext = chdfile + strlen(chdfile) - 4;
uint32_t total_sector_size = 0;
if (strncasecmp(".chd", ext, 4))
{
//Not a CHD
return 0;
}
toc_t tmpTOC = { };
memset(drv->track, 0, sizeof(drv->track));
drv->track_cnt = 0;
chd_error err = mister_load_chd(chdfile, &tmpTOC);
if (err != CHDERR_NONE)
{
return 0;
}
if (drv->chd_hunkbuf)
{
free(drv->chd_hunkbuf);
}
drv->chd_hunkbuf = (uint8_t *)malloc(tmpTOC.chd_hunksize);
drv->chd_hunknum = -1;
drv->chd_f = tmpTOC.chd_f;
//don't use add_track, just do it ourselves...
for (int i = 0; i < tmpTOC.last; i++)
{
cd_track_t *chd_track = &tmpTOC.tracks[i];
track_t *trk = &drv->track[i];
trk->number = i + 1;
trk->sectorSize = chd_track->sector_size;
if (chd_track->type)
{
trk->attr = 0x40;
if (chd_track->type == 2)
{
trk->mode2 = true;
}
}
trk->chd_offset = chd_track->offset;
trk->start = chd_track->start;
trk->length = chd_track->end - chd_track->start;
drv->track_cnt++;
total_sector_size += trk->length * trk->sectorSize;
}
//Add the lead-out track
track_t *lead_out = &drv->track[drv->track_cnt];
lead_out->number = drv->track_cnt + 1;
lead_out->attr = 0;
lead_out->start = tmpTOC.tracks[tmpTOC.last - 1].end;
lead_out->length = 0;
drv->track_cnt++;
drv->total_sectors = total_sector_size / 512;
drv->chd_total_size = total_sector_size;
for (uint8_t i = 0; i < drv->track_cnt; i++)
{
if (drv->track[i].attr == 0x40)
{
drv->data_num = i;
}
}
return chdfile;
}
static const char* load_cue_file(drive_t *drv, const char *cuefile)
{
memset(drv->track, 0, sizeof(drv->track));
drv->track_cnt = 0;
track_t track = {};
uint32_t shift = 0;
uint32_t currPregap = 0;
uint32_t totalPregap = 0;
int32_t prestart = -1;
int track_number;
int success;
int canAddTrack = 0;
std::string pathname(cuefile);
std::size_t found = pathname.find_last_of('/');
if (found == std::string::npos) return 0; // no folder name?
pathname.resize(found + 1);
std::ifstream in;
in.open(cuefile, std::ios::in);
if (in.fail()) return 0;
while (!in.eof())
{
// get next line
std::string buf;
std::getline(in, buf);
if (in.fail() && !in.eof()) return 0; // probably a binary file
std::istringstream line(buf);
std::string command;
get_word(command, line);
//printf("command: %s\n", command.c_str());
if (command == "TRACK")
{
if (canAddTrack) success = add_track(drv, &track, shift, prestart, totalPregap, currPregap);
else success = 1;
track.start = 0;
track.skip = 0;
currPregap = 0;
prestart = -1;
line >> track_number; // (cin) read into a true int first
track.number = static_cast<uint8_t>(track_number);
std::string type;
get_word(type, line);
//printf(" type: %s\n", type.c_str());
if (type == "AUDIO")
{
track.sectorSize = BYTES_PER_RAW_REDBOOK_FRAME;
track.attr = 0;
track.mode2 = false;
}
else if (type == "MODE1/2048")
{
track.sectorSize = BYTES_PER_COOKED_REDBOOK_FRAME;
track.attr = 0x40;
track.mode2 = false;
}
else if (type == "MODE1/2352")
{
track.sectorSize = BYTES_PER_RAW_REDBOOK_FRAME;
track.attr = 0x40;
track.mode2 = false;
}
else if (type == "MODE2/2336")
{
track.sectorSize = 2336;
track.attr = 0x40;
track.mode2 = true;
}
else if (type == "MODE2/2352")
{
track.sectorSize = BYTES_PER_RAW_REDBOOK_FRAME;
track.attr = 0x40;
track.mode2 = true;
}
else success = 0;
canAddTrack = 1;
}
else if (command == "INDEX")
{
int index;
line >> index;
uint32_t frame;
success = get_timecode(frame, line);
if (index == 1) track.start = frame;
else if (index == 0) prestart = static_cast<int32_t>(frame);
// ignore other indices
}
else if (command == "FILE")
{
if (canAddTrack) success = add_track(drv, &track, shift, prestart, totalPregap, currPregap);
else success = 1;
canAddTrack = 0;
std::string filename;
std::getline(std::getline(line, filename, '"'), filename, '"');
strcpy(track.filename, pathname.c_str());
strcat(track.filename, filename.c_str());
printf("cue: got new file name: %s\n", track.filename);
}
else if (command == "PREGAP") success = get_timecode(currPregap, line);
// ignored commands
else if (command == "CATALOG" || command == "CDTEXTFILE" || command == "FLAGS" || command == "ISRC" ||
command == "PERFORMER" || command == "POSTGAP" || command == "REM" ||
command == "SONGWRITER" || command == "TITLE" || command.empty())
{
success = 1;
}
// failure
else
{
success = 0;
}
if (!success)
{
return 0;
}
}
// add last track
if (!add_track(drv, &track, shift, prestart, totalPregap, currPregap))
{
return 0;
}
// add lead-out track
track.number++;
track.filename[0] = 0;
track.attr = 0;//sync with load iso
track.start = drv->track[track.number - 1].start + drv->track[track.number - 1].length;
track.length = 0;
if (!add_track(drv, &track, shift, -1, totalPregap, 0))
{
return 0;
}
for (uint8_t i = 0; i < drv->track_cnt; i++)
{
if (drv->track[i].attr == 0x40)
{
drv->data_num = i;
return drv->track[i].filename;
}
}
return 0;
}
inline TMSF frames_to_msf(uint32_t frames)
{
TMSF msf = { 0, 0, 0 };
msf.fr = frames % REDBOOK_FRAMES_PER_SECOND;
frames /= REDBOOK_FRAMES_PER_SECOND;
msf.sec = frames % 60;
frames /= 60;
msf.min = static_cast<uint8_t>(frames);
return msf;
}
static int get_tracks(drive_t *drv, int& start_track_num, int& end_track_num, TMSF& lead_out_msf)
{
if (drv->track_cnt < 2 || !drv->track[0].length) return 0;
start_track_num = drv->track[0].number;
end_track_num = drv->track[drv->track_cnt - 2].number;
lead_out_msf = frames_to_msf(drv->track[drv->track_cnt - 1].start + REDBOOK_FRAME_PADDING);
return 1;
}
static int get_track_info(drive_t *drv, int requested_track_num, TMSF& start_msf, unsigned char& attr)
{
if (drv->track_cnt < 2 || requested_track_num < 1 || requested_track_num >= drv->track_cnt)
{
return 0;
}
start_msf = frames_to_msf(drv->track[requested_track_num - 1].start + REDBOOK_FRAME_PADDING);
attr = drv->track[requested_track_num - 1].attr;
return 1;
}
static int read_toc(drive_t *drv, uint8_t *cmdbuf)
{
/* NTS: The SCSI MMC standards say we're allowed to indicate the return data
* is longer than it's allocation length. But here's the thing: some MS-DOS
* CD-ROM drivers will ask for the TOC but only provide enough room for one
* entry (OAKCDROM.SYS) and if we signal more data than it's buffer, it will
* reject our response and render the CD-ROM drive inaccessible. So to make
* this emulation work, we have to cut our response short to the driver's
* allocation length */
unsigned int AllocationLength = ((unsigned int)cmdbuf[7] << 8) + cmdbuf[8];
unsigned char Format = cmdbuf[2] & 0xF;
unsigned char Track = cmdbuf[6];
bool TIME = !!(cmdbuf[1] & 2);
unsigned char *write;
int first, last, track;
TMSF leadOut;
printf("read_toc in:\n");
hexdump(cmdbuf, 12);
memset(ide_buf, 0, 8);
if (!get_tracks(drv, first, last, leadOut))
{
printf("WARNING: ATAPI READ TOC failed to get track info\n");
return 8;
}
/* start 2 bytes out. we'll fill in the data length later */
write = ide_buf + 2;
if (Format == 1) /* Read multisession info */
{
unsigned char attr;
TMSF start;
*write++ = (unsigned char)1; /* @+2 first complete session */
*write++ = (unsigned char)1; /* @+3 last complete session */
if (!get_track_info(drv, first, start, attr))
{
printf("WARNING: ATAPI READ TOC unable to read track %u information\n", first);
attr = 0x41; /* ADR=1 CONTROL=4 */
start.min = 0;
start.sec = 0;
start.fr = 0;
}
printf("Track %u attr=0x%02x %02u:%02u:%02u\n", first, attr, start.min, start.sec, start.fr);
*write++ = 0x00; /* entry+0 RESERVED */
*write++ = (attr >> 4) | 0x10; /* entry+1 ADR=1 CONTROL=4 (DATA) */
*write++ = (unsigned char)first;/* entry+2 TRACK */
*write++ = 0x00; /* entry+3 RESERVED */
/* then, start address of first track in session */
if (TIME)
{
*write++ = 0x00;
*write++ = start.min;
*write++ = start.sec;
*write++ = start.fr;
}
else
{
uint32_t sec = (start.min * 60u * 75u) + (start.sec * 75u) + start.fr - 150u;
*write++ = (unsigned char)(sec >> 24u);
*write++ = (unsigned char)(sec >> 16u);
*write++ = (unsigned char)(sec >> 8u);
*write++ = (unsigned char)(sec >> 0u);
}
}
else if (Format == 0) /* Read table of contents */
{
*write++ = (unsigned char)first; /* @+2 */
*write++ = (unsigned char)last; /* @+3 */
for (track = first; track <= last; track++)
{
unsigned char attr;
TMSF start;
if (!get_track_info(drv, track, start, attr))
{
printf("WARNING: ATAPI READ TOC unable to read track %u information\n", track);
attr = 0x41; /* ADR=1 CONTROL=4 */
start.min = 0;
start.sec = 0;
start.fr = 0;
}
if (track < Track) continue;
if ((write + 8) > (ide_buf + AllocationLength)) break;
printf("Track %u attr=0x%02x %02u:%02u:%02u\n", track, attr, start.min, start.sec, start.fr);
*write++ = 0x00; /* entry+0 RESERVED */
*write++ = (attr >> 4) | 0x10; /* entry+1 ADR=1 CONTROL=4 (DATA) */
*write++ = (unsigned char)track;/* entry+2 TRACK */
*write++ = 0x00; /* entry+3 RESERVED */
if (TIME)
{
*write++ = 0x00;
*write++ = start.min;
*write++ = start.sec;
*write++ = start.fr;
}
else
{
uint32_t sec = (start.min * 60u * 75u) + (start.sec * 75u) + start.fr - 150u;
*write++ = (unsigned char)(sec >> 24u);
*write++ = (unsigned char)(sec >> 16u);
*write++ = (unsigned char)(sec >> 8u);
*write++ = (unsigned char)(sec >> 0u);
}
}
if ((write + 8) <= (ide_buf + AllocationLength))
{
*write++ = 0x00;
*write++ = 0x14;
*write++ = 0xAA;/*TRACK*/
*write++ = 0x00;
if (TIME)
{
*write++ = 0x00;
*write++ = leadOut.min;
*write++ = leadOut.sec;
*write++ = leadOut.fr;
}
else
{
uint32_t sec = (leadOut.min * 60u * 75u) + (leadOut.sec * 75u) + leadOut.fr - 150u;
*write++ = (unsigned char)(sec >> 24u);
*write++ = (unsigned char)(sec >> 16u);
*write++ = (unsigned char)(sec >> 8u);
*write++ = (unsigned char)(sec >> 0u);
}
}
}
else
{
printf("WARNING: ATAPI READ TOC Format=%u not supported\n", Format);
return 8;
}
/* update the TOC data length field */
unsigned int x = (unsigned int)(write - ide_buf) - 2;
ide_buf[0] = x >> 8;
ide_buf[1] = x & 0xFF;
printf("read_toc result:\n");
hexdump(ide_buf, write - ide_buf);
return write - ide_buf;
}
void cdrom_mode_select(ide_config *ide)
{
uint8_t *mode_page = &ide_buf[8];
drive_t *drv = &ide->drive[ide->regs.drv];
uint8_t page_code = mode_page[0] & 0x3F;
switch (page_code) {
case 0x0E:
{
uint8_t p0vol = mode_page[9];
uint8_t p1vol = mode_page[11];
//in gain factor
drv->volume_l = (p0vol + 1) / 256.0f;
drv->volume_r = (p1vol + 1) / 256.0f;
}
break;
}
}
static uint16_t mode_sense(drive_t *drv, int page)
{
uint8_t *write = ide_buf;
uint8_t *plen;
uint32_t x;
int valid = 0;
printf("mode_sense page: %X\n", page);
/* Mode Parameter List MMC-3 Table 340 */
/* - Mode parameter header */
/* - Page(s) */
/* Mode Parameter Header (response for 10-byte MODE SENSE) SPC-2 Table 148 */
*write++ = 0x00; /* MODE DATA LENGTH (MSB) */
*write++ = 0x00; /* (LSB) */
*write++ = 0x00; /* MEDIUM TYPE */
*write++ = 0x00; /* DEVICE-SPECIFIC PARAMETER */
*write++ = 0x00; /* Reserved */
*write++ = 0x00; /* Reserved */
*write++ = 0x00; /* BLOCK DESCRIPTOR LENGTH (MSB) */
*write++ = 0x00; /* (LSB) */
/* NTS: MMC-3 Table 342 says that BLOCK DESCRIPTOR LENGTH is zero, where it would be 8 for legacy units */
/* Mode Page Format MMC-3 Table 341 */
if (page == 0x01 || page == 0x3F)
{
valid = 1;
*write++ = 1; /* PS|reserved|Page Code */
plen = write;
*write++ = 0x00; /* Page Length (n - 1) ... Length in bytes of the mode parameters that follow */
*write++ = 0x00; /* +2 Error recovery Parameter AWRE|ARRE|TB|RC|Reserved|PER|DTE|DCR */
*write++ = 3; /* +3 Read Retry Count */
*write++ = 0x00; /* +4 Reserved */
*write++ = 0x00; /* +5 Reserved */
*write++ = 0x00; /* +6 Reserved */
*write++ = 0x00; /* +7 Reserved */
*plen = write - plen - 1;
}
/* CD-ROM audio control MMC-3 Section 6.3.7 table 354 */
/* also MMC-1 Section 5.2.3.1 table 97 */
if (page == 0x0E || page == 0x3F)
{
valid = 1;
*write++ = 0x0E; /* PS|reserved|Page Code */
plen = write;
*write++ = 0x00; /* Page Length (n - 1) ... Length in bytes of the mode parameters that follow */
*write++ = 0x04; /* +2 Reserved|IMMED=1|SOTC=0|Reserved */
*write++ = 0x00; /* +3 Reserved */
*write++ = 0x00; /* +4 Reserved */
*write++ = 0x00; /* +5 Reserved */
*write++ = 0x00; /* +6 Obsolete (75) */
*write++ = 75; /* +7 Obsolete (75) */
*write++ = 0x01; /* +8 output port 0 selection (0001b = channel 0) */
*write++ = (uint8_t)((drv->volume_l * 256) - 1); /* +9 output port 0 volume (0xFF = 0dB atten.) */
*write++ = 0x02; /* +10 output port 1 selection (0010b = channel 1) */
*write++ = (uint8_t)((drv->volume_l * 256) - 1); /* +11 output port 1 volume (0xFF = 0dB atten.) */
*write++ = 0x00; /* +12 output port 2 selection (none) */
*write++ = 0x00; /* +13 output port 2 volume (0x00 = mute) */
*write++ = 0x00; /* +14 output port 3 selection (none) */
*write++ = 0x00; /* +15 output port 3 volume (0x00 = mute) */
*plen = write - plen - 1;
}
/* CD-ROM mechanical status MMC-3 Section 6.3.11 table 361 */
if (page == 0x2A || page == 0x3F)
{
valid = 1;
*write++ = 0x2A; /* PS|reserved|Page Code */
plen = write;
*write++ = 0x00; /* Page Length (n - 1) ... Length in bytes of the mode parameters that follow */
/* MSB | | | | | | | LSB */
*write++ = 0x07; /* +2 Reserved |Reserved |DVD-RAM read |DVD-R read |DVD-ROM read | Method 2 | CD-RW read | CD-R read */
*write++ = 0x00; /* +3 Reserved |Reserved |DVD-RAM write|DVD-R write | Reserved | Test Write | CD-RW write | CD-R write */
*write++ = 0x71; /* +4 Buffer Underrun|Multisession |Mode 2 form 2|Mode 2 form 1|Digital Port 2|Digital Port 1 | Composite | Audio play */
*write++ = 0xFF; /* +5 Read code bar |UPC |ISRC |C2 Pointers |R-W deintcorr | R-W supported |CDDA accurate |CDDA support */
*write++ = 0x2F; /* +6 Loading mechanism type |Reserved |Eject |Prevent Jumper |Lock state |Lock */
/* 0 (0x00) = Caddy
* 1 (0x20) = Tray
* 2 (0x40) = Popup
* 3 (0x60) = Reserved
* 4 (0x80) = Changer with indivually changeable discs
* 5 (0xA0) = Changer using a magazine mechanism
* 6 (0xC0) = Reserved
* 6 (0xE0) = Reserved */
*write++ = 0x03; /* +7 Reserved |Reserved |R-W in leadin|Side chg cap |S/W slot sel |Changer disc pr|Sep. ch. mute |Sep. volume levels */
x = 176 * 8; /* +8 maximum speed supported in kB: 8X (obsolete in MMC-3) */
*write++ = x >> 8;
*write++ = x;
x = 256; /* +10 Number of volume levels supported */
*write++ = x >> 8;
*write++ = x;
x = 6 * 256; /* +12 buffer size supported by drive in kB */
*write++ = x >> 8;
*write++ = x;
x = 176 * 8; /* +14 current read speed selected in kB: 8X (obsolete in MMC-3) */
*write++ = x >> 8;
*write++ = x;
*plen = write - plen - 1;
}
if (!valid)
{
*write++ = page; /* PS|reserved|Page Code */
*write++ = 0x06; /* Page Length (n - 1) ... Length in bytes of the mode parameters that follow */
memset(write, 0, 6); write += 6;
printf("WARNING: MODE SENSE on page 0x%02x not supported\n", page);
}
/* mode param header, data length */
x = (uint32_t)(write - ide_buf) - 2;
ide_buf[0] = (unsigned char)(x >> 8u);
ide_buf[1] = (unsigned char)x;
hexdump(ide_buf, x + 2);
return x + 2;
}
static int get_subchan(drive_t *drv, unsigned char& attr, unsigned char& track_num, unsigned char& index, TMSF& relative_msf, TMSF& absolute_msf)
{
attr = 0;
track_num = 1;
index = 1;
relative_msf.min = relative_msf.fr = 0; relative_msf.sec = 2;
absolute_msf.min = absolute_msf.fr = 0; absolute_msf.sec = 2;
if (drv->play_start_lba == 0xFFFFFFFF) return 0;
//TODO: use current play position when audio playback will be implemented
uint32_t cur_pos = drv->play_start_lba;
bool is_index0;
track_t *cur_track = NULL;
cur_track = get_track_from_lba(drv, cur_pos, is_index0);
if (cur_track)
{
track_num = cur_track->number;
attr = cur_track->attr;
absolute_msf = frames_to_msf(cur_pos + REDBOOK_FRAME_PADDING);
int relative_diff = cur_pos - cur_track->start;
relative_msf = frames_to_msf(abs(relative_diff));
index = is_index0 ? 0 : 1;
return 1;
}
return 0;
}
static int read_subchannel(drive_t *drv, uint8_t* cmdbuf)
{
unsigned char paramList = cmdbuf[3];
unsigned char attr, track, index;
bool SUBQ = !!(cmdbuf[2] & 0x40);
bool TIME = !!(cmdbuf[1] & 2);
unsigned char *write;
TMSF rel, abs;
if (paramList == 0 || paramList > 3)
{
printf("ATAPI READ SUBCHANNEL unknown param list\n");
memset(ide_buf, 0, 8);
return 8;
}
else if (paramList == 2)
{
printf("ATAPI READ SUBCHANNEL Media Catalog Number not supported\n");
memset(ide_buf, 0, 8);
return 8;
}
else if (paramList == 3) {
printf("ATAPI READ SUBCHANNEL ISRC not supported\n");
memset(ide_buf, 0, 8);
return 8;
}
/* get current subchannel position */
if (!get_subchan(drv, attr, track, index, rel, abs))
{
printf("ATAPI READ SUBCHANNEL unable to read current pos\n");
memset(ide_buf, 0, 8);
return 8;
}
memset(ide_buf, 0, 8);
write = ide_buf;
*write++ = 0x00;
*write++ = (!drv->playing) ? 0x13 : drv->paused ? 0x12 : 0x11; /* AUDIO STATUS */
*write++ = 0x00; /* SUBCHANNEL DATA LENGTH */
*write++ = 0x00;
if (SUBQ)
{
*write++ = 0x01; /* subchannel data format code */
*write++ = (attr >> 4) | 0x10; /* ADR/CONTROL */
*write++ = track;
*write++ = index;
if (TIME)
{
*write++ = 0x00;
*write++ = abs.min;
*write++ = abs.sec;
*write++ = abs.fr;
*write++ = 0x00;
*write++ = rel.min;
*write++ = rel.sec;
*write++ = rel.fr;
}
else
{
uint32_t sec;
sec = (abs.min * 60u * 75u) + (abs.sec * 75u) + abs.fr - 150u;
*write++ = (unsigned char)(sec >> 24u);
*write++ = (unsigned char)(sec >> 16u);
*write++ = (unsigned char)(sec >> 8u);
*write++ = (unsigned char)(sec >> 0u);
sec = (rel.min * 60u * 75u) + (rel.sec * 75u) + rel.fr - 150u;
*write++ = (unsigned char)(sec >> 24u);
*write++ = (unsigned char)(sec >> 16u);
*write++ = (unsigned char)(sec >> 8u);
*write++ = (unsigned char)(sec >> 0u);
}
}
unsigned int x = (unsigned int)(write - ide_buf) - 4;
ide_buf[2] = x >> 8;
ide_buf[3] = x;
dbg_hexdump(ide_buf, write - ide_buf);
return write - ide_buf;
}
static void pkt_send(ide_config *ide, void *data, uint16_t size)
{
ide->regs.pkt_io_size = (size + 1) / 2;
ide_send_data(data, ide->regs.pkt_io_size);
ide->regs.cylinder = size;
ide->regs.sector_count = 2;
ide->regs.status = ATA_STATUS_RDY | ATA_STATUS_DRQ | ATA_STATUS_IRQ;
ide_set_regs(ide);
ide->state = IDE_STATE_WAIT_PKT_RD;
}
static void read_cd_sectors(ide_config *ide, track_t *track, int cnt)
{
drive_t *drv = &ide->drive[ide->regs.drv];
uint32_t sz = drv->track[drv->data_num].sectorSize;
if (sz == 2048)
{
if (!ide->null) ide->null = (FileReadAdv(&track->f, ide_buf, cnt * sz, -1) <= 0);