bcc9eda0 |
1 | // (c) Copyright 2006-2009 notaz, All rights reserved.\r |
ea8c405f |
2 | // Free for non-commercial use.\r |
3 | \r |
4 | // For commercial use, separate licencing terms must be obtained.\r |
5 | \r |
6 | #include <stdio.h>\r |
7 | #include <stdlib.h>\r |
b24e0f6c |
8 | #include <stdarg.h>\r |
ea8c405f |
9 | #ifndef NO_SYNC\r |
10 | #include <unistd.h>\r |
11 | #endif\r |
12 | \r |
13 | #include "emu.h"\r |
14 | #include "menu.h"\r |
15 | #include "fonts.h"\r |
16 | #include "lprintf.h"\r |
58c86d00 |
17 | #include "config.h"\r |
713c9224 |
18 | #include "plat.h"\r |
d34a42f9 |
19 | #include "input.h"\r |
f2cf8472 |
20 | #include "posix.h"\r |
ea8c405f |
21 | \r |
efcba75f |
22 | #include <pico/pico_int.h>\r |
23 | #include <pico/patch.h>\r |
24 | #include <pico/cd/cue.h>\r |
ea8c405f |
25 | \r |
ea8c405f |
26 | \r |
54646a39 |
27 | #define STATUS_MSG_TIMEOUT 2000\r |
28 | \r |
e2de9939 |
29 | void *g_screen_ptr;\r |
30 | \r |
31 | #if !SCREEN_SIZE_FIXED\r |
32 | int g_screen_width = SCREEN_WIDTH;\r |
33 | int g_screen_height = SCREEN_HEIGHT;\r |
34 | #endif\r |
35 | \r |
c46ffd31 |
36 | char *PicoConfigFile = "config.cfg";\r |
58c86d00 |
37 | currentConfig_t currentConfig, defaultConfig;\r |
ea8c405f |
38 | int state_slot = 0;\r |
39 | int config_slot = 0, config_slot_current = 0;\r |
f2cf8472 |
40 | int pico_pen_x = 320/2, pico_pen_y = 240/2;\r |
c060a9ab |
41 | int pico_inp_mode = 0;\r |
713c9224 |
42 | int engineState = PGS_Menu;\r |
43 | \r |
ae87bffa |
44 | /* tmp buff to reduce stack usage for plats with small stack */\r |
45 | static char static_buff[512];\r |
713c9224 |
46 | /* TODO: len checking */\r |
ae87bffa |
47 | char rom_fname_reload[512];\r |
48 | char rom_fname_loaded[512];\r |
713c9224 |
49 | int rom_loaded = 0;\r |
f2cf8472 |
50 | int reset_timing = 0;\r |
b24e0f6c |
51 | static unsigned int notice_msg_time; /* when started showing */\r |
52 | static char noticeMsg[40];\r |
ea8c405f |
53 | \r |
54 | unsigned char *movie_data = NULL;\r |
55 | static int movie_size = 0;\r |
56 | \r |
ea8c405f |
57 | \r |
ee2a3bdf |
58 | /* don't use tolower() for easy old glibc binary compatibility */\r |
f8af9634 |
59 | static void strlwr_(char *string)\r |
ea8c405f |
60 | {\r |
f8af9634 |
61 | char *p;\r |
62 | for (p = string; *p; p++)\r |
ee2a3bdf |
63 | if ('A' <= *p && *p <= 'Z')\r |
64 | *p += 'a' - 'A';\r |
ea8c405f |
65 | }\r |
66 | \r |
ca482e5d |
67 | static int try_rfn_cut(char *fname)\r |
ea8c405f |
68 | {\r |
69 | FILE *tmp;\r |
70 | char *p;\r |
71 | \r |
ca482e5d |
72 | p = fname + strlen(fname) - 1;\r |
73 | for (; p > fname; p--)\r |
ea8c405f |
74 | if (*p == '.') break;\r |
75 | *p = 0;\r |
76 | \r |
ca482e5d |
77 | if((tmp = fopen(fname, "rb"))) {\r |
ea8c405f |
78 | fclose(tmp);\r |
79 | return 1;\r |
80 | }\r |
81 | return 0;\r |
82 | }\r |
83 | \r |
35e3031a |
84 | static void get_ext(const char *file, char *ext)\r |
ea8c405f |
85 | {\r |
35e3031a |
86 | const char *p;\r |
ea8c405f |
87 | \r |
88 | p = file + strlen(file) - 4;\r |
89 | if (p < file) p = file;\r |
90 | strncpy(ext, p, 4);\r |
91 | ext[4] = 0;\r |
92 | strlwr_(ext);\r |
93 | }\r |
94 | \r |
fcdefcf6 |
95 | static void fname_ext(char *dst, int dstlen, const char *prefix, const char *ext, const char *fname)\r |
96 | {\r |
97 | int prefix_len = 0;\r |
98 | const char *p;\r |
99 | \r |
100 | *dst = 0;\r |
101 | if (prefix) {\r |
102 | int len = plat_get_root_dir(dst, dstlen);\r |
103 | strcpy(dst + len, prefix);\r |
104 | prefix_len = len + strlen(prefix);\r |
105 | }\r |
106 | \r |
107 | p = fname + strlen(fname) - 1;\r |
108 | for (; p >= fname && *p != PATH_SEP_C; p--)\r |
109 | ;\r |
110 | p++;\r |
111 | strncpy(dst + prefix_len, p, dstlen - prefix_len - 1);\r |
112 | \r |
113 | dst[dstlen - 8] = 0;\r |
114 | if (dst[strlen(dst) - 4] == '.')\r |
115 | dst[strlen(dst) - 4] = 0;\r |
116 | if (ext)\r |
117 | strcat(dst, ext);\r |
118 | }\r |
119 | \r |
120 | static void romfname_ext(char *dst, int dstlen, const char *prefix, const char *ext)\r |
121 | {\r |
122 | fname_ext(dst, dstlen, prefix, ext, rom_fname_loaded);\r |
123 | }\r |
124 | \r |
b24e0f6c |
125 | void emu_status_msg(const char *format, ...)\r |
126 | {\r |
127 | va_list vl;\r |
54646a39 |
128 | int ret;\r |
b24e0f6c |
129 | \r |
130 | va_start(vl, format);\r |
54646a39 |
131 | ret = vsnprintf(noticeMsg, sizeof(noticeMsg), format, vl);\r |
b24e0f6c |
132 | va_end(vl);\r |
133 | \r |
54646a39 |
134 | /* be sure old text gets overwritten */\r |
135 | for (; ret < 28; ret++)\r |
136 | noticeMsg[ret] = ' ';\r |
137 | noticeMsg[ret] = 0;\r |
138 | \r |
b24e0f6c |
139 | notice_msg_time = plat_get_ticks_ms();\r |
140 | }\r |
141 | \r |
54646a39 |
142 | static const char * const biosfiles_us[] = { "us_scd1_9210", "us_scd2_9306", "SegaCDBIOS9303" };\r |
143 | static const char * const biosfiles_eu[] = { "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303" };\r |
144 | static const char * const biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };\r |
ea8c405f |
145 | \r |
a47dd663 |
146 | static int find_bios(int region, char **bios_file)\r |
ea8c405f |
147 | {\r |
ea8c405f |
148 | int i, count;\r |
54646a39 |
149 | const char * const *files;\r |
ea8c405f |
150 | FILE *f = NULL;\r |
151 | \r |
152 | if (region == 4) { // US\r |
153 | files = biosfiles_us;\r |
154 | count = sizeof(biosfiles_us) / sizeof(char *);\r |
155 | } else if (region == 8) { // EU\r |
156 | files = biosfiles_eu;\r |
157 | count = sizeof(biosfiles_eu) / sizeof(char *);\r |
158 | } else if (region == 1 || region == 2) {\r |
159 | files = biosfiles_jp;\r |
160 | count = sizeof(biosfiles_jp) / sizeof(char *);\r |
161 | } else {\r |
162 | return 0;\r |
163 | }\r |
164 | \r |
165 | for (i = 0; i < count; i++)\r |
166 | {\r |
ae87bffa |
167 | emu_make_path(static_buff, files[i], sizeof(static_buff) - 4);\r |
168 | strcat(static_buff, ".bin");\r |
169 | f = fopen(static_buff, "rb");\r |
ea8c405f |
170 | if (f) break;\r |
171 | \r |
ae87bffa |
172 | static_buff[strlen(static_buff) - 4] = 0;\r |
173 | strcat(static_buff, ".zip");\r |
174 | f = fopen(static_buff, "rb");\r |
ea8c405f |
175 | if (f) break;\r |
176 | }\r |
177 | \r |
178 | if (f) {\r |
ae87bffa |
179 | lprintf("using bios: %s\n", static_buff);\r |
ea8c405f |
180 | fclose(f);\r |
ae87bffa |
181 | if (bios_file)\r |
182 | *bios_file = static_buff;\r |
ea8c405f |
183 | return 1;\r |
184 | } else {\r |
ae87bffa |
185 | sprintf(static_buff, "no %s BIOS files found, read docs",\r |
ea8c405f |
186 | region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r |
ae87bffa |
187 | me_update_msg(static_buff);\r |
ea8c405f |
188 | return 0;\r |
189 | }\r |
190 | }\r |
191 | \r |
c6196c0f |
192 | /* check if the name begins with BIOS name */\r |
1ca2ea4f |
193 | /*\r |
c6196c0f |
194 | static int emu_isBios(const char *name)\r |
195 | {\r |
196 | int i;\r |
197 | for (i = 0; i < sizeof(biosfiles_us)/sizeof(biosfiles_us[0]); i++)\r |
198 | if (strstr(name, biosfiles_us[i]) != NULL) return 1;\r |
199 | for (i = 0; i < sizeof(biosfiles_eu)/sizeof(biosfiles_eu[0]); i++)\r |
200 | if (strstr(name, biosfiles_eu[i]) != NULL) return 1;\r |
201 | for (i = 0; i < sizeof(biosfiles_jp)/sizeof(biosfiles_jp[0]); i++)\r |
202 | if (strstr(name, biosfiles_jp[i]) != NULL) return 1;\r |
203 | return 0;\r |
204 | }\r |
1ca2ea4f |
205 | */\r |
c6196c0f |
206 | \r |
1ca2ea4f |
207 | static unsigned char id_header[0x100];\r |
58c86d00 |
208 | \r |
3e49ffd0 |
209 | /* checks if fname points to valid MegaCD image */\r |
35e3031a |
210 | static int emu_cd_check(int *pregion, const char *fname_in)\r |
ea8c405f |
211 | {\r |
35e3031a |
212 | const char *fname = fname_in;\r |
ea8c405f |
213 | unsigned char buf[32];\r |
214 | pm_file *cd_f;\r |
9037e45d |
215 | int region = 4; // 1: Japan, 4: US, 8: Europe\r |
35e3031a |
216 | char ext[5];\r |
9037e45d |
217 | cue_track_type type = CT_UNKNOWN;\r |
218 | cue_data_t *cue_data = NULL;\r |
b923ecbe |
219 | \r |
ca482e5d |
220 | get_ext(fname_in, ext);\r |
9037e45d |
221 | if (strcasecmp(ext, ".cue") == 0) {\r |
ca482e5d |
222 | cue_data = cue_parse(fname_in);\r |
9037e45d |
223 | if (cue_data != NULL) {\r |
224 | fname = cue_data->tracks[1].fname;\r |
225 | type = cue_data->tracks[1].type;\r |
226 | }\r |
f9f40f10 |
227 | else\r |
228 | return -1;\r |
9037e45d |
229 | }\r |
230 | \r |
231 | cd_f = pm_open(fname);\r |
232 | if (cue_data != NULL)\r |
233 | cue_destroy(cue_data);\r |
ea8c405f |
234 | \r |
f9f40f10 |
235 | if (cd_f == NULL) return 0; // let the upper level handle this\r |
ea8c405f |
236 | \r |
237 | if (pm_read(buf, 32, cd_f) != 32) {\r |
238 | pm_close(cd_f);\r |
f9f40f10 |
239 | return -1;\r |
ea8c405f |
240 | }\r |
241 | \r |
9037e45d |
242 | if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) {\r |
243 | if (type && type != CT_ISO)\r |
244 | elprintf(EL_STATUS, ".cue has wrong type: %i", type);\r |
245 | type = CT_ISO; // Sega CD (ISO)\r |
246 | }\r |
247 | if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) {\r |
248 | if (type && type != CT_BIN)\r |
249 | elprintf(EL_STATUS, ".cue has wrong type: %i", type);\r |
250 | type = CT_BIN; // Sega CD (BIN)\r |
251 | }\r |
252 | \r |
253 | if (type == CT_UNKNOWN) {\r |
ea8c405f |
254 | pm_close(cd_f);\r |
255 | return 0;\r |
256 | }\r |
257 | \r |
9037e45d |
258 | pm_seek(cd_f, (type == CT_ISO) ? 0x100 : 0x110, SEEK_SET);\r |
1ca2ea4f |
259 | pm_read(id_header, sizeof(id_header), cd_f);\r |
58c86d00 |
260 | \r |
ea8c405f |
261 | /* it seems we have a CD image here. Try to detect region now.. */\r |
9037e45d |
262 | pm_seek(cd_f, (type == CT_ISO) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);\r |
ea8c405f |
263 | pm_read(buf, 1, cd_f);\r |
264 | pm_close(cd_f);\r |
265 | \r |
266 | if (buf[0] == 0x64) region = 8; // EU\r |
267 | if (buf[0] == 0xa1) region = 1; // JAP\r |
268 | \r |
269 | lprintf("detected %s Sega/Mega CD image with %s region\n",\r |
9037e45d |
270 | type == CT_BIN ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r |
ea8c405f |
271 | \r |
272 | if (pregion != NULL) *pregion = region;\r |
273 | \r |
274 | return type;\r |
275 | }\r |
276 | \r |
3e49ffd0 |
277 | static int detect_media(const char *fname)\r |
278 | {\r |
279 | static const short sms_offsets[] = { 0x7ff0, 0x3ff0, 0x1ff0 };\r |
87b0845f |
280 | static const char *sms_exts[] = { "sms", "gg", "sg" };\r |
281 | static const char *md_exts[] = { "gen", "bin", "smd" };\r |
282 | char buff0[32], buff[32];\r |
283 | unsigned short *d16;\r |
3e49ffd0 |
284 | pm_file *pmf;\r |
3e49ffd0 |
285 | char ext[5];\r |
286 | int i;\r |
287 | \r |
288 | get_ext(fname, ext);\r |
289 | \r |
290 | // detect wrong extensions\r |
291 | if (!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) // s.gz ~ .mds.gz\r |
292 | return PM_BAD;\r |
293 | \r |
294 | /* don't believe in extensions, except .cue */\r |
295 | if (strcasecmp(ext, ".cue") == 0)\r |
296 | return PM_CD;\r |
297 | \r |
298 | pmf = pm_open(fname);\r |
299 | if (pmf == NULL)\r |
300 | return PM_BAD;\r |
301 | \r |
87b0845f |
302 | if (pm_read(buff0, 32, pmf) != 32) {\r |
3e49ffd0 |
303 | pm_close(pmf);\r |
304 | return PM_BAD;\r |
305 | }\r |
306 | \r |
87b0845f |
307 | if (strncasecmp("SEGADISCSYSTEM", buff0 + 0x00, 14) == 0 ||\r |
308 | strncasecmp("SEGADISCSYSTEM", buff0 + 0x10, 14) == 0) {\r |
3e49ffd0 |
309 | pm_close(pmf);\r |
310 | return PM_CD;\r |
311 | }\r |
312 | \r |
87b0845f |
313 | /* check for SMD evil */\r |
314 | if (pmf->size >= 0x4200 && (pmf->size & 0x3fff) == 0x200) {\r |
315 | if (pm_seek(pmf, sms_offsets[0] + 0x200, SEEK_SET) == sms_offsets[0] + 0x200 &&\r |
316 | pm_read(buff, 16, pmf) == 16 &&\r |
317 | strncmp("TMR SEGA", buff, 8) == 0)\r |
318 | goto looks_like_sms;\r |
319 | \r |
320 | /* could parse further but don't bother */\r |
321 | goto extension_check;\r |
322 | }\r |
323 | \r |
324 | /* MD header? Act as TMSS BIOS here */\r |
325 | if (pm_seek(pmf, 0x100, SEEK_SET) == 0x100 && pm_read(buff, 16, pmf) == 16) {\r |
326 | if (strncmp(buff, "SEGA", 4) == 0 || strncmp(buff, " SEG", 4) == 0)\r |
327 | goto looks_like_md;\r |
328 | }\r |
329 | \r |
3e49ffd0 |
330 | for (i = 0; i < array_size(sms_offsets); i++) {\r |
331 | if (pm_seek(pmf, sms_offsets[i], SEEK_SET) != sms_offsets[i])\r |
87b0845f |
332 | continue;\r |
3e49ffd0 |
333 | \r |
334 | if (pm_read(buff, 16, pmf) != 16)\r |
87b0845f |
335 | continue;\r |
3e49ffd0 |
336 | \r |
87b0845f |
337 | if (strncmp("TMR SEGA", buff, 8) == 0)\r |
338 | goto looks_like_sms;\r |
339 | }\r |
340 | \r |
341 | extension_check:\r |
342 | /* probably some headerless thing. Maybe check the extension after all. */\r |
343 | for (i = 0; i < array_size(md_exts); i++)\r |
344 | if (strcasecmp(pmf->ext, md_exts[i]) == 0)\r |
345 | goto looks_like_md;\r |
346 | \r |
347 | for (i = 0; i < array_size(sms_exts); i++)\r |
348 | if (strcasecmp(pmf->ext, sms_exts[i]) == 0)\r |
349 | goto looks_like_sms;\r |
350 | \r |
351 | /* If everything else fails, make a guess on the reset vector */\r |
352 | d16 = (unsigned short *)(buff0 + 4);\r |
353 | if ((((d16[0] << 16) | d16[1]) & 0xffffff) >= pmf->size) {\r |
354 | lprintf("bad MD reset vector, assuming SMS\n");\r |
355 | goto looks_like_sms;\r |
3e49ffd0 |
356 | }\r |
357 | \r |
87b0845f |
358 | looks_like_md:\r |
3e49ffd0 |
359 | pm_close(pmf);\r |
3e49ffd0 |
360 | return PM_MD_CART;\r |
87b0845f |
361 | \r |
362 | looks_like_sms:\r |
363 | pm_close(pmf);\r |
364 | return PM_MARK3;\r |
3e49ffd0 |
365 | }\r |
366 | \r |
7a87643e |
367 | static int extract_text(char *dest, const unsigned char *src, int len, int swab)\r |
58c86d00 |
368 | {\r |
369 | char *p = dest;\r |
370 | int i;\r |
371 | \r |
372 | if (swab) swab = 1;\r |
373 | \r |
374 | for (i = len - 1; i >= 0; i--)\r |
375 | {\r |
376 | if (src[i^swab] != ' ') break;\r |
377 | }\r |
378 | len = i + 1;\r |
379 | \r |
380 | for (i = 0; i < len; i++)\r |
381 | {\r |
382 | unsigned char s = src[i^swab];\r |
383 | if (s >= 0x20 && s < 0x7f && s != '#' && s != '|' &&\r |
384 | s != '[' && s != ']' && s != '\\')\r |
385 | {\r |
386 | *p++ = s;\r |
387 | }\r |
388 | else\r |
389 | {\r |
390 | sprintf(p, "\\%02x", s);\r |
391 | p += 3;\r |
392 | }\r |
393 | }\r |
394 | \r |
395 | return p - dest;\r |
396 | }\r |
397 | \r |
fcdefcf6 |
398 | static char *emu_make_rom_id(const char *fname)\r |
58c86d00 |
399 | {\r |
ca482e5d |
400 | static char id_string[3+0xe*3+0x3*3+0x30*3+3];\r |
bdec53c9 |
401 | int pos, swab = 1;\r |
58c86d00 |
402 | \r |
602133e1 |
403 | if (PicoAHW & PAHW_MCD) {\r |
bdec53c9 |
404 | strcpy(id_string, "CD|");\r |
405 | swab = 0;\r |
406 | }\r |
fcdefcf6 |
407 | else if (PicoAHW & PAHW_SMS)\r |
408 | strcpy(id_string, "MS|");\r |
409 | else strcpy(id_string, "MD|");\r |
58c86d00 |
410 | pos = 3;\r |
411 | \r |
fcdefcf6 |
412 | if (!(PicoAHW & PAHW_SMS)) {\r |
413 | pos += extract_text(id_string + pos, id_header + 0x80, 0x0e, swab); // serial\r |
414 | id_string[pos] = '|'; pos++;\r |
415 | pos += extract_text(id_string + pos, id_header + 0xf0, 0x03, swab); // region\r |
416 | id_string[pos] = '|'; pos++;\r |
417 | pos += extract_text(id_string + pos, id_header + 0x50, 0x30, swab); // overseas name\r |
418 | id_string[pos] = 0;\r |
419 | if (pos > 5)\r |
420 | return id_string;\r |
421 | pos = 3;\r |
422 | }\r |
423 | \r |
424 | // can't find name in ROM, use filename\r |
425 | fname_ext(id_string + 3, sizeof(id_string) - 3, NULL, NULL, fname);\r |
58c86d00 |
426 | \r |
58c86d00 |
427 | return id_string;\r |
428 | }\r |
429 | \r |
ca482e5d |
430 | // buffer must be at least 150 byte long\r |
a47dd663 |
431 | void emu_get_game_name(char *str150)\r |
ca482e5d |
432 | {\r |
433 | int ret, swab = (PicoAHW & PAHW_MCD) ? 0 : 1;\r |
434 | char *s, *d;\r |
435 | \r |
436 | ret = extract_text(str150, id_header + 0x50, 0x30, swab); // overseas name\r |
437 | \r |
438 | for (s = d = str150 + 1; s < str150+ret; s++)\r |
439 | {\r |
440 | if (*s == 0) break;\r |
441 | if (*s != ' ' || d[-1] != ' ')\r |
442 | *d++ = *s;\r |
443 | }\r |
444 | *d = 0;\r |
445 | }\r |
446 | \r |
a47dd663 |
447 | static void shutdown_MCD(void)\r |
448 | {\r |
449 | if ((PicoAHW & PAHW_MCD) && Pico_mcd != NULL)\r |
450 | Stop_CD();\r |
451 | PicoAHW &= ~PAHW_MCD;\r |
452 | }\r |
453 | \r |
974fdb5b |
454 | static void system_announce(void)\r |
455 | {\r |
f3a57b2d |
456 | const char *sys_name, *tv_standard, *extra = "";\r |
974fdb5b |
457 | int fps;\r |
458 | \r |
459 | if (PicoAHW & PAHW_SMS) {\r |
460 | sys_name = "Master System";\r |
f3a57b2d |
461 | #ifdef NO_SMS\r |
462 | extra = " [no support]";\r |
463 | #endif\r |
974fdb5b |
464 | } else if (PicoAHW & PAHW_PICO) {\r |
465 | sys_name = "Pico";\r |
466 | } else if (PicoAHW & PAHW_MCD) {\r |
467 | sys_name = "Mega CD";\r |
468 | if ((Pico.m.hardware & 0xc0) == 0x80)\r |
469 | sys_name = "Sega CD";\r |
470 | } else if (PicoAHW & PAHW_32X) {\r |
471 | sys_name = "32X";\r |
472 | } else {\r |
473 | sys_name = "MegaDrive";\r |
474 | if ((Pico.m.hardware & 0xc0) == 0x80)\r |
475 | sys_name = "Genesis";\r |
476 | }\r |
477 | tv_standard = Pico.m.pal ? "PAL" : "NTSC";\r |
478 | fps = Pico.m.pal ? 50 : 60;\r |
479 | \r |
f3a57b2d |
480 | emu_status_msg("%s %s / %dFPS%s", tv_standard, sys_name, fps, extra);\r |
974fdb5b |
481 | }\r |
482 | \r |
ca482e5d |
483 | // note: this function might mangle rom_fname\r |
3e49ffd0 |
484 | // XXX: portions of this code should move to pico/\r |
a47dd663 |
485 | int emu_reload_rom(char *rom_fname)\r |
ea8c405f |
486 | {\r |
487 | unsigned int rom_size = 0;\r |
ca482e5d |
488 | char *used_rom_name = rom_fname;\r |
eacee137 |
489 | unsigned char *rom_data = NULL;\r |
ea8c405f |
490 | char ext[5];\r |
f9f40f10 |
491 | pm_file *rom = NULL;\r |
3e49ffd0 |
492 | int cd_state = CIT_NOT_CD;\r |
493 | int ret, media_type, cd_region;\r |
494 | int cfg_loaded = 0, bad_rom = 0;\r |
ea8c405f |
495 | \r |
ca482e5d |
496 | lprintf("emu_ReloadRom(%s)\n", rom_fname);\r |
ea8c405f |
497 | \r |
ca482e5d |
498 | get_ext(rom_fname, ext);\r |
ea8c405f |
499 | \r |
77189b7d |
500 | // early cleanup\r |
501 | PicoPatchUnload();\r |
eacee137 |
502 | if (movie_data) {\r |
ea8c405f |
503 | free(movie_data);\r |
504 | movie_data = 0;\r |
505 | }\r |
77189b7d |
506 | \r |
2d2247c2 |
507 | if (!strcmp(ext, ".gmv"))\r |
508 | {\r |
ea8c405f |
509 | // check for both gmv and rom\r |
510 | int dummy;\r |
ca482e5d |
511 | FILE *movie_file = fopen(rom_fname, "rb");\r |
b8a1c09a |
512 | if (!movie_file) {\r |
c6c6c9cd |
513 | me_update_msg("Failed to open movie.");\r |
ea8c405f |
514 | return 0;\r |
515 | }\r |
516 | fseek(movie_file, 0, SEEK_END);\r |
517 | movie_size = ftell(movie_file);\r |
518 | fseek(movie_file, 0, SEEK_SET);\r |
b8a1c09a |
519 | if (movie_size < 64+3) {\r |
c6c6c9cd |
520 | me_update_msg("Invalid GMV file.");\r |
ea8c405f |
521 | fclose(movie_file);\r |
522 | return 0;\r |
523 | }\r |
524 | movie_data = malloc(movie_size);\r |
b8a1c09a |
525 | if (movie_data == NULL) {\r |
c6c6c9cd |
526 | me_update_msg("low memory.");\r |
ea8c405f |
527 | fclose(movie_file);\r |
528 | return 0;\r |
529 | }\r |
b8a1c09a |
530 | dummy = fread(movie_data, 1, movie_size, movie_file);\r |
ea8c405f |
531 | fclose(movie_file);\r |
532 | if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {\r |
c6c6c9cd |
533 | me_update_msg("Invalid GMV file.");\r |
ea8c405f |
534 | return 0;\r |
535 | }\r |
ca482e5d |
536 | dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);\r |
ea8c405f |
537 | if (!dummy) {\r |
c6c6c9cd |
538 | me_update_msg("Could't find a ROM for movie.");\r |
ea8c405f |
539 | return 0;\r |
540 | }\r |
ca482e5d |
541 | get_ext(rom_fname, ext);\r |
f8af9634 |
542 | lprintf("gmv loaded for %s\n", rom_fname);\r |
ea8c405f |
543 | }\r |
ca482e5d |
544 | else if (!strcmp(ext, ".pat"))\r |
545 | {\r |
ea8c405f |
546 | int dummy;\r |
ca482e5d |
547 | PicoPatchLoad(rom_fname);\r |
548 | dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);\r |
ea8c405f |
549 | if (!dummy) {\r |
c6c6c9cd |
550 | me_update_msg("Could't find a ROM to patch.");\r |
ea8c405f |
551 | return 0;\r |
552 | }\r |
ca482e5d |
553 | get_ext(rom_fname, ext);\r |
ea8c405f |
554 | }\r |
555 | \r |
3e49ffd0 |
556 | media_type = detect_media(rom_fname);\r |
557 | if (media_type == PM_BAD) {\r |
558 | me_update_msg("Not a ROM/CD img selected.");\r |
559 | return 0;\r |
560 | }\r |
561 | \r |
a47dd663 |
562 | shutdown_MCD();\r |
5e49c3a8 |
563 | PicoCartUnload();\r |
564 | rom_loaded = 0;\r |
565 | \r |
87b0845f |
566 | PicoAHW = 0;\r |
ea8c405f |
567 | \r |
3e49ffd0 |
568 | if (media_type == PM_CD)\r |
ea8c405f |
569 | {\r |
3e49ffd0 |
570 | // check for MegaCD image\r |
571 | cd_state = emu_cd_check(&cd_region, rom_fname);\r |
572 | if (cd_state >= 0 && cd_state != CIT_NOT_CD)\r |
573 | {\r |
574 | // valid CD image, check for BIOS..\r |
ea8c405f |
575 | \r |
3e49ffd0 |
576 | // we need to have config loaded at this point\r |
fcdefcf6 |
577 | ret = emu_read_config(rom_fname, 0);\r |
578 | if (!ret) emu_read_config(NULL, 0);\r |
3e49ffd0 |
579 | cfg_loaded = 1;\r |
ea8c405f |
580 | \r |
3e49ffd0 |
581 | if (PicoRegionOverride) {\r |
582 | cd_region = PicoRegionOverride;\r |
583 | lprintf("override region to %s\n", cd_region != 4 ?\r |
584 | (cd_region == 8 ? "EU" : "JAP") : "USA");\r |
585 | }\r |
586 | if (!find_bios(cd_region, &used_rom_name))\r |
587 | return 0;\r |
588 | \r |
589 | get_ext(used_rom_name, ext);\r |
590 | PicoAHW |= PAHW_MCD;\r |
ea8c405f |
591 | }\r |
3e49ffd0 |
592 | else {\r |
593 | me_update_msg("Invalid CD image");\r |
ea8c405f |
594 | return 0;\r |
595 | }\r |
ea8c405f |
596 | }\r |
3e49ffd0 |
597 | else if (media_type == PM_MARK3) {\r |
598 | lprintf("detected SMS ROM\n");\r |
599 | PicoAHW = PAHW_SMS;\r |
ea8c405f |
600 | }\r |
601 | \r |
602 | rom = pm_open(used_rom_name);\r |
3e49ffd0 |
603 | if (rom == NULL) {\r |
604 | me_update_msg("Failed to open ROM");\r |
605 | return 0;\r |
ea8c405f |
606 | }\r |
607 | \r |
4b167c12 |
608 | menu_romload_prepare(used_rom_name); // also CD load\r |
ae87bffa |
609 | used_rom_name = NULL; // uses static_buff\r |
ea8c405f |
610 | \r |
3e49ffd0 |
611 | ret = PicoCartLoad(rom, &rom_data, &rom_size, (PicoAHW & PAHW_SMS) ? 1 : 0);\r |
612 | pm_close(rom);\r |
613 | if (ret != 0) {\r |
c6c6c9cd |
614 | if (ret == 2) me_update_msg("Out of memory");\r |
615 | else if (ret == 3) me_update_msg("Read failed");\r |
616 | else me_update_msg("PicoCartLoad() failed.");\r |
3e49ffd0 |
617 | goto fail;\r |
618 | }\r |
619 | \r |
620 | // detect wrong files\r |
621 | if (strncmp((char *)rom_data, "Pico", 4) == 0)\r |
622 | bad_rom = 1;\r |
623 | else if (!(PicoAHW & PAHW_SMS)) {\r |
624 | unsigned short *d = (unsigned short *)(rom_data + 4);\r |
625 | if ((((d[0] << 16) | d[1]) & 0xffffff) >= (int)rom_size) {\r |
626 | lprintf("bad reset vector\n");\r |
627 | bad_rom = 1;\r |
628 | }\r |
ea8c405f |
629 | }\r |
ea8c405f |
630 | \r |
3e49ffd0 |
631 | if (bad_rom) {\r |
632 | me_update_msg("Bad ROM detected.");\r |
633 | goto fail;\r |
ea8c405f |
634 | }\r |
635 | \r |
636 | // load config for this ROM (do this before insert to get correct region)\r |
602133e1 |
637 | if (!(PicoAHW & PAHW_MCD))\r |
1ca2ea4f |
638 | memcpy(id_header, rom_data + 0x100, sizeof(id_header));\r |
ea8c405f |
639 | if (!cfg_loaded) {\r |
fcdefcf6 |
640 | ret = emu_read_config(rom_fname, 0);\r |
641 | if (!ret) emu_read_config(NULL, 0);\r |
ea8c405f |
642 | }\r |
643 | \r |
ae87bffa |
644 | emu_make_path(static_buff, "carthw.cfg", sizeof(static_buff));\r |
645 | if (PicoCartInsert(rom_data, rom_size, static_buff)) {\r |
c6c6c9cd |
646 | me_update_msg("Failed to load ROM.");\r |
3e49ffd0 |
647 | goto fail;\r |
ea8c405f |
648 | }\r |
649 | \r |
ea8c405f |
650 | // insert CD if it was detected\r |
b923ecbe |
651 | if (cd_state != CIT_NOT_CD) {\r |
ca482e5d |
652 | ret = Insert_CD(rom_fname, cd_state);\r |
ea8c405f |
653 | if (ret != 0) {\r |
3e49ffd0 |
654 | PicoCartUnload();\r |
655 | rom_data = NULL; // freed by unload\r |
c6c6c9cd |
656 | me_update_msg("Insert_CD() failed, invalid CD image?");\r |
3e49ffd0 |
657 | goto fail;\r |
ea8c405f |
658 | }\r |
659 | }\r |
660 | \r |
4b167c12 |
661 | menu_romload_end();\r |
662 | \r |
ea8c405f |
663 | if (PicoPatches) {\r |
664 | PicoPatchPrepare();\r |
665 | PicoPatchApply();\r |
666 | }\r |
667 | \r |
668 | // additional movie stuff\r |
f9f40f10 |
669 | if (movie_data)\r |
670 | {\r |
671 | if (movie_data[0x14] == '6')\r |
602133e1 |
672 | PicoOpt |= POPT_6BTN_PAD; // 6 button pad\r |
673 | else PicoOpt &= ~POPT_6BTN_PAD;\r |
2aa27095 |
674 | PicoOpt |= POPT_DIS_VDP_FIFO; // no VDP fifo timing\r |
f9f40f10 |
675 | if (movie_data[0xF] >= 'A') {\r |
676 | if (movie_data[0x16] & 0x80) {\r |
ea8c405f |
677 | PicoRegionOverride = 8;\r |
678 | } else {\r |
679 | PicoRegionOverride = 4;\r |
680 | }\r |
1cb1584b |
681 | PicoReset();\r |
ea8c405f |
682 | // TODO: bits 6 & 5\r |
683 | }\r |
684 | movie_data[0x18+30] = 0;\r |
b24e0f6c |
685 | emu_status_msg("MOVIE: %s", (char *) &movie_data[0x18]);\r |
ea8c405f |
686 | }\r |
687 | else\r |
688 | {\r |
974fdb5b |
689 | system_announce();\r |
602133e1 |
690 | PicoOpt &= ~POPT_DIS_VDP_FIFO;\r |
ea8c405f |
691 | }\r |
ea8c405f |
692 | \r |
bcc9eda0 |
693 | strncpy(rom_fname_loaded, rom_fname, sizeof(rom_fname_loaded)-1);\r |
694 | rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0;\r |
695 | rom_loaded = 1;\r |
696 | \r |
ea8c405f |
697 | // load SRAM for this ROM\r |
a47dd663 |
698 | if (currentConfig.EmuOpt & EOPT_EN_SRAM)\r |
699 | emu_save_load_game(1, 1);\r |
ea8c405f |
700 | \r |
701 | return 1;\r |
f9f40f10 |
702 | \r |
f9f40f10 |
703 | fail:\r |
3e49ffd0 |
704 | if (rom_data)\r |
705 | free(rom_data);\r |
706 | menu_romload_end();\r |
f9f40f10 |
707 | return 0;\r |
ea8c405f |
708 | }\r |
709 | \r |
35e3031a |
710 | int emu_swap_cd(const char *fname)\r |
711 | {\r |
712 | cd_img_type cd_type;\r |
713 | int ret = -1;\r |
714 | \r |
715 | cd_type = emu_cd_check(NULL, fname);\r |
716 | if (cd_type != CIT_NOT_CD)\r |
717 | ret = Insert_CD(fname, cd_type);\r |
718 | if (ret != 0) {\r |
719 | me_update_msg("Load failed, invalid CD image?");\r |
720 | return 0;\r |
721 | }\r |
722 | \r |
723 | strncpy(rom_fname_loaded, fname, sizeof(rom_fname_loaded)-1);\r |
724 | rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0;\r |
725 | return 1;\r |
726 | }\r |
727 | \r |
ae87bffa |
728 | // <base dir><end>\r |
27701801 |
729 | void emu_make_path(char *buff, const char *end, int size)\r |
08fe8094 |
730 | {\r |
27701801 |
731 | int pos, end_len;\r |
732 | \r |
733 | end_len = strlen(end);\r |
734 | pos = plat_get_root_dir(buff, size);\r |
735 | strncpy(buff + pos, end, size - pos);\r |
736 | buff[size - 1] = 0;\r |
737 | if (pos + end_len > size - 1)\r |
738 | lprintf("Warning: path truncated: %s\n", buff);\r |
739 | }\r |
740 | \r |
741 | static void make_config_cfg(char *cfg_buff_512)\r |
742 | {\r |
743 | emu_make_path(cfg_buff_512, PicoConfigFile, 512-6);\r |
08fe8094 |
744 | if (config_slot != 0)\r |
745 | {\r |
27701801 |
746 | char *p = strrchr(cfg_buff_512, '.');\r |
747 | if (p == NULL)\r |
748 | p = cfg_buff_512 + strlen(cfg_buff_512);\r |
08fe8094 |
749 | sprintf(p, ".%i.cfg", config_slot);\r |
750 | }\r |
27701801 |
751 | cfg_buff_512[511] = 0;\r |
08fe8094 |
752 | }\r |
753 | \r |
697746df |
754 | void emu_prep_defconfig(void)\r |
755 | {\r |
756 | memset(&defaultConfig, 0, sizeof(defaultConfig));\r |
fcdefcf6 |
757 | defaultConfig.EmuOpt = 0x9d | EOPT_RAM_TIMINGS|EOPT_EN_CD_LEDS;\r |
697746df |
758 | defaultConfig.s_PicoOpt = POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80 |\r |
759 | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC|POPT_ACC_SPRITES |\r |
760 | POPT_EN_32X|POPT_EN_PWM;\r |
761 | defaultConfig.s_PsndRate = 44100;\r |
762 | defaultConfig.s_PicoRegion = 0; // auto\r |
763 | defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP\r |
764 | defaultConfig.s_PicoCDBuffers = 0;\r |
fcdefcf6 |
765 | defaultConfig.confirm_save = EOPT_CONFIRM_SAVE;\r |
697746df |
766 | defaultConfig.Frameskip = -1; // auto\r |
767 | defaultConfig.volume = 50;\r |
768 | defaultConfig.gamma = 100;\r |
769 | defaultConfig.scaling = 0;\r |
770 | defaultConfig.turbo_rate = 15;\r |
771 | \r |
772 | // platform specific overrides\r |
773 | pemu_prep_defconfig();\r |
774 | }\r |
775 | \r |
d90f5bd7 |
776 | void emu_set_defconfig(void)\r |
ca482e5d |
777 | {\r |
a47dd663 |
778 | memcpy(¤tConfig, &defaultConfig, sizeof(currentConfig));\r |
ca482e5d |
779 | PicoOpt = currentConfig.s_PicoOpt;\r |
780 | PsndRate = currentConfig.s_PsndRate;\r |
781 | PicoRegionOverride = currentConfig.s_PicoRegion;\r |
782 | PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder;\r |
783 | PicoCDBuffers = currentConfig.s_PicoCDBuffers;\r |
fcdefcf6 |
784 | p32x_msh2_multiplier = MSH2_MULTI_DEFAULT;\r |
785 | p32x_ssh2_multiplier = SSH2_MULTI_DEFAULT;\r |
ca482e5d |
786 | }\r |
787 | \r |
fcdefcf6 |
788 | int emu_read_config(const char *rom_fname, int no_defaults)\r |
ea8c405f |
789 | {\r |
58c86d00 |
790 | char cfg[512];\r |
58c86d00 |
791 | int ret;\r |
ea8c405f |
792 | \r |
490eb480 |
793 | if (!no_defaults)\r |
794 | emu_set_defconfig();\r |
795 | \r |
fcdefcf6 |
796 | if (rom_fname == NULL)\r |
ea8c405f |
797 | {\r |
fcdefcf6 |
798 | // global config\r |
08fe8094 |
799 | make_config_cfg(cfg);\r |
58c86d00 |
800 | ret = config_readsect(cfg, NULL);\r |
ea8c405f |
801 | }\r |
58c86d00 |
802 | else\r |
803 | {\r |
fcdefcf6 |
804 | char *sect = emu_make_rom_id(rom_fname);\r |
ea8c405f |
805 | \r |
58c86d00 |
806 | if (config_slot != 0)\r |
807 | sprintf(cfg, "game.%i.cfg", config_slot);\r |
808 | else strcpy(cfg, "game.cfg");\r |
1ca2ea4f |
809 | \r |
810 | ret = -1;\r |
08fe8094 |
811 | if (config_havesect(cfg, sect))\r |
812 | {\r |
813 | // read user's config\r |
7b802576 |
814 | int vol = currentConfig.volume;\r |
d90f5bd7 |
815 | emu_set_defconfig();\r |
1ca2ea4f |
816 | ret = config_readsect(cfg, sect);\r |
7b802576 |
817 | currentConfig.volume = vol; // make vol global (bah)\r |
1ca2ea4f |
818 | }\r |
a39b7867 |
819 | else\r |
08fe8094 |
820 | {\r |
821 | // read global config, and apply game_def.cfg on top\r |
822 | make_config_cfg(cfg);\r |
823 | config_readsect(cfg, NULL);\r |
d8afe7b8 |
824 | emu_make_path(cfg, "game_def.cfg", sizeof(cfg));\r |
825 | ret = config_readsect(cfg, sect);\r |
08fe8094 |
826 | }\r |
58c86d00 |
827 | \r |
ca482e5d |
828 | if (ret == 0)\r |
58c86d00 |
829 | {\r |
1ca2ea4f |
830 | lprintf("loaded cfg from sect \"%s\"\n", sect);\r |
58c86d00 |
831 | }\r |
ea8c405f |
832 | }\r |
58c86d00 |
833 | \r |
697746df |
834 | pemu_validate_config();\r |
ee2a3bdf |
835 | \r |
ea8c405f |
836 | // some sanity checks\r |
2445b7cb |
837 | #ifdef PSP\r |
ee2a3bdf |
838 | /* TODO: mv to plat_validate_config() */\r |
839 | if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;\r |
2445b7cb |
840 | if (currentConfig.gamma < -4 || currentConfig.gamma > 16) currentConfig.gamma = 0;\r |
6fc57144 |
841 | if (currentConfig.gamma2 < 0 || currentConfig.gamma2 > 2) currentConfig.gamma2 = 0;\r |
2445b7cb |
842 | #endif\r |
ee2a3bdf |
843 | if (currentConfig.volume < 0 || currentConfig.volume > 99)\r |
844 | currentConfig.volume = 50;\r |
845 | \r |
846 | if (ret == 0)\r |
847 | config_slot_current = config_slot;\r |
991473ad |
848 | \r |
58c86d00 |
849 | return (ret == 0);\r |
ea8c405f |
850 | }\r |
851 | \r |
852 | \r |
a47dd663 |
853 | int emu_write_config(int is_game)\r |
ea8c405f |
854 | {\r |
58c86d00 |
855 | char cfg[512], *game_sect = NULL;\r |
856 | int ret, write_lrom = 0;\r |
ea8c405f |
857 | \r |
58c86d00 |
858 | if (!is_game)\r |
ea8c405f |
859 | {\r |
ca482e5d |
860 | make_config_cfg(cfg);\r |
58c86d00 |
861 | write_lrom = 1;\r |
ea8c405f |
862 | } else {\r |
863 | if (config_slot != 0)\r |
58c86d00 |
864 | sprintf(cfg, "game.%i.cfg", config_slot);\r |
865 | else strcpy(cfg, "game.cfg");\r |
fcdefcf6 |
866 | game_sect = emu_make_rom_id(rom_fname_loaded);\r |
a47dd663 |
867 | lprintf("emu_write_config: sect \"%s\"\n", game_sect);\r |
ea8c405f |
868 | }\r |
869 | \r |
a47dd663 |
870 | lprintf("emu_write_config: %s ", cfg);\r |
58c86d00 |
871 | ret = config_writesect(cfg, game_sect);\r |
1ca2ea4f |
872 | if (write_lrom) config_writelrom(cfg);\r |
ea8c405f |
873 | #ifndef NO_SYNC\r |
58c86d00 |
874 | sync();\r |
ea8c405f |
875 | #endif\r |
58c86d00 |
876 | lprintf((ret == 0) ? "(ok)\n" : "(failed)\n");\r |
ea8c405f |
877 | \r |
58c86d00 |
878 | if (ret == 0) config_slot_current = config_slot;\r |
879 | return ret == 0;\r |
ea8c405f |
880 | }\r |
881 | \r |
882 | \r |
e2de9939 |
883 | /* always using built-in font */\r |
884 | \r |
cc41eb4f |
885 | #define mk_text_out(name, type, val, topleft, step_x, step_y) \\r |
e2de9939 |
886 | void name(int x, int y, const char *text) \\r |
887 | { \\r |
888 | int i, l, len = strlen(text); \\r |
cc41eb4f |
889 | type *screen = (type *)(topleft) + x * step_x + y * step_y; \\r |
e2de9939 |
890 | \\r |
cc41eb4f |
891 | for (i = 0; i < len; i++, screen += 8 * step_x) \\r |
e2de9939 |
892 | { \\r |
893 | for (l = 0; l < 8; l++) \\r |
894 | { \\r |
895 | unsigned char fd = fontdata8x8[text[i] * 8 + l];\\r |
cc41eb4f |
896 | type *s = screen + l * step_y; \\r |
897 | if (fd&0x80) s[step_x * 0] = val; \\r |
898 | if (fd&0x40) s[step_x * 1] = val; \\r |
899 | if (fd&0x20) s[step_x * 2] = val; \\r |
900 | if (fd&0x10) s[step_x * 3] = val; \\r |
901 | if (fd&0x08) s[step_x * 4] = val; \\r |
902 | if (fd&0x04) s[step_x * 5] = val; \\r |
903 | if (fd&0x02) s[step_x * 6] = val; \\r |
904 | if (fd&0x01) s[step_x * 7] = val; \\r |
e2de9939 |
905 | } \\r |
906 | } \\r |
ea8c405f |
907 | }\r |
908 | \r |
cc41eb4f |
909 | mk_text_out(emu_text_out8, unsigned char, 0xf0, g_screen_ptr, 1, g_screen_width)\r |
910 | mk_text_out(emu_text_out16, unsigned short, 0xffff, g_screen_ptr, 1, g_screen_width)\r |
911 | mk_text_out(emu_text_out8_rot, unsigned char, 0xf0,\r |
912 | (char *)g_screen_ptr + (g_screen_width - 1) * g_screen_height, -g_screen_height, 1)\r |
913 | mk_text_out(emu_text_out16_rot, unsigned short, 0xffff,\r |
914 | (short *)g_screen_ptr + (g_screen_width - 1) * g_screen_height, -g_screen_height, 1)\r |
ea8c405f |
915 | \r |
e2de9939 |
916 | #undef mk_text_out\r |
ea8c405f |
917 | \r |
918 | \r |
d34a42f9 |
919 | void update_movie(void)\r |
ea8c405f |
920 | {\r |
921 | int offs = Pico.m.frame_count*3 + 0x40;\r |
922 | if (offs+3 > movie_size) {\r |
923 | free(movie_data);\r |
924 | movie_data = 0;\r |
b24e0f6c |
925 | emu_status_msg("END OF MOVIE.");\r |
ea8c405f |
926 | lprintf("END OF MOVIE.\n");\r |
ea8c405f |
927 | } else {\r |
928 | // MXYZ SACB RLDU\r |
929 | PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU\r |
f8af9634 |
930 | if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // C\r |
931 | if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // A\r |
932 | if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // B\r |
ea8c405f |
933 | PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU\r |
f8af9634 |
934 | if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // C\r |
935 | if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // A\r |
936 | if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // B\r |
ea8c405f |
937 | PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX\r |
938 | if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X\r |
939 | if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z\r |
940 | PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX\r |
941 | if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X\r |
942 | if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z\r |
943 | }\r |
944 | }\r |
945 | \r |
ea8c405f |
946 | static int try_ropen_file(const char *fname)\r |
947 | {\r |
948 | FILE *f;\r |
949 | \r |
950 | f = fopen(fname, "rb");\r |
951 | if (f) {\r |
952 | fclose(f);\r |
953 | return 1;\r |
954 | }\r |
955 | return 0;\r |
956 | }\r |
957 | \r |
a47dd663 |
958 | char *emu_get_save_fname(int load, int is_sram, int slot)\r |
ea8c405f |
959 | {\r |
ae87bffa |
960 | char *saveFname = static_buff;\r |
ea8c405f |
961 | char ext[16];\r |
962 | \r |
963 | if (is_sram)\r |
964 | {\r |
6bc00695 |
965 | strcpy(ext, (PicoAHW & PAHW_MCD) ? ".brm" : ".srm");\r |
fcdefcf6 |
966 | romfname_ext(saveFname, sizeof(static_buff),\r |
967 | (PicoAHW & PAHW_MCD) ? "brm"PATH_SEP : "srm"PATH_SEP, ext);\r |
6bc00695 |
968 | if (!load)\r |
969 | return saveFname;\r |
970 | \r |
971 | if (try_ropen_file(saveFname))\r |
972 | return saveFname;\r |
973 | \r |
fcdefcf6 |
974 | romfname_ext(saveFname, sizeof(static_buff), NULL, ext);\r |
6bc00695 |
975 | if (try_ropen_file(saveFname))\r |
976 | return saveFname;\r |
ea8c405f |
977 | }\r |
978 | else\r |
979 | {\r |
6bc00695 |
980 | const char *ext_main = (currentConfig.EmuOpt & EOPT_GZIP_SAVES) ? ".mds.gz" : ".mds";\r |
981 | const char *ext_othr = (currentConfig.EmuOpt & EOPT_GZIP_SAVES) ? ".mds" : ".mds.gz";\r |
ea8c405f |
982 | ext[0] = 0;\r |
6bc00695 |
983 | if (slot > 0 && slot < 10)\r |
984 | sprintf(ext, ".%i", slot);\r |
985 | strcat(ext, ext_main);\r |
986 | \r |
987 | if (!load) {\r |
fcdefcf6 |
988 | romfname_ext(saveFname, sizeof(static_buff), "mds" PATH_SEP, ext);\r |
6bc00695 |
989 | return saveFname;\r |
990 | }\r |
991 | else {\r |
fcdefcf6 |
992 | romfname_ext(saveFname, sizeof(static_buff), "mds" PATH_SEP, ext);\r |
6bc00695 |
993 | if (try_ropen_file(saveFname))\r |
994 | return saveFname;\r |
ea8c405f |
995 | \r |
fcdefcf6 |
996 | romfname_ext(saveFname, sizeof(static_buff), NULL, ext);\r |
6bc00695 |
997 | if (try_ropen_file(saveFname))\r |
998 | return saveFname;\r |
999 | \r |
1000 | // try the other ext\r |
1001 | ext[0] = 0;\r |
1002 | if (slot > 0 && slot < 10)\r |
1003 | sprintf(ext, ".%i", slot);\r |
1004 | strcat(ext, ext_othr);\r |
1005 | \r |
fcdefcf6 |
1006 | romfname_ext(saveFname, sizeof(static_buff), "mds"PATH_SEP, ext);\r |
6bc00695 |
1007 | if (try_ropen_file(saveFname))\r |
1008 | return saveFname;\r |
ea8c405f |
1009 | }\r |
1010 | }\r |
1011 | \r |
6bc00695 |
1012 | return NULL;\r |
ea8c405f |
1013 | }\r |
1014 | \r |
cca8800d |
1015 | int emu_check_save_file(int slot, int *time)\r |
ea8c405f |
1016 | {\r |
a47dd663 |
1017 | return emu_get_save_fname(1, 0, slot) ? 1 : 0;\r |
ea8c405f |
1018 | }\r |
1019 | \r |
a47dd663 |
1020 | int emu_save_load_game(int load, int sram)\r |
ea8c405f |
1021 | {\r |
1022 | int ret = 0;\r |
1023 | char *saveFname;\r |
1024 | \r |
1025 | // make save filename\r |
a47dd663 |
1026 | saveFname = emu_get_save_fname(load, sram, state_slot);\r |
ea8c405f |
1027 | if (saveFname == NULL) {\r |
d34a42f9 |
1028 | if (!sram)\r |
b24e0f6c |
1029 | emu_status_msg(load ? "LOAD FAILED (missing file)" : "SAVE FAILED");\r |
ea8c405f |
1030 | return -1;\r |
1031 | }\r |
1032 | \r |
1033 | lprintf("saveLoad (%i, %i): %s\n", load, sram, saveFname);\r |
1034 | \r |
da42200b |
1035 | if (sram)\r |
1036 | {\r |
ea8c405f |
1037 | FILE *sramFile;\r |
1038 | int sram_size;\r |
1039 | unsigned char *sram_data;\r |
1040 | int truncate = 1;\r |
602133e1 |
1041 | if (PicoAHW & PAHW_MCD)\r |
1042 | {\r |
45f2f245 |
1043 | if (PicoOpt & POPT_EN_MCD_RAMCART) {\r |
ea8c405f |
1044 | sram_size = 0x12000;\r |
1045 | sram_data = SRam.data;\r |
1046 | if (sram_data)\r |
1047 | memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4);\r |
1048 | } else {\r |
1049 | sram_size = 0x2000;\r |
1050 | sram_data = Pico_mcd->bram;\r |
1051 | truncate = 0; // the .brm may contain RAM cart data after normal brm\r |
1052 | }\r |
1053 | } else {\r |
45f2f245 |
1054 | sram_size = SRam.size;\r |
ea8c405f |
1055 | sram_data = SRam.data;\r |
1056 | }\r |
b8a1c09a |
1057 | if (sram_data == NULL)\r |
1058 | return 0; // SRam forcefully disabled for this game\r |
ea8c405f |
1059 | \r |
602133e1 |
1060 | if (load)\r |
1061 | {\r |
ea8c405f |
1062 | sramFile = fopen(saveFname, "rb");\r |
b8a1c09a |
1063 | if (!sramFile)\r |
1064 | return -1;\r |
1065 | ret = fread(sram_data, 1, sram_size, sramFile);\r |
1066 | ret = ret > 0 ? 0 : -1;\r |
ea8c405f |
1067 | fclose(sramFile);\r |
602133e1 |
1068 | if ((PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_RAMCART))\r |
ea8c405f |
1069 | memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4);\r |
1070 | } else {\r |
1071 | // sram save needs some special processing\r |
1072 | // see if we have anything to save\r |
1073 | for (; sram_size > 0; sram_size--)\r |
1074 | if (sram_data[sram_size-1]) break;\r |
1075 | \r |
1076 | if (sram_size) {\r |
1077 | sramFile = fopen(saveFname, truncate ? "wb" : "r+b");\r |
1078 | if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry\r |
1079 | if (!sramFile) return -1;\r |
1080 | ret = fwrite(sram_data, 1, sram_size, sramFile);\r |
1081 | ret = (ret != sram_size) ? -1 : 0;\r |
1082 | fclose(sramFile);\r |
1083 | #ifndef NO_SYNC\r |
1084 | sync();\r |
1085 | #endif\r |
1086 | }\r |
1087 | }\r |
1088 | return ret;\r |
1089 | }\r |
1090 | else\r |
1091 | {\r |
bcc9eda0 |
1092 | ret = PicoState(saveFname, !load);\r |
1093 | if (!ret) {\r |
ea8c405f |
1094 | #ifndef NO_SYNC\r |
bcc9eda0 |
1095 | if (!load) sync();\r |
ea8c405f |
1096 | #endif\r |
54646a39 |
1097 | emu_status_msg(load ? "STATE LOADED" : "STATE SAVED");\r |
bcc9eda0 |
1098 | } else {\r |
b24e0f6c |
1099 | emu_status_msg(load ? "LOAD FAILED" : "SAVE FAILED");\r |
ea8c405f |
1100 | ret = -1;\r |
1101 | }\r |
1102 | \r |
ea8c405f |
1103 | return ret;\r |
1104 | }\r |
1105 | }\r |
bdec53c9 |
1106 | \r |
a47dd663 |
1107 | void emu_set_fastforward(int set_on)\r |
c060a9ab |
1108 | {\r |
1109 | static void *set_PsndOut = NULL;\r |
1110 | static int set_Frameskip, set_EmuOpt, is_on = 0;\r |
1111 | \r |
1112 | if (set_on && !is_on) {\r |
1113 | set_PsndOut = PsndOut;\r |
1114 | set_Frameskip = currentConfig.Frameskip;\r |
1115 | set_EmuOpt = currentConfig.EmuOpt;\r |
1116 | PsndOut = NULL;\r |
1117 | currentConfig.Frameskip = 8;\r |
1118 | currentConfig.EmuOpt &= ~4;\r |
1119 | currentConfig.EmuOpt |= 0x40000;\r |
1120 | is_on = 1;\r |
b24e0f6c |
1121 | emu_status_msg("FAST FORWARD");\r |
c060a9ab |
1122 | }\r |
1123 | else if (!set_on && is_on) {\r |
1124 | PsndOut = set_PsndOut;\r |
1125 | currentConfig.Frameskip = set_Frameskip;\r |
1126 | currentConfig.EmuOpt = set_EmuOpt;\r |
1127 | PsndRerate(1);\r |
1128 | is_on = 0;\r |
1129 | }\r |
1130 | }\r |
1131 | \r |
d687ef50 |
1132 | static void emu_tray_open(void)\r |
c060a9ab |
1133 | {\r |
d687ef50 |
1134 | engineState = PGS_TrayMenu;\r |
1135 | }\r |
1136 | \r |
1137 | static void emu_tray_close(void)\r |
1138 | {\r |
1139 | emu_status_msg("CD tray closed.");\r |
f2cf8472 |
1140 | }\r |
1141 | \r |
974fdb5b |
1142 | void emu_32x_startup(void)\r |
1143 | {\r |
c7eb229a |
1144 | plat_video_toggle_renderer(0, 0); // HACK\r |
974fdb5b |
1145 | system_announce();\r |
c7eb229a |
1146 | \r |
1147 | // force mode change event\r |
1148 | rendstatus_old = -1;\r |
974fdb5b |
1149 | }\r |
1150 | \r |
f2cf8472 |
1151 | void emu_reset_game(void)\r |
1152 | {\r |
1153 | PicoReset();\r |
1154 | reset_timing = 1;\r |
1155 | }\r |
1156 | \r |
1157 | void run_events_pico(unsigned int events)\r |
1158 | {\r |
1159 | int lim_x;\r |
1160 | \r |
1161 | if (events & PEV_PICO_SWINP) {\r |
c060a9ab |
1162 | pico_inp_mode++;\r |
d34a42f9 |
1163 | if (pico_inp_mode > 2)\r |
1164 | pico_inp_mode = 0;\r |
c060a9ab |
1165 | switch (pico_inp_mode) {\r |
b24e0f6c |
1166 | case 2: emu_status_msg("Input: Pen on Pad"); break;\r |
1167 | case 1: emu_status_msg("Input: Pen on Storyware"); break;\r |
1168 | case 0: emu_status_msg("Input: Joystick");\r |
c060a9ab |
1169 | PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;\r |
1170 | break;\r |
1171 | }\r |
c060a9ab |
1172 | }\r |
f2cf8472 |
1173 | if (events & PEV_PICO_PPREV) {\r |
c060a9ab |
1174 | PicoPicohw.page--;\r |
d34a42f9 |
1175 | if (PicoPicohw.page < 0)\r |
1176 | PicoPicohw.page = 0;\r |
b24e0f6c |
1177 | emu_status_msg("Page %i", PicoPicohw.page);\r |
c060a9ab |
1178 | }\r |
f2cf8472 |
1179 | if (events & PEV_PICO_PNEXT) {\r |
c060a9ab |
1180 | PicoPicohw.page++;\r |
d34a42f9 |
1181 | if (PicoPicohw.page > 6)\r |
1182 | PicoPicohw.page = 6;\r |
b24e0f6c |
1183 | emu_status_msg("Page %i", PicoPicohw.page);\r |
c060a9ab |
1184 | }\r |
f2cf8472 |
1185 | \r |
1186 | if (pico_inp_mode == 0)\r |
1187 | return;\r |
1188 | \r |
1189 | /* handle other input modes */\r |
1190 | if (PicoPad[0] & 1) pico_pen_y--;\r |
1191 | if (PicoPad[0] & 2) pico_pen_y++;\r |
1192 | if (PicoPad[0] & 4) pico_pen_x--;\r |
1193 | if (PicoPad[0] & 8) pico_pen_x++;\r |
1194 | PicoPad[0] &= ~0x0f; // release UDLR\r |
1195 | \r |
1196 | lim_x = (Pico.video.reg[12]&1) ? 319 : 255;\r |
1197 | if (pico_pen_y < 8)\r |
1198 | pico_pen_y = 8;\r |
1199 | if (pico_pen_y > 224 - PICO_PEN_ADJUST_Y)\r |
1200 | pico_pen_y = 224 - PICO_PEN_ADJUST_Y;\r |
1201 | if (pico_pen_x < 0)\r |
1202 | pico_pen_x = 0;\r |
1203 | if (pico_pen_x > lim_x - PICO_PEN_ADJUST_X)\r |
1204 | pico_pen_x = lim_x - PICO_PEN_ADJUST_X;\r |
1205 | \r |
1206 | PicoPicohw.pen_pos[0] = pico_pen_x;\r |
1207 | if (!(Pico.video.reg[12] & 1))\r |
1208 | PicoPicohw.pen_pos[0] += pico_pen_x / 4;\r |
1209 | PicoPicohw.pen_pos[0] += 0x3c;\r |
1210 | PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);\r |
c060a9ab |
1211 | }\r |
1212 | \r |
d34a42f9 |
1213 | static void do_turbo(int *pad, int acts)\r |
f0f0d2df |
1214 | {\r |
1215 | static int turbo_pad = 0;\r |
1216 | static unsigned char turbo_cnt[3] = { 0, 0, 0 };\r |
1217 | int inc = currentConfig.turbo_rate * 2;\r |
1218 | \r |
1219 | if (acts & 0x1000) {\r |
1220 | turbo_cnt[0] += inc;\r |
1221 | if (turbo_cnt[0] >= 60)\r |
1222 | turbo_pad ^= 0x10, turbo_cnt[0] = 0;\r |
1223 | }\r |
1224 | if (acts & 0x2000) {\r |
1225 | turbo_cnt[1] += inc;\r |
1226 | if (turbo_cnt[1] >= 60)\r |
1227 | turbo_pad ^= 0x20, turbo_cnt[1] = 0;\r |
1228 | }\r |
1229 | if (acts & 0x4000) {\r |
1230 | turbo_cnt[2] += inc;\r |
1231 | if (turbo_cnt[2] >= 60)\r |
1232 | turbo_pad ^= 0x40, turbo_cnt[2] = 0;\r |
1233 | }\r |
1234 | *pad |= turbo_pad & (acts >> 8);\r |
1235 | }\r |
c060a9ab |
1236 | \r |
f2cf8472 |
1237 | static void run_events_ui(unsigned int which)\r |
d34a42f9 |
1238 | {\r |
1239 | if (which & (PEV_STATE_LOAD|PEV_STATE_SAVE))\r |
1240 | {\r |
1241 | int do_it = 1;\r |
cca8800d |
1242 | if ( emu_check_save_file(state_slot, NULL) &&\r |
fcdefcf6 |
1243 | (((which & PEV_STATE_LOAD) && (currentConfig.confirm_save & EOPT_CONFIRM_LOAD)) ||\r |
1244 | ((which & PEV_STATE_SAVE) && (currentConfig.confirm_save & EOPT_CONFIRM_SAVE))) )\r |
d34a42f9 |
1245 | {\r |
1246 | const char *nm;\r |
1247 | char tmp[64];\r |
1248 | int keys, len;\r |
1249 | \r |
1250 | strcpy(tmp, (which & PEV_STATE_LOAD) ? "LOAD STATE?" : "OVERWRITE SAVE?");\r |
1251 | len = strlen(tmp);\r |
1252 | nm = in_get_key_name(-1, -PBTN_MA3);\r |
1253 | snprintf(tmp + len, sizeof(tmp) - len, "(%s=yes, ", nm);\r |
1254 | len = strlen(tmp);\r |
1255 | nm = in_get_key_name(-1, -PBTN_MBACK);\r |
1256 | snprintf(tmp + len, sizeof(tmp) - len, "%s=no)", nm);\r |
1257 | \r |
1258 | plat_status_msg_busy_first(tmp);\r |
1259 | \r |
45285368 |
1260 | in_set_config_int(0, IN_CFG_BLOCKING, 1);\r |
d34a42f9 |
1261 | while (in_menu_wait_any(50) & (PBTN_MA3|PBTN_MBACK))\r |
1262 | ;\r |
1263 | while ( !((keys = in_menu_wait_any(50)) & (PBTN_MA3|PBTN_MBACK)) )\r |
1264 | ;\r |
1265 | if (keys & PBTN_MBACK)\r |
1266 | do_it = 0;\r |
1267 | while (in_menu_wait_any(50) & (PBTN_MA3|PBTN_MBACK))\r |
1268 | ;\r |
45285368 |
1269 | in_set_config_int(0, IN_CFG_BLOCKING, 0);\r |
d34a42f9 |
1270 | }\r |
1271 | if (do_it) {\r |
54646a39 |
1272 | plat_status_msg_busy_first((which & PEV_STATE_LOAD) ? "LOADING STATE" : "SAVING STATE");\r |
d34a42f9 |
1273 | PicoStateProgressCB = plat_status_msg_busy_next;\r |
a47dd663 |
1274 | emu_save_load_game((which & PEV_STATE_LOAD) ? 1 : 0, 0);\r |
d34a42f9 |
1275 | PicoStateProgressCB = NULL;\r |
1276 | }\r |
1277 | }\r |
5a681086 |
1278 | if (which & PEV_SWITCH_RND)\r |
d34a42f9 |
1279 | {\r |
5a681086 |
1280 | plat_video_toggle_renderer(1, 0);\r |
d34a42f9 |
1281 | }\r |
1282 | if (which & (PEV_SSLOT_PREV|PEV_SSLOT_NEXT))\r |
1283 | {\r |
1284 | if (which & PEV_SSLOT_PREV) {\r |
1285 | state_slot -= 1;\r |
1286 | if (state_slot < 0)\r |
1287 | state_slot = 9;\r |
1288 | } else {\r |
1289 | state_slot += 1;\r |
1290 | if (state_slot > 9)\r |
1291 | state_slot = 0;\r |
1292 | }\r |
1293 | \r |
b24e0f6c |
1294 | emu_status_msg("SAVE SLOT %i [%s]", state_slot,\r |
cca8800d |
1295 | emu_check_save_file(state_slot, NULL) ? "USED" : "FREE");\r |
d34a42f9 |
1296 | }\r |
1297 | if (which & PEV_MENU)\r |
1298 | engineState = PGS_Menu;\r |
1299 | }\r |
1300 | \r |
1301 | void emu_update_input(void)\r |
1302 | {\r |
093b8a42 |
1303 | static int prev_events = 0;\r |
1304 | int actions[IN_BINDTYPE_COUNT] = { 0, };\r |
1305 | int pl_actions[2];\r |
1306 | int events;\r |
d34a42f9 |
1307 | \r |
093b8a42 |
1308 | in_update(actions);\r |
d34a42f9 |
1309 | \r |
093b8a42 |
1310 | pl_actions[0] = actions[IN_BINDTYPE_PLAYER12];\r |
1311 | pl_actions[1] = actions[IN_BINDTYPE_PLAYER12] >> 16;\r |
d34a42f9 |
1312 | \r |
093b8a42 |
1313 | PicoPad[0] = pl_actions[0] & 0xfff;\r |
1314 | PicoPad[1] = pl_actions[1] & 0xfff;\r |
d34a42f9 |
1315 | \r |
093b8a42 |
1316 | if (pl_actions[0] & 0x7000)\r |
1317 | do_turbo(&PicoPad[0], pl_actions[0]);\r |
1318 | if (pl_actions[1] & 0x7000)\r |
1319 | do_turbo(&PicoPad[1], pl_actions[1]);\r |
1320 | \r |
1321 | events = actions[IN_BINDTYPE_EMU] & PEV_MASK;\r |
d34a42f9 |
1322 | \r |
1323 | // volume is treated in special way and triggered every frame\r |
1324 | if (events & (PEV_VOL_DOWN|PEV_VOL_UP))\r |
1325 | plat_update_volume(1, events & PEV_VOL_UP);\r |
1326 | \r |
093b8a42 |
1327 | if ((events ^ prev_events) & PEV_FF) {\r |
a47dd663 |
1328 | emu_set_fastforward(events & PEV_FF);\r |
d34a42f9 |
1329 | plat_update_volume(0, 0);\r |
f2cf8472 |
1330 | reset_timing = 1;\r |
d34a42f9 |
1331 | }\r |
1332 | \r |
093b8a42 |
1333 | events &= ~prev_events;\r |
d34a42f9 |
1334 | \r |
f2cf8472 |
1335 | if (PicoAHW == PAHW_PICO)\r |
1336 | run_events_pico(events);\r |
d34a42f9 |
1337 | if (events)\r |
f2cf8472 |
1338 | run_events_ui(events);\r |
d34a42f9 |
1339 | if (movie_data)\r |
1340 | update_movie();\r |
1341 | \r |
093b8a42 |
1342 | prev_events = actions[IN_BINDTYPE_EMU] & PEV_MASK;\r |
d34a42f9 |
1343 | }\r |
1344 | \r |
f2cf8472 |
1345 | static void mkdir_path(char *path_with_reserve, int pos, const char *name)\r |
1346 | {\r |
1347 | strcpy(path_with_reserve + pos, name);\r |
1348 | if (plat_is_dir(path_with_reserve))\r |
1349 | return;\r |
1350 | if (mkdir(path_with_reserve, 0777) < 0)\r |
1351 | lprintf("failed to create: %s\n", path_with_reserve);\r |
1352 | }\r |
1353 | \r |
a4edca53 |
1354 | void emu_cmn_forced_frame(int no_scale, int do_emu)\r |
1355 | {\r |
1356 | int po_old = PicoOpt;\r |
1357 | \r |
1358 | memset32(g_screen_ptr, 0, g_screen_width * g_screen_height * 2 / 4);\r |
1359 | \r |
8340e7c9 |
1360 | PicoOpt &= ~POPT_ALT_RENDERER;\r |
a4edca53 |
1361 | PicoOpt |= POPT_ACC_SPRITES;\r |
1362 | if (!no_scale)\r |
1363 | PicoOpt |= POPT_EN_SOFTSCALE;\r |
1364 | \r |
1365 | PicoDrawSetOutFormat(PDF_RGB555, 1);\r |
1366 | Pico.m.dirtyPal = 1;\r |
1367 | if (do_emu)\r |
1368 | PicoFrame();\r |
1369 | else\r |
1370 | PicoFrameDrawOnly();\r |
1371 | \r |
1372 | PicoOpt = po_old;\r |
1373 | }\r |
1374 | \r |
f2cf8472 |
1375 | void emu_init(void)\r |
1376 | {\r |
27701801 |
1377 | char path[512];\r |
f2cf8472 |
1378 | int pos;\r |
1379 | \r |
83ff19ec |
1380 | #if 0\r |
1381 | // FIXME: handle through menu, etc\r |
1382 | FILE *f;\r |
1383 | f = fopen("32X_M_BIOS.BIN", "rb");\r |
1384 | p32x_bios_m = malloc(2048);\r |
1385 | fread(p32x_bios_m, 1, 2048, f);\r |
1386 | fclose(f);\r |
1387 | f = fopen("32X_S_BIOS.BIN", "rb");\r |
1388 | p32x_bios_s = malloc(1024);\r |
1389 | fread(p32x_bios_s, 1, 1024, f);\r |
1390 | fclose(f);\r |
1391 | #endif\r |
1392 | \r |
f2cf8472 |
1393 | /* make dirs for saves */\r |
27701801 |
1394 | pos = plat_get_root_dir(path, sizeof(path) - 4);\r |
1395 | mkdir_path(path, pos, "mds");\r |
1396 | mkdir_path(path, pos, "srm");\r |
1397 | mkdir_path(path, pos, "brm");\r |
1398 | \r |
f6c49d38 |
1399 | pprof_init();\r |
1400 | \r |
27701801 |
1401 | make_config_cfg(path);\r |
1402 | config_readlrom(path);\r |
f2cf8472 |
1403 | \r |
1404 | PicoInit();\r |
1405 | PicoMessage = plat_status_msg_busy_next;\r |
d687ef50 |
1406 | PicoMCDopenTray = emu_tray_open;\r |
1407 | PicoMCDcloseTray = emu_tray_close;\r |
f2cf8472 |
1408 | }\r |
1409 | \r |
1410 | void emu_finish(void)\r |
1411 | {\r |
1412 | // save SRAM\r |
a47dd663 |
1413 | if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {\r |
1414 | emu_save_load_game(0, 1);\r |
f2cf8472 |
1415 | SRam.changed = 0;\r |
1416 | }\r |
1417 | \r |
27701801 |
1418 | if (!(currentConfig.EmuOpt & EOPT_NO_AUTOSVCFG)) {\r |
1419 | char cfg[512];\r |
1420 | make_config_cfg(cfg);\r |
1421 | config_writelrom(cfg);\r |
1422 | #ifndef NO_SYNC\r |
1423 | sync();\r |
1424 | #endif\r |
1425 | }\r |
f2cf8472 |
1426 | \r |
f6c49d38 |
1427 | pprof_finish();\r |
1428 | \r |
f2cf8472 |
1429 | PicoExit();\r |
1430 | }\r |
1431 | \r |
b24e0f6c |
1432 | static void skip_frame(int do_audio)\r |
1433 | {\r |
1434 | PicoSkipFrame = do_audio ? 1 : 2;\r |
1435 | PicoFrame();\r |
1436 | PicoSkipFrame = 0;\r |
1437 | }\r |
1438 | \r |
1439 | /* our tick here is 1 us right now */\r |
1440 | #define ms_to_ticks(x) (unsigned int)(x * 1000)\r |
1441 | #define get_ticks() plat_get_ticks_us()\r |
1442 | \r |
1443 | void emu_loop(void)\r |
1444 | {\r |
1445 | int pframes_done; /* "period" frames, used for sync */\r |
1446 | int frames_done, frames_shown; /* actual frames for fps counter */\r |
19954be1 |
1447 | int target_fps, target_frametime;\r |
b24e0f6c |
1448 | unsigned int timestamp_base = 0, timestamp_fps;\r |
1449 | char *notice_msg = NULL;\r |
1450 | char fpsbuff[24];\r |
1451 | int i;\r |
1452 | \r |
1453 | fpsbuff[0] = 0;\r |
1454 | \r |
1455 | /* make sure we are in correct mode */\r |
b24e0f6c |
1456 | Pico.m.dirtyPal = 1;\r |
19954be1 |
1457 | rendstatus_old = -1;\r |
b24e0f6c |
1458 | \r |
46bcb899 |
1459 | PicoLoopPrepare();\r |
1460 | \r |
1461 | // prepare CD buffer\r |
1462 | if (PicoAHW & PAHW_MCD)\r |
1463 | PicoCDBufferInit();\r |
1464 | \r |
1465 | pemu_loop_prep();\r |
1466 | \r |
b24e0f6c |
1467 | /* number of ticks per frame */\r |
1468 | if (Pico.m.pal) {\r |
1469 | target_fps = 50;\r |
1470 | target_frametime = ms_to_ticks(1000) / 50;\r |
1471 | } else {\r |
1472 | target_fps = 60;\r |
1473 | target_frametime = ms_to_ticks(1000) / 60 + 1;\r |
1474 | }\r |
1475 | \r |
b24e0f6c |
1476 | timestamp_fps = get_ticks();\r |
1477 | reset_timing = 1;\r |
1478 | \r |
1479 | frames_done = frames_shown = pframes_done = 0;\r |
1480 | \r |
7b436906 |
1481 | plat_video_wait_vsync();\r |
1482 | \r |
b24e0f6c |
1483 | /* loop with resync every 1 sec. */\r |
1484 | while (engineState == PGS_Running)\r |
1485 | {\r |
1486 | unsigned int timestamp;\r |
1487 | int diff, diff_lim;\r |
b24e0f6c |
1488 | \r |
f6c49d38 |
1489 | pprof_start(main);\r |
1490 | \r |
b24e0f6c |
1491 | timestamp = get_ticks();\r |
1492 | if (reset_timing) {\r |
1493 | reset_timing = 0;\r |
1494 | timestamp_base = timestamp;\r |
1495 | pframes_done = 0;\r |
1496 | }\r |
1497 | \r |
1498 | // show notice_msg message?\r |
1499 | if (notice_msg_time != 0)\r |
1500 | {\r |
1501 | static int noticeMsgSum;\r |
54646a39 |
1502 | if (timestamp - ms_to_ticks(notice_msg_time) > ms_to_ticks(STATUS_MSG_TIMEOUT)) {\r |
b24e0f6c |
1503 | notice_msg_time = 0;\r |
1504 | plat_status_msg_clear();\r |
1505 | notice_msg = NULL;\r |
1506 | } else {\r |
1507 | int sum = noticeMsg[0] + noticeMsg[1] + noticeMsg[2];\r |
1508 | if (sum != noticeMsgSum) {\r |
1509 | plat_status_msg_clear();\r |
1510 | noticeMsgSum = sum;\r |
1511 | }\r |
1512 | notice_msg = noticeMsg;\r |
1513 | }\r |
1514 | }\r |
1515 | \r |
b24e0f6c |
1516 | // second changed?\r |
1517 | if (timestamp - timestamp_fps >= ms_to_ticks(1000))\r |
1518 | {\r |
1519 | #ifdef BENCHMARK\r |
1520 | static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r |
1521 | if (++bench == 10) {\r |
1522 | bench = 0;\r |
1523 | bench_fps_s = bench_fps;\r |
1524 | bf[bfp++ & 3] = bench_fps;\r |
1525 | bench_fps = 0;\r |
1526 | }\r |
1527 | bench_fps += frames_shown;\r |
1528 | sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);\r |
07e5dbab |
1529 | printf("%s\n", fpsbuff);\r |
b24e0f6c |
1530 | #else\r |
1531 | if (currentConfig.EmuOpt & EOPT_SHOW_FPS) {\r |
1532 | sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r |
1533 | if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }\r |
1534 | }\r |
1535 | #endif\r |
1536 | frames_shown = frames_done = 0;\r |
1537 | timestamp_fps += ms_to_ticks(1000);\r |
1538 | }\r |
1539 | #ifdef PFRAMES\r |
1540 | sprintf(fpsbuff, "%i", Pico.m.frame_count);\r |
1541 | #endif\r |
1542 | \r |
1543 | if (timestamp - timestamp_base >= ms_to_ticks(1000))\r |
1544 | {\r |
21ecaf23 |
1545 | if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && currentConfig.Frameskip >= 0)\r |
b24e0f6c |
1546 | pframes_done = 0;\r |
5a681086 |
1547 | else\r |
b24e0f6c |
1548 | pframes_done -= target_fps;\r |
5a681086 |
1549 | if (pframes_done < -2) {\r |
1550 | /* don't drag more than 2 frames behind */\r |
1551 | pframes_done = -2;\r |
1552 | timestamp_base = timestamp - 2 * target_frametime;\r |
21ecaf23 |
1553 | }\r |
5a681086 |
1554 | else\r |
1555 | timestamp_base += ms_to_ticks(1000);\r |
b24e0f6c |
1556 | }\r |
1557 | \r |
1558 | diff = timestamp - timestamp_base;\r |
1559 | diff_lim = (pframes_done + 1) * target_frametime;\r |
1560 | \r |
1561 | if (currentConfig.Frameskip >= 0) // frameskip enabled\r |
1562 | {\r |
1563 | for (i = 0; i < currentConfig.Frameskip; i++) {\r |
1564 | emu_update_input();\r |
1565 | skip_frame(1);\r |
1566 | pframes_done++; frames_done++;\r |
1567 | diff_lim += target_frametime;\r |
1568 | \r |
ae87bffa |
1569 | if (!(currentConfig.EmuOpt & (EOPT_NO_FRMLIMIT|EOPT_EXT_FRMLIMIT))) {\r |
b24e0f6c |
1570 | timestamp = get_ticks();\r |
1571 | diff = timestamp - timestamp_base;\r |
0d83abe2 |
1572 | if (!reset_timing && diff < diff_lim) // we are too fast\r |
b24e0f6c |
1573 | plat_wait_till_us(timestamp_base + diff_lim);\r |
1574 | }\r |
1575 | }\r |
1576 | }\r |
1577 | else if (diff > diff_lim)\r |
1578 | {\r |
1579 | /* no time left for this frame - skip */\r |
5a681086 |
1580 | /* limit auto frameskip to 8 */\r |
1581 | if (frames_done / 8 <= frames_shown) {\r |
1582 | emu_update_input();\r |
1583 | skip_frame(diff < diff_lim + target_frametime * 16);\r |
1584 | pframes_done++; frames_done++;\r |
b24e0f6c |
1585 | continue;\r |
1586 | }\r |
b24e0f6c |
1587 | }\r |
1588 | \r |
1589 | emu_update_input();\r |
1590 | PicoFrame();\r |
d08e7326 |
1591 | pemu_finalize_frame(fpsbuff, notice_msg);\r |
1592 | \r |
f6c49d38 |
1593 | // plat_video_flip();\r |
b24e0f6c |
1594 | \r |
1595 | /* frame limiter */\r |
ae87bffa |
1596 | if (!reset_timing && !(currentConfig.EmuOpt & (EOPT_NO_FRMLIMIT|EOPT_EXT_FRMLIMIT)))\r |
b24e0f6c |
1597 | {\r |
1598 | timestamp = get_ticks();\r |
1599 | diff = timestamp - timestamp_base;\r |
1600 | \r |
1601 | // sleep or vsync if we are still too fast\r |
1602 | if (diff < diff_lim)\r |
1603 | {\r |
1604 | // we are too fast\r |
7b436906 |
1605 | plat_wait_till_us(timestamp_base + diff_lim - target_frametime / 4);\r |
1606 | if (currentConfig.EmuOpt & EOPT_VSYNC)\r |
b24e0f6c |
1607 | plat_video_wait_vsync();\r |
b24e0f6c |
1608 | }\r |
1609 | }\r |
1610 | \r |
d08e7326 |
1611 | // XXX: for some plats it might be better to flip before vsync\r |
1612 | // (due to shadow registers in display hw)\r |
1613 | plat_video_flip();\r |
b24e0f6c |
1614 | \r |
1615 | pframes_done++; frames_done++; frames_shown++;\r |
f6c49d38 |
1616 | \r |
1617 | pprof_end(main);\r |
b24e0f6c |
1618 | }\r |
1619 | \r |
1620 | emu_set_fastforward(0);\r |
1621 | \r |
b24e0f6c |
1622 | // save SRAM\r |
1623 | if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {\r |
1624 | plat_status_msg_busy_first("Writing SRAM/BRAM...");\r |
1625 | emu_save_load_game(0, 1);\r |
1626 | SRam.changed = 0;\r |
1627 | }\r |
1628 | \r |
b24e0f6c |
1629 | pemu_loop_end();\r |
730259b7 |
1630 | \r |
1631 | // pemu_loop_end() might want to do 1 frame for bg image,\r |
1632 | // so free CD buffer here\r |
1633 | if (PicoAHW & PAHW_MCD)\r |
1634 | PicoCDBufferFree();\r |
b24e0f6c |
1635 | }\r |
1636 | \r |