more Pandora porting work
authornotaz <notasas@gmail.com>
Wed, 23 Jul 2008 11:21:08 +0000 (11:21 +0000)
committernotaz <notasas@gmail.com>
Wed, 23 Jul 2008 11:21:08 +0000 (11:21 +0000)
git-svn-id: file:///home/notaz/opt/svn/PicoDrive/platform@560 be3aeb3a-fb24-0410-a615-afba39da0efa

common/common.h
gp2x/Makefile
pandora/Makefile
pandora/emu.c [new file with mode: 0644]
pandora/main.c
pandora/pandora.c

index f2964bc..72bd17d 100644 (file)
@@ -101,8 +101,8 @@ void gp2x_pd_clone_buffer2(void);
 void menu_darken_bg(void *dst, int pixels, int darker);
 void menu_flip(void);
 
-#define SCREEN_WIDTH  320
-#define SCREEN_HEIGHT 240
+#define SCREEN_WIDTH  800
+#define SCREEN_HEIGHT 480
 #define SCREEN_BUFFER gp2x_screen
 
 #define read_buttons(which) \
@@ -112,9 +112,9 @@ void menu_flip(void);
 #define menu_draw_begin() \
        gp2x_pd_clone_buffer2()
 #define clear_screen() \
-       memset(gp2x_screen, 0, 320*240*2)
+       memset(gp2x_screen, 0, 800*480*2)
 #define darken_screen() \
-       menu_darken_bg(gp2x_screen, 320*240, 0)
+       menu_darken_bg(gp2x_screen, 800*480, 0)
 #define menu_draw_end() \
        menu_flip()
 
index b032133..57bd810 100644 (file)
@@ -147,7 +147,7 @@ DIRS = platform platform/gp2x platform/common Pico Pico/cd Pico/Pico Pico/sound
 \r
 all: mkdirs PicoDrive.gpe\r
 \r
-PicoDrive.gpe : $(OBJS) ../common/helix/helix_mp3.a\r
+PicoDrive.gpe : $(OBJS) ../common/helix/$(CROSS)helix-mp3.a\r
        @echo ">>>" $@\r
        $(GCC) -o $@ $(COPT) $^ -lm -lpng -Wl,-Map=PicoDrive.map\r
 ifeq ($(DEBUG),)\r
@@ -228,8 +228,8 @@ Pico/Pico.o Pico/cd/Pico.o: ../../Pico/PicoFrameHints.c ../../Pico/PicoInt.h
 Pico/Memory.o Pico/cd/Memory.o : ../../Pico/MemoryCmn.c ../../Pico/PicoInt.h\r
 \r
 # build helix libs\r
-../common/helix/helix_mp3.a:\r
-       make -C ../common/helix\r
+../common/helix/$(CROSS)helix-mp3.a:\r
+       make -C ../common/helix clean all\r
 \r
 readme.txt: ../../tools/textfilter ../base_readme.txt\r
        ../../tools/textfilter ../base_readme.txt $@ GP2X\r
index ffa1d72..016a76f 100644 (file)
@@ -49,7 +49,7 @@ LD = $(CROSS)ld
 OBJCOPY = $(CROSS)objcopy\r
 \r
 # frontend\r
-OBJS += pandora.o main.o platform/gp2x/menu.o platform/gp2x/usbjoy.o platform/gp2x/emu.o\r
+OBJS += pandora.o main.o emu.o platform/gp2x/menu.o platform/gp2x/usbjoy.o\r
 \r
 # common\r
 OBJS += platform/common/emu.o platform/common/menu.o platform/common/fonts.o platform/common/config.o \\r
@@ -141,7 +141,7 @@ DIRS = platform platform/gp2x platform/common Pico Pico/cd Pico/Pico Pico/sound
 \r
 all: mkdirs PicoDrive\r
 \r
-PicoDrive : $(OBJS) ../common/helix/helix_mp3.a\r
+PicoDrive : $(OBJS) ../common/helix/$(CROSS)helix-mp3.a\r
        @echo ">>>" $@\r
        $(GCC) -o $@ $(COPT) $^ -lm -Wl,-Map=PicoDrive.map # -lpng\r
 ifeq ($(DEBUG),)\r
@@ -218,8 +218,8 @@ Pico/Pico.o Pico/cd/Pico.o: ../../Pico/PicoFrameHints.c ../../Pico/PicoInt.h
 Pico/Memory.o Pico/cd/Memory.o : ../../Pico/MemoryCmn.c ../../Pico/PicoInt.h\r
 \r
 # build helix libs\r
-../common/helix/helix_mp3.a:\r
-       make -C ../common/helix\r
+../common/helix/$(CROSS)helix-mp3.a:\r
+       make -C ../common/helix clean all\r
 \r
 readme.txt: ../../tools/textfilter ../base_readme.txt\r
        ../../tools/textfilter ../base_readme.txt $@ GP2X\r
