2 * All this is mostly based on rlyeh's minimal library.
\r
3 * Copied here to review all his code and understand what's going on.
\r
8 GP2X minimal library v0.A by rlyeh, (c) 2005. emulnation.info@rlyeh (swap it!)
\r
10 Thanks to Squidge, Robster, snaff, Reesy and NK, for the help & previous work! :-)
\r
15 Free for non-commercial projects (it would be nice receiving a mail from you).
\r
16 Other cases, ask me first.
\r
18 GamePark Holdings is not allowed to use this library and/or use parts from it.
\r
28 #include <sys/mman.h>
\r
29 #include <sys/types.h>
\r
30 #include <sys/stat.h>
\r
35 #include "../linux/usbjoy.h"
\r
36 #include "../linux/sndout_oss.h"
\r
37 #include "../common/arm_utils.h"
\r
38 #include "../common/arm_linux.h"
\r
40 volatile unsigned short *gp2x_memregs;
\r
42 volatile unsigned long *gp2x_memregl;
\r
43 static void *gp2x_screens[4];
\r
44 static int screensel = 0;
\r
47 static int touchdev = -1;
\r
48 static int touchcal[7] = { 6203, 0, -1501397, 0, -4200, 16132680, 65536 };
\r
52 #define FRAMEBUFF_WHOLESIZE (0x30000*4) // 320*240*2 + some more
\r
53 #define FRAMEBUFF_ADDR0 (0x4000000-FRAMEBUFF_WHOLESIZE)
\r
54 #define FRAMEBUFF_ADDR1 (FRAMEBUFF_ADDR0+0x30000)
\r
55 #define FRAMEBUFF_ADDR2 (FRAMEBUFF_ADDR1+0x30000)
\r
56 #define FRAMEBUFF_ADDR3 (FRAMEBUFF_ADDR2+0x30000)
\r
58 static const int gp2x_screenaddrs[4] = { FRAMEBUFF_ADDR0, FRAMEBUFF_ADDR1, FRAMEBUFF_ADDR2, FRAMEBUFF_ADDR3 };
\r
59 static int gp2x_screenaddrs_use[4];
\r
60 static unsigned short gp2x_screenaddr_old[4];
\r
64 void gp2x_video_flip(void)
\r
66 unsigned short lsw = (unsigned short) gp2x_screenaddrs_use[screensel&3];
\r
67 unsigned short msw = (unsigned short)(gp2x_screenaddrs_use[screensel&3] >> 16);
\r
69 gp2x_memregs[0x2910>>1] = msw;
\r
70 gp2x_memregs[0x2914>>1] = msw;
\r
71 gp2x_memregs[0x290E>>1] = lsw;
\r
72 gp2x_memregs[0x2912>>1] = lsw;
\r
74 // jump to other buffer:
\r
75 gp2x_screen = gp2x_screens[++screensel&3];
\r
78 /* doulblebuffered flip */
\r
79 void gp2x_video_flip2(void)
\r
81 unsigned short msw = (unsigned short)(gp2x_screenaddrs_use[screensel&1] >> 16);
\r
83 gp2x_memregs[0x2910>>1] = msw;
\r
84 gp2x_memregs[0x2914>>1] = msw;
\r
85 gp2x_memregs[0x290E>>1] = 0;
\r
86 gp2x_memregs[0x2912>>1] = 0;
\r
88 // jump to other buffer:
\r
89 gp2x_screen = gp2x_screens[++screensel&1];
\r
93 void gp2x_video_changemode2(int bpp)
\r
95 gp2x_memregs[0x28DA>>1]=(((bpp+1)/8)<<9)|0xAB; /*8/15/16/24bpp...*/
\r
96 gp2x_memregs[0x290C>>1]=320*((bpp+1)/8); /*line width in bytes*/
\r
100 void gp2x_video_changemode(int bpp)
\r
102 gp2x_video_changemode2(bpp);
\r
104 gp2x_memset_all_buffers(0, 0, 320*240*2);
\r
109 void gp2x_video_setpalette(int *pal, int len)
\r
111 unsigned short *g=(unsigned short *)pal;
\r
112 volatile unsigned short *memreg = &gp2x_memregs[0x295A>>1];
\r
113 gp2x_memregs[0x2958>>1] = 0;
\r
116 while(len--) *memreg=*g++;
\r
120 // TV Compatible function //
\r
121 void gp2x_video_RGB_setscaling(int ln_offs, int W, int H)
\r
123 float escalaw, escalah;
\r
124 int bpp = (gp2x_memregs[0x28DA>>1]>>9)&0x3;
\r
125 unsigned short scalw;
\r
128 gp2x_screenaddrs_use[0] = gp2x_screenaddrs[0] + ln_offs * 320 * bpp;
\r
129 gp2x_screenaddrs_use[1] = gp2x_screenaddrs[1] + ln_offs * 320 * bpp;
\r
130 gp2x_screenaddrs_use[2] = gp2x_screenaddrs[2] + ln_offs * 320 * bpp;
\r
131 gp2x_screenaddrs_use[3] = gp2x_screenaddrs[3] + ln_offs * 320 * bpp;
\r
133 escalaw = 1024.0; // RGB Horiz LCD
\r
134 escalah = 320.0; // RGB Vert LCD
\r
136 if(gp2x_memregs[0x2800>>1]&0x100) //TV-Out
\r
138 escalaw=489.0; // RGB Horiz TV (PAL, NTSC)
\r
139 if (gp2x_memregs[0x2818>>1] == 287) //PAL
\r
140 escalah=274.0; // RGB Vert TV PAL
\r
141 else if (gp2x_memregs[0x2818>>1] == 239) //NTSC
\r
142 escalah=331.0; // RGB Vert TV NTSC
\r
145 // scale horizontal
\r
146 scalw = (unsigned short)((float)escalaw *(W/320.0));
\r
147 /* if there is no horizontal scaling, vertical doesn't work. Here is a nasty wrokaround... */
\r
148 if (H != 240 && W == 320) scalw--;
\r
149 gp2x_memregs[0x2906>>1]=scalw;
\r
151 gp2x_memregl[0x2908>>2]=(unsigned long)((float)escalah *bpp *(H/240.0));
\r
155 void gp2x_video_wait_vsync(void)
\r
157 unsigned short v = gp2x_memregs[0x1182>>1];
\r
158 while (!((v ^ gp2x_memregs[0x1182>>1]) & 0x10)) spend_cycles(1024);
\r
162 void gp2x_video_flush_cache(void)
\r
164 // since we are using the mmu hack, we must flush the cache first
\r
165 cache_flush_d_inval_i(gp2x_screen, (char *)gp2x_screen + 320*240*2);
\r
169 void gp2x_memcpy_buffers(int buffers, void *data, int offset, int len)
\r
172 if (buffers & (1<<0)) { dst = (char *)gp2x_screens[0] + offset; if (dst != data) memcpy(dst, data, len); }
\r
173 if (buffers & (1<<1)) { dst = (char *)gp2x_screens[1] + offset; if (dst != data) memcpy(dst, data, len); }
\r
174 if (buffers & (1<<2)) { dst = (char *)gp2x_screens[2] + offset; if (dst != data) memcpy(dst, data, len); }
\r
175 if (buffers & (1<<3)) { dst = (char *)gp2x_screens[3] + offset; if (dst != data) memcpy(dst, data, len); }
\r
179 void gp2x_memcpy_all_buffers(void *data, int offset, int len)
\r
181 gp2x_memcpy_buffers(0xf, data, offset, len);
\r
185 void gp2x_memset_all_buffers(int offset, int byte, int len)
\r
187 memset((char *)gp2x_screens[0] + offset, byte, len);
\r
188 memset((char *)gp2x_screens[1] + offset, byte, len);
\r
189 memset((char *)gp2x_screens[2] + offset, byte, len);
\r
190 memset((char *)gp2x_screens[3] + offset, byte, len);
\r
194 void gp2x_pd_clone_buffer2(void)
\r
196 memcpy(gp2x_screen, gp2x_screens[2], 320*240*2);
\r
200 unsigned long gp2x_joystick_read(int allow_usb_joy)
\r
203 unsigned long value=(gp2x_memregs[0x1198>>1] & 0x00FF); // GPIO M
\r
204 if(value==0xFD) value=0xFA;
\r
205 if(value==0xF7) value=0xEB;
\r
206 if(value==0xDF) value=0xAF;
\r
207 if(value==0x7F) value=0xBE;
\r
208 value = ~((gp2x_memregs[0x1184>>1] & 0xFF00) | value | (gp2x_memregs[0x1186>>1] << 16)); // C D
\r
210 if (allow_usb_joy && num_of_joys > 0) {
\r
211 // check the usb joy as well..
\r
213 for (i = 0; i < num_of_joys; i++)
\r
214 value |= usbjoy_check(i);
\r
220 typedef struct ucb1x00_ts_event
\r
222 unsigned short pressure;
\r
225 unsigned short pad;
\r
226 struct timeval stamp;
\r
227 } UCB1X00_TS_EVENT;
\r
229 int gp2x_touchpad_read(int *x, int *y)
\r
231 UCB1X00_TS_EVENT event;
\r
232 static int zero_seen = 0;
\r
235 if (touchdev < 0) return -1;
\r
237 retval = read(touchdev, &event, sizeof(event));
\r
239 printf("touch read failed %i %i\n", retval, errno);
\r
242 // this is to ignore the messed-up 4.1.x driver
\r
243 if (event.pressure == 0) zero_seen = 1;
\r
245 if (x) *x = (event.x * touchcal[0] + touchcal[2]) >> 16;
\r
246 if (y) *y = (event.y * touchcal[4] + touchcal[5]) >> 16;
\r
247 // printf("read %i %i %i\n", event.pressure, *x, *y);
\r
249 return zero_seen ? event.pressure : 0;
\r
254 void Pause940(int yes)
\r
257 gp2x_memregs[0x0904>>1] &= 0xFFFE;
\r
259 gp2x_memregs[0x0904>>1] |= 1;
\r
263 void Reset940(int yes, int bank)
\r
265 gp2x_memregs[0x3B48>>1] = ((yes&1) << 7) | (bank & 0x03);
\r
268 static void proc_set(const char *path, const char *val)
\r
273 f = fopen(path, "w");
\r
275 printf("failed to open: %s\n", path);
\r
282 printf("\"%s\" is set to: ", path);
\r
283 f = fopen(path, "r");
\r
285 printf("(open failed)\n");
\r
289 fgets(tmp, sizeof(tmp), f);
\r
296 void gp2x_init(void)
\r
298 printf("entering init()\n"); fflush(stdout);
\r
300 memdev = open("/dev/mem", O_RDWR);
\r
303 perror("open(\"/dev/mem\")");
\r
307 gp2x_memregs = mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);
\r
308 printf("memregs are @ %p\n", gp2x_memregs);
\r
309 if(gp2x_memregs == MAP_FAILED)
\r
311 perror("mmap(memregs)");
\r
314 gp2x_memregl = (unsigned long *) gp2x_memregs;
\r
316 gp2x_memregs[0x2880>>1] &= ~0x383; // disable cursor, subpict, osd, video layers
\r
318 gp2x_screens[0] = mmap(0, FRAMEBUFF_WHOLESIZE, PROT_WRITE, MAP_SHARED, memdev, FRAMEBUFF_ADDR0);
\r
319 if(gp2x_screens[0] == MAP_FAILED)
\r
321 perror("mmap(gp2x_screen)");
\r
324 printf("framebuffers point to %p\n", gp2x_screens[0]);
\r
325 gp2x_screens[1] = (char *) gp2x_screens[0]+0x30000;
\r
326 gp2x_screens[2] = (char *) gp2x_screens[1]+0x30000;
\r
327 gp2x_screens[3] = (char *) gp2x_screens[2]+0x30000;
\r
329 gp2x_screen = gp2x_screens[0];
\r
332 gp2x_screenaddr_old[0] = gp2x_memregs[0x290E>>1];
\r
333 gp2x_screenaddr_old[1] = gp2x_memregs[0x2910>>1];
\r
334 gp2x_screenaddr_old[2] = gp2x_memregs[0x2912>>1];
\r
335 gp2x_screenaddr_old[3] = gp2x_memregs[0x2914>>1];
\r
337 memcpy(gp2x_screenaddrs_use, gp2x_screenaddrs, sizeof(gp2x_screenaddrs));
\r
338 gp2x_memset_all_buffers(0, 0, 320*240*2);
\r
343 /* init usb joys -GnoStiC */
\r
347 touchdev = open("/dev/touchscreen/wm97xx", O_RDONLY);
\r
348 if (touchdev >= 0) {
\r
349 FILE *pcf = fopen("/etc/pointercal", "r");
\r
351 fscanf(pcf, "%d %d %d %d %d %d %d", &touchcal[0], &touchcal[1],
\r
352 &touchcal[2], &touchcal[3], &touchcal[4], &touchcal[5], &touchcal[6]);
\r
355 printf("found touchscreen/wm97xx\n");
\r
358 /* disable Linux read-ahead */
\r
359 proc_set("/proc/sys/vm/max-readahead", "0\n");
\r
360 proc_set("/proc/sys/vm/min-readahead", "0\n");
\r
362 printf("exitting init()\n"); fflush(stdout);
\r
365 char *ext_menu = 0, *ext_state = 0;
\r
367 void gp2x_deinit(void)
\r
372 gp2x_video_changemode(15);
\r
373 gp2x_memregs[0x290E>>1] = gp2x_screenaddr_old[0];
\r
374 gp2x_memregs[0x2910>>1] = gp2x_screenaddr_old[1];
\r
375 gp2x_memregs[0x2912>>1] = gp2x_screenaddr_old[2];
\r
376 gp2x_memregs[0x2914>>1] = gp2x_screenaddr_old[3];
\r
378 munmap(gp2x_screens[0], FRAMEBUFF_WHOLESIZE);
\r
379 munmap((void *)gp2x_memregs, 0x10000);
\r
381 if (touchdev >= 0) close(touchdev);
\r
386 printf("all done, running ");
\r
388 // Zaq121's alternative frontend support from MAME
\r
389 if (ext_menu && ext_state) {
\r
390 printf("%s -state %s\n", ext_menu, ext_state);
\r
391 execl(ext_menu, ext_menu, "-state", ext_state, NULL);
\r
392 } else if(ext_menu) {
\r
393 printf("%s\n", ext_menu);
\r
394 execl(ext_menu, ext_menu, NULL);
\r
396 printf("gp2xmenu\n");
\r
397 chdir("/usr/gp2x");
\r
398 execl("gp2xmenu", "gp2xmenu", NULL);
\r
403 void lprintf(const char *fmt, ...)
\r