3 * (C) notaz, 2007-2010
\r
4 * (C) irixxxx, 2019-2024
\r
6 * This work is licensed under the terms of MAME license.
\r
7 * See COPYING file in the top-level directory.
\r
18 #include "../libpicofe/posix.h"
\r
19 #include "../libpicofe/input.h"
\r
20 #include "../libpicofe/fonts.h"
\r
21 #include "../libpicofe/sndout.h"
\r
22 #include "../libpicofe/lprintf.h"
\r
23 #include "../libpicofe/readpng.h"
\r
24 #include "../libpicofe/plat.h"
\r
26 #include "keyboard.h"
\r
27 #include "input_pico.h"
\r
28 #include "menu_pico.h"
\r
29 #include "config_file.h"
\r
31 #include <pico/pico_int.h>
\r
32 #include <pico/patch.h>
\r
34 #if defined(__GNUC__) && __GNUC__ >= 7
\r
35 #pragma GCC diagnostic ignored "-Wformat-truncation"
\r
39 #define PATH_SEP "/"
\r
40 #define PATH_SEP_C '/'
\r
42 #define PATH_SEP "\\"
\r
43 #define PATH_SEP_C '\\'
\r
46 #define STATUS_MSG_TIMEOUT 2000
\r
50 int g_screen_width = 320;
\r
51 int g_screen_height = 240;
\r
52 int g_screen_ppitch = 320; // pitch in pixels
\r
54 const char *PicoConfigFile = "config2.cfg";
\r
55 currentConfig_t currentConfig, defaultConfig;
\r
57 int config_slot = 0, config_slot_current = 0;
\r
58 int pico_pen_x = 320/2, pico_pen_y = 240/2;
\r
60 int flip_after_sync;
\r
61 int engineState = PGS_Menu;
\r
66 int mouse_x, mouse_y;
\r
68 static int pico_page;
\r
69 static int pico_w, pico_h;
\r
70 static u16 *pico_overlay;
\r
71 static int pico_pad;
\r
73 static short __attribute__((aligned(4))) sndBuffer[2*54000/50];
\r
75 /* tmp buff to reduce stack usage for plats with small stack */
\r
76 static char static_buff[512];
\r
77 const char *rom_fname_reload;
\r
78 char rom_fname_loaded[512];
\r
79 int reset_timing = 0;
\r
80 static unsigned int notice_msg_time; /* when started showing */
\r
81 static char noticeMsg[40];
\r
83 unsigned char *movie_data = NULL;
\r
84 static int movie_size = 0;
\r
87 /* don't use tolower() for easy old glibc binary compatibility */
\r
88 static void strlwr_(char *string)
\r
91 for (p = string; *p; p++)
\r
92 if ('A' <= *p && *p <= 'Z')
\r
96 static int try_rfn_cut(char *fname)
\r
101 p = fname + strlen(fname) - 1;
\r
102 for (; p > fname; p--)
\r
103 if (*p == '.') break;
\r
106 if((tmp = fopen(fname, "rb"))) {
\r
113 static void get_ext(const char *file, char *ext)
\r
117 p = file + strlen(file) - 4;
\r
118 if (p < file) p = file;
\r
119 strncpy(ext, p, 4);
\r
124 static void fname_ext(char *dst, int dstlen, const char *prefix, const char *ext, const char *fname)
\r
126 int prefix_len = 0;
\r
131 int len = plat_get_root_dir(dst, dstlen);
\r
132 strcpy(dst + len, prefix);
\r
133 prefix_len = len + strlen(prefix);
\r
136 p = fname + strlen(fname) - 1;
\r
137 for (; p >= fname && *p != PATH_SEP_C; p--)
\r
140 strncpy(dst + prefix_len, p, dstlen - prefix_len - 1);
\r
142 dst[dstlen - 8] = 0;
\r
143 if ((p = strrchr(dst, '.')) != NULL)
\r
149 static void romfname_ext(char *dst, int dstlen, const char *prefix, const char *ext)
\r
151 fname_ext(dst, dstlen, prefix, ext, rom_fname_loaded);
\r
154 void emu_status_msg(const char *format, ...)
\r
159 va_start(vl, format);
\r
160 ret = vsnprintf(noticeMsg, sizeof(noticeMsg), format, vl);
\r
163 /* be sure old text gets overwritten */
\r
164 for (; ret < 28; ret++)
\r
165 noticeMsg[ret] = ' ';
\r
166 noticeMsg[ret] = 0;
\r
168 notice_msg_time = plat_get_ticks_ms();
\r
171 static const char * const biosfiles_us[] = {
\r
172 "us_scd2_9306", "SegaCDBIOS9303", "us_scd1_9210", "bios_CD_U"
\r
174 static const char * const biosfiles_eu[] = {
\r
175 "eu_mcd2_9306", "eu_mcd2_9303", "eu_mcd1_9210", "bios_CD_E"
\r
177 static const char * const biosfiles_jp[] = {
\r
178 "jp_mcd2_921222", "jp_mcd1_9112", "jp_mcd1_9111", "bios_CD_J"
\r
181 static const char *find_bios(int *region, const char *cd_fname)
\r
184 const char * const *files;
\r
188 // we need to have config loaded at this point
\r
189 ret = emu_read_config(cd_fname, 0);
\r
190 if (!ret) emu_read_config(NULL, 0);
\r
192 if (PicoIn.regionOverride) {
\r
193 *region = PicoIn.regionOverride;
\r
194 lprintf("override region to %s\n", *region != 4 ?
\r
195 (*region == 8 ? "EU" : "JAP") : "USA");
\r
198 // locate BIOS file
\r
199 if (*region == 4) { // US
\r
200 files = biosfiles_us;
\r
201 count = sizeof(biosfiles_us) / sizeof(char *);
\r
202 } else if (*region == 8) { // EU
\r
203 files = biosfiles_eu;
\r
204 count = sizeof(biosfiles_eu) / sizeof(char *);
\r
205 } else if (*region == 1 || *region == 2) {
\r
206 files = biosfiles_jp;
\r
207 count = sizeof(biosfiles_jp) / sizeof(char *);
\r
212 for (i = 0; i < count; i++)
\r
214 emu_make_path(static_buff, files[i], sizeof(static_buff) - 4);
\r
215 strcat(static_buff, ".bin");
\r
216 f = fopen(static_buff, "rb");
\r
219 static_buff[strlen(static_buff) - 4] = 0;
\r
220 strcat(static_buff, ".zip");
\r
221 f = fopen(static_buff, "rb");
\r
224 strcpy(static_buff, files[i]);
\r
225 strcat(static_buff, ".bin");
\r
226 f = fopen(static_buff, "rb");
\r
229 static_buff[strlen(static_buff) - 4] = 0;
\r
230 strcat(static_buff, ".zip");
\r
231 f = fopen(static_buff, "rb");
\r
236 lprintf("using bios: %s\n", static_buff);
\r
238 return static_buff;
\r
240 sprintf(static_buff, "no %s BIOS files found, read docs",
\r
241 *region != 4 ? (*region == 8 ? "EU" : "JAP") : "USA");
\r
242 menu_update_msg(static_buff);
\r
247 static const char *find_msu(const char *cd_fname)
\r
251 // look for MSU.MD or MD+ rom file. XXX another extension list? ugh...
\r
252 static const char *md_exts[] = { "gen", "smd", "md", "32x" };
\r
253 char *ext = strrchr(cd_fname, '.');
\r
254 int extpos = ext ? ext-cd_fname : strlen(cd_fname);
\r
255 strcpy(static_buff, cd_fname);
\r
256 static_buff[extpos++] = '.';
\r
257 for (i = 0; i < ARRAY_SIZE(md_exts); i++) {
\r
258 strcpy(static_buff+extpos, md_exts[i]);
\r
259 if (access(static_buff, R_OK) == 0) {
\r
260 printf("found MSU rom: %s\n",static_buff);
\r
261 return static_buff;
\r
267 /* check if the name begins with BIOS name */
\r
269 static int emu_isBios(const char *name)
\r
272 for (i = 0; i < sizeof(biosfiles_us)/sizeof(biosfiles_us[0]); i++)
\r
273 if (strstr(name, biosfiles_us[i]) != NULL) return 1;
\r
274 for (i = 0; i < sizeof(biosfiles_eu)/sizeof(biosfiles_eu[0]); i++)
\r
275 if (strstr(name, biosfiles_eu[i]) != NULL) return 1;
\r
276 for (i = 0; i < sizeof(biosfiles_jp)/sizeof(biosfiles_jp[0]); i++)
\r
277 if (strstr(name, biosfiles_jp[i]) != NULL) return 1;
\r
282 static int extract_text(char *dest, const unsigned char *src, int len, int swab)
\r
287 if (swab) swab = 1;
\r
289 for (i = len - 1; i >= 0; i--)
\r
291 if (src[i^swab] != ' ') break;
\r
295 for (i = 0; i < len; i++)
\r
297 unsigned char s = src[i^swab];
\r
298 if (s >= 0x20 && s < 0x7f && s != '#' && s != '|' &&
\r
299 s != '[' && s != ']' && s != '\\')
\r
305 sprintf(p, "\\%02x", s);
\r
313 static char *emu_make_rom_id(const char *fname)
\r
315 static char id_string[3+0xe*3+0x3*3+0x30*3+3];
\r
318 if (PicoIn.AHW & PAHW_MCD) {
\r
319 strcpy(id_string, "CD|");
\r
322 else if (PicoIn.AHW & PAHW_SMS)
\r
323 strcpy(id_string, "MS|");
\r
324 else strcpy(id_string, "MD|");
\r
327 if (!(PicoIn.AHW & PAHW_SMS)) {
\r
328 pos += extract_text(id_string + pos, media_id_header + 0x80, 0x0e, swab); // serial
\r
329 id_string[pos] = '|'; pos++;
\r
330 pos += extract_text(id_string + pos, media_id_header + 0xf0, 0x03, swab); // region
\r
331 id_string[pos] = '|'; pos++;
\r
332 pos += extract_text(id_string + pos, media_id_header + 0x50, 0x30, swab); // overseas name
\r
333 id_string[pos] = 0;
\r
339 // can't find name in ROM, use filename
\r
340 fname_ext(id_string + 3, sizeof(id_string) - 3, NULL, NULL, fname);
\r
345 // buffer must be at least 150 byte long
\r
346 void emu_get_game_name(char *str150)
\r
348 int ret, swab = (PicoIn.AHW & PAHW_MCD) ? 0 : 1;
\r
351 ret = extract_text(str150, media_id_header + 0x50, 0x30, swab); // overseas name
\r
353 for (s = d = str150 + 1; s < str150+ret; s++)
\r
355 if (*s == 0) break;
\r
356 if (*s != ' ' || d[-1] != ' ')
\r
362 static void system_announce(void)
\r
364 const char *sys_name, *tv_standard, *extra = "";
\r
367 if (PicoIn.AHW & PAHW_SMS) {
\r
368 sys_name = "Master System";
\r
369 if (PicoIn.AHW & PAHW_GG)
\r
370 sys_name = "Game Gear";
\r
371 else if (PicoIn.AHW & PAHW_SG)
\r
372 sys_name = "SG-1000";
\r
373 else if (PicoIn.AHW & PAHW_SC)
\r
374 sys_name = "SC-3000";
\r
375 else if (Pico.m.hardware & PMS_HW_JAP)
\r
376 sys_name = "Mark III";
\r
378 extra = " [no support]";
\r
380 } else if (PicoIn.AHW & PAHW_PICO) {
\r
382 } else if ((PicoIn.AHW & (PAHW_32X|PAHW_MCD)) == (PAHW_32X|PAHW_MCD)) {
\r
383 sys_name = "32X + Mega CD";
\r
384 if ((Pico.m.hardware & 0xc0) == 0x80)
\r
385 sys_name = "32X + Sega CD";
\r
386 } else if (PicoIn.AHW & PAHW_MCD) {
\r
387 sys_name = "Mega CD";
\r
388 if ((Pico.m.hardware & 0xc0) == 0x80)
\r
389 sys_name = "Sega CD";
\r
390 } else if (PicoIn.AHW & PAHW_32X) {
\r
393 sys_name = "Mega Drive";
\r
394 if ((Pico.m.hardware & 0xc0) == 0x80)
\r
395 sys_name = "Genesis";
\r
397 tv_standard = Pico.m.pal ? "PAL" : "NTSC";
\r
398 fps = Pico.m.pal ? 50 : 60;
\r
400 emu_status_msg("%s %s / %dFPS%s", tv_standard, sys_name, fps, extra);
\r
403 static void do_region_override(const char *media_fname)
\r
405 // we only need to override region if config tells us so
\r
406 int ret = emu_read_config(media_fname, 0);
\r
407 if (!ret) emu_read_config(NULL, 0);
\r
410 int emu_reload_rom(const char *rom_fname_in)
\r
412 // use setting before rom config is loaded
\r
413 int autoload = g_autostateld_opt;
\r
414 char *rom_fname = NULL;
\r
416 enum media_type_e media_type;
\r
417 int menu_romload_started = 0;
\r
418 char carthw_path[512];
\r
421 lprintf("emu_ReloadRom(%s)\n", rom_fname_in);
\r
423 rom_fname = strdup(rom_fname_in);
\r
424 if (rom_fname == NULL)
\r
427 get_ext(rom_fname, ext);
\r
436 if (!strcasecmp(ext, ".gmv"))
\r
438 // check for both gmv and rom
\r
440 FILE *movie_file = fopen(rom_fname, "rb");
\r
442 menu_update_msg("Failed to open movie.");
\r
445 fseek(movie_file, 0, SEEK_END);
\r
446 movie_size = ftell(movie_file);
\r
447 fseek(movie_file, 0, SEEK_SET);
\r
448 if (movie_size < 64+3) {
\r
449 menu_update_msg("Invalid GMV file.");
\r
450 fclose(movie_file);
\r
453 movie_data = malloc(movie_size);
\r
454 if (movie_data == NULL) {
\r
455 menu_update_msg("low memory.");
\r
456 fclose(movie_file);
\r
459 dummy = fread(movie_data, 1, movie_size, movie_file);
\r
460 fclose(movie_file);
\r
461 if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {
\r
462 menu_update_msg("Invalid GMV file.");
\r
465 dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);
\r
467 menu_update_msg("Could't find a ROM for movie.");
\r
470 get_ext(rom_fname, ext);
\r
471 lprintf("gmv loaded for %s\n", rom_fname);
\r
473 else if (!strcasecmp(ext, ".pat"))
\r
476 PicoPatchLoad(rom_fname);
\r
477 dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);
\r
479 menu_update_msg("Could't find a ROM to patch.");
\r
482 get_ext(rom_fname, ext);
\r
485 menu_romload_prepare(rom_fname); // also CD load
\r
486 menu_romload_started = 1;
\r
488 emu_make_path(carthw_path, "carthw.cfg", sizeof(carthw_path));
\r
490 media_type = PicoLoadMedia(rom_fname, NULL, 0, carthw_path,
\r
491 find_bios, find_msu, do_region_override);
\r
493 switch (media_type) {
\r
494 case PM_BAD_DETECT:
\r
495 menu_update_msg("Not a ROM/CD img selected.");
\r
498 menu_update_msg("Invalid CD image");
\r
500 case PM_BAD_CD_NO_BIOS:
\r
501 // find_bios() prints a message
\r
504 menu_update_msg("Load error");
\r
510 // make quirks visible in UI
\r
511 if (PicoIn.quirks & PQUIRK_FORCE_6BTN)
\r
512 currentConfig.input_dev0 = PICO_INPUT_PAD_6BTN;
\r
514 menu_romload_end();
\r
515 menu_romload_started = 0;
\r
518 PicoPatchPrepare();
\r
522 // additional movie stuff
\r
525 enum input_device indev = (movie_data[0x14] == '6') ?
\r
526 PICO_INPUT_PAD_6BTN : PICO_INPUT_PAD_3BTN;
\r
527 PicoSetInputDevice(0, indev);
\r
528 PicoSetInputDevice(1, indev);
\r
530 PicoIn.opt |= POPT_DIS_VDP_FIFO; // no VDP fifo timing
\r
531 if (movie_data[0xF] >= 'A') {
\r
532 if (movie_data[0x16] & 0x80) {
\r
533 PicoIn.regionOverride = 8;
\r
535 PicoIn.regionOverride = 4;
\r
538 // TODO: bits 6 & 5
\r
540 movie_data[0x18+30] = 0;
\r
541 emu_status_msg("MOVIE: %s", (char *) &movie_data[0x18]);
\r
545 PicoSetInputDevice(0, currentConfig.input_dev0);
\r
546 PicoSetInputDevice(1, currentConfig.input_dev1);
\r
549 PicoIn.opt &= ~POPT_DIS_VDP_FIFO;
\r
552 strncpy(rom_fname_loaded, rom_fname, sizeof(rom_fname_loaded)-1);
\r
553 rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0;
\r
555 // load SRAM for this ROM
\r
556 if (currentConfig.EmuOpt & EOPT_EN_SRAM)
\r
557 emu_save_load_game(1, 1);
\r
561 int time, newest = 0, newest_slot = -1;
\r
564 for (slot = 0; slot < 10; slot++) {
\r
565 if (emu_check_save_file(slot, &time)) {
\r
566 if (time > newest) {
\r
568 newest_slot = slot;
\r
573 if (newest_slot >= 0) {
\r
574 lprintf("autoload slot %d\n", newest_slot);
\r
575 state_slot = newest_slot;
\r
576 emu_save_load_game(1, 0);
\r
579 lprintf("no save to autoload.\n");
\r
585 if (menu_romload_started)
\r
586 menu_romload_end();
\r
591 int emu_swap_cd(const char *fname)
\r
593 enum cd_track_type cd_type;
\r
596 cd_type = PicoCdCheck(fname, NULL);
\r
597 if (cd_type != CT_UNKNOWN)
\r
598 ret = cdd_load(fname, cd_type);
\r
600 menu_update_msg("Load failed, invalid CD image?");
\r
604 strncpy(rom_fname_loaded, fname, sizeof(rom_fname_loaded)-1);
\r
605 rom_fname_loaded[sizeof(rom_fname_loaded) - 1] = 0;
\r
610 int emu_play_tape(const char *fname)
\r
614 ret = PicoPlayTape(fname);
\r
616 menu_update_msg("loading tape failed");
\r
622 int emu_record_tape(const char *ext)
\r
626 fname_ext(static_buff, sizeof(static_buff), "tape"PATH_SEP, ext, rom_fname_loaded);
\r
627 ret = PicoRecordTape(static_buff);
\r
629 menu_update_msg("recording tape failed");
\r
636 void emu_make_path(char *buff, const char *end, int size)
\r
640 end_len = strlen(end);
\r
641 pos = plat_get_root_dir(buff, size);
\r
642 strncpy(buff + pos, end, size - pos);
\r
643 buff[size - 1] = 0;
\r
644 if (pos + end_len > size - 1)
\r
645 lprintf("Warning: path truncated: %s\n", buff);
\r
648 static void make_config_cfg(char *cfg_buff_512)
\r
650 emu_make_path(cfg_buff_512, PicoConfigFile, 512-6);
\r
651 if (config_slot != 0)
\r
653 char *p = strrchr(cfg_buff_512, '.');
\r
655 p = cfg_buff_512 + strlen(cfg_buff_512);
\r
656 sprintf(p, ".%i.cfg", config_slot);
\r
658 cfg_buff_512[511] = 0;
\r
661 void emu_prep_defconfig(void)
\r
663 memset(&defaultConfig, 0, sizeof(defaultConfig));
\r
664 defaultConfig.EmuOpt = EOPT_EN_SRAM | EOPT_EN_SOUND | EOPT_16BPP |
\r
665 EOPT_EN_CD_LEDS | EOPT_GZIP_SAVES | EOPT_PICO_PEN;
\r
666 defaultConfig.s_PicoOpt = POPT_EN_SNDFILTER|POPT_EN_GG_LCD|POPT_EN_YM2413 |
\r
667 POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80 |
\r
668 POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_MCD_GFX |
\r
669 POPT_EN_DRC|POPT_ACC_SPRITES |
\r
670 POPT_EN_32X|POPT_EN_PWM;
\r
671 defaultConfig.s_PsndRate = 44100;
\r
672 defaultConfig.s_PicoRegion = 0; // auto
\r
673 defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP
\r
674 defaultConfig.s_hwSelect = PHWS_AUTO;
\r
675 defaultConfig.s_PicoCDBuffers = 0;
\r
676 defaultConfig.s_PicoSndFilterAlpha = 0x10000 * 60 / 100;
\r
677 defaultConfig.confirm_save = EOPT_CONFIRM_SAVE;
\r
678 defaultConfig.Frameskip = -1; // auto
\r
679 defaultConfig.input_dev0 = PICO_INPUT_PAD_3BTN;
\r
680 defaultConfig.input_dev1 = PICO_INPUT_PAD_3BTN;
\r
681 defaultConfig.volume = 50;
\r
682 defaultConfig.gamma = 100;
\r
683 defaultConfig.scaling = 0;
\r
684 defaultConfig.turbo_rate = 15;
\r
685 defaultConfig.msh2_khz = PICO_MSH2_HZ / 1000;
\r
686 defaultConfig.ssh2_khz = PICO_SSH2_HZ / 1000;
\r
687 defaultConfig.max_skip = 4;
\r
689 // platform specific overrides
\r
690 pemu_prep_defconfig();
\r
693 void emu_set_defconfig(void)
\r
695 memcpy(¤tConfig, &defaultConfig, sizeof(currentConfig));
\r
696 PicoIn.opt = currentConfig.s_PicoOpt;
\r
697 PicoIn.sndRate = currentConfig.s_PsndRate;
\r
698 PicoIn.regionOverride = currentConfig.s_PicoRegion;
\r
699 PicoIn.autoRgnOrder = currentConfig.s_PicoAutoRgnOrder;
\r
700 PicoIn.hwSelect = currentConfig.s_hwSelect;
\r
701 PicoIn.sndFilterAlpha = currentConfig.s_PicoSndFilterAlpha;
\r
704 int emu_read_config(const char *rom_fname, int no_defaults)
\r
710 emu_set_defconfig();
\r
712 if (rom_fname == NULL)
\r
715 make_config_cfg(cfg);
\r
716 ret = config_readsect(cfg, NULL);
\r
723 if (config_slot != 0)
\r
724 snprintf(ext, sizeof(ext), ".%i.cfg", config_slot);
\r
726 strcpy(ext, ".cfg");
\r
728 fname_ext(cfg, sizeof(cfg), "cfg"PATH_SEP, ext, rom_fname);
\r
730 // read user's config
\r
731 vol = currentConfig.volume;
\r
732 ret = config_readsect(cfg, NULL);
\r
733 currentConfig.volume = vol; // make vol global (bah)
\r
737 // read global config, and apply game_def.cfg on top
\r
738 make_config_cfg(cfg);
\r
739 config_readsect(cfg, NULL);
\r
741 emu_make_path(cfg, "game_def.cfg", sizeof(cfg));
\r
742 ret = config_readsect(cfg, emu_make_rom_id(rom_fname));
\r
746 pemu_validate_config();
\r
747 PicoIn.overclockM68k = currentConfig.overclock_68k;
\r
749 // some sanity checks
\r
750 if (currentConfig.volume < 0 || currentConfig.volume > 99)
\r
751 currentConfig.volume = 50;
\r
754 config_slot_current = config_slot;
\r
760 int emu_write_config(int is_game)
\r
763 int ret, write_lrom = 0;
\r
767 make_config_cfg(cfg);
\r
772 if (config_slot != 0)
\r
773 snprintf(ext, sizeof(ext), ".%i.cfg", config_slot);
\r
775 strcpy(ext, ".cfg");
\r
777 romfname_ext(cfg, sizeof(cfg), "cfg"PATH_SEP, ext);
\r
780 lprintf("emu_write_config: %s ", cfg);
\r
781 ret = config_write(cfg);
\r
782 if (write_lrom) config_writelrom(cfg);
\r
786 lprintf((ret == 0) ? "(ok)\n" : "(failed)\n");
\r
788 if (ret == 0) config_slot_current = config_slot;
\r
793 /* always using built-in font */
\r
795 #define mk_text_out(name, type, val, topleft, step_x, step_y) \
\r
796 void name(int x, int y, const char *text) \
\r
798 int i, l, len = strlen(text); \
\r
799 type *screen = (type *)(topleft) + x * step_x + y * step_y; \
\r
801 for (i = 0; i < len; i++, screen += 8 * step_x) \
\r
803 for (l = 0; l < 8; l++) \
\r
805 unsigned char fd = fontdata8x8[text[i] * 8 + l];\
\r
806 type *s = screen + l * step_y; \
\r
807 if (fd&0x80) s[step_x * 0] = val; \
\r
808 if (fd&0x40) s[step_x * 1] = val; \
\r
809 if (fd&0x20) s[step_x * 2] = val; \
\r
810 if (fd&0x10) s[step_x * 3] = val; \
\r
811 if (fd&0x08) s[step_x * 4] = val; \
\r
812 if (fd&0x04) s[step_x * 5] = val; \
\r
813 if (fd&0x02) s[step_x * 6] = val; \
\r
814 if (fd&0x01) s[step_x * 7] = val; \
\r
819 mk_text_out(emu_text_out8, unsigned char, 0xf0, g_screen_ptr, 1, g_screen_ppitch)
\r
820 mk_text_out(emu_text_out16, unsigned short, 0xffff, g_screen_ptr, 1, g_screen_ppitch)
\r
821 mk_text_out(emu_text_out8_rot, unsigned char, 0xf0,
\r
822 (char *)g_screen_ptr + (g_screen_ppitch - 1) * g_screen_height, -g_screen_height, 1)
\r
823 mk_text_out(emu_text_out16_rot, unsigned short, 0xffff,
\r
824 (short *)g_screen_ptr + (g_screen_ppitch - 1) * g_screen_height, -g_screen_height, 1)
\r
828 void emu_osd_text16(int x, int y, const char *text)
\r
830 int len = strlen(text) * 8;
\r
834 if (x + len > g_screen_width)
\r
835 len = g_screen_width - x;
\r
837 for (h = 0; h < 8; h++) {
\r
839 p = (unsigned short *)g_screen_ptr
\r
840 + x + g_screen_ppitch * (y + h);
\r
841 for (i = len; i > 0; i--, p++)
\r
842 *p = (*p >> 2) & 0x39e7;
\r
844 emu_text_out16(x, y, text);
\r
847 static void update_movie(void)
\r
849 int offs = Pico.m.frame_count*3 + 0x40;
\r
850 if (offs+3 > movie_size) {
\r
853 emu_status_msg("END OF MOVIE.");
\r
854 lprintf("END OF MOVIE.\n");
\r
857 PicoIn.pad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU
\r
858 if(!(movie_data[offs] & 0x10)) PicoIn.pad[0] |= 0x40; // C
\r
859 if(!(movie_data[offs] & 0x20)) PicoIn.pad[0] |= 0x10; // A
\r
860 if(!(movie_data[offs] & 0x40)) PicoIn.pad[0] |= 0x20; // B
\r
861 PicoIn.pad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU
\r
862 if(!(movie_data[offs+1] & 0x10)) PicoIn.pad[1] |= 0x40; // C
\r
863 if(!(movie_data[offs+1] & 0x20)) PicoIn.pad[1] |= 0x10; // A
\r
864 if(!(movie_data[offs+1] & 0x40)) PicoIn.pad[1] |= 0x20; // B
\r
865 PicoIn.pad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX
\r
866 if(!(movie_data[offs+2] & 0x01)) PicoIn.pad[0] |= 0x0400; // X
\r
867 if(!(movie_data[offs+2] & 0x04)) PicoIn.pad[0] |= 0x0100; // Z
\r
868 PicoIn.pad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX
\r
869 if(!(movie_data[offs+2] & 0x10)) PicoIn.pad[1] |= 0x0400; // X
\r
870 if(!(movie_data[offs+2] & 0x40)) PicoIn.pad[1] |= 0x0100; // Z
\r
874 static int try_ropen_file(const char *fname, int *time)
\r
879 f = fopen(fname, "rb");
\r
881 if (time != NULL) {
\r
883 if (fstat(fileno(f), &st) == 0)
\r
884 *time = (int)st.st_mtime;
\r
892 char *emu_get_save_fname(int load, int is_sram, int slot, int *time)
\r
894 char *saveFname = static_buff;
\r
899 strcpy(ext, (PicoIn.AHW & PAHW_MCD) && Pico.romsize == 0 ? ".brm" : ".srm");
\r
900 romfname_ext(saveFname, sizeof(static_buff),
\r
901 (PicoIn.AHW & PAHW_MCD) && Pico.romsize == 0 ? "brm"PATH_SEP : "srm"PATH_SEP, ext);
\r
905 if (try_ropen_file(saveFname, time))
\r
908 romfname_ext(saveFname, sizeof(static_buff), NULL, ext);
\r
909 if (try_ropen_file(saveFname, time))
\r
914 const char *ext_main = (currentConfig.EmuOpt & EOPT_GZIP_SAVES) ? ".mds.gz" : ".mds";
\r
915 const char *ext_othr = (currentConfig.EmuOpt & EOPT_GZIP_SAVES) ? ".mds" : ".mds.gz";
\r
917 if (slot > 0 && slot < 10)
\r
918 sprintf(ext, ".%i", slot);
\r
919 strcat(ext, ext_main);
\r
922 romfname_ext(saveFname, sizeof(static_buff), "mds" PATH_SEP, ext);
\r
926 romfname_ext(saveFname, sizeof(static_buff), "mds" PATH_SEP, ext);
\r
927 if (try_ropen_file(saveFname, time))
\r
930 romfname_ext(saveFname, sizeof(static_buff), NULL, ext);
\r
931 if (try_ropen_file(saveFname, time))
\r
934 // try the other ext
\r
936 if (slot > 0 && slot < 10)
\r
937 sprintf(ext, ".%i", slot);
\r
938 strcat(ext, ext_othr);
\r
940 romfname_ext(saveFname, sizeof(static_buff), "mds"PATH_SEP, ext);
\r
941 if (try_ropen_file(saveFname, time))
\r
949 int emu_check_save_file(int slot, int *time)
\r
951 return emu_get_save_fname(1, 0, slot, time) ? 1 : 0;
\r
954 int emu_save_load_game(int load, int sram)
\r
959 // make save filename
\r
960 saveFname = emu_get_save_fname(load, sram, state_slot, NULL);
\r
961 if (saveFname == NULL) {
\r
963 emu_status_msg(load ? "LOAD FAILED (missing file)" : "SAVE FAILED");
\r
967 lprintf("saveLoad (%i, %i): %s\n", load, sram, saveFname);
\r
973 unsigned char *sram_data;
\r
975 if ((PicoIn.AHW & PAHW_MCD) && Pico.romsize == 0)
\r
977 if (PicoIn.opt & POPT_EN_MCD_RAMCART) {
\r
978 sram_size = 0x12000;
\r
979 sram_data = Pico.sv.data;
\r
981 memcpy(sram_data, Pico_mcd->bram, 0x2000);
\r
983 sram_size = 0x2000;
\r
984 sram_data = Pico_mcd->bram;
\r
985 truncate = 0; // the .brm may contain RAM cart data after normal brm
\r
988 sram_size = Pico.sv.size;
\r
989 sram_data = Pico.sv.data;
\r
991 if (sram_data == NULL)
\r
992 return 0; // cart saves forcefully disabled for this game
\r
996 sramFile = fopen(saveFname, "rb");
\r
999 ret = fread(sram_data, 1, sram_size, sramFile);
\r
1000 ret = ret > 0 ? 0 : -1;
\r
1002 if ((PicoIn.AHW & PAHW_MCD) && Pico.romsize == 0 && (PicoIn.opt&POPT_EN_MCD_RAMCART))
\r
1003 memcpy(Pico_mcd->bram, sram_data, 0x2000);
\r
1005 // sram save needs some special processing
\r
1006 // see if we have anything to save
\r
1007 for (; sram_size > 0; sram_size--)
\r
1008 if (sram_data[sram_size-1]) break;
\r
1011 sramFile = fopen(saveFname, truncate ? "wb" : "r+b");
\r
1012 if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry
\r
1013 if (!sramFile) return -1;
\r
1014 ret = fwrite(sram_data, 1, sram_size, sramFile);
\r
1015 ret = (ret != sram_size) ? -1 : 0;
\r
1026 ret = PicoState(saveFname, !load);
\r
1029 if (!load) sync();
\r
1031 emu_status_msg(load ? "STATE LOADED" : "STATE SAVED");
\r
1033 emu_status_msg(load ? "LOAD FAILED" : "SAVE FAILED");
\r
1041 void emu_set_fastforward(int set_on)
\r
1043 static void *set_PsndOut = NULL;
\r
1044 static int set_Frameskip, set_EmuOpt, is_on = 0;
\r
1046 if (set_on && !is_on) {
\r
1047 set_PsndOut = PicoIn.sndOut;
\r
1048 set_Frameskip = currentConfig.Frameskip;
\r
1049 set_EmuOpt = currentConfig.EmuOpt;
\r
1050 PicoIn.sndOut = NULL;
\r
1051 currentConfig.Frameskip = 8;
\r
1052 currentConfig.EmuOpt &= ~EOPT_EN_SOUND;
\r
1053 currentConfig.EmuOpt |= EOPT_NO_FRMLIMIT;
\r
1055 emu_status_msg("FAST FORWARD");
\r
1057 else if (!set_on && is_on) {
\r
1058 PicoIn.sndOut = set_PsndOut;
\r
1059 currentConfig.Frameskip = set_Frameskip;
\r
1060 currentConfig.EmuOpt = set_EmuOpt;
\r
1066 static void emu_tray_open(void)
\r
1068 engineState = PGS_TrayMenu;
\r
1071 static void emu_tray_close(void)
\r
1073 emu_status_msg("CD tray closed.");
\r
1076 void emu_32x_startup(void)
\r
1078 plat_video_toggle_renderer(0, 0); // HACK
\r
1079 system_announce();
\r
1082 void emu_reset_game(void)
\r
1088 static u16 *load_pico_overlay(int page, int w, int h)
\r
1090 static const char *pic_exts[] = { "png", "PNG" };
\r
1091 char *ext, *fname = NULL;
\r
1094 if (pico_page == page && pico_w == w && pico_h == h)
\r
1095 return pico_overlay;
\r
1097 pico_w = w, pico_h = h;
\r
1099 ext = strrchr(rom_fname_loaded, '.');
\r
1100 extpos = ext ? ext-rom_fname_loaded : strlen(rom_fname_loaded);
\r
1101 strcpy(static_buff, rom_fname_loaded);
\r
1102 static_buff[extpos++] = '_';
\r
1104 static_buff[extpos++] = 'p';
\r
1105 static_buff[extpos++] = 'a';
\r
1106 static_buff[extpos++] = 'd';
\r
1108 static_buff[extpos++] = '0'+PicoPicohw.page;
\r
1109 static_buff[extpos++] = '.';
\r
1111 for (i = 0; i < ARRAY_SIZE(pic_exts); i++) {
\r
1112 strcpy(static_buff+extpos, pic_exts[i]);
\r
1113 if (access(static_buff, R_OK) == 0) {
\r
1114 printf("found Pico file: %s\n", static_buff);
\r
1115 fname = static_buff;
\r
1120 pico_overlay = realloc(pico_overlay, w*h*2);
\r
1121 memset(pico_overlay, 0, w*h*2);
\r
1122 if (!fname || !pico_overlay || readpng(pico_overlay, fname, READPNG_SCALE, w, h)) {
\r
1124 free(pico_overlay);
\r
1125 pico_overlay = NULL;
\r
1128 return pico_overlay;
\r
1131 void emu_pico_overlay(u16 *pd, int w, int h, int pitch)
\r
1133 u16 *overlay = NULL;
\r
1137 if (pico_inp_mode == 1) {
\r
1138 oh = (w/2 < h ? w/2 : h); // storyware has squished h
\r
1139 overlay = load_pico_overlay(PicoPicohw.page, w, oh);
\r
1140 } else if (pico_inp_mode == 2)
\r
1141 overlay = load_pico_overlay(-1, w, oh);
\r
1143 // copy overlay onto buffer
\r
1145 for (y = 0; y < oh; y++)
\r
1146 memcpy(pd + y*pitch, overlay + y*w, w*2);
\r
1148 memset(pd + y*pitch, 0, w*2);
\r
1152 void run_events_pico(unsigned int events)
\r
1154 // treat pad ports equal to support pad in one and mouse in the other
\r
1155 PicoIn.pad[0] |= PicoIn.pad[1];
\r
1157 if (events & PEV_PICO_PPREV) {
\r
1158 PicoPicohw.page--;
\r
1159 if (PicoPicohw.page < 0)
\r
1160 PicoPicohw.page = 0;
\r
1161 emu_status_msg("Page %i", PicoPicohw.page);
\r
1163 if (events & PEV_PICO_PNEXT) {
\r
1164 PicoPicohw.page++;
\r
1165 if (PicoPicohw.page > 7)
\r
1166 PicoPicohw.page = 7;
\r
1167 if (PicoPicohw.page == 7) {
\r
1168 // Used in games that require the Keyboard Pico peripheral
\r
1169 emu_status_msg("Test Page");
\r
1171 emu_status_msg("Page %i", PicoPicohw.page);
\r
1174 if (events & PEV_PICO_STORY) {
\r
1175 if (pico_inp_mode == 1) {
\r
1176 pico_inp_mode = 0;
\r
1177 emu_status_msg("Input: D-Pad");
\r
1179 pico_inp_mode = 1;
\r
1180 emu_status_msg("Input: Pen on Storyware");
\r
1183 if (events & PEV_PICO_PAD) {
\r
1184 if (pico_inp_mode == 2) {
\r
1185 pico_inp_mode = 0;
\r
1186 emu_status_msg("Input: D-Pad");
\r
1188 pico_inp_mode = 2;
\r
1189 emu_status_msg("Input: Pen on Pad");
\r
1193 if ((currentConfig.EmuOpt & EOPT_PICO_PEN) &&
\r
1194 (PicoIn.pad[0]&0x20) && pico_inp_mode && pico_overlay) {
\r
1195 pico_inp_mode = 0;
\r
1196 emu_status_msg("Input: D-Pad");
\r
1199 PicoPicohw.kb.active = (PicoIn.opt & POPT_EN_KBD ? kbd_mode : 0);
\r
1201 if (pico_inp_mode == 0)
\r
1204 /* handle other input modes using the pen */
\r
1205 if (PicoIn.opt & POPT_EN_MOUSE) {
\r
1206 pico_pen_x = PicoIn.mouse[0];
\r
1207 pico_pen_y = PicoIn.mouse[1];
\r
1209 if (PicoIn.pad[0] & 1) pico_pen_y--;
\r
1210 if (PicoIn.pad[0] & 2) pico_pen_y++;
\r
1211 if (PicoIn.pad[0] & 4) pico_pen_x--;
\r
1212 if (PicoIn.pad[0] & 8) pico_pen_x++;
\r
1213 PicoIn.pad[0] &= ~0x0f; // release UDLR
\r
1216 if ((pico_pad ^ PicoIn.pad[0]) & PicoIn.pad[0] & (1<<GBTN_START)) {
\r
1217 PicoPicohw.pen_pos[0] ^= 0x8000;
\r
1218 PicoPicohw.pen_pos[1] ^= 0x8000;
\r
1219 emu_status_msg("Pen %s", PicoPicohw.pen_pos[0] & 0x8000 ? "Up" : "Down");
\r
1221 pico_pad = PicoIn.pad[0];
\r
1223 /* cursor position, cursor drawing must not cross screen borders */
\r
1224 if (pico_pen_y < PICO_PEN_ADJUST_Y)
\r
1225 pico_pen_y = PICO_PEN_ADJUST_Y;
\r
1226 if (pico_pen_y > 223-1 - PICO_PEN_ADJUST_Y)
\r
1227 pico_pen_y = 223-1 - PICO_PEN_ADJUST_Y;
\r
1228 if (pico_pen_x < PICO_PEN_ADJUST_X)
\r
1229 pico_pen_x = PICO_PEN_ADJUST_X;
\r
1230 if (pico_pen_x > 319-1 - PICO_PEN_ADJUST_X)
\r
1231 pico_pen_x = 319-1 - PICO_PEN_ADJUST_X;
\r
1233 PicoPicohw.pen_pos[0] &= 0x8000;
\r
1234 PicoPicohw.pen_pos[1] &= 0x8000;
\r
1235 PicoPicohw.pen_pos[0] |= 0x03c + pico_pen_x;
\r
1236 PicoPicohw.pen_pos[1] |= (pico_inp_mode == 1 ? 0x2f8 : 0x1fc) + pico_pen_y;
\r
1239 static void do_turbo(unsigned short *pad, int acts)
\r
1241 static int turbo_pad = 0;
\r
1242 static unsigned char turbo_cnt[3] = { 0, 0, 0 };
\r
1243 int inc = currentConfig.turbo_rate * 2;
\r
1245 if (acts & 0x1000) {
\r
1246 turbo_cnt[0] += inc;
\r
1247 if (turbo_cnt[0] >= 60)
\r
1248 turbo_pad ^= 0x10, turbo_cnt[0] = 0;
\r
1250 if (acts & 0x2000) {
\r
1251 turbo_cnt[1] += inc;
\r
1252 if (turbo_cnt[1] >= 60)
\r
1253 turbo_pad ^= 0x20, turbo_cnt[1] = 0;
\r
1255 if (acts & 0x4000) {
\r
1256 turbo_cnt[2] += inc;
\r
1257 if (turbo_cnt[2] >= 60)
\r
1258 turbo_pad ^= 0x40, turbo_cnt[2] = 0;
\r
1260 *pad |= turbo_pad & (acts >> 8);
\r
1263 static void run_events_ui(unsigned int which)
\r
1265 if (which & (PEV_STATE_LOAD|PEV_STATE_SAVE))
\r
1268 if ( emu_check_save_file(state_slot, NULL) &&
\r
1269 (((which & PEV_STATE_LOAD) && (currentConfig.confirm_save & EOPT_CONFIRM_LOAD)) ||
\r
1270 ((which & PEV_STATE_SAVE) && (currentConfig.confirm_save & EOPT_CONFIRM_SAVE))) )
\r
1276 strcpy(tmp, (which & PEV_STATE_LOAD) ? "LOAD STATE? " : "OVERWRITE SAVE? ");
\r
1277 len = strlen(tmp);
\r
1278 nm = in_get_key_name(-1, -PBTN_MOK);
\r
1279 snprintf(tmp + len, sizeof(tmp) - len, "(%s=yes, ", nm);
\r
1280 len = strlen(tmp);
\r
1281 nm = in_get_key_name(-1, -PBTN_MBACK);
\r
1282 snprintf(tmp + len, sizeof(tmp) - len, "%s=no)", nm);
\r
1284 plat_status_msg_busy_first(tmp);
\r
1286 in_set_config_int(0, IN_CFG_BLOCKING, 1);
\r
1287 while (in_menu_wait_any(NULL, 50) & (PBTN_MOK | PBTN_MBACK))
\r
1289 while ( !((keys = in_menu_wait_any(NULL, 50)) & (PBTN_MOK | PBTN_MBACK)))
\r
1291 if (keys & PBTN_MBACK)
\r
1293 while (in_menu_wait_any(NULL, 50) & (PBTN_MOK | PBTN_MBACK))
\r
1295 in_set_config_int(0, IN_CFG_BLOCKING, 0);
\r
1296 plat_status_msg_clear();
\r
1299 plat_status_msg_busy_first((which & PEV_STATE_LOAD) ? "LOADING STATE" : "SAVING STATE");
\r
1300 PicoStateProgressCB = plat_status_msg_busy_next;
\r
1301 emu_save_load_game((which & PEV_STATE_LOAD) ? 1 : 0, 0);
\r
1302 PicoStateProgressCB = NULL;
\r
1304 plat_status_msg_busy_done();
\r
1306 if (which & (PEV_SSLOT_PREV|PEV_SSLOT_NEXT))
\r
1308 if (which & PEV_SSLOT_PREV) {
\r
1310 if (state_slot < 0)
\r
1314 if (state_slot > 9)
\r
1318 emu_status_msg("SAVE SLOT %i [%s]", state_slot,
\r
1319 emu_check_save_file(state_slot, NULL) ? "USED" : "FREE");
\r
1321 if (which & PEV_SWITCH_RND)
\r
1323 plat_video_toggle_renderer(1, 0);
\r
1325 if (which & PEV_GRAB_INPUT)
\r
1327 if (PicoIn.opt & POPT_EN_MOUSE) {
\r
1328 grab_mode = !grab_mode;
\r
1329 in_update_analog(0, 2, &mouse_x);
\r
1330 in_update_analog(0, 3, &mouse_y);
\r
1331 in_update_analog(0, 0, &mouse_x);
\r
1332 in_update_analog(0, 1, &mouse_y);
\r
1333 emu_status_msg("Mouse capture %s", grab_mode ? "on" : "off");
\r
1336 emu_status_msg("No mouse configured");
\r
1339 plat_grab_cursor(grab_mode);
\r
1341 if (which & PEV_SWITCH_KBD)
\r
1343 if (! (PicoIn.opt & POPT_EN_KBD)) {
\r
1345 emu_status_msg("No keyboard configured");
\r
1347 kbd_mode = !kbd_mode;
\r
1348 emu_status_msg("Keyboard %s", kbd_mode ? "on" : "off");
\r
1351 plat_video_clear_buffers();
\r
1353 if (which & PEV_RESET)
\r
1355 if (which & PEV_MENU)
\r
1356 engineState = PGS_Menu;
\r
1359 void emu_update_input(void)
\r
1361 static int prev_events = 0;
\r
1362 int actions[IN_BINDTYPE_COUNT] = { 0, };
\r
1363 int actions_kbd[IN_BIND_LAST] = { 0, };
\r
1364 int pl_actions[4];
\r
1365 int count_kbd = 0, buttons = 0;
\r
1366 int events, i = 0;
\r
1368 in_update(actions);
\r
1370 pl_actions[0] = actions[IN_BINDTYPE_PLAYER12];
\r
1371 pl_actions[1] = actions[IN_BINDTYPE_PLAYER12] >> 16;
\r
1372 pl_actions[2] = actions[IN_BINDTYPE_PLAYER34];
\r
1373 pl_actions[3] = actions[IN_BINDTYPE_PLAYER34] >> 16;
\r
1375 events = actions[IN_BINDTYPE_EMU] & PEV_MASK;
\r
1377 // update mouse coordinates if there is an emulated mouse
\r
1378 if (PicoIn.opt & POPT_EN_MOUSE) {
\r
1380 in_update_analog(0, 0, &PicoIn.mouse[0]);
\r
1381 in_update_analog(0, 1, &PicoIn.mouse[1]);
\r
1382 // scale mouse coordinates from -1024..1024 to 0..screen_w/h
\r
1383 PicoIn.mouse[0] = (PicoIn.mouse[0]+1024) * g_screen_width /2048;
\r
1384 PicoIn.mouse[1] = (PicoIn.mouse[1]+1024) * g_screen_height/2048;
\r
1387 in_update_analog(0, 2, &xrel);
\r
1388 in_update_analog(0, 3, &yrel);
\r
1389 mouse_x += xrel, mouse_y += yrel;
\r
1390 // scale mouse coordinates from -1024..1024 to 0..screen_w/h
\r
1391 PicoIn.mouse[0] = (mouse_x+1024) * g_screen_width /2048;
\r
1392 PicoIn.mouse[1] = (mouse_y+1024) * g_screen_height/2048;
\r
1395 in_update_analog(0, -1, &i); // get mouse buttons, bit 2-0 = RML
\r
1396 if (PicoIn.AHW & PAHW_PICO) {
\r
1397 // TODO is maintaining 2 different mappings necessary?
\r
1398 if (i & 1) buttons |= 1<<GBTN_C; // pen button
\r
1399 if (i & 2) buttons |= 1<<GBTN_B; // red button
\r
1400 if (i & 4) buttons |= 1<<GBTN_START; // pen up/down
\r
1402 if (i & 1) buttons |= 1<<GBTN_B; // as Sega Mouse
\r
1403 if (i & 2) buttons |= 1<<GBTN_START;
\r
1404 if (i & 4) buttons |= 1<<GBTN_C;
\r
1407 if (currentConfig.input_dev0 == PICO_INPUT_MOUSE)
\r
1408 pl_actions[0] |= buttons;
\r
1409 if (currentConfig.input_dev1 == PICO_INPUT_MOUSE)
\r
1410 pl_actions[1] |= buttons;
\r
1414 int mask = (PicoIn.AHW & PAHW_PICO ? 0xf : 0x0);
\r
1415 if (currentConfig.keyboard == 2)
\r
1416 count_kbd = in_update_kbd(actions_kbd);
\r
1417 else if (currentConfig.keyboard == 1)
\r
1418 count_kbd = vkbd_update(vkbd, pl_actions[0], actions_kbd);
\r
1420 // FIXME: Only passthrough joystick input to avoid collisions
\r
1421 // with PS/2 bindings. Ideally we should check if the device this
\r
1422 // input originated from is the same as the device used for
\r
1423 // PS/2 input, and passthrough if they are different devices.
\r
1424 PicoIn.pad[0] = pl_actions[0] & mask;
\r
1425 PicoIn.pad[1] = pl_actions[1] & mask;
\r
1426 PicoIn.pad[2] = pl_actions[2] & mask;
\r
1427 PicoIn.pad[3] = pl_actions[3] & mask;
\r
1429 // Ignore events mapped to bindings that collide with PS/2 peripherals.
\r
1430 // Note that calls to emu_set_fastforward() should be avoided as well,
\r
1431 // since fast-forward activates even with parameter set_on = 0.
\r
1432 events &= PEV_SWITCH_KBD;
\r
1434 PicoIn.pad[0] = pl_actions[0] & 0xfff;
\r
1435 PicoIn.pad[1] = pl_actions[1] & 0xfff;
\r
1436 PicoIn.pad[2] = pl_actions[2] & 0xfff;
\r
1437 PicoIn.pad[3] = pl_actions[3] & 0xfff;
\r
1439 if (pl_actions[0] & 0x7000)
\r
1440 do_turbo(&PicoIn.pad[0], pl_actions[0]);
\r
1441 if (pl_actions[1] & 0x7000)
\r
1442 do_turbo(&PicoIn.pad[1], pl_actions[1]);
\r
1443 if (pl_actions[2] & 0x7000)
\r
1444 do_turbo(&PicoIn.pad[2], pl_actions[2]);
\r
1445 if (pl_actions[3] & 0x7000)
\r
1446 do_turbo(&PicoIn.pad[3], pl_actions[3]);
\r
1448 if ((events ^ prev_events) & PEV_FF) {
\r
1449 emu_set_fastforward(events & PEV_FF);
\r
1450 plat_update_volume(0, 0);
\r
1455 // volume is treated in special way and triggered every frame
\r
1456 if (events & (PEV_VOL_DOWN|PEV_VOL_UP))
\r
1457 plat_update_volume(1, events & PEV_VOL_UP);
\r
1459 events &= ~prev_events;
\r
1461 // update keyboard input, actions only updated if keyboard mode active
\r
1463 for (i = 0; i < count_kbd; i++) {
\r
1464 if (actions_kbd[i]) {
\r
1465 unsigned int key = (actions_kbd[i] & 0xff);
\r
1466 if (key == PEVB_KBD_LSHIFT || key == PEVB_KBD_RSHIFT ||
\r
1467 key == PEVB_KBD_CTRL || key == PEVB_KBD_FUNC) {
\r
1468 PicoIn.kbd = (PicoIn.kbd & 0x00ff) | (key << 8);
\r
1470 PicoIn.kbd = (PicoIn.kbd & 0xff00) | key;
\r
1475 if (PicoIn.AHW & PAHW_PICO)
\r
1476 run_events_pico(events);
\r
1478 run_events_ui(events);
\r
1482 prev_events = actions[IN_BINDTYPE_EMU] & PEV_MASK;
\r
1485 static void mkdir_path(char *path_with_reserve, int pos, const char *name)
\r
1487 strcpy(path_with_reserve + pos, name);
\r
1488 if (plat_is_dir(path_with_reserve))
\r
1490 if (mkdir(path_with_reserve, 0755) < 0)
\r
1491 lprintf("failed to create: %s\n", path_with_reserve);
\r
1494 void emu_cmn_forced_frame(int no_scale, int do_emu, void *buf)
\r
1496 int po_old = PicoIn.opt;
\r
1499 for (y = 0; y < g_screen_height; y++)
\r
1500 memset32((short *)g_screen_ptr + g_screen_ppitch * y, 0,
\r
1501 g_screen_width * 2 / 4);
\r
1503 PicoIn.opt &= ~(POPT_ALT_RENDERER|POPT_EN_SOFTSCALE);
\r
1504 PicoIn.opt |= POPT_ACC_SPRITES;
\r
1505 if (!no_scale && currentConfig.scaling)
\r
1506 PicoIn.opt |= POPT_EN_SOFTSCALE;
\r
1508 PicoDrawSetOutFormat(PDF_RGB555, 1);
\r
1509 PicoDrawSetOutBuf(buf, g_screen_ppitch * 2);
\r
1510 Pico.m.dirtyPal = 1;
\r
1514 PicoFrameDrawOnly();
\r
1516 PicoIn.opt = po_old;
\r
1519 void emu_init(void)
\r
1525 // FIXME: handle through menu, etc
\r
1527 f = fopen("32X_M_BIOS.BIN", "rb");
\r
1528 p32x_bios_m = malloc(2048);
\r
1529 fread(p32x_bios_m, 1, 2048, f);
\r
1531 f = fopen("32X_S_BIOS.BIN", "rb");
\r
1532 p32x_bios_s = malloc(1024);
\r
1533 fread(p32x_bios_s, 1, 1024, f);
\r
1537 /* make dirs for saves */
\r
1538 pos = plat_get_root_dir(path, sizeof(path) - 4);
\r
1539 mkdir_path(path, pos, "mds");
\r
1540 mkdir_path(path, pos, "srm");
\r
1541 mkdir_path(path, pos, "brm");
\r
1542 mkdir_path(path, pos, "tape");
\r
1543 mkdir_path(path, pos, "cfg");
\r
1547 make_config_cfg(path);
\r
1548 config_readlrom(path);
\r
1551 PicoIn.osdMessage = plat_status_msg_busy_next;
\r
1552 PicoIn.mcdTrayOpen = emu_tray_open;
\r
1553 PicoIn.mcdTrayClose = emu_tray_close;
\r
1558 void emu_finish(void)
\r
1561 if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && Pico.sv.changed) {
\r
1562 emu_save_load_game(0, 1);
\r
1563 Pico.sv.changed = 0;
\r
1566 if (!(currentConfig.EmuOpt & EOPT_NO_AUTOSVCFG)) {
\r
1568 make_config_cfg(cfg);
\r
1569 config_writelrom(cfg);
\r
1581 static void snd_write_nonblocking(int len)
\r
1583 sndout_write_nb(PicoIn.sndOut, len);
\r
1586 void emu_sound_start(void)
\r
1588 PicoIn.sndOut = NULL;
\r
1590 // auto-select rate?
\r
1591 if (PicoIn.sndRate > 52000 && PicoIn.sndRate < 54000)
\r
1592 PicoIn.sndRate = YM2612_NATIVE_RATE();
\r
1593 if (currentConfig.EmuOpt & EOPT_EN_SOUND)
\r
1595 int is_stereo = (PicoIn.opt & POPT_EN_STEREO) ? 1 : 0;
\r
1597 memset(sndBuffer, 0, sizeof(sndBuffer));
\r
1598 PicoIn.sndOut = sndBuffer;
\r
1599 PsndRerate(Pico.m.frame_count ? 1 : 0);
\r
1601 printf("starting audio: %i len: %i stereo: %i, pal: %i\n",
\r
1602 PicoIn.sndRate, Pico.snd.len, is_stereo, Pico.m.pal);
\r
1604 sndout_start(PicoIn.sndRate, is_stereo);
\r
1605 PicoIn.writeSound = snd_write_nonblocking;
\r
1606 plat_update_volume(0, 0);
\r
1610 void emu_sound_stop(void)
\r
1615 void emu_sound_wait(void)
\r
1620 static void emu_loop_prep(void)
\r
1622 static int pal_old = -1;
\r
1623 static int filter_old = -1;
\r
1625 if (currentConfig.CPUclock != plat_target_cpu_clock_get())
\r
1626 plat_target_cpu_clock_set(currentConfig.CPUclock);
\r
1628 if (Pico.m.pal != pal_old) {
\r
1629 plat_target_lcdrate_set(Pico.m.pal);
\r
1630 pal_old = Pico.m.pal;
\r
1633 if (currentConfig.filter != filter_old) {
\r
1634 plat_target_hwfilter_set(currentConfig.filter);
\r
1635 filter_old = currentConfig.filter;
\r
1638 plat_target_gamma_set(currentConfig.gamma, 0);
\r
1641 if (currentConfig.keyboard == 1) {
\r
1642 if (PicoIn.AHW & PAHW_SMS) vkbd = vkbd_init(0);
\r
1643 else if (PicoIn.AHW & PAHW_PICO) vkbd = vkbd_init(1);
\r
1645 PicoIn.opt &= ~POPT_EN_KBD;
\r
1646 if (((PicoIn.AHW & PAHW_PICO) || (PicoIn.AHW & PAHW_SC)) && currentConfig.keyboard)
\r
1647 PicoIn.opt |= POPT_EN_KBD;
\r
1649 PicoIn.opt &= ~POPT_EN_MOUSE;
\r
1650 if (!(PicoIn.AHW & PAHW_8BIT) && (currentConfig.input_dev0 == PICO_INPUT_MOUSE ||
\r
1651 currentConfig.input_dev1 == PICO_INPUT_MOUSE)) {
\r
1652 PicoIn.opt |= POPT_EN_MOUSE;
\r
1653 plat_grab_cursor(grab_mode);
\r
1659 /* our tick here is 1 us right now */
\r
1660 #define ms_to_ticks(x) (int)(x * 1000)
\r
1661 #define get_ticks() plat_get_ticks_us()
\r
1662 #define vsync_delay ms_to_ticks(1)
\r
1664 void emu_loop(void)
\r
1666 int frames_done, frames_shown; /* actual frames for fps counter */
\r
1667 int target_frametime;
\r
1668 unsigned int timestamp = 0;
\r
1669 unsigned int timestamp_aim = 0;
\r
1670 unsigned int timestamp_fps = 0;
\r
1671 char *notice_msg = NULL;
\r
1673 int fskip_cnt = 0;
\r
1677 PicoLoopPrepare();
\r
1679 plat_video_loop_prepare();
\r
1681 pemu_sound_start();
\r
1683 /* number of ticks per frame */
\r
1685 target_frametime = ms_to_ticks(1000) / 50;
\r
1687 target_frametime = ms_to_ticks(1000) / 60;
\r
1690 frames_done = frames_shown = 0;
\r
1692 /* loop with resync every 1 sec. */
\r
1693 while (engineState == PGS_Running)
\r
1698 pprof_start(main);
\r
1700 if (reset_timing) {
\r
1702 plat_video_wait_vsync();
\r
1703 timestamp_aim = get_ticks();
\r
1704 timestamp_fps = timestamp_aim;
\r
1707 else if (currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) {
\r
1708 timestamp_aim = get_ticks();
\r
1711 timestamp = get_ticks();
\r
1713 // show notice_msg message?
\r
1714 if (notice_msg_time != 0)
\r
1716 static int noticeMsgSum;
\r
1717 if (timestamp - ms_to_ticks(notice_msg_time)
\r
1718 > ms_to_ticks(STATUS_MSG_TIMEOUT))
\r
1720 notice_msg_time = 0;
\r
1721 notice_msg = NULL;
\r
1722 plat_status_msg_clear();
\r
1725 int sum = noticeMsg[0] + noticeMsg[1] + noticeMsg[2];
\r
1726 if (sum != noticeMsgSum) {
\r
1727 plat_status_msg_clear();
\r
1728 noticeMsgSum = sum;
\r
1730 notice_msg = noticeMsg;
\r
1734 // second changed?
\r
1735 if (timestamp - timestamp_fps >= ms_to_ticks(1000))
\r
1738 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
\r
1739 if (++bench == 10) {
\r
1741 bench_fps_s = bench_fps;
\r
1742 bf[bfp++ & 3] = bench_fps;
\r
1745 bench_fps += frames_shown;
\r
1746 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
\r
1747 printf("%s\n", fpsbuff);
\r
1749 if (currentConfig.EmuOpt & EOPT_SHOW_FPS)
\r
1750 snprintf(fpsbuff, 8, "%02i/%02i ", frames_shown, frames_done);
\r
1752 frames_shown = frames_done = 0;
\r
1753 timestamp_fps += ms_to_ticks(1000);
\r
1756 sprintf(fpsbuff, "%i", Pico.m.frame_count);
\r
1759 diff = timestamp_aim - timestamp;
\r
1761 if (currentConfig.Frameskip >= 0) // frameskip enabled (or 0)
\r
1763 if (fskip_cnt < currentConfig.Frameskip) {
\r
1771 else if (diff < -target_frametime)
\r
1773 /* no time left for this frame - skip */
\r
1774 /* limit auto frameskip to max_skip */
\r
1775 if (fskip_cnt < currentConfig.max_skip) {
\r
1785 // don't go in debt too much
\r
1786 while (diff < -target_frametime * 3) {
\r
1787 timestamp_aim += target_frametime;
\r
1788 diff = timestamp_aim - timestamp;
\r
1791 emu_update_input();
\r
1794 skip |= (PicoIn.AHW & PAHW_SMS) &&
\r
1795 (Pico.m.hardware & PMS_HW_3D) &&
\r
1796 (PicoMem.zram[0x1ffb] & 1);
\r
1799 int do_audio = diff > -target_frametime * 2;
\r
1800 PicoIn.skipFrame = do_audio ? 1 : 2;
\r
1802 PicoIn.skipFrame = 0;
\r
1806 pemu_finalize_frame(fpsbuff, notice_msg);
\r
1810 timestamp_aim += target_frametime;
\r
1812 if (!skip && !flip_after_sync)
\r
1813 plat_video_flip();
\r
1815 /* frame limiter */
\r
1816 if (!skip && !reset_timing
\r
1817 && !(currentConfig.EmuOpt & (EOPT_NO_FRMLIMIT|EOPT_EXT_FRMLIMIT)))
\r
1819 unsigned int timestamp = get_ticks();
\r
1820 diff = timestamp_aim - timestamp;
\r
1822 // sleep or vsync if we are still too fast
\r
1823 if (diff > target_frametime + vsync_delay && (currentConfig.EmuOpt & EOPT_VSYNC)) {
\r
1824 // we are too fast
\r
1825 plat_video_wait_vsync();
\r
1826 timestamp = get_ticks();
\r
1827 diff = timestamp_aim - timestamp;
\r
1829 if (diff > target_frametime + vsync_delay) {
\r
1831 plat_wait_till_us(timestamp + (diff - target_frametime));
\r
1835 if (!skip && flip_after_sync)
\r
1836 plat_video_flip();
\r
1841 emu_set_fastforward(0);
\r
1844 if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && Pico.sv.changed) {
\r
1845 plat_status_msg_busy_first("Writing SRAM/BRAM...");
\r
1846 emu_save_load_game(0, 1);
\r
1847 Pico.sv.changed = 0;
\r
1852 plat_grab_cursor(0);
\r