tweaks from 2008 (gpsp09-2xb_3)
[gpsp.git] / gp2x / gp2xminilib.c
1
2 /*
3   GP2X minimal library v0.5 by rlyeh, 2005.
4  
5   + GP2X video library with double buffering.
6   + GP2X soundring buffer library with double buffering.
7   + GP2X joystick library.
8  
9   Thanks to Squidge, Robster, snaff and NK, for the help & previous work! :-)
10  
11  
12   What's new
13   ==========
14  
15   0.5: patched sound for real stereo (using NK's solution); better init code.
16  
17   0.4: lots of cleanups; sound is threaded now, double buffered too; 8 bpp video support; better exiting code.
18  
19   0.3: shorter library; improved joystick diagonal detection.
20  
21   0.2: better code layout; public release.
22  
23   0.1: beta release
24 */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <sys/mman.h>
31 #include <sys/ioctl.h>
32 #include <sys/soundcard.h>
33 #include <linux/fb.h>
34 #include <pthread.h>
35 #include "gp2xminilib.h"
36
37 extern void gp2x_sound_frame(void *blah, void *bufferg, int samples);
38  
39
40
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;
46
47 void gp2x_video_flip(void)
48 {
49   unsigned long address=gp2x_physvram[gp2x_physvram[3]];
50  
51   gp2x_screen15=gp2x_logvram15[gp2x_physvram[3]^=1]; 
52   gp2x_screen8 =gp2x_logvram8 [gp2x_physvram[3]   ]; 
53  
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);
58 }
59  
60 void gp2x_video_setpalette(void)
61 {int i;
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];
64 }
65  
66 unsigned long gp2x_joystick_read(void)
67 {
68   unsigned long value=(gp2x_memregs[0x1198>>1] & 0x00FF);
69  
70   if(value==0xFD) value=0xFA;
71   if(value==0xF7) value=0xEB;
72   if(value==0xDF) value=0xAF;
73   if(value==0x7F) value=0xBE;
74  
75   return ~((gp2x_memregs[0x1184>>1] & 0xFF00) | value | (gp2x_memregs[0x1186>>1] << 16));
76 }
77  
78 #if 0
79 void *gp2x_sound_play(void *blah)
80 {
81   struct timespec ts;
82   int flip=0;
83  
84   ts.tv_sec=0, ts.tv_nsec=gp2x_sound_buffer[2];
85  
86   while(! gp2x_sound_thread_exit)
87   {
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]);
90  
91    flip^=gp2x_sound_buffer[1];
92  
93    //nanosleep(&ts, NULL);
94   }
95  
96   return NULL;
97 }
98 #endif
99
100 void gp2x_deinit(void)
101 {int i;
102   if(gp2x_sound_thread) { gp2x_sound_thread_exit=1; for(i=0;i<1000000;i++); }
103  
104   gp2x_memregs[0x28DA>>1]=0x4AB; 
105   gp2x_memregs[0x290C>>1]=640;   
106  
107   close(gp2x_dev[0]);
108   close(gp2x_dev[1]);
109   close(gp2x_dev[2]);
110   //close(gp2x_dev[3]);
111   //fcloseall();
112 }
113  
114 void gp2x_init(int bpp, int rate, int bits, int stereo, int Hz)
115 {
116   struct fb_fix_screeninfo fixed_info;
117  
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);
122  
123   gp2x_memregs=(unsigned short *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, gp2x_dev[2], 0xc0000000);
124  
125   if(!gp2x_sound_thread) { gp2x_memregs[0x0F16>>1] = 0x830a; sleep(1); 
126                            gp2x_memregs[0x0F58>>1] = 0x100c; sleep(1); }
127  
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;
132  
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;
137  
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*/
140  
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);
144  
145   gp2x_sound_buffer[1]=(gp2x_sound_buffer[0]=(rate/Hz)) << (stereo + (bits==16));
146   gp2x_sound_buffer[2]=(1000000/Hz);
147  
148   if(!gp2x_sound_thread) { gp2x_sound_thread = 1; //pthread_create( &gp2x_sound_thread, NULL, gp2x_sound_play, NULL);
149                            atexit(gp2x_deinit); }
150 }
151  
152  
153  
154 /*
155  
156 EXAMPLE
157 =======
158  
159    now supply your own function for 16 bits, stereo:
160  
161    void gp2x_sound_frame(void *blah, void *bufferg, int samples)
162    {
163      signed short *buffer=(signed short *)bufferg;
164      while(samples--)
165      {
166        *buffer++=0; //Left channel
167        *buffer++=0; //Right channel
168      }
169    }
170  
171    or 16 bits mono:
172  
173    void gp2x_sound_frame(void *blah, void *bufferg, int samples)
174    {
175      signed short *buffer=(signed short *)bufferg;
176      while(samples--)
177      {
178        *buffer++=0; //Central channel
179      }
180    }
181  
182   now the main program...
183  
184   hicolor example:
185  
186   int main(int argc, char *argv[])
187   {
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)
190  
191    //Warning: GP2X does not support 8bit sound sampling! (at least within Linux)
192  
193    gp2x_init(16,44100,16,1,50);
194  
195    while(1)
196    {
197     unsigned long  pad=gp2x_joystick_read();
198     unsigned short color=gp2x_video_color15(255,255,255,0);
199  
200     if(pad & GP2X_L) if(pad & GP2X_R) exit();           
201  
202     if(pad & GP2X_A) color=gp2x_color15(255,255,255,0);  //white
203      else            color=gp2x_color15(255,0,0,0);      //red
204  
205     gp2x_screen15[160+120*320]=color;                    //x=160, y=120
206     gp2x_video_flip();
207    }
208   }
209  
210   palettized example:
211  
212   int main(int argc, char *argv[])
213   {
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)
216  
217    //Warning: GP2X does not support 8bit sound sampling! (at least within Linux)
218  
219    gp2x_init(8,11025,16,1,60);
220  
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();
225  
226    while(1)
227    {
228     unsigned long pad=gp2x_joystick_read();
229     unsigned char color;
230  
231     if(pad & GP2X_L) if(pad & GP2X_R) exit();           
232  
233     if(pad & GP2X_A) color=1;  //white
234      else            color=2;  //red
235  
236     gp2x_screen8[160+120*320]=color;                      //x=160, y=120
237     gp2x_video_flip();
238    }
239   }
240  
241 */
242