X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=platform%2Fcommon%2Femu.c;h=7385e159995423cdfc99eeda2b484da55d63502f;hb=refs%2Fremotes%2Fgithub%2Fmaster;hp=6a8c940b22726e2a2cfb8df81fb1025a52f5bba4;hpb=9db6a54485501b56b0f2f5db4d093c38fe495bda;p=picodrive.git diff --git a/platform/common/emu.c b/platform/common/emu.c index 6a8c940b..8e6ad540 100644 --- a/platform/common/emu.c +++ b/platform/common/emu.c @@ -1,6 +1,7 @@ /* * PicoDrive * (C) notaz, 2007-2010 + * (C) irixxxx, 2019-2024 * * This work is licensed under the terms of MAME license. * See COPYING file in the top-level directory. @@ -19,8 +20,10 @@ #include "../libpicofe/fonts.h" #include "../libpicofe/sndout.h" #include "../libpicofe/lprintf.h" +#include "../libpicofe/readpng.h" #include "../libpicofe/plat.h" #include "emu.h" +#include "keyboard.h" #include "input_pico.h" #include "menu_pico.h" #include "config_file.h" @@ -28,6 +31,10 @@ #include #include +#if defined(__GNUC__) && __GNUC__ >= 7 +#pragma GCC diagnostic ignored "-Wformat-truncation" +#endif + #ifndef _WIN32 #define PATH_SEP "/" #define PATH_SEP_C '/' @@ -42,6 +49,7 @@ void *g_screen_ptr; int g_screen_width = 320; int g_screen_height = 240; +int g_screen_ppitch = 320; // pitch in pixels const char *PicoConfigFile = "config2.cfg"; currentConfig_t currentConfig, defaultConfig; @@ -52,7 +60,17 @@ int pico_inp_mode; int flip_after_sync; int engineState = PGS_Menu; -static short __attribute__((aligned(4))) sndBuffer[2*44100/50]; +int grab_mode; +int kbd_mode; +struct vkbd *vkbd; +int mouse_x, mouse_y; + +static int pico_page; +static int pico_w, pico_h; +static u16 *pico_overlay; +static int pico_pad; + +static short __attribute__((aligned(4))) sndBuffer[2*54000/50]; /* tmp buff to reduce stack usage for plats with small stack */ static char static_buff[512]; @@ -122,8 +140,8 @@ static void fname_ext(char *dst, int dstlen, const char *prefix, const char *ext strncpy(dst + prefix_len, p, dstlen - prefix_len - 1); dst[dstlen - 8] = 0; - if (dst[strlen(dst) - 4] == '.') - dst[strlen(dst) - 4] = 0; + if ((p = strrchr(dst, '.')) != NULL) + dst[p-dst] = 0; if (ext) strcat(dst, ext); } @@ -171,12 +189,13 @@ static const char *find_bios(int *region, const char *cd_fname) ret = emu_read_config(cd_fname, 0); if (!ret) emu_read_config(NULL, 0); - if (PicoRegionOverride) { - *region = PicoRegionOverride; + if (PicoIn.regionOverride) { + *region = PicoIn.regionOverride; lprintf("override region to %s\n", *region != 4 ? (*region == 8 ? "EU" : "JAP") : "USA"); } + // locate BIOS file if (*region == 4) { // US files = biosfiles_us; count = sizeof(biosfiles_us) / sizeof(char *); @@ -201,6 +220,16 @@ static const char *find_bios(int *region, const char *cd_fname) strcat(static_buff, ".zip"); f = fopen(static_buff, "rb"); if (f) break; + + strcpy(static_buff, files[i]); + strcat(static_buff, ".bin"); + f = fopen(static_buff, "rb"); + if (f) break; + + static_buff[strlen(static_buff) - 4] = 0; + strcat(static_buff, ".zip"); + f = fopen(static_buff, "rb"); + if (f) break; } if (f) { @@ -215,6 +244,26 @@ static const char *find_bios(int *region, const char *cd_fname) } } +static const char *find_msu(const char *cd_fname) +{ + int i; + + // look for MSU.MD or MD+ rom file. XXX another extension list? ugh... + static const char *md_exts[] = { "gen", "smd", "md", "32x" }; + char *ext = strrchr(cd_fname, '.'); + int extpos = ext ? ext-cd_fname : strlen(cd_fname); + strcpy(static_buff, cd_fname); + static_buff[extpos++] = '.'; + for (i = 0; i < ARRAY_SIZE(md_exts); i++) { + strcpy(static_buff+extpos, md_exts[i]); + if (access(static_buff, R_OK) == 0) { + printf("found MSU rom: %s\n",static_buff); + return static_buff; + } + } + return NULL; +} + /* check if the name begins with BIOS name */ /* static int emu_isBios(const char *name) @@ -266,16 +315,16 @@ static char *emu_make_rom_id(const char *fname) static char id_string[3+0xe*3+0x3*3+0x30*3+3]; int pos, swab = 1; - if (PicoAHW & PAHW_MCD) { + if (PicoIn.AHW & PAHW_MCD) { strcpy(id_string, "CD|"); swab = 0; } - else if (PicoAHW & PAHW_SMS) + else if (PicoIn.AHW & PAHW_SMS) strcpy(id_string, "MS|"); else strcpy(id_string, "MD|"); pos = 3; - if (!(PicoAHW & PAHW_SMS)) { + if (!(PicoIn.AHW & PAHW_SMS)) { pos += extract_text(id_string + pos, media_id_header + 0x80, 0x0e, swab); // serial id_string[pos] = '|'; pos++; pos += extract_text(id_string + pos, media_id_header + 0xf0, 0x03, swab); // region @@ -296,7 +345,7 @@ static char *emu_make_rom_id(const char *fname) // buffer must be at least 150 byte long void emu_get_game_name(char *str150) { - int ret, swab = (PicoAHW & PAHW_MCD) ? 0 : 1; + int ret, swab = (PicoIn.AHW & PAHW_MCD) ? 0 : 1; char *s, *d; ret = extract_text(str150, media_id_header + 0x50, 0x30, swab); // overseas name @@ -315,25 +364,33 @@ static void system_announce(void) const char *sys_name, *tv_standard, *extra = ""; int fps; - if (PicoAHW & PAHW_SMS) { + if (PicoIn.AHW & PAHW_SMS) { sys_name = "Master System"; + if (PicoIn.AHW & PAHW_GG) + sys_name = "Game Gear"; + else if (PicoIn.AHW & PAHW_SG) + sys_name = "SG-1000"; + else if (PicoIn.AHW & PAHW_SC) + sys_name = "SC-3000"; + else if (Pico.m.hardware & PMS_HW_JAP) + sys_name = "Mark III"; #ifdef NO_SMS extra = " [no support]"; #endif - } else if (PicoAHW & PAHW_PICO) { + } else if (PicoIn.AHW & PAHW_PICO) { sys_name = "Pico"; - } else if ((PicoAHW & (PAHW_32X|PAHW_MCD)) == (PAHW_32X|PAHW_MCD)) { + } else if ((PicoIn.AHW & (PAHW_32X|PAHW_MCD)) == (PAHW_32X|PAHW_MCD)) { sys_name = "32X + Mega CD"; if ((Pico.m.hardware & 0xc0) == 0x80) sys_name = "32X + Sega CD"; - } else if (PicoAHW & PAHW_MCD) { + } else if (PicoIn.AHW & PAHW_MCD) { sys_name = "Mega CD"; if ((Pico.m.hardware & 0xc0) == 0x80) sys_name = "Sega CD"; - } else if (PicoAHW & PAHW_32X) { + } else if (PicoIn.AHW & PAHW_32X) { sys_name = "32X"; } else { - sys_name = "MegaDrive"; + sys_name = "Mega Drive"; if ((Pico.m.hardware & 0xc0) == 0x80) sys_name = "Genesis"; } @@ -376,7 +433,7 @@ int emu_reload_rom(const char *rom_fname_in) movie_data = 0; } - if (!strcmp(ext, ".gmv")) + if (!strcasecmp(ext, ".gmv")) { // check for both gmv and rom int dummy; @@ -413,7 +470,7 @@ int emu_reload_rom(const char *rom_fname_in) get_ext(rom_fname, ext); lprintf("gmv loaded for %s\n", rom_fname); } - else if (!strcmp(ext, ".pat")) + else if (!strcasecmp(ext, ".pat")) { int dummy; PicoPatchLoad(rom_fname); @@ -430,8 +487,8 @@ int emu_reload_rom(const char *rom_fname_in) emu_make_path(carthw_path, "carthw.cfg", sizeof(carthw_path)); - media_type = PicoLoadMedia(rom_fname, carthw_path, - find_bios, do_region_override); + media_type = PicoLoadMedia(rom_fname, NULL, 0, carthw_path, + find_bios, find_msu, do_region_override); switch (media_type) { case PM_BAD_DETECT: @@ -451,7 +508,7 @@ int emu_reload_rom(const char *rom_fname_in) } // make quirks visible in UI - if (PicoQuirks & PQUIRK_FORCE_6BTN) + if (PicoIn.quirks & PQUIRK_FORCE_6BTN) currentConfig.input_dev0 = PICO_INPUT_PAD_6BTN; menu_romload_end(); @@ -470,12 +527,12 @@ int emu_reload_rom(const char *rom_fname_in) PicoSetInputDevice(0, indev); PicoSetInputDevice(1, indev); - PicoOpt |= POPT_DIS_VDP_FIFO; // no VDP fifo timing + PicoIn.opt |= POPT_DIS_VDP_FIFO; // no VDP fifo timing if (movie_data[0xF] >= 'A') { if (movie_data[0x16] & 0x80) { - PicoRegionOverride = 8; + PicoIn.regionOverride = 8; } else { - PicoRegionOverride = 4; + PicoIn.regionOverride = 4; } PicoReset(); // TODO: bits 6 & 5 @@ -485,8 +542,11 @@ int emu_reload_rom(const char *rom_fname_in) } else { + PicoSetInputDevice(0, currentConfig.input_dev0); + PicoSetInputDevice(1, currentConfig.input_dev1); + system_announce(); - PicoOpt &= ~POPT_DIS_VDP_FIFO; + PicoIn.opt &= ~POPT_DIS_VDP_FIFO; } strncpy(rom_fname_loaded, rom_fname, sizeof(rom_fname_loaded)-1); @@ -530,11 +590,11 @@ out: int emu_swap_cd(const char *fname) { - enum cd_img_type cd_type; + enum cd_track_type cd_type; int ret = -1; cd_type = PicoCdCheck(fname, NULL); - if (cd_type != CIT_NOT_CD) + if (cd_type != CT_UNKNOWN) ret = cdd_load(fname, cd_type); if (ret != 0) { menu_update_msg("Load failed, invalid CD image?"); @@ -547,6 +607,31 @@ int emu_swap_cd(const char *fname) return 1; } +int emu_play_tape(const char *fname) +{ + int ret; + + ret = PicoPlayTape(fname); + if (ret != 0) { + menu_update_msg("loading tape failed"); + return 0; + } + return 1; +} + +int emu_record_tape(const char *ext) +{ + int ret; + + fname_ext(static_buff, sizeof(static_buff), "tape"PATH_SEP, ext, rom_fname_loaded); + ret = PicoRecordTape(static_buff); + if (ret != 0) { + menu_update_msg("recording tape failed"); + return 0; + } + return 1; +} + // void emu_make_path(char *buff, const char *end, int size) { @@ -576,15 +661,19 @@ static void make_config_cfg(char *cfg_buff_512) void emu_prep_defconfig(void) { memset(&defaultConfig, 0, sizeof(defaultConfig)); - defaultConfig.EmuOpt = 0x9d | EOPT_EN_CD_LEDS; - defaultConfig.s_PicoOpt = POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80 | + defaultConfig.EmuOpt = EOPT_EN_SRAM | EOPT_EN_SOUND | EOPT_16BPP | + EOPT_EN_CD_LEDS | EOPT_GZIP_SAVES | EOPT_PICO_PEN; + defaultConfig.s_PicoOpt = POPT_EN_SNDFILTER|POPT_EN_GG_LCD|POPT_EN_YM2413 | + POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80 | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_MCD_GFX | POPT_EN_DRC|POPT_ACC_SPRITES | POPT_EN_32X|POPT_EN_PWM; defaultConfig.s_PsndRate = 44100; defaultConfig.s_PicoRegion = 0; // auto defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP + defaultConfig.s_hwSelect = PHWS_AUTO; defaultConfig.s_PicoCDBuffers = 0; + defaultConfig.s_PicoSndFilterAlpha = 0x10000 * 60 / 100; defaultConfig.confirm_save = EOPT_CONFIRM_SAVE; defaultConfig.Frameskip = -1; // auto defaultConfig.input_dev0 = PICO_INPUT_PAD_3BTN; @@ -595,6 +684,7 @@ void emu_prep_defconfig(void) defaultConfig.turbo_rate = 15; defaultConfig.msh2_khz = PICO_MSH2_HZ / 1000; defaultConfig.ssh2_khz = PICO_SSH2_HZ / 1000; + defaultConfig.max_skip = 4; // platform specific overrides pemu_prep_defconfig(); @@ -603,10 +693,12 @@ void emu_prep_defconfig(void) void emu_set_defconfig(void) { memcpy(¤tConfig, &defaultConfig, sizeof(currentConfig)); - PicoOpt = currentConfig.s_PicoOpt; - PsndRate = currentConfig.s_PsndRate; - PicoRegionOverride = currentConfig.s_PicoRegion; - PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder; + PicoIn.opt = currentConfig.s_PicoOpt; + PicoIn.sndRate = currentConfig.s_PsndRate; + PicoIn.regionOverride = currentConfig.s_PicoRegion; + PicoIn.autoRgnOrder = currentConfig.s_PicoAutoRgnOrder; + PicoIn.hwSelect = currentConfig.s_hwSelect; + PicoIn.sndFilterAlpha = currentConfig.s_PicoSndFilterAlpha; } int emu_read_config(const char *rom_fname, int no_defaults) @@ -652,14 +744,9 @@ int emu_read_config(const char *rom_fname, int no_defaults) } pemu_validate_config(); + PicoIn.overclockM68k = currentConfig.overclock_68k; // some sanity checks -#ifdef PSP - /* TODO: mv to plat_validate_config() */ - if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200; - if (currentConfig.gamma < -4 || currentConfig.gamma > 16) currentConfig.gamma = 0; - if (currentConfig.gamma2 < 0 || currentConfig.gamma2 > 2) currentConfig.gamma2 = 0; -#endif if (currentConfig.volume < 0 || currentConfig.volume > 99) currentConfig.volume = 50; @@ -729,12 +816,12 @@ void name(int x, int y, const char *text) \ } \ } -mk_text_out(emu_text_out8, unsigned char, 0xf0, g_screen_ptr, 1, g_screen_width) -mk_text_out(emu_text_out16, unsigned short, 0xffff, g_screen_ptr, 1, g_screen_width) +mk_text_out(emu_text_out8, unsigned char, 0xf0, g_screen_ptr, 1, g_screen_ppitch) +mk_text_out(emu_text_out16, unsigned short, 0xffff, g_screen_ptr, 1, g_screen_ppitch) mk_text_out(emu_text_out8_rot, unsigned char, 0xf0, - (char *)g_screen_ptr + (g_screen_width - 1) * g_screen_height, -g_screen_height, 1) + (char *)g_screen_ptr + (g_screen_ppitch - 1) * g_screen_height, -g_screen_height, 1) mk_text_out(emu_text_out16_rot, unsigned short, 0xffff, - (short *)g_screen_ptr + (g_screen_width - 1) * g_screen_height, -g_screen_height, 1) + (short *)g_screen_ptr + (g_screen_ppitch - 1) * g_screen_height, -g_screen_height, 1) #undef mk_text_out @@ -750,7 +837,7 @@ void emu_osd_text16(int x, int y, const char *text) for (h = 0; h < 8; h++) { unsigned short *p; p = (unsigned short *)g_screen_ptr - + x + g_screen_width * (y + h); + + x + g_screen_ppitch * (y + h); for (i = len; i > 0; i--, p++) *p = (*p >> 2) & 0x39e7; } @@ -767,20 +854,20 @@ static void update_movie(void) lprintf("END OF MOVIE.\n"); } else { // MXYZ SACB RLDU - PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU - if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // C - if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // A - if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // B - PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU - if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // C - if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // A - if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // B - PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX - if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X - if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z - PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX - if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X - if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z + PicoIn.pad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU + if(!(movie_data[offs] & 0x10)) PicoIn.pad[0] |= 0x40; // C + if(!(movie_data[offs] & 0x20)) PicoIn.pad[0] |= 0x10; // A + if(!(movie_data[offs] & 0x40)) PicoIn.pad[0] |= 0x20; // B + PicoIn.pad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU + if(!(movie_data[offs+1] & 0x10)) PicoIn.pad[1] |= 0x40; // C + if(!(movie_data[offs+1] & 0x20)) PicoIn.pad[1] |= 0x10; // A + if(!(movie_data[offs+1] & 0x40)) PicoIn.pad[1] |= 0x20; // B + PicoIn.pad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX + if(!(movie_data[offs+2] & 0x01)) PicoIn.pad[0] |= 0x0400; // X + if(!(movie_data[offs+2] & 0x04)) PicoIn.pad[0] |= 0x0100; // Z + PicoIn.pad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX + if(!(movie_data[offs+2] & 0x10)) PicoIn.pad[1] |= 0x0400; // X + if(!(movie_data[offs+2] & 0x40)) PicoIn.pad[1] |= 0x0100; // Z } } @@ -809,9 +896,9 @@ char *emu_get_save_fname(int load, int is_sram, int slot, int *time) if (is_sram) { - strcpy(ext, (PicoAHW & PAHW_MCD) ? ".brm" : ".srm"); + strcpy(ext, (PicoIn.AHW & PAHW_MCD) && Pico.romsize == 0 ? ".brm" : ".srm"); romfname_ext(saveFname, sizeof(static_buff), - (PicoAHW & PAHW_MCD) ? "brm"PATH_SEP : "srm"PATH_SEP, ext); + (PicoIn.AHW & PAHW_MCD) && Pico.romsize == 0 ? "brm"PATH_SEP : "srm"PATH_SEP, ext); if (!load) return saveFname; @@ -885,24 +972,24 @@ int emu_save_load_game(int load, int sram) int sram_size; unsigned char *sram_data; int truncate = 1; - if (PicoAHW & PAHW_MCD) + if ((PicoIn.AHW & PAHW_MCD) && Pico.romsize == 0) { - if (PicoOpt & POPT_EN_MCD_RAMCART) { + if (PicoIn.opt & POPT_EN_MCD_RAMCART) { sram_size = 0x12000; - sram_data = SRam.data; + sram_data = Pico.sv.data; if (sram_data) - memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4); + memcpy(sram_data, Pico_mcd->bram, 0x2000); } else { sram_size = 0x2000; sram_data = Pico_mcd->bram; truncate = 0; // the .brm may contain RAM cart data after normal brm } } else { - sram_size = SRam.size; - sram_data = SRam.data; + sram_size = Pico.sv.size; + sram_data = Pico.sv.data; } if (sram_data == NULL) - return 0; // SRam forcefully disabled for this game + return 0; // cart saves forcefully disabled for this game if (load) { @@ -912,8 +999,8 @@ int emu_save_load_game(int load, int sram) ret = fread(sram_data, 1, sram_size, sramFile); ret = ret > 0 ? 0 : -1; fclose(sramFile); - if ((PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_RAMCART)) - memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4); + if ((PicoIn.AHW & PAHW_MCD) && Pico.romsize == 0 && (PicoIn.opt&POPT_EN_MCD_RAMCART)) + memcpy(Pico_mcd->bram, sram_data, 0x2000); } else { // sram save needs some special processing // see if we have anything to save @@ -957,25 +1044,22 @@ void emu_set_fastforward(int set_on) static int set_Frameskip, set_EmuOpt, is_on = 0; if (set_on && !is_on) { - set_PsndOut = PsndOut; + set_PsndOut = PicoIn.sndOut; set_Frameskip = currentConfig.Frameskip; set_EmuOpt = currentConfig.EmuOpt; - PsndOut = NULL; + PicoIn.sndOut = NULL; currentConfig.Frameskip = 8; - currentConfig.EmuOpt &= ~4; - currentConfig.EmuOpt |= 0x40000; + currentConfig.EmuOpt &= ~EOPT_EN_SOUND; + currentConfig.EmuOpt |= EOPT_NO_FRMLIMIT; is_on = 1; emu_status_msg("FAST FORWARD"); } else if (!set_on && is_on) { - PsndOut = set_PsndOut; + PicoIn.sndOut = set_PsndOut; currentConfig.Frameskip = set_Frameskip; currentConfig.EmuOpt = set_EmuOpt; PsndRerate(1); is_on = 0; - // mainly to unbreak pcm - if (PicoAHW & PAHW_MCD) - pcd_state_loaded(); } } @@ -1001,22 +1085,75 @@ void emu_reset_game(void) reset_timing = 1; } -void run_events_pico(unsigned int events) +static u16 *load_pico_overlay(int page, int w, int h) { - int lim_x; - - if (events & PEV_PICO_SWINP) { - pico_inp_mode++; - if (pico_inp_mode > 2) - pico_inp_mode = 0; - switch (pico_inp_mode) { - case 2: emu_status_msg("Input: Pen on Pad"); break; - case 1: emu_status_msg("Input: Pen on Storyware"); break; - case 0: emu_status_msg("Input: Joystick"); - PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000; - break; + static const char *pic_exts[] = { "png", "PNG" }; + char *ext, *fname = NULL; + int extpos, i; + + if (pico_page == page && pico_w == w && pico_h == h) + return pico_overlay; + pico_page = page; + pico_w = w, pico_h = h; + + ext = strrchr(rom_fname_loaded, '.'); + extpos = ext ? ext-rom_fname_loaded : strlen(rom_fname_loaded); + strcpy(static_buff, rom_fname_loaded); + static_buff[extpos++] = '_'; + if (page < 0) { + static_buff[extpos++] = 'p'; + static_buff[extpos++] = 'a'; + static_buff[extpos++] = 'd'; + } else + static_buff[extpos++] = '0'+PicoPicohw.page; + static_buff[extpos++] = '.'; + + for (i = 0; i < ARRAY_SIZE(pic_exts); i++) { + strcpy(static_buff+extpos, pic_exts[i]); + if (access(static_buff, R_OK) == 0) { + printf("found Pico file: %s\n", static_buff); + fname = static_buff; + break; } } + + pico_overlay = realloc(pico_overlay, w*h*2); + memset(pico_overlay, 0, w*h*2); + if (!fname || !pico_overlay || readpng(pico_overlay, fname, READPNG_SCALE, w, h)) { + if (pico_overlay) + free(pico_overlay); + pico_overlay = NULL; + } + + return pico_overlay; +} + +void emu_pico_overlay(u16 *pd, int w, int h, int pitch) +{ + u16 *overlay = NULL; + int y, oh = h; + + // get overlay + if (pico_inp_mode == 1) { + oh = (w/2 < h ? w/2 : h); // storyware has squished h + overlay = load_pico_overlay(PicoPicohw.page, w, oh); + } else if (pico_inp_mode == 2) + overlay = load_pico_overlay(-1, w, oh); + + // copy overlay onto buffer + if (overlay) { + for (y = 0; y < oh; y++) + memcpy(pd + y*pitch, overlay + y*w, w*2); + if (y < h) + memset(pd + y*pitch, 0, w*2); + } +} + +void run_events_pico(unsigned int events) +{ + // treat pad ports equal to support pad in one and mouse in the other + PicoIn.pad[0] |= PicoIn.pad[1]; + if (events & PEV_PICO_PPREV) { PicoPicohw.page--; if (PicoPicohw.page < 0) @@ -1025,39 +1162,81 @@ void run_events_pico(unsigned int events) } if (events & PEV_PICO_PNEXT) { PicoPicohw.page++; - if (PicoPicohw.page > 6) - PicoPicohw.page = 6; - emu_status_msg("Page %i", PicoPicohw.page); + if (PicoPicohw.page > 7) + PicoPicohw.page = 7; + if (PicoPicohw.page == 7) { + // Used in games that require the Keyboard Pico peripheral + emu_status_msg("Test Page"); + } else { + emu_status_msg("Page %i", PicoPicohw.page); + } + } + if (events & PEV_PICO_STORY) { + if (pico_inp_mode == 1) { + pico_inp_mode = 0; + emu_status_msg("Input: D-Pad"); + } else { + pico_inp_mode = 1; + emu_status_msg("Input: Pen on Storyware"); + } + } + if (events & PEV_PICO_PAD) { + if (pico_inp_mode == 2) { + pico_inp_mode = 0; + emu_status_msg("Input: D-Pad"); + } else { + pico_inp_mode = 2; + emu_status_msg("Input: Pen on Pad"); + } + } + + if ((currentConfig.EmuOpt & EOPT_PICO_PEN) && + (PicoIn.pad[0]&0x20) && pico_inp_mode && pico_overlay) { + pico_inp_mode = 0; + emu_status_msg("Input: D-Pad"); } + PicoPicohw.kb.active = (PicoIn.opt & POPT_EN_KBD ? kbd_mode : 0); + if (pico_inp_mode == 0) return; - /* handle other input modes */ - if (PicoPad[0] & 1) pico_pen_y--; - if (PicoPad[0] & 2) pico_pen_y++; - if (PicoPad[0] & 4) pico_pen_x--; - if (PicoPad[0] & 8) pico_pen_x++; - PicoPad[0] &= ~0x0f; // release UDLR - - lim_x = (Pico.video.reg[12]&1) ? 319 : 255; - if (pico_pen_y < 8) - pico_pen_y = 8; - if (pico_pen_y > 224 - PICO_PEN_ADJUST_Y) - pico_pen_y = 224 - PICO_PEN_ADJUST_Y; - if (pico_pen_x < 0) - pico_pen_x = 0; - if (pico_pen_x > lim_x - PICO_PEN_ADJUST_X) - pico_pen_x = lim_x - PICO_PEN_ADJUST_X; - - PicoPicohw.pen_pos[0] = pico_pen_x; - if (!(Pico.video.reg[12] & 1)) - PicoPicohw.pen_pos[0] += pico_pen_x / 4; - PicoPicohw.pen_pos[0] += 0x3c; - PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y); + /* handle other input modes using the pen */ + if (PicoIn.opt & POPT_EN_MOUSE) { + pico_pen_x = PicoIn.mouse[0]; + pico_pen_y = PicoIn.mouse[1]; + } else { + if (PicoIn.pad[0] & 1) pico_pen_y--; + if (PicoIn.pad[0] & 2) pico_pen_y++; + if (PicoIn.pad[0] & 4) pico_pen_x--; + if (PicoIn.pad[0] & 8) pico_pen_x++; + PicoIn.pad[0] &= ~0x0f; // release UDLR + } + + if ((pico_pad ^ PicoIn.pad[0]) & PicoIn.pad[0] & (1< 223-1 - PICO_PEN_ADJUST_Y) + pico_pen_y = 223-1 - PICO_PEN_ADJUST_Y; + if (pico_pen_x < PICO_PEN_ADJUST_X) + pico_pen_x = PICO_PEN_ADJUST_X; + if (pico_pen_x > 319-1 - PICO_PEN_ADJUST_X) + pico_pen_x = 319-1 - PICO_PEN_ADJUST_X; + + PicoPicohw.pen_pos[0] &= 0x8000; + PicoPicohw.pen_pos[1] &= 0x8000; + PicoPicohw.pen_pos[0] |= 0x03c + pico_pen_x; + PicoPicohw.pen_pos[1] |= (pico_inp_mode == 1 ? 0x2f8 : 0x1fc) + pico_pen_y; } -static void do_turbo(int *pad, int acts) +static void do_turbo(unsigned short *pad, int acts) { static int turbo_pad = 0; static unsigned char turbo_cnt[3] = { 0, 0, 0 }; @@ -1114,6 +1293,7 @@ static void run_events_ui(unsigned int which) while (in_menu_wait_any(NULL, 50) & (PBTN_MOK | PBTN_MBACK)) ; in_set_config_int(0, IN_CFG_BLOCKING, 0); + plat_status_msg_clear(); } if (do_it) { plat_status_msg_busy_first((which & PEV_STATE_LOAD) ? "LOADING STATE" : "SAVING STATE"); @@ -1121,10 +1301,7 @@ static void run_events_ui(unsigned int which) emu_save_load_game((which & PEV_STATE_LOAD) ? 1 : 0, 0); PicoStateProgressCB = NULL; } - } - if (which & PEV_SWITCH_RND) - { - plat_video_toggle_renderer(1, 0); + plat_status_msg_busy_done(); } if (which & (PEV_SSLOT_PREV|PEV_SSLOT_NEXT)) { @@ -1141,6 +1318,38 @@ static void run_events_ui(unsigned int which) emu_status_msg("SAVE SLOT %i [%s]", state_slot, emu_check_save_file(state_slot, NULL) ? "USED" : "FREE"); } + if (which & PEV_SWITCH_RND) + { + plat_video_toggle_renderer(1, 0); + } + if (which & PEV_GRAB_INPUT) + { + if (PicoIn.opt & POPT_EN_MOUSE) { + grab_mode = !grab_mode; + in_update_analog(0, 2, &mouse_x); + in_update_analog(0, 3, &mouse_y); + in_update_analog(0, 0, &mouse_x); + in_update_analog(0, 1, &mouse_y); + emu_status_msg("Mouse capture %s", grab_mode ? "on" : "off"); + } else { + grab_mode = 0; + emu_status_msg("No mouse configured"); + } + + plat_grab_cursor(grab_mode); + } + if (which & PEV_SWITCH_KBD) + { + if (! (PicoIn.opt & POPT_EN_KBD)) { + kbd_mode = 0; + emu_status_msg("No keyboard configured"); + } else { + kbd_mode = !kbd_mode; + emu_status_msg("Keyboard %s", kbd_mode ? "on" : "off"); + } + if (! kbd_mode) + plat_video_clear_buffers(); + } if (which & PEV_RESET) emu_reset_game(); if (which & PEV_MENU) @@ -1151,37 +1360,119 @@ void emu_update_input(void) { static int prev_events = 0; int actions[IN_BINDTYPE_COUNT] = { 0, }; - int pl_actions[2]; - int events; + int actions_kbd[IN_BIND_LAST] = { 0, }; + int pl_actions[4]; + int count_kbd = 0, buttons = 0; + int events, i = 0; in_update(actions); pl_actions[0] = actions[IN_BINDTYPE_PLAYER12]; pl_actions[1] = actions[IN_BINDTYPE_PLAYER12] >> 16; + pl_actions[2] = actions[IN_BINDTYPE_PLAYER34]; + pl_actions[3] = actions[IN_BINDTYPE_PLAYER34] >> 16; - PicoPad[0] = pl_actions[0] & 0xfff; - PicoPad[1] = pl_actions[1] & 0xfff; + events = actions[IN_BINDTYPE_EMU] & PEV_MASK; - if (pl_actions[0] & 0x7000) - do_turbo(&PicoPad[0], pl_actions[0]); - if (pl_actions[1] & 0x7000) - do_turbo(&PicoPad[1], pl_actions[1]); + // update mouse coordinates if there is an emulated mouse + if (PicoIn.opt & POPT_EN_MOUSE) { + if (!grab_mode) { + in_update_analog(0, 0, &PicoIn.mouse[0]); + in_update_analog(0, 1, &PicoIn.mouse[1]); + // scale mouse coordinates from -1024..1024 to 0..screen_w/h + PicoIn.mouse[0] = (PicoIn.mouse[0]+1024) * g_screen_width /2048; + PicoIn.mouse[1] = (PicoIn.mouse[1]+1024) * g_screen_height/2048; + } else { + int xrel, yrel; + in_update_analog(0, 2, &xrel); + in_update_analog(0, 3, &yrel); + mouse_x += xrel, mouse_y += yrel; + // scale mouse coordinates from -1024..1024 to 0..screen_w/h + PicoIn.mouse[0] = (mouse_x+1024) * g_screen_width /2048; + PicoIn.mouse[1] = (mouse_y+1024) * g_screen_height/2048; + } - events = actions[IN_BINDTYPE_EMU] & PEV_MASK; + in_update_analog(0, -1, &i); // get mouse buttons, bit 2-0 = RML + if (PicoIn.AHW & PAHW_PICO) { + // TODO is maintaining 2 different mappings necessary? + if (i & 1) buttons |= 1< 52000 && PicoIn.sndRate < 54000) + PicoIn.sndRate = YM2612_NATIVE_RATE(); if (currentConfig.EmuOpt & EOPT_EN_SOUND) { - int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0; + int is_stereo = (PicoIn.opt & POPT_EN_STEREO) ? 1 : 0; + memset(sndBuffer, 0, sizeof(sndBuffer)); + PicoIn.sndOut = sndBuffer; PsndRerate(Pico.m.frame_count ? 1 : 0); printf("starting audio: %i len: %i stereo: %i, pal: %i\n", - PsndRate, PsndLen, is_stereo, Pico.m.pal); - sndout_start(PsndRate, is_stereo); - PicoWriteSound = snd_write_nonblocking; + PicoIn.sndRate, Pico.snd.len, is_stereo, Pico.m.pal); + + sndout_start(PicoIn.sndRate, is_stereo); + PicoIn.writeSound = snd_write_nonblocking; plat_update_volume(0, 0); - memset(sndBuffer, 0, sizeof(sndBuffer)); - PsndOut = sndBuffer; } } @@ -1337,20 +1637,37 @@ static void emu_loop_prep(void) plat_target_gamma_set(currentConfig.gamma, 0); + vkbd = NULL; + if (currentConfig.keyboard == 1) { + if (PicoIn.AHW & PAHW_SMS) vkbd = vkbd_init(0); + else if (PicoIn.AHW & PAHW_PICO) vkbd = vkbd_init(1); + } + PicoIn.opt &= ~POPT_EN_KBD; + if (((PicoIn.AHW & PAHW_PICO) || (PicoIn.AHW & PAHW_SC)) && currentConfig.keyboard) + PicoIn.opt |= POPT_EN_KBD; + + PicoIn.opt &= ~POPT_EN_MOUSE; + if (!(PicoIn.AHW & PAHW_8BIT) && (currentConfig.input_dev0 == PICO_INPUT_MOUSE || + currentConfig.input_dev1 == PICO_INPUT_MOUSE)) { + PicoIn.opt |= POPT_EN_MOUSE; + plat_grab_cursor(grab_mode); + } + pemu_loop_prep(); } /* our tick here is 1 us right now */ -#define ms_to_ticks(x) (unsigned int)(x * 1000) -#define get_ticks() plat_get_ticks_us() +#define ms_to_ticks(x) (int)(x * 1000) +#define get_ticks() plat_get_ticks_us() +#define vsync_delay ms_to_ticks(1) void emu_loop(void) { int frames_done, frames_shown; /* actual frames for fps counter */ - int target_frametime_x3; - unsigned int timestamp_x3 = 0; - unsigned int timestamp_aim_x3 = 0; - unsigned int timestamp_fps_x3 = 0; + int target_frametime; + unsigned int timestamp = 0; + unsigned int timestamp_aim = 0; + unsigned int timestamp_fps = 0; char *notice_msg = NULL; char fpsbuff[24]; int fskip_cnt = 0; @@ -1365,9 +1682,9 @@ void emu_loop(void) /* number of ticks per frame */ if (Pico.m.pal) - target_frametime_x3 = 3 * ms_to_ticks(1000) / 50; + target_frametime = ms_to_ticks(1000) / 50; else - target_frametime_x3 = 3 * ms_to_ticks(1000) / 60; + target_frametime = ms_to_ticks(1000) / 60; reset_timing = 1; frames_done = frames_shown = 0; @@ -1383,28 +1700,26 @@ void emu_loop(void) if (reset_timing) { reset_timing = 0; plat_video_wait_vsync(); - timestamp_aim_x3 = get_ticks() * 3; - timestamp_fps_x3 = timestamp_aim_x3; + timestamp_aim = get_ticks(); + timestamp_fps = timestamp_aim; fskip_cnt = 0; } else if (currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) { - timestamp_aim_x3 = get_ticks() * 3; + timestamp_aim = get_ticks(); } - timestamp_x3 = get_ticks() * 3; + timestamp = get_ticks(); // show notice_msg message? if (notice_msg_time != 0) { static int noticeMsgSum; - if (timestamp_x3 - ms_to_ticks(notice_msg_time) * 3 - > ms_to_ticks(STATUS_MSG_TIMEOUT) * 3) + if (timestamp - ms_to_ticks(notice_msg_time) + > ms_to_ticks(STATUS_MSG_TIMEOUT)) { notice_msg_time = 0; - plat_status_msg_clear(); - plat_video_flip(); - plat_status_msg_clear(); /* Do it again in case of double buffering */ notice_msg = NULL; + plat_status_msg_clear(); } else { int sum = noticeMsg[0] + noticeMsg[1] + noticeMsg[2]; @@ -1417,7 +1732,7 @@ void emu_loop(void) } // second changed? - if (timestamp_x3 - timestamp_fps_x3 >= ms_to_ticks(1000) * 3) + if (timestamp - timestamp_fps >= ms_to_ticks(1000)) { #ifdef BENCHMARK static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4]; @@ -1435,13 +1750,13 @@ void emu_loop(void) snprintf(fpsbuff, 8, "%02i/%02i ", frames_shown, frames_done); #endif frames_shown = frames_done = 0; - timestamp_fps_x3 += ms_to_ticks(1000) * 3; + timestamp_fps += ms_to_ticks(1000); } #ifdef PFRAMES sprintf(fpsbuff, "%i", Pico.m.frame_count); #endif - diff = timestamp_aim_x3 - timestamp_x3; + diff = timestamp_aim - timestamp; if (currentConfig.Frameskip >= 0) // frameskip enabled (or 0) { @@ -1453,26 +1768,38 @@ void emu_loop(void) fskip_cnt = 0; } } - else if (diff < -target_frametime_x3) + else if (diff < -target_frametime) { /* no time left for this frame - skip */ - /* limit auto frameskip to 8 */ - if (frames_done / 8 <= frames_shown) + /* limit auto frameskip to max_skip */ + if (fskip_cnt < currentConfig.max_skip) { + fskip_cnt++; skip = 1; - } + } + else { + fskip_cnt = 0; + } + } else + fskip_cnt = 0; // don't go in debt too much - while (diff < -target_frametime_x3 * 3) { - timestamp_aim_x3 += target_frametime_x3; - diff = timestamp_aim_x3 - timestamp_x3; + while (diff < -target_frametime * 3) { + timestamp_aim += target_frametime; + diff = timestamp_aim - timestamp; } emu_update_input(); + + // 3D glasses + skip |= (PicoIn.AHW & PAHW_SMS) && + (Pico.m.hardware & PMS_HW_3D) && + (PicoMem.zram[0x1ffb] & 1); + if (skip) { - int do_audio = diff > -target_frametime_x3 * 2; - PicoSkipFrame = do_audio ? 1 : 2; + int do_audio = diff > -target_frametime * 2; + PicoIn.skipFrame = do_audio ? 1 : 2; PicoFrame(); - PicoSkipFrame = 0; + PicoIn.skipFrame = 0; } else { PicoFrame(); @@ -1480,7 +1807,7 @@ void emu_loop(void) frames_shown++; } frames_done++; - timestamp_aim_x3 += target_frametime_x3; + timestamp_aim += target_frametime; if (!skip && !flip_after_sync) plat_video_flip(); @@ -1490,18 +1817,18 @@ void emu_loop(void) && !(currentConfig.EmuOpt & (EOPT_NO_FRMLIMIT|EOPT_EXT_FRMLIMIT))) { unsigned int timestamp = get_ticks(); - diff = timestamp_aim_x3 - timestamp * 3; + diff = timestamp_aim - timestamp; // sleep or vsync if we are still too fast - if (diff > target_frametime_x3 && (currentConfig.EmuOpt & EOPT_VSYNC)) { + if (diff > target_frametime + vsync_delay && (currentConfig.EmuOpt & EOPT_VSYNC)) { // we are too fast plat_video_wait_vsync(); timestamp = get_ticks(); - diff = timestamp * 3 - timestamp_aim_x3; + diff = timestamp_aim - timestamp; } - if (diff > target_frametime_x3) { + if (diff > target_frametime + vsync_delay) { // still too fast - plat_wait_till_us(timestamp + (diff - target_frametime_x3) / 3); + plat_wait_till_us(timestamp + (diff - target_frametime)); } } @@ -1514,12 +1841,13 @@ void emu_loop(void) emu_set_fastforward(0); // save SRAM - if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) { + if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && Pico.sv.changed) { plat_status_msg_busy_first("Writing SRAM/BRAM..."); emu_save_load_game(0, 1); - SRam.changed = 0; + Pico.sv.changed = 0; } pemu_loop_end(); emu_sound_stop(); + plat_grab_cursor(0); }