X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=gp2x%2Femu.c;h=0375a7ff99bc0780635a6511b3c167b21008fe87;hb=8e708f920ffd20b026aed616e58c8c8e94c8fb8d;hp=8bd1b66ac3b5944c92861370822ee294b5c160d1;hpb=8ede36b95ea08e10f12e67a487c508a0c8993d6c;p=libpicofe.git diff --git a/gp2x/emu.c b/gp2x/emu.c index 8bd1b66..0375a7f 100644 --- a/gp2x/emu.c +++ b/gp2x/emu.c @@ -21,10 +21,12 @@ #include "../common/arm_utils.h" #include "../common/fonts.h" #include "../common/emu.h" +#include "../common/config.h" #include "cpuctrl.h" #include #include +#include #include //#define PFRAMES @@ -43,10 +45,9 @@ char romFileName[PATH_MAX]; extern int crashed_940; -static short sndBuffer[2*44100/50]; +static short __attribute__((aligned(4))) sndBuffer[2*44100/50]; static struct timeval noticeMsgTime = { 0, 0 }; // when started showing static int osd_fps_x; -static int combo_keys = 0, combo_acts = 0; // keys and actions which need button combos static int gp2x_old_gamma = 100; char noticeMsg[64]; // notice msg to draw unsigned char *PicoDraw2FB = NULL; // temporary buffer for alt renderer @@ -96,36 +97,6 @@ void emu_Init(void) } -static void find_combos(void) -{ - int act, u; - - // find out which keys and actions are combos - combo_keys = combo_acts = 0; - for (act = 0; act < 32; act++) - { - int keyc = 0; - if (act == 16) continue; // player2 flag - for (u = 0; u < 32; u++) - { - if (currentConfig.KeyBinds[u] & (1 << act)) keyc++; - } - if (keyc > 1) - { - // loop again and mark those keys and actions as combo - for (u = 0; u < 32; u++) - { - if (currentConfig.KeyBinds[u] & (1 << act)) { - combo_keys |= 1 << u; - combo_acts |= 1 << act; - } - } - } - } - // printf("combo keys/acts: %08x %08x\n", combo_keys, combo_acts); -} - - static void scaling_update(void) { PicoOpt &= ~0x4100; @@ -147,20 +118,10 @@ void emu_Deinit(void) } if (!(currentConfig.EmuOpt & 0x20)) { - FILE *f = fopen(PicoConfigFile, "r+b"); - if (!f) emu_WriteConfig(0); - else { - // if we already have config, reload it, except last ROM - fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET); - fread(¤tConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f); - fseek(f, 0, SEEK_SET); - fwrite(¤tConfig, 1, sizeof(currentConfig), f); - fflush(f); - fclose(f); + config_writelrom(PicoConfigFile); #ifndef NO_SYNC - sync(); + sync(); #endif - } } free(PicoDraw2FB); @@ -172,34 +133,43 @@ void emu_Deinit(void) set_gamma(100, 0); } +void emu_prepareDefaultConfig(void) +{ + memset(&defaultConfig, 0, sizeof(defaultConfig)); + defaultConfig.EmuOpt = 0x1d | 0x00700; // | <- ram_tmng, confirm_save, cd_leds + defaultConfig.s_PicoOpt = 0x0f | 0x20e00; // | <- use_940, cd_pcm, cd_cdda, svp drc + defaultConfig.s_PsndRate = 44100; + defaultConfig.s_PicoRegion = 0; // auto + defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP + defaultConfig.s_PicoCDBuffers = 64; + defaultConfig.Frameskip = -1; // auto + defaultConfig.CPUclock = 200; + defaultConfig.volume = 50; + defaultConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU + defaultConfig.KeyBinds[ 4] = 1<<1; + defaultConfig.KeyBinds[ 2] = 1<<2; + defaultConfig.KeyBinds[ 6] = 1<<3; + defaultConfig.KeyBinds[14] = 1<<4; + defaultConfig.KeyBinds[13] = 1<<5; + defaultConfig.KeyBinds[12] = 1<<6; + defaultConfig.KeyBinds[ 8] = 1<<7; + defaultConfig.KeyBinds[15] = 1<<26; // switch rend + defaultConfig.KeyBinds[10] = 1<<27; // save state + defaultConfig.KeyBinds[11] = 1<<28; // load state + defaultConfig.KeyBinds[23] = 1<<29; // vol up + defaultConfig.KeyBinds[22] = 1<<30; // vol down + defaultConfig.gamma = 100; + defaultConfig.scaling = 0; +} + void emu_setDefaultConfig(void) { - memset(¤tConfig, 0, sizeof(currentConfig)); - currentConfig.lastRomFile[0] = 0; - currentConfig.EmuOpt = 0x1f | 0x600; // | confirm_save, cd_leds - currentConfig.PicoOpt = 0x0f | 0xe00; // | use_940, cd_pcm, cd_cdda - currentConfig.PsndRate = 22050; // 44100; - currentConfig.PicoRegion = 0; // auto - currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP - currentConfig.Frameskip = -1; // auto - currentConfig.CPUclock = 200; - currentConfig.volume = 50; - currentConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU - currentConfig.KeyBinds[ 4] = 1<<1; - currentConfig.KeyBinds[ 2] = 1<<2; - currentConfig.KeyBinds[ 6] = 1<<3; - currentConfig.KeyBinds[14] = 1<<4; - currentConfig.KeyBinds[13] = 1<<5; - currentConfig.KeyBinds[12] = 1<<6; - currentConfig.KeyBinds[ 8] = 1<<7; - currentConfig.KeyBinds[15] = 1<<26; // switch rend - currentConfig.KeyBinds[10] = 1<<27; // save state - currentConfig.KeyBinds[11] = 1<<28; // load state - currentConfig.KeyBinds[23] = 1<<29; // vol up - currentConfig.KeyBinds[22] = 1<<30; // vol down - currentConfig.gamma = 100; - currentConfig.PicoCDBuffers = 64; - currentConfig.scaling = 0; + memcpy(¤tConfig, &defaultConfig, sizeof(currentConfig)); + PicoOpt = currentConfig.s_PicoOpt; + PsndRate = currentConfig.s_PsndRate; + PicoRegionOverride = currentConfig.s_PicoRegion; + PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder; + PicoCDBuffers = currentConfig.s_PicoCDBuffers; } void osd_text(int x, int y, const char *text) @@ -278,7 +248,8 @@ static void blit(const char *fps, const char *notice) { int emu_opt = currentConfig.EmuOpt; - if (PicoOpt&0x10) { + if (PicoOpt&0x10) + { // 8bit fast renderer if (Pico.m.dirtyPal) { Pico.m.dirtyPal = 0; @@ -286,8 +257,14 @@ static void blit(const char *fps, const char *notice) // feed new palette to our device gp2x_video_setpalette(localPal, 0x40); } + // a hack for VR + if (PicoRead16Hook == PicoSVPRead16) + memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328); + // do actual copy vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8); - } else if (!(emu_opt&0x80)) { + } + else if (!(emu_opt&0x80)) + { // 8bit accurate renderer if (Pico.m.dirtyPal) { Pico.m.dirtyPal = 0; @@ -420,9 +397,69 @@ static void emu_msg_tray_open(void) gettimeofday(¬iceMsgTime, 0); } +static void update_volume(int has_changed, int is_up) +{ + static int prev_frame = 0, wait_frames = 0; + int vol = currentConfig.volume; + + if (has_changed) + { + if (vol < 5 && (PicoOpt&8) && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12) + wait_frames++; + else { + if (is_up) { + if (vol < 99) vol++; + } else { + if (vol > 0) vol--; + } + wait_frames = 0; + gp2x_sound_volume(vol, vol); + currentConfig.volume = vol; + } + sprintf(noticeMsg, "VOL: %02i", vol); + gettimeofday(¬iceMsgTime, 0); + prev_frame = Pico.m.frame_count; + } + + // set the right mixer func + if (!(PicoOpt&8)) return; // just use defaults for mono + if (vol >= 5) + PsndMix_32_to_16l = mix_32_to_16l_stereo; + else { + mix_32_to_16l_level = 5 - vol; + PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl; + } +} + +static void change_fast_forward(int set_on) +{ + static void *set_PsndOut = NULL; + static int set_Frameskip, set_EmuOpt, is_on = 0; + + if (set_on && !is_on) { + set_PsndOut = PsndOut; + set_Frameskip = currentConfig.Frameskip; + set_EmuOpt = currentConfig.EmuOpt; + PsndOut = NULL; + currentConfig.Frameskip = 8; + currentConfig.EmuOpt &= ~4; + is_on = 1; + } + else if (!set_on && is_on) { + PsndOut = set_PsndOut; + currentConfig.Frameskip = set_Frameskip; + currentConfig.EmuOpt = set_EmuOpt; + PsndRerate(1); + update_volume(0, 0); + reset_timing = 1; + is_on = 0; + } +} + static void RunEvents(unsigned int which) { - if(which & 0x1800) { // save or load (but not both) + if (which & 0x1800) // save or load (but not both) + { int do_it = 1; if ( emu_checkSaveFile(state_slot) && (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load @@ -444,7 +481,8 @@ static void RunEvents(unsigned int which) reset_timing = 1; } - if(which & 0x0400) { // switch renderer + if (which & 0x0400) // switch renderer + { if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; } else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10; else currentConfig.EmuOpt &= ~0x80; @@ -461,7 +499,8 @@ static void RunEvents(unsigned int which) gettimeofday(¬iceMsgTime, 0); } - if(which & 0x0300) { + if (which & 0x0300) + { if(which&0x0200) { state_slot -= 1; if(state_slot < 0) state_slot = 9; @@ -472,7 +511,7 @@ static void RunEvents(unsigned int which) sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE"); gettimeofday(¬iceMsgTime, 0); } - if(which & 0x0080) { + if (which & 0x0080) { engineState = PGS_Menu; } } @@ -495,23 +534,26 @@ static void updateKeys(void) for (i = 0; i < 32; i++) { - if (keys & (1 << i)) { + if (keys & (1 << i)) + { int pl, acts = currentConfig.KeyBinds[i]; if (!acts) continue; pl = (acts >> 16) & 1; - if (combo_keys & (1 << i)) { - int u = i+1, acts_c = acts & combo_acts; + if (kb_combo_keys & (1 << i)) + { + int u, acts_c = acts & kb_combo_acts; // let's try to find the other one - if (acts_c) - for (; u < 32; u++) - if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) { - allActions[pl] |= acts_c; + if (acts_c) { + for (u = i + 1; u < 32; u++) + if ( (keys & (1 << u)) && (currentConfig.KeyBinds[u] & acts_c) ) { + allActions[pl] |= acts_c & currentConfig.KeyBinds[u]; keys &= ~((1 << i) | (1 << u)); break; } + } // add non-combo actions if combo ones were not found if (!acts_c || u == 32) - allActions[pl] |= acts & ~combo_acts; + allActions[pl] |= acts & ~kb_combo_acts; } else { allActions[pl] |= acts; } @@ -540,18 +582,11 @@ static void updateKeys(void) events = (allActions[0] | allActions[1]) >> 16; // volume is treated in special way and triggered every frame - if(events & 0x6000) { - int vol = currentConfig.volume; - if (events & 0x2000) { - if (vol < 99) vol++; - } else { - if (vol > 0) vol--; - } - gp2x_sound_volume(vol, vol); - sprintf(noticeMsg, "VOL: %02i", vol); - gettimeofday(¬iceMsgTime, 0); - currentConfig.volume = vol; - } + if (events & 0x6000) + update_volume(1, events & 0x2000); + + if ((events ^ prevEvents) & 0x40) + change_fast_forward(events & 0x40); events &= ~prevEvents; if (events) RunEvents(events); @@ -629,7 +664,7 @@ static void simpleWait(int thissec, int lim_time) void emu_Loop(void) { static int gp2x_old_clock = 200; - static int PsndRate_old = 0, PicoOpt_old = 0, EmuOpt_old = 0, PsndLen_real = 0, pal_old = 0; + static int PsndRate_old = 0, PicoOpt_old = 0, EmuOpt_old = 0, pal_old = 0; char fpsbuff[24]; // fps count c string struct timeval tval; // timing int thissec = 0, frames_done = 0, frames_shown = 0, oldmodes = 0; @@ -665,7 +700,7 @@ void emu_Loop(void) scaling_update(); Pico.m.dirtyPal = 1; oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc; - find_combos(); + emu_findKeyBindCombos(); // pal/ntsc might have changed, reset related stuff target_fps = Pico.m.pal ? 50 : 60; @@ -673,16 +708,12 @@ void emu_Loop(void) reset_timing = 1; // prepare sound stuff - if(currentConfig.EmuOpt & 4) { + if (currentConfig.EmuOpt & 4) + { int snd_excess_add; if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old || ((PicoOpt&0x200) && crashed_940)) { - /* if 940 is turned off, we need it to be put back to sleep */ - if (!(PicoOpt&0x200) && ((PicoOpt^PicoOpt_old)&0x200)) { - Reset940(1, 2); - Pause940(1); - } - sound_rerate(Pico.m.frame_count ? 1 : 0); + PsndRerate(Pico.m.frame_count ? 1 : 0); } snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps; printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n", @@ -690,14 +721,14 @@ void emu_Loop(void) gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3); gp2x_sound_volume(currentConfig.volume, currentConfig.volume); PicoWriteSound = updateSound; + update_volume(0, 0); memset(sndBuffer, 0, sizeof(sndBuffer)); PsndOut = sndBuffer; PsndRate_old = PsndRate; - PsndLen_real = PsndLen; PicoOpt_old = PicoOpt; pal_old = Pico.m.pal; } else { - PsndOut = 0; + PsndOut = NULL; } // prepare CD buffer @@ -722,14 +753,15 @@ void emu_Loop(void) int modes; gettimeofday(&tval, 0); - if(reset_timing) { + if (reset_timing) { reset_timing = 0; thissec = tval.tv_sec; frames_shown = frames_done = tval.tv_usec/target_frametime; } // show notice message? - if(noticeMsgTime.tv_sec) { + if (noticeMsgTime.tv_sec) + { static int noticeMsgSum; if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0; @@ -744,7 +776,8 @@ void emu_Loop(void) // check for mode changes modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8); - if (modes != oldmodes) { + if (modes != oldmodes) + { int scalex = 320; osd_fps_x = OSD_FPS_X; if (modes & 4) { @@ -766,10 +799,11 @@ void emu_Loop(void) } // second changed? - if(thissec != tval.tv_sec) { + if (thissec != tval.tv_sec) + { #ifdef BENCHMARK static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4]; - if(++bench == 10) { + if (++bench == 10) { bench = 0; bench_fps_s = bench_fps; bf[bfp++ & 3] = bench_fps; @@ -778,12 +812,13 @@ void emu_Loop(void) bench_fps += frames_shown; sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2); #else - if(currentConfig.EmuOpt & 2) + if (currentConfig.EmuOpt & 2) sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done); + if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; } #endif thissec = tval.tv_sec; - if(PsndOut == 0 && currentConfig.Frameskip >= 0) { + if (PsndOut == 0 && currentConfig.Frameskip >= 0) { frames_done = frames_shown = 0; } else { // it is quite common for this implementation to leave 1 fame unfinished @@ -807,7 +842,7 @@ void emu_Loop(void) for(i = 0; i < currentConfig.Frameskip; i++) { updateKeys(); SkipFrame(1); frames_done++; - if (PsndOut) { // do framelimitting if sound is enabled + if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled gettimeofday(&tval, 0); if(thissec != tval.tv_sec) tval.tv_usec+=1000000; if(tval.tv_usec < lim_time) { // we are too fast @@ -900,7 +935,7 @@ if (Pico.m.frame_count == 31563) { { // sleep or vsync if we are still too fast // usleep sleeps for ~20ms minimum, so it is not a solution here - if(tval.tv_usec < lim_time) + if (!reset_timing && tval.tv_usec < lim_time) { // we are too fast if (vsync_offset) { @@ -918,6 +953,7 @@ if (Pico.m.frame_count == 31563) { frames_done++; frames_shown++; } + change_fast_forward(0); if (PicoMCD & 1) PicoCDBufferFree();