sh2: timing fixes
[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/cue.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)
35{
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 }
97
98extension_check:
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 }
114
115looks_like_md:
116 pm_close(pmf);
117 return PM_MD_CART;
118
119looks_like_sms:
120 pm_close(pmf);
121 return PM_MARK3;
122}
123
124/* checks if fname points to valid MegaCD image */
125int PicoCdCheck(const char *fname_in, int *pregion)
126{
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)
188 *pregion = region;
189
190 return type;
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;
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;
204 int cd_region = 0;
205 int ret;
206
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;
215
216 if (media_type == PM_CD)
217 {
218 // check for MegaCD image
219 cd_img_type = PicoCdCheck(filename, &cd_region);
220 if (cd_img_type >= 0 && cd_img_type != CIT_NOT_CD)
221 {
222 // valid CD image, ask frontend for BIOS..
223 rom_fname = NULL;
224 if (get_bios_filename != NULL)
225 rom_fname = get_bios_filename(&cd_region, filename);
226 if (rom_fname == NULL) {
227 media_type = PM_BAD_CD_NO_BIOS;
228 goto out;
229 }
230
231 PicoAHW |= PAHW_MCD;
232 }
233 else {
234 media_type = PM_BAD_CD;
235 goto out;
236 }
237 }
238 else if (media_type == PM_MARK3) {
239 lprintf("detected SMS ROM\n");
240 PicoAHW = PAHW_SMS;
241 }
242
243 rom = pm_open(rom_fname);
244 if (rom == NULL) {
245 lprintf("Failed to open ROM");
246 media_type = PM_ERROR;
247 goto out;
248 }
249
250 ret = PicoCartLoad(rom, &rom_data, &rom_size, (PicoAHW & PAHW_SMS) ? 1 : 0);
251 pm_close(rom);
252 if (ret != 0) {
253 if (ret == 2) lprintf("Out of memory");
254 else if (ret == 3) lprintf("Read failed");
255 else lprintf("PicoCartLoad() failed.");
256 media_type = PM_ERROR;
257 goto out;
258 }
259
260 // detect wrong files
261 if (strncmp((char *)rom_data, "Pico", 4) == 0) {
262 lprintf("savestate selected?\n");
263 media_type = PM_BAD_DETECT;
264 goto out;
265 }
266
267 if (!(PicoAHW & PAHW_SMS)) {
268 unsigned short *d = (unsigned short *)(rom_data + 4);
269 if ((((d[0] << 16) | d[1]) & 0xffffff) >= (int)rom_size) {
270 lprintf("bad reset vector\n");
271 media_type = PM_BAD_DETECT;
272 goto out;
273 }
274 }
275
276 // load config for this ROM (do this before insert to get correct region)
277 if (!(PicoAHW & PAHW_MCD)) {
278 memcpy(media_id_header, rom_data + 0x100, sizeof(media_id_header));
279 if (do_region_override != NULL)
280 do_region_override(filename);
281 }
282
283 if (PicoCartInsert(rom_data, rom_size, carthw_cfg_fname)) {
284 media_type = PM_ERROR;
285 goto out;
286 }
287 rom_data = NULL; // now belongs to PicoCart
288
289 // insert CD if it was detected
290 if (cd_img_type != CIT_NOT_CD) {
291 ret = Insert_CD(filename, cd_img_type);
292 if (ret != 0) {
293 PicoCartUnload();
294 media_type = PM_BAD_CD;
295 goto out;
296 }
297 }
298
299out:
300 if (rom_data)
301 free(rom_data);
302 return media_type;
303}
304
305// vim:shiftwidth=2:ts=2:expandtab