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 "../libpicofe/input.h"
20 #include "../libpicofe/plat.h"
21 #include "../libpicofe/menu.h"
22 #include "../libpicofe/linux/in_evdev.h"
23 #include "../libpicofe/linux/fbdev.h"
24 #include "../libpicofe/linux/xenv.h"
26 static int g_layer_x, g_layer_y, g_layer_w, g_layer_h;
27 static struct vout_fbdev *main_fb, *layer_fb;
28 static void *layer_buf;
29 static int bounce_buf[320 * 241 / 4];
30 static unsigned short pal[256];
39 static const struct in_default_bind in_evdev_defbinds[] = {
40 { KEY_UP, IN_BINDTYPE_PLAYER12, NKEYB_UP },
41 { KEY_DOWN, IN_BINDTYPE_PLAYER12, NKEYB_DOWN },
42 { KEY_LEFT, IN_BINDTYPE_PLAYER12, NKEYB_LEFT },
43 { KEY_RIGHT, IN_BINDTYPE_PLAYER12, NKEYB_RIGHT },
44 { KEY_PAGEDOWN, IN_BINDTYPE_PLAYER12, NKEYB_B },
45 { KEY_END, IN_BINDTYPE_PLAYER12, NKEYB_A },
46 { KEY_HOME, IN_BINDTYPE_PLAYER12, NKEYB_B_TURBO },
47 { KEY_PAGEUP, IN_BINDTYPE_PLAYER12, NKEYB_A_TURBO },
48 { KEY_LEFTCTRL, IN_BINDTYPE_PLAYER12, NKEYB_SELECT },
49 { KEY_LEFTALT, IN_BINDTYPE_PLAYER12, NKEYB_START },
50 { KEY_1, IN_BINDTYPE_EMU, EACTB_SAVE_STATE },
51 { KEY_2, IN_BINDTYPE_EMU, EACTB_LOAD_STATE },
52 { KEY_3, IN_BINDTYPE_EMU, EACTB_PREV_SLOT },
53 { KEY_4, IN_BINDTYPE_EMU, EACTB_NEXT_SLOT },
54 { KEY_5, IN_BINDTYPE_EMU, EACTB_FDS_INSERT },
55 { KEY_6, IN_BINDTYPE_EMU, EACTB_FDS_SELECT },
56 { KEY_7, IN_BINDTYPE_EMU, EACTB_INSERT_COIN },
57 { KEY_SPACE, IN_BINDTYPE_EMU, EACTB_ENTER_MENU },
61 static int omap_setup_layer_(int fd, int enabled, int x, int y, int w, int h)
63 struct omapfb_plane_info pi = { 0, };
64 struct omapfb_mem_info mi = { 0, };
67 ret = ioctl(fd, OMAPFB_QUERY_PLANE, &pi);
69 perror("QUERY_PLANE");
73 ret = ioctl(fd, OMAPFB_QUERY_MEM, &mi);
79 /* must disable when changing stuff */
82 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
84 perror("SETUP_PLANE");
87 if (mi.size < 640*512*3*3) {
88 mi.size = 640*512*3*3;
89 ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi);
100 pi.enabled = enabled;
102 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
104 perror("SETUP_PLANE");
111 static int omap_enable_layer(int enabled)
113 return omap_setup_layer_(vout_fbdev_get_fd(layer_fb), enabled,
114 g_layer_x, g_layer_y, g_layer_w, g_layer_h);
117 void platform_init(void)
119 const char *main_fb_name, *layer_fb_name;
122 memset(&Settings, 0, sizeof(Settings));
123 Settings.frameskip = 0;
124 Settings.sound_rate = 44100;
125 Settings.turbo_rate_add = (8*2 << 24) / 60 + 1; // 8Hz turbofire
126 Settings.gamma = 100;
127 Settings.sstate_confirm = 1;
128 Settings.scaling = SCALING_4_3;
130 main_fb_name = getenv("FBDEV_MAIN");
131 if (main_fb_name == NULL)
132 main_fb_name = "/dev/fb0";
134 layer_fb_name = getenv("FBDEV_LAYER");
135 if (layer_fb_name == NULL)
136 layer_fb_name = "/dev/fb1";
138 // must set the layer up first to be able to use it
139 fd = open(layer_fb_name, O_RDWR);
141 fprintf(stderr, "%s: ", layer_fb_name);
146 g_layer_x = 80, g_layer_y = 0;
147 g_layer_w = 640, g_layer_h = 480;
149 ret = omap_setup_layer_(fd, 0, g_layer_x, g_layer_y, g_layer_w, g_layer_h);
152 fprintf(stderr, "failed to set up layer, exiting.\n");
156 xenv_init(NULL, "fceu");
159 main_fb = vout_fbdev_init(main_fb_name, &w, &h, 16, 2);
160 if (main_fb == NULL) {
161 fprintf(stderr, "couldn't init fb: %s\n", main_fb_name);
167 g_menuscreen_ptr = vout_fbdev_flip(main_fb);
171 layer_fb = vout_fbdev_init(layer_fb_name, &w, &h, 16, 3);
172 if (layer_fb == NULL) {
173 fprintf(stderr, "couldn't init fb: %s\n", layer_fb_name);
176 layer_buf = vout_fbdev_resize(layer_fb, 256, 240, 16,
178 if (layer_buf == NULL) {
179 fprintf(stderr, "couldn't get layer buf\n");
185 XBuf = (void *)bounce_buf;
190 vout_fbdev_finish(layer_fb);
192 vout_fbdev_finish(main_fb);
197 void platform_late_init(void)
199 in_evdev_init(in_evdev_defbinds);
201 plat_target_setup_input();
205 void CleanSurface(void)
207 memset(bounce_buf, 0, sizeof(bounce_buf));
208 vout_fbdev_clear(layer_fb);
225 // 16: rrrr rggg gg0b bbbb
226 void FCEUD_SetPalette(uint8 index, uint8 r, uint8 g, uint8 b)
228 pal[index] = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3);
231 void FCEUD_GetPalette(uint8 index, uint8 *r, uint8 *g, uint8 *b)
233 unsigned int v = pal[index];
234 *r = (v >> 8) & 0xf8;
235 *g = (v >> 3) & 0xfc;
239 void BlitPrepare(int skip)
248 if (eoptions & EO_CLIPSIDES)
250 int i, *p = bounce_buf + 32/4;
251 for (i = 240; i; i--, p += 320/4)
253 p[0] = p[1] = p[62] = p[63] = 0;
257 p = bounce_buf + 32/4;
259 for (i = srendline; i > 0; i--, p += 320/4)
263 p = bounce_buf + erendline*320/4 + 32/4;
264 for (i = 239-erendline; i > 0; i--, p += 320/4)
268 /* this is called before throttle, so we blit here too */
269 s = (char *)bounce_buf + 32;
270 d = (short *)layer_buf;
272 for (i = 0; i < 239; i++, d += 256, s += 320)
273 do_clut(d, s, pal, 256);
275 layer_buf = vout_fbdev_flip(layer_fb);
278 void BlitScreen(int skip)
285 static struct timeval tv_expect;
286 static int skip_count = 0;
287 static int us_interval, us_interval1024;
288 #define MAX_LAG_FRAMES 3
290 void RefreshThrottleFPS(void)
293 us_interval = PAL ? 20000 : 16667;
294 us_interval1024 = PAL ? 20000*1024 : 17066667;
296 vout_fbdev_wait_vsync(layer_fb);
297 gettimeofday(&tv_expect, 0);
298 tv_expect.tv_usec *= 1024;
301 void SpeedThrottle(void)
306 gettimeofday(&now, 0);
308 // usec*1024 units to prevent drifting
309 tv_expect.tv_usec += us_interval1024;
310 if (tv_expect.tv_usec > 1000000 * 1024) {
311 tv_expect.tv_usec -= 1000000 * 1024;
315 diff = (tv_expect.tv_sec - now.tv_sec) * 1000000 +
316 (tv_expect.tv_usec >> 10) - now.tv_usec;
318 if (Settings.frameskip >= 0)
320 if (skip_count >= Settings.frameskip)
329 FSkip = diff < -us_interval;
332 if (diff > MAX_LAG_FRAMES * us_interval
333 || diff < -MAX_LAG_FRAMES * us_interval)
335 //printf("reset %d\n", diff);
336 RefreshThrottleFPS();
339 else if (diff > us_interval)
341 usleep(diff - us_interval);
345 /* called just before emulation */
346 void platform_apply_config(void)
350 switch (Settings.scaling)
356 case SCALING_PROPORTIONAL:
357 // assumes screen width > height
358 mult = (float)g_menuscreen_h / 240;
359 g_layer_w = (int)(256 * mult);
360 g_layer_h = g_menuscreen_h;
363 g_layer_w = g_menuscreen_h * 4 / 3;
364 g_layer_h = g_menuscreen_h;
366 case SCALING_FULLSCREEN:
367 g_layer_w = g_menuscreen_w;
368 g_layer_h = g_menuscreen_h;
371 if (g_layer_w > g_menuscreen_w)
372 g_layer_w = g_menuscreen_w;
373 if (g_layer_h > g_menuscreen_h)
374 g_layer_h = g_menuscreen_h;
375 g_layer_x = g_menuscreen_w / 2 - g_layer_w / 2;
376 g_layer_y = g_menuscreen_h / 2 - g_layer_h / 2;
378 plat_target_set_filter(Settings.hw_filter);
379 plat_target_set_lcdrate(PAL);
381 // adjust since we run at 60Hz, and real NES doesn't
382 FCEUI_Sound(Settings.sound_rate + Settings.sound_rate / 980);
384 omap_enable_layer(1);
387 void platform_set_volume(int val)
391 void plat_video_menu_enter(int is_rom_loaded)
393 unsigned short *d = g_menubg_src_ptr;
394 unsigned char *s, *sr = (void *)bounce_buf;
398 omap_enable_layer(0);
403 for (y = 0; y < g_menuscreen_h; y++)
405 s = sr + v * 320 + 32;
407 for (x = 0; x < g_menuscreen_w; )
409 w = g_menuscreen_w - x;
412 do_clut(d + x, s + u, pal, w);
418 if (v > erendlinev[PAL])
423 void plat_video_menu_begin(void)
427 void plat_video_menu_end(void)
429 g_menuscreen_ptr = vout_fbdev_flip(main_fb);
432 void plat_video_menu_leave(void)
434 memset(g_menuscreen_ptr, 0, g_menuscreen_w * g_menuscreen_h * 2);
435 g_menuscreen_ptr = vout_fbdev_flip(main_fb);
436 // layer enabled in platform_apply_config()
439 char *DriverUsage="";
441 ARGPSTRUCT DriverArgs[]={
445 void DoDriverArgs(void)
449 void GetBaseDirectory(char *BaseDirectory)
451 strcpy(BaseDirectory, "fceultra");
454 void platform_finish(void)
456 omap_enable_layer(0);
457 vout_fbdev_finish(layer_fb);
458 vout_fbdev_finish(main_fb);