platform ps2, handle audio similar to psp
[picodrive.git] / pico / media.c
... / ...
CommitLineData
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/cd_parse.h"
12
13unsigned char media_id_header[0x100];
14
15static void strlwr_(char *string)
16{
17 char *p;
18 for (p = string; *p; p++)
19 if ('A' <= *p && *p <= 'Z')
20 *p += 'a' - 'A';
21}
22
23static void get_ext(const char *file, char *ext)
24{
25 const char *p;
26
27 p = file + strlen(file) - 4;
28 if (p < file) p = file;
29 strncpy(ext, p, 4);
30 ext[4] = 0;
31 strlwr_(ext);
32}
33
34static int detect_media(const char *fname, const unsigned char *rom, unsigned int romsize)
35{
36 static const short sms_offsets[] = { 0x7ff0, 0x3ff0, 0x1ff0 };
37 static const char *sms_exts[] = { "sms", "gg", "sg", "sc" };
38 static const char *md_exts[] = { "gen", "smd", "md", "32x" };
39 static const char *pico_exts[] = { "pco" };
40 char buff0[512], buff[32];
41 unsigned short *d16 = NULL;
42 pm_file *pmf = NULL;
43 const char *ext_ptr = NULL;
44 char ext[8];
45 int i;
46
47 ext[0] = '\0';
48 if ((ext_ptr = strrchr(fname, '.'))) {
49 strncpy(ext, ext_ptr + 1, sizeof(ext));
50 ext[sizeof(ext) - 1] = '\0';
51 }
52
53 // detect wrong extensions
54 if (!strcasecmp(ext, "srm") || !strcasecmp(ext, "gz")) // s.gz ~ .mds.gz
55 return PM_BAD_DETECT;
56
57 /* don't believe in extensions, except .cue and .chd */
58 if (strcasecmp(ext, "cue") == 0 || strcasecmp(ext, "chd") == 0)
59 return PM_CD;
60
61 /* Open rom file, if required */
62 if (!rom) {
63 pmf = pm_open(fname);
64 if (pmf == NULL)
65 return PM_BAD_DETECT;
66 romsize = pmf->size;
67 }
68
69 if (!rom) {
70 if (pm_read(buff0, 512, pmf) != 512) {
71 pm_close(pmf);
72 return PM_BAD_DETECT;
73 }
74 } else {
75 if (romsize < 512)
76 return PM_BAD_DETECT;
77 memcpy(buff0, rom, 512);
78 }
79
80 if (strncasecmp("SEGADISCSYSTEM", buff0 + 0x00, 14) == 0 ||
81 strncasecmp("SEGADISCSYSTEM", buff0 + 0x10, 14) == 0) {
82 pm_close(pmf);
83 return PM_CD;
84 }
85
86 /* check for SMD evil */
87 if (romsize >= 0x4200 && (romsize & 0x3fff) == 0x200) {
88 buff[0] = '\0';
89
90 if (!rom) {
91 if (pm_seek(pmf, sms_offsets[0] + 0x200, SEEK_SET) == sms_offsets[0] + 0x200)
92 pm_read(buff, 16, pmf);
93 } else {
94 if (romsize >= sms_offsets[0] + 0x200 + 16)
95 memcpy(buff, rom + sms_offsets[0] + 0x200, 16);
96 }
97
98 if (strncmp("TMR SEGA", buff, 8) == 0)
99 goto looks_like_sms;
100
101 /* could parse further but don't bother */
102 goto extension_check;
103 }
104
105 /* fetch header info */
106 memset(buff, '\0', 17);
107 if (!rom) {
108 if (pm_seek(pmf, 0x100, SEEK_SET) == 0x100)
109 pm_read(buff, 16, pmf);
110 } else {
111 if (romsize >= 0x100 + 16)
112 memcpy(buff, rom + 0x100, 16);
113 }
114 /* PICO header? Almost always appropriately marked */
115 if (strstr(buff, " PICO "))
116 goto looks_like_pico;
117 /* MD header? Act as TMSS BIOS here */
118 if (strncmp(buff, "SEGA", 4) == 0 || strncmp(buff, " SEG", 4) == 0)
119 goto looks_like_md;
120
121 for (i = 0; i < ARRAY_SIZE(sms_offsets); i++) {
122 if (!rom) {
123 if (pm_seek(pmf, sms_offsets[i], SEEK_SET) != sms_offsets[i])
124 continue;
125
126 if (pm_read(buff, 16, pmf) != 16)
127 continue;
128 } else {
129 if (romsize < sms_offsets[i] + 16)
130 continue;
131
132 memcpy(buff, rom + sms_offsets[i], 16);
133 }
134
135 if (strncmp("TMR SEGA", buff, 8) == 0)
136 goto looks_like_sms;
137 }
138
139extension_check:
140 /* probably some headerless thing. Maybe check the extension after all. */
141 ext_ptr = pmf && *pmf->ext ? pmf->ext : ext;
142
143 for (i = 0; i < ARRAY_SIZE(md_exts); i++)
144 if (strcasecmp(ext_ptr, md_exts[i]) == 0)
145 goto looks_like_md;
146
147 for (i = 0; i < ARRAY_SIZE(sms_exts); i++)
148 if (strcasecmp(ext_ptr, sms_exts[i]) == 0)
149 goto looks_like_sms;
150
151 for (i = 0; i < ARRAY_SIZE(pico_exts); i++)
152 if (strcasecmp(ext_ptr, pico_exts[i]) == 0)
153 goto looks_like_pico;
154
155 /* If everything else fails, make a guess on the reset vector */
156 d16 = (unsigned short *)(buff0 + 4);
157 if ((((d16[0] << 16) | d16[1]) & 0xffffff) >= romsize) {
158 lprintf("bad MD reset vector, assuming SMS\n");
159 goto looks_like_sms;
160 }
161 d16 = (unsigned short *)(buff0 + 0x1a0);
162 if ((((d16[0] << 16) | d16[1]) & 0xffffff) != 0) {
163 lprintf("bad MD rom start, assuming SMS\n");
164 goto looks_like_sms;
165 }
166
167looks_like_md:
168 pm_close(pmf);
169 return PM_MD_CART;
170
171looks_like_sms:
172 pm_close(pmf);
173 return PM_MARK3;
174
175looks_like_pico:
176 pm_close(pmf);
177 return PM_PICO;
178}
179
180/* checks if fname points to valid MegaCD image */
181int PicoCdCheck(const char *fname_in, int *pregion)
182{
183 const char *fname = fname_in;
184 unsigned char buf[32];
185 pm_file *cd_f;
186 int region = 4; // 1: Japan, 4: US, 8: Europe
187 char ext[5];
188 enum cd_track_type type = CT_UNKNOWN;
189 cd_data_t *cd_data = NULL;
190
191 // opens a cue, or searches for one
192 if (!cd_data && (cd_data = cue_parse(fname_in)) == NULL) {
193 get_ext(fname_in, ext);
194 if (strcasecmp(ext, ".cue") == 0)
195 return -1;
196 }
197 // opens a chd
198 if (!cd_data && (cd_data = chd_parse(fname_in)) == NULL) {
199 get_ext(fname_in, ext);
200 if (strcasecmp(ext, ".chd") == 0)
201 return -1;
202 }
203
204 if (cd_data != NULL) {
205 // 1st track contains the code
206 fname = cd_data->tracks[1].fname;
207 type = cd_data->tracks[1].type;
208 }
209
210 cd_f = pm_open(fname);
211 cdparse_destroy(cd_data);
212 if (cd_f == NULL) return CT_UNKNOWN; // let the upper level handle this
213
214 if (pm_read(buf, 32, cd_f) != 32) {
215 pm_close(cd_f);
216 return -1;
217 }
218
219 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) {
220 if (type && type != CT_ISO)
221 elprintf(EL_STATUS, ".cue has wrong type: %i", type);
222 type = CT_ISO; // Sega CD (ISO)
223 }
224 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) {
225 if (type && type != CT_BIN)
226 elprintf(EL_STATUS, ".cue has wrong type: %i", type);
227 type = CT_BIN; // Sega CD (BIN)
228 }
229
230 if (type == CT_UNKNOWN) {
231 pm_close(cd_f);
232 return 0;
233 }
234
235 pm_seek(cd_f, (type == CT_ISO) ? 0x100 : 0x110, SEEK_SET);
236 pm_read(media_id_header, sizeof(media_id_header), cd_f);
237
238 /* it seems we have a CD image here. Try to detect region now.. */
239 pm_seek(cd_f, (type == CT_ISO) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);
240 pm_read(buf, 1, cd_f);
241 pm_close(cd_f);
242
243 if (buf[0] == 0x64) region = 8; // EU
244 if (buf[0] == 0xa1) region = 1; // JAP
245
246 lprintf("detected %s Sega/Mega CD image with %s region\n",
247 type == CT_BIN ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
248
249 if (pregion != NULL)
250 *pregion = region;
251
252 return type;
253}
254
255enum media_type_e PicoLoadMedia(const char *filename,
256 const unsigned char *rom, unsigned int romsize,
257 const char *carthw_cfg_fname,
258 const char *(*get_bios_filename)(int *region, const char *cd_fname),
259 void (*do_region_override)(const char *media_filename))
260{
261 const char *rom_fname = filename;
262 enum media_type_e media_type;
263 enum cd_track_type cd_img_type = CT_UNKNOWN;
264 pm_file *rom_file = NULL;
265 unsigned char *rom_data = NULL;
266 unsigned int rom_size = 0;
267 int cd_region = 0;
268 int ret;
269
270 media_type = detect_media(filename, rom, romsize);
271 if (media_type == PM_BAD_DETECT)
272 goto out;
273
274 if ((PicoIn.AHW & PAHW_MCD) && Pico_mcd != NULL)
275 cdd_unload();
276 PicoCartUnload();
277 PicoIn.AHW = 0;
278 PicoIn.quirks = 0;
279
280 if (media_type == PM_CD)
281 {
282 // check for MegaCD image
283 cd_img_type = PicoCdCheck(filename, &cd_region);
284 if ((int)cd_img_type >= 0 && cd_img_type != CT_UNKNOWN)
285 {
286 // valid CD image, ask frontend for BIOS..
287 rom_fname = NULL;
288 if (get_bios_filename != NULL)
289 rom_fname = get_bios_filename(&cd_region, filename);
290 if (rom_fname == NULL) {
291 media_type = PM_BAD_CD_NO_BIOS;
292 goto out;
293 }
294
295 PicoIn.AHW |= PAHW_MCD;
296 }
297 else {
298 media_type = PM_BAD_CD;
299 goto out;
300 }
301 }
302 else if (media_type == PM_MARK3) {
303 PicoIn.AHW = PAHW_SMS;
304 }
305 else if (media_type == PM_PICO) {
306 PicoIn.AHW = PAHW_PICO;
307 }
308
309 if (!rom) {
310 rom_file = pm_open(rom_fname);
311 if (rom_file == NULL) {
312 lprintf("Failed to open ROM\n");
313 media_type = PM_ERROR;
314 goto out;
315 }
316 }
317
318 ret = PicoCartLoad(rom_file, rom, romsize, &rom_data, &rom_size, (PicoIn.AHW & PAHW_SMS) ? 1 : 0);
319 if (ret != 0) {
320 if (ret == 2) lprintf("Out of memory\n");
321 else if (ret == 3) lprintf("Read failed\n");
322 else lprintf("PicoCartLoad() failed.\n");
323 media_type = PM_ERROR;
324 goto out;
325 }
326
327 // detect wrong files
328 if (strncmp((char *)rom_data, "Pico", 4) == 0) {
329 lprintf("savestate selected?\n");
330 media_type = PM_BAD_DETECT;
331 goto out;
332 }
333
334 if (!(PicoIn.AHW & PAHW_SMS)) {
335 unsigned short *d = (unsigned short *)(rom_data + 4);
336 if ((((d[0] << 16) | d[1]) & 0xffffff) >= (int)rom_size) {
337 lprintf("bad reset vector\n");
338 media_type = PM_BAD_DETECT;
339 goto out;
340 }
341 }
342
343 // load config for this ROM (do this before insert to get correct region)
344 if (!(PicoIn.AHW & PAHW_MCD)) {
345 memcpy(media_id_header, rom_data + 0x100, sizeof(media_id_header));
346 if (do_region_override != NULL)
347 do_region_override(filename);
348 }
349
350 // simple test for GG. Do this here since m.hardware is nulled in Insert
351 if ((PicoIn.AHW & PAHW_SMS) && !PicoIn.hwSelect) {
352 const char *ext = NULL;
353 if (rom_file && (*rom_file->ext != '\0')) {
354 ext = rom_file->ext;
355 }
356 else if ((ext = strrchr(filename, '.'))) {
357 if (*(++ext) == '\0') {
358 ext = NULL;
359 }
360 }
361 if (ext && !strcasecmp(ext,"gg")) {
362 PicoIn.AHW |= PAHW_GG;
363 lprintf("detected GG ROM\n");
364 } else if (ext && !strcasecmp(ext,"sg")) {
365 PicoIn.AHW |= PAHW_SG;
366 lprintf("detected SG-1000 ROM\n");
367 } else if (ext && !strcasecmp(ext,"sc")) {
368 PicoIn.AHW |= PAHW_SC;
369 lprintf("detected SC-3000 ROM\n");
370 } else
371 lprintf("detected SMS ROM\n");
372 }
373
374 if (PicoCartInsert(rom_data, rom_size, carthw_cfg_fname)) {
375 media_type = PM_ERROR;
376 goto out;
377 }
378 rom_data = NULL; // now belongs to PicoCart
379
380 // insert CD if it was detected
381 Pico.m.ncart_in = 0;
382 if (cd_img_type != CT_UNKNOWN) {
383 ret = cdd_load(filename, cd_img_type);
384 if (ret != 0) {
385 PicoCartUnload();
386 media_type = PM_BAD_CD;
387 goto out;
388 }
389 if (Pico.romsize <= 0x20000)
390 Pico.m.ncart_in = 1;
391 }
392
393 if (PicoIn.quirks & PQUIRK_FORCE_6BTN)
394 PicoSetInputDevice(0, PICO_INPUT_PAD_6BTN);
395
396out:
397 if (rom_file)
398 pm_close(rom_file);
399 if (rom_data)
400 PicoCartUnload();
401 return media_type;
402}
403
404// vim:shiftwidth=2:ts=2:expandtab