SH2 drc, dummy soc for GP2X
[libpicofe.git] / linux / gp2x.c
... / ...
CommitLineData
1/* faking/emulating gp2x by using xlib */
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <stdarg.h>
6#include <pthread.h>
7#include <semaphore.h>
8
9#include <unistd.h>
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <fcntl.h>
13#include <errno.h>
14
15#include "../gp2x/version.h"
16#include "../common/emu.h"
17#include "../common/menu.h"
18#include "../common/readpng.h"
19#include "sndout_oss.h"
20
21#include "log_io.h"
22
23unsigned long current_keys = 0;
24static int current_bpp = 8;
25static int current_pal[256];
26static const char *verstring = "PicoDrive " VERSION;
27static int scr_changed = 0, scr_w = SCREEN_WIDTH, scr_h = SCREEN_HEIGHT;
28void *gp2x_screens[4];
29
30// dummies
31int mix_32_to_16l_level;
32int crashed_940 = 0;
33int default_cpu_clock = 123;
34void *gp2x_memregs = NULL;
35
36/* ifndef is for qemu build without video out */
37#ifndef ARM
38
39/* faking GP2X pad */
40enum { GP2X_UP=0x1, GP2X_LEFT=0x4, GP2X_DOWN=0x10, GP2X_RIGHT=0x40,
41 GP2X_START=1<<8, GP2X_SELECT=1<<9, GP2X_L=1<<10, GP2X_R=1<<11,
42 GP2X_A=1<<12, GP2X_B=1<<13, GP2X_X=1<<14, GP2X_Y=1<<15,
43 GP2X_VOL_UP=1<<23, GP2X_VOL_DOWN=1<<22, GP2X_PUSH=1<<27 };
44
45static void key_press_event(int keycode)
46{
47 switch (keycode)
48 {
49 case 111:
50 case 0x62: current_keys |= GP2X_UP; break;
51 case 116:
52 case 0x68: current_keys |= GP2X_DOWN; break;
53 case 113:
54 case 0x64: current_keys |= GP2X_LEFT; break;
55 case 114:
56 case 0x66: current_keys |= GP2X_RIGHT; break;
57 case 0x24: current_keys |= GP2X_START; break; // enter
58 case 0x23: current_keys |= GP2X_SELECT;break; // ]
59 case 0x34: current_keys |= GP2X_A; break; // z
60 case 0x35: current_keys |= GP2X_X; break; // x
61 case 0x36: current_keys |= GP2X_B; break; // c
62 case 0x37: current_keys |= GP2X_Y; break; // v
63 case 0x27: current_keys |= GP2X_L; break; // s
64 case 0x28: current_keys |= GP2X_R; break; // d
65 case 0x29: current_keys |= GP2X_PUSH; break; // f
66 case 0x18: current_keys |= GP2X_VOL_DOWN;break; // q
67 case 0x19: current_keys |= GP2X_VOL_UP;break; // w
68 case 0x2d: log_io_clear(); break; // k
69 case 0x2e: log_io_dump(); break; // l
70 case 0x17: { // tab
71 extern int PicoReset(void);
72 PicoReset();
73 break;
74 }
75 }
76}
77
78static void key_release_event(int keycode)
79{
80 switch (keycode)
81 {
82 case 111:
83 case 0x62: current_keys &= ~GP2X_UP; break;
84 case 116:
85 case 0x68: current_keys &= ~GP2X_DOWN; break;
86 case 113:
87 case 0x64: current_keys &= ~GP2X_LEFT; break;
88 case 114:
89 case 0x66: current_keys &= ~GP2X_RIGHT; break;
90 case 0x24: current_keys &= ~GP2X_START; break; // enter
91 case 0x23: current_keys &= ~GP2X_SELECT;break; // ]
92 case 0x34: current_keys &= ~GP2X_A; break; // z
93 case 0x35: current_keys &= ~GP2X_X; break; // x
94 case 0x36: current_keys &= ~GP2X_B; break; // c
95 case 0x37: current_keys &= ~GP2X_Y; break; // v
96 case 0x27: current_keys &= ~GP2X_L; break; // s
97 case 0x28: current_keys &= ~GP2X_R; break; // d
98 case 0x29: current_keys &= ~GP2X_PUSH; break; // f
99 case 0x18: current_keys &= ~GP2X_VOL_DOWN;break; // q
100 case 0x19: current_keys &= ~GP2X_VOL_UP;break; // w
101 }
102}
103
104/* --- */
105
106#include <X11/Xlib.h>
107#include <X11/Xutil.h>
108
109static Display *xlib_display;
110static Window xlib_window;
111static XImage *ximage;
112
113static void ximage_realloc(Display *display, Visual *visual)
114{
115 void *xlib_screen;
116
117 XLockDisplay(xlib_display);
118
119 if (ximage != NULL)
120 XDestroyImage(ximage);
121 ximage = NULL;
122
123 xlib_screen = calloc(scr_w * scr_h, 4);
124 if (xlib_screen != NULL)
125 ximage = XCreateImage(display, visual, 24, ZPixmap, 0,
126 xlib_screen, scr_w, scr_h, 32, 0);
127 if (ximage == NULL)
128 fprintf(stderr, "failed to alloc ximage\n");
129
130 XUnlockDisplay(xlib_display);
131}
132
133static void xlib_update(void)
134{
135 Status xstatus;
136
137 XLockDisplay(xlib_display);
138
139 xstatus = XPutImage(xlib_display, xlib_window, DefaultGC(xlib_display, 0), ximage,
140 0, 0, 0, 0, g_screen_width, g_screen_height);
141 if (xstatus != 0)
142 fprintf(stderr, "XPutImage %d\n", xstatus);
143
144 XUnlockDisplay(xlib_display);
145}
146
147static void *xlib_threadf(void *targ)
148{
149 unsigned int width, height, display_width, display_height;
150 sem_t *sem = targ;
151 XTextProperty windowName;
152 Window win;
153 XEvent report;
154 Display *display;
155 Visual *visual;
156 int screen;
157
158 XInitThreads();
159
160 xlib_display = display = XOpenDisplay(NULL);
161 if (display == NULL)
162 {
163 fprintf(stderr, "cannot connect to X server %s\n",
164 XDisplayName(NULL));
165 sem_post(sem);
166 return NULL;
167 }
168
169 visual = DefaultVisual(display, 0);
170 if (visual->class != TrueColor)
171 {
172 fprintf(stderr, "cannot handle non true color visual\n");
173 XCloseDisplay(display);
174 sem_post(sem);
175 return NULL;
176 }
177
178 printf("X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display),
179 VendorRelease(display), DisplayString(display), ProtocolVersion(display),
180 ProtocolRevision(display));
181
182 screen = DefaultScreen(display);
183
184 ximage_realloc(display, visual);
185 sem_post(sem);
186
187 display_width = DisplayWidth(display, screen);
188 display_height = DisplayHeight(display, screen);
189
190 xlib_window = win = XCreateSimpleWindow(display,
191 RootWindow(display, screen),
192 display_width / 2 - scr_w / 2,
193 display_height / 2 - scr_h / 2,
194 scr_w + 2, scr_h + 2, 1,
195 BlackPixel(display, screen),
196 BlackPixel(display, screen));
197
198 XStringListToTextProperty((char **)&verstring, 1, &windowName);
199 XSetWMName(display, win, &windowName);
200
201 XSelectInput(display, win, ExposureMask |
202 KeyPressMask | KeyReleaseMask |
203 StructureNotifyMask);
204
205 XMapWindow(display, win);
206
207 while (1)
208 {
209 XNextEvent(display, &report);
210 switch (report.type)
211 {
212 case Expose:
213 while (XCheckTypedEvent(display, Expose, &report))
214 ;
215 xlib_update();
216 break;
217
218 case ConfigureNotify:
219 width = report.xconfigure.width;
220 height = report.xconfigure.height;
221 if (scr_w != width - 2 || scr_h != height - 2) {
222 scr_w = width - 2;
223 scr_h = height - 2;
224 scr_changed = 1;
225 }
226 break;
227
228 case ButtonPress:
229 break;
230
231 case KeyPress:
232 key_press_event(report.xkey.keycode);
233 break;
234
235 case KeyRelease:
236 key_release_event(report.xkey.keycode);
237 break;
238
239 default:
240 break;
241 }
242 }
243}
244
245static void xlib_init(void)
246{
247 pthread_t x_thread;
248 sem_t xlib_sem;
249
250 sem_init(&xlib_sem, 0, 0);
251
252 pthread_create(&x_thread, NULL, xlib_threadf, &xlib_sem);
253 pthread_detach(x_thread);
254
255 sem_wait(&xlib_sem);
256 sem_destroy(&xlib_sem);
257}
258#endif // !ARM
259
260/* --- */
261
262static void realloc_screen(void)
263{
264 void *old = g_screen_ptr;
265 int i;
266 g_screen_width = scr_w;
267 g_screen_height = scr_h;
268 g_screen_ptr = calloc(g_screen_width * g_screen_height * 2, 1);
269 free(old);
270 scr_changed = 0;
271
272 for (i = 0; i < 4; i++)
273 gp2x_screens[i] = g_screen_ptr;
274}
275
276/* gp2x/emu.c stuff, most to be rm'd */
277static void gp2x_video_flip_(void)
278{
279#ifndef ARM
280 unsigned int *image;
281 int pixel_count, i;
282
283 if (ximage == NULL)
284 return;
285
286 pixel_count = g_screen_width * g_screen_height;
287 image = (void *)ximage->data;
288
289 if (current_bpp == 8)
290 {
291 unsigned char *pixels = g_screen_ptr;
292 int pix;
293
294 for (i = 0; i < pixel_count; i++)
295 {
296 pix = current_pal[pixels[i]];
297 image[i] = pix;
298 }
299 }
300 else
301 {
302 unsigned short *pixels = g_screen_ptr;
303
304 for (i = 0; i < pixel_count; i++)
305 {
306 /* in: rrrr rggg gggb bbbb */
307 /* out: rrrr r000 gggg gg00 bbbb b000 */
308 image[i] = (pixels[i] << 8) & 0xf80000;
309 image[i] |= (pixels[i] << 5) & 0x00fc00;
310 image[i] |= (pixels[i] << 3) & 0x0000f8;
311 }
312 }
313 xlib_update();
314
315 if (scr_changed) {
316 realloc_screen();
317 ximage_realloc(xlib_display, DefaultVisual(xlib_display, 0));
318 }
319#endif
320}
321
322static void gp2x_video_changemode_ll_(int bpp)
323{
324 current_bpp = bpp;
325}
326
327static void gp2x_video_setpalette_(int *pal, int len)
328{
329 memcpy(current_pal, pal, len*4);
330}
331
332void gp2x_memcpy_all_buffers(void *data, int offset, int len)
333{
334}
335
336void gp2x_memset_all_buffers(int offset, int byte, int len)
337{
338 memset((char *)g_screen_ptr + offset, byte, len);
339}
340
341void gp2x_video_changemode(int bpp)
342{
343 gp2x_video_changemode_ll_(bpp);
344}
345
346void gp2x_make_fb_bufferable(int yes)
347{
348}
349
350int soc_detect(void)
351{
352 return 0;
353}
354
355/* plat */
356static char menu_bg_buffer[320*240*2];
357char cpu_clk_name[16] = "GP2X CPU clocks";
358
359void plat_video_menu_enter(int is_rom_loaded)
360{
361 if (is_rom_loaded)
362 {
363 // darken the active framebuffer
364 memset(g_screen_ptr, 0, 320*8*2);
365 menu_darken_bg((char *)g_screen_ptr + 320*8*2, 320*224, 1);
366 memset((char *)g_screen_ptr + 320*232*2, 0, 320*8*2);
367 }
368 else
369 {
370 char buff[256];
371
372 // should really only happen once, on startup..
373 emu_make_path(buff, "skin/background.png", sizeof(buff));
374 if (readpng(g_screen_ptr, buff, READPNG_BG) < 0)
375 memset(g_screen_ptr, 0, 320*240*2);
376 }
377
378 memcpy(menu_bg_buffer, g_screen_ptr, 320*240*2);
379
380 // switch to 16bpp
381 gp2x_video_changemode_ll_(16);
382 gp2x_video_flip_();
383}
384
385void plat_video_menu_begin(void)
386{
387 memcpy(g_screen_ptr, menu_bg_buffer, 320*240*2);
388}
389
390void plat_video_menu_end(void)
391{
392 gp2x_video_flip_();
393}
394
395void plat_validate_config(void)
396{
397 extern int PicoOpt;
398// PicoOpt &= ~POPT_EXT_FM;
399 PicoOpt &= ~(1<<17); // POPT_EN_SVP_DRC
400}
401
402void plat_early_init(void)
403{
404}
405
406void plat_init(void)
407{
408 realloc_screen();
409 memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);
410
411 // snd
412 sndout_oss_init();
413
414#ifndef ARM
415 xlib_init();
416#endif
417}
418
419void plat_finish(void)
420{
421 free(g_screen_ptr);
422 sndout_oss_exit();
423}
424
425/* nasty */
426static void do_nothing()
427{
428}
429
430void *gp2x_video_flip = gp2x_video_flip_;
431void *gp2x_video_flip2 = gp2x_video_flip_;
432void *gp2x_video_changemode_ll = gp2x_video_changemode_ll_;
433void *gp2x_video_setpalette = gp2x_video_setpalette_;
434
435void *gp2x_video_RGB_setscaling = do_nothing;
436void *gp2x_video_wait_vsync = do_nothing;
437void *gp2x_set_cpuclk = do_nothing;
438void *gp2x_read_battery = do_nothing;
439void *set_lcd_custom_rate = do_nothing;
440void *unset_lcd_custom_rate = do_nothing;
441void *set_lcd_gamma = do_nothing;
442void *set_ram_timings = do_nothing;
443void *unset_ram_timings = do_nothing;
444
445/* joy */
446int gp2x_touchpad_read(int *x, int *y)
447{
448 return -1;
449}
450
451/* misc */
452void spend_cycles(int c)
453{
454 usleep(c/200);
455}
456
457int mp3_get_bitrate(FILE *f, int size)
458{
459 return 128;
460}
461
462void mp3_start_play(FILE *f, int pos)
463{
464}
465
466void mp3_update(int *buffer, int length, int stereo)
467{
468}
469
470void cache_flush_d_inval_i()
471{
472}
473
474/* lprintf */
475void lprintf(const char *fmt, ...)
476{
477 va_list vl;
478
479 va_start(vl, fmt);
480 vprintf(fmt, vl);
481 va_end(vl);
482}
483