2009-05-21 17:48:31 +02:00
|
|
|
/* Parts used from cpuctrl */
|
|
|
|
/* cpuctrl for GP2X
|
|
|
|
Copyright (C) 2005 Hermes/PS2Reality
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2009-06-06 22:33:15 +02:00
|
|
|
#define _BSD_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
2009-05-21 17:48:31 +02:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/soundcard.h>
|
2009-06-06 22:33:15 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2009-05-21 17:48:31 +02:00
|
|
|
#include "../common.h"
|
|
|
|
#include "gp2x.h"
|
2009-05-30 21:47:25 +02:00
|
|
|
#include "warm.h"
|
2009-05-21 17:48:31 +02:00
|
|
|
|
2009-05-21 18:14:28 +02:00
|
|
|
u32 gp2x_audio_volume = 74/2;
|
2009-05-21 17:48:31 +02:00
|
|
|
u32 gpsp_gp2x_dev_audio = 0;
|
|
|
|
u32 gpsp_gp2x_dev = 0;
|
2009-05-30 21:47:25 +02:00
|
|
|
u32 gpsp_gp2x_gpiodev = 0;
|
2009-05-21 17:48:31 +02:00
|
|
|
|
2009-05-30 21:47:25 +02:00
|
|
|
static volatile u16 *gpsp_gp2x_memregs;
|
|
|
|
static volatile u32 *gpsp_gp2x_memregl;
|
2009-05-21 17:48:31 +02:00
|
|
|
|
2009-05-31 22:10:24 +02:00
|
|
|
#ifdef WIZ_BUILD
|
|
|
|
#include <linux/fb.h>
|
|
|
|
void *gpsp_gp2x_screen;
|
2009-06-08 00:57:08 +02:00
|
|
|
#define fb_buf_count 4
|
|
|
|
static u32 fb_paddr[fb_buf_count];
|
|
|
|
static void *fb_vaddr[fb_buf_count];
|
2009-05-31 22:10:24 +02:00
|
|
|
static u32 fb_work_buf;
|
2009-06-08 00:57:08 +02:00
|
|
|
static int fb_buf_use;
|
2009-05-31 22:10:24 +02:00
|
|
|
|
|
|
|
static void fb_video_init()
|
|
|
|
{
|
|
|
|
struct fb_fix_screeninfo fbfix;
|
|
|
|
int i, ret;
|
|
|
|
int fbdev;
|
|
|
|
|
|
|
|
fbdev = open("/dev/fb0", O_RDWR);
|
|
|
|
if (fbdev < 0) {
|
|
|
|
perror("can't open fbdev");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
perror("ioctl(fbdev) failed");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("framebuffer: \"%s\" @ %08lx\n", fbfix.id, fbfix.smem_start);
|
|
|
|
fb_paddr[0] = fbfix.smem_start;
|
|
|
|
close(fbdev);
|
|
|
|
|
|
|
|
fb_vaddr[0] = mmap(0, 320*240*2*fb_buf_count, PROT_READ|PROT_WRITE,
|
|
|
|
MAP_SHARED, gpsp_gp2x_dev, fb_paddr[0]);
|
|
|
|
if (fb_vaddr[0] == MAP_FAILED)
|
|
|
|
{
|
|
|
|
perror("mmap(fb_vaddr) failed");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
memset(fb_vaddr[0], 0, 320*240*2*fb_buf_count);
|
|
|
|
|
|
|
|
printf(" %p -> %08x\n", fb_vaddr[0], fb_paddr[0]);
|
|
|
|
for (i = 1; i < fb_buf_count; i++)
|
|
|
|
{
|
|
|
|
fb_paddr[i] = fb_paddr[i-1] + 320*240*2;
|
|
|
|
fb_vaddr[i] = (char *)fb_vaddr[i-1] + 320*240*2;
|
|
|
|
printf(" %p -> %08x\n", fb_vaddr[i], fb_paddr[i]);
|
|
|
|
}
|
|
|
|
fb_work_buf = 0;
|
|
|
|
fb_buf_use = fb_buf_count;
|
|
|
|
|
|
|
|
pollux_video_flip();
|
|
|
|
warm_change_cb_upper(WCB_C_BIT|WCB_B_BIT, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pollux_video_flip()
|
|
|
|
{
|
|
|
|
warm_cache_op_all(WOP_D_CLEAN);
|
|
|
|
gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[fb_work_buf];
|
|
|
|
gpsp_gp2x_memregl[0x4058>>2] |= 0x10;
|
|
|
|
fb_work_buf++;
|
|
|
|
if (fb_work_buf >= fb_buf_use)
|
|
|
|
fb_work_buf = 0;
|
|
|
|
gpsp_gp2x_screen = fb_vaddr[fb_work_buf];
|
|
|
|
}
|
|
|
|
|
|
|
|
void fb_use_buffers(int count)
|
|
|
|
{
|
|
|
|
if (count < 1)
|
|
|
|
count = 1;
|
|
|
|
else if (count > fb_buf_count)
|
|
|
|
count = fb_buf_count;
|
|
|
|
fb_buf_use = count;
|
2009-06-08 00:57:08 +02:00
|
|
|
memset(fb_vaddr[0], 0, 320*240*2*count);
|
2009-05-31 22:10:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fb_video_exit()
|
|
|
|
{
|
|
|
|
/* switch to default fb mem */
|
|
|
|
gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[0];
|
|
|
|
gpsp_gp2x_memregl[0x4058>>2] |= 0x10;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-05-30 21:47:25 +02:00
|
|
|
void gp2x_init()
|
2009-05-21 17:48:31 +02:00
|
|
|
{
|
2009-06-06 22:33:15 +02:00
|
|
|
FILE *f;
|
2009-05-21 17:48:31 +02:00
|
|
|
gpsp_gp2x_dev = open("/dev/mem", O_RDWR);
|
|
|
|
gpsp_gp2x_dev_audio = open("/dev/mixer", O_RDWR);
|
|
|
|
gpsp_gp2x_memregl =
|
|
|
|
(unsigned long *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED,
|
|
|
|
gpsp_gp2x_dev, 0xc0000000);
|
|
|
|
gpsp_gp2x_memregs = (unsigned short *)gpsp_gp2x_memregl;
|
2009-06-06 22:33:15 +02:00
|
|
|
warm_init();
|
2009-05-30 21:47:25 +02:00
|
|
|
#ifdef WIZ_BUILD
|
|
|
|
gpsp_gp2x_gpiodev = open("/dev/GPIO", O_RDONLY);
|
2009-05-31 22:10:24 +02:00
|
|
|
fb_video_init();
|
|
|
|
#endif
|
2009-05-21 17:48:31 +02:00
|
|
|
|
2009-06-06 22:33:15 +02:00
|
|
|
f = fopen("romdir.txt", "r");
|
|
|
|
if (f != NULL)
|
|
|
|
{
|
|
|
|
char buff[256];
|
|
|
|
char *s = fgets(buff, sizeof(buff) - 1, f);
|
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
int r = strlen(s);
|
|
|
|
while (r > 0 && isspace(buff[r-1]))
|
|
|
|
buff[--r] = 0;
|
|
|
|
chdir(buff);
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2009-05-21 17:48:31 +02:00
|
|
|
gp2x_sound_volume(1);
|
|
|
|
}
|
|
|
|
|
2009-06-06 22:33:15 +02:00
|
|
|
#include <errno.h>
|
2009-05-21 17:48:31 +02:00
|
|
|
void gp2x_quit()
|
|
|
|
{
|
2009-06-06 22:33:15 +02:00
|
|
|
char buff[256];
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
getcwd(buff, sizeof(buff));
|
|
|
|
chdir(main_path);
|
|
|
|
f = fopen("romdir.txt", "r+");
|
|
|
|
if (f != NULL)
|
|
|
|
{
|
|
|
|
fprintf(f, "%s", buff);
|
|
|
|
fclose(f);
|
|
|
|
truncate("romdir.txt", strlen(buff));
|
|
|
|
sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
warm_finish();
|
2009-05-30 21:47:25 +02:00
|
|
|
#ifdef WIZ_BUILD
|
|
|
|
close(gpsp_gp2x_gpiodev);
|
2009-05-31 22:10:24 +02:00
|
|
|
fb_video_exit();
|
2009-05-30 21:47:25 +02:00
|
|
|
#endif
|
2009-05-31 22:10:24 +02:00
|
|
|
munmap((void *)gpsp_gp2x_memregl, 0x10000);
|
|
|
|
close(gpsp_gp2x_dev_audio);
|
|
|
|
close(gpsp_gp2x_dev);
|
2009-05-21 18:14:28 +02:00
|
|
|
|
2009-06-06 22:33:15 +02:00
|
|
|
fcloseall();
|
2009-05-30 21:47:25 +02:00
|
|
|
//chdir("/usr/gp2x");
|
|
|
|
//execl("gp2xmenu", "gp2xmenu", NULL);
|
|
|
|
exit(0);
|
2009-05-21 17:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void gp2x_sound_volume(u32 volume_up)
|
|
|
|
{
|
|
|
|
u32 volume;
|
|
|
|
if((volume_up == 0) && (gp2x_audio_volume > 0))
|
|
|
|
gp2x_audio_volume--;
|
|
|
|
|
|
|
|
if((volume_up != 0) && (gp2x_audio_volume < 100))
|
|
|
|
gp2x_audio_volume++;
|
|
|
|
|
|
|
|
volume = (gp2x_audio_volume * 0x50) / 100;
|
|
|
|
volume = (gp2x_audio_volume << 8) | gp2x_audio_volume;
|
|
|
|
ioctl(gpsp_gp2x_dev_audio, SOUND_MIXER_WRITE_PCM, &volume);
|
|
|
|
}
|
|
|
|
|
2009-05-30 21:47:25 +02:00
|
|
|
u32 gpsp_gp2x_joystick_read(void)
|
|
|
|
{
|
|
|
|
#ifdef WIZ_BUILD
|
|
|
|
u32 value = 0;
|
|
|
|
read(gpsp_gp2x_gpiodev, &value, 4);
|
|
|
|
if(value & 0x02)
|
|
|
|
value |= 0x05;
|
|
|
|
if(value & 0x08)
|
|
|
|
value |= 0x14;
|
|
|
|
if(value & 0x20)
|
|
|
|
value |= 0x50;
|
|
|
|
if(value & 0x80)
|
|
|
|
value |= 0x41;
|
|
|
|
return value;
|
|
|
|
#else
|
|
|
|
u32 value = (gpsp_gp2x_memregs[0x1198 >> 1] & 0x00FF);
|
|
|
|
|
|
|
|
if(value == 0xFD)
|
|
|
|
value = 0xFA;
|
|
|
|
if(value == 0xF7)
|
|
|
|
value = 0xEB;
|
|
|
|
if(value == 0xDF)
|
|
|
|
value = 0xAF;
|
|
|
|
if(value == 0x7F)
|
|
|
|
value = 0xBE;
|
|
|
|
|
|
|
|
return ~((gpsp_gp2x_memregs[0x1184 >> 1] & 0xFF00) | value |
|
|
|
|
(gpsp_gp2x_memregs[0x1186 >> 1] << 16));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-06-06 22:33:15 +02:00
|
|
|
// Fout = (m * Fin) / (p * 2^s)
|
2009-05-30 21:47:25 +02:00
|
|
|
void set_FCLK(u32 MHZ)
|
|
|
|
{
|
2009-06-06 22:33:15 +02:00
|
|
|
u32 v;
|
|
|
|
u32 mdiv, pdiv, sdiv = 0;
|
|
|
|
#ifdef WIZ_BUILD
|
|
|
|
#define SYS_CLK_FREQ 27
|
|
|
|
// m = MDIV, p = PDIV, s = SDIV
|
|
|
|
pdiv = 9;
|
|
|
|
mdiv = (MHZ * pdiv) / SYS_CLK_FREQ;
|
|
|
|
mdiv &= 0x3ff;
|
|
|
|
v = (pdiv<<18) | (mdiv<<8) | sdiv;
|
|
|
|
|
|
|
|
gpsp_gp2x_memregl[0xf004>>2] = v;
|
|
|
|
gpsp_gp2x_memregl[0xf07c>>2] |= 0x8000;
|
|
|
|
#else
|
|
|
|
#define SYS_CLK_FREQ 7372800
|
|
|
|
// m = MDIV + 8, p = PDIV + 2, s = SDIV
|
|
|
|
pdiv = 3;
|
|
|
|
mdiv = (MHZ * pdiv * 1000000) / SYS_CLK_FREQ;
|
|
|
|
mdiv &= 0xff;
|
|
|
|
v = ((mdiv-8)<<8) | ((pdiv-2)<<2) | sdiv;
|
|
|
|
gpsp_gp2x_memregs[0x910>>1] = v;
|
2009-05-30 21:47:25 +02:00
|
|
|
#endif
|
2009-06-06 22:33:15 +02:00
|
|
|
}
|
2009-05-30 21:47:25 +02:00
|
|
|
|