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 STATUS_MSG_TIMEOUT 2000
\r
36 #if !SCREEN_SIZE_FIXED
\r
37 int g_screen_width = SCREEN_WIDTH;
\r
38 int g_screen_height = SCREEN_HEIGHT;
\r
41 char *PicoConfigFile = "config.cfg";
\r
42 currentConfig_t currentConfig, defaultConfig;
\r
44 int config_slot = 0, config_slot_current = 0;
\r
45 int pico_pen_x = 320/2, pico_pen_y = 240/2;
\r
46 int pico_inp_mode = 0;
\r
47 int engineState = PGS_Menu;
\r
49 static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
\r
51 /* tmp buff to reduce stack usage for plats with small stack */
\r
52 static char static_buff[512];
\r
53 const char *rom_fname_reload;
\r
54 char rom_fname_loaded[512];
\r
55 int reset_timing = 0;
\r
56 static unsigned int notice_msg_time; /* when started showing */
\r
57 static char noticeMsg[40];
\r
59 unsigned char *movie_data = NULL;
\r
60 static int movie_size = 0;
\r
63 /* don't use tolower() for easy old glibc binary compatibility */
\r
64 static void strlwr_(char *string)
\r
67 for (p = string; *p; p++)
\r
68 if ('A' <= *p && *p <= 'Z')
\r
72 static int try_rfn_cut(char *fname)
\r
77 p = fname + strlen(fname) - 1;
\r
78 for (; p > fname; p--)
\r
79 if (*p == '.') break;
\r
82 if((tmp = fopen(fname, "rb"))) {
\r
89 static void get_ext(const char *file, char *ext)
\r
93 p = file + strlen(file) - 4;
\r
94 if (p < file) p = file;
\r
100 static void fname_ext(char *dst, int dstlen, const char *prefix, const char *ext, const char *fname)
\r
102 int prefix_len = 0;
\r
107 int len = plat_get_root_dir(dst, dstlen);
\r
108 strcpy(dst + len, prefix);
\r
109 prefix_len = len + strlen(prefix);
\r
112 p = fname + strlen(fname) - 1;
\r
113 for (; p >= fname && *p != PATH_SEP_C; p--)
\r
116 strncpy(dst + prefix_len, p, dstlen - prefix_len - 1);
\r
118 dst[dstlen - 8] = 0;
\r
119 if (dst[strlen(dst) - 4] == '.')
\r
120 dst[strlen(dst) - 4] = 0;
\r
125 static void romfname_ext(char *dst, int dstlen, const char *prefix, const char *ext)
\r
127 fname_ext(dst, dstlen, prefix, ext, rom_fname_loaded);
\r
130 void emu_status_msg(const char *format, ...)
\r
135 va_start(vl, format);
\r
136 ret = vsnprintf(noticeMsg, sizeof(noticeMsg), format, vl);
\r
139 /* be sure old text gets overwritten */
\r
140 for (; ret < 28; ret++)
\r
141 noticeMsg[ret] = ' ';
\r
142 noticeMsg[ret] = 0;
\r
144 notice_msg_time = plat_get_ticks_ms();
\r
147 static const char * const biosfiles_us[] = {
\r
148 "us_scd1_9210", "us_scd2_9306", "SegaCDBIOS9303", "bios_CD_U"
\r
150 static const char * const biosfiles_eu[] = {
\r
151 "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303", "bios_CD_E"
\r
153 static const char * const biosfiles_jp[] = {
\r
154 "jp_mcd1_9112", "jp_mcd1_9111", "bios_CD_J"
\r
157 static const char *find_bios(int *region, const char *cd_fname)
\r
160 const char * const *files;
\r
164 // we need to have config loaded at this point
\r
165 ret = emu_read_config(cd_fname, 0);
\r
166 if (!ret) emu_read_config(NULL, 0);
\r
168 if (PicoRegionOverride) {
\r
169 *region = PicoRegionOverride;
\r
170 lprintf("override region to %s\n", *region != 4 ?
\r
171 (*region == 8 ? "EU" : "JAP") : "USA");
\r
174 if (*region == 4) { // US
\r
175 files = biosfiles_us;
\r
176 count = sizeof(biosfiles_us) / sizeof(char *);
\r
177 } else if (*region == 8) { // EU
\r
178 files = biosfiles_eu;
\r
179 count = sizeof(biosfiles_eu) / sizeof(char *);
\r
180 } else if (*region == 1 || *region == 2) {
\r
181 files = biosfiles_jp;
\r
182 count = sizeof(biosfiles_jp) / sizeof(char *);
\r
187 for (i = 0; i < count; i++)
\r
189 emu_make_path(static_buff, files[i], sizeof(static_buff) - 4);
\r
190 strcat(static_buff, ".bin");
\r
191 f = fopen(static_buff, "rb");
\r
194 static_buff[strlen(static_buff) - 4] = 0;
\r
195 strcat(static_buff, ".zip");
\r
196 f = fopen(static_buff, "rb");
\r
201 lprintf("using bios: %s\n", static_buff);
\r
203 return static_buff;
\r
205 sprintf(static_buff, "no %s BIOS files found, read docs",
\r
206 *region != 4 ? (*region == 8 ? "EU" : "JAP") : "USA");
\r
207 menu_update_msg(static_buff);
\r
212 /* check if the name begins with BIOS name */
\r
214 static int emu_isBios(const char *name)
\r
217 for (i = 0; i < sizeof(biosfiles_us)/sizeof(biosfiles_us[0]); i++)
\r
218 if (strstr(name, biosfiles_us[i]) != NULL) return 1;
\r
219 for (i = 0; i < sizeof(biosfiles_eu)/sizeof(biosfiles_eu[0]); i++)
\r
220 if (strstr(name, biosfiles_eu[i]) != NULL) return 1;
\r
221 for (i = 0; i < sizeof(biosfiles_jp)/sizeof(biosfiles_jp[0]); i++)
\r
222 if (strstr(name, biosfiles_jp[i]) != NULL) return 1;
\r
227 static int extract_text(char *dest, const unsigned char *src, int len, int swab)
\r
232 if (swab) swab = 1;
\r
234 for (i = len - 1; i >= 0; i--)
\r
236 if (src[i^swab] != ' ') break;
\r
240 for (i = 0; i < len; i++)
\r
242 unsigned char s = src[i^swab];
\r
243 if (s >= 0x20 && s < 0x7f && s != '#' && s != '|' &&
\r
244 s != '[' && s != ']' && s != '\\')
\r
250 sprintf(p, "\\%02x", s);
\r
258 static char *emu_make_rom_id(const char *fname)
\r
260 static char id_string[3+0xe*3+0x3*3+0x30*3+3];
\r
263 if (PicoAHW & PAHW_MCD) {
\r
264 strcpy(id_string, "CD|");
\r
267 else if (PicoAHW & PAHW_SMS)
\r
268 strcpy(id_string, "MS|");
\r
269 else strcpy(id_string, "MD|");
\r
272 if (!(PicoAHW & PAHW_SMS)) {
\r
273 pos += extract_text(id_string + pos, media_id_header + 0x80, 0x0e, swab); // serial
\r
274 id_string[pos] = '|'; pos++;
\r
275 pos += extract_text(id_string + pos, media_id_header + 0xf0, 0x03, swab); // region
\r
276 id_string[pos] = '|'; pos++;
\r
277 pos += extract_text(id_string + pos, media_id_header + 0x50, 0x30, swab); // overseas name
\r
278 id_string[pos] = 0;
\r
284 // can't find name in ROM, use filename
\r
285 fname_ext(id_string + 3, sizeof(id_string) - 3, NULL, NULL, fname);
\r
290 // buffer must be at least 150 byte long
\r
291 void emu_get_game_name(char *str150)
\r
293 int ret, swab = (PicoAHW & PAHW_MCD) ? 0 : 1;
\r
296 ret = extract_text(str150, media_id_header + 0x50, 0x30, swab); // overseas name
\r
298 for (s = d = str150 + 1; s < str150+ret; s++)
\r
300 if (*s == 0) break;
\r
301 if (*s != ' ' || d[-1] != ' ')
\r
307 static void system_announce(void)
\r
309 const char *sys_name, *tv_standard, *extra = "";
\r
312 if (PicoAHW & PAHW_SMS) {
\r
313 sys_name = "Master System";
\r
315 extra = " [no support]";
\r
317 } else if (PicoAHW & PAHW_PICO) {
\r
319 } else if (PicoAHW & PAHW_MCD) {
\r
320 sys_name = "Mega CD";
\r
321 if ((Pico.m.hardware & 0xc0) == 0x80)
\r
322 sys_name = "Sega CD";
\r
323 } else if (PicoAHW & PAHW_32X) {
\r
326 sys_name = "MegaDrive";
\r
327 if ((Pico.m.hardware & 0xc0) == 0x80)
\r
328 sys_name = "Genesis";
\r
330 tv_standard = Pico.m.pal ? "PAL" : "NTSC";
\r
331 fps = Pico.m.pal ? 50 : 60;
\r
333 emu_status_msg("%s %s / %dFPS%s", tv_standard, sys_name, fps, extra);
\r
336 static void do_region_override(const char *media_fname)
\r
338 // we only need to override region if config tells us so
\r
339 int ret = emu_read_config(media_fname, 0);
\r
340 if (!ret) emu_read_config(NULL, 0);
\r
343 int emu_reload_rom(const char *rom_fname_in)
\r
345 char *rom_fname = NULL;
\r
347 enum media_type_e media_type;
\r
348 int menu_romload_started = 0;
\r
349 char carthw_path[512];
\r
352 lprintf("emu_ReloadRom(%s)\n", rom_fname_in);
\r
354 rom_fname = strdup(rom_fname_in);
\r
355 if (rom_fname == NULL)
\r
358 get_ext(rom_fname, ext);
\r
367 if (!strcmp(ext, ".gmv"))
\r
369 // check for both gmv and rom
\r
371 FILE *movie_file = fopen(rom_fname, "rb");
\r
373 menu_update_msg("Failed to open movie.");
\r
376 fseek(movie_file, 0, SEEK_END);
\r
377 movie_size = ftell(movie_file);
\r
378 fseek(movie_file, 0, SEEK_SET);
\r
379 if (movie_size < 64+3) {
\r
380 menu_update_msg("Invalid GMV file.");
\r
381 fclose(movie_file);
\r
384 movie_data = malloc(movie_size);
\r
385 if (movie_data == NULL) {
\r
386 menu_update_msg("low memory.");
\r
387 fclose(movie_file);
\r
390 dummy = fread(movie_data, 1, movie_size, movie_file);
\r
391 fclose(movie_file);
\r
392 if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {
\r
393 menu_update_msg("Invalid GMV file.");
\r
396 dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);
\r
398 menu_update_msg("Could't find a ROM for movie.");
\r
401 get_ext(rom_fname, ext);
\r
402 lprintf("gmv loaded for %s\n", rom_fname);
\r
404 else if (!strcmp(ext, ".pat"))
\r
407 PicoPatchLoad(rom_fname);
\r
408 dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);
\r
410 menu_update_msg("Could't find a ROM to patch.");
\r
413 get_ext(rom_fname, ext);
\r
416 menu_romload_prepare(rom_fname); // also CD load
\r
417 menu_romload_started = 1;
\r
419 emu_make_path(carthw_path, "carthw.cfg", sizeof(carthw_path));
\r
421 media_type = PicoLoadMedia(rom_fname, carthw_path,
\r
422 find_bios, do_region_override);
\r
424 switch (media_type) {
\r
425 case PM_BAD_DETECT:
\r
426 menu_update_msg("Not a ROM/CD img selected.");
\r
429 menu_update_msg("Invalid CD image");
\r
431 case PM_BAD_CD_NO_BIOS:
\r
432 // find_bios() prints a message
\r
435 menu_update_msg("Load error");
\r
441 menu_romload_end();
\r
442 menu_romload_started = 0;
\r
445 PicoPatchPrepare();
\r
449 // additional movie stuff
\r
452 if (movie_data[0x14] == '6')
\r
453 PicoOpt |= POPT_6BTN_PAD; // 6 button pad
\r
454 else PicoOpt &= ~POPT_6BTN_PAD;
\r
455 PicoOpt |= POPT_DIS_VDP_FIFO; // no VDP fifo timing
\r
456 if (movie_data[0xF] >= 'A') {
\r
457 if (movie_data[0x16] & 0x80) {
\r
458 PicoRegionOverride = 8;
\r
460 PicoRegionOverride = 4;
\r
463 // TODO: bits 6 & 5
\r
465 movie_data[0x18+30] = 0;
\r
466 emu_status_msg("MOVIE: %s", (char *) &movie_data[0x18]);
\r
471 PicoOpt &= ~POPT_DIS_VDP_FIFO;
\r
474 strncpy(rom_fname_loaded, rom_fname, sizeof(rom_fname_loaded)-1);
\r
475 rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0;
\r
477 // load SRAM for this ROM
\r
478 if (currentConfig.EmuOpt & EOPT_EN_SRAM)
\r
479 emu_save_load_game(1, 1);
\r
483 if (menu_romload_started)
\r
484 menu_romload_end();
\r
489 int emu_swap_cd(const char *fname)
\r
491 cd_img_type cd_type;
\r
494 cd_type = PicoCdCheck(fname, NULL);
\r
495 if (cd_type != CIT_NOT_CD)
\r
496 ret = Insert_CD(fname, cd_type);
\r
498 menu_update_msg("Load failed, invalid CD image?");
\r
502 strncpy(rom_fname_loaded, fname, sizeof(rom_fname_loaded)-1);
\r
503 rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0;
\r
508 void emu_make_path(char *buff, const char *end, int size)
\r
512 end_len = strlen(end);
\r
513 pos = plat_get_root_dir(buff, size);
\r
514 strncpy(buff + pos, end, size - pos);
\r
515 buff[size - 1] = 0;
\r
516 if (pos + end_len > size - 1)
\r
517 lprintf("Warning: path truncated: %s\n", buff);
\r
520 static void make_config_cfg(char *cfg_buff_512)
\r
522 emu_make_path(cfg_buff_512, PicoConfigFile, 512-6);
\r
523 if (config_slot != 0)
\r
525 char *p = strrchr(cfg_buff_512, '.');
\r
527 p = cfg_buff_512 + strlen(cfg_buff_512);
\r
528 sprintf(p, ".%i.cfg", config_slot);
\r
530 cfg_buff_512[511] = 0;
\r
533 void emu_prep_defconfig(void)
\r
535 memset(&defaultConfig, 0, sizeof(defaultConfig));
\r
536 defaultConfig.EmuOpt = 0x9d | EOPT_RAM_TIMINGS|EOPT_EN_CD_LEDS;
\r
537 defaultConfig.s_PicoOpt = POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80 |
\r
538 POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_MCD_GFX |
\r
539 POPT_EN_SVP_DRC|POPT_ACC_SPRITES |
\r
540 POPT_EN_32X|POPT_EN_PWM;
\r
541 defaultConfig.s_PsndRate = 44100;
\r
542 defaultConfig.s_PicoRegion = 0; // auto
\r
543 defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP
\r
544 defaultConfig.s_PicoCDBuffers = 0;
\r
545 defaultConfig.confirm_save = EOPT_CONFIRM_SAVE;
\r
546 defaultConfig.Frameskip = -1; // auto
\r
547 defaultConfig.volume = 50;
\r
548 defaultConfig.gamma = 100;
\r
549 defaultConfig.scaling = 0;
\r
550 defaultConfig.turbo_rate = 15;
\r
551 defaultConfig.msh2_khz = PICO_MSH2_HZ / 1000;
\r
552 defaultConfig.ssh2_khz = PICO_SSH2_HZ / 1000;
\r
554 // platform specific overrides
\r
555 pemu_prep_defconfig();
\r
558 void emu_set_defconfig(void)
\r
560 memcpy(¤tConfig, &defaultConfig, sizeof(currentConfig));
\r
561 PicoOpt = currentConfig.s_PicoOpt;
\r
562 PsndRate = currentConfig.s_PsndRate;
\r
563 PicoRegionOverride = currentConfig.s_PicoRegion;
\r
564 PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder;
\r
565 PicoCDBuffers = currentConfig.s_PicoCDBuffers;
\r
568 int emu_read_config(const char *rom_fname, int no_defaults)
\r
574 emu_set_defconfig();
\r
576 if (rom_fname == NULL)
\r
579 make_config_cfg(cfg);
\r
580 ret = config_readsect(cfg, NULL);
\r
587 if (config_slot != 0)
\r
588 snprintf(ext, sizeof(ext), ".%i.cfg", config_slot);
\r
590 strcpy(ext, ".cfg");
\r
592 fname_ext(cfg, sizeof(cfg), "cfg"PATH_SEP, ext, rom_fname);
\r
594 // read user's config
\r
595 vol = currentConfig.volume;
\r
596 ret = config_readsect(cfg, NULL);
\r
597 currentConfig.volume = vol; // make vol global (bah)
\r
601 // read global config, and apply game_def.cfg on top
\r
602 make_config_cfg(cfg);
\r
603 config_readsect(cfg, NULL);
\r
605 emu_make_path(cfg, "game_def.cfg", sizeof(cfg));
\r
606 ret = config_readsect(cfg, emu_make_rom_id(rom_fname));
\r
610 pemu_validate_config();
\r
612 // some sanity checks
\r
614 /* TODO: mv to plat_validate_config() */
\r
615 if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;
\r
616 if (currentConfig.gamma < -4 || currentConfig.gamma > 16) currentConfig.gamma = 0;
\r
617 if (currentConfig.gamma2 < 0 || currentConfig.gamma2 > 2) currentConfig.gamma2 = 0;
\r
619 if (currentConfig.volume < 0 || currentConfig.volume > 99)
\r
620 currentConfig.volume = 50;
\r
623 config_slot_current = config_slot;
\r
629 int emu_write_config(int is_game)
\r
632 int ret, write_lrom = 0;
\r
636 make_config_cfg(cfg);
\r
641 if (config_slot != 0)
\r
642 snprintf(ext, sizeof(ext), ".%i.cfg", config_slot);
\r
644 strcpy(ext, ".cfg");
\r
646 romfname_ext(cfg, sizeof(cfg), "cfg"PATH_SEP, ext);
\r
649 lprintf("emu_write_config: %s ", cfg);
\r
650 ret = config_write(cfg);
\r
651 if (write_lrom) config_writelrom(cfg);
\r
655 lprintf((ret == 0) ? "(ok)\n" : "(failed)\n");
\r
657 if (ret == 0) config_slot_current = config_slot;
\r
662 /* always using built-in font */
\r
664 #define mk_text_out(name, type, val, topleft, step_x, step_y) \
\r
665 void name(int x, int y, const char *text) \
\r
667 int i, l, len = strlen(text); \
\r
668 type *screen = (type *)(topleft) + x * step_x + y * step_y; \
\r
670 for (i = 0; i < len; i++, screen += 8 * step_x) \
\r
672 for (l = 0; l < 8; l++) \
\r
674 unsigned char fd = fontdata8x8[text[i] * 8 + l];\
\r
675 type *s = screen + l * step_y; \
\r
676 if (fd&0x80) s[step_x * 0] = val; \
\r
677 if (fd&0x40) s[step_x * 1] = val; \
\r
678 if (fd&0x20) s[step_x * 2] = val; \
\r
679 if (fd&0x10) s[step_x * 3] = val; \
\r
680 if (fd&0x08) s[step_x * 4] = val; \
\r
681 if (fd&0x04) s[step_x * 5] = val; \
\r
682 if (fd&0x02) s[step_x * 6] = val; \
\r
683 if (fd&0x01) s[step_x * 7] = val; \
\r
688 mk_text_out(emu_text_out8, unsigned char, 0xf0, g_screen_ptr, 1, g_screen_width)
\r
689 mk_text_out(emu_text_out16, unsigned short, 0xffff, g_screen_ptr, 1, g_screen_width)
\r
690 mk_text_out(emu_text_out8_rot, unsigned char, 0xf0,
\r
691 (char *)g_screen_ptr + (g_screen_width - 1) * g_screen_height, -g_screen_height, 1)
\r
692 mk_text_out(emu_text_out16_rot, unsigned short, 0xffff,
\r
693 (short *)g_screen_ptr + (g_screen_width - 1) * g_screen_height, -g_screen_height, 1)
\r
698 void update_movie(void)
\r
700 int offs = Pico.m.frame_count*3 + 0x40;
\r
701 if (offs+3 > movie_size) {
\r
704 emu_status_msg("END OF MOVIE.");
\r
705 lprintf("END OF MOVIE.\n");
\r
708 PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU
\r
709 if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // C
\r
710 if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // A
\r
711 if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // B
\r
712 PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU
\r
713 if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // C
\r
714 if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // A
\r
715 if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // B
\r
716 PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX
\r
717 if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X
\r
718 if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z
\r
719 PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX
\r
720 if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X
\r
721 if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z
\r
725 static int try_ropen_file(const char *fname)
\r
729 f = fopen(fname, "rb");
\r
737 char *emu_get_save_fname(int load, int is_sram, int slot)
\r
739 char *saveFname = static_buff;
\r
744 strcpy(ext, (PicoAHW & PAHW_MCD) ? ".brm" : ".srm");
\r
745 romfname_ext(saveFname, sizeof(static_buff),
\r
746 (PicoAHW & PAHW_MCD) ? "brm"PATH_SEP : "srm"PATH_SEP, ext);
\r
750 if (try_ropen_file(saveFname))
\r
753 romfname_ext(saveFname, sizeof(static_buff), NULL, ext);
\r
754 if (try_ropen_file(saveFname))
\r
759 const char *ext_main = (currentConfig.EmuOpt & EOPT_GZIP_SAVES) ? ".mds.gz" : ".mds";
\r
760 const char *ext_othr = (currentConfig.EmuOpt & EOPT_GZIP_SAVES) ? ".mds" : ".mds.gz";
\r
762 if (slot > 0 && slot < 10)
\r
763 sprintf(ext, ".%i", slot);
\r
764 strcat(ext, ext_main);
\r
767 romfname_ext(saveFname, sizeof(static_buff), "mds" PATH_SEP, ext);
\r
771 romfname_ext(saveFname, sizeof(static_buff), "mds" PATH_SEP, ext);
\r
772 if (try_ropen_file(saveFname))
\r
775 romfname_ext(saveFname, sizeof(static_buff), NULL, ext);
\r
776 if (try_ropen_file(saveFname))
\r
779 // try the other ext
\r
781 if (slot > 0 && slot < 10)
\r
782 sprintf(ext, ".%i", slot);
\r
783 strcat(ext, ext_othr);
\r
785 romfname_ext(saveFname, sizeof(static_buff), "mds"PATH_SEP, ext);
\r
786 if (try_ropen_file(saveFname))
\r
794 int emu_check_save_file(int slot, int *time)
\r
796 return emu_get_save_fname(1, 0, slot) ? 1 : 0;
\r
799 int emu_save_load_game(int load, int sram)
\r
804 // make save filename
\r
805 saveFname = emu_get_save_fname(load, sram, state_slot);
\r
806 if (saveFname == NULL) {
\r
808 emu_status_msg(load ? "LOAD FAILED (missing file)" : "SAVE FAILED");
\r
812 lprintf("saveLoad (%i, %i): %s\n", load, sram, saveFname);
\r
818 unsigned char *sram_data;
\r
820 if (PicoAHW & PAHW_MCD)
\r
822 if (PicoOpt & POPT_EN_MCD_RAMCART) {
\r
823 sram_size = 0x12000;
\r
824 sram_data = SRam.data;
\r
826 memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4);
\r
828 sram_size = 0x2000;
\r
829 sram_data = Pico_mcd->bram;
\r
830 truncate = 0; // the .brm may contain RAM cart data after normal brm
\r
833 sram_size = SRam.size;
\r
834 sram_data = SRam.data;
\r
836 if (sram_data == NULL)
\r
837 return 0; // SRam forcefully disabled for this game
\r
841 sramFile = fopen(saveFname, "rb");
\r
844 ret = fread(sram_data, 1, sram_size, sramFile);
\r
845 ret = ret > 0 ? 0 : -1;
\r
847 if ((PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_RAMCART))
\r
848 memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4);
\r
850 // sram save needs some special processing
\r
851 // see if we have anything to save
\r
852 for (; sram_size > 0; sram_size--)
\r
853 if (sram_data[sram_size-1]) break;
\r
856 sramFile = fopen(saveFname, truncate ? "wb" : "r+b");
\r
857 if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry
\r
858 if (!sramFile) return -1;
\r
859 ret = fwrite(sram_data, 1, sram_size, sramFile);
\r
860 ret = (ret != sram_size) ? -1 : 0;
\r
871 ret = PicoState(saveFname, !load);
\r
876 emu_status_msg(load ? "STATE LOADED" : "STATE SAVED");
\r
878 emu_status_msg(load ? "LOAD FAILED" : "SAVE FAILED");
\r
886 void emu_set_fastforward(int set_on)
\r
888 static void *set_PsndOut = NULL;
\r
889 static int set_Frameskip, set_EmuOpt, is_on = 0;
\r
891 if (set_on && !is_on) {
\r
892 set_PsndOut = PsndOut;
\r
893 set_Frameskip = currentConfig.Frameskip;
\r
894 set_EmuOpt = currentConfig.EmuOpt;
\r
896 currentConfig.Frameskip = 8;
\r
897 currentConfig.EmuOpt &= ~4;
\r
898 currentConfig.EmuOpt |= 0x40000;
\r
900 emu_status_msg("FAST FORWARD");
\r
902 else if (!set_on && is_on) {
\r
903 PsndOut = set_PsndOut;
\r
904 currentConfig.Frameskip = set_Frameskip;
\r
905 currentConfig.EmuOpt = set_EmuOpt;
\r
911 static void emu_tray_open(void)
\r
913 engineState = PGS_TrayMenu;
\r
916 static void emu_tray_close(void)
\r
918 emu_status_msg("CD tray closed.");
\r
921 void emu_32x_startup(void)
\r
923 plat_video_toggle_renderer(0, 0); // HACK
\r
927 void emu_reset_game(void)
\r
933 void run_events_pico(unsigned int events)
\r
937 if (events & PEV_PICO_SWINP) {
\r
939 if (pico_inp_mode > 2)
\r
941 switch (pico_inp_mode) {
\r
942 case 2: emu_status_msg("Input: Pen on Pad"); break;
\r
943 case 1: emu_status_msg("Input: Pen on Storyware"); break;
\r
944 case 0: emu_status_msg("Input: Joystick");
\r
945 PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;
\r
949 if (events & PEV_PICO_PPREV) {
\r
951 if (PicoPicohw.page < 0)
\r
952 PicoPicohw.page = 0;
\r
953 emu_status_msg("Page %i", PicoPicohw.page);
\r
955 if (events & PEV_PICO_PNEXT) {
\r
957 if (PicoPicohw.page > 6)
\r
958 PicoPicohw.page = 6;
\r
959 emu_status_msg("Page %i", PicoPicohw.page);
\r
962 if (pico_inp_mode == 0)
\r
965 /* handle other input modes */
\r
966 if (PicoPad[0] & 1) pico_pen_y--;
\r
967 if (PicoPad[0] & 2) pico_pen_y++;
\r
968 if (PicoPad[0] & 4) pico_pen_x--;
\r
969 if (PicoPad[0] & 8) pico_pen_x++;
\r
970 PicoPad[0] &= ~0x0f; // release UDLR
\r
972 lim_x = (Pico.video.reg[12]&1) ? 319 : 255;
\r
973 if (pico_pen_y < 8)
\r
975 if (pico_pen_y > 224 - PICO_PEN_ADJUST_Y)
\r
976 pico_pen_y = 224 - PICO_PEN_ADJUST_Y;
\r
977 if (pico_pen_x < 0)
\r
979 if (pico_pen_x > lim_x - PICO_PEN_ADJUST_X)
\r
980 pico_pen_x = lim_x - PICO_PEN_ADJUST_X;
\r
982 PicoPicohw.pen_pos[0] = pico_pen_x;
\r
983 if (!(Pico.video.reg[12] & 1))
\r
984 PicoPicohw.pen_pos[0] += pico_pen_x / 4;
\r
985 PicoPicohw.pen_pos[0] += 0x3c;
\r
986 PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);
\r
989 static void do_turbo(int *pad, int acts)
\r
991 static int turbo_pad = 0;
\r
992 static unsigned char turbo_cnt[3] = { 0, 0, 0 };
\r
993 int inc = currentConfig.turbo_rate * 2;
\r
995 if (acts & 0x1000) {
\r
996 turbo_cnt[0] += inc;
\r
997 if (turbo_cnt[0] >= 60)
\r
998 turbo_pad ^= 0x10, turbo_cnt[0] = 0;
\r
1000 if (acts & 0x2000) {
\r
1001 turbo_cnt[1] += inc;
\r
1002 if (turbo_cnt[1] >= 60)
\r
1003 turbo_pad ^= 0x20, turbo_cnt[1] = 0;
\r
1005 if (acts & 0x4000) {
\r
1006 turbo_cnt[2] += inc;
\r
1007 if (turbo_cnt[2] >= 60)
\r
1008 turbo_pad ^= 0x40, turbo_cnt[2] = 0;
\r
1010 *pad |= turbo_pad & (acts >> 8);
\r
1013 static void run_events_ui(unsigned int which)
\r
1015 if (which & (PEV_STATE_LOAD|PEV_STATE_SAVE))
\r
1018 if ( emu_check_save_file(state_slot, NULL) &&
\r
1019 (((which & PEV_STATE_LOAD) && (currentConfig.confirm_save & EOPT_CONFIRM_LOAD)) ||
\r
1020 ((which & PEV_STATE_SAVE) && (currentConfig.confirm_save & EOPT_CONFIRM_SAVE))) )
\r
1026 strcpy(tmp, (which & PEV_STATE_LOAD) ? "LOAD STATE?" : "OVERWRITE SAVE?");
\r
1027 len = strlen(tmp);
\r
1028 nm = in_get_key_name(-1, -PBTN_MA3);
\r
1029 snprintf(tmp + len, sizeof(tmp) - len, "(%s=yes, ", nm);
\r
1030 len = strlen(tmp);
\r
1031 nm = in_get_key_name(-1, -PBTN_MBACK);
\r
1032 snprintf(tmp + len, sizeof(tmp) - len, "%s=no)", nm);
\r
1034 plat_status_msg_busy_first(tmp);
\r
1036 in_set_config_int(0, IN_CFG_BLOCKING, 1);
\r
1037 while (in_menu_wait_any(NULL, 50) & (PBTN_MA3|PBTN_MBACK))
\r
1039 while ( !((keys = in_menu_wait_any(NULL, 50)) & (PBTN_MA3|PBTN_MBACK)) )
\r
1041 if (keys & PBTN_MBACK)
\r
1043 while (in_menu_wait_any(NULL, 50) & (PBTN_MA3|PBTN_MBACK))
\r
1045 in_set_config_int(0, IN_CFG_BLOCKING, 0);
\r
1048 plat_status_msg_busy_first((which & PEV_STATE_LOAD) ? "LOADING STATE" : "SAVING STATE");
\r
1049 PicoStateProgressCB = plat_status_msg_busy_next;
\r
1050 emu_save_load_game((which & PEV_STATE_LOAD) ? 1 : 0, 0);
\r
1051 PicoStateProgressCB = NULL;
\r
1054 if (which & PEV_SWITCH_RND)
\r
1056 plat_video_toggle_renderer(1, 0);
\r
1058 if (which & (PEV_SSLOT_PREV|PEV_SSLOT_NEXT))
\r
1060 if (which & PEV_SSLOT_PREV) {
\r
1062 if (state_slot < 0)
\r
1066 if (state_slot > 9)
\r
1070 emu_status_msg("SAVE SLOT %i [%s]", state_slot,
\r
1071 emu_check_save_file(state_slot, NULL) ? "USED" : "FREE");
\r
1073 if (which & PEV_MENU)
\r
1074 engineState = PGS_Menu;
\r
1077 void emu_update_input(void)
\r
1079 static int prev_events = 0;
\r
1080 int actions[IN_BINDTYPE_COUNT] = { 0, };
\r
1081 int pl_actions[2];
\r
1084 in_update(actions);
\r
1086 pl_actions[0] = actions[IN_BINDTYPE_PLAYER12];
\r
1087 pl_actions[1] = actions[IN_BINDTYPE_PLAYER12] >> 16;
\r
1089 PicoPad[0] = pl_actions[0] & 0xfff;
\r
1090 PicoPad[1] = pl_actions[1] & 0xfff;
\r
1092 if (pl_actions[0] & 0x7000)
\r
1093 do_turbo(&PicoPad[0], pl_actions[0]);
\r
1094 if (pl_actions[1] & 0x7000)
\r
1095 do_turbo(&PicoPad[1], pl_actions[1]);
\r
1097 events = actions[IN_BINDTYPE_EMU] & PEV_MASK;
\r
1099 // volume is treated in special way and triggered every frame
\r
1100 if (events & (PEV_VOL_DOWN|PEV_VOL_UP))
\r
1101 plat_update_volume(1, events & PEV_VOL_UP);
\r
1103 if ((events ^ prev_events) & PEV_FF) {
\r
1104 emu_set_fastforward(events & PEV_FF);
\r
1105 plat_update_volume(0, 0);
\r
1109 events &= ~prev_events;
\r
1111 if (PicoAHW == PAHW_PICO)
\r
1112 run_events_pico(events);
\r
1114 run_events_ui(events);
\r
1118 prev_events = actions[IN_BINDTYPE_EMU] & PEV_MASK;
\r
1121 static void mkdir_path(char *path_with_reserve, int pos, const char *name)
\r
1123 strcpy(path_with_reserve + pos, name);
\r
1124 if (plat_is_dir(path_with_reserve))
\r
1126 if (mkdir(path_with_reserve, 0777) < 0)
\r
1127 lprintf("failed to create: %s\n", path_with_reserve);
\r
1130 void emu_cmn_forced_frame(int no_scale, int do_emu)
\r
1132 int po_old = PicoOpt;
\r
1134 memset32(g_screen_ptr, 0, g_screen_width * g_screen_height * 2 / 4);
\r
1136 PicoOpt &= ~POPT_ALT_RENDERER;
\r
1137 PicoOpt |= POPT_ACC_SPRITES;
\r
1139 PicoOpt |= POPT_EN_SOFTSCALE;
\r
1141 PicoDrawSetOutFormat(PDF_RGB555, 1);
\r
1142 Pico.m.dirtyPal = 1;
\r
1146 PicoFrameDrawOnly();
\r
1151 void emu_init(void)
\r
1157 // FIXME: handle through menu, etc
\r
1159 f = fopen("32X_M_BIOS.BIN", "rb");
\r
1160 p32x_bios_m = malloc(2048);
\r
1161 fread(p32x_bios_m, 1, 2048, f);
\r
1163 f = fopen("32X_S_BIOS.BIN", "rb");
\r
1164 p32x_bios_s = malloc(1024);
\r
1165 fread(p32x_bios_s, 1, 1024, f);
\r
1169 /* make dirs for saves */
\r
1170 pos = plat_get_root_dir(path, sizeof(path) - 4);
\r
1171 mkdir_path(path, pos, "mds");
\r
1172 mkdir_path(path, pos, "srm");
\r
1173 mkdir_path(path, pos, "brm");
\r
1174 mkdir_path(path, pos, "cfg");
\r
1178 make_config_cfg(path);
\r
1179 config_readlrom(path);
\r
1182 PicoMessage = plat_status_msg_busy_next;
\r
1183 PicoMCDopenTray = emu_tray_open;
\r
1184 PicoMCDcloseTray = emu_tray_close;
\r
1189 void emu_finish(void)
\r
1192 if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {
\r
1193 emu_save_load_game(0, 1);
\r
1197 if (!(currentConfig.EmuOpt & EOPT_NO_AUTOSVCFG)) {
\r
1199 make_config_cfg(cfg);
\r
1200 config_writelrom(cfg);
\r
1212 static void snd_write_nonblocking(int len)
\r
1214 sndout_write_nb(PsndOut, len);
\r
1217 void emu_sound_start(void)
\r
1221 if (currentConfig.EmuOpt & EOPT_EN_SOUND)
\r
1223 int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0;
\r
1225 PsndRerate(Pico.m.frame_count ? 1 : 0);
\r
1227 printf("starting audio: %i len: %i stereo: %i, pal: %i\n",
\r
1228 PsndRate, PsndLen, is_stereo, Pico.m.pal);
\r
1229 sndout_start(PsndRate, is_stereo);
\r
1230 PicoWriteSound = snd_write_nonblocking;
\r
1231 plat_update_volume(0, 0);
\r
1232 memset(sndBuffer, 0, sizeof(sndBuffer));
\r
1233 PsndOut = sndBuffer;
\r
1237 void emu_sound_stop(void)
\r
1242 void emu_sound_wait(void)
\r
1247 static void emu_loop_prep(void)
\r
1249 static int pal_old = -1;
\r
1250 static int filter_old = -1;
\r
1252 if (currentConfig.CPUclock != plat_target_cpu_clock_get())
\r
1253 plat_target_cpu_clock_set(currentConfig.CPUclock);
\r
1255 if (Pico.m.pal != pal_old) {
\r
1256 plat_target_lcdrate_set(Pico.m.pal);
\r
1257 pal_old = Pico.m.pal;
\r
1260 if (currentConfig.filter != filter_old) {
\r
1261 plat_target_hwfilter_set(currentConfig.filter);
\r
1262 filter_old = currentConfig.filter;
\r
1265 plat_target_gamma_set(currentConfig.gamma, 0);
\r
1270 static void skip_frame(int do_audio)
\r
1272 PicoSkipFrame = do_audio ? 1 : 2;
\r
1274 PicoSkipFrame = 0;
\r
1277 /* our tick here is 1 us right now */
\r
1278 #define ms_to_ticks(x) (unsigned int)(x * 1000)
\r
1279 #define get_ticks() plat_get_ticks_us()
\r
1281 void emu_loop(void)
\r
1283 int pframes_done; /* "period" frames, used for sync */
\r
1284 int frames_done, frames_shown; /* actual frames for fps counter */
\r
1285 int target_fps, target_frametime;
\r
1286 unsigned int timestamp_base = 0, timestamp_fps;
\r
1287 char *notice_msg = NULL;
\r
1293 PicoLoopPrepare();
\r
1295 // prepare CD buffer
\r
1296 if (PicoAHW & PAHW_MCD)
\r
1297 PicoCDBufferInit();
\r
1299 plat_video_loop_prepare();
\r
1301 pemu_sound_start();
\r
1303 /* number of ticks per frame */
\r
1306 target_frametime = ms_to_ticks(1000) / 50;
\r
1309 target_frametime = ms_to_ticks(1000) / 60 + 1;
\r
1312 timestamp_fps = get_ticks();
\r
1315 frames_done = frames_shown = pframes_done = 0;
\r
1317 plat_video_wait_vsync();
\r
1319 /* loop with resync every 1 sec. */
\r
1320 while (engineState == PGS_Running)
\r
1322 unsigned int timestamp;
\r
1323 int diff, diff_lim;
\r
1325 pprof_start(main);
\r
1327 timestamp = get_ticks();
\r
1328 if (reset_timing) {
\r
1330 timestamp_base = timestamp;
\r
1334 // show notice_msg message?
\r
1335 if (notice_msg_time != 0)
\r
1337 static int noticeMsgSum;
\r
1338 if (timestamp - ms_to_ticks(notice_msg_time) > ms_to_ticks(STATUS_MSG_TIMEOUT)) {
\r
1339 notice_msg_time = 0;
\r
1340 plat_status_msg_clear();
\r
1341 notice_msg = NULL;
\r
1343 int sum = noticeMsg[0] + noticeMsg[1] + noticeMsg[2];
\r
1344 if (sum != noticeMsgSum) {
\r
1345 plat_status_msg_clear();
\r
1346 noticeMsgSum = sum;
\r
1348 notice_msg = noticeMsg;
\r
1352 // second changed?
\r
1353 if (timestamp - timestamp_fps >= ms_to_ticks(1000))
\r
1356 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
\r
1357 if (++bench == 10) {
\r
1359 bench_fps_s = bench_fps;
\r
1360 bf[bfp++ & 3] = bench_fps;
\r
1363 bench_fps += frames_shown;
\r
1364 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
\r
1365 printf("%s\n", fpsbuff);
\r
1367 if (currentConfig.EmuOpt & EOPT_SHOW_FPS) {
\r
1368 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
\r
1369 if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }
\r
1372 frames_shown = frames_done = 0;
\r
1373 timestamp_fps += ms_to_ticks(1000);
\r
1376 sprintf(fpsbuff, "%i", Pico.m.frame_count);
\r
1379 if (timestamp - timestamp_base >= ms_to_ticks(1000))
\r
1381 if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && currentConfig.Frameskip >= 0)
\r
1384 pframes_done -= target_fps;
\r
1385 if (pframes_done < -2) {
\r
1386 /* don't drag more than 2 frames behind */
\r
1387 pframes_done = -2;
\r
1388 timestamp_base = timestamp - 2 * target_frametime;
\r
1391 timestamp_base += ms_to_ticks(1000);
\r
1394 diff = timestamp - timestamp_base;
\r
1395 diff_lim = (pframes_done + 1) * target_frametime;
\r
1397 if (currentConfig.Frameskip >= 0) // frameskip enabled
\r
1399 for (i = 0; i < currentConfig.Frameskip; i++) {
\r
1400 emu_update_input();
\r
1402 pframes_done++; frames_done++;
\r
1403 diff_lim += target_frametime;
\r
1405 if (!(currentConfig.EmuOpt & (EOPT_NO_FRMLIMIT|EOPT_EXT_FRMLIMIT))) {
\r
1406 timestamp = get_ticks();
\r
1407 diff = timestamp - timestamp_base;
\r
1408 if (!reset_timing && diff < diff_lim) // we are too fast
\r
1409 plat_wait_till_us(timestamp_base + diff_lim);
\r
1413 else if (diff > diff_lim)
\r
1415 /* no time left for this frame - skip */
\r
1416 /* limit auto frameskip to 8 */
\r
1417 if (frames_done / 8 <= frames_shown) {
\r
1418 emu_update_input();
\r
1419 skip_frame(diff < diff_lim + target_frametime * 16);
\r
1420 pframes_done++; frames_done++;
\r
1425 emu_update_input();
\r
1427 pemu_finalize_frame(fpsbuff, notice_msg);
\r
1429 // plat_video_flip();
\r
1431 /* frame limiter */
\r
1432 if (!reset_timing && !(currentConfig.EmuOpt & (EOPT_NO_FRMLIMIT|EOPT_EXT_FRMLIMIT)))
\r
1434 timestamp = get_ticks();
\r
1435 diff = timestamp - timestamp_base;
\r
1437 // sleep or vsync if we are still too fast
\r
1438 if (diff < diff_lim)
\r
1440 // we are too fast
\r
1441 plat_wait_till_us(timestamp_base + diff_lim - target_frametime / 4);
\r
1442 if (currentConfig.EmuOpt & EOPT_VSYNC)
\r
1443 plat_video_wait_vsync();
\r
1447 // XXX: for some plats it might be better to flip before vsync
\r
1448 // (due to shadow registers in display hw)
\r
1449 plat_video_flip();
\r
1451 pframes_done++; frames_done++; frames_shown++;
\r
1456 emu_set_fastforward(0);
\r
1459 if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {
\r
1460 plat_status_msg_busy_first("Writing SRAM/BRAM...");
\r
1461 emu_save_load_game(0, 1);
\r
1468 // pemu_loop_end() might want to do 1 frame for bg image,
\r
1469 // so free CD buffer here
\r
1470 if (PicoAHW & PAHW_MCD)
\r
1471 PicoCDBufferFree();
\r