32x: a hack for dealing with pwm inaccuracy
[picodrive.git] / platform / gp2x / plat.c
CommitLineData
42171343 1#include <stdio.h>
2#include <stdlib.h>
1fb0dd88 3#include <string.h>
4
0d9bf4fc 5#include "plat_gp2x.h"
42171343 6#include "soc.h"
0d9bf4fc 7#include "warm.h"
1fb0dd88 8#include "../common/plat.h"
9#include "../common/readpng.h"
10#include "../common/menu.h"
e2de9939 11#include "../common/emu.h"
45285368 12#include "../common/input.h"
42171343 13#include "../linux/sndout_oss.h"
1fb0dd88 14
ee2a3bdf 15#include <pico/pico.h>
16
42171343 17/* GP2X local */
0d9bf4fc 18int default_cpu_clock;
f4750ee0 19int gp2x_dev_id;
20int gp2x_current_bpp;
42171343 21void *gp2x_screens[4];
22
45285368 23#include <linux/input.h>
24
25static const char * const caanoo_keys[KEY_MAX + 1] = {
26 [0 ... KEY_MAX] = NULL,
27 [KEY_UP] = "Up",
28 [KEY_LEFT] = "Left",
29 [KEY_RIGHT] = "Right",
30 [KEY_DOWN] = "Down",
31 [BTN_TRIGGER] = "A",
32 [BTN_THUMB] = "X",
33 [BTN_THUMB2] = "B",
34 [BTN_TOP] = "Y",
35 [BTN_TOP2] = "L",
36 [BTN_PINKIE] = "R",
37 [BTN_BASE] = "Home",
38 [BTN_BASE2] = "Lock",
39 [BTN_BASE3] = "I",
40 [BTN_BASE4] = "II",
41 [BTN_BASE5] = "Push",
42};
43
21ebcfd3 44struct in_default_bind in_evdev_defbinds[] =
45{
46 /* MXYZ SACB RLDU */
47 { KEY_UP, IN_BINDTYPE_PLAYER12, 0 },
48 { KEY_DOWN, IN_BINDTYPE_PLAYER12, 1 },
49 { KEY_LEFT, IN_BINDTYPE_PLAYER12, 2 },
50 { KEY_RIGHT, IN_BINDTYPE_PLAYER12, 3 },
51 { KEY_S, IN_BINDTYPE_PLAYER12, 4 }, /* B */
52 { KEY_D, IN_BINDTYPE_PLAYER12, 5 }, /* C */
53 { KEY_A, IN_BINDTYPE_PLAYER12, 6 }, /* A */
54 { KEY_ENTER, IN_BINDTYPE_PLAYER12, 7 },
55 { KEY_BACKSLASH, IN_BINDTYPE_EMU, PEVB_MENU },
56 /* Caanoo */
57 { BTN_THUMB, IN_BINDTYPE_PLAYER12, 4 }, /* B */
58 { BTN_THUMB2, IN_BINDTYPE_PLAYER12, 5 }, /* C */
59 { BTN_TRIGGER, IN_BINDTYPE_PLAYER12, 6 }, /* A */
60 { BTN_BASE3, IN_BINDTYPE_PLAYER12, 7 },
61 { BTN_TOP2, IN_BINDTYPE_EMU, PEVB_STATE_SAVE },
62 { BTN_PINKIE, IN_BINDTYPE_EMU, PEVB_STATE_LOAD },
63 { BTN_BASE, IN_BINDTYPE_EMU, PEVB_MENU },
64 { 0, 0, 0 }
65};
66
42171343 67void gp2x_video_changemode(int bpp)
68{
69 gp2x_video_changemode_ll(bpp);
70
f4750ee0 71 gp2x_current_bpp = bpp < 0 ? -bpp : bpp;
42171343 72}
73
74static void gp2x_memcpy_buffers(int buffers, void *data, int offset, int len)
75{
76 char *dst;
77 if (buffers & (1<<0)) { dst = (char *)gp2x_screens[0] + offset; if (dst != data) memcpy(dst, data, len); }
78 if (buffers & (1<<1)) { dst = (char *)gp2x_screens[1] + offset; if (dst != data) memcpy(dst, data, len); }
79 if (buffers & (1<<2)) { dst = (char *)gp2x_screens[2] + offset; if (dst != data) memcpy(dst, data, len); }
80 if (buffers & (1<<3)) { dst = (char *)gp2x_screens[3] + offset; if (dst != data) memcpy(dst, data, len); }
81}
82
83void gp2x_memcpy_all_buffers(void *data, int offset, int len)
84{
85 gp2x_memcpy_buffers(0xf, data, offset, len);
86}
87
88void gp2x_memset_all_buffers(int offset, int byte, int len)
89{
90 memset((char *)gp2x_screens[0] + offset, byte, len);
91 memset((char *)gp2x_screens[1] + offset, byte, len);
92 memset((char *)gp2x_screens[2] + offset, byte, len);
93 memset((char *)gp2x_screens[3] + offset, byte, len);
94}
95
0d9bf4fc 96void gp2x_make_fb_bufferable(int yes)
97{
98 int ret = 0;
99
100 yes = yes ? 1 : 0;
101 ret |= warm_change_cb_range(WCB_B_BIT, yes, gp2x_screens[0], 320*240*2);
102 ret |= warm_change_cb_range(WCB_B_BIT, yes, gp2x_screens[1], 320*240*2);
103 ret |= warm_change_cb_range(WCB_B_BIT, yes, gp2x_screens[2], 320*240*2);
104 ret |= warm_change_cb_range(WCB_B_BIT, yes, gp2x_screens[3], 320*240*2);
105
106 if (ret)
107 fprintf(stderr, "could not make fb buferable.\n");
108 else
109 printf("made fb buferable.\n");
110}
111
42171343 112/* common */
1fb0dd88 113void plat_video_menu_enter(int is_rom_loaded)
114{
f4750ee0 115 if (gp2x_current_bpp != 16 || gp2x_dev_id == GP2X_DEV_WIZ) {
116 /* try to switch nicely avoiding glitches */
117 gp2x_video_wait_vsync();
118 memset(gp2x_screens[0], 0, 320*240*2);
119 memset(gp2x_screens[1], 0, 320*240*2);
120 gp2x_video_flip2(); // might flip to fb2/3
121 gp2x_video_flip2(); // ..so we do it again
f4750ee0 122 }
45285368 123 else
124 gp2x_video_flip2();
cc41eb4f 125
1fb0dd88 126 // switch to 16bpp
42171343 127 gp2x_video_changemode_ll(16);
1fb0dd88 128 gp2x_video_RGB_setscaling(0, 320, 240);
1fb0dd88 129}
130
131void plat_video_menu_begin(void)
132{
c7eb229a 133 g_menuscreen_ptr = g_screen_ptr;
1fb0dd88 134}
135
136void plat_video_menu_end(void)
137{
1fb0dd88 138 gp2x_video_flip2();
139}
140
ee2a3bdf 141void plat_early_init(void)
42171343 142{
143 gp2x_soc_t soc;
f4750ee0 144 FILE *f;
42171343 145
146 soc = soc_detect();
147 switch (soc)
148 {
149 case SOCID_MMSP2:
0d9bf4fc 150 default_cpu_clock = 200;
f4750ee0 151 gp2x_dev_id = GP2X_DEV_GP2X;
42171343 152 break;
153 case SOCID_POLLUX:
0d9bf4fc 154 default_cpu_clock = 533;
f4750ee0 155 f = fopen("/dev/accel", "rb");
156 if (f) {
157 printf("detected Caanoo\n");
158 gp2x_dev_id = GP2X_DEV_CAANOO;
159 fclose(f);
160 }
161 else {
162 printf("detected Wiz\n");
163 gp2x_dev_id = GP2X_DEV_WIZ;
164 }
42171343 165 break;
166 default:
274f95a9 167 printf("could not recognize SoC, running in dummy mode.\n");
168 break;
42171343 169 }
f4750ee0 170
45285368 171 // just use gettimeofday until plat_init()
172 gp2x_get_ticks_ms = plat_get_ticks_ms_good;
173 gp2x_get_ticks_us = plat_get_ticks_us_good;
ee2a3bdf 174}
175
176void plat_init(void)
177{
178 gp2x_soc_t soc;
179
180 soc = soc_detect();
181 switch (soc)
182 {
183 case SOCID_MMSP2:
184 mmsp2_init();
185 break;
186 case SOCID_POLLUX:
187 pollux_init();
ee2a3bdf 188 break;
189 default:
274f95a9 190 dummy_init();
ee2a3bdf 191 break;
192 }
42171343 193
0d9bf4fc 194 warm_init();
195
42171343 196 gp2x_memset_all_buffers(0, 0, 320*240*2);
197
205bc456 198 // use buffer2 for menubg to save mem (using only buffers 0, 1 in menu)
697746df 199 g_menubg_ptr = gp2x_screens[2];
200
45285368 201 if (gp2x_dev_id == GP2X_DEV_CAANOO)
202 in_set_config(in_name_to_id("evdev:pollux-analog"), IN_CFG_KEY_NAMES,
203 caanoo_keys, sizeof(caanoo_keys));
204
205 gp2x_menu_init();
42171343 206}
207
208void plat_finish(void)
209{
0d9bf4fc 210 gp2x_soc_t soc;
211
212 warm_finish();
213
214 soc = soc_detect();
215 switch (soc)
42171343 216 {
217 case SOCID_MMSP2:
218 mmsp2_finish();
219 break;
220 case SOCID_POLLUX:
221 pollux_finish();
222 break;
274f95a9 223 default:
224 dummy_finish();
225 break;
42171343 226 }
42171343 227}
228