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