3 * (C) notaz, 2006-2010,2013
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
13 unsigned char media_id_header[0x100];
15 static void strlwr_(char *string)
18 for (p = string; *p; p++)
19 if ('A' <= *p && *p <= 'Z')
23 static void get_ext(const char *file, char *ext)
27 p = file + strlen(file) - 4;
28 if (p < file) p = file;
34 static int detect_media(const char *fname)
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];
47 // detect wrong extensions
48 if (!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) // s.gz ~ .mds.gz
51 /* don't believe in extensions, except .cue */
52 if (strcasecmp(ext, ".cue") == 0)
59 if (pm_read(buff0, 32, pmf) != 32) {
64 if (strncasecmp("SEGADISCSYSTEM", buff0 + 0x00, 14) == 0 ||
65 strncasecmp("SEGADISCSYSTEM", buff0 + 0x10, 14) == 0) {
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)
77 /* could parse further but don't bother */
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)
87 for (i = 0; i < ARRAY_SIZE(sms_offsets); i++) {
88 if (pm_seek(pmf, sms_offsets[i], SEEK_SET) != sms_offsets[i])
91 if (pm_read(buff, 16, pmf) != 16)
94 if (strncmp("TMR SEGA", buff, 8) == 0)
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)
104 for (i = 0; i < ARRAY_SIZE(sms_exts); i++)
105 if (strcasecmp(pmf->ext, sms_exts[i]) == 0)
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");
124 /* checks if fname points to valid MegaCD image */
125 int PicoCdCheck(const char *fname_in, int *pregion)
127 const char *fname = fname_in;
128 unsigned char buf[32];
130 int region = 4; // 1: Japan, 4: US, 8: Europe
132 cue_track_type type = CT_UNKNOWN;
133 cue_data_t *cue_data = NULL;
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;
142 get_ext(fname_in, ext);
143 if (strcasecmp(ext, ".cue") == 0)
147 cd_f = pm_open(fname);
148 if (cue_data != NULL)
149 cue_destroy(cue_data);
151 if (cd_f == NULL) return 0; // let the upper level handle this
153 if (pm_read(buf, 32, cd_f) != 32) {
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)
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)
169 if (type == CT_UNKNOWN) {
174 pm_seek(cd_f, (type == CT_ISO) ? 0x100 : 0x110, SEEK_SET);
175 pm_read(media_id_header, sizeof(media_id_header), cd_f);
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);
182 if (buf[0] == 0x64) region = 8; // EU
183 if (buf[0] == 0xa1) region = 1; // JAP
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");
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))
199 const char *rom_fname = filename;
200 enum media_type_e media_type;
201 cd_img_type cd_img_type = CIT_NOT_CD;
202 unsigned char *rom_data = NULL;
203 unsigned int rom_size = 0;
208 media_type = detect_media(filename);
209 if (media_type == PM_BAD_DETECT)
212 if ((PicoAHW & PAHW_MCD) && Pico_mcd != NULL)
218 if (media_type == PM_CD)
220 // check for MegaCD image
221 cd_img_type = PicoCdCheck(filename, &cd_region);
222 if ((int)cd_img_type >= 0 && cd_img_type != CIT_NOT_CD)
224 // valid CD image, ask frontend for BIOS..
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;
236 media_type = PM_BAD_CD;
240 else if (media_type == PM_MARK3) {
241 lprintf("detected SMS ROM\n");
245 rom = pm_open(rom_fname);
247 lprintf("Failed to open ROM");
248 media_type = PM_ERROR;
252 ret = PicoCartLoad(rom, &rom_data, &rom_size, (PicoAHW & PAHW_SMS) ? 1 : 0);
255 if (ret == 2) lprintf("Out of memory");
256 else if (ret == 3) lprintf("Read failed");
257 else lprintf("PicoCartLoad() failed.");
258 media_type = PM_ERROR;
262 // detect wrong files
263 if (strncmp((char *)rom_data, "Pico", 4) == 0) {
264 lprintf("savestate selected?\n");
265 media_type = PM_BAD_DETECT;
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");
273 media_type = PM_BAD_DETECT;
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));
281 if (do_region_override != NULL)
282 do_region_override(filename);
285 if (PicoCartInsert(rom_data, rom_size, carthw_cfg_fname)) {
286 media_type = PM_ERROR;
289 rom_data = NULL; // now belongs to PicoCart
291 // insert CD if it was detected
292 if (cd_img_type != CIT_NOT_CD) {
293 ret = Insert_CD(filename, cd_img_type);
296 media_type = PM_BAD_CD;
301 if (PicoQuirks & PQUIRK_FORCE_6BTN)
302 PicoSetInputDevice(0, PICO_INPUT_PAD_6BTN);
310 // vim:shiftwidth=2:ts=2:expandtab