1 // (c) Copyright 2006-2009 notaz, All rights reserved.
\r
2 // Free for non-commercial use.
\r
4 // For commercial use, separate licencing terms must be obtained.
\r
16 #include "lprintf.h"
\r
22 #include <pico/pico_int.h>
\r
23 #include <pico/patch.h>
\r
24 #include <pico/cd/cue.h>
\r
27 #define STATUS_MSG_TIMEOUT 2000
\r
31 #if !SCREEN_SIZE_FIXED
\r
32 int g_screen_width = SCREEN_WIDTH;
\r
33 int g_screen_height = SCREEN_HEIGHT;
\r
36 char *PicoConfigFile = "config.cfg";
\r
37 currentConfig_t currentConfig, defaultConfig;
\r
39 int config_slot = 0, config_slot_current = 0;
\r
40 int pico_pen_x = 320/2, pico_pen_y = 240/2;
\r
41 int pico_inp_mode = 0;
\r
42 int engineState = PGS_Menu;
\r
44 /* tmp buff to reduce stack usage for plats with small stack */
\r
45 static char static_buff[512];
\r
46 /* TODO: len checking */
\r
47 char rom_fname_reload[512];
\r
48 char rom_fname_loaded[512];
\r
50 int reset_timing = 0;
\r
51 static unsigned int notice_msg_time; /* when started showing */
\r
52 static char noticeMsg[40];
\r
54 unsigned char *movie_data = NULL;
\r
55 static int movie_size = 0;
\r
58 /* don't use tolower() for easy old glibc binary compatibility */
\r
59 static void strlwr_(char *string)
\r
62 for (p = string; *p; p++)
\r
63 if ('A' <= *p && *p <= 'Z')
\r
67 static int try_rfn_cut(char *fname)
\r
72 p = fname + strlen(fname) - 1;
\r
73 for (; p > fname; p--)
\r
74 if (*p == '.') break;
\r
77 if((tmp = fopen(fname, "rb"))) {
\r
84 static void get_ext(const char *file, char *ext)
\r
88 p = file + strlen(file) - 4;
\r
89 if (p < file) p = file;
\r
95 void emu_status_msg(const char *format, ...)
\r
100 va_start(vl, format);
\r
101 ret = vsnprintf(noticeMsg, sizeof(noticeMsg), format, vl);
\r
104 /* be sure old text gets overwritten */
\r
105 for (; ret < 28; ret++)
\r
106 noticeMsg[ret] = ' ';
\r
107 noticeMsg[ret] = 0;
\r
109 notice_msg_time = plat_get_ticks_ms();
\r
112 static const char * const biosfiles_us[] = { "us_scd1_9210", "us_scd2_9306", "SegaCDBIOS9303" };
\r
113 static const char * const biosfiles_eu[] = { "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303" };
\r
114 static const char * const biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };
\r
116 static int find_bios(int region, char **bios_file)
\r
119 const char * const *files;
\r
122 if (region == 4) { // US
\r
123 files = biosfiles_us;
\r
124 count = sizeof(biosfiles_us) / sizeof(char *);
\r
125 } else if (region == 8) { // EU
\r
126 files = biosfiles_eu;
\r
127 count = sizeof(biosfiles_eu) / sizeof(char *);
\r
128 } else if (region == 1 || region == 2) {
\r
129 files = biosfiles_jp;
\r
130 count = sizeof(biosfiles_jp) / sizeof(char *);
\r
135 for (i = 0; i < count; i++)
\r
137 emu_make_path(static_buff, files[i], sizeof(static_buff) - 4);
\r
138 strcat(static_buff, ".bin");
\r
139 f = fopen(static_buff, "rb");
\r
142 static_buff[strlen(static_buff) - 4] = 0;
\r
143 strcat(static_buff, ".zip");
\r
144 f = fopen(static_buff, "rb");
\r
149 lprintf("using bios: %s\n", static_buff);
\r
152 *bios_file = static_buff;
\r
155 sprintf(static_buff, "no %s BIOS files found, read docs",
\r
156 region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
\r
157 me_update_msg(static_buff);
\r
162 /* check if the name begins with BIOS name */
\r
164 static int emu_isBios(const char *name)
\r
167 for (i = 0; i < sizeof(biosfiles_us)/sizeof(biosfiles_us[0]); i++)
\r
168 if (strstr(name, biosfiles_us[i]) != NULL) return 1;
\r
169 for (i = 0; i < sizeof(biosfiles_eu)/sizeof(biosfiles_eu[0]); i++)
\r
170 if (strstr(name, biosfiles_eu[i]) != NULL) return 1;
\r
171 for (i = 0; i < sizeof(biosfiles_jp)/sizeof(biosfiles_jp[0]); i++)
\r
172 if (strstr(name, biosfiles_jp[i]) != NULL) return 1;
\r
177 static unsigned char id_header[0x100];
\r
179 /* checks if fname points to valid MegaCD image */
\r
180 static int emu_cd_check(int *pregion, const char *fname_in)
\r
182 const char *fname = fname_in;
\r
183 unsigned char buf[32];
\r
185 int region = 4; // 1: Japan, 4: US, 8: Europe
\r
187 cue_track_type type = CT_UNKNOWN;
\r
188 cue_data_t *cue_data = NULL;
\r
190 get_ext(fname_in, ext);
\r
191 if (strcasecmp(ext, ".cue") == 0) {
\r
192 cue_data = cue_parse(fname_in);
\r
193 if (cue_data != NULL) {
\r
194 fname = cue_data->tracks[1].fname;
\r
195 type = cue_data->tracks[1].type;
\r
201 cd_f = pm_open(fname);
\r
202 if (cue_data != NULL)
\r
203 cue_destroy(cue_data);
\r
205 if (cd_f == NULL) return 0; // let the upper level handle this
\r
207 if (pm_read(buf, 32, cd_f) != 32) {
\r
212 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) {
\r
213 if (type && type != CT_ISO)
\r
214 elprintf(EL_STATUS, ".cue has wrong type: %i", type);
\r
215 type = CT_ISO; // Sega CD (ISO)
\r
217 if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) {
\r
218 if (type && type != CT_BIN)
\r
219 elprintf(EL_STATUS, ".cue has wrong type: %i", type);
\r
220 type = CT_BIN; // Sega CD (BIN)
\r
223 if (type == CT_UNKNOWN) {
\r
228 pm_seek(cd_f, (type == CT_ISO) ? 0x100 : 0x110, SEEK_SET);
\r
229 pm_read(id_header, sizeof(id_header), cd_f);
\r
231 /* it seems we have a CD image here. Try to detect region now.. */
\r
232 pm_seek(cd_f, (type == CT_ISO) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);
\r
233 pm_read(buf, 1, cd_f);
\r
236 if (buf[0] == 0x64) region = 8; // EU
\r
237 if (buf[0] == 0xa1) region = 1; // JAP
\r
239 lprintf("detected %s Sega/Mega CD image with %s region\n",
\r
240 type == CT_BIN ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
\r
242 if (pregion != NULL) *pregion = region;
\r
247 static int detect_media(const char *fname)
\r
249 static const short sms_offsets[] = { 0x7ff0, 0x3ff0, 0x1ff0 };
\r
250 static const char *sms_exts[] = { "sms", "gg", "sg" };
\r
251 static const char *md_exts[] = { "gen", "bin", "smd" };
\r
252 char buff0[32], buff[32];
\r
253 unsigned short *d16;
\r
258 get_ext(fname, ext);
\r
260 // detect wrong extensions
\r
261 if (!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) // s.gz ~ .mds.gz
\r
264 /* don't believe in extensions, except .cue */
\r
265 if (strcasecmp(ext, ".cue") == 0)
\r
268 pmf = pm_open(fname);
\r
272 if (pm_read(buff0, 32, pmf) != 32) {
\r
277 if (strncasecmp("SEGADISCSYSTEM", buff0 + 0x00, 14) == 0 ||
\r
278 strncasecmp("SEGADISCSYSTEM", buff0 + 0x10, 14) == 0) {
\r
283 /* check for SMD evil */
\r
284 if (pmf->size >= 0x4200 && (pmf->size & 0x3fff) == 0x200) {
\r
285 if (pm_seek(pmf, sms_offsets[0] + 0x200, SEEK_SET) == sms_offsets[0] + 0x200 &&
\r
286 pm_read(buff, 16, pmf) == 16 &&
\r
287 strncmp("TMR SEGA", buff, 8) == 0)
\r
288 goto looks_like_sms;
\r
290 /* could parse further but don't bother */
\r
291 goto extension_check;
\r
294 /* MD header? Act as TMSS BIOS here */
\r
295 if (pm_seek(pmf, 0x100, SEEK_SET) == 0x100 && pm_read(buff, 16, pmf) == 16) {
\r
296 if (strncmp(buff, "SEGA", 4) == 0 || strncmp(buff, " SEG", 4) == 0)
\r
297 goto looks_like_md;
\r
300 for (i = 0; i < array_size(sms_offsets); i++) {
\r
301 if (pm_seek(pmf, sms_offsets[i], SEEK_SET) != sms_offsets[i])
\r
304 if (pm_read(buff, 16, pmf) != 16)
\r
307 if (strncmp("TMR SEGA", buff, 8) == 0)
\r
308 goto looks_like_sms;
\r
312 /* probably some headerless thing. Maybe check the extension after all. */
\r
313 for (i = 0; i < array_size(md_exts); i++)
\r
314 if (strcasecmp(pmf->ext, md_exts[i]) == 0)
\r
315 goto looks_like_md;
\r
317 for (i = 0; i < array_size(sms_exts); i++)
\r
318 if (strcasecmp(pmf->ext, sms_exts[i]) == 0)
\r
319 goto looks_like_sms;
\r
321 /* If everything else fails, make a guess on the reset vector */
\r
322 d16 = (unsigned short *)(buff0 + 4);
\r
323 if ((((d16[0] << 16) | d16[1]) & 0xffffff) >= pmf->size) {
\r
324 lprintf("bad MD reset vector, assuming SMS\n");
\r
325 goto looks_like_sms;
\r
337 static int extract_text(char *dest, const unsigned char *src, int len, int swab)
\r
342 if (swab) swab = 1;
\r
344 for (i = len - 1; i >= 0; i--)
\r
346 if (src[i^swab] != ' ') break;
\r
350 for (i = 0; i < len; i++)
\r
352 unsigned char s = src[i^swab];
\r
353 if (s >= 0x20 && s < 0x7f && s != '#' && s != '|' &&
\r
354 s != '[' && s != ']' && s != '\\')
\r
360 sprintf(p, "\\%02x", s);
\r
368 static char *emu_make_rom_id(void)
\r
370 static char id_string[3+0xe*3+0x3*3+0x30*3+3];
\r
373 if (PicoAHW & PAHW_MCD) {
\r
374 strcpy(id_string, "CD|");
\r
377 else strcpy(id_string, "MD|");
\r
380 pos += extract_text(id_string + pos, id_header + 0x80, 0x0e, swab); // serial
\r
381 id_string[pos] = '|'; pos++;
\r
382 pos += extract_text(id_string + pos, id_header + 0xf0, 0x03, swab); // region
\r
383 id_string[pos] = '|'; pos++;
\r
384 pos += extract_text(id_string + pos, id_header + 0x50, 0x30, swab); // overseas name
\r
385 id_string[pos] = 0;
\r
390 // buffer must be at least 150 byte long
\r
391 void emu_get_game_name(char *str150)
\r
393 int ret, swab = (PicoAHW & PAHW_MCD) ? 0 : 1;
\r
396 ret = extract_text(str150, id_header + 0x50, 0x30, swab); // overseas name
\r
398 for (s = d = str150 + 1; s < str150+ret; s++)
\r
400 if (*s == 0) break;
\r
401 if (*s != ' ' || d[-1] != ' ')
\r
407 static void shutdown_MCD(void)
\r
409 if ((PicoAHW & PAHW_MCD) && Pico_mcd != NULL)
\r
411 PicoAHW &= ~PAHW_MCD;
\r
414 static void system_announce(void)
\r
416 const char *sys_name, *tv_standard;
\r
419 if (PicoAHW & PAHW_SMS) {
\r
420 sys_name = "Master System";
\r
421 } else if (PicoAHW & PAHW_PICO) {
\r
423 } else if (PicoAHW & PAHW_MCD) {
\r
424 sys_name = "Mega CD";
\r
425 if ((Pico.m.hardware & 0xc0) == 0x80)
\r
426 sys_name = "Sega CD";
\r
427 } else if (PicoAHW & PAHW_32X) {
\r
430 sys_name = "MegaDrive";
\r
431 if ((Pico.m.hardware & 0xc0) == 0x80)
\r
432 sys_name = "Genesis";
\r
434 tv_standard = Pico.m.pal ? "PAL" : "NTSC";
\r
435 fps = Pico.m.pal ? 50 : 60;
\r
437 emu_status_msg("%s %s / %dFPS", tv_standard, sys_name, fps);
\r
440 // note: this function might mangle rom_fname
\r
441 // XXX: portions of this code should move to pico/
\r
442 int emu_reload_rom(char *rom_fname)
\r
444 unsigned int rom_size = 0;
\r
445 char *used_rom_name = rom_fname;
\r
446 unsigned char *rom_data = NULL;
\r
448 pm_file *rom = NULL;
\r
449 int cd_state = CIT_NOT_CD;
\r
450 int ret, media_type, cd_region;
\r
451 int cfg_loaded = 0, bad_rom = 0;
\r
453 lprintf("emu_ReloadRom(%s)\n", rom_fname);
\r
455 get_ext(rom_fname, ext);
\r
457 // check for movie file
\r
462 if (!strcmp(ext, ".gmv"))
\r
464 // check for both gmv and rom
\r
466 FILE *movie_file = fopen(rom_fname, "rb");
\r
468 me_update_msg("Failed to open movie.");
\r
471 fseek(movie_file, 0, SEEK_END);
\r
472 movie_size = ftell(movie_file);
\r
473 fseek(movie_file, 0, SEEK_SET);
\r
474 if(movie_size < 64+3) {
\r
475 me_update_msg("Invalid GMV file.");
\r
476 fclose(movie_file);
\r
479 movie_data = malloc(movie_size);
\r
480 if(movie_data == NULL) {
\r
481 me_update_msg("low memory.");
\r
482 fclose(movie_file);
\r
485 fread(movie_data, 1, movie_size, movie_file);
\r
486 fclose(movie_file);
\r
487 if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {
\r
488 me_update_msg("Invalid GMV file.");
\r
491 dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);
\r
493 me_update_msg("Could't find a ROM for movie.");
\r
496 get_ext(rom_fname, ext);
\r
497 lprintf("gmv loaded for %s\n", rom_fname);
\r
499 else if (!strcmp(ext, ".pat"))
\r
502 PicoPatchLoad(rom_fname);
\r
503 dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);
\r
505 me_update_msg("Could't find a ROM to patch.");
\r
508 get_ext(rom_fname, ext);
\r
511 media_type = detect_media(rom_fname);
\r
512 if (media_type == PM_BAD) {
\r
513 me_update_msg("Not a ROM/CD img selected.");
\r
524 if (media_type == PM_CD)
\r
526 // check for MegaCD image
\r
527 cd_state = emu_cd_check(&cd_region, rom_fname);
\r
528 if (cd_state >= 0 && cd_state != CIT_NOT_CD)
\r
530 // valid CD image, check for BIOS..
\r
532 // we need to have config loaded at this point
\r
533 ret = emu_read_config(1, 0);
\r
534 if (!ret) emu_read_config(0, 0);
\r
537 if (PicoRegionOverride) {
\r
538 cd_region = PicoRegionOverride;
\r
539 lprintf("override region to %s\n", cd_region != 4 ?
\r
540 (cd_region == 8 ? "EU" : "JAP") : "USA");
\r
542 if (!find_bios(cd_region, &used_rom_name))
\r
545 get_ext(used_rom_name, ext);
\r
546 PicoAHW |= PAHW_MCD;
\r
549 me_update_msg("Invalid CD image");
\r
553 else if (media_type == PM_MARK3) {
\r
554 lprintf("detected SMS ROM\n");
\r
555 PicoAHW = PAHW_SMS;
\r
558 rom = pm_open(used_rom_name);
\r
560 me_update_msg("Failed to open ROM");
\r
564 menu_romload_prepare(used_rom_name); // also CD load
\r
565 used_rom_name = NULL; // uses static_buff
\r
567 ret = PicoCartLoad(rom, &rom_data, &rom_size, (PicoAHW & PAHW_SMS) ? 1 : 0);
\r
570 if (ret == 2) me_update_msg("Out of memory");
\r
571 else if (ret == 3) me_update_msg("Read failed");
\r
572 else me_update_msg("PicoCartLoad() failed.");
\r
576 // detect wrong files
\r
577 if (strncmp((char *)rom_data, "Pico", 4) == 0)
\r
579 else if (!(PicoAHW & PAHW_SMS)) {
\r
580 unsigned short *d = (unsigned short *)(rom_data + 4);
\r
581 if ((((d[0] << 16) | d[1]) & 0xffffff) >= (int)rom_size) {
\r
582 lprintf("bad reset vector\n");
\r
588 me_update_msg("Bad ROM detected.");
\r
592 // load config for this ROM (do this before insert to get correct region)
\r
593 if (!(PicoAHW & PAHW_MCD))
\r
594 memcpy(id_header, rom_data + 0x100, sizeof(id_header));
\r
596 ret = emu_read_config(1, 0);
\r
597 if (!ret) emu_read_config(0, 0);
\r
600 emu_make_path(static_buff, "carthw.cfg", sizeof(static_buff));
\r
601 if (PicoCartInsert(rom_data, rom_size, static_buff)) {
\r
602 me_update_msg("Failed to load ROM.");
\r
606 // insert CD if it was detected
\r
607 if (cd_state != CIT_NOT_CD) {
\r
608 ret = Insert_CD(rom_fname, cd_state);
\r
611 rom_data = NULL; // freed by unload
\r
612 me_update_msg("Insert_CD() failed, invalid CD image?");
\r
617 menu_romload_end();
\r
620 PicoPatchPrepare();
\r
624 // additional movie stuff
\r
627 if (movie_data[0x14] == '6')
\r
628 PicoOpt |= POPT_6BTN_PAD; // 6 button pad
\r
629 else PicoOpt &= ~POPT_6BTN_PAD;
\r
630 PicoOpt |= POPT_DIS_VDP_FIFO; // no VDP fifo timing
\r
631 if (movie_data[0xF] >= 'A') {
\r
632 if (movie_data[0x16] & 0x80) {
\r
633 PicoRegionOverride = 8;
\r
635 PicoRegionOverride = 4;
\r
638 // TODO: bits 6 & 5
\r
640 movie_data[0x18+30] = 0;
\r
641 emu_status_msg("MOVIE: %s", (char *) &movie_data[0x18]);
\r
646 PicoOpt &= ~POPT_DIS_VDP_FIFO;
\r
649 strncpy(rom_fname_loaded, rom_fname, sizeof(rom_fname_loaded)-1);
\r
650 rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0;
\r
653 // load SRAM for this ROM
\r
654 if (currentConfig.EmuOpt & EOPT_EN_SRAM)
\r
655 emu_save_load_game(1, 1);
\r
662 menu_romload_end();
\r
666 int emu_swap_cd(const char *fname)
\r
668 cd_img_type cd_type;
\r
671 cd_type = emu_cd_check(NULL, fname);
\r
672 if (cd_type != CIT_NOT_CD)
\r
673 ret = Insert_CD(fname, cd_type);
\r
675 me_update_msg("Load failed, invalid CD image?");
\r
679 strncpy(rom_fname_loaded, fname, sizeof(rom_fname_loaded)-1);
\r
680 rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0;
\r
684 static void romfname_ext(char *dst, const char *prefix, const char *ext)
\r
687 int prefix_len = 0;
\r
689 // make save filename
\r
690 p = rom_fname_loaded + strlen(rom_fname_loaded) - 1;
\r
691 for (; p >= rom_fname_loaded && *p != PATH_SEP_C; p--); p++;
\r
694 int len = plat_get_root_dir(dst, 512);
\r
695 strcpy(dst + len, prefix);
\r
696 prefix_len = len + strlen(prefix);
\r
699 else p = rom_fname_loaded; // backward compatibility
\r
701 strncpy(dst + prefix_len, p, 511-prefix_len);
\r
703 if (dst[strlen(dst)-4] == '.') dst[strlen(dst)-4] = 0;
\r
704 if (ext) strcat(dst, ext);
\r
708 void emu_make_path(char *buff, const char *end, int size)
\r
712 end_len = strlen(end);
\r
713 pos = plat_get_root_dir(buff, size);
\r
714 strncpy(buff + pos, end, size - pos);
\r
715 buff[size - 1] = 0;
\r
716 if (pos + end_len > size - 1)
\r
717 lprintf("Warning: path truncated: %s\n", buff);
\r
720 static void make_config_cfg(char *cfg_buff_512)
\r
722 emu_make_path(cfg_buff_512, PicoConfigFile, 512-6);
\r
723 if (config_slot != 0)
\r
725 char *p = strrchr(cfg_buff_512, '.');
\r
727 p = cfg_buff_512 + strlen(cfg_buff_512);
\r
728 sprintf(p, ".%i.cfg", config_slot);
\r
730 cfg_buff_512[511] = 0;
\r
733 void emu_set_defconfig(void)
\r
735 memcpy(¤tConfig, &defaultConfig, sizeof(currentConfig));
\r
736 PicoOpt = currentConfig.s_PicoOpt;
\r
737 PsndRate = currentConfig.s_PsndRate;
\r
738 PicoRegionOverride = currentConfig.s_PicoRegion;
\r
739 PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder;
\r
740 PicoCDBuffers = currentConfig.s_PicoCDBuffers;
\r
743 int emu_read_config(int game, int no_defaults)
\r
749 emu_set_defconfig();
\r
753 make_config_cfg(cfg);
\r
754 ret = config_readsect(cfg, NULL);
\r
758 char *sect = emu_make_rom_id();
\r
760 // try new .cfg way
\r
761 if (config_slot != 0)
\r
762 sprintf(cfg, "game.%i.cfg", config_slot);
\r
763 else strcpy(cfg, "game.cfg");
\r
766 if (config_havesect(cfg, sect))
\r
768 // read user's config
\r
769 int vol = currentConfig.volume;
\r
770 emu_set_defconfig();
\r
771 ret = config_readsect(cfg, sect);
\r
772 currentConfig.volume = vol; // make vol global (bah)
\r
776 // read global config, and apply game_def.cfg on top
\r
777 make_config_cfg(cfg);
\r
778 config_readsect(cfg, NULL);
\r
779 emu_make_path(cfg, "game_def.cfg", sizeof(cfg));
\r
780 ret = config_readsect(cfg, sect);
\r
785 lprintf("loaded cfg from sect \"%s\"\n", sect);
\r
789 plat_validate_config();
\r
791 // some sanity checks
\r
793 /* TODO: mv to plat_validate_config() */
\r
794 if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;
\r
795 if (currentConfig.gamma < -4 || currentConfig.gamma > 16) currentConfig.gamma = 0;
\r
796 if (currentConfig.gamma2 < 0 || currentConfig.gamma2 > 2) currentConfig.gamma2 = 0;
\r
798 if (currentConfig.volume < 0 || currentConfig.volume > 99)
\r
799 currentConfig.volume = 50;
\r
802 config_slot_current = config_slot;
\r
808 int emu_write_config(int is_game)
\r
810 char cfg[512], *game_sect = NULL;
\r
811 int ret, write_lrom = 0;
\r
815 make_config_cfg(cfg);
\r
818 if (config_slot != 0)
\r
819 sprintf(cfg, "game.%i.cfg", config_slot);
\r
820 else strcpy(cfg, "game.cfg");
\r
821 game_sect = emu_make_rom_id();
\r
822 lprintf("emu_write_config: sect \"%s\"\n", game_sect);
\r
825 lprintf("emu_write_config: %s ", cfg);
\r
826 ret = config_writesect(cfg, game_sect);
\r
827 if (write_lrom) config_writelrom(cfg);
\r
831 lprintf((ret == 0) ? "(ok)\n" : "(failed)\n");
\r
833 if (ret == 0) config_slot_current = config_slot;
\r
838 /* always using built-in font */
\r
840 #define mk_text_out(name, type, val, topleft, step_x, step_y) \
\r
841 void name(int x, int y, const char *text) \
\r
843 int i, l, len = strlen(text); \
\r
844 type *screen = (type *)(topleft) + x * step_x + y * step_y; \
\r
846 for (i = 0; i < len; i++, screen += 8 * step_x) \
\r
848 for (l = 0; l < 8; l++) \
\r
850 unsigned char fd = fontdata8x8[text[i] * 8 + l];\
\r
851 type *s = screen + l * step_y; \
\r
852 if (fd&0x80) s[step_x * 0] = val; \
\r
853 if (fd&0x40) s[step_x * 1] = val; \
\r
854 if (fd&0x20) s[step_x * 2] = val; \
\r
855 if (fd&0x10) s[step_x * 3] = val; \
\r
856 if (fd&0x08) s[step_x * 4] = val; \
\r
857 if (fd&0x04) s[step_x * 5] = val; \
\r
858 if (fd&0x02) s[step_x * 6] = val; \
\r
859 if (fd&0x01) s[step_x * 7] = val; \
\r
864 mk_text_out(emu_text_out8, unsigned char, 0xf0, g_screen_ptr, 1, g_screen_width)
\r
865 mk_text_out(emu_text_out16, unsigned short, 0xffff, g_screen_ptr, 1, g_screen_width)
\r
866 mk_text_out(emu_text_out8_rot, unsigned char, 0xf0,
\r
867 (char *)g_screen_ptr + (g_screen_width - 1) * g_screen_height, -g_screen_height, 1)
\r
868 mk_text_out(emu_text_out16_rot, unsigned short, 0xffff,
\r
869 (short *)g_screen_ptr + (g_screen_width - 1) * g_screen_height, -g_screen_height, 1)
\r
874 void update_movie(void)
\r
876 int offs = Pico.m.frame_count*3 + 0x40;
\r
877 if (offs+3 > movie_size) {
\r
880 emu_status_msg("END OF MOVIE.");
\r
881 lprintf("END OF MOVIE.\n");
\r
884 PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU
\r
885 if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // C
\r
886 if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // A
\r
887 if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // B
\r
888 PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU
\r
889 if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // C
\r
890 if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // A
\r
891 if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // B
\r
892 PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX
\r
893 if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X
\r
894 if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z
\r
895 PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX
\r
896 if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X
\r
897 if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z
\r
901 static int try_ropen_file(const char *fname)
\r
905 f = fopen(fname, "rb");
\r
913 char *emu_get_save_fname(int load, int is_sram, int slot)
\r
915 char *saveFname = static_buff;
\r
920 strcpy(ext, (PicoAHW & PAHW_MCD) ? ".brm" : ".srm");
\r
921 romfname_ext(saveFname, (PicoAHW & PAHW_MCD) ? "brm"PATH_SEP : "srm"PATH_SEP, ext);
\r
925 if (try_ropen_file(saveFname))
\r
928 romfname_ext(saveFname, NULL, ext);
\r
929 if (try_ropen_file(saveFname))
\r
934 const char *ext_main = (currentConfig.EmuOpt & EOPT_GZIP_SAVES) ? ".mds.gz" : ".mds";
\r
935 const char *ext_othr = (currentConfig.EmuOpt & EOPT_GZIP_SAVES) ? ".mds" : ".mds.gz";
\r
937 if (slot > 0 && slot < 10)
\r
938 sprintf(ext, ".%i", slot);
\r
939 strcat(ext, ext_main);
\r
942 romfname_ext(saveFname, "mds" PATH_SEP, ext);
\r
946 romfname_ext(saveFname, "mds" PATH_SEP, ext);
\r
947 if (try_ropen_file(saveFname))
\r
950 romfname_ext(saveFname, NULL, ext);
\r
951 if (try_ropen_file(saveFname))
\r
954 // try the other ext
\r
956 if (slot > 0 && slot < 10)
\r
957 sprintf(ext, ".%i", slot);
\r
958 strcat(ext, ext_othr);
\r
960 romfname_ext(saveFname, "mds"PATH_SEP, ext);
\r
961 if (try_ropen_file(saveFname))
\r
969 int emu_check_save_file(int slot)
\r
971 return emu_get_save_fname(1, 0, slot) ? 1 : 0;
\r
974 int emu_save_load_game(int load, int sram)
\r
979 // make save filename
\r
980 saveFname = emu_get_save_fname(load, sram, state_slot);
\r
981 if (saveFname == NULL) {
\r
983 emu_status_msg(load ? "LOAD FAILED (missing file)" : "SAVE FAILED");
\r
987 lprintf("saveLoad (%i, %i): %s\n", load, sram, saveFname);
\r
993 unsigned char *sram_data;
\r
995 if (PicoAHW & PAHW_MCD)
\r
997 if (PicoOpt & POPT_EN_MCD_RAMCART) {
\r
998 sram_size = 0x12000;
\r
999 sram_data = SRam.data;
\r
1001 memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4);
\r
1003 sram_size = 0x2000;
\r
1004 sram_data = Pico_mcd->bram;
\r
1005 truncate = 0; // the .brm may contain RAM cart data after normal brm
\r
1008 sram_size = SRam.size;
\r
1009 sram_data = SRam.data;
\r
1011 if (!sram_data) return 0; // SRam forcefully disabled for this game
\r
1015 sramFile = fopen(saveFname, "rb");
\r
1016 if(!sramFile) return -1;
\r
1017 fread(sram_data, 1, sram_size, sramFile);
\r
1019 if ((PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_RAMCART))
\r
1020 memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4);
\r
1022 // sram save needs some special processing
\r
1023 // see if we have anything to save
\r
1024 for (; sram_size > 0; sram_size--)
\r
1025 if (sram_data[sram_size-1]) break;
\r
1028 sramFile = fopen(saveFname, truncate ? "wb" : "r+b");
\r
1029 if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry
\r
1030 if (!sramFile) return -1;
\r
1031 ret = fwrite(sram_data, 1, sram_size, sramFile);
\r
1032 ret = (ret != sram_size) ? -1 : 0;
\r
1043 ret = PicoState(saveFname, !load);
\r
1046 if (!load) sync();
\r
1048 emu_status_msg(load ? "STATE LOADED" : "STATE SAVED");
\r
1050 emu_status_msg(load ? "LOAD FAILED" : "SAVE FAILED");
\r
1058 void emu_set_fastforward(int set_on)
\r
1060 static void *set_PsndOut = NULL;
\r
1061 static int set_Frameskip, set_EmuOpt, is_on = 0;
\r
1063 if (set_on && !is_on) {
\r
1064 set_PsndOut = PsndOut;
\r
1065 set_Frameskip = currentConfig.Frameskip;
\r
1066 set_EmuOpt = currentConfig.EmuOpt;
\r
1068 currentConfig.Frameskip = 8;
\r
1069 currentConfig.EmuOpt &= ~4;
\r
1070 currentConfig.EmuOpt |= 0x40000;
\r
1072 emu_status_msg("FAST FORWARD");
\r
1074 else if (!set_on && is_on) {
\r
1075 PsndOut = set_PsndOut;
\r
1076 currentConfig.Frameskip = set_Frameskip;
\r
1077 currentConfig.EmuOpt = set_EmuOpt;
\r
1083 static void emu_tray_open(void)
\r
1085 engineState = PGS_TrayMenu;
\r
1088 static void emu_tray_close(void)
\r
1090 emu_status_msg("CD tray closed.");
\r
1093 void emu_32x_startup(void)
\r
1095 plat_video_toggle_renderer(0, 1, 0);
\r
1096 system_announce();
\r
1099 void emu_reset_game(void)
\r
1105 void run_events_pico(unsigned int events)
\r
1109 if (events & PEV_PICO_SWINP) {
\r
1111 if (pico_inp_mode > 2)
\r
1112 pico_inp_mode = 0;
\r
1113 switch (pico_inp_mode) {
\r
1114 case 2: emu_status_msg("Input: Pen on Pad"); break;
\r
1115 case 1: emu_status_msg("Input: Pen on Storyware"); break;
\r
1116 case 0: emu_status_msg("Input: Joystick");
\r
1117 PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;
\r
1121 if (events & PEV_PICO_PPREV) {
\r
1122 PicoPicohw.page--;
\r
1123 if (PicoPicohw.page < 0)
\r
1124 PicoPicohw.page = 0;
\r
1125 emu_status_msg("Page %i", PicoPicohw.page);
\r
1127 if (events & PEV_PICO_PNEXT) {
\r
1128 PicoPicohw.page++;
\r
1129 if (PicoPicohw.page > 6)
\r
1130 PicoPicohw.page = 6;
\r
1131 emu_status_msg("Page %i", PicoPicohw.page);
\r
1134 if (pico_inp_mode == 0)
\r
1137 /* handle other input modes */
\r
1138 if (PicoPad[0] & 1) pico_pen_y--;
\r
1139 if (PicoPad[0] & 2) pico_pen_y++;
\r
1140 if (PicoPad[0] & 4) pico_pen_x--;
\r
1141 if (PicoPad[0] & 8) pico_pen_x++;
\r
1142 PicoPad[0] &= ~0x0f; // release UDLR
\r
1144 lim_x = (Pico.video.reg[12]&1) ? 319 : 255;
\r
1145 if (pico_pen_y < 8)
\r
1147 if (pico_pen_y > 224 - PICO_PEN_ADJUST_Y)
\r
1148 pico_pen_y = 224 - PICO_PEN_ADJUST_Y;
\r
1149 if (pico_pen_x < 0)
\r
1151 if (pico_pen_x > lim_x - PICO_PEN_ADJUST_X)
\r
1152 pico_pen_x = lim_x - PICO_PEN_ADJUST_X;
\r
1154 PicoPicohw.pen_pos[0] = pico_pen_x;
\r
1155 if (!(Pico.video.reg[12] & 1))
\r
1156 PicoPicohw.pen_pos[0] += pico_pen_x / 4;
\r
1157 PicoPicohw.pen_pos[0] += 0x3c;
\r
1158 PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);
\r
1161 static void do_turbo(int *pad, int acts)
\r
1163 static int turbo_pad = 0;
\r
1164 static unsigned char turbo_cnt[3] = { 0, 0, 0 };
\r
1165 int inc = currentConfig.turbo_rate * 2;
\r
1167 if (acts & 0x1000) {
\r
1168 turbo_cnt[0] += inc;
\r
1169 if (turbo_cnt[0] >= 60)
\r
1170 turbo_pad ^= 0x10, turbo_cnt[0] = 0;
\r
1172 if (acts & 0x2000) {
\r
1173 turbo_cnt[1] += inc;
\r
1174 if (turbo_cnt[1] >= 60)
\r
1175 turbo_pad ^= 0x20, turbo_cnt[1] = 0;
\r
1177 if (acts & 0x4000) {
\r
1178 turbo_cnt[2] += inc;
\r
1179 if (turbo_cnt[2] >= 60)
\r
1180 turbo_pad ^= 0x40, turbo_cnt[2] = 0;
\r
1182 *pad |= turbo_pad & (acts >> 8);
\r
1185 static void run_events_ui(unsigned int which)
\r
1187 if (which & (PEV_STATE_LOAD|PEV_STATE_SAVE))
\r
1190 if ( emu_check_save_file(state_slot) &&
\r
1191 (((which & PEV_STATE_LOAD) && (currentConfig.EmuOpt & EOPT_CONFIRM_LOAD)) ||
\r
1192 ((which & PEV_STATE_SAVE) && (currentConfig.EmuOpt & EOPT_CONFIRM_SAVE))) )
\r
1198 strcpy(tmp, (which & PEV_STATE_LOAD) ? "LOAD STATE?" : "OVERWRITE SAVE?");
\r
1199 len = strlen(tmp);
\r
1200 nm = in_get_key_name(-1, -PBTN_MA3);
\r
1201 snprintf(tmp + len, sizeof(tmp) - len, "(%s=yes, ", nm);
\r
1202 len = strlen(tmp);
\r
1203 nm = in_get_key_name(-1, -PBTN_MBACK);
\r
1204 snprintf(tmp + len, sizeof(tmp) - len, "%s=no)", nm);
\r
1206 plat_status_msg_busy_first(tmp);
\r
1208 in_set_blocking(1);
\r
1209 while (in_menu_wait_any(50) & (PBTN_MA3|PBTN_MBACK))
\r
1211 while ( !((keys = in_menu_wait_any(50)) & (PBTN_MA3|PBTN_MBACK)) )
\r
1213 if (keys & PBTN_MBACK)
\r
1215 while (in_menu_wait_any(50) & (PBTN_MA3|PBTN_MBACK))
\r
1217 in_set_blocking(0);
\r
1220 plat_status_msg_busy_first((which & PEV_STATE_LOAD) ? "LOADING STATE" : "SAVING STATE");
\r
1221 PicoStateProgressCB = plat_status_msg_busy_next;
\r
1222 emu_save_load_game((which & PEV_STATE_LOAD) ? 1 : 0, 0);
\r
1223 PicoStateProgressCB = NULL;
\r
1226 if ((which & PEV_SWITCH_RND) && !(PicoAHW & PAHW_32X))
\r
1228 plat_video_toggle_renderer(1, 0, 0);
\r
1230 if (which & (PEV_SSLOT_PREV|PEV_SSLOT_NEXT))
\r
1232 if (which & PEV_SSLOT_PREV) {
\r
1234 if (state_slot < 0)
\r
1238 if (state_slot > 9)
\r
1242 emu_status_msg("SAVE SLOT %i [%s]", state_slot,
\r
1243 emu_check_save_file(state_slot) ? "USED" : "FREE");
\r
1245 if (which & PEV_MENU)
\r
1246 engineState = PGS_Menu;
\r
1249 void emu_update_input(void)
\r
1251 static int prev_events = 0;
\r
1252 int actions[IN_BINDTYPE_COUNT] = { 0, };
\r
1253 int pl_actions[2];
\r
1256 in_update(actions);
\r
1258 pl_actions[0] = actions[IN_BINDTYPE_PLAYER12];
\r
1259 pl_actions[1] = actions[IN_BINDTYPE_PLAYER12] >> 16;
\r
1261 PicoPad[0] = pl_actions[0] & 0xfff;
\r
1262 PicoPad[1] = pl_actions[1] & 0xfff;
\r
1264 if (pl_actions[0] & 0x7000)
\r
1265 do_turbo(&PicoPad[0], pl_actions[0]);
\r
1266 if (pl_actions[1] & 0x7000)
\r
1267 do_turbo(&PicoPad[1], pl_actions[1]);
\r
1269 events = actions[IN_BINDTYPE_EMU] & PEV_MASK;
\r
1271 // volume is treated in special way and triggered every frame
\r
1272 if (events & (PEV_VOL_DOWN|PEV_VOL_UP))
\r
1273 plat_update_volume(1, events & PEV_VOL_UP);
\r
1275 if ((events ^ prev_events) & PEV_FF) {
\r
1276 emu_set_fastforward(events & PEV_FF);
\r
1277 plat_update_volume(0, 0);
\r
1281 events &= ~prev_events;
\r
1283 if (PicoAHW == PAHW_PICO)
\r
1284 run_events_pico(events);
\r
1286 run_events_ui(events);
\r
1290 prev_events = actions[IN_BINDTYPE_EMU] & PEV_MASK;
\r
1293 static void mkdir_path(char *path_with_reserve, int pos, const char *name)
\r
1295 strcpy(path_with_reserve + pos, name);
\r
1296 if (plat_is_dir(path_with_reserve))
\r
1298 if (mkdir(path_with_reserve, 0777) < 0)
\r
1299 lprintf("failed to create: %s\n", path_with_reserve);
\r
1302 void emu_init(void)
\r
1308 // FIXME: handle through menu, etc
\r
1310 f = fopen("32X_M_BIOS.BIN", "rb");
\r
1311 p32x_bios_m = malloc(2048);
\r
1312 fread(p32x_bios_m, 1, 2048, f);
\r
1314 f = fopen("32X_S_BIOS.BIN", "rb");
\r
1315 p32x_bios_s = malloc(1024);
\r
1316 fread(p32x_bios_s, 1, 1024, f);
\r
1320 /* make dirs for saves */
\r
1321 pos = plat_get_root_dir(path, sizeof(path) - 4);
\r
1322 mkdir_path(path, pos, "mds");
\r
1323 mkdir_path(path, pos, "srm");
\r
1324 mkdir_path(path, pos, "brm");
\r
1326 make_config_cfg(path);
\r
1327 config_readlrom(path);
\r
1330 PicoMessage = plat_status_msg_busy_next;
\r
1331 PicoMCDopenTray = emu_tray_open;
\r
1332 PicoMCDcloseTray = emu_tray_close;
\r
1335 void emu_finish(void)
\r
1338 if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {
\r
1339 emu_save_load_game(0, 1);
\r
1343 if (!(currentConfig.EmuOpt & EOPT_NO_AUTOSVCFG)) {
\r
1345 make_config_cfg(cfg);
\r
1346 config_writelrom(cfg);
\r
1355 static void skip_frame(int do_audio)
\r
1357 PicoSkipFrame = do_audio ? 1 : 2;
\r
1359 PicoSkipFrame = 0;
\r
1362 /* our tick here is 1 us right now */
\r
1363 #define ms_to_ticks(x) (unsigned int)(x * 1000)
\r
1364 #define get_ticks() plat_get_ticks_us()
\r
1366 void emu_loop(void)
\r
1368 int pframes_done; /* "period" frames, used for sync */
\r
1369 int frames_done, frames_shown; /* actual frames for fps counter */
\r
1370 int target_fps, target_frametime;
\r
1371 unsigned int timestamp_base = 0, timestamp_fps;
\r
1372 char *notice_msg = NULL;
\r
1378 /* make sure we are in correct mode */
\r
1379 Pico.m.dirtyPal = 1;
\r
1380 rendstatus_old = -1;
\r
1382 /* number of ticks per frame */
\r
1385 target_frametime = ms_to_ticks(1000) / 50;
\r
1388 target_frametime = ms_to_ticks(1000) / 60 + 1;
\r
1391 // prepare CD buffer
\r
1392 if (PicoAHW & PAHW_MCD)
\r
1393 PicoCDBufferInit();
\r
1394 PicoLoopPrepare();
\r
1398 timestamp_fps = get_ticks();
\r
1401 frames_done = frames_shown = pframes_done = 0;
\r
1403 plat_video_wait_vsync();
\r
1405 /* loop with resync every 1 sec. */
\r
1406 while (engineState == PGS_Running)
\r
1408 unsigned int timestamp;
\r
1409 int diff, diff_lim;
\r
1411 timestamp = get_ticks();
\r
1412 if (reset_timing) {
\r
1414 timestamp_base = timestamp;
\r
1418 // show notice_msg message?
\r
1419 if (notice_msg_time != 0)
\r
1421 static int noticeMsgSum;
\r
1422 if (timestamp - ms_to_ticks(notice_msg_time) > ms_to_ticks(STATUS_MSG_TIMEOUT)) {
\r
1423 notice_msg_time = 0;
\r
1424 plat_status_msg_clear();
\r
1425 notice_msg = NULL;
\r
1427 int sum = noticeMsg[0] + noticeMsg[1] + noticeMsg[2];
\r
1428 if (sum != noticeMsgSum) {
\r
1429 plat_status_msg_clear();
\r
1430 noticeMsgSum = sum;
\r
1432 notice_msg = noticeMsg;
\r
1436 // second changed?
\r
1437 if (timestamp - timestamp_fps >= ms_to_ticks(1000))
\r
1440 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
\r
1441 if (++bench == 10) {
\r
1443 bench_fps_s = bench_fps;
\r
1444 bf[bfp++ & 3] = bench_fps;
\r
1447 bench_fps += frames_shown;
\r
1448 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
\r
1450 if (currentConfig.EmuOpt & EOPT_SHOW_FPS) {
\r
1451 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
\r
1452 if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }
\r
1455 frames_shown = frames_done = 0;
\r
1456 timestamp_fps += ms_to_ticks(1000);
\r
1459 sprintf(fpsbuff, "%i", Pico.m.frame_count);
\r
1462 if (timestamp - timestamp_base >= ms_to_ticks(1000))
\r
1464 if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && currentConfig.Frameskip >= 0)
\r
1467 pframes_done -= target_fps;
\r
1468 /* don't allow it to drift during heavy slowdowns */
\r
1469 if (pframes_done < -5) {
\r
1473 if (pframes_done < -2)
\r
1474 pframes_done = -2;
\r
1476 timestamp_base += ms_to_ticks(1000);
\r
1479 diff = timestamp - timestamp_base;
\r
1480 diff_lim = (pframes_done + 1) * target_frametime;
\r
1482 if (currentConfig.Frameskip >= 0) // frameskip enabled
\r
1484 for (i = 0; i < currentConfig.Frameskip; i++) {
\r
1485 emu_update_input();
\r
1487 pframes_done++; frames_done++;
\r
1488 diff_lim += target_frametime;
\r
1490 if (!(currentConfig.EmuOpt & (EOPT_NO_FRMLIMIT|EOPT_EXT_FRMLIMIT))) {
\r
1491 timestamp = get_ticks();
\r
1492 diff = timestamp - timestamp_base;
\r
1493 if (!reset_timing && diff < diff_lim) // we are too fast
\r
1494 plat_wait_till_us(timestamp_base + diff_lim);
\r
1498 else if (diff > diff_lim)
\r
1500 /* no time left for this frame - skip */
\r
1501 if (diff - diff_lim >= ms_to_ticks(200)) {
\r
1502 /* if too much behind, reset instead */
\r
1506 emu_update_input();
\r
1507 skip_frame(diff < diff_lim + target_frametime * 2);
\r
1508 pframes_done++; frames_done++;
\r
1512 emu_update_input();
\r
1515 /* frame limiter */
\r
1516 if (!reset_timing && !(currentConfig.EmuOpt & (EOPT_NO_FRMLIMIT|EOPT_EXT_FRMLIMIT)))
\r
1518 timestamp = get_ticks();
\r
1519 diff = timestamp - timestamp_base;
\r
1521 // sleep or vsync if we are still too fast
\r
1522 if (diff < diff_lim)
\r
1524 // we are too fast
\r
1525 plat_wait_till_us(timestamp_base + diff_lim - target_frametime / 4);
\r
1526 if (currentConfig.EmuOpt & EOPT_VSYNC)
\r
1527 plat_video_wait_vsync();
\r
1531 pemu_update_display(fpsbuff, notice_msg);
\r
1533 pframes_done++; frames_done++; frames_shown++;
\r
1536 emu_set_fastforward(0);
\r
1539 if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {
\r
1540 plat_status_msg_busy_first("Writing SRAM/BRAM...");
\r
1541 emu_save_load_game(0, 1);
\r
1547 // pemu_loop_end() might want to do 1 frame for bg image,
\r
1548 // so free CD buffer here
\r
1549 if (PicoAHW & PAHW_MCD)
\r
1550 PicoCDBufferFree();
\r