Add a couple of fixes to allow double buffering to work
[picodrive.git] / platform / gp2x / plat.c
CommitLineData
42171343 1#include <stdio.h>
2#include <stdlib.h>
1fb0dd88 3#include <string.h>
75a30842 4#include <linux/input.h>
1fb0dd88 5
e2de9939 6#include "../common/emu.h"
75a30842 7#include "../common/menu_pico.h"
8#include "../common/input_pico.h"
9#include "../libpicofe/input.h"
10#include "../libpicofe/plat.h"
11#include "../libpicofe/linux/in_evdev.h"
12#include "../libpicofe/gp2x/soc.h"
13#include "../libpicofe/gp2x/plat_gp2x.h"
14#include "../libpicofe/gp2x/in_gp2x.h"
15#include "940ctl.h"
16#include "warm.h"
17#include "plat.h"
1fb0dd88 18
ee2a3bdf 19#include <pico/pico.h>
20
42171343 21/* GP2X local */
f4750ee0 22int gp2x_current_bpp;
42171343 23void *gp2x_screens[4];
24
75a30842 25void (*gp2x_video_flip)(void);
26void (*gp2x_video_flip2)(void);
31f944ea 27void (*gp2x_video_changemode_ll)(int bpp, int is_pal);
75a30842 28void (*gp2x_video_setpalette)(int *pal, int len);
29void (*gp2x_video_RGB_setscaling)(int ln_offs, int W, int H);
30void (*gp2x_video_wait_vsync)(void);
45285368 31
75a30842 32static struct in_default_bind in_evdev_defbinds[] =
21ebcfd3 33{
34 /* MXYZ SACB RLDU */
75a30842 35 { KEY_UP, IN_BINDTYPE_PLAYER12, GBTN_UP },
36 { KEY_DOWN, IN_BINDTYPE_PLAYER12, GBTN_DOWN },
37 { KEY_LEFT, IN_BINDTYPE_PLAYER12, GBTN_LEFT },
38 { KEY_RIGHT, IN_BINDTYPE_PLAYER12, GBTN_RIGHT },
39 { KEY_A, IN_BINDTYPE_PLAYER12, GBTN_A },
40 { KEY_S, IN_BINDTYPE_PLAYER12, GBTN_B },
41 { KEY_D, IN_BINDTYPE_PLAYER12, GBTN_C },
42 { KEY_ENTER, IN_BINDTYPE_PLAYER12, GBTN_START },
21ebcfd3 43 { KEY_BACKSLASH, IN_BINDTYPE_EMU, PEVB_MENU },
44 /* Caanoo */
75a30842 45 { BTN_TRIGGER, IN_BINDTYPE_PLAYER12, GBTN_A },
46 { BTN_THUMB, IN_BINDTYPE_PLAYER12, GBTN_B },
47 { BTN_THUMB2, IN_BINDTYPE_PLAYER12, GBTN_C },
48 { BTN_BASE3, IN_BINDTYPE_PLAYER12, GBTN_START },
21ebcfd3 49 { BTN_TOP2, IN_BINDTYPE_EMU, PEVB_STATE_SAVE },
50 { BTN_PINKIE, IN_BINDTYPE_EMU, PEVB_STATE_LOAD },
51 { BTN_BASE, IN_BINDTYPE_EMU, PEVB_MENU },
52 { 0, 0, 0 }
53};
54
75a30842 55static struct in_default_bind in_gp2x_defbinds[] =
56{
57 { GP2X_BTN_UP, IN_BINDTYPE_PLAYER12, GBTN_UP },
58 { GP2X_BTN_DOWN, IN_BINDTYPE_PLAYER12, GBTN_DOWN },
59 { GP2X_BTN_LEFT, IN_BINDTYPE_PLAYER12, GBTN_LEFT },
60 { GP2X_BTN_RIGHT, IN_BINDTYPE_PLAYER12, GBTN_RIGHT },
61 { GP2X_BTN_A, IN_BINDTYPE_PLAYER12, GBTN_A },
62 { GP2X_BTN_X, IN_BINDTYPE_PLAYER12, GBTN_B },
63 { GP2X_BTN_B, IN_BINDTYPE_PLAYER12, GBTN_C },
64 { GP2X_BTN_START, IN_BINDTYPE_PLAYER12, GBTN_START },
65 { GP2X_BTN_Y, IN_BINDTYPE_EMU, PEVB_SWITCH_RND },
66 { GP2X_BTN_L, IN_BINDTYPE_EMU, PEVB_STATE_SAVE },
67 { GP2X_BTN_R, IN_BINDTYPE_EMU, PEVB_STATE_LOAD },
68 { GP2X_BTN_VOL_DOWN, IN_BINDTYPE_EMU, PEVB_VOL_DOWN },
69 { GP2X_BTN_VOL_UP, IN_BINDTYPE_EMU, PEVB_VOL_UP },
70 { GP2X_BTN_SELECT, IN_BINDTYPE_EMU, PEVB_MENU },
71 { 0, 0, 0 }
72};
73
31f944ea 74void gp2x_video_changemode(int bpp, int is_pal)
42171343 75{
31f944ea 76 gp2x_video_changemode_ll(bpp, is_pal);
42171343 77
f4750ee0 78 gp2x_current_bpp = bpp < 0 ? -bpp : bpp;
42171343 79}
80
81static void gp2x_memcpy_buffers(int buffers, void *data, int offset, int len)
82{
83 char *dst;
84 if (buffers & (1<<0)) { dst = (char *)gp2x_screens[0] + offset; if (dst != data) memcpy(dst, data, len); }
85 if (buffers & (1<<1)) { dst = (char *)gp2x_screens[1] + offset; if (dst != data) memcpy(dst, data, len); }
86 if (buffers & (1<<2)) { dst = (char *)gp2x_screens[2] + offset; if (dst != data) memcpy(dst, data, len); }
87 if (buffers & (1<<3)) { dst = (char *)gp2x_screens[3] + offset; if (dst != data) memcpy(dst, data, len); }
88}
89
90void gp2x_memcpy_all_buffers(void *data, int offset, int len)
91{
92 gp2x_memcpy_buffers(0xf, data, offset, len);
93}
94
95void gp2x_memset_all_buffers(int offset, int byte, int len)
96{
97 memset((char *)gp2x_screens[0] + offset, byte, len);
98 memset((char *)gp2x_screens[1] + offset, byte, len);
99 memset((char *)gp2x_screens[2] + offset, byte, len);
100 memset((char *)gp2x_screens[3] + offset, byte, len);
101}
102
0d9bf4fc 103void gp2x_make_fb_bufferable(int yes)
104{
105 int ret = 0;
106
107 yes = yes ? 1 : 0;
108 ret |= warm_change_cb_range(WCB_B_BIT, yes, gp2x_screens[0], 320*240*2);
109 ret |= warm_change_cb_range(WCB_B_BIT, yes, gp2x_screens[1], 320*240*2);
110 ret |= warm_change_cb_range(WCB_B_BIT, yes, gp2x_screens[2], 320*240*2);
111 ret |= warm_change_cb_range(WCB_B_BIT, yes, gp2x_screens[3], 320*240*2);
112
113 if (ret)
114 fprintf(stderr, "could not make fb buferable.\n");
115 else
116 printf("made fb buferable.\n");
117}
118
42171343 119/* common */
1fb0dd88 120void plat_video_menu_enter(int is_rom_loaded)
121{
f4750ee0 122 if (gp2x_current_bpp != 16 || gp2x_dev_id == GP2X_DEV_WIZ) {
123 /* try to switch nicely avoiding glitches */
124 gp2x_video_wait_vsync();
125 memset(gp2x_screens[0], 0, 320*240*2);
126 memset(gp2x_screens[1], 0, 320*240*2);
127 gp2x_video_flip2(); // might flip to fb2/3
128 gp2x_video_flip2(); // ..so we do it again
f4750ee0 129 }
45285368 130 else
131 gp2x_video_flip2();
cc41eb4f 132
1fb0dd88 133 // switch to 16bpp
31f944ea 134 gp2x_video_changemode_ll(16, 0);
1fb0dd88 135 gp2x_video_RGB_setscaling(0, 320, 240);
1fb0dd88 136}
137
138void plat_video_menu_begin(void)
139{
c7eb229a 140 g_menuscreen_ptr = g_screen_ptr;
1fb0dd88 141}
142
143void plat_video_menu_end(void)
144{
1fb0dd88 145 gp2x_video_flip2();
146}
147
75a30842 148void plat_video_menu_leave(void)
42171343 149{
75a30842 150}
f4750ee0 151
75a30842 152void plat_early_init(void)
153{
45285368 154 // just use gettimeofday until plat_init()
155 gp2x_get_ticks_ms = plat_get_ticks_ms_good;
156 gp2x_get_ticks_us = plat_get_ticks_us_good;
ee2a3bdf 157}
158
159void plat_init(void)
160{
75a30842 161 warm_init();
ee2a3bdf 162
75a30842 163 switch (gp2x_dev_id) {
164 case GP2X_DEV_GP2X:
165 sharedmem940_init();
166 vid_mmsp2_init();
ee2a3bdf 167 break;
75a30842 168 case GP2X_DEV_WIZ:
169 case GP2X_DEV_CAANOO:
170 vid_pollux_init();
ee2a3bdf 171 break;
172 }
42171343 173
75a30842 174 g_menuscreen_w = 320;
175 g_menuscreen_h = 240;
42171343 176 gp2x_memset_all_buffers(0, 0, 320*240*2);
177
75a30842 178 gp2x_make_fb_bufferable(1);
179
205bc456 180 // use buffer2 for menubg to save mem (using only buffers 0, 1 in menu)
697746df 181 g_menubg_ptr = gp2x_screens[2];
182
b7d64dbd 183 flip_after_sync = 1;
45285368 184 gp2x_menu_init();
75a30842 185
186 in_evdev_init(in_evdev_defbinds);
187 in_gp2x_init(in_gp2x_defbinds);
188 in_probe();
189 plat_target_setup_input();
42171343 190}
191
192void plat_finish(void)
193{
0d9bf4fc 194 warm_finish();
195
75a30842 196 switch (gp2x_dev_id) {
197 case GP2X_DEV_GP2X:
198 sharedmem940_finish();
199 vid_mmsp2_finish();
42171343 200 break;
75a30842 201 case GP2X_DEV_WIZ:
202 case GP2X_DEV_CAANOO:
203 vid_pollux_finish();
274f95a9 204 break;
42171343 205 }
42171343 206}