3 * (C) notaz, 2010,2011
\r
5 * This work is licensed under the terms of MAME license.
\r
6 * See COPYING file in the top-level directory.
\r
11 #include <sys/types.h>
\r
12 #include <sys/stat.h>
\r
14 #include <sys/ioctl.h>
\r
16 #include <linux/fb.h>
\r
17 #include <linux/omapfb.h>
\r
18 #include <linux/input.h>
\r
20 #include "../common/emu.h"
\r
21 #include "../common/arm_utils.h"
\r
22 #include "../common/input_pico.h"
\r
23 #include "../common/version.h"
\r
24 #include "../libpicofe/input.h"
\r
25 #include "../libpicofe/menu.h"
\r
26 #include "../libpicofe/plat.h"
\r
27 #include "../libpicofe/linux/in_evdev.h"
\r
28 #include "../libpicofe/linux/sndout_oss.h"
\r
29 #include "../libpicofe/linux/fbdev.h"
\r
30 #include "../libpicofe/linux/xenv.h"
\r
32 #include "asm_utils.h"
\r
34 #include <pico/pico_int.h>
\r
36 static struct vout_fbdev *main_fb, *layer_fb;
\r
37 // g_layer_* - in use, g_layer_c* - configured custom
\r
38 int g_layer_cx, g_layer_cy, g_layer_cw, g_layer_ch;
\r
39 static int g_layer_x, g_layer_y;
\r
40 static int g_layer_w = 320, g_layer_h = 240;
\r
41 static int g_osd_fps_x, g_osd_y, doing_bg_frame;
\r
43 static const char pnd_script_base[] = "sudo -n /usr/pandora/scripts";
\r
44 static unsigned char __attribute__((aligned(4))) fb_copy[320 * 240 * 2];
\r
45 static void *temp_frame;
\r
46 const char *renderer_names[] = { NULL };
\r
47 const char *renderer_names32x[] = { NULL };
\r
49 static const char * const pandora_gpio_keys[KEY_MAX + 1] = {
\r
50 [0 ... KEY_MAX] = NULL,
\r
52 [KEY_LEFT] = "Left",
\r
53 [KEY_RIGHT] = "Right",
\r
54 [KEY_DOWN] = "Down",
\r
56 [KEY_PAGEDOWN] = "X",
\r
59 [KEY_RIGHTSHIFT]= "L",
\r
60 [KEY_RIGHTCTRL] = "R",
\r
61 [KEY_LEFTALT] = "Start",
\r
62 [KEY_LEFTCTRL] = "Select",
\r
63 [KEY_MENU] = "Pandora",
\r
66 static struct in_default_bind in_evdev_defbinds[] =
\r
68 { KEY_UP, IN_BINDTYPE_PLAYER12, GBTN_UP },
\r
69 { KEY_DOWN, IN_BINDTYPE_PLAYER12, GBTN_DOWN },
\r
70 { KEY_LEFT, IN_BINDTYPE_PLAYER12, GBTN_LEFT },
\r
71 { KEY_RIGHT, IN_BINDTYPE_PLAYER12, GBTN_RIGHT },
\r
72 { KEY_A, IN_BINDTYPE_PLAYER12, GBTN_A },
\r
73 { KEY_S, IN_BINDTYPE_PLAYER12, GBTN_B },
\r
74 { KEY_D, IN_BINDTYPE_PLAYER12, GBTN_C },
\r
75 { KEY_ENTER, IN_BINDTYPE_PLAYER12, GBTN_START },
\r
76 { KEY_BACKSLASH, IN_BINDTYPE_EMU, PEVB_MENU },
\r
77 { KEY_SPACE, IN_BINDTYPE_EMU, PEVB_MENU },
\r
79 { KEY_HOME, IN_BINDTYPE_PLAYER12, GBTN_A },
\r
80 { KEY_PAGEDOWN, IN_BINDTYPE_PLAYER12, GBTN_B },
\r
81 { KEY_END, IN_BINDTYPE_PLAYER12, GBTN_C },
\r
82 { KEY_LEFTALT, IN_BINDTYPE_PLAYER12, GBTN_START },
\r
83 { KEY_RIGHTSHIFT,IN_BINDTYPE_EMU, PEVB_STATE_SAVE },
\r
84 { KEY_RIGHTCTRL, IN_BINDTYPE_EMU, PEVB_STATE_LOAD },
\r
85 { KEY_LEFTCTRL, IN_BINDTYPE_EMU, PEVB_MENU },
\r
89 void pemu_prep_defconfig(void)
\r
91 defaultConfig.EmuOpt |= EOPT_VSYNC|EOPT_16BPP;
\r
92 defaultConfig.s_PicoOpt |= POPT_EN_MCD_GFX|POPT_EN_MCD_PSYNC;
\r
93 defaultConfig.scaling = SCALE_2x2_3x2;
\r
96 void pemu_validate_config(void)
\r
98 currentConfig.CPUclock = plat_target_cpu_clock_get();
\r
101 static void osd_text(int x, int y, const char *text)
\r
103 int len = strlen(text)*8;
\r
107 for (h = 0; h < 8; h++) {
\r
109 p = (unsigned short *)g_screen_ptr + x + g_screen_width*(y + h);
\r
110 for (i = len; i; i--, p++)
\r
111 *p = (*p>>2) & 0x39e7;
\r
113 emu_text_out16(x, y, text);
\r
116 static void draw_cd_leds(void)
\r
119 old_reg = Pico_mcd->s68k_regs[0];
\r
123 unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;
\r
124 unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;
\r
125 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*2+ 4) =
\r
126 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*3+ 4) =
\r
127 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*4+ 4) = col_g;
\r
128 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*2+12) =
\r
129 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*3+12) =
\r
130 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*4+12) = col_r;
\r
133 unsigned int *p = (unsigned int *)((short *)g_screen_ptr + g_screen_width*2+4);
\r
134 unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;
\r
135 unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;
\r
136 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += g_screen_width/2 - 12/2;
\r
137 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += g_screen_width/2 - 12/2;
\r
138 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
\r
142 void pemu_finalize_frame(const char *fps, const char *notice)
\r
144 if (notice && notice[0])
\r
145 osd_text(2, g_osd_y, notice);
\r
146 if (fps && fps[0] && (currentConfig.EmuOpt & EOPT_SHOW_FPS))
\r
147 osd_text(g_osd_fps_x, g_osd_y, fps);
\r
148 if ((PicoAHW & PAHW_MCD) && (currentConfig.EmuOpt & EOPT_EN_CD_LEDS))
\r
152 void plat_video_flip(void)
\r
154 g_screen_ptr = vout_fbdev_flip(layer_fb);
\r
155 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);
\r
157 // XXX: drain OS event queue here, maybe we'll actually use it someday..
\r
158 xenv_update(NULL, NULL, NULL, NULL);
\r
161 void plat_video_toggle_renderer(int change, int is_menu)
\r
165 void plat_video_menu_enter(int is_rom_loaded)
\r
169 void plat_video_menu_begin(void)
\r
173 void plat_video_menu_end(void)
\r
175 g_menuscreen_ptr = vout_fbdev_flip(main_fb);
\r
178 void plat_video_menu_leave(void)
\r
182 void plat_video_wait_vsync(void)
\r
184 vout_fbdev_wait_vsync(main_fb);
\r
187 void plat_status_msg_clear(void)
\r
189 vout_fbdev_clear_lines(layer_fb, g_osd_y, 8);
\r
192 void plat_status_msg_busy_next(const char *msg)
\r
194 plat_status_msg_clear();
\r
195 pemu_finalize_frame("", msg);
\r
197 emu_status_msg("");
\r
201 void plat_status_msg_busy_first(const char *msg)
\r
203 plat_status_msg_busy_next(msg);
\r
206 void plat_update_volume(int has_changed, int is_up)
\r
210 void pemu_forced_frame(int no_scale, int do_emu)
\r
212 doing_bg_frame = 1;
\r
213 emu_cmn_forced_frame(no_scale, do_emu);
\r
214 doing_bg_frame = 0;
\r
216 // making a copy because enabling the layer clears it's mem
\r
217 memcpy((void *)fb_copy, g_screen_ptr, sizeof(fb_copy));
\r
218 g_menubg_src_ptr = fb_copy;
\r
221 void pemu_sound_start(void)
\r
226 void plat_debug_cat(char *str)
\r
230 static int pnd_setup_layer_(int fd, int enabled, int x, int y, int w, int h)
\r
232 struct omapfb_plane_info pi;
\r
233 struct omapfb_mem_info mi;
\r
236 ret = ioctl(fd, OMAPFB_QUERY_PLANE, &pi);
\r
238 perror("QUERY_PLANE");
\r
242 ret = ioctl(fd, OMAPFB_QUERY_MEM, &mi);
\r
244 perror("QUERY_MEM");
\r
248 /* must disable when changing stuff */
\r
251 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
\r
253 perror("SETUP_PLANE");
\r
256 mi.size = 320*240*2*4;
\r
257 ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi);
\r
259 perror("SETUP_MEM");
\r
267 pi.enabled = enabled;
\r
269 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
\r
271 perror("SETUP_PLANE");
\r
278 int pnd_setup_layer(int enabled, int x, int y, int w, int h)
\r
280 return pnd_setup_layer_(vout_fbdev_get_fd(layer_fb), enabled, x, y, w, h);
\r
283 void pnd_restore_layer_data(void)
\r
285 short *t = (short *)fb_copy + 320*240 / 2 + 160;
\r
287 // right now this is used by menu, which wants to preview something
\r
288 // so try to get something on the layer.
\r
289 if ((t[0] | t[5] | t[13]) == 0)
\r
290 memset32((void *)fb_copy, 0x07000700, sizeof(fb_copy) / 4);
\r
292 memcpy(g_screen_ptr, (void *)fb_copy, 320*240*2);
\r
296 void emu_video_mode_change(int start_line, int line_count, int is_32cols)
\r
298 int fb_w = 320, fb_h = 240, fb_left = 0, fb_right = 0, fb_top = 0, fb_bottom = 0;
\r
300 if (doing_bg_frame)
\r
305 fb_left = fb_right = 32;
\r
308 switch (currentConfig.scaling) {
\r
313 case SCALE_2x2_3x2:
\r
314 g_layer_w = fb_w * (is_32cols ? 3 : 2);
\r
315 g_layer_h = fb_h * 2;
\r
317 case SCALE_2x2_2x2:
\r
318 g_layer_w = fb_w * 2;
\r
319 g_layer_h = fb_h * 2;
\r
321 case SCALE_FULLSCREEN:
\r
326 g_layer_x = g_layer_cx;
\r
327 g_layer_y = g_layer_cy;
\r
328 g_layer_w = g_layer_cw;
\r
329 g_layer_h = g_layer_ch;
\r
333 if (currentConfig.scaling != SCALE_CUSTOM) {
\r
334 // center the layer
\r
335 g_layer_x = 800 / 2 - g_layer_w / 2;
\r
336 g_layer_y = 480 / 2 - g_layer_h / 2;
\r
339 switch (currentConfig.scaling) {
\r
340 case SCALE_FULLSCREEN:
\r
342 fb_top = start_line;
\r
346 g_osd_fps_x = is_32cols ? 232 : 264;
\r
347 g_osd_y = fb_top + fb_h - 8;
\r
349 pnd_setup_layer(1, g_layer_x, g_layer_y, g_layer_w, g_layer_h);
\r
350 vout_fbdev_clear(layer_fb);
\r
351 vout_fbdev_resize(layer_fb, fb_w, fb_h, 16, fb_left, fb_right, fb_top, fb_bottom, 3);
\r
354 PicoDrawSetOutFormat(PDF_RGB555, 0);
\r
357 void plat_video_loop_prepare(void)
\r
359 // make sure there is no junk left behind the layer
\r
360 memset(g_menuscreen_ptr, 0, g_menuscreen_w * g_menuscreen_h * 2);
\r
361 g_menuscreen_ptr = vout_fbdev_flip(main_fb);
\r
363 // emu_video_mode_change will call pnd_setup_layer()
\r
366 void pemu_loop_prep(void)
\r
368 // dirty buffers better go now than during gameplay
\r
373 void pemu_loop_end(void)
\r
375 /* do one more frame for menu bg */
\r
376 pemu_forced_frame(0, 1);
\r
378 pnd_setup_layer(0, g_layer_x, g_layer_y, g_layer_w, g_layer_h);
\r
381 void plat_wait_till_us(unsigned int us_to)
\r
386 now = plat_get_ticks_us();
\r
388 // XXX: need to check NOHZ
\r
389 diff = (signed int)(us_to - now);
\r
390 if (diff > 10000) {
\r
391 //printf("sleep %d\n", us_to - now);
\r
392 usleep(diff * 15 / 16);
\r
393 now = plat_get_ticks_us();
\r
394 //printf(" wake %d\n", (signed)(us_to - now));
\r
397 while ((signed int)(us_to - now) > 512) {
\r
398 spend_cycles(1024);
\r
399 now = plat_get_ticks_us();
\r
404 void plat_early_init(void)
\r
408 void plat_init(void)
\r
410 const char *main_fb_name, *layer_fb_name;
\r
413 main_fb_name = getenv("FBDEV_MAIN");
\r
414 if (main_fb_name == NULL)
\r
415 main_fb_name = "/dev/fb0";
\r
417 layer_fb_name = getenv("FBDEV_LAYER");
\r
418 if (layer_fb_name == NULL)
\r
419 layer_fb_name = "/dev/fb1";
\r
421 // must set the layer up first to be able to use it
\r
422 fd = open(layer_fb_name, O_RDWR);
\r
424 fprintf(stderr, "%s: ", layer_fb_name);
\r
429 ret = pnd_setup_layer_(fd, 0, g_layer_x, g_layer_y, g_layer_w, g_layer_h);
\r
432 fprintf(stderr, "failed to set up layer, exiting.\n");
\r
436 xenv_init(NULL, "PicoDrive " VERSION);
\r
439 main_fb = vout_fbdev_init(main_fb_name, &w, &h, 16, 2);
\r
440 if (main_fb == NULL) {
\r
441 fprintf(stderr, "couldn't init fb: %s\n", main_fb_name);
\r
445 g_menuscreen_w = w;
\r
446 g_menuscreen_h = h;
\r
447 g_menuscreen_ptr = vout_fbdev_flip(main_fb);
\r
450 layer_fb = vout_fbdev_init(layer_fb_name, &w, &h, 16, 3);
\r
451 if (layer_fb == NULL) {
\r
452 fprintf(stderr, "couldn't init fb: %s\n", layer_fb_name);
\r
456 if (w != g_screen_width || h != g_screen_height) {
\r
457 fprintf(stderr, "%dx%d not supported on %s\n", w, h, layer_fb_name);
\r
460 g_screen_ptr = vout_fbdev_flip(layer_fb);
\r
462 temp_frame = calloc(g_menuscreen_w * g_menuscreen_h * 2, 1);
\r
463 if (temp_frame == NULL) {
\r
464 fprintf(stderr, "OOM\n");
\r
467 g_menubg_ptr = temp_frame;
\r
468 g_menubg_src_ptr = temp_frame;
\r
472 in_evdev_init(in_evdev_defbinds);
\r
474 plat_target_setup_input();
\r
476 sndout_oss_frag_frames = 2;
\r
481 vout_fbdev_finish(layer_fb);
\r
483 vout_fbdev_finish(main_fb);
\r
487 void plat_finish(void)
\r
489 vout_fbdev_finish(main_fb);
\r
492 printf("all done\n");
\r