diff --git a/pandora/emu.c b/pandora/emu.c
new file mode 100644 (file)
index 0000000..16fa519
--- /dev/null
@@ -0,0 +1,1014 @@
+// (c) Copyright 2006-2007 notaz, All rights reserved.\r
+// Free for non-commercial use.\r
+\r
+// For commercial use, separate licencing terms must be obtained.\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <sys/time.h>\r
+#include <sys/stat.h>\r
+#include <sys/types.h>\r
+#include <linux/limits.h>\r
+#include <ctype.h>\r
+#include <unistd.h>\r
+\r
+#include <stdarg.h>\r
+\r
+#include "../gp2x/emu.h"\r
+#include "../gp2x/usbjoy.h"\r
+#include "../gp2x/menu.h"\r
+#include "../common/arm_utils.h"\r
+#include "../common/fonts.h"\r
+#include "../common/emu.h"\r
+#include "../common/config.h"\r
+#include "../common/common.h"\r
+\r
+#include <Pico/PicoInt.h>\r
+#include <Pico/Patch.h>\r
+#include <Pico/sound/mix.h>\r
+#include <zlib/zlib.h>\r
+\r
+//#define PFRAMES\r
+\r
+#ifdef BENCHMARK\r
+#define OSD_FPS_X (800-200)\r
+#else\r
+#define OSD_FPS_X (800-120)\r
+#endif\r
+\r
+\r
+int engineState;\r
+int select_exits = 0;\r
+\r
+char romFileName[PATH_MAX];\r
+\r
+static short __attribute__((aligned(4))) sndBuffer[2*44100/50];\r
+static struct timeval noticeMsgTime = { 0, 0 };        // when started showing\r
+static int osd_fps_x;\r
+char noticeMsg[64];                    // notice msg to draw\r
+unsigned char *PicoDraw2FB = NULL;  // temporary buffer for alt renderer\r
+int reset_timing = 0;\r
+\r
+#define PICO_PEN_ADJUST_X 4\r
+#define PICO_PEN_ADJUST_Y 2\r
+static int pico_pen_x = SCREEN_WIDTH/2, pico_pen_y = 240/2;\r
+\r
+static void emu_msg_cb(const char *msg);\r
+static void emu_msg_tray_open(void);\r
+\r
+\r
+void emu_noticeMsgUpdated(void)\r
+{\r
+       gettimeofday(&noticeMsgTime, 0);\r
+}\r
+\r
+void emu_getMainDir(char *dst, int len)\r
+{\r
+       extern char **g_argv;\r
+       int j;\r
+\r
+       strncpy(dst, g_argv[0], len);\r
+       len -= 32; // reserve\r
+       if (len < 0) len = 0;\r
+       dst[len] = 0;\r
+       for (j = strlen(dst); j > 0; j--)\r
+               if (dst[j] == '/') { dst[j+1] = 0; break; }\r
+}\r
+\r
+void emu_Init(void)\r
+{\r
+       // make temp buffer for alt renderer\r
+       PicoDraw2FB = malloc((8+320)*(8+240+8));\r
+       if (!PicoDraw2FB)\r
+       {\r
+               printf("PicoDraw2FB == 0\n");\r
+       }\r
+\r
+       // make dirs for saves, cfgs, etc.\r
+       mkdir("mds", 0777);\r
+       mkdir("srm", 0777);\r
+       mkdir("brm", 0777);\r
+       mkdir("cfg", 0777);\r
+\r
+       PicoInit();\r
+       PicoMessage = emu_msg_cb;\r
+       PicoMCDopenTray = emu_msg_tray_open;\r
+       PicoMCDcloseTray = menu_loop_tray;\r
+}\r
+\r
+\r
+static void scaling_update(void)\r
+{\r
+       PicoOpt &= ~0x4100;\r
+       switch (currentConfig.scaling) {\r
+               default: break; // off\r
+               case 1:  // hw hor\r
+               case 2:  PicoOpt |=  0x0100; break; // hw hor+vert\r
+               case 3:  PicoOpt |=  0x4000; break; // sw hor\r
+       }\r
+}\r
+\r
+\r
+void emu_Deinit(void)\r
+{\r
+       // save SRAM\r
+       if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
+               emu_SaveLoadGame(0, 1);\r
+               SRam.changed = 0;\r
+       }\r
+\r
+       if (!(currentConfig.EmuOpt & 0x20)) {\r
+               config_writelrom(PicoConfigFile);\r
+#ifndef NO_SYNC\r
+               sync();\r
+#endif\r
+       }\r
+\r
+       free(PicoDraw2FB);\r
+\r
+       PicoExit();\r
+}\r
+\r
+void emu_prepareDefaultConfig(void)\r
+{\r
+       memset(&defaultConfig, 0, sizeof(defaultConfig));\r
+       defaultConfig.EmuOpt    = 0x9f | 0x00700; // | <- ram_tmng, confirm_save, cd_leds\r
+       defaultConfig.s_PicoOpt = 0x0f | POPT_EXT_FM|POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC|POPT_ACC_SPRITES|POPT_EN_MCD_GFX;\r
+       defaultConfig.s_PsndRate = 44100;\r
+       defaultConfig.s_PicoRegion = 0; // auto\r
+       defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP\r
+       defaultConfig.s_PicoCDBuffers = 0;\r
+       defaultConfig.Frameskip = 0; // auto\r
+       defaultConfig.CPUclock = 200;\r
+       defaultConfig.volume = 50;\r
+       defaultConfig.scaling = 0;\r
+       defaultConfig.turbo_rate = 15;\r
+}\r
+\r
+void emu_setDefaultConfig(void)\r
+{\r
+       memcpy(&currentConfig, &defaultConfig, sizeof(currentConfig));\r
+       PicoOpt = currentConfig.s_PicoOpt;\r
+       PsndRate = currentConfig.s_PsndRate;\r
+       PicoRegionOverride = currentConfig.s_PicoRegion;\r
+       PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder;\r
+       PicoCDBuffers = currentConfig.s_PicoCDBuffers;\r
+}\r
+\r
+static void textOut16(int x, int y, const char *text)\r
+{\r
+       int i,l,len=strlen(text);\r
+       unsigned int *screen = (unsigned int *)((unsigned short *)SCREEN_BUFFER + (x&~1) + y*SCREEN_WIDTH);\r
+\r
+       for (i = 0; i < len; i++)\r
+       {\r
+               for (l=0;l<16;l+=2)\r
+               {\r
+                       unsigned char fd = fontdata8x8[((text[i])*8)+l/2];\r
+                       int u = l+1;\r
+                       if(fd&0x80) screen[l*SCREEN_WIDTH/2+0]=screen[u*SCREEN_WIDTH/2+0]=0xffffffff;\r
+                       if(fd&0x40) screen[l*SCREEN_WIDTH/2+1]=screen[u*SCREEN_WIDTH/2+1]=0xffffffff;\r
+                       if(fd&0x20) screen[l*SCREEN_WIDTH/2+2]=screen[u*SCREEN_WIDTH/2+2]=0xffffffff;\r
+                       if(fd&0x10) screen[l*SCREEN_WIDTH/2+3]=screen[u*SCREEN_WIDTH/2+3]=0xffffffff;\r
+                       if(fd&0x08) screen[l*SCREEN_WIDTH/2+4]=screen[u*SCREEN_WIDTH/2+4]=0xffffffff;\r
+                       if(fd&0x04) screen[l*SCREEN_WIDTH/2+5]=screen[u*SCREEN_WIDTH/2+5]=0xffffffff;\r
+                       if(fd&0x02) screen[l*SCREEN_WIDTH/2+6]=screen[u*SCREEN_WIDTH/2+6]=0xffffffff;\r
+                       if(fd&0x01) screen[l*SCREEN_WIDTH/2+7]=screen[u*SCREEN_WIDTH/2+7]=0xffffffff;\r
+               }\r
+               screen += 8;\r
+       }\r
+}\r
+\r
+\r
+void osd_text(int x, int y, const char *text)\r
+{\r
+       int len = strlen(text)*8;\r
+\r
+       if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
+               int *p, i, h;\r
+               x &= ~3; // align x\r
+               len = (len+3) >> 2;\r
+               for (h = 0; h < 8; h++) {\r
+                       p = (int *) ((unsigned char *) gp2x_screen+x+SCREEN_WIDTH*(y+h));\r
+                       for (i = len; i; i--, p++) *p = 0xe0e0e0e0;\r
+               }\r
+               emu_textOut8(x, y, text);\r
+       } else {\r
+               int *p, i, h;\r
+               x &= ~1; // align x\r
+               len++;\r
+               for (h = 0; h < 16; h++) {\r
+                       p = (int *) ((unsigned short *) gp2x_screen+x+SCREEN_WIDTH*(y+h));\r
+                       for (i = len; i; i--, p++) *p = (*p>>2)&0x39e7;\r
+               }\r
+               textOut16(x, y, text);\r
+       }\r
+}\r
+\r
+static void draw_cd_leds(void)\r
+{\r
+//     static\r
+       int old_reg;\r
+//     if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change // mmu hack problems?\r
+       old_reg = Pico_mcd->s68k_regs[0];\r
+\r
+       if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
+               // 8-bit modes\r
+               unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;\r
+               unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;\r
+               *(unsigned int *)((char *)gp2x_screen + SCREEN_WIDTH*2+ 4) =\r
+               *(unsigned int *)((char *)gp2x_screen + SCREEN_WIDTH*3+ 4) =\r
+               *(unsigned int *)((char *)gp2x_screen + SCREEN_WIDTH*4+ 4) = col_g;\r
+               *(unsigned int *)((char *)gp2x_screen + SCREEN_WIDTH*2+12) =\r
+               *(unsigned int *)((char *)gp2x_screen + SCREEN_WIDTH*3+12) =\r
+               *(unsigned int *)((char *)gp2x_screen + SCREEN_WIDTH*4+12) = col_r;\r
+       } else {\r
+               // 16-bit modes\r
+               unsigned int *p = (unsigned int *)((short *)gp2x_screen + SCREEN_WIDTH*2+4);\r
+               unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;\r
+               unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;\r
+               *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += SCREEN_WIDTH/2 - 12/2;\r
+               *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += SCREEN_WIDTH/2 - 12/2;\r
+               *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;\r
+       }\r
+}\r
+\r
+static void draw_pico_ptr(void)\r
+{\r
+       unsigned short *p = (unsigned short *)gp2x_screen;\r
+\r
+       // only if pen enabled and for 16bit modes\r
+       if (pico_inp_mode == 0 || (PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) return;\r
+\r
+       if (!(Pico.video.reg[12]&1) && !(PicoOpt&POPT_DIS_32C_BORDER))\r
+               p += 32;\r
+\r
+       p += SCREEN_WIDTH * (pico_pen_y + PICO_PEN_ADJUST_Y);\r
+       p += pico_pen_x + PICO_PEN_ADJUST_X;\r
+       p[0]   ^= 0xffff;\r
+       p[319] ^= 0xffff;\r
+       p[320] ^= 0xffff;\r
+       p[321] ^= 0xffff;\r
+       p[640] ^= 0xffff;\r
+}\r
+\r
+static int EmuScanEnd16(unsigned int num)\r
+{\r
+       unsigned char  *ps=HighCol+8;\r
+       unsigned short *pd;\r
+       unsigned short *pal=HighPal;\r
+       int sh = Pico.video.reg[0xC]&8;\r
+       int len, mask = 0xff;\r
+\r
+       if (!(Pico.video.reg[1]&8)) num += 8;\r
+       pd=(unsigned short *)gp2x_screen + num*800*2 + 800/2 - 320*2/2;\r
+\r
+       if (Pico.m.dirtyPal)\r
+               PicoDoHighPal555(sh);\r
+\r
+       if (Pico.video.reg[12]&1) {\r
+               len = 320;\r
+       } else {\r
+               pd+=32;\r
+               len = 256;\r
+       }\r
+\r
+       if (!sh && (rendstatus & PDRAW_ACC_SPRITES))\r
+               mask=0x3f; // accurate sprites, upper bits are priority stuff\r
+\r
+\r
+       for (; len > 0; len--)\r
+       {\r
+               unsigned int p = pal[*ps++ & mask];\r
+               p |= p << 16;\r
+               *(unsigned int *)pd = p;\r
+               *(unsigned int *)(&pd[800]) = p;\r
+               pd += 2;\r
+       }\r
+\r
+       return 0;\r
+}\r
+\r
+static int EmuScanBegin8(unsigned int num)\r
+{\r
+       if (!(Pico.video.reg[1]&8)) num += 8;\r
+       DrawLineDest = (unsigned char *)  gp2x_screen + SCREEN_WIDTH * num;\r
+\r
+       return 0;\r
+}\r
+\r
+int localPal[0x100];\r
+static void (*vidCpyM2)(void *dest, void *src) = NULL;\r
+\r
+static void blit(const char *fps, const char *notice)\r
+{\r
+       int emu_opt = currentConfig.EmuOpt;\r
+\r
+       if (PicoOpt&0x10)\r
+       {\r
+               // 8bit fast renderer\r
+               if (Pico.m.dirtyPal) {\r
+                       Pico.m.dirtyPal = 0;\r
+                       vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
+                       // feed new palette to our device\r
+                       gp2x_video_setpalette(localPal, 0x40);\r
+               }\r
+               // a hack for VR\r
+               if (PicoRead16Hook == PicoSVPRead16)\r
+                       memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);\r
+               // do actual copy\r
+               vidCpyM2((unsigned char *)gp2x_screen+SCREEN_WIDTH*8, PicoDraw2FB+328*8);\r
+       }\r
+       else if (!(emu_opt&0x80))\r
+       {\r
+               // 8bit accurate renderer\r
+               if (Pico.m.dirtyPal)\r
+               {\r
+                       int pallen = 0x40;\r
+                       Pico.m.dirtyPal = 0;\r
+                       if (Pico.video.reg[0xC]&8) // shadow/hilight mode\r
+                       {\r
+                               vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
+                               vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);\r
+                               vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);\r
+                               memcpy32(localPal+0xc0, localPal+0x40, 0x40);\r
+                               pallen = 0x100;\r
+                       }\r
+                       else if (rendstatus & PDRAW_ACC_SPRITES) {\r
+                               vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
+                               memcpy32(localPal+0x40, localPal, 0x40);\r
+                               memcpy32(localPal+0x80, localPal, 0x40);\r
+                               memcpy32(localPal+0xc0, localPal, 0x40);\r
+                               pallen = 0x100;\r
+                       }\r
+                       else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes\r
+                               vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
+                               vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);\r
+                               vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);\r
+                               pallen = 0xc0;\r
+                       }\r
+                       else {\r
+                               vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
+                       }\r
+                       if (pallen > 0xc0) {\r
+                               localPal[0xc0] = 0x0000c000;\r
+                               localPal[0xd0] = 0x00c00000;\r
+                               localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
+                               localPal[0xf0] = 0x00ffffff;\r
+                       }\r
+                       gp2x_video_setpalette(localPal, pallen);\r
+               }\r
+       }\r
+\r
+       if (notice || (emu_opt & 2)) {\r
+               int h = SCREEN_HEIGHT-16;\r
+               if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8)) h -= 16;\r
+               if (notice) osd_text(4, h, notice);\r
+               if (emu_opt & 2)\r
+                       osd_text(osd_fps_x+24*2, h-8, fps);\r
+       }\r
+       if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))\r
+               draw_cd_leds();\r
+       if (PicoAHW & PAHW_PICO)\r
+               draw_pico_ptr();\r
+{\r
+//static int u=0;\r
+//memset((char *)gp2x_screen+800*471*2, (u++)>>1, 800*9*2);\r
+}\r
+\r
+       //gp2x_video_wait_vsync();\r
+       gp2x_video_flip();\r
+}\r
+\r
+\r
+// clears whole screen or just the notice area (in all buffers)\r
+static void clearArea(int full)\r
+{\r
+       if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
+               // 8-bit renderers\r
+               if (full) gp2x_memset_all_buffers(0, 0xe0, SCREEN_WIDTH*240);\r
+               else      gp2x_memset_all_buffers(SCREEN_WIDTH*232, 0xe0, SCREEN_WIDTH*8);\r
+       } else {\r
+               // 16bit accurate renderer\r
+               if (full) gp2x_memset_all_buffers(0, 0, SCREEN_WIDTH*SCREEN_HEIGHT*2);\r
+               else      gp2x_memset_all_buffers(SCREEN_WIDTH*(SCREEN_HEIGHT-16)*2, 0, SCREEN_WIDTH*16*2);\r
+       }\r
+}\r
+\r
+\r
+static void vidResetMode(void)\r
+{\r
+       if (PicoOpt&0x10) {\r
+               gp2x_video_changemode(8);\r
+       } else if (currentConfig.EmuOpt&0x80) {\r
+               gp2x_video_changemode(16);\r
+               PicoDrawSetColorFormat(-1);\r
+               PicoScanEnd = EmuScanEnd16;\r
+       } else {\r
+               gp2x_video_changemode(8);\r
+               PicoDrawSetColorFormat(2);\r
+               PicoScanBegin = EmuScanBegin8;\r
+       }\r
+       if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
+               // setup pal for 8-bit modes\r
+               localPal[0xc0] = 0x0000c000; // MCD LEDs\r
+               localPal[0xd0] = 0x00c00000;\r
+               localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
+               localPal[0xf0] = 0x00ffffff;\r
+               gp2x_video_setpalette(localPal, 0x100);\r
+               gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
+               gp2x_video_flip();\r
+       }\r
+       Pico.m.dirtyPal = 1;\r
+       // reset scaling\r
+       if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8))\r
+            gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);\r
+       else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);\r
+}\r
+\r
+\r
+static void emu_msg_cb(const char *msg)\r
+{\r
+       if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
+               // 8-bit renderers\r
+               gp2x_memset_all_buffers(SCREEN_WIDTH*(SCREEN_HEIGHT-16), 0xe0, SCREEN_WIDTH*16);\r
+               osd_text(4, SCREEN_HEIGHT-16, msg);\r
+               gp2x_memcpy_all_buffers((char *)gp2x_screen+SCREEN_WIDTH*(SCREEN_HEIGHT-16),\r
+                       SCREEN_WIDTH*(SCREEN_HEIGHT-16), SCREEN_WIDTH*16);\r
+       } else {\r
+               // 16bit accurate renderer\r
+               gp2x_memset_all_buffers(SCREEN_WIDTH*(SCREEN_HEIGHT-16)*2, 0, SCREEN_WIDTH*16*2);\r
+               osd_text(4, SCREEN_HEIGHT-16, msg);\r
+               gp2x_memcpy_all_buffers((char *)gp2x_screen+SCREEN_WIDTH*(SCREEN_HEIGHT-16)*2,\r
+                       SCREEN_WIDTH*(SCREEN_HEIGHT-16)*2, SCREEN_WIDTH*16*2);\r
+       }\r
+       gettimeofday(&noticeMsgTime, 0);\r
+       noticeMsgTime.tv_sec -= 2;\r
+\r
+       /* assumption: emu_msg_cb gets called only when something slow is about to happen */\r
+       reset_timing = 1;\r
+}\r
+\r
+static void emu_state_cb(const char *str)\r
+{\r
+       clearArea(0);\r
+       blit("", str);\r
+}\r
+\r
+static void emu_msg_tray_open(void)\r
+{\r
+       strcpy(noticeMsg, "CD tray opened");\r
+       gettimeofday(&noticeMsgTime, 0);\r
+}\r
+\r
+static void RunEventsPico(unsigned int events, unsigned int gp2x_keys)\r
+{\r
+       int ret, px, py, lim_x;\r
+       static int pdown_frames = 0;\r
+\r
+       emu_RunEventsPico(events);\r
+\r
+       if (pico_inp_mode == 0) return;\r
+\r
+       // for F200\r
+       ret = gp2x_touchpad_read(&px, &py);\r
+       if (ret >= 0)\r
+       {\r
+               if (ret > 35000)\r
+               {\r
+                       if (pdown_frames++ > 5)\r
+                               PicoPad[0] |= 0x20;\r
+\r
+                       pico_pen_x = px;\r
+                       pico_pen_y = py;\r
+                       if (!(Pico.video.reg[12]&1)) {\r
+                               pico_pen_x -= 32;\r
+                               if (pico_pen_x <   0) pico_pen_x = 0;\r
+                               if (pico_pen_x > 248) pico_pen_x = 248;\r
+                       }\r
+                       if (pico_pen_y > 224) pico_pen_y = 224;\r
+               }\r
+               else\r
+                       pdown_frames = 0;\r
+\r
+               //if (ret == 0)\r
+               //      PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;\r
+       }\r
+\r
+       PicoPad[0] &= ~0x0f; // release UDLR\r
+       if (gp2x_keys & GP2X_UP)    pico_pen_y--;\r
+       if (gp2x_keys & GP2X_DOWN)  pico_pen_y++;\r
+       if (gp2x_keys & GP2X_LEFT)  pico_pen_x--;\r
+       if (gp2x_keys & GP2X_RIGHT) pico_pen_x++;\r
+\r
+       lim_x = (Pico.video.reg[12]&1) ? 319 : 255;\r
+       if (pico_pen_y < 8) pico_pen_y = 8;\r
+       if (pico_pen_y > 224-PICO_PEN_ADJUST_Y) pico_pen_y = 224-PICO_PEN_ADJUST_Y;\r
+       if (pico_pen_x < 0) pico_pen_x = 0;\r
+       if (pico_pen_x > lim_x-PICO_PEN_ADJUST_X) pico_pen_x = lim_x-PICO_PEN_ADJUST_X;\r
+\r
+       PicoPicohw.pen_pos[0] = pico_pen_x;\r
+       if (!(Pico.video.reg[12]&1)) PicoPicohw.pen_pos[0] += pico_pen_x/4;\r
+       PicoPicohw.pen_pos[0] += 0x3c;\r
+       PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);\r
+}\r
+\r
+static void update_volume(int has_changed, int is_up)\r
+{\r
+       static int prev_frame = 0, wait_frames = 0;\r
+       int vol = currentConfig.volume;\r
+\r
+       if (has_changed)\r
+       {\r
+               if (vol < 5 && (PicoOpt&8) && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)\r
+                       wait_frames++;\r
+               else {\r
+                       if (is_up) {\r
+                               if (vol < 99) vol++;\r
+                       } else {\r
+                               if (vol >  0) vol--;\r
+                       }\r
+                       wait_frames = 0;\r
+                       gp2x_sound_volume(vol, vol);\r
+                       currentConfig.volume = vol;\r
+               }\r
+               sprintf(noticeMsg, "VOL: %02i", vol);\r
+               gettimeofday(&noticeMsgTime, 0);\r
+               prev_frame = Pico.m.frame_count;\r
+       }\r
+\r
+       // set the right mixer func\r
+       if (!(PicoOpt&8)) return; // just use defaults for mono\r
+       if (vol >= 5)\r
+               PsndMix_32_to_16l = mix_32_to_16l_stereo;\r
+       else {\r
+               mix_32_to_16l_level = 5 - vol;\r
+               PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;\r
+       }\r
+}\r
+\r
+static void RunEvents(unsigned int which)\r
+{\r
+       if (which & 0x1800) // save or load (but not both)\r
+       {\r
+               int do_it = 1;\r
+               if ( emu_checkSaveFile(state_slot) &&\r
+                               (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) ||   // load\r
+                                (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) { // save\r
+                       unsigned long keys;\r
+                       blit("", (which & 0x1000) ? "LOAD STATE? (Y=yes, X=no)" : "OVERWRITE SAVE? (Y=yes, X=no)");\r
+                       while ( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )\r
+                               usleep(50*1024);\r
+                       if (keys & GP2X_X) do_it = 0;\r
+                       while ( gp2x_joystick_read(1) & (GP2X_X|GP2X_Y) ) // wait for release\r
+                               usleep(50*1024);\r
+                       clearArea(0);\r
+               }\r
+               if (do_it) {\r
+                       osd_text(4, SCREEN_HEIGHT-16, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");\r
+                       PicoStateProgressCB = emu_state_cb;\r
+                       gp2x_memcpy_all_buffers(gp2x_screen, 0, SCREEN_WIDTH*SCREEN_HEIGHT*2);\r
+                       emu_SaveLoadGame((which & 0x1000) >> 12, 0);\r
+                       PicoStateProgressCB = NULL;\r
+               }\r
+\r
+               reset_timing = 1;\r
+       }\r
+       if (which & 0x0400) // switch renderer\r
+       {\r
+               if      (  PicoOpt&0x10)             { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }\r
+               else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;\r
+               else   currentConfig.EmuOpt &= ~0x80;\r
+\r
+               vidResetMode();\r
+\r
+               if (PicoOpt&0x10) {\r
+                       strcpy(noticeMsg, " 8bit fast renderer");\r
+               } else if (currentConfig.EmuOpt&0x80) {\r
+                       strcpy(noticeMsg, "16bit accurate renderer");\r
+               } else {\r
+                       strcpy(noticeMsg, " 8bit accurate renderer");\r
+               }\r
+\r
+               gettimeofday(&noticeMsgTime, 0);\r
+       }\r
+       if (which & 0x0300)\r
+       {\r
+               if(which&0x0200) {\r
+                       state_slot -= 1;\r
+                       if(state_slot < 0) state_slot = 9;\r
+               } else {\r
+                       state_slot += 1;\r
+                       if(state_slot > 9) state_slot = 0;\r
+               }\r
+               sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");\r
+               gettimeofday(&noticeMsgTime, 0);\r
+       }\r
+       if (which & 0x0080) {\r
+               engineState = PGS_Menu;\r
+       }\r
+}\r
+\r
+static void updateKeys(void)\r
+{\r
+       unsigned int keys, keys2, allActions[2] = { 0, 0 }, events;\r
+       static unsigned int prevEvents = 0;\r
+       int joy, i;\r
+\r
+       keys = gp2x_joystick_read(0);\r
+       if (keys & GP2X_SELECT) {\r
+               engineState = select_exits ? PGS_Quit : PGS_Menu;\r
+               // wait until select is released, so menu would not resume game\r
+               while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);\r
+       }\r
+\r
+       keys &= CONFIGURABLE_KEYS;\r
+       keys2 = keys;\r
+\r
+       for (i = 0; i < 32; i++)\r
+       {\r
+               if (keys2 & (1 << i))\r
+               {\r
+                       int pl, acts = currentConfig.KeyBinds[i];\r
+                       if (!acts) continue;\r
+                       pl = (acts >> 16) & 1;\r
+                       if (kb_combo_keys & (1 << i))\r
+                       {\r
+                               int u = i+1, acts_c = acts & kb_combo_acts;\r
+                               // let's try to find the other one\r
+                               if (acts_c) {\r
+                                       for (; u < 32; u++)\r
+                                               if ( (keys2 & (1 << u)) && (currentConfig.KeyBinds[u] & acts_c) ) {\r
+                                                       allActions[pl] |= acts_c & currentConfig.KeyBinds[u];\r
+                                                       keys2 &= ~((1 << i) | (1 << u));\r
+                                                       break;\r
+                                               }\r
+                               }\r
+                               // add non-combo actions if combo ones were not found\r
+                               if (!acts_c || u == 32)\r
+                                       allActions[pl] |= acts & ~kb_combo_acts;\r
+                       } else {\r
+                               allActions[pl] |= acts;\r
+                       }\r
+               }\r
+       }\r
+\r
+       // add joy inputs\r
+       if (num_of_joys > 0)\r
+       {\r
+               gp2x_usbjoy_update();\r
+               for (joy = 0; joy < num_of_joys; joy++) {\r
+                       int btns = gp2x_usbjoy_check2(joy);\r
+                       for (i = 0; i < 32; i++) {\r
+                               if (btns & (1 << i)) {\r
+                                       int acts = currentConfig.JoyBinds[joy][i];\r
+                                       int pl = (acts >> 16) & 1;\r
+                                       allActions[pl] |= acts;\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+\r
+       PicoPad[0] = allActions[0] & 0xfff;\r
+       PicoPad[1] = allActions[1] & 0xfff;\r
+\r
+       if (allActions[0] & 0x7000) emu_DoTurbo(&PicoPad[0], allActions[0]);\r
+       if (allActions[1] & 0x7000) emu_DoTurbo(&PicoPad[1], allActions[1]);\r
+\r
+       events = (allActions[0] | allActions[1]) >> 16;\r
+\r
+       // volume is treated in special way and triggered every frame\r
+       if (events & 0x6000)\r
+               update_volume(1, events & 0x2000);\r
+\r
+       if ((events ^ prevEvents) & 0x40) {\r
+               emu_changeFastForward(events & 0x40);\r
+               update_volume(0, 0);\r
+               reset_timing = 1;\r
+       }\r
+\r
+       events &= ~prevEvents;\r
+\r
+       if (PicoAHW == PAHW_PICO)\r
+               RunEventsPico(events, keys);\r
+       if (events) RunEvents(events);\r
+       if (movie_data) emu_updateMovie();\r
+\r
+       prevEvents = (allActions[0] | allActions[1]) >> 16;\r
+}\r
+\r
+\r
+static void updateSound(int len)\r
+{\r
+       if (PicoOpt&8) len<<=1;\r
+\r
+       /* avoid writing audio when lagging behind to prevent audio lag */\r
+       if (PicoSkipFrame != 2)\r
+               gp2x_sound_write(PsndOut, len<<1);\r
+}\r
+\r
+\r
+static void SkipFrame(int do_audio)\r
+{\r
+       PicoSkipFrame=do_audio ? 1 : 2;\r
+       PicoFrame();\r
+       PicoSkipFrame=0;\r
+}\r
+\r
+\r
+void emu_forcedFrame(int opts)\r
+{\r
+       int po_old = PicoOpt;\r
+       int eo_old = currentConfig.EmuOpt;\r
+\r
+       PicoOpt &= ~0x10;\r
+       PicoOpt |= opts|POPT_ACC_SPRITES; // acc_sprites\r
+       currentConfig.EmuOpt |= 0x80;\r
+\r
+       //vidResetMode();\r
+       PicoDrawSetColorFormat(-1);\r
+       PicoScanEnd = EmuScanEnd16;\r
+       Pico.m.dirtyPal = 1;\r
+       PicoFrameDrawOnly();\r
+\r
+/*\r
+       if (!(Pico.video.reg[12]&1)) {\r
+               vidCpyM2 = vidCpyM2_32col;\r
+               clearArea(1);\r
+       } else  vidCpyM2 = vidCpyM2_40col;\r
+\r
+       vidCpyM2((unsigned char *)gp2x_screen+SCREEN_WIDTH*8, PicoDraw2FB+328*8);\r
+       vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
+       gp2x_video_setpalette(localPal, 0x40);\r
+*/\r
+       PicoOpt = po_old;\r
+       currentConfig.EmuOpt = eo_old;\r
+}\r
+\r
+static void simpleWait(int thissec, int lim_time)\r
+{\r
+       struct timeval tval;\r
+\r
+       spend_cycles(1024);\r
+       gettimeofday(&tval, 0);\r
+       if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
+\r
+       while (tval.tv_usec < lim_time)\r
+       {\r
+               spend_cycles(1024);\r
+               gettimeofday(&tval, 0);\r
+               if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
+       }\r
+}\r
+\r
+\r
+void emu_Loop(void)\r
+{\r
+       static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;\r
+       char fpsbuff[24]; // fps count c string\r
+       struct timeval tval; // timing\r
+       int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync\r
+       int  frames_done,  frames_shown,  thissec; // actual frames\r
+       int oldmodes = 0, target_fps, target_frametime, lim_time, vsync_offset, i;\r
+       char *notice = 0;\r
+\r
+       printf("entered emu_Loop()\n");\r
+\r
+       fpsbuff[0] = 0;\r
+\r
+       // make sure we are in correct mode\r
+       vidResetMode();\r
+       scaling_update();\r
+       Pico.m.dirtyPal = 1;\r
+       oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;\r
+       emu_findKeyBindCombos();\r
+\r
+       // pal/ntsc might have changed, reset related stuff\r
+       target_fps = Pico.m.pal ? 50 : 60;\r
+       target_frametime = 1000000/target_fps;\r
+       reset_timing = 1;\r
+\r
+       // prepare sound stuff\r
+       if (currentConfig.EmuOpt & 4)\r
+       {\r
+               int snd_excess_add;\r
+               if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old)\r
+                       PsndRerate(Pico.m.frame_count ? 1 : 0);\r
+\r
+               snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
+               printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r
+                       PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);\r
+               gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);\r
+               gp2x_sound_volume(currentConfig.volume, currentConfig.volume);\r
+               PicoWriteSound = updateSound;\r
+               update_volume(0, 0);\r
+               memset(sndBuffer, 0, sizeof(sndBuffer));\r
+               PsndOut = sndBuffer;\r
+               PsndRate_old = PsndRate;\r
+               PicoOpt_old  = PicoOpt;\r
+               pal_old = Pico.m.pal;\r
+       } else {\r
+               PsndOut = NULL;\r
+       }\r
+\r
+       // prepare CD buffer\r
+       if (PicoAHW & PAHW_MCD) PicoCDBufferInit();\r
+\r
+       // calc vsync offset to sync timing code with vsync\r
+       if (currentConfig.EmuOpt&0x2000) {\r
+               gettimeofday(&tval, 0);\r
+               gp2x_video_wait_vsync();\r
+               gettimeofday(&tval, 0);\r
+               vsync_offset = tval.tv_usec;\r
+               while (vsync_offset >= target_frametime)\r
+                       vsync_offset -= target_frametime;\r
+               if (!vsync_offset) vsync_offset++;\r
+               printf("vsync_offset: %i\n", vsync_offset);\r
+       } else\r
+               vsync_offset = 0;\r
+\r
+       frames_done = frames_shown = thissec =\r
+       pframes_done = pframes_shown = pthissec = 0;\r
+\r
+       // loop\r
+       while (engineState == PGS_Running)\r
+       {\r
+               int modes;\r
+\r
+               gettimeofday(&tval, 0);\r
+               if (reset_timing) {\r
+                       reset_timing = 0;\r
+                       pthissec = tval.tv_sec;\r
+                       pframes_shown = pframes_done = tval.tv_usec/target_frametime;\r
+               }\r
+\r
+               // show notice message?\r
+               if (noticeMsgTime.tv_sec)\r
+               {\r
+                       static int noticeMsgSum;\r
+                       if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec\r
+                               noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;\r
+                               clearArea(0);\r
+                               notice = 0;\r
+                       } else {\r
+                               int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];\r
+                               if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }\r
+                               notice = noticeMsg;\r
+                       }\r
+               }\r
+\r
+               // check for mode changes\r
+               modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);\r
+               if (modes != oldmodes)\r
+               {\r
+                       int scalex = SCREEN_WIDTH;\r
+                       osd_fps_x = OSD_FPS_X;\r
+                       if (modes & 4) {\r
+                               vidCpyM2 = vidCpyM2_40col;\r
+                       } else {\r
+                               if (PicoOpt & 0x100) {\r
+                                       vidCpyM2 = vidCpyM2_32col_nobord;\r
+                                       scalex = 256;\r
+                                       osd_fps_x = OSD_FPS_X - 64;\r
+                               } else {\r
+                                       vidCpyM2 = vidCpyM2_32col;\r
+                               }\r
+                       }\r
+                       if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode\r
+                            gp2x_video_RGB_setscaling(8, scalex, 224);\r
+                       else gp2x_video_RGB_setscaling(0, scalex, 240);\r
+                       oldmodes = modes;\r
+                       clearArea(1);\r
+               }\r
+\r
+               // second changed?\r
+               if (thissec != tval.tv_sec)\r
+               {\r
+#ifdef BENCHMARK\r
+                       static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r
+                       if (++bench == 10) {\r
+                               bench = 0;\r
+                               bench_fps_s = bench_fps;\r
+                               bf[bfp++ & 3] = bench_fps;\r
+                               bench_fps = 0;\r
+                       }\r
+                       bench_fps += frames_shown;\r
+                       sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);\r
+#else\r
+                       if (currentConfig.EmuOpt & 2) {\r
+                               //sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r
+                               sprintf(fpsbuff, "%4i", frames_shown);\r
+                               if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }\r
+                       }\r
+#endif\r
+                       frames_shown = frames_done = 0;\r
+                       thissec = tval.tv_sec;\r
+               }\r
+#ifdef PFRAMES\r
+               sprintf(fpsbuff, "%i", Pico.m.frame_count);\r
+#endif\r
+\r
+               if (pthissec != tval.tv_sec)\r
+               {\r
+                       if (PsndOut == 0 && currentConfig.Frameskip >= 0) {\r
+                               pframes_done = pframes_shown = 0;\r
+                       } else {\r
+                               // it is quite common for this implementation to leave 1 fame unfinished\r
+                               // when second changes, but we don't want buffer to starve.\r
+                               if(PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {\r
+                                       updateKeys();\r
+                                       SkipFrame(1); pframes_done++;\r
+                               }\r
+\r
+                               pframes_done  -= target_fps; if (pframes_done  < 0) pframes_done  = 0;\r
+                               pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;\r
+                               if (pframes_shown > pframes_done) pframes_shown = pframes_done;\r
+                       }\r
+                       pthissec = tval.tv_sec;\r
+               }\r
+\r
+               lim_time = (pframes_done+1) * target_frametime + vsync_offset;\r
+               if (currentConfig.Frameskip >= 0) // frameskip enabled\r
+               {\r
+                       for(i = 0; i < currentConfig.Frameskip; i++) {\r
+                               updateKeys();\r
+                               SkipFrame(1); pframes_done++; frames_done++;\r
+                               if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled\r
+                                       gettimeofday(&tval, 0);\r
+                                       if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;\r
+                                       if (tval.tv_usec < lim_time) { // we are too fast\r
+                                               simpleWait(pthissec, lim_time);\r
+                                       }\r
+                               }\r
+                               lim_time += target_frametime;\r
+                       }\r
+               }\r
+               else if (tval.tv_usec > lim_time) // auto frameskip\r
+               {\r
+                       // no time left for this frame - skip\r
+                       if (tval.tv_usec - lim_time >= 300000) {\r
+                               /* something caused a slowdown for us (disk access? cache flush?)\r
+                                * try to recover by resetting timing... */\r
+                               reset_timing = 1;\r
+                               continue;\r
+                       }\r
+                       updateKeys();\r
+                       SkipFrame(tval.tv_usec < lim_time+target_frametime*2); pframes_done++; frames_done++;\r
+                       continue;\r
+               }\r
+\r
+               updateKeys();\r
+               PicoFrame();\r
+\r
+               // check time\r
+               gettimeofday(&tval, 0);\r
+               if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;\r
+\r
+               if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection\r
+                       reset_timing = 1;\r
+/*             else if (PsndOut != NULL || currentConfig.Frameskip < 0)\r
+               {\r
+                       // sleep or vsync if we are still too fast\r
+                       // usleep sleeps for ~20ms minimum, so it is not a solution here\r
+                       if (!reset_timing && tval.tv_usec < lim_time)\r
+                       {\r
+                               // we are too fast\r
+                               if (vsync_offset) {\r
+                                       if (lim_time - tval.tv_usec > target_frametime/2)\r
+                                               simpleWait(pthissec, lim_time - target_frametime/4);\r
+                                       gp2x_video_wait_vsync();\r
+                               } else {\r
+                                       simpleWait(pthissec, lim_time);\r
+                               }\r
+                       }\r
+               }\r
+*/\r
+               blit(fpsbuff, notice);\r
+\r
+               pframes_done++; pframes_shown++;\r
+                frames_done++;  frames_shown++;\r
+       }\r
+\r
+       emu_changeFastForward(0);\r
+\r
+       if (PicoAHW & PAHW_MCD) PicoCDBufferFree();\r
+\r
+       // save SRAM\r
+       if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
+               emu_state_cb("Writing SRAM/BRAM..");\r
+               emu_SaveLoadGame(0, 1);\r
+               SRam.changed = 0;\r
+       }\r
+\r
+       // if in 8bit mode, generate 16bit image for menu background\r
+       if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))\r
+               emu_forcedFrame(POPT_EN_SOFTSCALE);\r
+}\r
+\r
+\r
+void emu_ResetGame(void)\r
+{\r
+       PicoReset();\r
+       reset_timing = 1;\r
+}\r
+\r
index 8261446..55ece53 100644 (file)
@@ -18,7 +18,6 @@
 #include "../gp2x/version.h"\r
 \r
 \r
