annotate cdimage.c @ 2274:bf1bb893f104

Properly ignore low bits of SP start address in disassembler
author Michael Pavone <pavone@retrodev.com>
date Thu, 29 Dec 2022 22:44:35 -0800
parents 646d1797f20d
children 9ead0fe69d9b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #include <ctype.h>
2074
c5323c02dde4 Fix windows build error
Michael Pavone <pavone@retrodev.com>
parents: 2062
diff changeset
2 #include <string.h>
2215
a8af8d898a7c Fix windows build for real
Michael Pavone <pavone@retrodev.com>
parents: 2149
diff changeset
3 #include <stdlib.h>
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5 #include "system.h"
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
6 #include "util.h"
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
8 static char* cmd_start(char *cur)
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
9 {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10 while (*cur && isblank(*cur))
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
11 {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
12 cur++;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
13 }
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
14 return cur;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15 }
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
16
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
17 static char* cmd_start_sameline(char *cur)
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
18 {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
19 while (*cur && isblank(*cur) && *cur != '\n')
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
20 {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
21 cur++;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
22 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
23 return cur;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
24 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
25
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
26 static char* word_end(char *cur)
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
27 {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
28 while (*cur && !isblank(*cur))
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
29 {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
30 cur++;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
31 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
32 return cur;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
33 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
34
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35 static char* next_line(char *cur)
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36 {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37 while (*cur && *cur != '\n')
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
38 {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
39 cur++;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
40 }
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
41 if (*cur) {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
42 return cur + 1;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
43 }
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44 return NULL;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
45 }
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
46
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
47 static char* next_blank(char *cur)
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
48 {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
49 while (*cur && !isblank(*cur))
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
50 {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
51 cur++;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
52 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
53 return cur;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
54 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
55
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
56 static uint32_t timecode_to_lba(char *timecode)
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
57 {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
58 char *end;
2149
9209858b2f74 Fix a couple of bugs in the TOC file parser
Michael Pavone <pavone@retrodev.com>
parents: 2143
diff changeset
59 int seconds = 0, minutes = 0;
9209858b2f74 Fix a couple of bugs in the TOC file parser
Michael Pavone <pavone@retrodev.com>
parents: 2143
diff changeset
60 int frames = strtol(timecode, &end, 10);
9209858b2f74 Fix a couple of bugs in the TOC file parser
Michael Pavone <pavone@retrodev.com>
parents: 2143
diff changeset
61 if (end && *end == ':') {
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
62 timecode = end + 1;
2149
9209858b2f74 Fix a couple of bugs in the TOC file parser
Michael Pavone <pavone@retrodev.com>
parents: 2143
diff changeset
63 seconds = frames;
9209858b2f74 Fix a couple of bugs in the TOC file parser
Michael Pavone <pavone@retrodev.com>
parents: 2143
diff changeset
64 frames = strtol(timecode, &end, 10);
9209858b2f74 Fix a couple of bugs in the TOC file parser
Michael Pavone <pavone@retrodev.com>
parents: 2143
diff changeset
65 if (end && *end == ':') {
9209858b2f74 Fix a couple of bugs in the TOC file parser
Michael Pavone <pavone@retrodev.com>
parents: 2143
diff changeset
66 minutes = seconds;
9209858b2f74 Fix a couple of bugs in the TOC file parser
Michael Pavone <pavone@retrodev.com>
parents: 2143
diff changeset
67 seconds = frames;
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
68 timecode = end + 1;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
69 frames = strtol(timecode, NULL, 10);
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
70 }
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
71 }
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
72 seconds += minutes * 60;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
73 return seconds * 75 + frames;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
74
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
75 }
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
76
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
77 enum {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
78 FAKE_DATA = 1,
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
79 FAKE_AUDIO,
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
80 };
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
81
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
82 static uint8_t bin_seek(system_media *media, uint32_t sector)
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
83 {
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
84 media->cur_sector = sector;
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
85 uint32_t lba = sector;
2089
0db3af42dd72 Fix some byte order stuff for audio tracks
Michael Pavone <pavone@retrodev.com>
parents: 2080
diff changeset
86 uint32_t track;
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
87 for (track = 0; track < media->num_tracks; track++)
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
88 {
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
89 if (lba < media->tracks[track].fake_pregap) {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
90 media->in_fake_pregap = media->tracks[track].type == TRACK_DATA ? FAKE_DATA : FAKE_AUDIO;
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
91 break;
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
92 }
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
93 lba -= media->tracks[track].fake_pregap;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
94 if (lba < media->tracks[track].start_lba) {
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
95 if (media->tracks[track].fake_pregap) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
96 media->in_fake_pregap = media->tracks[track].type == TRACK_DATA ? FAKE_DATA : FAKE_AUDIO;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
97 } else {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
98 media->in_fake_pregap = 0;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
99 }
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
100 break;
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
101 }
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
102 if (lba < media->tracks[track].end_lba) {
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
103 media->in_fake_pregap = 0;
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
104 break;
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
105 }
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
106 }
2089
0db3af42dd72 Fix some byte order stuff for audio tracks
Michael Pavone <pavone@retrodev.com>
parents: 2080
diff changeset
107 if (track < media->num_tracks) {
0db3af42dd72 Fix some byte order stuff for audio tracks
Michael Pavone <pavone@retrodev.com>
parents: 2080
diff changeset
108 media->cur_track = track;
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
109 if (!media->in_fake_pregap) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
110 if (track) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
111 lba -= media->tracks[track - 1].end_lba;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
112 }
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
113 if (media->tracks[track].has_subcodes) {
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
114 if (!media->tmp_buffer) {
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
115 media->tmp_buffer = calloc(1, 96);
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
116 }
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
117 fseek(media->tracks[track].f, media->tracks[track].file_offset + (lba + 1) * media->tracks[track].sector_bytes - 96, SEEK_SET);
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
118 int bytes = fread(media->tmp_buffer, 1, 96, media->tracks[track].f);
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
119 if (bytes != 96) {
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
120 fprintf(stderr, "Only read %d subcode bytes\n", bytes);
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
121 }
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
122 }
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
123 fseek(media->tracks[track].f, media->tracks[track].file_offset + lba * media->tracks[track].sector_bytes, SEEK_SET);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
124 }
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
125 }
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
126 return track;
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
127 }
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
128
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
129 static uint8_t fake_read(uint32_t sector, uint32_t offset)
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
130 {
2142
d9151d0894c7 Fix some CD-ROM emulation issues
Michael Pavone <pavone@retrodev.com>
parents: 2140
diff changeset
131 if (!offset || offset == 11 || (offset >= 16)) {
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
132 return 0;
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
133 //TODO: error detection and correction bytes
2142
d9151d0894c7 Fix some CD-ROM emulation issues
Michael Pavone <pavone@retrodev.com>
parents: 2140
diff changeset
134 } else if (offset < 11) {
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
135 return 0xFF;
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
136 } else if (offset == 12) {
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
137 uint32_t minute = (sector / 75) / 60;
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
138 return (minute % 10) | ((minute / 10 ) << 4);
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
139 } else if (offset == 13) {
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
140 uint32_t seconds = (sector / 75) % 60;
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
141 return (seconds % 10) | ((seconds / 10 ) << 4);
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
142 } else if (offset == 14) {
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
143 uint32_t frames = sector % 75;
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
144 return (frames % 10) | ((frames / 10 ) << 4);
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
145 } else {
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
146 return 1;
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
147 }
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
148 }
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
149
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
150 static uint8_t bin_read(system_media *media, uint32_t offset)
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
151 {
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
152 if (media->in_fake_pregap == FAKE_DATA) {
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
153 return fake_read(media->cur_sector, offset);
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
154 } else if (media->in_fake_pregap == FAKE_AUDIO) {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents: 2079
diff changeset
155 return 0;
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
156 } else if ((media->tracks[media->cur_track].sector_bytes < 2352 && offset < 16) || offset > (media->tracks[media->cur_track].sector_bytes + 16)) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
157 return fake_read(media->cur_sector, offset);
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
158 } else {
2089
0db3af42dd72 Fix some byte order stuff for audio tracks
Michael Pavone <pavone@retrodev.com>
parents: 2080
diff changeset
159 if (media->tracks[media->cur_track].need_swap) {
0db3af42dd72 Fix some byte order stuff for audio tracks
Michael Pavone <pavone@retrodev.com>
parents: 2080
diff changeset
160 if (offset & 1) {
0db3af42dd72 Fix some byte order stuff for audio tracks
Michael Pavone <pavone@retrodev.com>
parents: 2080
diff changeset
161 return media->byte_storage;
0db3af42dd72 Fix some byte order stuff for audio tracks
Michael Pavone <pavone@retrodev.com>
parents: 2080
diff changeset
162 }
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
163 media->byte_storage = fgetc(media->tracks[media->cur_track].f);
2089
0db3af42dd72 Fix some byte order stuff for audio tracks
Michael Pavone <pavone@retrodev.com>
parents: 2080
diff changeset
164 }
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
165 return fgetc(media->tracks[media->cur_track].f);
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
166 }
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
167 }
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
168
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
169 static uint8_t bin_subcode_read(system_media *media, uint32_t offset)
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
170 {
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
171 if (media->in_fake_pregap || !media->tracks[media->cur_track].has_subcodes) {
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
172 //TODO: Fake PQ subcodes
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
173 return 0;
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
174 }
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
175 //TODO: Translate "cooked" subcodes back to raw format
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
176 return media->tmp_buffer[offset];
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
177 }
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
178
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
179 uint8_t parse_cue(system_media *media)
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
180 {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
181 char *line = media->buffer;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
182 media->num_tracks = 0;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
183 do {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
184 char *cmd = cmd_start(line);
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
185 if (cmd) {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
186 if (startswith(cmd, "TRACK ")) {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
187 media->num_tracks++;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
188 }
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
189 line = next_line(cmd);
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
190 } else {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
191 line = NULL;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
192 }
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
193 } while (line);
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
194 track_info *tracks = calloc(sizeof(track_info), media->num_tracks);
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
195 media->tracks = tracks;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
196 line = media->buffer;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
197 int track = -1;
2089
0db3af42dd72 Fix some byte order stuff for audio tracks
Michael Pavone <pavone@retrodev.com>
parents: 2080
diff changeset
198 uint8_t audio_byte_swap = 0;
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
199 FILE *f = NULL;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
200 int track_of_file = -1;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
201 uint8_t has_index_0 = 0;
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
202 do {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
203 char *cmd = cmd_start(line);
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
204 if (*cmd) {
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
205 if (startswith(cmd, "TRACK ")) {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
206 track++;
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
207 track_of_file++;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
208 has_index_0 = 0;
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
209 cmd += 6;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
210 char *end;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
211 int file_track = strtol(cmd, &end, 10);
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
212 if (file_track != (track + 1)) {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
213 warning("Expected track %d, but found track %d in CUE sheet\n", track + 1, file_track);
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
214 }
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
215 tracks[track].f = f;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
216
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
217
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
218 cmd = cmd_start(end);
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
219 if (*cmd) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
220 if (startswith(cmd, "AUDIO")) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
221 tracks[track].type = TRACK_AUDIO;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
222 tracks[track].need_swap = audio_byte_swap;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
223 tracks[track].sector_bytes = 2352;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
224 } else {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
225 tracks[track].type = TRACK_DATA;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
226 tracks[track].need_swap = 0;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
227 tracks[track].sector_bytes = 0;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
228 char *slash = strchr(cmd, '/');
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
229 if (slash) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
230 tracks[track].sector_bytes = atoi(slash+1);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
231 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
232 if (!tracks[track].sector_bytes) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
233 warning("Missing sector size for data track %d in cue", track + 1);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
234 tracks[track].sector_bytes = 2352;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
235 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
236 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
237
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
238 }
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
239 } else if (startswith(cmd, "FILE ")) {
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
240 cmd += 5;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
241 cmd = strchr(cmd, '"');
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
242 if (cmd) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
243 cmd++;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
244 char *end = strchr(cmd, '"');
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
245 if (end) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
246 char *fname;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
247 //TODO: zipped BIN/CUE support
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
248 if (is_absolute_path(cmd)) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
249 fname = malloc(end-cmd + 1);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
250 memcpy(fname, cmd, end-cmd);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
251 fname[end-cmd] = 0;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
252 } else {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
253 size_t dirlen = strlen(media->dir);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
254 fname = malloc(dirlen + 1 + (end-cmd) + 1);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
255 memcpy(fname, media->dir, dirlen);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
256 fname[dirlen] = PATH_SEP[0];
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
257 memcpy(fname + dirlen + 1, cmd, end-cmd);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
258 fname[dirlen + 1 + (end-cmd)] = 0;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
259 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
260 f = fopen(fname, "rb");
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
261 if (!f) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
262 fatal_error("Failed to open %s specified by FILE command in CUE sheet %s.%s\n", fname, media->name, media->extension);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
263 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
264 free(fname);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
265 track_of_file = -1;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
266 for (end++; *end && *end != '\n' && *end != '\r'; end++)
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
267 {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
268 if (!isspace(*end)) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
269 if (startswith(end, "BINARY")) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
270 audio_byte_swap = 0;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
271 } else if (startswith(end, "MOTOROLA")) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
272 audio_byte_swap = 1;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
273 } else {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
274 warning("Unsupported FILE type in CUE sheet. Only BINARY and MOTOROLA are supported\n");
2089
0db3af42dd72 Fix some byte order stuff for audio tracks
Michael Pavone <pavone@retrodev.com>
parents: 2080
diff changeset
275 }
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
276 break;
2089
0db3af42dd72 Fix some byte order stuff for audio tracks
Michael Pavone <pavone@retrodev.com>
parents: 2080
diff changeset
277 }
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
278 }
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
279 }
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
280 }
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
281 } else if (track >= 0) {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
282 if (startswith(cmd, "PREGAP ")) {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
283 tracks[track].fake_pregap = timecode_to_lba(cmd + 7);
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
284 } else if (startswith(cmd, "INDEX ")) {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
285 char *after;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
286 int index = strtol(cmd + 6, &after, 10);
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
287 uint8_t has_start_lba = 0;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
288 uint32_t start_lba;
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
289 if (!index) {
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
290 tracks[track].pregap_lba = start_lba = timecode_to_lba(after);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
291 has_index_0 = 1;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
292 has_start_lba = 1;
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
293 } else if (index == 1) {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
294 tracks[track].start_lba = timecode_to_lba(after);
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
295 if (!has_index_0) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
296 start_lba = tracks[track].start_lba;
2250
646d1797f20d Fix bug in CUE parser that caused regression in Bill Walsh College Football
Michael Pavone <pavone@retrodev.com>
parents: 2215
diff changeset
297 if (!tracks[track].fake_pregap) {
646d1797f20d Fix bug in CUE parser that caused regression in Bill Walsh College Football
Michael Pavone <pavone@retrodev.com>
parents: 2215
diff changeset
298 tracks[track].pregap_lba = start_lba;
646d1797f20d Fix bug in CUE parser that caused regression in Bill Walsh College Football
Michael Pavone <pavone@retrodev.com>
parents: 2215
diff changeset
299 }
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
300 has_start_lba = 1;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
301 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
302 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
303 if (has_start_lba) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
304 if (track > 0) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
305 tracks[track-1].end_lba = start_lba;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
306 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
307 if (track_of_file > 0) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
308 tracks[track].file_offset = tracks[track-1].file_offset + tracks[track-1].end_lba * tracks[track-1].sector_bytes;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
309 if (track_of_file > 1) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
310 tracks[track].file_offset -= tracks[track-2].end_lba * tracks[track-1].sector_bytes;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
311 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
312 } else {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
313 tracks[track].file_offset = 0;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
314 }
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
315 }
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
316 }
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
317 }
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
318 if (cmd && *cmd) {
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
319 line = next_line(cmd);
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
320 } else {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
321 line = NULL;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
322 }
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
323 } else {
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
324 line = NULL;
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
325 }
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
326 } while (line);
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
327 if (media->num_tracks > 0 && media->tracks[0].f) {
2143
67f20f9188b0 Fix support for CUE sheets that refer to multiple files
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
328 //end of last track in a file is implictly based on the size
67f20f9188b0 Fix support for CUE sheets that refer to multiple files
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
329 f = tracks[0].f;
67f20f9188b0 Fix support for CUE sheets that refer to multiple files
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
330 uint32_t offset = 0;
67f20f9188b0 Fix support for CUE sheets that refer to multiple files
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
331 for (int track = 0; track < media->num_tracks; track++) {
67f20f9188b0 Fix support for CUE sheets that refer to multiple files
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
332 if (track == media->num_tracks - 1 && tracks[track].f) {
67f20f9188b0 Fix support for CUE sheets that refer to multiple files
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
333 uint32_t start_lba =tracks[track].fake_pregap ? tracks[track].start_lba : tracks[track].pregap_lba;
67f20f9188b0 Fix support for CUE sheets that refer to multiple files
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
334 tracks[track].end_lba = start_lba + (file_size(tracks[track].f) - tracks[track].file_offset)/ tracks[track].sector_bytes;
67f20f9188b0 Fix support for CUE sheets that refer to multiple files
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
335 } else if (tracks[track].f != f) {
67f20f9188b0 Fix support for CUE sheets that refer to multiple files
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
336 uint32_t start_lba =tracks[track-1].fake_pregap ? tracks[track-1].start_lba : tracks[track-1].pregap_lba;
67f20f9188b0 Fix support for CUE sheets that refer to multiple files
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
337 tracks[track-1].end_lba = start_lba + (file_size(tracks[track-1].f) - tracks[track-1].file_offset)/ tracks[track-1].sector_bytes;
67f20f9188b0 Fix support for CUE sheets that refer to multiple files
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
338 offset = tracks[track-1].end_lba;
67f20f9188b0 Fix support for CUE sheets that refer to multiple files
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
339 }
67f20f9188b0 Fix support for CUE sheets that refer to multiple files
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
340 if (!tracks[track].fake_pregap) {
67f20f9188b0 Fix support for CUE sheets that refer to multiple files
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
341 tracks[track].pregap_lba += offset;
67f20f9188b0 Fix support for CUE sheets that refer to multiple files
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
342 }
67f20f9188b0 Fix support for CUE sheets that refer to multiple files
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
343 tracks[track].start_lba += offset;
67f20f9188b0 Fix support for CUE sheets that refer to multiple files
Michael Pavone <pavone@retrodev.com>
parents: 2142
diff changeset
344 tracks[track].end_lba += offset;
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
345 }
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
346 //replace cue sheet with first sector
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
347 free(media->buffer);
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
348 media->buffer = calloc(2048, 1);
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
349 if (tracks[0].type == TRACK_DATA && tracks[0].sector_bytes == 2352) {
2062
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
350 // if the first track is a data track, don't trust the CUE sheet and look at the MM:SS:FF from first sector
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
351 uint8_t msf[3];
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
352 fseek(tracks[0].f, 12, SEEK_SET);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
353 if (sizeof(msf) == fread(msf, 1, sizeof(msf), tracks[0].f)) {
2062
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
354 tracks[0].fake_pregap = msf[2] + (msf[0] * 60 + msf[1]) * 75;
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
355 }
2089
0db3af42dd72 Fix some byte order stuff for audio tracks
Michael Pavone <pavone@retrodev.com>
parents: 2080
diff changeset
356 } else if (!tracks[0].start_lba && !tracks[0].fake_pregap) {
0db3af42dd72 Fix some byte order stuff for audio tracks
Michael Pavone <pavone@retrodev.com>
parents: 2080
diff changeset
357 tracks[0].fake_pregap = 2 * 75;
2062
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
358 }
07ed42bd7b4c Some progress on CDC and CDD emulation. Now passes first 3 "CDC INIT" tests in mcd-verificator
Michael Pavone <pavone@retrodev.com>
parents: 2061
diff changeset
359
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
360 fseek(tracks[0].f, tracks[0].sector_bytes >= 2352 ? 16 : 0, SEEK_SET);
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
361 media->size = fread(media->buffer, 1, 2048, tracks[0].f);
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
362 media->seek = bin_seek;
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
363 media->read = bin_read;
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
364 media->read_subcodes = bin_subcode_read;
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
365 }
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
366 uint8_t valid = media->num_tracks > 0 && media->tracks[0].f != NULL;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
367 media->type = valid ? MEDIA_CDROM : MEDIA_CART;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
368 return valid;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
369 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
370
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
371 uint8_t parse_toc(system_media *media)
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
372 {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
373 char *line = media->buffer;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
374 media->num_tracks = 0;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
375 do {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
376 char *cmd = cmd_start(line);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
377 if (cmd) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
378 if (startswith(cmd, "TRACK ")) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
379 media->num_tracks++;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
380 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
381 line = next_line(cmd);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
382 } else {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
383 line = NULL;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
384 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
385 } while (line);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
386 track_info *tracks = calloc(sizeof(track_info), media->num_tracks);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
387 media->tracks = tracks;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
388 line = media->buffer;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
389 char *last_file_name = NULL;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
390 FILE *f = NULL;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
391 int track = -1;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
392 do {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
393 char *cmd = cmd_start(line);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
394 if (*cmd) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
395 if (startswith(cmd, "TRACK ")) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
396 track++;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
397 cmd = cmd_start(cmd + 6);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
398 if (startswith(cmd, "AUDIO")) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
399 tracks[track].type = TRACK_AUDIO;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
400 tracks[track].sector_bytes = 2352;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
401 tracks[track].need_swap = 1;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
402 } else {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
403 tracks[track].type = TRACK_DATA;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
404 tracks[track].need_swap = 0;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
405 if (startswith(cmd, "MODE1_RAW") || startswith(cmd, "MODE2_RAW")) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
406 tracks[track].sector_bytes = 2352;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
407 } else if (startswith(cmd, "MODE2_FORM2")) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
408 tracks[track].sector_bytes = 2324;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
409 } else if (startswith(cmd, "MODE1") || startswith(cmd, "MODE2_FORM1")) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
410 tracks[track].sector_bytes = 2048;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
411 } else if (startswith(cmd, "MODE2")) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
412 tracks[track].sector_bytes = 2336;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
413 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
414 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
415 cmd = word_end(cmd);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
416 if (*cmd && *cmd != '\n') {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
417 cmd = cmd_start_sameline(cmd);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
418 if (*cmd && *cmd != '\n') {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
419 //TODO: record whether subcode is in raw format or not
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
420 if (startswith(cmd, "RW_RAW")) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
421 tracks[track].sector_bytes += 96;
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
422 tracks[track].has_subcodes = SUBCODES_RAW;
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
423 } else if (startswith(cmd, "RW")) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
424 tracks[track].sector_bytes += 96;
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
425 tracks[track].has_subcodes = SUBCODES_COOKED;
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
426 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
427 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
428 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
429 if (track) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
430 tracks[track].start_lba = tracks[track].pregap_lba = tracks[track].end_lba = tracks[track-1].end_lba;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
431 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
432 } else if (track >= 0) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
433 uint8_t is_datafile = startswith(cmd, "DATAFILE");
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
434 if (is_datafile || startswith(cmd, "FILE")) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
435
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
436 if (tracks[track].f) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
437 warning("TOC file has more than one file for track %d, only one is supported\n", track + 1);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
438 } else {
2140
6d0de02a068a Fix bug in TOC parser
Michael Pavone <pavone@retrodev.com>
parents: 2122
diff changeset
439 cmd += is_datafile ? 8 : 4;
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
440 char *fname_start = strchr(cmd, '"');
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
441 if (fname_start) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
442 ++fname_start;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
443 char *fname_end = strchr(fname_start, '"');
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
444 if (fname_end) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
445 if (!last_file_name || strncmp(last_file_name, fname_start, fname_end-fname_start)) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
446 free(last_file_name);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
447 last_file_name = calloc(1, 1 + fname_end-fname_start);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
448 memcpy(last_file_name, fname_start, fname_end-fname_start);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
449 char *fname;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
450 //TODO: zipped BIN/TOC support
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
451 if (is_absolute_path(last_file_name)) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
452 fname = last_file_name;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
453 } else {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
454 size_t dirlen = strlen(media->dir);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
455 fname = malloc(dirlen + 1 + (fname_end-fname_start) + 1);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
456 memcpy(fname, media->dir, dirlen);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
457 fname[dirlen] = PATH_SEP[0];
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
458 memcpy(fname + dirlen + 1, fname_start, fname_end-fname_start);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
459 fname[dirlen + 1 + (fname_end-fname_start)] = 0;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
460 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
461 f = fopen(fname, "rb");
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
462 if (!f) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
463 fatal_error("Failed to open %s specified by DATAFILE command in TOC file %s.%s\n", fname, media->name, media->extension);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
464 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
465 if (fname != last_file_name) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
466 free(fname);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
467 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
468 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
469 tracks[track].f = f;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
470 cmd = fname_end + 1;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
471 cmd = cmd_start_sameline(cmd);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
472 if (*cmd == '#') {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
473 char *end;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
474 tracks[track].file_offset = strtol(cmd + 1, &end, 10);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
475 cmd = cmd_start_sameline(end);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
476 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
477 if (!is_datafile) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
478 if (isdigit(*cmd)) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
479 uint32_t start = timecode_to_lba(cmd);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
480 tracks[track].file_offset += start * tracks[track].sector_bytes;
2149
9209858b2f74 Fix a couple of bugs in the TOC file parser
Michael Pavone <pavone@retrodev.com>
parents: 2143
diff changeset
481 cmd = cmd_start_sameline(cmd);
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
482 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
483 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
484 if (isdigit(*cmd)) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
485 uint32_t length = timecode_to_lba(cmd);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
486 tracks[track].end_lba += length;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
487 } else {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
488 long fsize = file_size(f);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
489 tracks[track].end_lba += fsize - tracks[track].file_offset;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
490 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
491 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
492 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
493 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
494 } else if (startswith(cmd, "SILENCE")) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
495 cmd = cmd_start_sameline(cmd + 7);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
496 tracks[track].fake_pregap += timecode_to_lba(cmd);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
497 } else if (startswith(cmd, "START")) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
498 cmd = cmd_start_sameline(cmd + 5);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
499 tracks[track].start_lba = tracks[track].pregap_lba + timecode_to_lba(cmd);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
500 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
501 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
502 if (cmd && *cmd) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
503 line = next_line(cmd);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
504 } else {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
505 line = NULL;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
506 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
507 } else {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
508 line = NULL;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
509 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
510 } while (line);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
511 if (media->num_tracks > 0 && media->tracks[0].f) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
512 //replace cue sheet with first sector
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
513 free(media->buffer);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
514 media->buffer = calloc(2048, 1);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
515 if (tracks[0].type == TRACK_DATA && tracks[0].sector_bytes == 2352) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
516 // if the first track is a data track, don't trust the TOC file and look at the MM:SS:FF from first sector
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
517 uint8_t msf[3];
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
518 fseek(tracks[0].f, 12, SEEK_SET);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
519 if (sizeof(msf) == fread(msf, 1, sizeof(msf), tracks[0].f)) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
520 tracks[0].fake_pregap = msf[2] + (msf[0] * 60 + msf[1]) * 75;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
521 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
522 } else if (!tracks[0].start_lba && !tracks[0].fake_pregap) {
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
523 tracks[0].fake_pregap = 2 * 75;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
524 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
525
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
526 fseek(tracks[0].f, tracks[0].sector_bytes == 2352 ? 16 : 0, SEEK_SET);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
527 media->size = fread(media->buffer, 1, 2048, tracks[0].f);
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
528 media->seek = bin_seek;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
529 media->read = bin_read;
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
530 media->read_subcodes = bin_subcode_read;
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
531 }
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
532 uint8_t valid = media->num_tracks > 0 && media->tracks[0].f != NULL;
2061
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents: 2059
diff changeset
533 media->type = valid ? MEDIA_CDROM : MEDIA_CART;
7c1760b5b3e5 Implemented basic TOC functionality of CDD MCU
Michael Pavone <pavone@retrodev.com>
parents: 2059
diff changeset
534 return valid;
2059
6399a776e981 Add basic support for BIN/CUE images
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
535 }
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
536
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
537 uint32_t make_iso_media(system_media *media, const char *filename)
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
538 {
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
539 FILE *f = fopen(filename, "rb");
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
540 if (!f) {
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
541 return 0;
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
542 }
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
543 media->buffer = calloc(2048, 1);
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
544 media->size = fread(media->buffer, 1, 2048, f);
2079
5a2b759f6b2d BIOS doesn't like it if TOC read happens too quickly
Michael Pavone <pavone@retrodev.com>
parents: 2076
diff changeset
545 media->num_tracks = 1;
5a2b759f6b2d BIOS doesn't like it if TOC read happens too quickly
Michael Pavone <pavone@retrodev.com>
parents: 2076
diff changeset
546 media->tracks = calloc(sizeof(track_info), 1);
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
547 media->tracks[0] = (track_info){
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
548 .f = f,
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
549 .file_offset = 0,
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
550 .fake_pregap = 2 * 75,
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
551 .start_lba = 0,
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
552 .end_lba = file_size(f),
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
553 .sector_bytes = 2048,
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
554 .has_subcodes = SUBCODES_NONE,
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
555 .need_swap = 0,
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
556 .type = TRACK_DATA
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
557 };
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
558 media->type = MEDIA_CDROM;
2114
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
559 media->seek = bin_seek;
2449c88cea36 Enhance support for CUE files and add initial support for cdrdao TOC files
Michael Pavone <pavone@retrodev.com>
parents: 2089
diff changeset
560 media->read = bin_read;
2116
cd057d6fe030 Initial stab at subcode emulation
Michael Pavone <pavone@retrodev.com>
parents: 2114
diff changeset
561 media->read_subcodes = bin_subcode_read;
2076
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
562 return media->size;
3f29e2726522 Added basic support for ISO images for games that only have a data track
Michael Pavone <pavone@retrodev.com>
parents: 2074
diff changeset
563 }