--- /dev/null
+export CROSS = arm-none-linux-gnueabi-\r
+\r
+# settings\r
+#mz80 = 1\r
+#debug_cyclone = 1\r
+asm_memory = 1\r
+asm_render = 1\r
+asm_ym2612 = 1\r
+asm_misc = 1\r
+asm_cdpico = 1\r
+asm_cdmemory = 1\r
+amalgamate = 0\r
+#profile = 1\r
+#use_musashi = 1\r
+#up = 1\r
+\r
+\r
+ifeq "$(debug_cyclone)" "1"\r
+use_cyclone = 1\r
+use_musashi = 1\r
+endif\r
+ifeq "$(use_musashi)" "1"\r
+asm_cdpico = 0\r
+asm_memory = 0\r
+asm_cdmemory = 0\r
+else\r
+use_cyclone = 1\r
+endif\r
+\r
+DEFINC = -I../.. -I. -DARM -DPANDORA\r
+COPT_COMMON = -static -Wall -Winline\r
+ifeq ($(DEBUG),)\r
+COPT_COMMON += -O3 -ftracer -fstrength-reduce -fomit-frame-pointer -fstrict-aliasing -ffast-math\r
+else\r
+COPT_COMMON += -ggdb\r
+endif\r
+ifeq "$(profile)" "1"\r
+COPT_COMMON += -fprofile-generate\r
+endif\r
+ifeq "$(profile)" "2"\r
+COPT_COMMON += -fprofile-use\r
+endif\r
+COPT = $(COPT_COMMON)\r
+ASOPT =\r
+GCC = $(CROSS)gcc\r
+STRIP = $(CROSS)strip\r
+AS = $(CROSS)as\r
+LD = $(CROSS)ld\r
+OBJCOPY = $(CROSS)objcopy\r
+\r
+# frontend\r
+OBJS += pandora.o main.o platform/gp2x/menu.o platform/gp2x/usbjoy.o platform/gp2x/emu.o\r
+\r
+# common\r
+OBJS += platform/common/emu.o platform/common/menu.o platform/common/fonts.o platform/common/config.o \\r
+ platform/common/arm_utils.o platform/common/mp3_helix.o # platform/common/readpng.o\r
+\r
+# Pico\r
+ifeq "$(amalgamate)" "1"\r
+OBJS += ../../PicoAll.o\r
+else\r
+OBJS += Pico/Area.o Pico/Cart.o Pico/Memory.o Pico/Misc.o Pico/Pico.o Pico/Sek.o \\r
+ Pico/VideoPort.o Pico/Draw2.o Pico/Draw.o Pico/Patch.o Pico/Debug.o\r
+# Pico - CD\r
+OBJS += Pico/cd/Pico.o Pico/cd/Memory.o Pico/cd/Sek.o Pico/cd/LC89510.o \\r
+ Pico/cd/cd_sys.o Pico/cd/cd_file.o Pico/cd/cue.o Pico/cd/gfx_cd.o \\r
+ Pico/cd/Area.o Pico/cd/Misc.o Pico/cd/pcm.o Pico/cd/buffering.o\r
+endif\r
+# Pico - Pico\r
+OBJS += Pico/Pico/Pico.o Pico/Pico/Memory.o Pico/Pico/xpcm.o\r
+# Pico - carthw\r
+OBJS += Pico/carthw/carthw.o Pico/carthw/svp/svp.o Pico/carthw/svp/Memory.o \\r
+ Pico/carthw/svp/ssp16.o Pico/carthw/svp/compiler.o Pico/carthw/svp/stub_arm.o\r
+\r
+# asm stuff\r
+ifeq "$(asm_render)" "1"\r
+DEFINC += -D_ASM_DRAW_C\r
+OBJS += Pico/draw_asm.o Pico/draw2_asm.o\r
+endif\r
+ifeq "$(asm_memory)" "1"\r
+DEFINC += -D_ASM_MEMORY_C\r
+OBJS += Pico/memory_asm.o\r
+endif\r
+ifeq "$(asm_ym2612)" "1"\r
+DEFINC += -D_ASM_YM2612_C\r
+OBJS += Pico/sound/ym2612_asm.o\r
+endif\r
+ifeq "$(asm_misc)" "1"\r
+DEFINC += -D_ASM_MISC_C\r
+OBJS += Pico/misc_asm.o\r
+OBJS += Pico/cd/misc_asm.o\r
+endif\r
+ifeq "$(asm_cdpico)" "1"\r
+DEFINC += -D_ASM_CD_PICO_C\r
+OBJS += Pico/cd/pico_asm.o\r
+endif\r
+ifeq "$(asm_cdmemory)" "1"\r
+DEFINC += -D_ASM_CD_MEMORY_C\r
+OBJS += Pico/cd/memory_asm.o\r
+endif\r
+\r
+# Pico - sound\r
+ifneq "$(amalgamate)" "1"\r
+OBJS += Pico/sound/sound.o\r
+endif\r
+OBJS += Pico/sound/mix_asm.o\r
+OBJS += Pico/sound/sn76496.o Pico/sound/ym2612.o\r
+# zlib\r
+OBJS += zlib/gzio.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o zlib/trees.o \\r
+ zlib/deflate.o zlib/crc32.o zlib/adler32.o zlib/zutil.o zlib/compress.o\r
+# unzip\r
+OBJS += unzip/unzip.o unzip/unzip_stream.o\r
+# debug\r
+ifeq "$(debug_cyclone)" "1"\r
+OBJS += Pico/DebugCPU.o cpu/musashi/m68kdasm.o\r
+endif\r
+# CPU cores\r
+ifeq "$(use_musashi)" "1"\r
+DEFINC += -DEMU_M68K\r
+OBJS += cpu/musashi/m68kops.o cpu/musashi/m68kcpu.o\r
+endif\r
+ifeq "$(use_cyclone)" "1"\r
+DEFINC += -DEMU_C68K\r
+OBJS += cpu/Cyclone/proj/Cyclone.o cpu/Cyclone/tools/idle.o\r
+endif\r
+# drz80/mz80\r
+ifeq "$(mz80)" "1"\r
+DEFINC += -D_USE_MZ80\r
+OBJS += cpu/mz80/mz80.o\r
+else\r
+DEFINC += -D_USE_DRZ80\r
+OBJS += cpu/DrZ80/drz80.o\r
+endif\r
+\r
+vpath %.c = ../..\r
+vpath %.s = ../..\r
+vpath %.S = ../..\r
+DIRS = platform platform/gp2x platform/common Pico Pico/cd Pico/Pico Pico/sound Pico/carthw/svp \\r
+ zlib unzip cpu cpu/musashi cpu/Cyclone/proj cpu/Cyclone/tools cpu/mz80 cpu/DrZ80\r
+\r
+\r
+all: mkdirs PicoDrive\r
+\r
+PicoDrive : $(OBJS) ../common/helix/helix_mp3.a\r
+ @echo ">>>" $@\r
+ $(GCC) -o $@ $(COPT) $^ -lm -Wl,-Map=PicoDrive.map # -lpng\r
+ifeq ($(DEBUG),)\r
+ $(STRIP) $@\r
+endif\r
+\r
+\r
+# cleanup\r
+clean: tidy\r
+ $(RM) PicoDrive\r
+tidy:\r
+ $(RM) $(OBJS)\r
+# rm -rf $(DIRS) # don't clean, gcda may be there\r
+# @make -C ../../cpu/Cyclone/proj -f Makefile.linux clean\r
+\r
+\r
+clean_prof:\r
+ find ../.. -name '*.gcno' -delete\r
+ find ../.. -name '*.gcda' -delete\r
+\r
+\r
+mkdirs:\r
+ mkdir -p $(DIRS)\r
+\r
+.c.o:\r
+ @echo ">>>" $<\r
+ $(GCC) $(COPT) $(DEFINC) -c $< -o $@\r
+.s.o:\r
+ @echo ">>>" $<\r
+ $(GCC) $(COPT) $(DEFINC) -c $< -o $@\r
+.S.o:\r
+ @echo ">>>" $<\r
+ $(GCC) $(COPT) $(DEFINC) -c $< -o $@\r
+\r
+Pico/carthw/svp/compiler.o : ../../Pico/carthw/svp/ssp16.o ../../Pico/carthw/svp/gen_arm.c\r
+\r
+Pico/draw_asm.o : ../../Pico/Draw.s\r
+ @echo ">>>" $<\r
+ $(AS) $(ASOPT) $< -o $@\r
+Pico/draw2_asm.o : ../../Pico/Draw2.s\r
+ @echo ">>>" $<\r
+ $(AS) $(ASOPT) $< -o $@\r
+Pico/memory_asm.o : ../../Pico/Memory.s\r
+ @echo ">>>" $<\r
+ $(AS) $(ASOPT) $< -o $@\r
+Pico/sound/ym2612_asm.o : ../../Pico/sound/ym2612.s\r
+ @echo ">>>" $<\r
+ $(AS) $(ASOPT) $< -o $@\r
+Pico/sound/mix_asm.o : ../../Pico/sound/mix.s\r
+ @echo ">>>" $<\r
+ $(AS) $(ASOPT) $< -o $@\r
+Pico/misc_asm.o : ../../Pico/Misc.s\r
+ @echo ">>>" $<\r
+ $(AS) $(ASOPT) $< -o $@\r
+Pico/cd/pico_asm.o : ../../Pico/cd/Pico.s\r
+ @echo ">>>" $<\r
+ $(AS) $(ASOPT) $< -o $@\r
+Pico/cd/memory_asm.o : ../../Pico/cd/Memory.s\r
+ @echo ">>>" $<\r
+ $(AS) $(ASOPT) $< -o $@\r
+Pico/cd/misc_asm.o : ../../Pico/cd/Misc.s\r
+ @echo ">>>" $<\r
+ $(AS) $(ASOPT) $< -o $@\r
+\r
+# build Cyclone\r
+../../cpu/Cyclone/proj/Cyclone.s :\r
+ @echo building Cyclone...\r
+ @make -C ../../cpu/Cyclone/proj CONFIG_FILE=config_pico.h\r
+\r
+../../cpu/musashi/m68kops.c :\r
+ @make -C ../../cpu/musashi\r
+\r
+Pico/Pico.o Pico/cd/Pico.o: ../../Pico/PicoFrameHints.c ../../Pico/PicoInt.h\r
+Pico/Memory.o Pico/cd/Memory.o : ../../Pico/MemoryCmn.c ../../Pico/PicoInt.h\r
+\r
+# build helix libs\r
+../common/helix/helix_mp3.a:\r
+ make -C ../common/helix\r
+\r
+readme.txt: ../../tools/textfilter ../base_readme.txt\r
+ ../../tools/textfilter ../base_readme.txt $@ GP2X\r
+\r
+../../tools/textfilter: ../../tools/textfilter.c\r
+ make -C ../../tools/ textfilter\r
+\r
+\r
+# ----------- release -----------\r
+ifneq ($(findstring rel,$(MAKECMDGOALS)),)\r
+$(error TODO)\r
+ifeq ($(VER),)\r
+$(error need VER)\r
+endif\r
+endif\r
+\r
--- /dev/null
+/**\r
+ * All this is mostly based on rlyeh's minimal library.\r
+ * Copied here to review all his code and understand what's going on.\r
+**/\r
+\r
+/*\r
+\r
+ GP2X minimal library v0.A by rlyeh, (c) 2005. emulnation.info@rlyeh (swap it!)\r
+\r
+ Thanks to Squidge, Robster, snaff, Reesy and NK, for the help & previous work! :-)\r
+\r
+ License\r
+ =======\r
+\r
+ Free for non-commercial projects (it would be nice receiving a mail from you).\r
+ Other cases, ask me first.\r
+\r
+ GamePark Holdings is not allowed to use this library and/or use parts from it.\r
+\r
+*/\r
+\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <stdarg.h>\r
+#include <string.h>\r
+#include <unistd.h>\r
+#include <sys/mman.h>\r
+#include <sys/types.h>\r
+#include <sys/stat.h>\r
+#include <sys/ioctl.h>\r
+#include <sys/soundcard.h>\r
+#include <fcntl.h>\r
+#include <errno.h>\r
+\r
+#include "../gp2x/gp2x.h"\r
+#include "../gp2x/usbjoy.h"\r
+#include "../common/arm_utils.h"\r
+\r
+volatile unsigned short *gp2x_memregs;\r
+//static\r
+volatile unsigned long *gp2x_memregl;\r
+static void *gp2x_screens[4];\r
+static int screensel = 0;\r
+//static\r
+int memdev = 0;\r
+static int sounddev = -1, mixerdev = -1, touchdev = -1;\r
+static int touchcal[7] = { 6203, 0, -1501397, 0, -4200, 16132680, 65536 };\r
+\r
+void *gp2x_screen;\r
+\r
+#define FRAMEBUFF_WHOLESIZE (0x30000*4) // 320*240*2 + some more\r
+#define FRAMEBUFF_ADDR0 (0x4000000-FRAMEBUFF_WHOLESIZE)\r
+#define FRAMEBUFF_ADDR1 (FRAMEBUFF_ADDR0+0x30000)\r
+#define FRAMEBUFF_ADDR2 (FRAMEBUFF_ADDR1+0x30000)\r
+#define FRAMEBUFF_ADDR3 (FRAMEBUFF_ADDR2+0x30000)\r
+\r
+static const int gp2x_screenaddrs[4] = { FRAMEBUFF_ADDR0, FRAMEBUFF_ADDR1, FRAMEBUFF_ADDR2, FRAMEBUFF_ADDR3 };\r
+static int gp2x_screenaddrs_use[4];\r
+static unsigned short gp2x_screenaddr_old[4];\r
+\r
+\r
+/* video stuff */\r
+void gp2x_video_flip(void)\r
+{\r
+ unsigned short lsw = (unsigned short) gp2x_screenaddrs_use[screensel&3];\r
+ unsigned short msw = (unsigned short)(gp2x_screenaddrs_use[screensel&3] >> 16);\r
+\r
+ gp2x_memregs[0x2910>>1] = msw;\r
+ gp2x_memregs[0x2914>>1] = msw;\r
+ gp2x_memregs[0x290E>>1] = lsw;\r
+ gp2x_memregs[0x2912>>1] = lsw;\r
+\r
+ // jump to other buffer:\r
+ gp2x_screen = gp2x_screens[++screensel&3];\r
+}\r
+\r
+/* doulblebuffered flip */\r
+void gp2x_video_flip2(void)\r
+{\r
+ unsigned short msw = (unsigned short)(gp2x_screenaddrs_use[screensel&1] >> 16);\r
+\r
+ gp2x_memregs[0x2910>>1] = msw;\r
+ gp2x_memregs[0x2914>>1] = msw;\r
+ gp2x_memregs[0x290E>>1] = 0;\r
+ gp2x_memregs[0x2912>>1] = 0;\r
+\r
+ // jump to other buffer:\r
+ gp2x_screen = gp2x_screens[++screensel&1];\r
+}\r
+\r
+\r
+void gp2x_video_changemode2(int bpp)\r
+{\r
+ gp2x_memregs[0x28DA>>1]=(((bpp+1)/8)<<9)|0xAB; /*8/15/16/24bpp...*/\r
+ gp2x_memregs[0x290C>>1]=320*((bpp+1)/8); /*line width in bytes*/\r
+}\r
+\r
+\r
+void gp2x_video_changemode(int bpp)\r
+{\r
+ gp2x_video_changemode2(bpp);\r
+\r
+ gp2x_memset_all_buffers(0, 0, 320*240*2);\r
+ gp2x_video_flip();\r
+}\r
+\r
+\r
+void gp2x_video_setpalette(int *pal, int len)\r
+{\r
+ unsigned short *g=(unsigned short *)pal;\r
+ volatile unsigned short *memreg = &gp2x_memregs[0x295A>>1];\r
+ gp2x_memregs[0x2958>>1] = 0;\r
+\r
+ len *= 2;\r
+ while(len--) *memreg=*g++;\r
+}\r
+\r
+\r
+// TV Compatible function //\r
+void gp2x_video_RGB_setscaling(int ln_offs, int W, int H)\r
+{\r
+ float escalaw, escalah;\r
+ int bpp = (gp2x_memregs[0x28DA>>1]>>9)&0x3;\r
+ unsigned short scalw;\r
+\r
+ // set offset\r
+ gp2x_screenaddrs_use[0] = gp2x_screenaddrs[0] + ln_offs * 320 * bpp;\r
+ gp2x_screenaddrs_use[1] = gp2x_screenaddrs[1] + ln_offs * 320 * bpp;\r
+ gp2x_screenaddrs_use[2] = gp2x_screenaddrs[2] + ln_offs * 320 * bpp;\r
+ gp2x_screenaddrs_use[3] = gp2x_screenaddrs[3] + ln_offs * 320 * bpp;\r
+\r
+ escalaw = 1024.0; // RGB Horiz LCD\r
+ escalah = 320.0; // RGB Vert LCD\r
+\r
+ if(gp2x_memregs[0x2800>>1]&0x100) //TV-Out\r
+ {\r
+ escalaw=489.0; // RGB Horiz TV (PAL, NTSC)\r
+ if (gp2x_memregs[0x2818>>1] == 287) //PAL\r
+ escalah=274.0; // RGB Vert TV PAL\r
+ else if (gp2x_memregs[0x2818>>1] == 239) //NTSC\r
+ escalah=331.0; // RGB Vert TV NTSC\r
+ }\r
+\r
+ // scale horizontal\r
+ scalw = (unsigned short)((float)escalaw *(W/320.0));\r
+ /* if there is no horizontal scaling, vertical doesn't work. Here is a nasty wrokaround... */\r
+ if (H != 240 && W == 320) scalw--;\r
+ gp2x_memregs[0x2906>>1]=scalw;\r
+ // scale vertical\r
+ gp2x_memregl[0x2908>>2]=(unsigned long)((float)escalah *bpp *(H/240.0));\r
+}\r
+\r
+\r
+void gp2x_video_wait_vsync(void)\r
+{\r
+ unsigned short v = gp2x_memregs[0x1182>>1];\r
+ while (!((v ^ gp2x_memregs[0x1182>>1]) & 0x10)) spend_cycles(1024);\r
+}\r
+\r
+\r
+void gp2x_video_flush_cache(void)\r
+{\r
+ // since we are using the mmu hack, we must flush the cache first\r
+ // (the params are most likely wrong, but they seem to work somehow)\r
+ //flushcache(addr, addr + 320*240*2, 0);\r
+ flushcache(gp2x_screen, (char *)gp2x_screen + 320*240*2, 0);\r
+}\r
+\r
+\r
+void gp2x_memcpy_buffers(int buffers, void *data, int offset, int len)\r
+{\r
+ char *dst;\r
+ if (buffers & (1<<0)) { dst = (char *)gp2x_screens[0] + offset; if (dst != data) memcpy(dst, data, len); }\r
+ if (buffers & (1<<1)) { dst = (char *)gp2x_screens[1] + offset; if (dst != data) memcpy(dst, data, len); }\r
+ if (buffers & (1<<2)) { dst = (char *)gp2x_screens[2] + offset; if (dst != data) memcpy(dst, data, len); }\r
+ if (buffers & (1<<3)) { dst = (char *)gp2x_screens[3] + offset; if (dst != data) memcpy(dst, data, len); }\r
+}\r
+\r
+\r
+void gp2x_memcpy_all_buffers(void *data, int offset, int len)\r
+{\r
+ gp2x_memcpy_buffers(0xf, data, offset, len);\r
+}\r
+\r
+\r
+void gp2x_memset_all_buffers(int offset, int byte, int len)\r
+{\r
+ memset((char *)gp2x_screens[0] + offset, byte, len);\r
+ memset((char *)gp2x_screens[1] + offset, byte, len);\r
+ memset((char *)gp2x_screens[2] + offset, byte, len);\r
+ memset((char *)gp2x_screens[3] + offset, byte, len);\r
+}\r
+\r
+\r
+void gp2x_pd_clone_buffer2(void)\r
+{\r
+ memcpy(gp2x_screen, gp2x_screens[2], 320*240*2);\r
+}\r
+\r
+\r
+unsigned long gp2x_joystick_read(int allow_usb_joy)\r
+{\r
+ int i;\r
+ unsigned long value=(gp2x_memregs[0x1198>>1] & 0x00FF); // GPIO M\r
+ if(value==0xFD) value=0xFA;\r
+ if(value==0xF7) value=0xEB;\r
+ if(value==0xDF) value=0xAF;\r
+ if(value==0x7F) value=0xBE;\r
+ value = ~((gp2x_memregs[0x1184>>1] & 0xFF00) | value | (gp2x_memregs[0x1186>>1] << 16)); // C D\r
+\r
+ if (allow_usb_joy && num_of_joys > 0) {\r
+ // check the usb joy as well..\r
+ gp2x_usbjoy_update();\r
+ for (i = 0; i < num_of_joys; i++)\r
+ value |= gp2x_usbjoy_check(i);\r
+ }\r
+\r
+ return value;\r
+}\r
+\r
+typedef struct ucb1x00_ts_event\r
+{\r
+ unsigned short pressure;\r
+ unsigned short x;\r
+ unsigned short y;\r
+ unsigned short pad;\r
+ struct timeval stamp;\r
+} UCB1X00_TS_EVENT;\r
+\r
+int gp2x_touchpad_read(int *x, int *y)\r
+{\r
+ UCB1X00_TS_EVENT event;\r
+ static int zero_seen = 0;\r
+ int retval;\r
+\r
+ if (touchdev < 0) return -1;\r
+\r
+ retval = read(touchdev, &event, sizeof(event));\r
+ if (retval <= 0) {\r
+ printf("touch read failed %i %i\n", retval, errno);\r
+ return -1;\r
+ }\r
+ // this is to ignore the messed-up 4.1.x driver\r
+ if (event.pressure == 0) zero_seen = 1;\r
+\r
+ if (x) *x = (event.x * touchcal[0] + touchcal[2]) >> 16;\r
+ if (y) *y = (event.y * touchcal[4] + touchcal[5]) >> 16;\r
+ // printf("read %i %i %i\n", event.pressure, *x, *y);\r
+\r
+ return zero_seen ? event.pressure : 0;\r
+}\r
+\r
+\r
+static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0;\r
+\r
+void gp2x_start_sound(int rate, int bits, int stereo)\r
+{\r
+ int frag = 0, bsize, buffers;\r
+\r
+ // if no settings change, we don't need to do anything\r
+ if (rate == s_oldrate && s_oldbits == bits && s_oldstereo == stereo) return;\r
+\r
+ if (sounddev > 0) close(sounddev);\r
+ sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);\r
+ if (sounddev == -1)\r
+ printf("open(\"/dev/dsp\") failed with %i\n", errno);\r
+\r
+ ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);\r
+ ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);\r
+ ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);\r
+ // calculate buffer size\r
+ buffers = 16;\r
+ bsize = rate / 32;\r
+ if (rate > 22050) { bsize*=4; buffers*=2; } // 44k mode seems to be very demanding\r
+ while ((bsize>>=1)) frag++;\r
+ frag |= buffers<<16; // 16 buffers\r
+ ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);\r
+ usleep(192*1024);\r
+\r
+ printf("gp2x_set_sound: %i/%ibit/%s, %i buffers of %i bytes\n",\r
+ rate, bits, stereo?"stereo":"mono", frag>>16, 1<<(frag&0xffff));\r
+\r
+ s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo;\r
+}\r
+\r
+\r
+void gp2x_sound_write(void *buff, int len)\r
+{\r
+ write(sounddev, buff, len);\r
+}\r
+\r
+void gp2x_sound_sync(void)\r
+{\r
+ ioctl(sounddev, SOUND_PCM_SYNC, 0);\r
+}\r
+\r
+void gp2x_sound_volume(int l, int r)\r
+{\r
+ l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;\r
+ l<<=8; l|=r;\r
+ ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/\r
+}\r
+\r
+\r
+/* 940 */\r
+void Pause940(int yes)\r
+{\r
+ if(yes)\r
+ gp2x_memregs[0x0904>>1] &= 0xFFFE;\r
+ else\r
+ gp2x_memregs[0x0904>>1] |= 1;\r
+}\r
+\r
+\r
+void Reset940(int yes, int bank)\r
+{\r
+ gp2x_memregs[0x3B48>>1] = ((yes&1) << 7) | (bank & 0x03);\r
+}\r
+\r
+static void proc_set(const char *path, const char *val)\r
+{\r
+ FILE *f;\r
+ char tmp[16];\r
+\r
+ f = fopen(path, "w");\r
+ if (f == NULL) {\r
+ printf("failed to open: %s\n", path);\r
+ return;\r
+ }\r
+\r
+ fprintf(f, "0\n");\r
+ fclose(f);\r
+\r
+ printf("\"%s\" is set to: ", path);\r
+ f = fopen(path, "r");\r
+ if (f == NULL) {\r
+ printf("(open failed)\n");\r
+ return;\r
+ }\r
+\r
+ fgets(tmp, sizeof(tmp), f);\r
+ printf("%s", tmp);\r
+ fclose(f);\r
+}\r
+\r
+\r
+/* common */\r
+void gp2x_init(void)\r
+{\r
+ printf("entering init()\n"); fflush(stdout);\r
+\r
+ memdev = open("/dev/mem", O_RDWR);\r
+ if (memdev == -1)\r
+ {\r
+ printf("open(\"/dev/mem\") failed with %i\n", errno);\r
+ exit(1);\r
+ }\r
+\r
+ gp2x_memregs = mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);\r
+ printf("memregs are @ %p\n", gp2x_memregs);\r
+ if(gp2x_memregs == MAP_FAILED)\r
+ {\r
+ printf("mmap(memregs) failed with %i\n", errno);\r
+ exit(1);\r
+ }\r
+ gp2x_memregl = (unsigned long *) gp2x_memregs;\r
+\r
+ gp2x_memregs[0x2880>>1] &= ~0x383; // disable cursor, subpict, osd, video layers\r
+\r
+ gp2x_screens[0] = mmap(0, FRAMEBUFF_WHOLESIZE, PROT_WRITE, MAP_SHARED, memdev, FRAMEBUFF_ADDR0);\r
+ if(gp2x_screens[0] == MAP_FAILED)\r
+ {\r
+ printf("mmap(gp2x_screen) failed with %i\n", errno);\r
+ exit(1);\r
+ }\r
+ printf("framebuffers point to %p\n", gp2x_screens[0]);\r
+ gp2x_screens[1] = (char *) gp2x_screens[0]+0x30000;\r
+ gp2x_screens[2] = (char *) gp2x_screens[1]+0x30000;\r
+ gp2x_screens[3] = (char *) gp2x_screens[2]+0x30000;\r
+\r
+ gp2x_screen = gp2x_screens[0];\r
+ screensel = 0;\r
+\r
+ gp2x_screenaddr_old[0] = gp2x_memregs[0x290E>>1];\r
+ gp2x_screenaddr_old[1] = gp2x_memregs[0x2910>>1];\r
+ gp2x_screenaddr_old[2] = gp2x_memregs[0x2912>>1];\r
+ gp2x_screenaddr_old[3] = gp2x_memregs[0x2914>>1];\r
+\r
+ memcpy(gp2x_screenaddrs_use, gp2x_screenaddrs, sizeof(gp2x_screenaddrs));\r
+ gp2x_memset_all_buffers(0, 0, 320*240*2);\r
+\r
+ // snd\r
+ mixerdev = open("/dev/mixer", O_RDWR);\r
+ if (mixerdev == -1)\r
+ printf("open(\"/dev/mixer\") failed with %i\n", errno);\r
+\r
+ /* init usb joys -GnoStiC */\r
+ gp2x_usbjoy_init();\r
+\r
+ // touchscreen\r
+ touchdev = open("/dev/touchscreen/wm97xx", O_RDONLY);\r
+ if (touchdev >= 0) {\r
+ FILE *pcf = fopen("/etc/pointercal", "r");\r
+ if (pcf) {\r
+ fscanf(pcf, "%d %d %d %d %d %d %d", &touchcal[0], &touchcal[1],\r
+ &touchcal[2], &touchcal[3], &touchcal[4], &touchcal[5], &touchcal[6]);\r
+ fclose(pcf);\r
+ }\r
+ printf("found touchscreen/wm97xx\n");\r
+ }\r
+\r
+ /* disable Linux read-ahead */\r
+ proc_set("/proc/sys/vm/max-readahead", "0\n");\r
+ proc_set("/proc/sys/vm/min-readahead", "0\n");\r
+\r
+ printf("exitting init()\n"); fflush(stdout);\r
+}\r
+\r
+char *ext_menu = 0, *ext_state = 0;\r
+\r
+void gp2x_deinit(void)\r
+{\r
+ Reset940(1, 3);\r
+ Pause940(1);\r
+\r
+ gp2x_video_changemode(15);\r
+ gp2x_memregs[0x290E>>1] = gp2x_screenaddr_old[0];\r
+ gp2x_memregs[0x2910>>1] = gp2x_screenaddr_old[1];\r
+ gp2x_memregs[0x2912>>1] = gp2x_screenaddr_old[2];\r
+ gp2x_memregs[0x2914>>1] = gp2x_screenaddr_old[3];\r
+\r
+ munmap(gp2x_screens[0], FRAMEBUFF_WHOLESIZE);\r
+ munmap((void *)gp2x_memregs, 0x10000);\r
+ close(memdev);\r
+ close(mixerdev);\r
+ if (sounddev >= 0) close(sounddev);\r
+ if (touchdev >= 0) close(touchdev);\r
+\r
+ gp2x_usbjoy_deinit();\r
+\r
+ printf("all done, running ");\r
+\r
+ // Zaq121's alternative frontend support from MAME\r
+ if(ext_menu && ext_state) {\r
+ printf("%s -state %s\n", ext_menu, ext_state);\r
+ execl(ext_menu, ext_menu, "-state", ext_state, NULL);\r
+ } else if(ext_menu) {\r
+ printf("%s\n", ext_menu);\r
+ execl(ext_menu, ext_menu, NULL);\r
+ } else {\r
+ printf("gp2xmenu\n");\r
+ chdir("/usr/gp2x");\r
+ execl("gp2xmenu", "gp2xmenu", NULL);\r
+ }\r
+}\r
+\r
+/* lprintf */\r
+void lprintf(const char *fmt, ...)\r
+{\r
+ va_list vl;\r
+\r
+ va_start(vl, fmt);\r
+ vprintf(fmt, vl);\r
+ va_end(vl);\r
+}\r
+\r
+\r
+/* fake GP2X */\r
+int crashed_940 = 0;\r
+\r
+int readpng(void *dest, const char *fname, int what) { return -1; }\r
+void set_gamma(int g100, int A_SNs_curve) {}\r
+void set_FCLK(unsigned MHZ) {}\r
+void set_LCD_custom_rate(int rate) {}\r
+void unset_LCD_custom_rate(void) {}\r
+\r