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 #define LAYER_MEM_SIZE (320*240*2 * 4)
\r
38 static struct vout_fbdev *main_fb, *layer_fb;
\r
39 // g_layer_* - in use, g_layer_c* - configured custom
\r
40 int g_layer_cx, g_layer_cy, g_layer_cw, g_layer_ch;
\r
41 static int g_layer_x, g_layer_y;
\r
42 static int g_layer_w = 320, g_layer_h = 240;
\r
43 static int g_osd_fps_x, g_osd_y, doing_bg_frame;
\r
45 static unsigned char __attribute__((aligned(4))) fb_copy[320 * 240 * 2];
\r
46 static void *temp_frame;
\r
47 const char *renderer_names[] = { NULL };
\r
48 const char *renderer_names32x[] = { NULL };
\r
50 static const char * const pandora_gpio_keys[KEY_MAX + 1] = {
\r
51 [0 ... KEY_MAX] = NULL,
\r
53 [KEY_LEFT] = "Left",
\r
54 [KEY_RIGHT] = "Right",
\r
55 [KEY_DOWN] = "Down",
\r
57 [KEY_PAGEDOWN] = "X",
\r
60 [KEY_RIGHTSHIFT]= "L",
\r
61 [KEY_RIGHTCTRL] = "R",
\r
62 [KEY_LEFTALT] = "Start",
\r
63 [KEY_LEFTCTRL] = "Select",
\r
64 [KEY_MENU] = "Pandora",
\r
67 static struct in_default_bind in_evdev_defbinds[] =
\r
69 { KEY_UP, IN_BINDTYPE_PLAYER12, GBTN_UP },
\r
70 { KEY_DOWN, IN_BINDTYPE_PLAYER12, GBTN_DOWN },
\r
71 { KEY_LEFT, IN_BINDTYPE_PLAYER12, GBTN_LEFT },
\r
72 { KEY_RIGHT, IN_BINDTYPE_PLAYER12, GBTN_RIGHT },
\r
73 { KEY_A, IN_BINDTYPE_PLAYER12, GBTN_A },
\r
74 { KEY_S, IN_BINDTYPE_PLAYER12, GBTN_B },
\r
75 { KEY_D, IN_BINDTYPE_PLAYER12, GBTN_C },
\r
76 { KEY_ENTER, IN_BINDTYPE_PLAYER12, GBTN_START },
\r
77 { KEY_F, IN_BINDTYPE_EMU, PEVB_FF },
\r
78 { KEY_BACKSPACE,IN_BINDTYPE_EMU, PEVB_FF },
\r
79 { KEY_BACKSLASH,IN_BINDTYPE_EMU, PEVB_MENU },
\r
80 { KEY_SPACE, IN_BINDTYPE_EMU, PEVB_MENU },
\r
81 { KEY_LEFTCTRL, IN_BINDTYPE_EMU, PEVB_MENU },
\r
82 { KEY_HOME, IN_BINDTYPE_PLAYER12, GBTN_A },
\r
83 { KEY_PAGEDOWN, IN_BINDTYPE_PLAYER12, GBTN_B },
\r
84 { KEY_END, IN_BINDTYPE_PLAYER12, GBTN_C },
\r
85 { KEY_LEFTALT, IN_BINDTYPE_PLAYER12, GBTN_START },
\r
86 { KEY_1, IN_BINDTYPE_EMU, PEVB_STATE_SAVE },
\r
87 { KEY_2, IN_BINDTYPE_EMU, PEVB_STATE_LOAD },
\r
88 { KEY_3, IN_BINDTYPE_EMU, PEVB_SSLOT_PREV },
\r
89 { KEY_4, IN_BINDTYPE_EMU, PEVB_SSLOT_NEXT },
\r
90 { KEY_5, IN_BINDTYPE_EMU, PEVB_PICO_PPREV },
\r
91 { KEY_6, IN_BINDTYPE_EMU, PEVB_PICO_PNEXT },
\r
92 { KEY_7, IN_BINDTYPE_EMU, PEVB_PICO_SWINP },
\r
96 static const struct menu_keymap key_pbtn_map[] =
\r
98 { KEY_UP, PBTN_UP },
\r
99 { KEY_DOWN, PBTN_DOWN },
\r
100 { KEY_LEFT, PBTN_LEFT },
\r
101 { KEY_RIGHT, PBTN_RIGHT },
\r
103 { KEY_END, PBTN_MOK },
\r
104 { KEY_PAGEDOWN, PBTN_MBACK },
\r
105 { KEY_HOME, PBTN_MA2 },
\r
106 { KEY_PAGEUP, PBTN_MA3 },
\r
107 { KEY_LEFTCTRL, PBTN_MENU },
\r
108 { KEY_RIGHTSHIFT, PBTN_L },
\r
109 { KEY_RIGHTCTRL, PBTN_R },
\r
110 /* "normal" keyboards */
\r
111 { KEY_ENTER, PBTN_MOK },
\r
112 { KEY_ESC, PBTN_MBACK },
\r
113 { KEY_SEMICOLON, PBTN_MA2 },
\r
114 { KEY_APOSTROPHE, PBTN_MA3 },
\r
115 { KEY_BACKSLASH, PBTN_MENU },
\r
116 { KEY_LEFTBRACE, PBTN_L },
\r
117 { KEY_RIGHTBRACE, PBTN_R },
\r
120 static const struct in_pdata pandora_evdev_pdata = {
\r
121 .defbinds = in_evdev_defbinds,
\r
122 .key_map = key_pbtn_map,
\r
123 .kmap_size = sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]),
\r
126 void pemu_prep_defconfig(void)
\r
128 defaultConfig.EmuOpt |= EOPT_VSYNC|EOPT_16BPP;
\r
129 defaultConfig.s_PicoOpt |= POPT_EN_MCD_GFX;
\r
130 defaultConfig.scaling = SCALE_2x2_3x2;
\r
133 void pemu_validate_config(void)
\r
135 currentConfig.CPUclock = plat_target_cpu_clock_get();
\r
138 static void draw_cd_leds(void)
\r
141 old_reg = Pico_mcd->s68k_regs[0];
\r
145 unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;
\r
146 unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;
\r
147 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*2+ 4) =
\r
148 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*3+ 4) =
\r
149 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*4+ 4) = col_g;
\r
150 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*2+12) =
\r
151 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*3+12) =
\r
152 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*4+12) = col_r;
\r
155 unsigned int *p = (unsigned int *)((short *)g_screen_ptr + g_screen_width*2+4);
\r
156 unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;
\r
157 unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;
\r
158 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += g_screen_width/2 - 12/2;
\r
159 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += g_screen_width/2 - 12/2;
\r
160 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
\r
164 void pemu_finalize_frame(const char *fps, const char *notice)
\r
166 if (notice && notice[0])
\r
167 emu_osd_text16(2, g_osd_y, notice);
\r
168 if (fps && fps[0] && (currentConfig.EmuOpt & EOPT_SHOW_FPS))
\r
169 emu_osd_text16(g_osd_fps_x, g_osd_y, fps);
\r
170 if ((PicoAHW & PAHW_MCD) && (currentConfig.EmuOpt & EOPT_EN_CD_LEDS))
\r
174 void plat_video_flip(void)
\r
176 g_screen_ptr = vout_fbdev_flip(layer_fb);
\r
177 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);
\r
179 // XXX: drain OS event queue here, maybe we'll actually use it someday..
\r
180 xenv_update(NULL, NULL, NULL, NULL);
\r
183 void plat_video_toggle_renderer(int change, int is_menu)
\r
187 void plat_video_menu_enter(int is_rom_loaded)
\r
191 void plat_video_menu_begin(void)
\r
195 void plat_video_menu_end(void)
\r
197 g_menuscreen_ptr = vout_fbdev_flip(main_fb);
\r
200 void plat_video_menu_leave(void)
\r
204 void plat_video_wait_vsync(void)
\r
206 vout_fbdev_wait_vsync(main_fb);
\r
209 void plat_status_msg_clear(void)
\r
211 vout_fbdev_clear_lines(layer_fb, g_osd_y, 8);
\r
214 void plat_status_msg_busy_next(const char *msg)
\r
216 plat_status_msg_clear();
\r
217 pemu_finalize_frame("", msg);
\r
219 emu_status_msg("");
\r
223 void plat_status_msg_busy_first(const char *msg)
\r
225 plat_status_msg_busy_next(msg);
\r
228 void plat_update_volume(int has_changed, int is_up)
\r
232 void pemu_forced_frame(int no_scale, int do_emu)
\r
234 doing_bg_frame = 1;
\r
235 emu_cmn_forced_frame(no_scale, do_emu);
\r
236 doing_bg_frame = 0;
\r
238 // making a copy because enabling the layer clears it's mem
\r
239 memcpy((void *)fb_copy, g_screen_ptr, sizeof(fb_copy));
\r
240 g_menubg_src_ptr = fb_copy;
\r
243 void pemu_sound_start(void)
\r
248 void plat_debug_cat(char *str)
\r
252 static int pnd_setup_layer_(int fd, int enabled, int x, int y, int w, int h)
\r
254 struct omapfb_plane_info pi;
\r
255 struct omapfb_mem_info mi;
\r
260 ret = ioctl(fd, OMAPFB_QUERY_PLANE, &pi);
\r
262 perror("QUERY_PLANE");
\r
266 ret = ioctl(fd, OMAPFB_QUERY_MEM, &mi);
\r
268 perror("QUERY_MEM");
\r
272 /* must disable when changing stuff */
\r
273 is_enabled = pi.enabled;
\r
276 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
\r
278 perror("SETUP_PLANE");
\r
283 if (mi.size < LAYER_MEM_SIZE) {
\r
284 unsigned int size_old = mi.size;
\r
286 mi.size = LAYER_MEM_SIZE;
\r
287 ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi);
\r
289 perror("SETUP_MEM");
\r
290 fprintf(stderr, "(requested %u, had %u)\n",
\r
291 mi.size, size_old);
\r
300 pi.enabled = enabled;
\r
302 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
\r
304 is_enabled = pi.enabled;
\r
307 perror("SETUP_PLANE");
\r
311 plat_target_switch_layer(1, is_enabled);
\r
316 int pnd_setup_layer(int enabled, int x, int y, int w, int h)
\r
318 return pnd_setup_layer_(vout_fbdev_get_fd(layer_fb), enabled, x, y, w, h);
\r
321 void pnd_restore_layer_data(void)
\r
323 short *t = (short *)fb_copy + 320*240 / 2 + 160;
\r
325 // right now this is used by menu, which wants to preview something
\r
326 // so try to get something on the layer.
\r
327 if ((t[0] | t[5] | t[13]) == 0)
\r
328 memset32((void *)fb_copy, 0x07000700, sizeof(fb_copy) / 4);
\r
330 memcpy(g_screen_ptr, (void *)fb_copy, 320*240*2);
\r
334 void emu_video_mode_change(int start_line, int line_count, int is_32cols)
\r
336 int fb_w = 320, fb_h = 240, fb_left = 0, fb_right = 0, fb_top = 0, fb_bottom = 0;
\r
338 if (doing_bg_frame)
\r
343 fb_left = fb_right = 32;
\r
346 switch (currentConfig.scaling) {
\r
351 case SCALE_2x2_3x2:
\r
352 g_layer_w = fb_w * (is_32cols ? 3 : 2);
\r
353 g_layer_h = fb_h * 2;
\r
355 case SCALE_2x2_2x2:
\r
356 g_layer_w = fb_w * 2;
\r
357 g_layer_h = fb_h * 2;
\r
359 case SCALE_FULLSCREEN:
\r
364 g_layer_x = g_layer_cx;
\r
365 g_layer_y = g_layer_cy;
\r
366 g_layer_w = g_layer_cw;
\r
367 g_layer_h = g_layer_ch;
\r
371 if (currentConfig.scaling != SCALE_CUSTOM) {
\r
372 // center the layer
\r
373 g_layer_x = 800 / 2 - g_layer_w / 2;
\r
374 g_layer_y = 480 / 2 - g_layer_h / 2;
\r
377 switch (currentConfig.scaling) {
\r
378 case SCALE_FULLSCREEN:
\r
380 fb_top = start_line;
\r
384 g_osd_fps_x = is_32cols ? 232 : 264;
\r
385 g_osd_y = fb_top + fb_h - 8;
\r
387 pnd_setup_layer(1, g_layer_x, g_layer_y, g_layer_w, g_layer_h);
\r
388 vout_fbdev_clear(layer_fb);
\r
389 vout_fbdev_resize(layer_fb, fb_w, fb_h, 16, fb_left, fb_right, fb_top, fb_bottom, 4);
\r
392 PicoDrawSetOutFormat(PDF_RGB555, 0);
\r
395 void plat_video_loop_prepare(void)
\r
397 // make sure there is no junk left behind the layer
\r
398 memset(g_menuscreen_ptr, 0, g_menuscreen_w * g_menuscreen_h * 2);
\r
399 g_menuscreen_ptr = vout_fbdev_flip(main_fb);
\r
401 // emu_video_mode_change will call pnd_setup_layer()
\r
404 void pemu_loop_prep(void)
\r
406 // dirty buffers better go now than during gameplay
\r
413 void pemu_loop_end(void)
\r
415 /* do one more frame for menu bg */
\r
416 pemu_forced_frame(0, 1);
\r
418 pnd_setup_layer(0, g_layer_x, g_layer_y, g_layer_w, g_layer_h);
\r
421 void plat_wait_till_us(unsigned int us_to)
\r
426 now = plat_get_ticks_us();
\r
428 // XXX: need to check NOHZ
\r
429 diff = (signed int)(us_to - now);
\r
430 if (diff > 10000) {
\r
431 //printf("sleep %d\n", us_to - now);
\r
432 usleep(diff * 15 / 16);
\r
433 now = plat_get_ticks_us();
\r
434 //printf(" wake %d\n", (signed)(us_to - now));
\r
437 while ((signed int)(us_to - now) > 512) {
\r
438 spend_cycles(1024);
\r
439 now = plat_get_ticks_us();
\r
444 void plat_early_init(void)
\r
448 void plat_init(void)
\r
450 const char *main_fb_name, *layer_fb_name;
\r
453 main_fb_name = getenv("FBDEV_MAIN");
\r
454 if (main_fb_name == NULL)
\r
455 main_fb_name = "/dev/fb0";
\r
457 layer_fb_name = getenv("FBDEV_LAYER");
\r
458 if (layer_fb_name == NULL)
\r
459 layer_fb_name = "/dev/fb1";
\r
461 // must set the layer up first to be able to use it
\r
462 fd = open(layer_fb_name, O_RDWR);
\r
464 fprintf(stderr, "%s: ", layer_fb_name);
\r
469 ret = pnd_setup_layer_(fd, 0, g_layer_x, g_layer_y, g_layer_w, g_layer_h);
\r
472 fprintf(stderr, "failed to set up layer, exiting.\n");
\r
476 xenv_init(NULL, "PicoDrive " VERSION);
\r
479 main_fb = vout_fbdev_init(main_fb_name, &w, &h, 16, 2);
\r
480 if (main_fb == NULL) {
\r
481 fprintf(stderr, "couldn't init fb: %s\n", main_fb_name);
\r
485 g_menuscreen_w = w;
\r
486 g_menuscreen_h = h;
\r
487 g_menuscreen_ptr = vout_fbdev_flip(main_fb);
\r
490 layer_fb = vout_fbdev_init(layer_fb_name, &w, &h, 16, 4);
\r
491 if (layer_fb == NULL) {
\r
492 fprintf(stderr, "couldn't init fb: %s\n", layer_fb_name);
\r
496 if (w != g_screen_width || h != g_screen_height) {
\r
497 fprintf(stderr, "%dx%d not supported on %s\n", w, h, layer_fb_name);
\r
500 g_screen_ptr = vout_fbdev_flip(layer_fb);
\r
502 temp_frame = calloc(g_menuscreen_w * g_menuscreen_h * 2, 1);
\r
503 if (temp_frame == NULL) {
\r
504 fprintf(stderr, "OOM\n");
\r
507 g_menubg_ptr = temp_frame;
\r
508 g_menubg_src_ptr = temp_frame;
\r
512 // default ROM path
\r
513 strcpy(rom_fname_loaded, "/media");
\r
515 in_evdev_init(&pandora_evdev_pdata);
\r
517 plat_target_setup_input();
\r
519 sndout_oss_frag_frames = 2;
\r
524 vout_fbdev_finish(layer_fb);
\r
526 vout_fbdev_finish(main_fb);
\r
530 void plat_finish(void)
\r
532 vout_fbdev_finish(main_fb);
\r
535 printf("all done\n");
\r