3 * (C) notaz, 2007-2010
\r
5 * This work is licensed under the terms of MAME license.
\r
6 * See COPYING file in the top-level directory.
\r
17 #include "../libpicofe/posix.h"
\r
18 #include "../libpicofe/input.h"
\r
19 #include "../libpicofe/fonts.h"
\r
20 #include "../libpicofe/sndout.h"
\r
21 #include "../libpicofe/lprintf.h"
\r
22 #include "../libpicofe/plat.h"
\r
24 #include "input_pico.h"
\r
25 #include "menu_pico.h"
\r
26 #include "config_file.h"
\r
28 #include <pico/pico_int.h>
\r
29 #include <pico/patch.h>
\r
32 #define PATH_SEP "/"
\r
33 #define PATH_SEP_C '/'
\r
35 #define PATH_SEP "\\"
\r
36 #define PATH_SEP_C '\\'
\r
39 #define STATUS_MSG_TIMEOUT 2000
\r
43 int g_screen_width = 320;
\r
44 int g_screen_height = 240;
\r
46 char *PicoConfigFile = "config.cfg";
\r
47 currentConfig_t currentConfig, defaultConfig;
\r
49 int config_slot = 0, config_slot_current = 0;
\r
50 int pico_pen_x = 320/2, pico_pen_y = 240/2;
\r
52 int flip_after_sync;
\r
53 int engineState = PGS_Menu;
\r
55 static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
\r
57 /* tmp buff to reduce stack usage for plats with small stack */
\r
58 static char static_buff[512];
\r
59 const char *rom_fname_reload;
\r
60 char rom_fname_loaded[512];
\r
61 int reset_timing = 0;
\r
62 static unsigned int notice_msg_time; /* when started showing */
\r
63 static char noticeMsg[40];
\r
65 unsigned char *movie_data = NULL;
\r
66 static int movie_size = 0;
\r
69 /* don't use tolower() for easy old glibc binary compatibility */
\r
70 static void strlwr_(char *string)
\r
73 for (p = string; *p; p++)
\r
74 if ('A' <= *p && *p <= 'Z')
\r
78 static int try_rfn_cut(char *fname)
\r
83 p = fname + strlen(fname) - 1;
\r
84 for (; p > fname; p--)
\r
85 if (*p == '.') break;
\r
88 if((tmp = fopen(fname, "rb"))) {
\r
95 static void get_ext(const char *file, char *ext)
\r
99 p = file + strlen(file) - 4;
\r
100 if (p < file) p = file;
\r
101 strncpy(ext, p, 4);
\r
106 static void fname_ext(char *dst, int dstlen, const char *prefix, const char *ext, const char *fname)
\r
108 int prefix_len = 0;
\r
113 int len = plat_get_root_dir(dst, dstlen);
\r
114 strcpy(dst + len, prefix);
\r
115 prefix_len = len + strlen(prefix);
\r
118 p = fname + strlen(fname) - 1;
\r
119 for (; p >= fname && *p != PATH_SEP_C; p--)
\r
122 strncpy(dst + prefix_len, p, dstlen - prefix_len - 1);
\r
124 dst[dstlen - 8] = 0;
\r
125 if (dst[strlen(dst) - 4] == '.')
\r
126 dst[strlen(dst) - 4] = 0;
\r
131 static void romfname_ext(char *dst, int dstlen, const char *prefix, const char *ext)
\r
133 fname_ext(dst, dstlen, prefix, ext, rom_fname_loaded);
\r
136 void emu_status_msg(const char *format, ...)
\r
141 va_start(vl, format);
\r
142 ret = vsnprintf(noticeMsg, sizeof(noticeMsg), format, vl);
\r
145 /* be sure old text gets overwritten */
\r
146 for (; ret < 28; ret++)
\r
147 noticeMsg[ret] = ' ';
\r
148 noticeMsg[ret] = 0;
\r
150 notice_msg_time = plat_get_ticks_ms();
\r
153 static const char * const biosfiles_us[] = {
\r
154 "us_scd1_9210", "us_scd2_9306", "SegaCDBIOS9303", "bios_CD_U"
\r
156 static const char * const biosfiles_eu[] = {
\r
157 "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303", "bios_CD_E"
\r
159 static const char * const biosfiles_jp[] = {
\r
160 "jp_mcd1_9112", "jp_mcd1_9111", "bios_CD_J"
\r
163 static const char *find_bios(int *region, const char *cd_fname)
\r
166 const char * const *files;
\r
170 // we need to have config loaded at this point
\r
171 ret = emu_read_config(cd_fname, 0);
\r
172 if (!ret) emu_read_config(NULL, 0);
\r
174 if (PicoRegionOverride) {
\r
175 *region = PicoRegionOverride;
\r
176 lprintf("override region to %s\n", *region != 4 ?
\r
177 (*region == 8 ? "EU" : "JAP") : "USA");
\r
180 if (*region == 4) { // US
\r
181 files = biosfiles_us;
\r
182 count = sizeof(biosfiles_us) / sizeof(char *);
\r
183 } else if (*region == 8) { // EU
\r
184 files = biosfiles_eu;
\r
185 count = sizeof(biosfiles_eu) / sizeof(char *);
\r
186 } else if (*region == 1 || *region == 2) {
\r
187 files = biosfiles_jp;
\r
188 count = sizeof(biosfiles_jp) / sizeof(char *);
\r
193 for (i = 0; i < count; i++)
\r
195 emu_make_path(static_buff, files[i], sizeof(static_buff) - 4);
\r
196 strcat(static_buff, ".bin");
\r
197 f = fopen(static_buff, "rb");
\r
200 static_buff[strlen(static_buff) - 4] = 0;
\r
201 strcat(static_buff, ".zip");
\r
202 f = fopen(static_buff, "rb");
\r
207 lprintf("using bios: %s\n", static_buff);
\r
209 return static_buff;
\r
211 sprintf(static_buff, "no %s BIOS files found, read docs",
\r
212 *region != 4 ? (*region == 8 ? "EU" : "JAP") : "USA");
\r
213 menu_update_msg(static_buff);
\r
218 /* check if the name begins with BIOS name */
\r
220 static int emu_isBios(const char *name)
\r
223 for (i = 0; i < sizeof(biosfiles_us)/sizeof(biosfiles_us[0]); i++)
\r
224 if (strstr(name, biosfiles_us[i]) != NULL) return 1;
\r
225 for (i = 0; i < sizeof(biosfiles_eu)/sizeof(biosfiles_eu[0]); i++)
\r
226 if (strstr(name, biosfiles_eu[i]) != NULL) return 1;
\r
227 for (i = 0; i < sizeof(biosfiles_jp)/sizeof(biosfiles_jp[0]); i++)
\r
228 if (strstr(name, biosfiles_jp[i]) != NULL) return 1;
\r
233 static int extract_text(char *dest, const unsigned char *src, int len, int swab)
\r
238 if (swab) swab = 1;
\r
240 for (i = len - 1; i >= 0; i--)
\r
242 if (src[i^swab] != ' ') break;
\r
246 for (i = 0; i < len; i++)
\r
248 unsigned char s = src[i^swab];
\r
249 if (s >= 0x20 && s < 0x7f && s != '#' && s != '|' &&
\r
250 s != '[' && s != ']' && s != '\\')
\r
256 sprintf(p, "\\%02x", s);
\r
264 static char *emu_make_rom_id(const char *fname)
\r
266 static char id_string[3+0xe*3+0x3*3+0x30*3+3];
\r
269 if (PicoAHW & PAHW_MCD) {
\r
270 strcpy(id_string, "CD|");
\r
273 else if (PicoAHW & PAHW_SMS)
\r
274 strcpy(id_string, "MS|");
\r
275 else strcpy(id_string, "MD|");
\r
278 if (!(PicoAHW & PAHW_SMS)) {
\r
279 pos += extract_text(id_string + pos, media_id_header + 0x80, 0x0e, swab); // serial
\r
280 id_string[pos] = '|'; pos++;
\r
281 pos += extract_text(id_string + pos, media_id_header + 0xf0, 0x03, swab); // region
\r
282 id_string[pos] = '|'; pos++;
\r
283 pos += extract_text(id_string + pos, media_id_header + 0x50, 0x30, swab); // overseas name
\r
284 id_string[pos] = 0;
\r
290 // can't find name in ROM, use filename
\r
291 fname_ext(id_string + 3, sizeof(id_string) - 3, NULL, NULL, fname);
\r
296 // buffer must be at least 150 byte long
\r
297 void emu_get_game_name(char *str150)
\r
299 int ret, swab = (PicoAHW & PAHW_MCD) ? 0 : 1;
\r
302 ret = extract_text(str150, media_id_header + 0x50, 0x30, swab); // overseas name
\r
304 for (s = d = str150 + 1; s < str150+ret; s++)
\r
306 if (*s == 0) break;
\r
307 if (*s != ' ' || d[-1] != ' ')
\r
313 static void system_announce(void)
\r
315 const char *sys_name, *tv_standard, *extra = "";
\r
318 if (PicoAHW & PAHW_SMS) {
\r
319 sys_name = "Master System";
\r
321 extra = " [no support]";
\r
323 } else if (PicoAHW & PAHW_PICO) {
\r
325 } else if (PicoAHW & PAHW_MCD) {
\r
326 sys_name = "Mega CD";
\r
327 if ((Pico.m.hardware & 0xc0) == 0x80)
\r
328 sys_name = "Sega CD";
\r
329 } else if (PicoAHW & PAHW_32X) {
\r
332 sys_name = "MegaDrive";
\r
333 if ((Pico.m.hardware & 0xc0) == 0x80)
\r
334 sys_name = "Genesis";
\r
336 tv_standard = Pico.m.pal ? "PAL" : "NTSC";
\r
337 fps = Pico.m.pal ? 50 : 60;
\r
339 emu_status_msg("%s %s / %dFPS%s", tv_standard, sys_name, fps, extra);
\r
342 static void do_region_override(const char *media_fname)
\r
344 // we only need to override region if config tells us so
\r
345 int ret = emu_read_config(media_fname, 0);
\r
346 if (!ret) emu_read_config(NULL, 0);
\r
349 int emu_reload_rom(const char *rom_fname_in)
\r
351 char *rom_fname = NULL;
\r
353 enum media_type_e media_type;
\r
354 int menu_romload_started = 0;
\r
355 char carthw_path[512];
\r
358 lprintf("emu_ReloadRom(%s)\n", rom_fname_in);
\r
360 rom_fname = strdup(rom_fname_in);
\r
361 if (rom_fname == NULL)
\r
364 get_ext(rom_fname, ext);
\r
373 if (!strcmp(ext, ".gmv"))
\r
375 // check for both gmv and rom
\r
377 FILE *movie_file = fopen(rom_fname, "rb");
\r
379 menu_update_msg("Failed to open movie.");
\r
382 fseek(movie_file, 0, SEEK_END);
\r
383 movie_size = ftell(movie_file);
\r
384 fseek(movie_file, 0, SEEK_SET);
\r
385 if (movie_size < 64+3) {
\r
386 menu_update_msg("Invalid GMV file.");
\r
387 fclose(movie_file);
\r
390 movie_data = malloc(movie_size);
\r
391 if (movie_data == NULL) {
\r
392 menu_update_msg("low memory.");
\r
393 fclose(movie_file);
\r
396 dummy = fread(movie_data, 1, movie_size, movie_file);
\r
397 fclose(movie_file);
\r
398 if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {
\r
399 menu_update_msg("Invalid GMV file.");
\r
402 dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);
\r
404 menu_update_msg("Could't find a ROM for movie.");
\r
407 get_ext(rom_fname, ext);
\r
408 lprintf("gmv loaded for %s\n", rom_fname);
\r
410 else if (!strcmp(ext, ".pat"))
\r
413 PicoPatchLoad(rom_fname);
\r
414 dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);
\r
416 menu_update_msg("Could't find a ROM to patch.");
\r
419 get_ext(rom_fname, ext);
\r
422 menu_romload_prepare(rom_fname); // also CD load
\r
423 menu_romload_started = 1;
\r
425 emu_make_path(carthw_path, "carthw.cfg", sizeof(carthw_path));
\r
427 media_type = PicoLoadMedia(rom_fname, carthw_path,
\r
428 find_bios, do_region_override);
\r
430 switch (media_type) {
\r
431 case PM_BAD_DETECT:
\r
432 menu_update_msg("Not a ROM/CD img selected.");
\r
435 menu_update_msg("Invalid CD image");
\r
437 case PM_BAD_CD_NO_BIOS:
\r
438 // find_bios() prints a message
\r
441 menu_update_msg("Load error");
\r
447 // make quirks visible in UI
\r
448 if (PicoQuirks & PQUIRK_FORCE_6BTN)
\r
449 currentConfig.input_dev0 = PICO_INPUT_PAD_6BTN;
\r
451 menu_romload_end();
\r
452 menu_romload_started = 0;
\r
455 PicoPatchPrepare();
\r
459 // additional movie stuff
\r
462 enum input_device indev = (movie_data[0x14] == '6') ?
\r
463 PICO_INPUT_PAD_6BTN : PICO_INPUT_PAD_3BTN;
\r
464 PicoSetInputDevice(0, indev);
\r
465 PicoSetInputDevice(1, indev);
\r
467 PicoOpt |= POPT_DIS_VDP_FIFO; // no VDP fifo timing
\r
468 if (movie_data[0xF] >= 'A') {
\r
469 if (movie_data[0x16] & 0x80) {
\r
470 PicoRegionOverride = 8;
\r
472 PicoRegionOverride = 4;
\r
475 // TODO: bits 6 & 5
\r
477 movie_data[0x18+30] = 0;
\r
478 emu_status_msg("MOVIE: %s", (char *) &movie_data[0x18]);
\r
483 PicoOpt &= ~POPT_DIS_VDP_FIFO;
\r
486 strncpy(rom_fname_loaded, rom_fname, sizeof(rom_fname_loaded)-1);
\r
487 rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0;
\r
489 // load SRAM for this ROM
\r
490 if (currentConfig.EmuOpt & EOPT_EN_SRAM)
\r
491 emu_save_load_game(1, 1);
\r
495 if (menu_romload_started)
\r
496 menu_romload_end();
\r
501 int emu_swap_cd(const char *fname)
\r
503 cd_img_type cd_type;
\r
506 cd_type = PicoCdCheck(fname, NULL);
\r
507 if (cd_type != CIT_NOT_CD)
\r
508 ret = Insert_CD(fname, cd_type);
\r
510 menu_update_msg("Load failed, invalid CD image?");
\r
514 strncpy(rom_fname_loaded, fname, sizeof(rom_fname_loaded)-1);
\r
515 rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0;
\r
520 void emu_make_path(char *buff, const char *end, int size)
\r
524 end_len = strlen(end);
\r
525 pos = plat_get_root_dir(buff, size);
\r
526 strncpy(buff + pos, end, size - pos);
\r
527 buff[size - 1] = 0;
\r
528 if (pos + end_len > size - 1)
\r
529 lprintf("Warning: path truncated: %s\n", buff);
\r
532 static void make_config_cfg(char *cfg_buff_512)
\r
534 emu_make_path(cfg_buff_512, PicoConfigFile, 512-6);
\r
535 if (config_slot != 0)
\r
537 char *p = strrchr(cfg_buff_512, '.');
\r
539 p = cfg_buff_512 + strlen(cfg_buff_512);
\r
540 sprintf(p, ".%i.cfg", config_slot);
\r
542 cfg_buff_512[511] = 0;
\r
545 void emu_prep_defconfig(void)
\r
547 memset(&defaultConfig, 0, sizeof(defaultConfig));
\r
548 defaultConfig.EmuOpt = 0x9d | EOPT_RAM_TIMINGS|EOPT_EN_CD_LEDS;
\r
549 defaultConfig.s_PicoOpt = POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80 |
\r
550 POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_MCD_GFX |
\r
551 POPT_EN_SVP_DRC|POPT_ACC_SPRITES |
\r
552 POPT_EN_32X|POPT_EN_PWM;
\r
553 defaultConfig.s_PsndRate = 44100;
\r
554 defaultConfig.s_PicoRegion = 0; // auto
\r
555 defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP
\r
556 defaultConfig.s_PicoCDBuffers = 0;
\r
557 defaultConfig.confirm_save = EOPT_CONFIRM_SAVE;
\r
558 defaultConfig.Frameskip = -1; // auto
\r
559 defaultConfig.input_dev0 = PICO_INPUT_PAD_3BTN;
\r
560 defaultConfig.input_dev1 = PICO_INPUT_PAD_3BTN;
\r
561 defaultConfig.volume = 50;
\r
562 defaultConfig.gamma = 100;
\r
563 defaultConfig.scaling = 0;
\r
564 defaultConfig.turbo_rate = 15;
\r
565 defaultConfig.msh2_khz = PICO_MSH2_HZ / 1000;
\r
566 defaultConfig.ssh2_khz = PICO_SSH2_HZ / 1000;
\r
568 // platform specific overrides
\r
569 pemu_prep_defconfig();
\r
572 void emu_set_defconfig(void)
\r
574 memcpy(¤tConfig, &defaultConfig, sizeof(currentConfig));
\r
575 PicoOpt = currentConfig.s_PicoOpt;
\r
576 PsndRate = currentConfig.s_PsndRate;
\r
577 PicoRegionOverride = currentConfig.s_PicoRegion;
\r
578 PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder;
\r
579 PicoCDBuffers = currentConfig.s_PicoCDBuffers;
\r
582 int emu_read_config(const char *rom_fname, int no_defaults)
\r
588 emu_set_defconfig();
\r
590 if (rom_fname == NULL)
\r
593 make_config_cfg(cfg);
\r
594 ret = config_readsect(cfg, NULL);
\r
601 if (config_slot != 0)
\r
602 snprintf(ext, sizeof(ext), ".%i.cfg", config_slot);
\r
604 strcpy(ext, ".cfg");
\r
606 fname_ext(cfg, sizeof(cfg), "cfg"PATH_SEP, ext, rom_fname);
\r
608 // read user's config
\r
609 vol = currentConfig.volume;
\r
610 ret = config_readsect(cfg, NULL);
\r
611 currentConfig.volume = vol; // make vol global (bah)
\r
615 // read global config, and apply game_def.cfg on top
\r
616 make_config_cfg(cfg);
\r
617 config_readsect(cfg, NULL);
\r
619 emu_make_path(cfg, "game_def.cfg", sizeof(cfg));
\r
620 ret = config_readsect(cfg, emu_make_rom_id(rom_fname));
\r
624 pemu_validate_config();
\r
626 // some sanity checks
\r
628 /* TODO: mv to plat_validate_config() */
\r
629 if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;
\r
630 if (currentConfig.gamma < -4 || currentConfig.gamma > 16) currentConfig.gamma = 0;
\r
631 if (currentConfig.gamma2 < 0 || currentConfig.gamma2 > 2) currentConfig.gamma2 = 0;
\r
633 if (currentConfig.volume < 0 || currentConfig.volume > 99)
\r
634 currentConfig.volume = 50;
\r
637 config_slot_current = config_slot;
\r
643 int emu_write_config(int is_game)
\r
646 int ret, write_lrom = 0;
\r
650 make_config_cfg(cfg);
\r
655 if (config_slot != 0)
\r
656 snprintf(ext, sizeof(ext), ".%i.cfg", config_slot);
\r
658 strcpy(ext, ".cfg");
\r
660 romfname_ext(cfg, sizeof(cfg), "cfg"PATH_SEP, ext);
\r
663 lprintf("emu_write_config: %s ", cfg);
\r
664 ret = config_write(cfg);
\r
665 if (write_lrom) config_writelrom(cfg);
\r
669 lprintf((ret == 0) ? "(ok)\n" : "(failed)\n");
\r
671 if (ret == 0) config_slot_current = config_slot;
\r
676 /* always using built-in font */
\r
678 #define mk_text_out(name, type, val, topleft, step_x, step_y) \
\r
679 void name(int x, int y, const char *text) \
\r
681 int i, l, len = strlen(text); \
\r
682 type *screen = (type *)(topleft) + x * step_x + y * step_y; \
\r
684 for (i = 0; i < len; i++, screen += 8 * step_x) \
\r
686 for (l = 0; l < 8; l++) \
\r
688 unsigned char fd = fontdata8x8[text[i] * 8 + l];\
\r
689 type *s = screen + l * step_y; \
\r
690 if (fd&0x80) s[step_x * 0] = val; \
\r
691 if (fd&0x40) s[step_x * 1] = val; \
\r
692 if (fd&0x20) s[step_x * 2] = val; \
\r
693 if (fd&0x10) s[step_x * 3] = val; \
\r
694 if (fd&0x08) s[step_x * 4] = val; \
\r
695 if (fd&0x04) s[step_x * 5] = val; \
\r
696 if (fd&0x02) s[step_x * 6] = val; \
\r
697 if (fd&0x01) s[step_x * 7] = val; \
\r
702 mk_text_out(emu_text_out8, unsigned char, 0xf0, g_screen_ptr, 1, g_screen_width)
\r
703 mk_text_out(emu_text_out16, unsigned short, 0xffff, g_screen_ptr, 1, g_screen_width)
\r
704 mk_text_out(emu_text_out8_rot, unsigned char, 0xf0,
\r
705 (char *)g_screen_ptr + (g_screen_width - 1) * g_screen_height, -g_screen_height, 1)
\r
706 mk_text_out(emu_text_out16_rot, unsigned short, 0xffff,
\r
707 (short *)g_screen_ptr + (g_screen_width - 1) * g_screen_height, -g_screen_height, 1)
\r
712 void update_movie(void)
\r
714 int offs = Pico.m.frame_count*3 + 0x40;
\r
715 if (offs+3 > movie_size) {
\r
718 emu_status_msg("END OF MOVIE.");
\r
719 lprintf("END OF MOVIE.\n");
\r
722 PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU
\r
723 if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // C
\r
724 if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // A
\r
725 if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // B
\r
726 PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU
\r
727 if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // C
\r
728 if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // A
\r
729 if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // B
\r
730 PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX
\r
731 if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X
\r
732 if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z
\r
733 PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX
\r
734 if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X
\r
735 if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z
\r
739 static int try_ropen_file(const char *fname)
\r
743 f = fopen(fname, "rb");
\r
751 char *emu_get_save_fname(int load, int is_sram, int slot)
\r
753 char *saveFname = static_buff;
\r
758 strcpy(ext, (PicoAHW & PAHW_MCD) ? ".brm" : ".srm");
\r
759 romfname_ext(saveFname, sizeof(static_buff),
\r
760 (PicoAHW & PAHW_MCD) ? "brm"PATH_SEP : "srm"PATH_SEP, ext);
\r
764 if (try_ropen_file(saveFname))
\r
767 romfname_ext(saveFname, sizeof(static_buff), NULL, ext);
\r
768 if (try_ropen_file(saveFname))
\r
773 const char *ext_main = (currentConfig.EmuOpt & EOPT_GZIP_SAVES) ? ".mds.gz" : ".mds";
\r
774 const char *ext_othr = (currentConfig.EmuOpt & EOPT_GZIP_SAVES) ? ".mds" : ".mds.gz";
\r
776 if (slot > 0 && slot < 10)
\r
777 sprintf(ext, ".%i", slot);
\r
778 strcat(ext, ext_main);
\r
781 romfname_ext(saveFname, sizeof(static_buff), "mds" PATH_SEP, ext);
\r
785 romfname_ext(saveFname, sizeof(static_buff), "mds" PATH_SEP, ext);
\r
786 if (try_ropen_file(saveFname))
\r
789 romfname_ext(saveFname, sizeof(static_buff), NULL, ext);
\r
790 if (try_ropen_file(saveFname))
\r
793 // try the other ext
\r
795 if (slot > 0 && slot < 10)
\r
796 sprintf(ext, ".%i", slot);
\r
797 strcat(ext, ext_othr);
\r
799 romfname_ext(saveFname, sizeof(static_buff), "mds"PATH_SEP, ext);
\r
800 if (try_ropen_file(saveFname))
\r
808 int emu_check_save_file(int slot, int *time)
\r
810 return emu_get_save_fname(1, 0, slot) ? 1 : 0;
\r
813 int emu_save_load_game(int load, int sram)
\r
818 // make save filename
\r
819 saveFname = emu_get_save_fname(load, sram, state_slot);
\r
820 if (saveFname == NULL) {
\r
822 emu_status_msg(load ? "LOAD FAILED (missing file)" : "SAVE FAILED");
\r
826 lprintf("saveLoad (%i, %i): %s\n", load, sram, saveFname);
\r
832 unsigned char *sram_data;
\r
834 if (PicoAHW & PAHW_MCD)
\r
836 if (PicoOpt & POPT_EN_MCD_RAMCART) {
\r
837 sram_size = 0x12000;
\r
838 sram_data = SRam.data;
\r
840 memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4);
\r
842 sram_size = 0x2000;
\r
843 sram_data = Pico_mcd->bram;
\r
844 truncate = 0; // the .brm may contain RAM cart data after normal brm
\r
847 sram_size = SRam.size;
\r
848 sram_data = SRam.data;
\r
850 if (sram_data == NULL)
\r
851 return 0; // SRam forcefully disabled for this game
\r
855 sramFile = fopen(saveFname, "rb");
\r
858 ret = fread(sram_data, 1, sram_size, sramFile);
\r
859 ret = ret > 0 ? 0 : -1;
\r
861 if ((PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_RAMCART))
\r
862 memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4);
\r
864 // sram save needs some special processing
\r
865 // see if we have anything to save
\r
866 for (; sram_size > 0; sram_size--)
\r
867 if (sram_data[sram_size-1]) break;
\r
870 sramFile = fopen(saveFname, truncate ? "wb" : "r+b");
\r
871 if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry
\r
872 if (!sramFile) return -1;
\r
873 ret = fwrite(sram_data, 1, sram_size, sramFile);
\r
874 ret = (ret != sram_size) ? -1 : 0;
\r
885 ret = PicoState(saveFname, !load);
\r
890 emu_status_msg(load ? "STATE LOADED" : "STATE SAVED");
\r
892 emu_status_msg(load ? "LOAD FAILED" : "SAVE FAILED");
\r
900 void emu_set_fastforward(int set_on)
\r
902 static void *set_PsndOut = NULL;
\r
903 static int set_Frameskip, set_EmuOpt, is_on = 0;
\r
905 if (set_on && !is_on) {
\r
906 set_PsndOut = PsndOut;
\r
907 set_Frameskip = currentConfig.Frameskip;
\r
908 set_EmuOpt = currentConfig.EmuOpt;
\r
910 currentConfig.Frameskip = 8;
\r
911 currentConfig.EmuOpt &= ~4;
\r
912 currentConfig.EmuOpt |= 0x40000;
\r
914 emu_status_msg("FAST FORWARD");
\r
916 else if (!set_on && is_on) {
\r
917 PsndOut = set_PsndOut;
\r
918 currentConfig.Frameskip = set_Frameskip;
\r
919 currentConfig.EmuOpt = set_EmuOpt;
\r
925 static void emu_tray_open(void)
\r
927 engineState = PGS_TrayMenu;
\r
930 static void emu_tray_close(void)
\r
932 emu_status_msg("CD tray closed.");
\r
935 void emu_32x_startup(void)
\r
937 plat_video_toggle_renderer(0, 0); // HACK
\r
941 void emu_reset_game(void)
\r
947 void run_events_pico(unsigned int events)
\r
951 if (events & PEV_PICO_SWINP) {
\r
953 if (pico_inp_mode > 2)
\r
955 switch (pico_inp_mode) {
\r
956 case 2: emu_status_msg("Input: Pen on Pad"); break;
\r
957 case 1: emu_status_msg("Input: Pen on Storyware"); break;
\r
958 case 0: emu_status_msg("Input: Joystick");
\r
959 PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;
\r
963 if (events & PEV_PICO_PPREV) {
\r
965 if (PicoPicohw.page < 0)
\r
966 PicoPicohw.page = 0;
\r
967 emu_status_msg("Page %i", PicoPicohw.page);
\r
969 if (events & PEV_PICO_PNEXT) {
\r
971 if (PicoPicohw.page > 6)
\r
972 PicoPicohw.page = 6;
\r
973 emu_status_msg("Page %i", PicoPicohw.page);
\r
976 if (pico_inp_mode == 0)
\r
979 /* handle other input modes */
\r
980 if (PicoPad[0] & 1) pico_pen_y--;
\r
981 if (PicoPad[0] & 2) pico_pen_y++;
\r
982 if (PicoPad[0] & 4) pico_pen_x--;
\r
983 if (PicoPad[0] & 8) pico_pen_x++;
\r
984 PicoPad[0] &= ~0x0f; // release UDLR
\r
986 lim_x = (Pico.video.reg[12]&1) ? 319 : 255;
\r
987 if (pico_pen_y < 8)
\r
989 if (pico_pen_y > 224 - PICO_PEN_ADJUST_Y)
\r
990 pico_pen_y = 224 - PICO_PEN_ADJUST_Y;
\r
991 if (pico_pen_x < 0)
\r
993 if (pico_pen_x > lim_x - PICO_PEN_ADJUST_X)
\r
994 pico_pen_x = lim_x - PICO_PEN_ADJUST_X;
\r
996 PicoPicohw.pen_pos[0] = pico_pen_x;
\r
997 if (!(Pico.video.reg[12] & 1))
\r
998 PicoPicohw.pen_pos[0] += pico_pen_x / 4;
\r
999 PicoPicohw.pen_pos[0] += 0x3c;
\r
1000 PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);
\r
1003 static void do_turbo(int *pad, int acts)
\r
1005 static int turbo_pad = 0;
\r
1006 static unsigned char turbo_cnt[3] = { 0, 0, 0 };
\r
1007 int inc = currentConfig.turbo_rate * 2;
\r
1009 if (acts & 0x1000) {
\r
1010 turbo_cnt[0] += inc;
\r
1011 if (turbo_cnt[0] >= 60)
\r
1012 turbo_pad ^= 0x10, turbo_cnt[0] = 0;
\r
1014 if (acts & 0x2000) {
\r
1015 turbo_cnt[1] += inc;
\r
1016 if (turbo_cnt[1] >= 60)
\r
1017 turbo_pad ^= 0x20, turbo_cnt[1] = 0;
\r
1019 if (acts & 0x4000) {
\r
1020 turbo_cnt[2] += inc;
\r
1021 if (turbo_cnt[2] >= 60)
\r
1022 turbo_pad ^= 0x40, turbo_cnt[2] = 0;
\r
1024 *pad |= turbo_pad & (acts >> 8);
\r
1027 static void run_events_ui(unsigned int which)
\r
1029 if (which & (PEV_STATE_LOAD|PEV_STATE_SAVE))
\r
1032 if ( emu_check_save_file(state_slot, NULL) &&
\r
1033 (((which & PEV_STATE_LOAD) && (currentConfig.confirm_save & EOPT_CONFIRM_LOAD)) ||
\r
1034 ((which & PEV_STATE_SAVE) && (currentConfig.confirm_save & EOPT_CONFIRM_SAVE))) )
\r
1040 strcpy(tmp, (which & PEV_STATE_LOAD) ? "LOAD STATE?" : "OVERWRITE SAVE?");
\r
1041 len = strlen(tmp);
\r
1042 nm = in_get_key_name(-1, -PBTN_MA3);
\r
1043 snprintf(tmp + len, sizeof(tmp) - len, "(%s=yes, ", nm);
\r
1044 len = strlen(tmp);
\r
1045 nm = in_get_key_name(-1, -PBTN_MBACK);
\r
1046 snprintf(tmp + len, sizeof(tmp) - len, "%s=no)", nm);
\r
1048 plat_status_msg_busy_first(tmp);
\r
1050 in_set_config_int(0, IN_CFG_BLOCKING, 1);
\r
1051 while (in_menu_wait_any(NULL, 50) & (PBTN_MA3|PBTN_MBACK))
\r
1053 while ( !((keys = in_menu_wait_any(NULL, 50)) & (PBTN_MA3|PBTN_MBACK)) )
\r
1055 if (keys & PBTN_MBACK)
\r
1057 while (in_menu_wait_any(NULL, 50) & (PBTN_MA3|PBTN_MBACK))
\r
1059 in_set_config_int(0, IN_CFG_BLOCKING, 0);
\r
1062 plat_status_msg_busy_first((which & PEV_STATE_LOAD) ? "LOADING STATE" : "SAVING STATE");
\r
1063 PicoStateProgressCB = plat_status_msg_busy_next;
\r
1064 emu_save_load_game((which & PEV_STATE_LOAD) ? 1 : 0, 0);
\r
1065 PicoStateProgressCB = NULL;
\r
1068 if (which & PEV_SWITCH_RND)
\r
1070 plat_video_toggle_renderer(1, 0);
\r
1072 if (which & (PEV_SSLOT_PREV|PEV_SSLOT_NEXT))
\r
1074 if (which & PEV_SSLOT_PREV) {
\r
1076 if (state_slot < 0)
\r
1080 if (state_slot > 9)
\r
1084 emu_status_msg("SAVE SLOT %i [%s]", state_slot,
\r
1085 emu_check_save_file(state_slot, NULL) ? "USED" : "FREE");
\r
1087 if (which & PEV_MENU)
\r
1088 engineState = PGS_Menu;
\r
1091 void emu_update_input(void)
\r
1093 static int prev_events = 0;
\r
1094 int actions[IN_BINDTYPE_COUNT] = { 0, };
\r
1095 int pl_actions[2];
\r
1098 in_update(actions);
\r
1100 pl_actions[0] = actions[IN_BINDTYPE_PLAYER12];
\r
1101 pl_actions[1] = actions[IN_BINDTYPE_PLAYER12] >> 16;
\r
1103 PicoPad[0] = pl_actions[0] & 0xfff;
\r
1104 PicoPad[1] = pl_actions[1] & 0xfff;
\r
1106 if (pl_actions[0] & 0x7000)
\r
1107 do_turbo(&PicoPad[0], pl_actions[0]);
\r
1108 if (pl_actions[1] & 0x7000)
\r
1109 do_turbo(&PicoPad[1], pl_actions[1]);
\r
1111 events = actions[IN_BINDTYPE_EMU] & PEV_MASK;
\r
1113 // volume is treated in special way and triggered every frame
\r
1114 if (events & (PEV_VOL_DOWN|PEV_VOL_UP))
\r
1115 plat_update_volume(1, events & PEV_VOL_UP);
\r
1117 if ((events ^ prev_events) & PEV_FF) {
\r
1118 emu_set_fastforward(events & PEV_FF);
\r
1119 plat_update_volume(0, 0);
\r
1123 events &= ~prev_events;
\r
1125 if (PicoAHW == PAHW_PICO)
\r
1126 run_events_pico(events);
\r
1128 run_events_ui(events);
\r
1132 prev_events = actions[IN_BINDTYPE_EMU] & PEV_MASK;
\r
1135 static void mkdir_path(char *path_with_reserve, int pos, const char *name)
\r
1137 strcpy(path_with_reserve + pos, name);
\r
1138 if (plat_is_dir(path_with_reserve))
\r
1140 if (mkdir(path_with_reserve, 0777) < 0)
\r
1141 lprintf("failed to create: %s\n", path_with_reserve);
\r
1144 void emu_cmn_forced_frame(int no_scale, int do_emu)
\r
1146 int po_old = PicoOpt;
\r
1148 memset32(g_screen_ptr, 0, g_screen_width * g_screen_height * 2 / 4);
\r
1150 PicoOpt &= ~POPT_ALT_RENDERER;
\r
1151 PicoOpt |= POPT_ACC_SPRITES;
\r
1153 PicoOpt |= POPT_EN_SOFTSCALE;
\r
1155 PicoDrawSetOutFormat(PDF_RGB555, 1);
\r
1156 Pico.m.dirtyPal = 1;
\r
1160 PicoFrameDrawOnly();
\r
1165 void emu_init(void)
\r
1171 // FIXME: handle through menu, etc
\r
1173 f = fopen("32X_M_BIOS.BIN", "rb");
\r
1174 p32x_bios_m = malloc(2048);
\r
1175 fread(p32x_bios_m, 1, 2048, f);
\r
1177 f = fopen("32X_S_BIOS.BIN", "rb");
\r
1178 p32x_bios_s = malloc(1024);
\r
1179 fread(p32x_bios_s, 1, 1024, f);
\r
1183 /* make dirs for saves */
\r
1184 pos = plat_get_root_dir(path, sizeof(path) - 4);
\r
1185 mkdir_path(path, pos, "mds");
\r
1186 mkdir_path(path, pos, "srm");
\r
1187 mkdir_path(path, pos, "brm");
\r
1188 mkdir_path(path, pos, "cfg");
\r
1192 make_config_cfg(path);
\r
1193 config_readlrom(path);
\r
1196 PicoMessage = plat_status_msg_busy_next;
\r
1197 PicoMCDopenTray = emu_tray_open;
\r
1198 PicoMCDcloseTray = emu_tray_close;
\r
1203 void emu_finish(void)
\r
1206 if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {
\r
1207 emu_save_load_game(0, 1);
\r
1211 if (!(currentConfig.EmuOpt & EOPT_NO_AUTOSVCFG)) {
\r
1213 make_config_cfg(cfg);
\r
1214 config_writelrom(cfg);
\r
1226 static void snd_write_nonblocking(int len)
\r
1228 sndout_write_nb(PsndOut, len);
\r
1231 void emu_sound_start(void)
\r
1235 if (currentConfig.EmuOpt & EOPT_EN_SOUND)
\r
1237 int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0;
\r
1239 PsndRerate(Pico.m.frame_count ? 1 : 0);
\r
1241 printf("starting audio: %i len: %i stereo: %i, pal: %i\n",
\r
1242 PsndRate, PsndLen, is_stereo, Pico.m.pal);
\r
1243 sndout_start(PsndRate, is_stereo);
\r
1244 PicoWriteSound = snd_write_nonblocking;
\r
1245 plat_update_volume(0, 0);
\r
1246 memset(sndBuffer, 0, sizeof(sndBuffer));
\r
1247 PsndOut = sndBuffer;
\r
1251 void emu_sound_stop(void)
\r
1256 void emu_sound_wait(void)
\r
1261 static void emu_loop_prep(void)
\r
1263 static int pal_old = -1;
\r
1264 static int filter_old = -1;
\r
1266 if (currentConfig.CPUclock != plat_target_cpu_clock_get())
\r
1267 plat_target_cpu_clock_set(currentConfig.CPUclock);
\r
1269 if (Pico.m.pal != pal_old) {
\r
1270 plat_target_lcdrate_set(Pico.m.pal);
\r
1271 pal_old = Pico.m.pal;
\r
1274 if (currentConfig.filter != filter_old) {
\r
1275 plat_target_hwfilter_set(currentConfig.filter);
\r
1276 filter_old = currentConfig.filter;
\r
1279 plat_target_gamma_set(currentConfig.gamma, 0);
\r
1284 static void skip_frame(int do_audio)
\r
1286 PicoSkipFrame = do_audio ? 1 : 2;
\r
1288 PicoSkipFrame = 0;
\r
1291 /* our tick here is 1 us right now */
\r
1292 #define ms_to_ticks(x) (unsigned int)(x * 1000)
\r
1293 #define get_ticks() plat_get_ticks_us()
\r
1295 void emu_loop(void)
\r
1297 int pframes_done; /* "period" frames, used for sync */
\r
1298 int frames_done, frames_shown; /* actual frames for fps counter */
\r
1299 int target_fps, target_frametime;
\r
1300 unsigned int timestamp_base = 0, timestamp_fps;
\r
1301 char *notice_msg = NULL;
\r
1307 PicoLoopPrepare();
\r
1309 // prepare CD buffer
\r
1310 if (PicoAHW & PAHW_MCD)
\r
1311 PicoCDBufferInit();
\r
1313 plat_video_loop_prepare();
\r
1315 pemu_sound_start();
\r
1317 /* number of ticks per frame */
\r
1320 target_frametime = ms_to_ticks(1000) / 50;
\r
1323 target_frametime = ms_to_ticks(1000) / 60 + 1;
\r
1326 timestamp_fps = get_ticks();
\r
1329 frames_done = frames_shown = pframes_done = 0;
\r
1331 plat_video_wait_vsync();
\r
1333 /* loop with resync every 1 sec. */
\r
1334 while (engineState == PGS_Running)
\r
1336 unsigned int timestamp;
\r
1337 int diff, diff_lim;
\r
1339 pprof_start(main);
\r
1341 timestamp = get_ticks();
\r
1342 if (reset_timing) {
\r
1344 timestamp_base = timestamp;
\r
1348 // show notice_msg message?
\r
1349 if (notice_msg_time != 0)
\r
1351 static int noticeMsgSum;
\r
1352 if (timestamp - ms_to_ticks(notice_msg_time) > ms_to_ticks(STATUS_MSG_TIMEOUT)) {
\r
1353 notice_msg_time = 0;
\r
1354 plat_status_msg_clear();
\r
1355 notice_msg = NULL;
\r
1357 int sum = noticeMsg[0] + noticeMsg[1] + noticeMsg[2];
\r
1358 if (sum != noticeMsgSum) {
\r
1359 plat_status_msg_clear();
\r
1360 noticeMsgSum = sum;
\r
1362 notice_msg = noticeMsg;
\r
1366 // second changed?
\r
1367 if (timestamp - timestamp_fps >= ms_to_ticks(1000))
\r
1370 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
\r
1371 if (++bench == 10) {
\r
1373 bench_fps_s = bench_fps;
\r
1374 bf[bfp++ & 3] = bench_fps;
\r
1377 bench_fps += frames_shown;
\r
1378 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
\r
1379 printf("%s\n", fpsbuff);
\r
1381 if (currentConfig.EmuOpt & EOPT_SHOW_FPS)
\r
1382 sprintf(fpsbuff, "%02i/%02i ", frames_shown, frames_done);
\r
1384 frames_shown = frames_done = 0;
\r
1385 timestamp_fps += ms_to_ticks(1000);
\r
1388 sprintf(fpsbuff, "%i", Pico.m.frame_count);
\r
1391 if (timestamp - timestamp_base >= ms_to_ticks(1000))
\r
1393 if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && currentConfig.Frameskip >= 0)
\r
1396 pframes_done -= target_fps;
\r
1397 if (pframes_done < -2) {
\r
1398 /* don't drag more than 2 frames behind */
\r
1399 pframes_done = -2;
\r
1400 timestamp_base = timestamp - 2 * target_frametime;
\r
1403 timestamp_base += ms_to_ticks(1000);
\r
1406 diff = timestamp - timestamp_base;
\r
1407 diff_lim = (pframes_done + 1) * target_frametime;
\r
1409 if (currentConfig.Frameskip >= 0) // frameskip enabled
\r
1411 for (i = 0; i < currentConfig.Frameskip; i++) {
\r
1412 emu_update_input();
\r
1414 pframes_done++; frames_done++;
\r
1415 diff_lim += target_frametime;
\r
1417 if (!(currentConfig.EmuOpt & (EOPT_NO_FRMLIMIT|EOPT_EXT_FRMLIMIT))) {
\r
1418 timestamp = get_ticks();
\r
1419 diff = timestamp - timestamp_base;
\r
1420 if (!reset_timing && diff < diff_lim) // we are too fast
\r
1421 plat_wait_till_us(timestamp_base + diff_lim);
\r
1425 else if (diff > diff_lim)
\r
1427 /* no time left for this frame - skip */
\r
1428 /* limit auto frameskip to 8 */
\r
1429 if (frames_done / 8 <= frames_shown) {
\r
1430 emu_update_input();
\r
1431 skip_frame(diff < diff_lim + target_frametime * 16);
\r
1432 pframes_done++; frames_done++;
\r
1437 emu_update_input();
\r
1439 pemu_finalize_frame(fpsbuff, notice_msg);
\r
1441 if (!flip_after_sync)
\r
1442 plat_video_flip();
\r
1444 /* frame limiter */
\r
1445 if (!reset_timing && !(currentConfig.EmuOpt & (EOPT_NO_FRMLIMIT|EOPT_EXT_FRMLIMIT)))
\r
1447 timestamp = get_ticks();
\r
1448 diff = timestamp - timestamp_base;
\r
1450 // sleep or vsync if we are still too fast
\r
1451 if (diff < diff_lim)
\r
1453 // we are too fast
\r
1454 plat_wait_till_us(timestamp_base + diff_lim - target_frametime / 4);
\r
1455 if (currentConfig.EmuOpt & EOPT_VSYNC)
\r
1456 plat_video_wait_vsync();
\r
1460 if (flip_after_sync)
\r
1461 plat_video_flip();
\r
1463 pframes_done++; frames_done++; frames_shown++;
\r
1468 emu_set_fastforward(0);
\r
1471 if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {
\r
1472 plat_status_msg_busy_first("Writing SRAM/BRAM...");
\r
1473 emu_save_load_game(0, 1);
\r
1480 // pemu_loop_end() might want to do 1 frame for bg image,
\r
1481 // so free CD buffer here
\r
1482 if (PicoAHW & PAHW_MCD)
\r
1483 PicoCDBufferFree();
\r