-extern char *ext_menu, *ext_state;\r
 extern int select_exits;\r
 extern char *PicoConfigFile;\r
 int mmuhack_status = 0; // TODO rm\r
@@ -32,13 +31,7 @@ void parse_cmd_line(int argc, char *argv[])
        {\r
                if(argv[x][0] == '-')\r
                {\r
-                       if(strcasecmp(argv[x], "-menu") == 0) {\r
-                               if(x+1 < argc) { ++x; ext_menu = argv[x]; } /* External Frontend: Program Name */\r
-                       }\r
-                       else if(strcasecmp(argv[x], "-state") == 0) {\r
-                               if(x+1 < argc) { ++x; ext_state = argv[x]; } /* External Frontend: Arguments */\r
-                       }\r
-                       else if(strcasecmp(argv[x], "-config") == 0) {\r
+                       if(strcasecmp(argv[x], "-config") == 0) {\r
                                if(x+1 < argc) { ++x; PicoConfigFile = argv[x]; }\r
                        }\r
                        else if(strcasecmp(argv[x], "-selectexit") == 0) {\r
index 3fc4690..1779725 100644 (file)
@@ -1,24 +1,3 @@
-/**\r
- * All this is mostly based on rlyeh's minimal library.\r
- * Copied here to review all his code and understand what's going on.\r
-**/\r
-\r
-/*\r
-\r
-  GP2X minimal library v0.A by rlyeh, (c) 2005. emulnation.info@rlyeh (swap it!)\r
-\r
-  Thanks to Squidge, Robster, snaff, Reesy and NK, for the help & previous work! :-)\r
-\r
-  License\r
-  =======\r
-\r
-  Free for non-commercial projects (it would be nice receiving a mail from you).\r
-  Other cases, ask me first.\r
-\r
-  GamePark Holdings is not allowed to use this library and/or use parts from it.\r
-\r
-*/\r
-\r
 \r
 #include <stdio.h>\r
 #include <stdlib.h>\r
 #include "../gp2x/usbjoy.h"\r
 #include "../common/arm_utils.h"\r
 \r
-volatile unsigned short *gp2x_memregs;\r
-//static\r
-volatile unsigned long  *gp2x_memregl;\r
-static void *gp2x_screens[4];\r
-static int screensel = 0;\r
+static volatile unsigned int *memregs;\r
 //static\r
 int memdev = 0;\r
 static int sounddev = -1, mixerdev = -1, touchdev = -1;\r
 static int touchcal[7] = { 6203, 0, -1501397, 0, -4200, 16132680, 65536 };\r
 \r
+#define SCREEN_MAP_SIZE (((800*(480+11)*2)+0xfff)&~0xfff)\r
+static void *screen;\r
 void *gp2x_screen;\r
 \r
-#define FRAMEBUFF_WHOLESIZE (0x30000*4) // 320*240*2 + some more\r
-#define FRAMEBUFF_ADDR0 (0x4000000-FRAMEBUFF_WHOLESIZE)\r
-#define FRAMEBUFF_ADDR1 (FRAMEBUFF_ADDR0+0x30000)\r
-#define FRAMEBUFF_ADDR2 (FRAMEBUFF_ADDR1+0x30000)\r
-#define FRAMEBUFF_ADDR3 (FRAMEBUFF_ADDR2+0x30000)\r
-\r
-static const int gp2x_screenaddrs[4] = { FRAMEBUFF_ADDR0, FRAMEBUFF_ADDR1, FRAMEBUFF_ADDR2, FRAMEBUFF_ADDR3 };\r
-static int gp2x_screenaddrs_use[4];\r
-static unsigned short gp2x_screenaddr_old[4];\r
-\r
 \r
 /* video stuff */\r
 void gp2x_video_flip(void)\r
 {\r
-       unsigned short lsw = (unsigned short) gp2x_screenaddrs_use[screensel&3];\r
-       unsigned short msw = (unsigned short)(gp2x_screenaddrs_use[screensel&3] >> 16);\r
-\r
-       gp2x_memregs[0x2910>>1] = msw;\r
-       gp2x_memregs[0x2914>>1] = msw;\r
-       gp2x_memregs[0x290E>>1] = lsw;\r
-       gp2x_memregs[0x2912>>1] = lsw;\r
-\r
-       // jump to other buffer:\r
-       gp2x_screen = gp2x_screens[++screensel&3];\r
 }\r
 \r
 /* doulblebuffered flip */\r
 void gp2x_video_flip2(void)\r
 {\r
-       unsigned short msw = (unsigned short)(gp2x_screenaddrs_use[screensel&1] >> 16);\r
-\r
-       gp2x_memregs[0x2910>>1] = msw;\r
-       gp2x_memregs[0x2914>>1] = msw;\r
-       gp2x_memregs[0x290E>>1] = 0;\r
-       gp2x_memregs[0x2912>>1] = 0;\r
-\r
-       // jump to other buffer:\r
-       gp2x_screen = gp2x_screens[++screensel&1];\r
 }\r
 \r
 \r
 void gp2x_video_changemode2(int bpp)\r
 {\r
-       gp2x_memregs[0x28DA>>1]=(((bpp+1)/8)<<9)|0xAB; /*8/15/16/24bpp...*/\r
-       gp2x_memregs[0x290C>>1]=320*((bpp+1)/8); /*line width in bytes*/\r
 }\r
 \r
 \r
 void gp2x_video_changemode(int bpp)\r
 {\r
-       gp2x_video_changemode2(bpp);\r
-\r
-       gp2x_memset_all_buffers(0, 0, 320*240*2);\r
-       gp2x_video_flip();\r
 }\r
 \r
 \r
 void gp2x_video_setpalette(int *pal, int len)\r
 {\r
-       unsigned short *g=(unsigned short *)pal;\r
-       volatile unsigned short *memreg = &gp2x_memregs[0x295A>>1];\r
-       gp2x_memregs[0x2958>>1] = 0;\r
-\r
-       len *= 2;\r
-       while(len--) *memreg=*g++;\r
 }\r
 \r
 \r
-// TV Compatible function //\r
 void gp2x_video_RGB_setscaling(int ln_offs, int W, int H)\r
 {\r
-       float escalaw, escalah;\r
-       int bpp = (gp2x_memregs[0x28DA>>1]>>9)&0x3;\r
-       unsigned short scalw;\r
-\r
-       // set offset\r
-       gp2x_screenaddrs_use[0] = gp2x_screenaddrs[0] + ln_offs * 320 * bpp;\r
-       gp2x_screenaddrs_use[1] = gp2x_screenaddrs[1] + ln_offs * 320 * bpp;\r
-       gp2x_screenaddrs_use[2] = gp2x_screenaddrs[2] + ln_offs * 320 * bpp;\r
-       gp2x_screenaddrs_use[3] = gp2x_screenaddrs[3] + ln_offs * 320 * bpp;\r
-\r
-       escalaw = 1024.0; // RGB Horiz LCD\r
-       escalah = 320.0; // RGB Vert LCD\r
-\r
-       if(gp2x_memregs[0x2800>>1]&0x100) //TV-Out\r
-       {\r
-               escalaw=489.0; // RGB Horiz TV (PAL, NTSC)\r
-               if (gp2x_memregs[0x2818>>1]  == 287) //PAL\r
-                       escalah=274.0; // RGB Vert TV PAL\r
-               else if (gp2x_memregs[0x2818>>1]  == 239) //NTSC\r
-                       escalah=331.0; // RGB Vert TV NTSC\r
-       }\r
-\r
-       // scale horizontal\r
-       scalw = (unsigned short)((float)escalaw *(W/320.0));\r
-       /* if there is no horizontal scaling, vertical doesn't work. Here is a nasty wrokaround... */\r
-       if (H != 240 && W == 320) scalw--;\r
-       gp2x_memregs[0x2906>>1]=scalw;\r
-       // scale vertical\r
-       gp2x_memregl[0x2908>>2]=(unsigned long)((float)escalah *bpp *(H/240.0));\r
 }\r
 \r
 \r
 void gp2x_video_wait_vsync(void)\r
 {\r
-       unsigned short v = gp2x_memregs[0x1182>>1];\r
-       while (!((v ^ gp2x_memregs[0x1182>>1]) & 0x10)) spend_cycles(1024);\r
 }\r
 \r
 \r
 void gp2x_video_flush_cache(void)\r
 {\r
-       // since we are using the mmu hack, we must flush the cache first\r
-       // (the params are most likely wrong, but they seem to work somehow)\r
-       //flushcache(addr, addr + 320*240*2, 0);\r
-       flushcache(gp2x_screen, (char *)gp2x_screen + 320*240*2, 0);\r
+//     flushcache(gp2x_screen, (char *)gp2x_screen + 320*240*2, 0);\r
 }\r
 \r
 \r
 void gp2x_memcpy_buffers(int buffers, void *data, int offset, int len)\r
 {\r
-       char *dst;\r
-       if (buffers & (1<<0)) { dst = (char *)gp2x_screens[0] + offset; if (dst != data) memcpy(dst, data, len); }\r
-       if (buffers & (1<<1)) { dst = (char *)gp2x_screens[1] + offset; if (dst != data) memcpy(dst, data, len); }\r
-       if (buffers & (1<<2)) { dst = (char *)gp2x_screens[2] + offset; if (dst != data) memcpy(dst, data, len); }\r
-       if (buffers & (1<<3)) { dst = (char *)gp2x_screens[3] + offset; if (dst != data) memcpy(dst, data, len); }\r
 }\r
 \r
 \r
 void gp2x_memcpy_all_buffers(void *data, int offset, int len)\r
 {\r
-       gp2x_memcpy_buffers(0xf, data, offset, len);\r
 }\r
 \r
 \r
 void gp2x_memset_all_buffers(int offset, int byte, int len)\r
 {\r
-       memset((char *)gp2x_screens[0] + offset, byte, len);\r
-       memset((char *)gp2x_screens[1] + offset, byte, len);\r
-       memset((char *)gp2x_screens[2] + offset, byte, len);\r
-       memset((char *)gp2x_screens[3] + offset, byte, len);\r
+       memset((char *)gp2x_screen + offset, byte, len);\r
 }\r
 \r
 \r
 void gp2x_pd_clone_buffer2(void)\r
 {\r
-       memcpy(gp2x_screen, gp2x_screens[2], 320*240*2);\r
 }\r
 \r
 \r
 unsigned long gp2x_joystick_read(int allow_usb_joy)\r
 {\r
+       unsigned long value = 0;\r
        int i;\r
-       unsigned long value=(gp2x_memregs[0x1198>>1] & 0x00FF); // GPIO M\r
-       if(value==0xFD) value=0xFA;\r
-       if(value==0xF7) value=0xEB;\r
-       if(value==0xDF) value=0xAF;\r
-       if(value==0x7F) value=0xBE;\r
-       value = ~((gp2x_memregs[0x1184>>1] & 0xFF00) | value | (gp2x_memregs[0x1186>>1] << 16)); // C D\r
 \r
        if (allow_usb_joy && num_of_joys > 0) {\r
                // check the usb joy as well..\r
@@ -252,10 +138,11 @@ int gp2x_touchpad_read(int *x, int *y)
 }\r
 \r
 \r
-static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0;\r
+//static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0;\r
 \r
 void gp2x_start_sound(int rate, int bits, int stereo)\r
 {\r
+#if 0\r
        int frag = 0, bsize, buffers;\r
 \r
        // if no settings change, we don't need to do anything\r
@@ -282,72 +169,36 @@ void gp2x_start_sound(int rate, int bits, int stereo)
                rate, bits, stereo?"stereo":"mono", frag>>16, 1<<(frag&0xffff));\r
 \r
        s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo;\r
+#endif\r
 }\r
 \r
 \r
 void gp2x_sound_write(void *buff, int len)\r
 {\r
-       write(sounddev, buff, len);\r
+//     write(sounddev, buff, len);\r
 }\r
 \r
 void gp2x_sound_sync(void)\r
 {\r
-       ioctl(sounddev, SOUND_PCM_SYNC, 0);\r
+//     ioctl(sounddev, SOUND_PCM_SYNC, 0);\r
 }\r
 \r
 void gp2x_sound_volume(int l, int r)\r
 {\r
+#if 0\r
        l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;\r
        l<<=8; l|=r;\r
        ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/\r
-}\r
-\r
-\r
-/* 940 */\r
-void Pause940(int yes)\r
-{\r
-       if(yes)\r
-               gp2x_memregs[0x0904>>1] &= 0xFFFE;\r
-       else\r
-               gp2x_memregs[0x0904>>1] |= 1;\r
-}\r
-\r
-\r
-void Reset940(int yes, int bank)\r
-{\r
-       gp2x_memregs[0x3B48>>1] = ((yes&1) << 7) | (bank & 0x03);\r
-}\r
-\r
-static void proc_set(const char *path, const char *val)\r
-{\r
-       FILE *f;\r
-       char tmp[16];\r
-\r
-       f = fopen(path, "w");\r
-       if (f == NULL) {\r
-               printf("failed to open: %s\n", path);\r
-               return;\r
-       }\r
-\r
-       fprintf(f, "0\n");\r
-       fclose(f);\r
-\r
-       printf("\"%s\" is set to: ", path);\r
-       f = fopen(path, "r");\r
-       if (f == NULL) {\r
-               printf("(open failed)\n");\r
-               return;\r
-       }\r
-\r
-       fgets(tmp, sizeof(tmp), f);\r
-       printf("%s", tmp);\r
-       fclose(f);\r
+#endif\r
 }\r
 \r
 \r
 /* common */\r
 void gp2x_init(void)\r
 {\r
+//     struct fb_fix_screeninfo fbfix;\r
+       int fbdev;\r
+\r
        printf("entering init()\n"); fflush(stdout);\r
 \r
        memdev = open("/dev/mem", O_RDWR);\r
@@ -357,38 +208,37 @@ void gp2x_init(void)
                exit(1);\r
        }\r
 \r
-       gp2x_memregs = mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);\r
-       printf("memregs are @ %p\n", gp2x_memregs);\r
-       if(gp2x_memregs == MAP_FAILED)\r
+       memregs = mmap(0, 0x01000000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x48000000);\r
+       if (memregs == MAP_FAILED)\r
        {\r
                printf("mmap(memregs) failed with %i\n", errno);\r
                exit(1);\r
        }\r
-       gp2x_memregl = (unsigned long *) gp2x_memregs;\r
 \r
-       gp2x_memregs[0x2880>>1] &= ~0x383; // disable cursor, subpict, osd, video layers\r
-\r
-       gp2x_screens[0] = mmap(0, FRAMEBUFF_WHOLESIZE, PROT_WRITE, MAP_SHARED, memdev, FRAMEBUFF_ADDR0);\r
-       if(gp2x_screens[0] == MAP_FAILED)\r
+       fbdev = open("/dev/fb0", O_RDWR);\r
+       if (fbdev == -1)\r
        {\r
-               printf("mmap(gp2x_screen) failed with %i\n", errno);\r
+               printf("open(\"/dev/fb0\") failed with %i\n", errno);\r
                exit(1);\r
        }\r
-       printf("framebuffers point to %p\n", gp2x_screens[0]);\r
-       gp2x_screens[1] = (char *) gp2x_screens[0]+0x30000;\r
-       gp2x_screens[2] = (char *) gp2x_screens[1]+0x30000;\r
-       gp2x_screens[3] = (char *) gp2x_screens[2]+0x30000;\r
-\r
-       gp2x_screen = gp2x_screens[0];\r
-       screensel = 0;\r
 \r
-       gp2x_screenaddr_old[0] = gp2x_memregs[0x290E>>1];\r
-       gp2x_screenaddr_old[1] = gp2x_memregs[0x2910>>1];\r
-       gp2x_screenaddr_old[2] = gp2x_memregs[0x2912>>1];\r
-       gp2x_screenaddr_old[3] = gp2x_memregs[0x2914>>1];\r
+/*\r
+       ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix);\r
+       if (ret == -1)\r
+       {\r
+               printf("ioctl(fbdev) failed with %i\n", errno);\r
+               exit(1);\r
+       }\r
+*/\r
+       screen = mmap(0, SCREEN_MAP_SIZE, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev, 0);\r
+       if (screen == MAP_FAILED)\r
+       {\r
+               printf("mmap(fbptr) failed with %i\n", errno);\r
+               exit(1);\r
+       }\r
+       printf("fbptr %p\n", screen);\r
+       gp2x_screen = (char *)screen + 800*10*2-64;\r
 \r
-       memcpy(gp2x_screenaddrs_use, gp2x_screenaddrs, sizeof(gp2x_screenaddrs));\r
-       gp2x_memset_all_buffers(0, 0, 320*240*2);\r
 \r
        // snd\r
        mixerdev = open("/dev/mixer", O_RDWR);\r
@@ -410,49 +260,23 @@ void gp2x_init(void)
                printf("found touchscreen/wm97xx\n");\r
        }\r
 \r
-       /* disable Linux read-ahead */\r
-       proc_set("/proc/sys/vm/max-readahead", "0\n");\r
-       proc_set("/proc/sys/vm/min-readahead", "0\n");\r
-\r
        printf("exitting init()\n"); fflush(stdout);\r
 }\r
 \r
-char *ext_menu = 0, *ext_state = 0;\r
-\r
 void gp2x_deinit(void)\r
 {\r
-       Reset940(1, 3);\r
-       Pause940(1);\r
-\r
-       gp2x_video_changemode(15);\r
-       gp2x_memregs[0x290E>>1] = gp2x_screenaddr_old[0];\r
-       gp2x_memregs[0x2910>>1] = gp2x_screenaddr_old[1];\r
-       gp2x_memregs[0x2912>>1] = gp2x_screenaddr_old[2];\r
-       gp2x_memregs[0x2914>>1] = gp2x_screenaddr_old[3];\r
+       //gp2x_video_changemode(15);\r
 \r
-       munmap(gp2x_screens[0], FRAMEBUFF_WHOLESIZE);\r
-       munmap((void *)gp2x_memregs, 0x10000);\r
+       munmap(screen, SCREEN_MAP_SIZE);\r
+       munmap((void *)memregs, 0x10000);\r
        close(memdev);\r
-       close(mixerdev);\r
+       if (mixerdev >= 0) close(mixerdev);\r
        if (sounddev >= 0) close(sounddev);\r
        if (touchdev >= 0) close(touchdev);\r
 \r
        gp2x_usbjoy_deinit();\r
 \r
-       printf("all done, running ");\r
-\r
-       // Zaq121's alternative frontend support from MAME\r
-       if(ext_menu && ext_state) {\r
-               printf("%s -state %s\n", ext_menu, ext_state);\r
-               execl(ext_menu, ext_menu, "-state", ext_state, NULL);\r
-       } else if(ext_menu) {\r
-               printf("%s\n", ext_menu);\r
-               execl(ext_menu, ext_menu, NULL);\r
-       } else {\r
-               printf("gp2xmenu\n");\r
-               chdir("/usr/gp2x");\r
-               execl("gp2xmenu", "gp2xmenu", NULL);\r
-       }\r
+       printf("all done");\r
 }\r
 \r
 /* lprintf */\r
@@ -474,4 +298,6 @@ void set_gamma(int g100, int A_SNs_curve) {}
 void set_FCLK(unsigned MHZ) {}\r
 void set_LCD_custom_rate(int rate) {}\r
 void unset_LCD_custom_rate(void) {}\r
+void Pause940(int yes) {}\r
+void Reset940(int yes, int bank) {}\r
 \r