/*
GP2X minimal library v0.3 by rlyeh, 2005.
+ GP2X video library with double buffering.
+ GP2X joystick library.
+ GP2X soundring buffer library.
Thanks to Squidge and Robster, for the help & previous work!
*/
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>
#include <linux/fb.h>
extern void sound_frame(void *blah, void *bufferg, int samples);
enum { GP2X_UP=0x1,GP2X_LEFT=0x4,GP2X_DOWN=0x10,GP2X_RIGHT=0x40,
GP2X_START=1<<8,GP2X_SELECT=1<<9,GP2X_L=1<<10,GP2X_R=1<<11,
GP2X_A=1<<12,GP2X_B=1<<13,GP2X_X=1<<14,GP2X_Y=1<<15,
GP2X_VOLM=1<<22,GP2X_VOLP=1<<23,GP2X_PUSH=1<<27,GP2X_BUFFSIZE=44100/50 };
unsigned long gp2x_dev[4], gp2x_physvram[4];
unsigned short *gp2x_memregs, *gp2x_screen, *gp2x_logvram[2], gp2x_sound_buffer[2+GP2X_BUFFSIZE*4];
void gp2x_video_flip(void)
{
unsigned long address=gp2x_physvram[gp2x_physvram[3]];
gp2x_screen=gp2x_logvram[gp2x_physvram[3]^=1];
gp2x_memregs[0x290E>>1]=(unsigned short)(address & 0xffff);
gp2x_memregs[0x2910>>1]=(unsigned short)(address >> 16);
gp2x_memregs[0x2912>>1]=(unsigned short)(address & 0xffff);
gp2x_memregs[0x2914>>1]=(unsigned short)(address >> 16);
}
unsigned long gp2x_joystick_read(void)
{
unsigned long value=(gp2x_memregs[0x1198>>1] & 0x00FF);
if(value==0xFD) value=0xFA;
if(value==0xF7) value=0xEB;
if(value==0F) value=0xAF;
if(value==0x7F) value=0xBE;
return ~((gp2x_memregs[0x1184>>1] & 0xFF00) | value | (gp2x_memregs[0x1186>>1] << 16));
}
void gp2x_sound_play(void)
{
if( !gp2x_dev[3] ) return;
sound_frame(NULL, &gp2x_sound_buffer[2], gp2x_sound_buffer[0]);
write( gp2x_dev[3], &gp2x_sound_buffer[2], gp2x_sound_buffer[1]);
}
void gp2x_init(int rate, int bits, int stereo)
{
struct fb_fix_screeninfo fixed_info;
gp2x_dev[0] = open("/dev/fb0", O_RDWR);
gp2x_dev[1] = open("/dev/fb1", O_RDWR);
gp2x_dev[2] = open("/dev/mem", O_RDWR);
gp2x_dev[3] = open("/dev/dsp", O_WRONLY);
gp2x_memregs=(unsigned short *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, gp2x_dev[2], 0xc0000000);
ioctl (gp2x_dev[0], FBIOGET_FSCREENINFO, &fixed_info);
gp2x_logvram[0]=(unsigned short *)mmap(0, 320*240*2, PROT_WRITE, MAP_SHARED, gp2x_dev[0], 0);
gp2x_physvram[0]=fixed_info.smem_start;
ioctl (gp2x_dev[1], FBIOGET_FSCREENINFO, &fixed_info);
gp2x_logvram[1]=(unsigned short *)mmap(0, 320*240*2, PROT_WRITE, MAP_SHARED, gp2x_dev[1], 0);
gp2x_physvram[1]=fixed_info.smem_start;
gp2x_screen=gp2x_logvram[gp2x_physvram[3]=0];
gp2x_sound_buffer[1]=(gp2x_sound_buffer[0]=GP2X_BUFFSIZE/(44100/rate)) << (stereo + (bits==16));
ioctl(gp2x_dev[3], SNDCTL_DSP_SETFMT, &bits);
ioctl(gp2x_dev[3], SNDCTL_DSP_STEREO, &stereo);
ioctl(gp2x_dev[3], SNDCTL_DSP_SPEED, &rate);
}
void gp2x_deinit(void)
{
close(gp2x_dev[0]);
close(gp2x_dev[1]);
close(gp2x_dev[2]);
close(gp2x_dev[3]);
chdir("/usr/gp2x");
execl("gp2xmenu",NULL);
}
/*
EXAMPLE
=======
now supply your own function for 16 bits, stereo:
void sound_frame(void *blah, void *bufferg, int samples)
{
signed short *buffer=(signed short *)bufferg;
while(samples--)
{
*buffer++=0; //Left channel
*buffer++=0; //Right channel
}
}
or for 8 bits, mono:
void sound_frame(void *blah, void *bufferg, int samples)
{
unsigned char *buffer=(unsigned char *)bufferg;
while(samples--)
{
*buffer++=0; //Central channel
}
}
etc... and done:
int main(int argc, char *argv[])
{
gp2x_init(44100,16,1);
while(1)
{
gp2x_screen[160+120*320]=(gp2x_joystick_read()&GP2X_X?0xFFFF:0); //pixel is RGB:565
gp2x_video_flip();
gp2x_sound_play();
}
gp2x_deinit();
}
*/
--------------------------------------------
http://retrodev.info
-- NK's Speaker Changed sound test. --
First, Speaker was stereo.
But, came out well for stereo sound system as basis establishment is and change a little establishment price by Mono.
Circuit of internal organs speaker need to be normally and change a little basis establishment price.
And right and left was normally at refreshing in sound player after first booting case of earphone.
However, volume adjustment defect be mistaken ...
Earphone will study solution method after test much more.
GP2X minimal library v0.3 by rlyeh, 2005.
+ GP2X video library with double buffering.
+ GP2X joystick library.
+ GP2X soundring buffer library.
Thanks to Squidge and Robster, for the help & previous work!
*/
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>
#include <linux/fb.h>
extern void sound_frame(void *blah, void *bufferg, int samples);
enum { GP2X_UP=0x1,GP2X_LEFT=0x4,GP2X_DOWN=0x10,GP2X_RIGHT=0x40,
GP2X_START=1<<8,GP2X_SELECT=1<<9,GP2X_L=1<<10,GP2X_R=1<<11,
GP2X_A=1<<12,GP2X_B=1<<13,GP2X_X=1<<14,GP2X_Y=1<<15,
GP2X_VOLM=1<<22,GP2X_VOLP=1<<23,GP2X_PUSH=1<<27,GP2X_BUFFSIZE=44100/50 };
unsigned long gp2x_dev[4], gp2x_physvram[4];
unsigned short *gp2x_memregs, *gp2x_screen, *gp2x_logvram[2], gp2x_sound_buffer[2+GP2X_BUFFSIZE*4];
void gp2x_video_flip(void)
{
unsigned long address=gp2x_physvram[gp2x_physvram[3]];
gp2x_screen=gp2x_logvram[gp2x_physvram[3]^=1];
gp2x_memregs[0x290E>>1]=(unsigned short)(address & 0xffff);
gp2x_memregs[0x2910>>1]=(unsigned short)(address >> 16);
gp2x_memregs[0x2912>>1]=(unsigned short)(address & 0xffff);
gp2x_memregs[0x2914>>1]=(unsigned short)(address >> 16);
}
unsigned long gp2x_joystick_read(void)
{
unsigned long value=(gp2x_memregs[0x1198>>1] & 0x00FF);
if(value==0xFD) value=0xFA;
if(value==0xF7) value=0xEB;
if(value==0F) value=0xAF;
if(value==0x7F) value=0xBE;
return ~((gp2x_memregs[0x1184>>1] & 0xFF00) | value | (gp2x_memregs[0x1186>>1] << 16));
}
void gp2x_sound_play(void)
{
if( !gp2x_dev[3] ) return;
sound_frame(NULL, &gp2x_sound_buffer[2], gp2x_sound_buffer[0]);
write( gp2x_dev[3], &gp2x_sound_buffer[2], gp2x_sound_buffer[1]);
}
void gp2x_init(int rate, int bits, int stereo)
{
struct fb_fix_screeninfo fixed_info;
gp2x_dev[0] = open("/dev/fb0", O_RDWR);
gp2x_dev[1] = open("/dev/fb1", O_RDWR);
gp2x_dev[2] = open("/dev/mem", O_RDWR);
gp2x_dev[3] = open("/dev/dsp", O_WRONLY);
gp2x_memregs=(unsigned short *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, gp2x_dev[2], 0xc0000000);
ioctl (gp2x_dev[0], FBIOGET_FSCREENINFO, &fixed_info);
gp2x_logvram[0]=(unsigned short *)mmap(0, 320*240*2, PROT_WRITE, MAP_SHARED, gp2x_dev[0], 0);
gp2x_physvram[0]=fixed_info.smem_start;
ioctl (gp2x_dev[1], FBIOGET_FSCREENINFO, &fixed_info);
gp2x_logvram[1]=(unsigned short *)mmap(0, 320*240*2, PROT_WRITE, MAP_SHARED, gp2x_dev[1], 0);
gp2x_physvram[1]=fixed_info.smem_start;
gp2x_screen=gp2x_logvram[gp2x_physvram[3]=0];
gp2x_sound_buffer[1]=(gp2x_sound_buffer[0]=GP2X_BUFFSIZE/(44100/rate)) << (stereo + (bits==16));
ioctl(gp2x_dev[3], SNDCTL_DSP_SETFMT, &bits);
ioctl(gp2x_dev[3], SNDCTL_DSP_STEREO, &stereo);
ioctl(gp2x_dev[3], SNDCTL_DSP_SPEED, &rate);
}
void gp2x_deinit(void)
{
close(gp2x_dev[0]);
close(gp2x_dev[1]);
close(gp2x_dev[2]);
close(gp2x_dev[3]);
chdir("/usr/gp2x");
execl("gp2xmenu",NULL);
}
/*
EXAMPLE
=======
now supply your own function for 16 bits, stereo:
void sound_frame(void *blah, void *bufferg, int samples)
{
signed short *buffer=(signed short *)bufferg;
while(samples--)
{
*buffer++=0; //Left channel
*buffer++=0; //Right channel
}
}
or for 8 bits, mono:
void sound_frame(void *blah, void *bufferg, int samples)
{
unsigned char *buffer=(unsigned char *)bufferg;
while(samples--)
{
*buffer++=0; //Central channel
}
}
etc... and done:
int main(int argc, char *argv[])
{
gp2x_init(44100,16,1);
while(1)
{
gp2x_screen[160+120*320]=(gp2x_joystick_read()&GP2X_X?0xFFFF:0); //pixel is RGB:565
gp2x_video_flip();
gp2x_sound_play();
}
gp2x_deinit();
}
*/
--------------------------------------------
http://retrodev.info
-- NK's Speaker Changed sound test. --
First, Speaker was stereo.
But, came out well for stereo sound system as basis establishment is and change a little establishment price by Mono.
Circuit of internal organs speaker need to be normally and change a little basis establishment price.
And right and left was normally at refreshing in sound player after first booting case of earphone.
However, volume adjustment defect be mistaken ...
Earphone will study solution method after test much more.