(Win32) Some Win32 compatibility patches (courtesy of mudlord)
[picodrive.git] / platform / pandora / plat.c
1 /*\r
2  * PicoDrive\r
3  * (C) notaz, 2010,2011\r
4  *\r
5  * This work is licensed under the terms of MAME license.\r
6  * See COPYING file in the top-level directory.\r
7  */\r
8 \r
9 #include <stdio.h>\r
10 #include <unistd.h>\r
11 #include <sys/types.h>\r
12 #include <sys/stat.h>\r
13 #include <fcntl.h>\r
14 #include <sys/ioctl.h>\r
15 #include <unistd.h>\r
16 #include <linux/fb.h>\r
17 #include <linux/omapfb.h>\r
18 #include <linux/input.h>\r
19 \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
31 #include "plat.h"\r
32 #include "asm_utils.h"\r
33 \r
34 #include <pico/pico_int.h>\r
35 \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
42 \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
48 \r
49 static const char * const pandora_gpio_keys[KEY_MAX + 1] = {\r
50         [0 ... KEY_MAX] = NULL,\r
51         [KEY_UP]        = "Up",\r
52         [KEY_LEFT]      = "Left",\r
53         [KEY_RIGHT]     = "Right",\r
54         [KEY_DOWN]      = "Down",\r
55         [KEY_HOME]      = "A",\r
56         [KEY_PAGEDOWN]  = "X",\r
57         [KEY_END]       = "B",\r
58         [KEY_PAGEUP]    = "Y",\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
64 };\r
65 \r
66 static struct in_default_bind in_evdev_defbinds[] =\r
67 {\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
78         /* Pandora */\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
86         { 0, 0, 0 }\r
87 };\r
88 \r
89 void pemu_prep_defconfig(void)\r
90 {\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
94 }\r
95 \r
96 void pemu_validate_config(void)\r
97 {\r
98         currentConfig.CPUclock = plat_target_cpu_clock_get();\r
99 }\r
100 \r
101 static void osd_text(int x, int y, const char *text)\r
102 {\r
103         int len = strlen(text)*8;\r
104         int i, h;\r
105 \r
106         len++;\r
107         for (h = 0; h < 8; h++) {\r
108                 unsigned short *p;\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
112         }\r
113         emu_text_out16(x, y, text);\r
114 }\r
115 \r
116 static void draw_cd_leds(void)\r
117 {\r
118         int old_reg;\r
119         old_reg = Pico_mcd->s68k_regs[0];\r
120 \r
121         if (0) {\r
122                 // 8-bit modes\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
131         } else {\r
132                 // 16-bit modes\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
139         }\r
140 }\r
141 \r
142 void pemu_finalize_frame(const char *fps, const char *notice)\r
143 {\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
149                 draw_cd_leds();\r
150 }\r
151 \r
152 void plat_video_flip(void)\r
153 {\r
154         g_screen_ptr = vout_fbdev_flip(layer_fb);\r
155         PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
156 \r
157         // XXX: drain OS event queue here, maybe we'll actually use it someday..\r
158         xenv_update(NULL, NULL, NULL, NULL);\r
159 }\r
160 \r
161 void plat_video_toggle_renderer(int change, int is_menu)\r
162 {\r
163 }\r
164 \r
165 void plat_video_menu_enter(int is_rom_loaded)\r
166 {\r
167 }\r
168 \r
169 void plat_video_menu_begin(void)\r
170 {\r
171 }\r
172 \r
173 void plat_video_menu_end(void)\r
174 {\r
175         g_menuscreen_ptr = vout_fbdev_flip(main_fb);\r
176 }\r
177 \r
178 void plat_video_menu_leave(void)\r
179 {\r
180 }\r
181 \r
182 void plat_video_wait_vsync(void)\r
183 {\r
184         vout_fbdev_wait_vsync(main_fb);\r
185 }\r
186 \r
187 void plat_status_msg_clear(void)\r
188 {\r
189         vout_fbdev_clear_lines(layer_fb, g_osd_y, 8);\r
190 }\r
191 \r
192 void plat_status_msg_busy_next(const char *msg)\r
193 {\r
194         plat_status_msg_clear();\r
195         pemu_finalize_frame("", msg);\r
196         plat_video_flip();\r
197         emu_status_msg("");\r
198         reset_timing = 1;\r
199 }\r
200 \r
201 void plat_status_msg_busy_first(const char *msg)\r
202 {\r
203         plat_status_msg_busy_next(msg);\r
204 }\r
205 \r
206 void plat_update_volume(int has_changed, int is_up)\r
207 {\r
208 }\r
209 \r
210 void pemu_forced_frame(int no_scale, int do_emu)\r
211 {\r
212         doing_bg_frame = 1;\r
213         emu_cmn_forced_frame(no_scale, do_emu);\r
214         doing_bg_frame = 0;\r
215 \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
219 }\r
220 \r
221 void pemu_sound_start(void)\r
222 {\r
223         emu_sound_start();\r
224 }\r
225 \r
226 void plat_debug_cat(char *str)\r
227 {\r
228 }\r
229 \r
230 static int pnd_setup_layer_(int fd, int enabled, int x, int y, int w, int h)\r
231 {\r
232         struct omapfb_plane_info pi;\r
233         struct omapfb_mem_info mi;\r
234         int ret;\r
235 \r
236         ret = ioctl(fd, OMAPFB_QUERY_PLANE, &pi);\r
237         if (ret != 0) {\r
238                 perror("QUERY_PLANE");\r
239                 return -1;\r
240         }\r
241 \r
242         ret = ioctl(fd, OMAPFB_QUERY_MEM, &mi);\r
243         if (ret != 0) {\r
244                 perror("QUERY_MEM");\r
245                 return -1;\r
246         }\r
247 \r
248         /* must disable when changing stuff */\r
249         if (pi.enabled) {\r
250                 pi.enabled = 0;\r
251                 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);\r
252                 if (ret != 0)\r
253                         perror("SETUP_PLANE");\r
254         }\r
255 \r
256         mi.size = 320*240*2*4;\r
257         ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi);\r
258         if (ret != 0) {\r
259                 perror("SETUP_MEM");\r
260                 return -1;\r
261         }\r
262 \r
263         pi.pos_x = x;\r
264         pi.pos_y = y;\r
265         pi.out_width = w;\r
266         pi.out_height = h;\r
267         pi.enabled = enabled;\r
268 \r
269         ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);\r
270         if (ret != 0) {\r
271                 perror("SETUP_PLANE");\r
272                 return -1;\r
273         }\r
274 \r
275         return 0;\r
276 }\r
277 \r
278 int pnd_setup_layer(int enabled, int x, int y, int w, int h)\r
279 {\r
280         return pnd_setup_layer_(vout_fbdev_get_fd(layer_fb), enabled, x, y, w, h);\r
281 }\r
282 \r
283 void pnd_restore_layer_data(void)\r
284 {\r
285         short *t = (short *)fb_copy + 320*240 / 2 + 160;\r
286 \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
291 \r
292         memcpy(g_screen_ptr, (void *)fb_copy, 320*240*2);\r
293         plat_video_flip();\r
294 }\r
295 \r
296 void emu_video_mode_change(int start_line, int line_count, int is_32cols)\r
297 {\r
298         int fb_w = 320, fb_h = 240, fb_left = 0, fb_right = 0, fb_top = 0, fb_bottom = 0;\r
299 \r
300         if (doing_bg_frame)\r
301                 return;\r
302 \r
303         if (is_32cols) {\r
304                 fb_w = 256;\r
305                 fb_left = fb_right = 32;\r
306         }\r
307 \r
308         switch (currentConfig.scaling) {\r
309         case SCALE_1x1:\r
310                 g_layer_w = fb_w;\r
311                 g_layer_h = fb_h;\r
312                 break;\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
316                 break;\r
317         case SCALE_2x2_2x2:\r
318                 g_layer_w = fb_w * 2;\r
319                 g_layer_h = fb_h * 2;\r
320                 break;\r
321         case SCALE_FULLSCREEN:\r
322                 g_layer_w = 800;\r
323                 g_layer_h = 480;\r
324                 break;\r
325         case SCALE_CUSTOM:\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
330                 break;\r
331         }\r
332 \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
337         }\r
338 \r
339         switch (currentConfig.scaling) {\r
340         case SCALE_FULLSCREEN:\r
341         case SCALE_CUSTOM:\r
342                 fb_top = start_line;\r
343                 fb_h = line_count;\r
344                 break;\r
345         }\r
346         g_osd_fps_x = is_32cols ? 232 : 264;\r
347         g_osd_y = fb_top + fb_h - 8;\r
348 \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
352         plat_video_flip();\r
353 \r
354         PicoDrawSetOutFormat(PDF_RGB555, 0);\r
355 }\r
356 \r
357 void plat_video_loop_prepare(void)\r
358 {\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
362 \r
363         // emu_video_mode_change will call pnd_setup_layer()\r
364 }\r
365 \r
366 void pemu_loop_prep(void)\r
367 {\r
368         // dirty buffers better go now than during gameplay\r
369         sync();\r
370         sleep(0);\r
371 }\r
372 \r
373 void pemu_loop_end(void)\r
374 {\r
375         /* do one more frame for menu bg */\r
376         pemu_forced_frame(0, 1);\r
377 \r
378         pnd_setup_layer(0, g_layer_x, g_layer_y, g_layer_w, g_layer_h);\r
379 }\r
380 \r
381 void plat_wait_till_us(unsigned int us_to)\r
382 {\r
383         unsigned int now;\r
384         signed int diff;\r
385 \r
386         now = plat_get_ticks_us();\r
387 \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
395         }\r
396 /*\r
397         while ((signed int)(us_to - now) > 512) {\r
398                 spend_cycles(1024);\r
399                 now = plat_get_ticks_us();\r
400         }\r
401 */\r
402 }\r
403 \r
404 void plat_early_init(void)\r
405 {\r
406 }\r
407 \r
408 void plat_init(void)\r
409 {\r
410         const char *main_fb_name, *layer_fb_name;\r
411         int fd, ret, w, h;\r
412 \r
413         main_fb_name = getenv("FBDEV_MAIN");\r
414         if (main_fb_name == NULL)\r
415                 main_fb_name = "/dev/fb0";\r
416 \r
417         layer_fb_name = getenv("FBDEV_LAYER");\r
418         if (layer_fb_name == NULL)\r
419                 layer_fb_name = "/dev/fb1";\r
420 \r
421         // must set the layer up first to be able to use it\r
422         fd = open(layer_fb_name, O_RDWR);\r
423         if (fd == -1) {\r
424                 fprintf(stderr, "%s: ", layer_fb_name);\r
425                 perror("open");\r
426                 exit(1);\r
427         }\r
428 \r
429         ret = pnd_setup_layer_(fd, 0, g_layer_x, g_layer_y, g_layer_w, g_layer_h);\r
430         close(fd);\r
431         if (ret != 0) {\r
432                 fprintf(stderr, "failed to set up layer, exiting.\n");\r
433                 exit(1);\r
434         }\r
435 \r
436         xenv_init(NULL, "PicoDrive " VERSION);\r
437 \r
438         w = h = 0;\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
442                 exit(1);\r
443         }\r
444 \r
445         g_menuscreen_w = w;\r
446         g_menuscreen_h = h;\r
447         g_menuscreen_ptr = vout_fbdev_flip(main_fb);\r
448 \r
449         w = 320; h = 240;\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
453                 goto fail0;\r
454         }\r
455 \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
458                 goto fail1;\r
459         }\r
460         g_screen_ptr = vout_fbdev_flip(layer_fb);\r
461 \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
465                 goto fail1;\r
466         }\r
467         g_menubg_ptr = temp_frame;\r
468         g_menubg_src_ptr = temp_frame;\r
469 \r
470         pnd_menu_init();\r
471 \r
472         in_evdev_init(in_evdev_defbinds);\r
473         in_probe();\r
474         plat_target_setup_input();\r
475 \r
476         sndout_oss_frag_frames = 2;\r
477 \r
478         return;\r
479 \r
480 fail1:\r
481         vout_fbdev_finish(layer_fb);\r
482 fail0:\r
483         vout_fbdev_finish(main_fb);\r
484         exit(1);\r
485 }\r
486 \r
487 void plat_finish(void)\r
488 {\r
489         vout_fbdev_finish(main_fb);\r
490         xenv_finish();\r
491 \r
492         printf("all done\n");\r
493 }\r
494 \r