4c2e3554 |
1 | /* |
2 | * PicoDrive |
3 | * (C) notaz, 2006-2010,2013 |
4 | * |
5 | * This work is licensed under the terms of MAME license. |
6 | * See COPYING file in the top-level directory. |
7 | */ |
8 | |
9 | #include <string.h> |
10 | #include "pico_int.h" |
11 | #include "cd/cue.h" |
12 | |
13 | unsigned char media_id_header[0x100]; |
14 | |
15 | static void strlwr_(char *string) |
16 | { |
3e9da86e |
17 | char *p; |
18 | for (p = string; *p; p++) |
19 | if ('A' <= *p && *p <= 'Z') |
20 | *p += 'a' - 'A'; |
4c2e3554 |
21 | } |
22 | |
23 | static void get_ext(const char *file, char *ext) |
24 | { |
3e9da86e |
25 | const char *p; |
4c2e3554 |
26 | |
3e9da86e |
27 | p = file + strlen(file) - 4; |
28 | if (p < file) p = file; |
29 | strncpy(ext, p, 4); |
30 | ext[4] = 0; |
31 | strlwr_(ext); |
4c2e3554 |
32 | } |
33 | |
34 | static int detect_media(const char *fname) |
35 | { |
3e9da86e |
36 | static const short sms_offsets[] = { 0x7ff0, 0x3ff0, 0x1ff0 }; |
37 | static const char *sms_exts[] = { "sms", "gg", "sg" }; |
38 | static const char *md_exts[] = { "gen", "bin", "smd" }; |
39 | char buff0[32], buff[32]; |
40 | unsigned short *d16; |
41 | pm_file *pmf; |
42 | char ext[5]; |
43 | int i; |
44 | |
45 | get_ext(fname, ext); |
46 | |
47 | // detect wrong extensions |
48 | if (!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) // s.gz ~ .mds.gz |
49 | return PM_BAD_DETECT; |
50 | |
51 | /* don't believe in extensions, except .cue */ |
52 | if (strcasecmp(ext, ".cue") == 0) |
53 | return PM_CD; |
54 | |
55 | pmf = pm_open(fname); |
56 | if (pmf == NULL) |
57 | return PM_BAD_DETECT; |
58 | |
59 | if (pm_read(buff0, 32, pmf) != 32) { |
60 | pm_close(pmf); |
61 | return PM_BAD_DETECT; |
62 | } |
63 | |
64 | if (strncasecmp("SEGADISCSYSTEM", buff0 + 0x00, 14) == 0 || |
65 | strncasecmp("SEGADISCSYSTEM", buff0 + 0x10, 14) == 0) { |
66 | pm_close(pmf); |
67 | return PM_CD; |
68 | } |
69 | |
70 | /* check for SMD evil */ |
71 | if (pmf->size >= 0x4200 && (pmf->size & 0x3fff) == 0x200) { |
72 | if (pm_seek(pmf, sms_offsets[0] + 0x200, SEEK_SET) == sms_offsets[0] + 0x200 && |
73 | pm_read(buff, 16, pmf) == 16 && |
74 | strncmp("TMR SEGA", buff, 8) == 0) |
75 | goto looks_like_sms; |
76 | |
77 | /* could parse further but don't bother */ |
78 | goto extension_check; |
79 | } |
80 | |
81 | /* MD header? Act as TMSS BIOS here */ |
82 | if (pm_seek(pmf, 0x100, SEEK_SET) == 0x100 && pm_read(buff, 16, pmf) == 16) { |
83 | if (strncmp(buff, "SEGA", 4) == 0 || strncmp(buff, " SEG", 4) == 0) |
84 | goto looks_like_md; |
85 | } |
86 | |
87 | for (i = 0; i < ARRAY_SIZE(sms_offsets); i++) { |
88 | if (pm_seek(pmf, sms_offsets[i], SEEK_SET) != sms_offsets[i]) |
89 | continue; |
90 | |
91 | if (pm_read(buff, 16, pmf) != 16) |
92 | continue; |
93 | |
94 | if (strncmp("TMR SEGA", buff, 8) == 0) |
95 | goto looks_like_sms; |
96 | } |
4c2e3554 |
97 | |
98 | extension_check: |
3e9da86e |
99 | /* probably some headerless thing. Maybe check the extension after all. */ |
100 | for (i = 0; i < ARRAY_SIZE(md_exts); i++) |
101 | if (strcasecmp(pmf->ext, md_exts[i]) == 0) |
102 | goto looks_like_md; |
103 | |
104 | for (i = 0; i < ARRAY_SIZE(sms_exts); i++) |
105 | if (strcasecmp(pmf->ext, sms_exts[i]) == 0) |
106 | goto looks_like_sms; |
107 | |
108 | /* If everything else fails, make a guess on the reset vector */ |
109 | d16 = (unsigned short *)(buff0 + 4); |
110 | if ((((d16[0] << 16) | d16[1]) & 0xffffff) >= pmf->size) { |
111 | lprintf("bad MD reset vector, assuming SMS\n"); |
112 | goto looks_like_sms; |
113 | } |
4c2e3554 |
114 | |
115 | looks_like_md: |
3e9da86e |
116 | pm_close(pmf); |
117 | return PM_MD_CART; |
4c2e3554 |
118 | |
119 | looks_like_sms: |
3e9da86e |
120 | pm_close(pmf); |
121 | return PM_MARK3; |
4c2e3554 |
122 | } |
123 | |
124 | /* checks if fname points to valid MegaCD image */ |
125 | int PicoCdCheck(const char *fname_in, int *pregion) |
126 | { |
3e9da86e |
127 | const char *fname = fname_in; |
128 | unsigned char buf[32]; |
129 | pm_file *cd_f; |
130 | int region = 4; // 1: Japan, 4: US, 8: Europe |
131 | char ext[5]; |
132 | cue_track_type type = CT_UNKNOWN; |
133 | cue_data_t *cue_data = NULL; |
134 | |
e71fae1f |
135 | // opens a cue, or searches for one |
136 | cue_data = cue_parse(fname_in); |
137 | if (cue_data != NULL) { |
138 | fname = cue_data->tracks[1].fname; |
139 | type = cue_data->tracks[1].type; |
140 | } |
141 | else { |
142 | get_ext(fname_in, ext); |
143 | if (strcasecmp(ext, ".cue") == 0) |
3e9da86e |
144 | return -1; |
145 | } |
146 | |
147 | cd_f = pm_open(fname); |
148 | if (cue_data != NULL) |
149 | cue_destroy(cue_data); |
150 | |
151 | if (cd_f == NULL) return 0; // let the upper level handle this |
152 | |
153 | if (pm_read(buf, 32, cd_f) != 32) { |
154 | pm_close(cd_f); |
155 | return -1; |
156 | } |
157 | |
158 | if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) { |
159 | if (type && type != CT_ISO) |
160 | elprintf(EL_STATUS, ".cue has wrong type: %i", type); |
161 | type = CT_ISO; // Sega CD (ISO) |
162 | } |
163 | if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) { |
164 | if (type && type != CT_BIN) |
165 | elprintf(EL_STATUS, ".cue has wrong type: %i", type); |
166 | type = CT_BIN; // Sega CD (BIN) |
167 | } |
168 | |
169 | if (type == CT_UNKNOWN) { |
170 | pm_close(cd_f); |
171 | return 0; |
172 | } |
173 | |
174 | pm_seek(cd_f, (type == CT_ISO) ? 0x100 : 0x110, SEEK_SET); |
175 | pm_read(media_id_header, sizeof(media_id_header), cd_f); |
176 | |
177 | /* it seems we have a CD image here. Try to detect region now.. */ |
178 | pm_seek(cd_f, (type == CT_ISO) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET); |
179 | pm_read(buf, 1, cd_f); |
180 | pm_close(cd_f); |
181 | |
182 | if (buf[0] == 0x64) region = 8; // EU |
183 | if (buf[0] == 0xa1) region = 1; // JAP |
184 | |
185 | lprintf("detected %s Sega/Mega CD image with %s region\n", |
186 | type == CT_BIN ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA"); |
187 | |
188 | if (pregion != NULL) |
4c2e3554 |
189 | *pregion = region; |
190 | |
3e9da86e |
191 | return type; |
4c2e3554 |
192 | } |
193 | |
194 | enum media_type_e PicoLoadMedia(const char *filename, |
195 | const char *carthw_cfg_fname, |
196 | const char *(*get_bios_filename)(int *region, const char *cd_fname), |
197 | void (*do_region_override)(const char *media_filename)) |
198 | { |
199 | const char *rom_fname = filename; |
200 | enum media_type_e media_type; |
3e9da86e |
201 | cd_img_type cd_img_type = CIT_NOT_CD; |
202 | unsigned char *rom_data = NULL; |
203 | unsigned int rom_size = 0; |
204 | pm_file *rom = NULL; |
4c2e3554 |
205 | int cd_region = 0; |
206 | int ret; |
207 | |
3e9da86e |
208 | media_type = detect_media(filename); |
209 | if (media_type == PM_BAD_DETECT) |
210 | goto out; |
211 | |
212 | if ((PicoAHW & PAHW_MCD) && Pico_mcd != NULL) |
213 | Stop_CD(); |
214 | PicoCartUnload(); |
215 | PicoAHW = 0; |
a76fad41 |
216 | PicoQuirks = 0; |
3e9da86e |
217 | |
218 | if (media_type == PM_CD) |
219 | { |
220 | // check for MegaCD image |
221 | cd_img_type = PicoCdCheck(filename, &cd_region); |
da77daa9 |
222 | if ((int)cd_img_type >= 0 && cd_img_type != CIT_NOT_CD) |
3e9da86e |
223 | { |
224 | // valid CD image, ask frontend for BIOS.. |
4c2e3554 |
225 | rom_fname = NULL; |
226 | if (get_bios_filename != NULL) |
227 | rom_fname = get_bios_filename(&cd_region, filename); |
228 | if (rom_fname == NULL) { |
229 | media_type = PM_BAD_CD_NO_BIOS; |
230 | goto out; |
231 | } |
232 | |
3e9da86e |
233 | PicoAHW |= PAHW_MCD; |
234 | } |
235 | else { |
4c2e3554 |
236 | media_type = PM_BAD_CD; |
237 | goto out; |
3e9da86e |
238 | } |
239 | } |
240 | else if (media_type == PM_MARK3) { |
241 | lprintf("detected SMS ROM\n"); |
242 | PicoAHW = PAHW_SMS; |
243 | } |
244 | |
245 | rom = pm_open(rom_fname); |
246 | if (rom == NULL) { |
247 | lprintf("Failed to open ROM"); |
4c2e3554 |
248 | media_type = PM_ERROR; |
3e9da86e |
249 | goto out; |
250 | } |
251 | |
252 | ret = PicoCartLoad(rom, &rom_data, &rom_size, (PicoAHW & PAHW_SMS) ? 1 : 0); |
253 | pm_close(rom); |
254 | if (ret != 0) { |
255 | if (ret == 2) lprintf("Out of memory"); |
256 | else if (ret == 3) lprintf("Read failed"); |
257 | else lprintf("PicoCartLoad() failed."); |
4c2e3554 |
258 | media_type = PM_ERROR; |
3e9da86e |
259 | goto out; |
260 | } |
4c2e3554 |
261 | |
3e9da86e |
262 | // detect wrong files |
263 | if (strncmp((char *)rom_data, "Pico", 4) == 0) { |
4c2e3554 |
264 | lprintf("savestate selected?\n"); |
265 | media_type = PM_BAD_DETECT; |
3e9da86e |
266 | goto out; |
4c2e3554 |
267 | } |
268 | |
3e9da86e |
269 | if (!(PicoAHW & PAHW_SMS)) { |
270 | unsigned short *d = (unsigned short *)(rom_data + 4); |
271 | if ((((d[0] << 16) | d[1]) & 0xffffff) >= (int)rom_size) { |
272 | lprintf("bad reset vector\n"); |
4c2e3554 |
273 | media_type = PM_BAD_DETECT; |
274 | goto out; |
3e9da86e |
275 | } |
276 | } |
4c2e3554 |
277 | |
3e9da86e |
278 | // load config for this ROM (do this before insert to get correct region) |
279 | if (!(PicoAHW & PAHW_MCD)) { |
280 | memcpy(media_id_header, rom_data + 0x100, sizeof(media_id_header)); |
4c2e3554 |
281 | if (do_region_override != NULL) |
282 | do_region_override(filename); |
283 | } |
284 | |
3e9da86e |
285 | if (PicoCartInsert(rom_data, rom_size, carthw_cfg_fname)) { |
4c2e3554 |
286 | media_type = PM_ERROR; |
3e9da86e |
287 | goto out; |
288 | } |
4c2e3554 |
289 | rom_data = NULL; // now belongs to PicoCart |
290 | |
3e9da86e |
291 | // insert CD if it was detected |
292 | if (cd_img_type != CIT_NOT_CD) { |
293 | ret = Insert_CD(filename, cd_img_type); |
294 | if (ret != 0) { |
295 | PicoCartUnload(); |
4c2e3554 |
296 | media_type = PM_BAD_CD; |
3e9da86e |
297 | goto out; |
298 | } |
299 | } |
4c2e3554 |
300 | |
a76fad41 |
301 | if (PicoQuirks & PQUIRK_FORCE_6BTN) |
302 | PicoSetInputDevice(0, PICO_INPUT_PAD_6BTN); |
303 | |
4c2e3554 |
304 | out: |
3e9da86e |
305 | if (rom_data) |
306 | free(rom_data); |
4c2e3554 |
307 | return media_type; |
308 | } |
309 | |
310 | // vim:shiftwidth=2:ts=2:expandtab |