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
27 #include <sys/mman.h>
\r
28 #include <sys/types.h>
\r
29 #include <sys/stat.h>
\r
30 #include <sys/ioctl.h>
\r
31 #include <sys/soundcard.h>
\r
38 volatile unsigned short *gp2x_memregs;
\r
40 volatile unsigned long *gp2x_memregl;
\r
41 static void *gp2x_screens[4];
\r
42 static int screensel = 0;
\r
45 static int sounddev = 0, mixerdev = 0;
\r
49 #define FRAMEBUFF_WHOLESIZE (0x30000*4) // 320*240*2 + some more
\r
50 #define FRAMEBUFF_ADDR0 (0x4000000-FRAMEBUFF_WHOLESIZE)
\r
51 #define FRAMEBUFF_ADDR1 (FRAMEBUFF_ADDR0+0x30000)
\r
52 #define FRAMEBUFF_ADDR2 (FRAMEBUFF_ADDR1+0x30000)
\r
53 #define FRAMEBUFF_ADDR3 (FRAMEBUFF_ADDR2+0x30000)
\r
55 static const int gp2x_screenaddrs[] = { FRAMEBUFF_ADDR0, FRAMEBUFF_ADDR1, FRAMEBUFF_ADDR2, FRAMEBUFF_ADDR3 };
\r
56 static unsigned short gp2x_screenaddr_old[4];
\r
60 void gp2x_video_flip(void)
\r
62 unsigned short msw = (unsigned short)(gp2x_screenaddrs[screensel&3] >> 16);
\r
64 gp2x_memregs[0x2910>>1] = msw;
\r
65 gp2x_memregs[0x2914>>1] = msw;
\r
66 gp2x_memregs[0x290E>>1] = 0;
\r
67 gp2x_memregs[0x2912>>1] = 0;
\r
69 // jump to other buffer:
\r
70 gp2x_screen = gp2x_screens[++screensel&3];
\r
73 /* doulblebuffered flip */
\r
74 void gp2x_video_flip2(void)
\r
76 unsigned short msw = (unsigned short)(gp2x_screenaddrs[screensel&1] >> 16);
\r
78 gp2x_memregs[0x2910>>1] = msw;
\r
79 gp2x_memregs[0x2914>>1] = msw;
\r
80 gp2x_memregs[0x290E>>1] = 0;
\r
81 gp2x_memregs[0x2912>>1] = 0;
\r
83 // jump to other buffer:
\r
84 gp2x_screen = gp2x_screens[++screensel&1];
\r
88 void gp2x_video_changemode2(int bpp)
\r
90 gp2x_memregs[0x28DA>>1]=(((bpp+1)/8)<<9)|0xAB; /*8/15/16/24bpp...*/
\r
91 gp2x_memregs[0x290C>>1]=320*((bpp+1)/8); /*line width in bytes*/
\r
95 void gp2x_video_changemode(int bpp)
\r
97 gp2x_video_changemode2(bpp);
\r
99 gp2x_memset_all_buffers(0, 0, 320*240*2);
\r
104 void gp2x_video_setpalette(int *pal, int len)
\r
106 unsigned short *g=(unsigned short *)pal;
\r
107 volatile unsigned short *memreg = &gp2x_memregs[0x295A>>1];
\r
108 gp2x_memregs[0x2958>>1] = 0;
\r
111 while(len--) *memreg=*g++;
\r
115 // TV Compatible function //
\r
116 void gp2x_video_RGB_setscaling(int W, int H)
\r
118 float escalaw, escalah;
\r
119 int bpp = (gp2x_memregs[0x28DA>>1]>>9)&0x3;
\r
121 escalaw = 1024.0; // RGB Horiz LCD
\r
122 escalah = 320.0; // RGB Vert LCD
\r
124 if(gp2x_memregs[0x2800>>1]&0x100) //TV-Out
\r
126 escalaw=489.0; // RGB Horiz TV (PAL, NTSC)
\r
127 if (gp2x_memregs[0x2818>>1] == 287) //PAL
\r
128 escalah=274.0; // RGB Vert TV PAL
\r
129 else if (gp2x_memregs[0x2818>>1] == 239) //NTSC
\r
130 escalah=331.0; // RGB Vert TV NTSC
\r
133 // scale horizontal
\r
134 gp2x_memregs[0x2906>>1]=(unsigned short)((float)escalaw *(W/320.0));
\r
136 gp2x_memregl[0x2908>>2]=(unsigned long)((float)escalah *bpp *(H/240.0));
\r
140 /* LCD updates @ 80Hz? */
\r
141 void gp2x_video_wait_vsync(void)
\r
143 gp2x_memregs[0x2846>>1] = 0x20|2; //(gp2x_memregs[0x2846>>1] | 0x20) & ~2;
\r
144 while(!(gp2x_memregs[0x2846>>1] & 2));// usleep(1);
\r
148 void gp2x_memcpy_buffers(int buffers, void *data, int offset, int len)
\r
150 if (buffers & (1<<0)) memcpy((char *)gp2x_screens[0] + offset, data, len);
\r
151 if (buffers & (1<<1)) memcpy((char *)gp2x_screens[1] + offset, data, len);
\r
152 if (buffers & (1<<2)) memcpy((char *)gp2x_screens[2] + offset, data, len);
\r
153 if (buffers & (1<<3)) memcpy((char *)gp2x_screens[3] + offset, data, len);
\r
157 void gp2x_memcpy_all_buffers(void *data, int offset, int len)
\r
159 gp2x_memcpy_buffers(0xf, data, offset, len);
\r
163 void gp2x_memset_all_buffers(int offset, int byte, int len)
\r
165 memset((char *)gp2x_screens[0] + offset, byte, len);
\r
166 memset((char *)gp2x_screens[1] + offset, byte, len);
\r
167 memset((char *)gp2x_screens[2] + offset, byte, len);
\r
168 memset((char *)gp2x_screens[3] + offset, byte, len);
\r
172 void gp2x_pd_clone_buffer2(void)
\r
174 memcpy(gp2x_screen, gp2x_screens[2], 320*240);
\r
175 memset(gp2x_screen, 0xe0, 320*8);
\r
176 memset(gp2x_screen + 320*232, 0xe0, 320*8);
\r
180 unsigned long gp2x_joystick_read(int allow_usb_joy)
\r
183 unsigned long value=(gp2x_memregs[0x1198>>1] & 0x00FF);
\r
184 if(value==0xFD) value=0xFA;
\r
185 if(value==0xF7) value=0xEB;
\r
186 if(value==0xDF) value=0xAF;
\r
187 if(value==0x7F) value=0xBE;
\r
188 value = ~((gp2x_memregs[0x1184>>1] & 0xFF00) | value | (gp2x_memregs[0x1186>>1] << 16));
\r
190 if (allow_usb_joy && num_of_joys > 0) {
\r
191 // check the usb joy as well..
\r
192 gp2x_usbjoy_update();
\r
193 for (i = 0; i < num_of_joys; i++)
\r
194 value |= gp2x_usbjoy_check(i);
\r
200 static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0;
\r
202 void gp2x_start_sound(int rate, int bits, int stereo)
\r
204 int frag = 0, bsize, buffers;
\r
206 // if no settings change, we don't need to do anything
\r
207 if (rate == s_oldrate && s_oldbits == bits && s_oldstereo == stereo) return;
\r
209 if (sounddev > 0) close(sounddev);
\r
210 sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);
\r
211 if (sounddev == -1)
\r
212 printf("open(\"/dev/dsp\") failed with %i\n", errno);
\r
214 ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);
\r
215 ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);
\r
216 ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);
\r
217 // calculate buffer size
\r
220 if (rate > 22050) { bsize*=4; buffers*=2; } // 44k mode seems to be very demanding
\r
221 while ((bsize>>=1)) frag++;
\r
222 frag |= buffers<<16; // 16 buffers
\r
223 ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);
\r
224 printf("gp2x_set_sound: %i/%ibit/%s, %i buffers of %i bytes\n",
\r
225 rate, bits, stereo?"stereo":"mono", frag>>16, 1<<(frag&0xffff));
\r
227 s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo;
\r
232 void gp2x_sound_write(void *buff, int len)
\r
234 write(sounddev, buff, len);
\r
237 void gp2x_sound_sync(void)
\r
239 ioctl(sounddev, SOUND_PCM_SYNC, 0);
\r
242 void gp2x_sound_volume(int l, int r)
\r
244 l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;
\r
246 ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/
\r
251 void Pause940(int yes)
\r
254 gp2x_memregs[0x0904>>1] &= 0xFFFE;
\r
256 gp2x_memregs[0x0904>>1] |= 1;
\r
260 void Reset940(int yes, int bank)
\r
262 gp2x_memregs[0x3B48>>1] = ((yes&1) << 7) | (bank & 0x03);
\r
268 void gp2x_init(void)
\r
270 printf("entering init()\n"); fflush(stdout);
\r
272 memdev = open("/dev/mem", O_RDWR);
\r
275 printf("open(\"/dev/mem\") failed with %i\n", errno);
\r
279 gp2x_memregs = mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);
\r
280 printf("memregs are @ %p\n", gp2x_memregs);
\r
281 if(gp2x_memregs == MAP_FAILED)
\r
283 printf("mmap(memregs) failed with %i\n", errno);
\r
286 gp2x_memregl = (unsigned long *) gp2x_memregs;
\r
288 gp2x_screens[0] = mmap(0, FRAMEBUFF_WHOLESIZE, PROT_WRITE, MAP_SHARED, memdev, FRAMEBUFF_ADDR0);
\r
289 if(gp2x_screens[0] == MAP_FAILED)
\r
291 printf("mmap(gp2x_screen) failed with %i\n", errno);
\r
294 printf("framebuffers point to %p\n", gp2x_screens[0]);
\r
295 gp2x_screens[1] = (char *) gp2x_screens[0]+0x30000;
\r
296 gp2x_screens[2] = (char *) gp2x_screens[1]+0x30000;
\r
297 gp2x_screens[3] = (char *) gp2x_screens[2]+0x30000;
\r
299 gp2x_screen = gp2x_screens[0];
\r
302 gp2x_screenaddr_old[0] = gp2x_memregs[0x290E>>1];
\r
303 gp2x_screenaddr_old[1] = gp2x_memregs[0x2910>>1];
\r
304 gp2x_screenaddr_old[2] = gp2x_memregs[0x2912>>1];
\r
305 gp2x_screenaddr_old[3] = gp2x_memregs[0x2914>>1];
\r
307 gp2x_memset_all_buffers(0, 0, 320*240*2);
\r
310 mixerdev = open("/dev/mixer", O_RDWR);
\r
311 if (mixerdev == -1)
\r
312 printf("open(\"/dev/mixer\") failed with %i\n", errno);
\r
314 /* init usb joys -GnoStiC */
\r
315 gp2x_usbjoy_init();
\r
317 printf("exitting init()\n"); fflush(stdout);
\r
320 char *ext_menu = 0, *ext_state = 0;
\r
322 void gp2x_deinit(void)
\r
327 gp2x_video_changemode(15);
\r
328 gp2x_memregs[0x290E>>1] = gp2x_screenaddr_old[0];
\r
329 gp2x_memregs[0x2910>>1] = gp2x_screenaddr_old[1];
\r
330 gp2x_memregs[0x2912>>1] = gp2x_screenaddr_old[2];
\r
331 gp2x_memregs[0x2914>>1] = gp2x_screenaddr_old[3];
\r
333 munmap(gp2x_screens[0], FRAMEBUFF_WHOLESIZE);
\r
334 munmap((void *)gp2x_memregs, 0x10000);
\r
337 if (sounddev > 0) close(sounddev);
\r
339 gp2x_usbjoy_deinit();
\r
341 printf("all done, running ");
\r
343 // Zaq121's alternative frontend support from MAME
\r
344 if(ext_menu && ext_state) {
\r
345 printf("%s -state %s\n", ext_menu, ext_state);
\r
346 execl(ext_menu, ext_menu, "-state", ext_state, NULL);
\r
347 } else if(ext_menu) {
\r
348 printf("%s\n", ext_menu);
\r
349 execl(ext_menu, ext_menu, NULL);
\r
351 printf("gp2xmenu\n");
\r
352 chdir("/usr/gp2x");
\r
353 execl("gp2xmenu", "gp2xmenu", NULL);
\r