sms wip: random tweaks, refactoring (palette, sound stuff, etc)
[picodrive.git] / platform / gp2x / emu.c
index dd4b686..77e33f2 100644 (file)
@@ -36,7 +36,7 @@ extern int crashed_940;
 static short __attribute__((aligned(4))) sndBuffer[2*(44100+100)/50];\r
 static unsigned char PicoDraw2FB_[(8+320) * (8+240+8)];\r
 unsigned char *PicoDraw2FB = PicoDraw2FB_;\r
-static int osd_fps_x;\r
+static int osd_fps_x, osd_y;\r
 \r
 extern void *gp2x_screens[4];\r
 \r
@@ -265,21 +265,66 @@ static int EmuScanEnd8_rot(unsigned int num)
        return 0;\r
 }\r
 \r
-int localPal[0x100];\r
-static void (*vidcpyM2)(void *dest, void *src, int m32col, int with_32c_border) = NULL;\r
+static int localPal[0x100];\r
+static void (*vidcpyM2)(void *dest, void *src, int m32col, int with_32c_border);\r
+static int (*make_local_pal)(int fast_mode);\r
+\r
+static int make_local_pal_md(int fast_mode)\r
+{\r
+       int pallen = 0xc0;\r
+\r
+       bgr444_to_rgb32(localPal, Pico.cram);\r
+       if (fast_mode)\r
+               return 0x40;\r
+\r
+       if (Pico.video.reg[0xC] & 8) { // shadow/hilight mode\r
+               bgr444_to_rgb32_sh(localPal, Pico.cram);\r
+               localPal[0xc0] = 0x0000c000;\r
+               localPal[0xd0] = 0x00c00000;\r
+               localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
+               localPal[0xf0] = 0x00ffffff;\r
+               pallen = 0x100;\r
+       }\r
+       else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes\r
+               bgr444_to_rgb32(localPal+0x40, HighPal);\r
+               bgr444_to_rgb32(localPal+0x80, HighPal+0x40);\r
+       }\r
+       else\r
+               memcpy32(localPal+0x80, localPal, 0x40); // for spr prio mess\r
+\r
+       return pallen;\r
+}\r
+\r
+static int make_local_pal_sms(int fast_mode)\r
+{\r
+       unsigned short *spal = Pico.cram;\r
+       unsigned int *dpal = (void *)localPal;\r
+       unsigned int i, t;\r
+\r
+       for (i = 0x40; i > 0; i--) {\r
+               t = *spal++;\r
+               t = ((t & 0x0003) << 22) | ((t & 0x000c) << 12) | ((t & 0x0030) << 2);\r
+               t |= t >> 2;\r
+               t |= t >> 4;\r
+               *dpal++ = t;\r
+       }\r
+\r
+       return 0x40;\r
+}\r
 \r
 void pemu_update_display(const char *fps, const char *notice)\r
 {\r
        int emu_opt = currentConfig.EmuOpt;\r
+       int ret;\r
 \r
        if (PicoOpt & POPT_ALT_RENDERER)\r
        {\r
                // 8bit fast renderer\r
                if (Pico.m.dirtyPal) {\r
                        Pico.m.dirtyPal = 0;\r
-                       vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
+                       ret = make_local_pal(1);\r
                        // feed new palette to our device\r
-                       gp2x_video_setpalette(localPal, 0x40);\r
+                       gp2x_video_setpalette(localPal, ret);\r
                }\r
                // a hack for VR\r
                if (PicoRead16Hook == PicoSVPRead16)\r
@@ -293,43 +338,17 @@ void pemu_update_display(const char *fps, const char *notice)
                // 8bit accurate renderer\r
                if (Pico.m.dirtyPal)\r
                {\r
-                       int pallen = 0xc0;\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_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
-                       }\r
-                       else {\r
-                               vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
-                               memcpy32(localPal+0x80, localPal, 0x40); // for spr prio mess\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
+                       ret = make_local_pal(0);\r
+                       gp2x_video_setpalette(localPal, ret);\r
                }\r
        }\r
 \r
        if (notice || (emu_opt & 2)) {\r
-               int h = 232;\r
-               if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(Pico.video.reg[1]&8))\r
-                       h -= 8;\r
                if (notice)\r
-                       osd_text(4, h, notice);\r
+                       osd_text(4, osd_y, notice);\r
                if (emu_opt & 2)\r
-                       osd_text(osd_fps_x, h, fps);\r
+                       osd_text(osd_fps_x, osd_y, fps);\r
        }\r
        if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))\r
                draw_cd_leds();\r
@@ -474,6 +493,9 @@ static void vidResetMode(void)
        if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(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
+       // palette converters for 8bit modes\r
+       make_local_pal = (PicoAHW & PAHW_SMS) ? make_local_pal_sms : make_local_pal_md;\r
 }\r
 \r
 void plat_video_toggle_renderer(int is_next, int is_menu)\r
@@ -498,6 +520,7 @@ void plat_video_toggle_renderer(int is_next, int is_menu)
                return;\r
 \r
        vidResetMode();\r
+       rendstatus_old = -1;\r
 \r
        if (PicoOpt & POPT_ALT_RENDERER) {\r
                emu_status_msg(" 8bit fast renderer");\r
@@ -694,18 +717,21 @@ void plat_debug_cat(char *str)
 {\r
 }\r
 \r
-void pemu_video_mode_change(int is_32col, int is_240_lines)\r
+void emu_video_mode_change(int start_line, int line_count, int is_32cols)\r
 {\r
        int scalex = 320;\r
        osd_fps_x = OSD_FPS_X;\r
-       if (is_32col && (PicoOpt & POPT_DIS_32C_BORDER)) {\r
+       osd_y = 232;\r
+       if (is_32cols && (PicoOpt & POPT_DIS_32C_BORDER)) {\r
                scalex = 256;\r
                osd_fps_x = OSD_FPS_X - 64;\r
        }\r
+\r
        /* want vertical scaling and game is not in 240 line mode */\r
-       if (currentConfig.scaling == EOPT_SCALE_HW_HV && !is_240_lines)\r
-               gp2x_video_RGB_setscaling(8, scalex, 224);\r
-       else\r
+       if (currentConfig.scaling == EOPT_SCALE_HW_HV) {\r
+               gp2x_video_RGB_setscaling(start_line, scalex, line_count);\r
+               osd_y = start_line + line_count - 8;\r
+       } else\r
                gp2x_video_RGB_setscaling(0, scalex, 240);\r
 \r
        // clear whole screen in all buffers\r
@@ -819,6 +845,7 @@ void pemu_loop_end(void)
        int eo_old = currentConfig.EmuOpt;\r
 \r
        pemu_sound_stop();\r
+       memset32(g_screen_ptr, 0, 320*240*2/4);\r
 \r
        /* do one more frame for menu bg */\r
        PicoOpt &= ~POPT_ALT_RENDERER;\r