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