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