2 * (C) GraÅžvydas "notaz" Ignotas, 2009-2012
4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
8 * Main Memory : Wiz = 0 ~ 2A00000, Caanoo = 0 ~ 5600000 (86M)
9 * Frame Buffer : Wiz = 2A00000 ~ 2E00000 (4M), Caanoo = 5600000 ~ 5700000 (1M)
10 * Sound Buffer : Wiz = 2E00000 ~ 3000000 (2M), Caanoo = 5700000 ~ 5800000 (1M)
11 * YUV Buffer : Caanoo = 5800000 ~ 6000000 (8M)
12 * 3D Buffer : Wiz = 3000000 ~ 4000000 (16M),Caanoo = 6000000 ~ 8000000 (32M)
14 * Caanoo dram column (or row?) is 1024 bytes?
16 * pollux display array:
17 * 27:24 | 23:18 | 17:11 | 10:6 | 5:0
18 * seg | y[11:5] | x[11:6] | y[4:0] | x[5:0]
19 * | blk_index[12:0] | block[10:0]
25 #include <sys/types.h>
28 #include <sys/ioctl.h>
32 #include <linux/soundcard.h>
33 #include <linux/input.h>
35 #include "libpicofe/plat.h"
36 #include "libpicofe/input.h"
37 #include "libpicofe/gp2x/in_gp2x.h"
38 #include "libpicofe/gp2x/soc_pollux.h"
39 #include "libpicofe/linux/in_evdev.h"
40 #include "libpicofe/menu.h"
41 #include "warm/warm.h"
42 #include "plugin_lib.h"
43 #include "pl_gun_ts.h"
45 #include "in_tsbutton.h"
50 #include "../libpcsxcore/psxmem_map.h"
53 static int fbdev = -1;
54 static void *fb_vaddrs[2];
55 static unsigned int fb_paddrs[2];
56 static int fb_work_buf;
58 #define FB_VRAM_SIZE (320*240*2*2*2) // 2 buffers with space for 24bpp mode
59 static unsigned int uppermem_pbase, vram_pbase;
61 static unsigned short *psx_vram;
62 static int psx_step, psx_width, psx_height, psx_bpp;
63 static int psx_offset_x, psx_offset_y, psx_src_width, psx_src_height;
64 static int fb_offset_x, fb_offset_y;
66 static void caanoo_init(void);
67 static void wiz_init(void);
70 static const struct in_default_bind in_evdev_defbinds[] = {
71 { KEY_UP, IN_BINDTYPE_PLAYER12, DKEY_UP },
72 { KEY_DOWN, IN_BINDTYPE_PLAYER12, DKEY_DOWN },
73 { KEY_LEFT, IN_BINDTYPE_PLAYER12, DKEY_LEFT },
74 { KEY_RIGHT, IN_BINDTYPE_PLAYER12, DKEY_RIGHT },
75 { BTN_TOP, IN_BINDTYPE_PLAYER12, DKEY_TRIANGLE },
76 { BTN_THUMB, IN_BINDTYPE_PLAYER12, DKEY_CROSS },
77 { BTN_THUMB2, IN_BINDTYPE_PLAYER12, DKEY_CIRCLE },
78 { BTN_TRIGGER, IN_BINDTYPE_PLAYER12, DKEY_SQUARE },
79 { BTN_BASE3, IN_BINDTYPE_PLAYER12, DKEY_START },
80 { BTN_BASE4, IN_BINDTYPE_PLAYER12, DKEY_SELECT },
81 { BTN_TOP2, IN_BINDTYPE_PLAYER12, DKEY_L1 },
82 { BTN_PINKIE, IN_BINDTYPE_PLAYER12, DKEY_R1 },
83 { BTN_BASE, IN_BINDTYPE_EMU, SACTION_ENTER_MENU },
87 static const struct menu_keymap key_pbtn_map[] =
90 { KEY_DOWN, PBTN_DOWN },
91 { KEY_LEFT, PBTN_LEFT },
92 { KEY_RIGHT, PBTN_RIGHT },
94 { BTN_THUMB2, PBTN_MOK },
95 { BTN_THUMB, PBTN_MBACK },
96 { BTN_TRIGGER, PBTN_MA2 },
97 { BTN_TOP, PBTN_MA3 },
98 { BTN_BASE, PBTN_MENU },
100 { BTN_PINKIE, PBTN_R },
101 /* "normal" keyboards */
102 { KEY_ENTER, PBTN_MOK },
103 { KEY_ESC, PBTN_MBACK },
104 { KEY_SEMICOLON, PBTN_MA2 },
105 { KEY_APOSTROPHE, PBTN_MA3 },
106 { KEY_BACKSLASH, PBTN_MENU },
107 { KEY_LEFTBRACE, PBTN_L },
108 { KEY_RIGHTBRACE, PBTN_R },
111 static const struct in_pdata gp2x_evdev_pdata = {
112 .defbinds = in_evdev_defbinds,
113 .key_map = key_pbtn_map,
114 .kmap_size = sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]),
117 static void *fb_flip(void)
119 memregl[0x406C>>2] = memregl[0x446C>>2] = fb_paddrs[fb_work_buf];
120 memregl[0x4058>>2] |= 0x10;
121 memregl[0x4458>>2] |= 0x10;
123 return fb_vaddrs[fb_work_buf];
126 static void pollux_changemode(int bpp, int is_bgr)
128 int code = 0, bytes = 2;
131 printf("changemode: %dbpp %s\n", bpp, is_bgr ? "bgr" : "rgb");
133 memregl[0x4004>>2] = 0x00ef013f;
134 memregl[0x4000>>2] |= 1 << 3;
143 code = is_bgr ? 0xc342 : 0x4432;
147 code = is_bgr ? 0xc653 : 0x4653;
151 printf("unhandled bpp request: %d\n", bpp);
155 // program both MLCs so that TV-out works
156 memregl[0x405c>>2] = memregl[0x445c>>2] = bytes;
157 memregl[0x4060>>2] = memregl[0x4460>>2] = 320 * bytes;
159 r = memregl[0x4058>>2];
160 r = (r & 0xffff) | (code << 16) | 0x10;
161 memregl[0x4058>>2] = r;
163 r = memregl[0x4458>>2];
164 r = (r & 0xffff) | (code << 16) | 0x10;
165 memregl[0x4458>>2] = r;
168 static int cpu_clock_wrapper(int mhz)
170 // stupid pll share hack - must restart audio
171 int pollux_cpu_clock_set(int cpu_clock);
172 extern long SPUopen(void);
173 extern long SPUclose(void);
175 pollux_cpu_clock_set(mhz);
182 #define TIMER_BASE3 0x1980
183 #define TIMER_REG(x) memregl[(TIMER_BASE3 + x) >> 2]
185 static __attribute__((unused)) unsigned int timer_get(void)
187 TIMER_REG(0x08) |= 0x48; /* run timer, latch value */
191 void plat_video_menu_enter(int is_rom_loaded)
193 if (pl_vout_buf != NULL) {
195 // have to do rgb conversion for menu bg
196 bgr555_to_rgb565(pl_vout_buf, pl_vout_buf, 320*240*2);
198 memset(pl_vout_buf, 0, 320*240*2);
201 pollux_changemode(16, 0);
204 void plat_video_menu_begin(void)
208 void plat_video_menu_end(void)
210 g_menuscreen_ptr = fb_flip();
213 void plat_video_menu_leave(void)
215 if (psx_vram == NULL) {
216 fprintf(stderr, "GPU plugin did not provide vram\n");
220 if (gp2x_dev_id == GP2X_DEV_CAANOO)
221 in_set_config_int(in_name_to_id("evdev:pollux-analog"),
222 IN_CFG_ABS_DEAD_ZONE, analog_deadzone);
224 memset(g_menuscreen_ptr, 0, 320*240 * psx_bpp/8);
225 g_menuscreen_ptr = fb_flip();
226 memset(g_menuscreen_ptr, 0, 320*240 * psx_bpp/8);
229 void *plat_prepare_screenshot(int *w, int *h, int *bpp)
231 bgr555_to_rgb565(pl_vout_buf, pl_vout_buf, 320*240*2);
238 void plat_minimize(void)
242 static void spend_cycles(int loops)
246 "0: subs r0,r0,#1 ;\n"
248 :: "r" (loops) : "cc", "r0");
251 #define DMA_BASE6 0x0300
252 #define DMA_REG(x) memregl[(DMA_BASE6 + x) >> 2]
254 /* this takes ~1.5ms, while ldm/stm ~1.95ms */
255 static void raw_blit_dma(int doffs, const void *vram, int w, int h,
256 int sstride, int bgr24)
258 unsigned int pixel_offset = (unsigned short *)vram - psx_vram;
259 unsigned int dst = fb_paddrs[fb_work_buf] +
260 (fb_offset_y * 320 + fb_offset_x) * psx_bpp / 8;
261 int spsx_line = pixel_offset / 1024 + psx_offset_y;
262 int spsx_offset = (pixel_offset + psx_offset_x) & 0x3f8;
263 unsigned int vram_byte_pos, vram_byte_step;
264 int dst_stride = 320 * psx_bpp / 8;
265 int len = psx_src_width * psx_bpp / 8;
268 //warm_cache_op_all(WOP_D_CLEAN);
273 if (DMA_REG(0x0c) & 0x90000) {
274 printf("already runnig DMA?\n");
275 DMA_REG(0x0c) = 0x100000;
277 if ((DMA_REG(0x2c) & 0x0f) < 5) {
278 printf("DMA queue busy?\n");
282 vram_byte_pos = vram_pbase;
283 vram_byte_pos += (spsx_line & 511) * 2 * 1024 + spsx_offset * 2;
284 vram_byte_step = psx_step * 2 * 1024;
286 for (i = psx_src_height; i > 0;
287 i--, vram_byte_pos += vram_byte_step, dst += dst_stride)
289 while ((DMA_REG(0x2c) & 0x0f) < 4)
292 // XXX: it seems we must always set all regs, what is autoincrement there for?
293 DMA_REG(0x20) = 1; // queue wait cmd
294 DMA_REG(0x10) = vram_byte_pos; // DMA src
295 DMA_REG(0x14) = dst; // DMA dst
296 DMA_REG(0x18) = len - 1; // len
297 DMA_REG(0x1c) = 0x80000; // go
301 #define make_flip_func(name, blitfunc) \
302 static void name(int doffs, const void *vram_, int w, int h, int sstride, int bgr24) \
304 const unsigned short *vram = vram_; \
305 unsigned char *dst = (unsigned char *)g_menuscreen_ptr + \
306 (fb_offset_y * 320 + fb_offset_x) * psx_bpp / 8; \
307 int dst_stride = 320 * psx_bpp / 8; \
308 int len = psx_src_width * psx_bpp / 8; \
311 vram += psx_offset_y * 1024 + psx_offset_x; \
312 vram = (void *)((long)vram & ~3); \
313 for (i = psx_src_height; i > 0; i--, vram += psx_step * 1024, dst += dst_stride)\
314 blitfunc(dst, vram, len); \
317 make_flip_func(raw_blit_soft, memcpy)
318 make_flip_func(raw_blit_soft_368, blit320_368)
319 make_flip_func(raw_blit_soft_512, blit320_512)
320 make_flip_func(raw_blit_soft_640, blit320_640)
322 void *plat_gvideo_set_mode(int *w_, int *h_, int *bpp_)
324 int poff_w, poff_h, w_max;
325 int w = *w_, h = *h_, bpp = *bpp_;
327 if (!w || !h || !bpp)
330 printf("psx mode: %dx%d@%d\n", w, h, bpp);
335 switch (w + (bpp != 16) + !soft_scaling) {
337 pl_plat_blit = raw_blit_soft_640;
341 pl_plat_blit = raw_blit_soft_512;
346 pl_plat_blit = raw_blit_soft_368;
350 pl_plat_blit = have_warm ? raw_blit_dma : raw_blit_soft;
363 poff_w = w / 2 - w_max / 2;
368 fb_offset_x = 320/2 - w / 2;
370 poff_h = h / 2 - 240/2;
373 fb_offset_y = 240/2 - h / 2;
375 psx_offset_x = poff_w * psx_bpp/8 / 2;
376 psx_offset_y = poff_h;
380 if (fb_offset_x || fb_offset_y) {
381 // not fullscreen, must clear borders
382 memset(g_menuscreen_ptr, 0, 320*240 * psx_bpp/8);
383 g_menuscreen_ptr = fb_flip();
384 memset(g_menuscreen_ptr, 0, 320*240 * psx_bpp/8);
387 pollux_changemode(bpp, 1);
389 pl_set_gun_rect(fb_offset_x, fb_offset_y, w > 320 ? 320 : w, h);
393 *h_ = fb_offset_y + psx_src_height;
395 return g_menuscreen_ptr;
398 /* not really used, we do raw_flip */
399 void plat_gvideo_open(int is_pal)
403 void *plat_gvideo_flip(void)
405 g_menuscreen_ptr = fb_flip();
406 return g_menuscreen_ptr;
409 void plat_gvideo_close(void)
413 static void *pl_emu_mmap(unsigned long addr, size_t size, int is_fixed,
425 if (size > 0x400000) {
426 fprintf(stderr, "unexpected ram map request: %08lx %x\n",
430 pbase = (uppermem_pbase + 0xffffff) & ~0xffffff;
432 retval = (void *)addr;
433 ret = warm_mmap_section(retval, pbase, size, WCB_C_BIT);
435 fprintf(stderr, "ram section map failed\n");
440 if (size > 0x400000) {
441 fprintf(stderr, "unexpected vram map request: %08lx %x\n",
447 vram_pbase = (uppermem_pbase + 0xffffff) & ~0xffffff;
448 retval = (void *)addr;
450 ret = warm_mmap_section(retval, vram_pbase, size, WCB_C_BIT);
452 fprintf(stderr, "vram section map failed\n");
457 // mostly for Wiz to not run out of RAM
458 if (size > 0x800000) {
459 fprintf(stderr, "unexpected LUT map request: %08lx %x\n",
463 pbase = (uppermem_pbase + 0xffffff) & ~0xffffff;
465 retval = (void *)addr;
466 ret = warm_mmap_section(retval, pbase, size, WCB_C_BIT);
468 fprintf(stderr, "LUT section map failed\n");
477 retval = plat_mmap(addr, size, 0, is_fixed);
480 if (tag == MAP_TAG_VRAM)
485 static void pl_emu_munmap(void *ptr, size_t size, enum psxMapTag tag)
491 warm_munmap_section(ptr, size);
494 plat_munmap(ptr, size);
501 const char *main_fb_name = "/dev/fb0";
502 struct fb_fix_screeninfo fbfix;
507 fbdev = open(main_fb_name, O_RDWR);
509 fprintf(stderr, "%s: ", main_fb_name);
514 ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix);
516 perror("ioctl(fbdev) failed");
519 uppermem_pbase = fbfix.smem_start;
521 printf("framebuffer: \"%s\" @ %08lx\n", fbfix.id, fbfix.smem_start);
522 fb_paddrs[0] = fbfix.smem_start;
523 fb_paddrs[1] = fb_paddrs[0] + 320*240*4; // leave space for 24bpp
526 have_warm = (ret == 0);
529 // map fb as write-through cached section
530 fb_vaddrs[0] = (void *)0x7fe00000;
531 ret = warm_mmap_section(fb_vaddrs[0], fb_paddrs[0],
532 FB_VRAM_SIZE, WCB_C_BIT);
534 fprintf(stderr, "fb section map failed\n");
537 // we could continue but it would just get messy
541 if (fb_vaddrs[0] == NULL) {
542 fb_vaddrs[0] = mmap(0, FB_VRAM_SIZE, PROT_READ|PROT_WRITE,
543 MAP_SHARED, memdev, fb_paddrs[0]);
544 if (fb_vaddrs[0] == MAP_FAILED) {
545 perror("mmap(fb_vaddrs) failed");
549 memset(fb_vaddrs[0], 0, FB_VRAM_SIZE);
550 warm_change_cb_range(WCB_C_BIT, 1, fb_vaddrs[0], FB_VRAM_SIZE);
552 printf(" mapped @%p\n", fb_vaddrs[0]);
554 fb_vaddrs[1] = (char *)fb_vaddrs[0] + 320*240*4;
556 memset(fb_vaddrs[0], 0, FB_VRAM_SIZE);
558 pollux_changemode(16, 0);
559 g_menuscreen_w = g_menuscreen_pp = 320;
560 g_menuscreen_h = 240;
561 g_menuscreen_ptr = fb_flip();
564 DMA_REG(0x0c) = 0x20000; // pending IRQ clear
567 in_evdev_init(&gp2x_evdev_pdata);
568 if (gp2x_dev_id == GP2X_DEV_CAANOO)
573 pl_plat_blit = have_warm ? raw_blit_dma : raw_blit_soft;
576 psx_src_height = 240;
579 pl_rearmed_cbs.screen_w = 320;
580 pl_rearmed_cbs.screen_h = 240;
582 plat_target_setup_input();
584 plat_target.cpu_clock_set = cpu_clock_wrapper;
586 psxMapHook = pl_emu_mmap;
587 psxUnmapHook = pl_emu_munmap;
590 void plat_finish(void)
593 memset(fb_vaddrs[0], 0, FB_VRAM_SIZE);
594 munmap(fb_vaddrs[0], FB_VRAM_SIZE);
596 plat_target_finish();
599 /* Caanoo stuff, perhaps move later */
600 static const char * const caanoo_keys[KEY_MAX + 1] = {
601 [0 ... KEY_MAX] = NULL,
604 [KEY_RIGHT] = "Right",
613 [BTN_BASE2] = "Lock",
616 [BTN_BASE5] = "Push",
622 short time, strength;
626 #define HAPTIC_IOCTL_MAGIC 'I'
627 #define HAPTIC_PLAY_PATTERN _IOW(HAPTIC_IOCTL_MAGIC, 4, struct haptic_data)
628 #define HAPTIC_INDIVIDUAL_MODE _IOW(HAPTIC_IOCTL_MAGIC, 5, unsigned int)
629 #define HAPTIC_SET_VIB_LEVEL _IOW(HAPTIC_IOCTL_MAGIC, 9, unsigned int)
631 static int hapticdev = -1;
632 static struct haptic_data haptic_seq[2];
634 static int haptic_read(const char *fname, struct haptic_data *data)
640 f = fopen(fname, "r");
642 fprintf(stderr, "fopen(%s)", fname);
647 for (i = 0; i < sizeof(data->actions) / sizeof(data->actions[0]); ) {
648 p = fgets(buf, sizeof(buf), f);
651 while (*p != 0 && *p == ' ')
653 if (*p == 0 || *p == ';' || *p == '#')
656 ret = sscanf(buf, "%d %d", &v1, &v2);
658 fprintf(stderr, "can't parse: %s", buf);
662 data->actions[i].time = v1;
663 data->actions[i].strength = v2;
669 fprintf(stderr, "bad haptic file: %s\n", fname);
677 static int haptic_init(void)
681 ret = haptic_read("haptic_w.cfg", &haptic_seq[0]);
684 ret = haptic_read("haptic_s.cfg", &haptic_seq[1]);
688 hapticdev = open("/dev/isa1200", O_RDWR | O_NONBLOCK);
689 if (hapticdev == -1) {
690 perror("open(/dev/isa1200)");
695 ret = ioctl(hapticdev, HAPTIC_INDIVIDUAL_MODE, &i); /* use 2 of them */
697 ret |= ioctl(hapticdev, HAPTIC_SET_VIB_LEVEL, &i); /* max */
699 fprintf(stderr, "haptic ioctls failed\n");
708 void plat_trigger_vibrate(int pad, int low, int high)
713 if (low == 0 && high == 0)
715 is_strong = (high >= 0xf0);
718 return; // it's broken
727 ioctl(hapticdev, HAPTIC_PLAY_PATTERN, &haptic_seq[!!is_strong]);
730 static void caanoo_init(void)
733 in_set_config(in_name_to_id("evdev:pollux-analog"), IN_CFG_KEY_NAMES,
734 caanoo_keys, sizeof(caanoo_keys));
738 static const struct in_default_bind in_gp2x_defbinds[] =
741 { GP2X_BTN_UP, IN_BINDTYPE_PLAYER12, DKEY_UP },
742 { GP2X_BTN_DOWN, IN_BINDTYPE_PLAYER12, DKEY_DOWN },
743 { GP2X_BTN_LEFT, IN_BINDTYPE_PLAYER12, DKEY_LEFT },
744 { GP2X_BTN_RIGHT, IN_BINDTYPE_PLAYER12, DKEY_RIGHT },
745 { GP2X_BTN_X, IN_BINDTYPE_PLAYER12, DKEY_CROSS },
746 { GP2X_BTN_B, IN_BINDTYPE_PLAYER12, DKEY_CIRCLE },
747 { GP2X_BTN_A, IN_BINDTYPE_PLAYER12, DKEY_SQUARE },
748 { GP2X_BTN_Y, IN_BINDTYPE_PLAYER12, DKEY_TRIANGLE },
749 { GP2X_BTN_L, IN_BINDTYPE_PLAYER12, DKEY_L1 },
750 { GP2X_BTN_R, IN_BINDTYPE_PLAYER12, DKEY_R1 },
751 { GP2X_BTN_START, IN_BINDTYPE_PLAYER12, DKEY_START },
752 { GP2X_BTN_SELECT, IN_BINDTYPE_EMU, SACTION_ENTER_MENU },
753 { GP2X_BTN_VOL_UP, IN_BINDTYPE_EMU, SACTION_VOLUME_UP },
754 { GP2X_BTN_VOL_DOWN, IN_BINDTYPE_EMU, SACTION_VOLUME_DOWN },
758 // unused dummy for in_gp2x
759 volatile unsigned short *gp2x_memregs;
761 static void wiz_init(void)
763 in_gp2x_init(in_gp2x_defbinds);