gfx chip faker, hint vector
[picodrive.git] / platform / gp2x / gp2x.c
... / ...
CommitLineData
1/**\r
2 * All this is mostly based on rlyeh's minimal library.\r
3 * Copied here to review all his code and understand what's going on.\r
4**/\r
5\r
6/*\r
7\r
8 GP2X minimal library v0.A by rlyeh, (c) 2005. emulnation.info@rlyeh (swap it!)\r
9\r
10 Thanks to Squidge, Robster, snaff, Reesy and NK, for the help & previous work! :-)\r
11\r
12 License\r
13 =======\r
14\r
15 Free for non-commercial projects (it would be nice receiving a mail from you).\r
16 Other cases, ask me first.\r
17\r
18 GamePark Holdings is not allowed to use this library and/or use parts from it.\r
19\r
20*/\r
21\r
22\r
23#include <stdio.h>\r
24#include <stdlib.h>\r
25#include <string.h>\r
26#include <unistd.h>\r
27#include <sys/mman.h>\r
28#include <sys/types.h>\r
29#include <sys/stat.h>\r
30#include <sys/ioctl.h>\r
31#include <sys/soundcard.h>\r
32#include <fcntl.h>\r
33#include <errno.h>\r
34\r
35#include "gp2x.h"\r
36#include "usbjoy.h"\r
37\r
38volatile unsigned short *gp2x_memregs;\r
39//static\r
40volatile unsigned long *gp2x_memregl;\r
41static void *gp2x_screens[4];\r
42static int screensel = 0;\r
43//static\r
44int memdev = 0;\r
45static int sounddev = 0, mixerdev = 0;\r
46\r
47void *gp2x_screen;\r
48\r
49#define FRAMEBUFF_ADDR0 0x4000000-640*480\r
50#define FRAMEBUFF_ADDR1 0x4000000-640*480*2\r
51#define FRAMEBUFF_ADDR2 0x4000000-640*480*3\r
52#define FRAMEBUFF_ADDR3 0x4000000-640*480*4\r
53\r
54static const int gp2x_screenaddrs[] = { FRAMEBUFF_ADDR0, FRAMEBUFF_ADDR1, FRAMEBUFF_ADDR2, FRAMEBUFF_ADDR3 };\r
55\r
56\r
57/* video stuff */\r
58void gp2x_video_flip(void)\r
59{\r
60 unsigned int address = gp2x_screenaddrs[screensel&3];\r
61\r
62 /* test */\r
63/* {\r
64 int i; char *p=gp2x_screen;\r
65 for (i=0; i < 240; i++) { memset(p+i*320, 0, 32); }\r
66 }*/\r
67\r
68 gp2x_memregs[0x290E>>1]=(unsigned short)(address);\r
69 gp2x_memregs[0x2910>>1]=(unsigned short)(address >> 16);\r
70 gp2x_memregs[0x2912>>1]=(unsigned short)(address);\r
71 gp2x_memregs[0x2914>>1]=(unsigned short)(address >> 16);\r
72\r
73 // jump to other buffer:\r
74 gp2x_screen = gp2x_screens[++screensel&3];\r
75}\r
76\r
77\r
78void gp2x_video_changemode(int bpp)\r
79{\r
80 gp2x_memregs[0x28DA>>1]=(((bpp+1)/8)<<9)|0xAB; /*8/15/16/24bpp...*/\r
81 gp2x_memregs[0x290C>>1]=320*((bpp+1)/8); /*line width in bytes*/\r
82\r
83 gp2x_memset_all_buffers(0, 0, 640*480);\r
84 gp2x_video_flip();\r
85}\r
86\r
87\r
88void gp2x_video_setpalette(int *pal, int len)\r
89{\r
90 unsigned short *g=(unsigned short *)pal;\r
91 volatile unsigned short *memreg = &gp2x_memregs[0x295A>>1];\r
92 gp2x_memregs[0x2958>>1] = 0;\r
93\r
94 len *= 2;\r
95 while(len--) *memreg=*g++;\r
96}\r
97\r
98\r
99// TV Compatible function //\r
100void gp2x_video_RGB_setscaling(int W, int H)\r
101{\r
102 float escalaw, escalah;\r
103 int bpp = (gp2x_memregs[0x28DA>>1]>>9)&0x3;\r
104\r
105 escalaw = 1024.0; // RGB Horiz LCD\r
106 escalah = 320.0; // RGB Vert LCD\r
107\r
108 if(gp2x_memregs[0x2800>>1]&0x100) //TV-Out\r
109 {\r
110 escalaw=489.0; // RGB Horiz TV (PAL, NTSC)\r
111 if (gp2x_memregs[0x2818>>1] == 287) //PAL\r
112 escalah=274.0; // RGB Vert TV PAL\r
113 else if (gp2x_memregs[0x2818>>1] == 239) //NTSC\r
114 escalah=331.0; // RGB Vert TV NTSC\r
115 }\r
116\r
117 // scale horizontal\r
118 gp2x_memregs[0x2906>>1]=(unsigned short)((float)escalaw *(W/320.0));\r
119 // scale vertical\r
120 gp2x_memregl[0x2908>>2]=(unsigned long)((float)escalah *bpp *(H/240.0));\r
121}\r
122\r
123\r
124/* LCD updates @ 80Hz? */\r
125void gp2x_video_wait_vsync(void)\r
126{\r
127 gp2x_memregs[0x2846>>1] = 0x20|2; //(gp2x_memregs[0x2846>>1] | 0x20) & ~2;\r
128 while(!(gp2x_memregs[0x2846>>1] & 2));// usleep(1);\r
129}\r
130\r
131\r
132void gp2x_memcpy_all_buffers(void *data, int offset, int len)\r
133{\r
134 memcpy((char *)gp2x_screens[0] + offset, data, len);\r
135 memcpy((char *)gp2x_screens[1] + offset, data, len);\r
136 memcpy((char *)gp2x_screens[2] + offset, data, len);\r
137 memcpy((char *)gp2x_screens[3] + offset, data, len);\r
138}\r
139\r
140\r
141void gp2x_memset_all_buffers(int offset, int byte, int len)\r
142{\r
143 memset((char *)gp2x_screens[0] + offset, byte, len);\r
144 memset((char *)gp2x_screens[1] + offset, byte, len);\r
145 memset((char *)gp2x_screens[2] + offset, byte, len);\r
146 memset((char *)gp2x_screens[3] + offset, byte, len);\r
147}\r
148\r
149\r
150unsigned long gp2x_joystick_read(int allow_usb_joy)\r
151{\r
152 int i;\r
153 unsigned long value=(gp2x_memregs[0x1198>>1] & 0x00FF);\r
154 if(value==0xFD) value=0xFA;\r
155 if(value==0xF7) value=0xEB;\r
156 if(value==0xDF) value=0xAF;\r
157 if(value==0x7F) value=0xBE;\r
158 value = ~((gp2x_memregs[0x1184>>1] & 0xFF00) | value | (gp2x_memregs[0x1186>>1] << 16));\r
159\r
160 if (allow_usb_joy && num_of_joys > 0) {\r
161 // check the usb joy as well..\r
162 gp2x_usbjoy_update();\r
163 for (i = 0; i < num_of_joys; i++)\r
164 value |= gp2x_usbjoy_check(i);\r
165 }\r
166\r
167 return value;\r
168}\r
169\r
170static int s_oldrate = 0, s_oldbits = 0, s_oldstereo = 0;\r
171\r
172void gp2x_start_sound(int rate, int bits, int stereo)\r
173{\r
174 int frag = 0, bsize, buffers;\r
175\r
176 // if no settings change, we don't need to do anything\r
177 if (rate == s_oldrate && s_oldbits == bits && s_oldstereo == stereo) return;\r
178\r
179 if (sounddev > 0) close(sounddev);\r
180 sounddev = open("/dev/dsp", O_WRONLY|O_ASYNC);\r
181 if (sounddev == -1)\r
182 printf("open(\"/dev/dsp\") failed with %i\n", errno);\r
183\r
184 ioctl(sounddev, SNDCTL_DSP_SPEED, &rate);\r
185 ioctl(sounddev, SNDCTL_DSP_SETFMT, &bits);\r
186 ioctl(sounddev, SNDCTL_DSP_STEREO, &stereo);\r
187 // calculate buffer size\r
188 buffers = 16;\r
189 bsize = rate / 32;\r
190 if (rate > 22050) { bsize*=4; buffers*=2; } // 44k mode seems to be very demanding\r
191 while ((bsize>>=1)) frag++;\r
192 frag |= buffers<<16; // 16 buffers\r
193 ioctl(sounddev, SNDCTL_DSP_SETFRAGMENT, &frag);\r
194 printf("gp2x_set_sound: %i/%ibit/%s, %i buffers of %i bytes\n",\r
195 rate, bits, stereo?"stereo":"mono", frag>>16, 1<<(frag&0xffff));\r
196\r
197 s_oldrate = rate; s_oldbits = bits; s_oldstereo = stereo;\r
198 usleep(100000);\r
199}\r
200\r
201\r
202void gp2x_sound_write(void *buff, int len)\r
203{\r
204 write(sounddev, buff, len);\r
205}\r
206\r
207\r
208void gp2x_sound_volume(int l, int r)\r
209{\r
210 l=l<0?0:l; l=l>255?255:l; r=r<0?0:r; r=r>255?255:r;\r
211 l<<=8; l|=r;\r
212 ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &l); /*SOUND_MIXER_WRITE_VOLUME*/\r
213}\r
214\r
215\r
216/* 940 */\r
217void Pause940(int yes)\r
218{\r
219 if(yes)\r
220 gp2x_memregs[0x0904>>1] &= 0xFFFE;\r
221 else\r
222 gp2x_memregs[0x0904>>1] |= 1;\r
223}\r
224\r
225\r
226void Reset940(int yes)\r
227{\r
228 gp2x_memregs[0x3B48>>1] = ((yes&1) << 7) | (0x03); /* bank=3 */\r
229}\r
230\r
231\r
232\r
233/* common */\r
234void gp2x_init(void)\r
235{\r
236 printf("entering init()\n"); fflush(stdout);\r
237\r
238 memdev = open("/dev/mem", O_RDWR);\r
239 if (memdev == -1)\r
240 {\r
241 printf("open(\"/dev/mem\") failed with %i\n", errno);\r
242 exit(1);\r
243 }\r
244\r
245 gp2x_memregs = mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);\r
246 printf("memregs are @ %p\n", gp2x_memregs);\r
247 if(gp2x_memregs == MAP_FAILED)\r
248 {\r
249 printf("mmap(memregs) failed with %i\n", errno);\r
250 exit(1);\r
251 }\r
252 gp2x_memregl = (unsigned long *) gp2x_memregs;\r
253\r
254 gp2x_screens[3] = mmap(0, 640*480*4, PROT_WRITE, MAP_SHARED, memdev, FRAMEBUFF_ADDR3);\r
255 if(gp2x_screens[3] == MAP_FAILED)\r
256 {\r
257 printf("mmap(gp2x_screen) failed with %i\n", errno);\r
258 exit(1);\r
259 }\r
260 printf("framebuffers point to %p\n", gp2x_screens[3]);\r
261 gp2x_screens[2] = (char *) gp2x_screens[3]+640*480;\r
262 gp2x_screens[1] = (char *) gp2x_screens[2]+640*480;\r
263 gp2x_screens[0] = (char *) gp2x_screens[1]+640*480;\r
264\r
265 gp2x_screen = gp2x_screens[0];\r
266 screensel = 0;\r
267\r
268 // snd\r
269 mixerdev = open("/dev/mixer", O_RDWR);\r
270 if (mixerdev == -1)\r
271 printf("open(\"/dev/mixer\") failed with %i\n", errno);\r
272\r
273 /* init usb joys -GnoStiC */\r
274 gp2x_usbjoy_init();\r
275\r
276 printf("exitting init()\n"); fflush(stdout);\r
277}\r
278\r
279char *ext_menu = 0, *ext_state = 0;\r
280\r
281void gp2x_deinit(void)\r
282{\r
283 Reset940(1);\r
284 Pause940(1);\r
285\r
286 gp2x_video_changemode(15);\r
287 munmap(gp2x_screens[0], 640*480*4);\r
288 munmap((void *)gp2x_memregs, 0x10000);\r
289 close(memdev);\r
290 close(mixerdev);\r
291 if (sounddev > 0) close(sounddev);\r
292\r
293 gp2x_usbjoy_deinit();\r
294\r
295 printf("all done, running ");\r
296\r
297 // Zaq121's alternative frontend support from MAME\r
298 if(ext_menu && ext_state) {\r
299 printf("%s -state %s\n", ext_menu, ext_state);\r
300 execl(ext_menu, ext_menu, "-state", ext_state, NULL);\r
301 } else if(ext_menu) {\r
302 printf("%s\n", ext_menu);\r
303 execl(ext_menu, ext_menu, NULL);\r
304 } else {\r
305 printf("gp2xmenu\n");\r
306 chdir("/usr/gp2x");\r
307 execl("gp2xmenu", "gp2xmenu", NULL);\r
308 }\r
309}\r
310\r
311\r