get rid of the silly unzip_stream
[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 #define LAYER_MEM_SIZE (320*240*2 * 4)\r
37 \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
44 \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
49 \r
50 static const char * const pandora_gpio_keys[KEY_MAX + 1] = {\r
51         [0 ... KEY_MAX] = NULL,\r
52         [KEY_UP]        = "Up",\r
53         [KEY_LEFT]      = "Left",\r
54         [KEY_RIGHT]     = "Right",\r
55         [KEY_DOWN]      = "Down",\r
56         [KEY_HOME]      = "A",\r
57         [KEY_PAGEDOWN]  = "X",\r
58         [KEY_END]       = "B",\r
59         [KEY_PAGEUP]    = "Y",\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
65 };\r
66 \r
67 static struct in_default_bind in_evdev_defbinds[] =\r
68 {\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
93         { 0, 0, 0 }\r
94 };\r
95 \r
96 static const struct menu_keymap key_pbtn_map[] =\r
97 {\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
102         /* Pandora */\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
118 };\r
119 \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
124 };\r
125 \r
126 void pemu_prep_defconfig(void)\r
127 {\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
131 }\r
132 \r
133 void pemu_validate_config(void)\r
134 {\r
135         currentConfig.CPUclock = plat_target_cpu_clock_get();\r
136 }\r
137 \r
138 static void draw_cd_leds(void)\r
139 {\r
140         int old_reg;\r
141         old_reg = Pico_mcd->s68k_regs[0];\r
142 \r
143         if (0) {\r
144                 // 8-bit modes\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
153         } else {\r
154                 // 16-bit modes\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
161         }\r
162 }\r
163 \r
164 void pemu_finalize_frame(const char *fps, const char *notice)\r
165 {\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
171                 draw_cd_leds();\r
172 }\r
173 \r
174 void plat_video_flip(void)\r
175 {\r
176         g_screen_ptr = vout_fbdev_flip(layer_fb);\r
177         PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
178 \r
179         // XXX: drain OS event queue here, maybe we'll actually use it someday..\r
180         xenv_update(NULL, NULL, NULL, NULL);\r
181 }\r
182 \r
183 void plat_video_toggle_renderer(int change, int is_menu)\r
184 {\r
185 }\r
186 \r
187 void plat_video_menu_enter(int is_rom_loaded)\r
188 {\r
189 }\r
190 \r
191 void plat_video_menu_begin(void)\r
192 {\r
193 }\r
194 \r
195 void plat_video_menu_end(void)\r
196 {\r
197         g_menuscreen_ptr = vout_fbdev_flip(main_fb);\r
198 }\r
199 \r
200 void plat_video_menu_leave(void)\r
201 {\r
202 }\r
203 \r
204 void plat_video_wait_vsync(void)\r
205 {\r
206         vout_fbdev_wait_vsync(main_fb);\r
207 }\r
208 \r
209 void plat_status_msg_clear(void)\r
210 {\r
211         vout_fbdev_clear_lines(layer_fb, g_osd_y, 8);\r
212 }\r
213 \r
214 void plat_status_msg_busy_next(const char *msg)\r
215 {\r
216         plat_status_msg_clear();\r
217         pemu_finalize_frame("", msg);\r
218         plat_video_flip();\r
219         emu_status_msg("");\r
220         reset_timing = 1;\r
221 }\r
222 \r
223 void plat_status_msg_busy_first(const char *msg)\r
224 {\r
225         plat_status_msg_busy_next(msg);\r
226 }\r
227 \r
228 void plat_update_volume(int has_changed, int is_up)\r
229 {\r
230 }\r
231 \r
232 void pemu_forced_frame(int no_scale, int do_emu)\r
233 {\r
234         doing_bg_frame = 1;\r
235         emu_cmn_forced_frame(no_scale, do_emu);\r
236         doing_bg_frame = 0;\r
237 \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
241 }\r
242 \r
243 void pemu_sound_start(void)\r
244 {\r
245         emu_sound_start();\r
246 }\r
247 \r
248 void plat_debug_cat(char *str)\r
249 {\r
250 }\r
251 \r
252 static int pnd_setup_layer_(int fd, int enabled, int x, int y, int w, int h)\r
253 {\r
254         struct omapfb_plane_info pi;\r
255         struct omapfb_mem_info mi;\r
256         int is_enabled;\r
257         int retval = 0;\r
258         int ret;\r
259 \r
260         ret = ioctl(fd, OMAPFB_QUERY_PLANE, &pi);\r
261         if (ret != 0) {\r
262                 perror("QUERY_PLANE");\r
263                 return -1;\r
264         }\r
265 \r
266         ret = ioctl(fd, OMAPFB_QUERY_MEM, &mi);\r
267         if (ret != 0) {\r
268                 perror("QUERY_MEM");\r
269                 return -1;\r
270         }\r
271 \r
272         /* must disable when changing stuff */\r
273         is_enabled = pi.enabled;\r
274         if (is_enabled) {\r
275                 pi.enabled = 0;\r
276                 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);\r
277                 if (ret != 0)\r
278                         perror("SETUP_PLANE");\r
279                 else\r
280                         is_enabled = 0;\r
281         }\r
282 \r
283         if (mi.size < LAYER_MEM_SIZE) {\r
284                 unsigned int size_old = mi.size;\r
285 \r
286                 mi.size = LAYER_MEM_SIZE;\r
287                 ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi);\r
288                 if (ret != 0) {\r
289                         perror("SETUP_MEM");\r
290                         fprintf(stderr, "(requested %u, had %u)\n",\r
291                                 mi.size, size_old);\r
292                         return -1;\r
293                 }\r
294         }\r
295 \r
296         pi.pos_x = x;\r
297         pi.pos_y = y;\r
298         pi.out_width = w;\r
299         pi.out_height = h;\r
300         pi.enabled = enabled;\r
301 \r
302         ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);\r
303         if (ret == 0) {\r
304                 is_enabled = pi.enabled;\r
305         }\r
306         else {\r
307                 perror("SETUP_PLANE");\r
308                 retval = -1;\r
309         }\r
310 \r
311         plat_target_switch_layer(1, is_enabled);\r
312 \r
313         return retval;\r
314 }\r
315 \r
316 int pnd_setup_layer(int enabled, int x, int y, int w, int h)\r
317 {\r
318         return pnd_setup_layer_(vout_fbdev_get_fd(layer_fb), enabled, x, y, w, h);\r
319 }\r
320 \r
321 void pnd_restore_layer_data(void)\r
322 {\r
323         short *t = (short *)fb_copy + 320*240 / 2 + 160;\r
324 \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
329 \r
330         memcpy(g_screen_ptr, (void *)fb_copy, 320*240*2);\r
331         plat_video_flip();\r
332 }\r
333 \r
334 void emu_video_mode_change(int start_line, int line_count, int is_32cols)\r
335 {\r
336         int fb_w = 320, fb_h = 240, fb_left = 0, fb_right = 0, fb_top = 0, fb_bottom = 0;\r
337 \r
338         if (doing_bg_frame)\r
339                 return;\r
340 \r
341         if (is_32cols) {\r
342                 fb_w = 256;\r
343                 fb_left = fb_right = 32;\r
344         }\r
345 \r
346         switch (currentConfig.scaling) {\r
347         case SCALE_1x1:\r
348                 g_layer_w = fb_w;\r
349                 g_layer_h = fb_h;\r
350                 break;\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
354                 break;\r
355         case SCALE_2x2_2x2:\r
356                 g_layer_w = fb_w * 2;\r
357                 g_layer_h = fb_h * 2;\r
358                 break;\r
359         case SCALE_FULLSCREEN:\r
360                 g_layer_w = 800;\r
361                 g_layer_h = 480;\r
362                 break;\r
363         case SCALE_CUSTOM:\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
368                 break;\r
369         }\r
370 \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
375         }\r
376 \r
377         switch (currentConfig.scaling) {\r
378         case SCALE_FULLSCREEN:\r
379         case SCALE_CUSTOM:\r
380                 fb_top = start_line;\r
381                 fb_h = line_count;\r
382                 break;\r
383         }\r
384         g_osd_fps_x = is_32cols ? 232 : 264;\r
385         g_osd_y = fb_top + fb_h - 8;\r
386 \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
390         plat_video_flip();\r
391 \r
392         PicoDrawSetOutFormat(PDF_RGB555, 0);\r
393 }\r
394 \r
395 void plat_video_loop_prepare(void)\r
396 {\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
400 \r
401         // emu_video_mode_change will call pnd_setup_layer()\r
402 }\r
403 \r
404 void pemu_loop_prep(void)\r
405 {\r
406         // dirty buffers better go now than during gameplay\r
407         fflush(stdout);\r
408         fflush(stderr);\r
409         sync();\r
410         sleep(0);\r
411 }\r
412 \r
413 void pemu_loop_end(void)\r
414 {\r
415         /* do one more frame for menu bg */\r
416         pemu_forced_frame(0, 1);\r
417 \r
418         pnd_setup_layer(0, g_layer_x, g_layer_y, g_layer_w, g_layer_h);\r
419 }\r
420 \r
421 void plat_wait_till_us(unsigned int us_to)\r
422 {\r
423         unsigned int now;\r
424         signed int diff;\r
425 \r
426         now = plat_get_ticks_us();\r
427 \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
435         }\r
436 /*\r
437         while ((signed int)(us_to - now) > 512) {\r
438                 spend_cycles(1024);\r
439                 now = plat_get_ticks_us();\r
440         }\r
441 */\r
442 }\r
443 \r
444 void plat_early_init(void)\r
445 {\r
446 }\r
447 \r
448 void plat_init(void)\r
449 {\r
450         const char *main_fb_name, *layer_fb_name;\r
451         int fd, ret, w, h;\r
452 \r
453         main_fb_name = getenv("FBDEV_MAIN");\r
454         if (main_fb_name == NULL)\r
455                 main_fb_name = "/dev/fb0";\r
456 \r
457         layer_fb_name = getenv("FBDEV_LAYER");\r
458         if (layer_fb_name == NULL)\r
459                 layer_fb_name = "/dev/fb1";\r
460 \r
461         // must set the layer up first to be able to use it\r
462         fd = open(layer_fb_name, O_RDWR);\r
463         if (fd == -1) {\r
464                 fprintf(stderr, "%s: ", layer_fb_name);\r
465                 perror("open");\r
466                 exit(1);\r
467         }\r
468 \r
469         ret = pnd_setup_layer_(fd, 0, g_layer_x, g_layer_y, g_layer_w, g_layer_h);\r
470         close(fd);\r
471         if (ret != 0) {\r
472                 fprintf(stderr, "failed to set up layer, exiting.\n");\r
473                 exit(1);\r
474         }\r
475 \r
476         xenv_init(NULL, "PicoDrive " VERSION);\r
477 \r
478         w = h = 0;\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
482                 exit(1);\r
483         }\r
484 \r
485         g_menuscreen_w = w;\r
486         g_menuscreen_h = h;\r
487         g_menuscreen_ptr = vout_fbdev_flip(main_fb);\r
488 \r
489         w = 320; h = 240;\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
493                 goto fail0;\r
494         }\r
495 \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
498                 goto fail1;\r
499         }\r
500         g_screen_ptr = vout_fbdev_flip(layer_fb);\r
501 \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
505                 goto fail1;\r
506         }\r
507         g_menubg_ptr = temp_frame;\r
508         g_menubg_src_ptr = temp_frame;\r
509 \r
510         pnd_menu_init();\r
511 \r
512         // default ROM path\r
513         strcpy(rom_fname_loaded, "/media");\r
514 \r
515         in_evdev_init(&pandora_evdev_pdata);\r
516         in_probe();\r
517         plat_target_setup_input();\r
518 \r
519         sndout_oss_frag_frames = 2;\r
520 \r
521         return;\r
522 \r
523 fail1:\r
524         vout_fbdev_finish(layer_fb);\r
525 fail0:\r
526         vout_fbdev_finish(main_fb);\r
527         exit(1);\r
528 }\r
529 \r
530 void plat_finish(void)\r
531 {\r
532         vout_fbdev_finish(main_fb);\r
533         xenv_finish();\r
534 \r
535         printf("all done\n");\r
536 }\r
537 \r