1 // (c) Copyright 2006-2009 notaz, All rights reserved.
\r
2 // Free for non-commercial use.
\r
4 // For commercial use, separate licencing terms must be obtained.
\r
9 #include <sys/types.h>
\r
10 #include <sys/stat.h>
\r
12 #include <sys/ioctl.h>
\r
14 #include <linux/fb.h>
\r
15 #include <linux/omapfb.h>
\r
17 #include "../common/emu.h"
\r
18 #include "../common/menu.h"
\r
19 #include "../common/plat.h"
\r
20 #include "../common/arm_utils.h"
\r
21 #include "../common/input.h"
\r
22 #include "../linux/sndout_oss.h"
\r
23 #include "../linux/fbdev.h"
\r
25 #include "asm_utils.h"
\r
26 #include "version.h"
\r
28 #include <pico/pico_int.h>
\r
30 #include <linux/input.h>
\r
32 static struct vout_fbdev *main_fb, *layer_fb;
\r
33 static int g_layer_x, g_layer_y;
\r
34 static int g_layer_w = 320, g_layer_h = 240;
\r
35 static int g_osd_fps_x, g_osd_y;
\r
37 static const char pnd_script_base[] = "sudo -n /usr/pandora/scripts";
\r
38 static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
\r
39 static unsigned char __attribute__((aligned(4))) temp_frame[g_menuscreen_w * g_menuscreen_h * 2];
\r
40 static unsigned char __attribute__((aligned(4))) fb_copy[g_screen_width * g_screen_height * 2];
\r
41 unsigned char *PicoDraw2FB = temp_frame;
\r
42 const char *renderer_names[] = { NULL };
\r
43 const char *renderer_names32x[] = { NULL };
\r
45 static const char * const pandora_gpio_keys[KEY_MAX + 1] = {
\r
46 [0 ... KEY_MAX] = NULL,
\r
48 [KEY_LEFT] = "Left",
\r
49 [KEY_RIGHT] = "Right",
\r
50 [KEY_DOWN] = "Down",
\r
52 [KEY_PAGEDOWN] = "X",
\r
55 [KEY_RIGHTSHIFT]= "L",
\r
56 [KEY_RIGHTCTRL] = "R",
\r
57 [KEY_LEFTALT] = "Start",
\r
58 [KEY_LEFTCTRL] = "Select",
\r
59 [KEY_MENU] = "Pandora",
\r
62 static int get_cpu_clock(void)
\r
66 f = fopen("/proc/pandora/cpu_mhz_max", "r");
\r
68 fscanf(f, "%d", &ret);
\r
74 void pemu_prep_defconfig(void)
\r
76 defaultConfig.EmuOpt |= EOPT_VSYNC|EOPT_16BPP;
\r
77 defaultConfig.s_PicoOpt |= POPT_EN_MCD_GFX|POPT_EN_MCD_PSYNC;
\r
78 defaultConfig.scaling = SCALE_2x2_3x2;
\r
81 void pemu_validate_config(void)
\r
83 currentConfig.CPUclock = get_cpu_clock();
\r
86 static void osd_text(int x, int y, const char *text)
\r
88 int len = strlen(text)*8;
\r
92 for (h = 0; h < 8; h++) {
\r
94 p = (unsigned short *)g_screen_ptr + x + g_screen_width*(y + h);
\r
95 for (i = len; i; i--, p++)
\r
96 *p = (*p>>2) & 0x39e7;
\r
98 emu_text_out16(x, y, text);
\r
101 static void draw_cd_leds(void)
\r
104 old_reg = Pico_mcd->s68k_regs[0];
\r
108 unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;
\r
109 unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;
\r
110 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*2+ 4) =
\r
111 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*3+ 4) =
\r
112 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*4+ 4) = col_g;
\r
113 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*2+12) =
\r
114 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*3+12) =
\r
115 *(unsigned int *)((char *)g_screen_ptr + g_screen_width*4+12) = col_r;
\r
118 unsigned int *p = (unsigned int *)((short *)g_screen_ptr + g_screen_width*2+4);
\r
119 unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;
\r
120 unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;
\r
121 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += g_screen_width/2 - 12/2;
\r
122 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += g_screen_width/2 - 12/2;
\r
123 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
\r
127 static int emuscan(unsigned int num)
\r
129 DrawLineDest = (unsigned short *)g_screen_ptr + num * g_screen_width;
\r
134 void pemu_finalize_frame(const char *fps, const char *notice)
\r
136 if (notice && notice[0])
\r
137 osd_text(2, g_osd_y, notice);
\r
138 if (fps && fps[0] && (currentConfig.EmuOpt & EOPT_SHOW_FPS))
\r
139 osd_text(g_osd_fps_x, g_osd_y, fps);
\r
140 if ((PicoAHW & PAHW_MCD) && (currentConfig.EmuOpt & EOPT_EN_CD_LEDS))
\r
144 void plat_video_flip(void)
\r
146 g_screen_ptr = vout_fbdev_flip(layer_fb);
\r
149 void plat_video_toggle_renderer(int change, int is_menu)
\r
153 void plat_video_menu_enter(int is_rom_loaded)
\r
157 void plat_video_menu_begin(void)
\r
161 void plat_video_menu_end(void)
\r
163 g_menuscreen_ptr = vout_fbdev_flip(main_fb);
\r
166 void plat_video_wait_vsync(void)
\r
168 vout_fbdev_wait_vsync(main_fb);
\r
171 void plat_status_msg_clear(void)
\r
173 vout_fbdev_clear_lines(layer_fb, g_osd_y, 8);
\r
176 void plat_status_msg_busy_next(const char *msg)
\r
178 plat_status_msg_clear();
\r
179 pemu_finalize_frame("", msg);
\r
181 emu_status_msg("");
\r
185 void plat_status_msg_busy_first(const char *msg)
\r
187 plat_status_msg_busy_next(msg);
\r
190 void plat_update_volume(int has_changed, int is_up)
\r
192 static int prev_frame = 0, wait_frames = 0;
\r
193 int vol = currentConfig.volume;
\r
198 if (vol < 99) vol++;
\r
200 if (vol > 0) vol--;
\r
203 sndout_oss_setvol(vol, vol);
\r
204 currentConfig.volume = vol;
\r
205 emu_status_msg("VOL: %02i", vol);
\r
206 prev_frame = Pico.m.frame_count;
\r
210 static void make_bg(void)
\r
212 unsigned short *s = (void *)fb_copy;
\r
213 unsigned int t, *d = (unsigned int *)g_menubg_src_ptr + 80 / 2;
\r
216 memset32(g_menubg_src_ptr, 0, 800 * 480 * 2 / 4);
\r
218 for (y = 0; y < 240; y++, s += 320, d += 800*2/2) {
\r
219 for (x = 0; x < 320; x++) {
\r
222 d[x] = d[x + 800 / 2] = t;
\r
227 void pemu_forced_frame(int no_scale, int do_emu)
\r
229 int po_old = PicoOpt;
\r
231 memset32(g_screen_ptr, 0, g_screen_width * g_screen_height * 2 / 4);
\r
233 PicoOpt |= POPT_ACC_SPRITES;
\r
235 PicoOpt |= POPT_EN_SOFTSCALE;
\r
237 PicoDrawSetOutFormat(PDF_RGB555, 1);
\r
238 Pico.m.dirtyPal = 1;
\r
242 PicoFrameDrawOnly();
\r
244 // making a copy because enabling the layer clears it's mem
\r
245 memcpy32((void *)fb_copy, g_screen_ptr, sizeof(fb_copy) / 4);
\r
246 make_bg(); // FIXME: honour no_scale
\r
251 static void updateSound(int len)
\r
256 if (PicoOpt & POPT_EN_STEREO)
\r
259 // sndout_oss_can_write() not reliable..
\r
260 if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && !sndout_oss_can_write(len))
\r
263 /* avoid writing audio when lagging behind to prevent audio lag */
\r
264 if (PicoSkipFrame == 2)
\r
267 t = plat_get_ticks_ms();
\r
268 sndout_oss_write(PsndOut, len);
\r
269 t = plat_get_ticks_ms() - t;
\r
271 printf("audio lag %u\n", t);
\r
274 void pemu_sound_start(void)
\r
276 int target_fps = Pico.m.pal ? 50 : 60;
\r
280 if (currentConfig.EmuOpt & EOPT_EN_SOUND)
\r
282 int snd_excess_add, frame_samples;
\r
283 int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0;
\r
285 PsndRerate(Pico.m.frame_count ? 1 : 0);
\r
287 frame_samples = PsndLen;
\r
288 snd_excess_add = ((PsndRate - PsndLen * target_fps)<<16) / target_fps;
\r
289 if (snd_excess_add != 0)
\r
293 * for 44k stereo, we do 1470 samples/frame
\r
294 * OMAP driver does power of 2 buffers, so we need at least 4K buffer.
\r
295 * The most we can lag is 1K samples, size of OMAP's McBSP FIFO,
\r
296 * with 2K sample buffer we might sometimes lag more than that,
\r
297 * thus causing underflows.
\r
299 printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
\r
300 PsndRate, PsndLen, snd_excess_add, is_stereo, Pico.m.pal);
\r
301 sndout_oss_start(PsndRate, frame_samples * 2, is_stereo);
\r
302 //sndout_oss_setvol(currentConfig.volume, currentConfig.volume);
\r
303 PicoWriteSound = updateSound;
\r
304 plat_update_volume(0, 0);
\r
305 memset(sndBuffer, 0, sizeof(sndBuffer));
\r
306 PsndOut = sndBuffer;
\r
310 void pemu_sound_stop(void)
\r
315 void pemu_sound_wait(void)
\r
317 // don't need to do anything, writes will block by themselves
\r
320 void plat_debug_cat(char *str)
\r
324 static int pnd_setup_layer_(int fd, int enabled, int x, int y, int w, int h)
\r
326 struct omapfb_plane_info pi;
\r
327 struct omapfb_mem_info mi;
\r
330 ret = ioctl(fd, OMAPFB_QUERY_PLANE, &pi);
\r
332 perror("QUERY_PLANE");
\r
336 ret = ioctl(fd, OMAPFB_QUERY_MEM, &mi);
\r
338 perror("QUERY_MEM");
\r
342 /* must disable when changing stuff */
\r
345 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
\r
347 perror("SETUP_PLANE");
\r
350 mi.size = 320*240*2*4;
\r
351 ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi);
\r
353 perror("SETUP_MEM");
\r
361 pi.enabled = enabled;
\r
363 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
\r
365 perror("SETUP_PLANE");
\r
372 int pnd_setup_layer(int enabled, int x, int y, int w, int h)
\r
374 return pnd_setup_layer_(vout_fbdev_get_fd(layer_fb), enabled, x, y, w, h);
\r
377 void pnd_restore_layer_data(void)
\r
379 short *t = (short *)fb_copy + 320*240 / 2 + 160;
\r
381 // right now this is used by menu, which wants to preview something
\r
382 // so try to get something on the layer.
\r
383 if ((t[0] | t[5] | t[13]) == 0)
\r
384 memset32((void *)fb_copy, 0x07000700, sizeof(fb_copy) / 4);
\r
386 memcpy32(g_screen_ptr, (void *)fb_copy, 320*240*2 / 4);
\r
390 static void apply_filter(int which)
\r
395 if (pnd_filter_list == NULL)
\r
398 for (i = 0; i < which; i++)
\r
399 if (pnd_filter_list[i] == NULL)
\r
402 if (pnd_filter_list[i] == NULL)
\r
405 snprintf(buf, sizeof(buf), "%s/op_videofir.sh %s", pnd_script_base, pnd_filter_list[i]);
\r
409 void emu_video_mode_change(int start_line, int line_count, int is_32cols)
\r
411 int fb_w = 320, fb_h = 240, fb_left = 0, fb_right = 0, fb_top = 0, fb_bottom = 0;
\r
413 PicoDrawSetOutFormat(PDF_RGB555, 1);
\r
414 PicoDrawSetCallbacks(emuscan, NULL);
\r
418 fb_left = fb_right = 32;
\r
421 switch (currentConfig.scaling) {
\r
426 case SCALE_2x2_3x2:
\r
427 g_layer_w = fb_w * (is_32cols ? 3 : 2);
\r
428 g_layer_h = fb_h * 2;
\r
430 case SCALE_2x2_2x2:
\r
431 g_layer_w = fb_w * 2;
\r
432 g_layer_h = fb_h * 2;
\r
434 case SCALE_FULLSCREEN:
\r
439 g_layer_x = g_layer_cx;
\r
440 g_layer_y = g_layer_cy;
\r
441 g_layer_w = g_layer_cw;
\r
442 g_layer_h = g_layer_ch;
\r
446 if (currentConfig.scaling != SCALE_CUSTOM) {
\r
447 // center the layer
\r
448 g_layer_x = 800 / 2 - g_layer_w / 2;
\r
449 g_layer_y = 480 / 2 - g_layer_h / 2;
\r
452 switch (currentConfig.scaling) {
\r
453 case SCALE_FULLSCREEN:
\r
455 fb_top = start_line;
\r
459 g_osd_fps_x = is_32cols ? 232 : 264;
\r
460 g_osd_y = fb_top + fb_h - 8;
\r
462 pnd_setup_layer(1, g_layer_x, g_layer_y, g_layer_w, g_layer_h);
\r
463 vout_fbdev_resize(layer_fb, fb_w, fb_h, fb_left, fb_right, fb_top, fb_bottom, 0);
\r
464 vout_fbdev_clear(layer_fb);
\r
468 void pemu_loop_prep(void)
\r
470 static int pal_old = -1;
\r
471 static int filter_old = -1;
\r
474 if (currentConfig.CPUclock != get_cpu_clock()) {
\r
475 snprintf(buf, sizeof(buf), "unset DISPLAY; echo y | %s/op_cpuspeed.sh %d",
\r
476 pnd_script_base, currentConfig.CPUclock);
\r
480 if (Pico.m.pal != pal_old) {
\r
481 snprintf(buf, sizeof(buf), "%s/op_lcdrate.sh %d",
\r
482 pnd_script_base, Pico.m.pal ? 50 : 60);
\r
484 pal_old = Pico.m.pal;
\r
487 if (currentConfig.filter != filter_old) {
\r
488 apply_filter(currentConfig.filter);
\r
489 filter_old = currentConfig.filter;
\r
492 // make sure there is no junk left behind the layer
\r
493 memset32(g_menuscreen_ptr, 0, 800 * 480 * 2 / 4);
\r
494 g_menuscreen_ptr = vout_fbdev_flip(main_fb);
\r
496 // emu_video_mode_change will call pnd_setup_layer()
\r
498 // dirty buffers better go now than during gameplay
\r
502 pemu_sound_start();
\r
505 void pemu_loop_end(void)
\r
509 /* do one more frame for menu bg */
\r
510 pemu_forced_frame(0, 1);
\r
512 pnd_setup_layer(0, g_layer_x, g_layer_y, g_layer_w, g_layer_h);
\r
515 void plat_wait_till_us(unsigned int us_to)
\r
520 now = plat_get_ticks_us();
\r
522 // XXX: need to check NOHZ
\r
523 diff = (signed int)(us_to - now);
\r
524 if (diff > 10000) {
\r
525 //printf("sleep %d\n", us_to - now);
\r
526 usleep(diff * 15 / 16);
\r
527 now = plat_get_ticks_us();
\r
528 //printf(" wake %d\n", (signed)(us_to - now));
\r
531 while ((signed int)(us_to - now) > 512) {
\r
532 spend_cycles(1024);
\r
533 now = plat_get_ticks_us();
\r
538 const char *plat_get_credits(void)
\r
540 return "PicoDrive v" VERSION " (c) notaz, 2006-2010\n\n\n"
\r
542 "fDave: Cyclone 68000 core,\n"
\r
543 " base code of PicoDrive\n"
\r
544 "Reesy & FluBBa: DrZ80 core\n"
\r
545 "MAME devs: YM2612 and SN76496 cores\n"
\r
546 "Pandora team: Pandora\n"
\r
547 "Inder, ketchupgun: graphics\n"
\r
549 "special thanks (for docs, ideas):\n"
\r
550 " Charles MacDonald, Haze,\n"
\r
551 " Stephane Dallongeville,\n"
\r
552 " Lordus, Exophase, Rokas,\n"
\r
553 " Nemesis, Tasco Deluxe";
\r
556 #include "../linux/oshide.h"
\r
558 void plat_early_init(void)
\r
562 void plat_init(void)
\r
564 const char *main_fb_name, *layer_fb_name;
\r
567 main_fb_name = getenv("FBDEV_MAIN");
\r
568 if (main_fb_name == NULL)
\r
569 main_fb_name = "/dev/fb0";
\r
571 layer_fb_name = getenv("FBDEV_LAYER");
\r
572 if (layer_fb_name == NULL)
\r
573 layer_fb_name = "/dev/fb1";
\r
575 // must set the layer up first to be able to use it
\r
576 fd = open(layer_fb_name, O_RDWR);
\r
578 fprintf(stderr, "%s: ", layer_fb_name);
\r
583 ret = pnd_setup_layer_(fd, 0, g_layer_x, g_layer_y, g_layer_w, g_layer_h);
\r
586 fprintf(stderr, "failed to set up layer, exiting.\n");
\r
593 main_fb = vout_fbdev_init(main_fb_name, &w, &h, 0);
\r
594 if (main_fb == NULL) {
\r
595 fprintf(stderr, "couldn't init fb: %s\n", main_fb_name);
\r
599 if (w != g_menuscreen_w || h != g_menuscreen_h) {
\r
600 fprintf(stderr, "%dx%d not supported on %s\n", w, h, main_fb_name);
\r
603 g_menuscreen_ptr = vout_fbdev_flip(main_fb);
\r
606 layer_fb = vout_fbdev_init(layer_fb_name, &w, &h, 0);
\r
607 if (layer_fb == NULL) {
\r
608 fprintf(stderr, "couldn't init fb: %s\n", layer_fb_name);
\r
612 if (w != g_screen_width || h != g_screen_height) {
\r
613 fprintf(stderr, "%dx%d not supported on %s\n", w, h, layer_fb_name);
\r
616 g_screen_ptr = vout_fbdev_flip(layer_fb);
\r
618 g_menubg_ptr = temp_frame;
\r
619 g_menubg_src_ptr = temp_frame;
\r
624 in_set_config(in_name_to_id("evdev:gpio-keys"), IN_CFG_KEY_NAMES,
\r
625 pandora_gpio_keys, sizeof(pandora_gpio_keys));
\r
629 vout_fbdev_finish(layer_fb);
\r
631 vout_fbdev_finish(main_fb);
\r
635 void plat_finish(void)
\r
638 vout_fbdev_finish(main_fb);
\r
641 printf("all done\n");
\r
645 void lprintf(const char *fmt, ...)
\r