9 #include <linux/input.h>
10 #include <linux/omapfb.h>
12 #include "../common/platform.h"
13 #include "../common/input.h"
14 #include "../common/settings.h"
15 #include "../common/main.h"
16 #include "../common/args.h"
17 #include "../../video.h"
18 #include "../arm/asmutils.h"
19 #include "../arm/neon_scale2x.h"
20 #include "../arm/neon_eagle2x.h"
21 #include "../libpicofe/input.h"
22 #include "../libpicofe/plat.h"
23 #include "../libpicofe/menu.h"
24 #include "../libpicofe/linux/in_evdev.h"
25 #include "../libpicofe/linux/fbdev.h"
26 #include "../libpicofe/linux/xenv.h"
28 static int g_layer_x, g_layer_y, g_layer_w, g_layer_h;
29 static struct vout_fbdev *main_fb, *layer_fb;
30 static void *layer_buf;
31 static int bounce_buf[320 * 241 / 4];
32 static unsigned short pal[256];
33 static unsigned int pal2[256]; // for neon_*2x
48 static const struct in_default_bind in_evdev_defbinds[] = {
49 { KEY_UP, IN_BINDTYPE_PLAYER12, NKEYB_UP },
50 { KEY_DOWN, IN_BINDTYPE_PLAYER12, NKEYB_DOWN },
51 { KEY_LEFT, IN_BINDTYPE_PLAYER12, NKEYB_LEFT },
52 { KEY_RIGHT, IN_BINDTYPE_PLAYER12, NKEYB_RIGHT },
53 { KEY_PAGEDOWN, IN_BINDTYPE_PLAYER12, NKEYB_B },
54 { KEY_END, IN_BINDTYPE_PLAYER12, NKEYB_A },
55 { KEY_HOME, IN_BINDTYPE_PLAYER12, NKEYB_B_TURBO },
56 { KEY_PAGEUP, IN_BINDTYPE_PLAYER12, NKEYB_A_TURBO },
57 { KEY_LEFTCTRL, IN_BINDTYPE_PLAYER12, NKEYB_SELECT },
58 { KEY_LEFTALT, IN_BINDTYPE_PLAYER12, NKEYB_START },
59 { KEY_1, IN_BINDTYPE_EMU, EACTB_SAVE_STATE },
60 { KEY_2, IN_BINDTYPE_EMU, EACTB_LOAD_STATE },
61 { KEY_3, IN_BINDTYPE_EMU, EACTB_PREV_SLOT },
62 { KEY_4, IN_BINDTYPE_EMU, EACTB_NEXT_SLOT },
63 { KEY_5, IN_BINDTYPE_EMU, EACTB_FDS_INSERT },
64 { KEY_6, IN_BINDTYPE_EMU, EACTB_FDS_SELECT },
65 { KEY_7, IN_BINDTYPE_EMU, EACTB_INSERT_COIN },
66 { KEY_SPACE, IN_BINDTYPE_EMU, EACTB_ENTER_MENU },
70 static int omap_setup_layer_(int fd, int enabled, int x, int y, int w, int h)
72 struct omapfb_plane_info pi = { 0, };
73 struct omapfb_mem_info mi = { 0, };
76 ret = ioctl(fd, OMAPFB_QUERY_PLANE, &pi);
78 perror("QUERY_PLANE");
82 ret = ioctl(fd, OMAPFB_QUERY_MEM, &mi);
88 /* must disable when changing stuff */
91 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
93 perror("SETUP_PLANE");
96 if (mi.size < 256*2*240*2*3) {
97 mi.size = 256*2*240*2*3;
98 ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi);
109 pi.enabled = enabled;
111 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
113 perror("SETUP_PLANE");
120 static int omap_enable_layer(int enabled)
122 return omap_setup_layer_(vout_fbdev_get_fd(layer_fb), enabled,
123 g_layer_x, g_layer_y, g_layer_w, g_layer_h);
126 void platform_init(void)
128 const char *main_fb_name, *layer_fb_name;
131 memset(&Settings, 0, sizeof(Settings));
132 Settings.frameskip = 0;
133 Settings.sound_rate = 44100;
134 Settings.turbo_rate_add = (8*2 << 24) / 60 + 1; // 8Hz turbofire
135 Settings.gamma = 100;
136 Settings.sstate_confirm = 1;
137 Settings.scaling = SCALING_4_3;
139 main_fb_name = getenv("FBDEV_MAIN");
140 if (main_fb_name == NULL)
141 main_fb_name = "/dev/fb0";
143 layer_fb_name = getenv("FBDEV_LAYER");
144 if (layer_fb_name == NULL)
145 layer_fb_name = "/dev/fb1";
147 // must set the layer up first to be able to use it
148 fd = open(layer_fb_name, O_RDWR);
150 fprintf(stderr, "%s: ", layer_fb_name);
155 g_layer_x = 80, g_layer_y = 0;
156 g_layer_w = 640, g_layer_h = 480;
158 ret = omap_setup_layer_(fd, 0, g_layer_x, g_layer_y, g_layer_w, g_layer_h);
161 fprintf(stderr, "failed to set up layer, exiting.\n");
165 xenv_init(NULL, "fceu");
168 main_fb = vout_fbdev_init(main_fb_name, &w, &h, 16, 2);
169 if (main_fb == NULL) {
170 fprintf(stderr, "couldn't init fb: %s\n", main_fb_name);
176 g_menuscreen_ptr = vout_fbdev_flip(main_fb);
180 layer_fb = vout_fbdev_init(layer_fb_name, &w, &h, 16, 3);
181 if (layer_fb == NULL) {
182 fprintf(stderr, "couldn't init fb: %s\n", layer_fb_name);
185 layer_buf = vout_fbdev_resize(layer_fb, 256, 240, 16,
187 if (layer_buf == NULL) {
188 fprintf(stderr, "couldn't get layer buf\n");
194 XBuf = (void *)bounce_buf;
199 vout_fbdev_finish(layer_fb);
201 vout_fbdev_finish(main_fb);
206 void platform_late_init(void)
208 in_evdev_init(in_evdev_defbinds);
210 plat_target_setup_input();
214 void CleanSurface(void)
216 memset(bounce_buf, 0, sizeof(bounce_buf));
217 vout_fbdev_clear(layer_fb);
234 // 16: rrrr rggg gg0b bbbb
235 void FCEUD_SetPalette(uint8 index, uint8 r, uint8 g, uint8 b)
237 unsigned int v = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3);
242 void FCEUD_GetPalette(uint8 index, uint8 *r, uint8 *g, uint8 *b)
244 unsigned int v = pal[index];
245 *r = (v >> 8) & 0xf8;
246 *g = (v >> 3) & 0xfc;
250 void BlitPrepare(int skip)
259 if (eoptions & EO_CLIPSIDES)
261 int i, *p = bounce_buf + 32/4;
262 for (i = 240; i; i--, p += 320/4)
264 p[0] = p[1] = p[62] = p[63] = 0;
268 p = bounce_buf + 32/4;
270 for (i = srendline; i > 0; i--, p += 320/4)
274 p = bounce_buf + erendline*320/4 + 32/4;
275 for (i = 239-erendline; i > 0; i--, p += 320/4)
279 /* this is called before throttle, so we blit here too */
280 s = (unsigned char *)bounce_buf + 32;
281 d = (unsigned short *)layer_buf;
283 switch (Settings.sw_filter)
285 case SWFILTER_SCALE2X:
286 neon_scale2x_8_16(s, d, pal2, 256, 320, 256*2*2, 240);
288 case SWFILTER_EAGLE2X:
289 neon_eagle2x_8_16(s, d, pal2, 256, 320, 256*2*2, 240);
293 for (i = 0; i < 239; i++, d += 256, s += 320)
294 do_clut(d, s, pal, 256);
298 layer_buf = vout_fbdev_flip(layer_fb);
301 void BlitScreen(int skip)
308 static struct timeval tv_expect;
309 static int skip_count = 0;
310 static int us_interval, us_interval1024;
311 #define MAX_LAG_FRAMES 3
313 void RefreshThrottleFPS(void)
316 us_interval = PAL ? 20000 : 16667;
317 us_interval1024 = PAL ? 20000*1024 : 17066667;
319 vout_fbdev_wait_vsync(layer_fb);
320 gettimeofday(&tv_expect, 0);
321 tv_expect.tv_usec *= 1024;
324 void SpeedThrottle(void)
329 gettimeofday(&now, 0);
331 // usec*1024 units to prevent drifting
332 tv_expect.tv_usec += us_interval1024;
333 if (tv_expect.tv_usec > 1000000 * 1024) {
334 tv_expect.tv_usec -= 1000000 * 1024;
338 diff = (tv_expect.tv_sec - now.tv_sec) * 1000000 +
339 (tv_expect.tv_usec >> 10) - now.tv_usec;
341 if (Settings.frameskip >= 0)
343 if (skip_count >= Settings.frameskip)
352 FSkip = diff < -us_interval;
355 if (diff > MAX_LAG_FRAMES * us_interval
356 || diff < -MAX_LAG_FRAMES * us_interval)
358 //printf("reset %d\n", diff);
359 RefreshThrottleFPS();
362 else if (diff > us_interval)
364 usleep(diff - us_interval);
368 /* called just before emulation */
369 void platform_apply_config(void)
371 static int old_layer_w, old_layer_h;
372 int fb_w = 256, fb_h = 240;
375 if (Settings.sw_filter == SWFILTER_SCALE2X
376 || Settings.sw_filter == SWFILTER_EAGLE2X)
382 if (fb_w != old_layer_w || fb_h != old_layer_h)
384 layer_buf = vout_fbdev_resize(layer_fb, fb_w, fb_h, 16,
386 if (layer_buf == NULL) {
387 fprintf(stderr, "fatal: mode change %dx%x -> %dx%d failed\n",
388 old_layer_w, old_layer_h, fb_w, fb_h);
396 switch (Settings.scaling)
402 case SCALING_PROPORTIONAL:
403 // assumes screen width > height
404 mult = (float)g_menuscreen_h / fb_h;
405 g_layer_w = (int)(fb_w * mult);
406 g_layer_h = g_menuscreen_h;
408 case SCALING_FULLSCREEN:
409 g_layer_w = g_menuscreen_w;
410 g_layer_h = g_menuscreen_h;
414 g_layer_w = g_menuscreen_h * 4 / 3;
415 g_layer_h = g_menuscreen_h;
418 if (g_layer_w > g_menuscreen_w)
419 g_layer_w = g_menuscreen_w;
420 if (g_layer_h > g_menuscreen_h)
421 g_layer_h = g_menuscreen_h;
422 g_layer_x = g_menuscreen_w / 2 - g_layer_w / 2;
423 g_layer_y = g_menuscreen_h / 2 - g_layer_h / 2;
425 plat_target_set_filter(Settings.hw_filter);
426 plat_target_set_lcdrate(PAL);
428 // adjust since we run at 60Hz, and real NES doesn't
429 FCEUI_Sound(Settings.sound_rate + Settings.sound_rate / 980);
431 omap_enable_layer(1);
434 void platform_set_volume(int val)
438 void plat_video_menu_enter(int is_rom_loaded)
440 unsigned short *d = g_menubg_src_ptr;
441 unsigned char *s, *sr = (void *)bounce_buf;
445 omap_enable_layer(0);
450 for (y = 0; y < g_menuscreen_h; y++)
452 s = sr + v * 320 + 32;
454 for (x = 0; x < g_menuscreen_w; )
456 w = g_menuscreen_w - x;
459 do_clut(d + x, s + u, pal, w);
465 if (v > erendlinev[PAL])
470 void plat_video_menu_begin(void)
474 void plat_video_menu_end(void)
476 g_menuscreen_ptr = vout_fbdev_flip(main_fb);
479 void plat_video_menu_leave(void)
481 memset(g_menuscreen_ptr, 0, g_menuscreen_w * g_menuscreen_h * 2);
482 g_menuscreen_ptr = vout_fbdev_flip(main_fb);
483 // layer enabled in platform_apply_config()
486 char *DriverUsage="";
488 ARGPSTRUCT DriverArgs[]={
492 void DoDriverArgs(void)
496 void GetBaseDirectory(char *BaseDirectory)
498 strcpy(BaseDirectory, "fceultra");
501 void platform_get_def_rompath(char *buf, int size)
503 strcpy(buf, "/media");
506 void platform_finish(void)
508 omap_enable_layer(0);
509 vout_fbdev_finish(layer_fb);
510 vout_fbdev_finish(main_fb);