rename cd/pico.c -> cd/mcd.c
[picodrive.git] / pico / media.c
CommitLineData
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
13unsigned char media_id_header[0x100];
14
15static 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
23static 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
34static 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
98extension_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
115looks_like_md:
3e9da86e 116 pm_close(pmf);
117 return PM_MD_CART;
4c2e3554 118
119looks_like_sms:
3e9da86e 120 pm_close(pmf);
121 return PM_MARK3;
4c2e3554 122}
123
124/* checks if fname points to valid MegaCD image */
125int 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
135 get_ext(fname_in, ext);
136 if (strcasecmp(ext, ".cue") == 0) {
137 cue_data = cue_parse(fname_in);
138 if (cue_data != NULL) {
139 fname = cue_data->tracks[1].fname;
140 type = cue_data->tracks[1].type;
141 }
142 else
143 return -1;
144 }
145
146 cd_f = pm_open(fname);
147 if (cue_data != NULL)
148 cue_destroy(cue_data);
149
150 if (cd_f == NULL) return 0; // let the upper level handle this
151
152 if (pm_read(buf, 32, cd_f) != 32) {
153 pm_close(cd_f);
154 return -1;
155 }
156
157 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) {
158 if (type && type != CT_ISO)
159 elprintf(EL_STATUS, ".cue has wrong type: %i", type);
160 type = CT_ISO; // Sega CD (ISO)
161 }
162 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) {
163 if (type && type != CT_BIN)
164 elprintf(EL_STATUS, ".cue has wrong type: %i", type);
165 type = CT_BIN; // Sega CD (BIN)
166 }
167
168 if (type == CT_UNKNOWN) {
169 pm_close(cd_f);
170 return 0;
171 }
172
173 pm_seek(cd_f, (type == CT_ISO) ? 0x100 : 0x110, SEEK_SET);
174 pm_read(media_id_header, sizeof(media_id_header), cd_f);
175
176 /* it seems we have a CD image here. Try to detect region now.. */
177 pm_seek(cd_f, (type == CT_ISO) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);
178 pm_read(buf, 1, cd_f);
179 pm_close(cd_f);
180
181 if (buf[0] == 0x64) region = 8; // EU
182 if (buf[0] == 0xa1) region = 1; // JAP
183
184 lprintf("detected %s Sega/Mega CD image with %s region\n",
185 type == CT_BIN ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
186
187 if (pregion != NULL)
4c2e3554 188 *pregion = region;
189
3e9da86e 190 return type;
4c2e3554 191}
192
193enum media_type_e PicoLoadMedia(const char *filename,
194 const char *carthw_cfg_fname,
195 const char *(*get_bios_filename)(int *region, const char *cd_fname),
196 void (*do_region_override)(const char *media_filename))
197{
198 const char *rom_fname = filename;
199 enum media_type_e media_type;
3e9da86e 200 cd_img_type cd_img_type = CIT_NOT_CD;
201 unsigned char *rom_data = NULL;
202 unsigned int rom_size = 0;
203 pm_file *rom = NULL;
4c2e3554 204 int cd_region = 0;
205 int ret;
206
3e9da86e 207 media_type = detect_media(filename);
208 if (media_type == PM_BAD_DETECT)
209 goto out;
210
211 if ((PicoAHW & PAHW_MCD) && Pico_mcd != NULL)
212 Stop_CD();
213 PicoCartUnload();
214 PicoAHW = 0;
a76fad41 215 PicoQuirks = 0;
3e9da86e 216
217 if (media_type == PM_CD)
218 {
219 // check for MegaCD image
220 cd_img_type = PicoCdCheck(filename, &cd_region);
da77daa9 221 if ((int)cd_img_type >= 0 && cd_img_type != CIT_NOT_CD)
3e9da86e 222 {
223 // valid CD image, ask frontend for BIOS..
4c2e3554 224 rom_fname = NULL;
225 if (get_bios_filename != NULL)
226 rom_fname = get_bios_filename(&cd_region, filename);
227 if (rom_fname == NULL) {
228 media_type = PM_BAD_CD_NO_BIOS;
229 goto out;
230 }
231
3e9da86e 232 PicoAHW |= PAHW_MCD;
233 }
234 else {
4c2e3554 235 media_type = PM_BAD_CD;
236 goto out;
3e9da86e 237 }
238 }
239 else if (media_type == PM_MARK3) {
240 lprintf("detected SMS ROM\n");
241 PicoAHW = PAHW_SMS;
242 }
243
244 rom = pm_open(rom_fname);
245 if (rom == NULL) {
246 lprintf("Failed to open ROM");
4c2e3554 247 media_type = PM_ERROR;
3e9da86e 248 goto out;
249 }
250
251 ret = PicoCartLoad(rom, &rom_data, &rom_size, (PicoAHW & PAHW_SMS) ? 1 : 0);
252 pm_close(rom);
253 if (ret != 0) {
254 if (ret == 2) lprintf("Out of memory");
255 else if (ret == 3) lprintf("Read failed");
256 else lprintf("PicoCartLoad() failed.");
4c2e3554 257 media_type = PM_ERROR;
3e9da86e 258 goto out;
259 }
4c2e3554 260
3e9da86e 261 // detect wrong files
262 if (strncmp((char *)rom_data, "Pico", 4) == 0) {
4c2e3554 263 lprintf("savestate selected?\n");
264 media_type = PM_BAD_DETECT;
3e9da86e 265 goto out;
4c2e3554 266 }
267
3e9da86e 268 if (!(PicoAHW & PAHW_SMS)) {
269 unsigned short *d = (unsigned short *)(rom_data + 4);
270 if ((((d[0] << 16) | d[1]) & 0xffffff) >= (int)rom_size) {
271 lprintf("bad reset vector\n");
4c2e3554 272 media_type = PM_BAD_DETECT;
273 goto out;
3e9da86e 274 }
275 }
4c2e3554 276
3e9da86e 277 // load config for this ROM (do this before insert to get correct region)
278 if (!(PicoAHW & PAHW_MCD)) {
279 memcpy(media_id_header, rom_data + 0x100, sizeof(media_id_header));
4c2e3554 280 if (do_region_override != NULL)
281 do_region_override(filename);
282 }
283
3e9da86e 284 if (PicoCartInsert(rom_data, rom_size, carthw_cfg_fname)) {
4c2e3554 285 media_type = PM_ERROR;
3e9da86e 286 goto out;
287 }
4c2e3554 288 rom_data = NULL; // now belongs to PicoCart
289
3e9da86e 290 // insert CD if it was detected
291 if (cd_img_type != CIT_NOT_CD) {
292 ret = Insert_CD(filename, cd_img_type);
293 if (ret != 0) {
294 PicoCartUnload();
4c2e3554 295 media_type = PM_BAD_CD;
3e9da86e 296 goto out;
297 }
298 }
4c2e3554 299
a76fad41 300 if (PicoQuirks & PQUIRK_FORCE_6BTN)
301 PicoSetInputDevice(0, PICO_INPUT_PAD_6BTN);
302
4c2e3554 303out:
3e9da86e 304 if (rom_data)
305 free(rom_data);
4c2e3554 306 return media_type;
307}
308
309// vim:shiftwidth=2:ts=2:expandtab