3 GP2X minimal library v0.5 by rlyeh, 2005.
5 + GP2X video library with double buffering.
6 + GP2X soundring buffer library with double buffering.
7 + GP2X joystick library.
9 Thanks to Squidge, Robster, snaff and NK, for the help & previous work! :-)
15 0.5: patched sound for real stereo (using NK's solution); better init code.
17 0.4: lots of cleanups; sound is threaded now, double buffered too; 8 bpp video support; better exiting code.
19 0.3: shorter library; improved joystick diagonal detection.
21 0.2: better code layout; public release.
31 #include <sys/ioctl.h>
32 #include <sys/soundcard.h>
35 #include "gp2xminilib.h"
37 extern void gp2x_sound_frame(void *blah, void *bufferg, int samples);
41 unsigned long gp2x_dev[4]={0,0,0,0}, gp2x_physvram[4];
42 unsigned short *gp2x_memregs, *gp2x_screen15, *gp2x_logvram15[2], gp2x_sound_buffer[4+(44100*2)*4]; //*2=stereo, *4=max buffers
43 volatile unsigned short gp2x_palette[512][2];
44 unsigned char *gp2x_screen8, *gp2x_logvram8[2];
45 pthread_t gp2x_sound_thread=0, gp2x_sound_thread_exit=0;
47 void gp2x_video_flip(void)
49 unsigned long address=gp2x_physvram[gp2x_physvram[3]];
51 gp2x_screen15=gp2x_logvram15[gp2x_physvram[3]^=1];
52 gp2x_screen8 =gp2x_logvram8 [gp2x_physvram[3] ];
54 gp2x_memregs[0x290E>>1]=(unsigned short)(address & 0xffff);
55 gp2x_memregs[0x2910>>1]=(unsigned short)(address >> 16);
56 gp2x_memregs[0x2912>>1]=(unsigned short)(address & 0xffff);
57 gp2x_memregs[0x2914>>1]=(unsigned short)(address >> 16);
60 void gp2x_video_setpalette(void)
62 gp2x_memregs[0x2958>>1]=0;
63 for(i=0; i<512; i++) gp2x_memregs[0x295A>>1]=gp2x_palette[i][0], gp2x_memregs[0x295A>>1]=gp2x_palette[i][1];
66 unsigned long gp2x_joystick_read(void)
68 unsigned long value=(gp2x_memregs[0x1198>>1] & 0x00FF);
70 if(value==0xFD) value=0xFA;
71 if(value==0xF7) value=0xEB;
72 if(value==0xDF) value=0xAF;
73 if(value==0x7F) value=0xBE;
75 return ~((gp2x_memregs[0x1184>>1] & 0xFF00) | value | (gp2x_memregs[0x1186>>1] << 16));
79 void *gp2x_sound_play(void *blah)
84 ts.tv_sec=0, ts.tv_nsec=gp2x_sound_buffer[2];
86 while(! gp2x_sound_thread_exit)
88 gp2x_sound_frame(blah, (void *)(&gp2x_sound_buffer[4+flip]), gp2x_sound_buffer[0]);
89 write(gp2x_dev[3], (void *)(&gp2x_sound_buffer[4+flip]), gp2x_sound_buffer[1]);
91 flip^=gp2x_sound_buffer[1];
93 //nanosleep(&ts, NULL);
100 void gp2x_deinit(void)
102 if(gp2x_sound_thread) { gp2x_sound_thread_exit=1; for(i=0;i<1000000;i++); }
104 gp2x_memregs[0x28DA>>1]=0x4AB;
105 gp2x_memregs[0x290C>>1]=640;
110 //close(gp2x_dev[3]);
114 void gp2x_init(int bpp, int rate, int bits, int stereo, int Hz)
116 struct fb_fix_screeninfo fixed_info;
118 if(!gp2x_dev[0]) gp2x_dev[0] = open("/dev/fb0", O_RDWR);
119 if(!gp2x_dev[1]) gp2x_dev[1] = open("/dev/fb1", O_RDWR);
120 if(!gp2x_dev[2]) gp2x_dev[2] = open("/dev/mem", O_RDWR);
121 //if(!gp2x_dev[3]) gp2x_dev[3] = open("/dev/dsp", O_WRONLY);
123 gp2x_memregs=(unsigned short *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, gp2x_dev[2], 0xc0000000);
125 if(!gp2x_sound_thread) { gp2x_memregs[0x0F16>>1] = 0x830a; sleep(1);
126 gp2x_memregs[0x0F58>>1] = 0x100c; sleep(1); }
128 ioctl (gp2x_dev[0], FBIOGET_FSCREENINFO, &fixed_info);
129 gp2x_screen15=gp2x_logvram15[0]=(unsigned short *)mmap(0, 320*240*2, PROT_WRITE, MAP_SHARED, gp2x_dev[0], 0);
130 gp2x_screen8=gp2x_logvram8[0]=(unsigned char *)gp2x_logvram15[0];
131 gp2x_physvram[0]=fixed_info.smem_start;
133 ioctl (gp2x_dev[1], FBIOGET_FSCREENINFO, &fixed_info);
134 gp2x_logvram15[1]=(unsigned short *)mmap(0, 320*240*2, PROT_WRITE, MAP_SHARED, gp2x_dev[1], 0);
135 gp2x_logvram8[1]=(unsigned char *)gp2x_logvram15[1];
136 gp2x_physvram[1]=fixed_info.smem_start;
138 gp2x_memregs[0x28DA>>1]=(((bpp+1)/8)<<9)|0xAB; /*8/15/16/24bpp...*/
139 gp2x_memregs[0x290C>>1]=320*((bpp+1)/8); /*line width in bytes*/
141 ioctl(gp2x_dev[3], SNDCTL_DSP_SPEED, &rate);
142 ioctl(gp2x_dev[3], SNDCTL_DSP_SETFMT, &bits);
143 ioctl(gp2x_dev[3], SNDCTL_DSP_STEREO, &stereo);
145 gp2x_sound_buffer[1]=(gp2x_sound_buffer[0]=(rate/Hz)) << (stereo + (bits==16));
146 gp2x_sound_buffer[2]=(1000000/Hz);
148 if(!gp2x_sound_thread) { gp2x_sound_thread = 1; //pthread_create( &gp2x_sound_thread, NULL, gp2x_sound_play, NULL);
149 atexit(gp2x_deinit); }
159 now supply your own function for 16 bits, stereo:
161 void gp2x_sound_frame(void *blah, void *bufferg, int samples)
163 signed short *buffer=(signed short *)bufferg;
166 *buffer++=0; //Left channel
167 *buffer++=0; //Right channel
173 void gp2x_sound_frame(void *blah, void *bufferg, int samples)
175 signed short *buffer=(signed short *)bufferg;
178 *buffer++=0; //Central channel
182 now the main program...
186 int main(int argc, char *argv[])
188 //this sets video to hicolor (16 bpp)
189 //it also sets sound to 44100,16bits,stereo and syncs audio to 50 Hz (PAL timing)
191 //Warning: GP2X does not support 8bit sound sampling! (at least within Linux)
193 gp2x_init(16,44100,16,1,50);
197 unsigned long pad=gp2x_joystick_read();
198 unsigned short color=gp2x_video_color15(255,255,255,0);
200 if(pad & GP2X_L) if(pad & GP2X_R) exit();
202 if(pad & GP2X_A) color=gp2x_color15(255,255,255,0); //white
203 else color=gp2x_color15(255,0,0,0); //red
205 gp2x_screen15[160+120*320]=color; //x=160, y=120
212 int main(int argc, char *argv[])
214 //this sets video to palette mode (8 bpp)
215 //it also sets sound to 11025,16bits,stereo and syncs audio to 60 Hz (NSTC timing)
217 //Warning: GP2X does not support 8bit sound sampling! (at least within Linux)
219 gp2x_init(8,11025,16,1,60);
221 gp2x_video_color8(0,0,0,0); //color #0 is black for us
222 gp2x_video_color8(1,255,255,255); //color #1 is white for us
223 gp2x_video_color8(2,255,0,0); //color #2 is red for us
224 gp2x_video_setpalette();
228 unsigned long pad=gp2x_joystick_read();
231 if(pad & GP2X_L) if(pad & GP2X_R) exit();
233 if(pad & GP2X_A) color=1; //white
236 gp2x_screen8[160+120*320]=color; //x=160, y=120