initial pandora port, with hardware scaling and stuff
[gpsp.git] / gp2x / gp2x.c
CommitLineData
8b6232a6 1/*
2 Parts used from cpuctrl, Copyright (C) 2005 Hermes/PS2Reality
3 Portions Copyright (C) 2009 notaz
2823a4c8 4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19*/
20
21
638cc626 22#define _GNU_SOURCE
8b6232a6 23#include "../common.h"
2823a4c8 24#include <sys/mman.h>
25#include <sys/ioctl.h>
26#include <sys/soundcard.h>
638cc626 27#include <sys/types.h>
28#include <unistd.h>
8b6232a6 29#include <ctype.h>
2823a4c8 30#include "gp2x.h"
e8f5db5d 31#include "pollux_dpc_set.h"
2823a4c8 32
43c24b30 33static u32 gpsp_gp2x_dev_audio;
34static u32 gpsp_gp2x_dev;
35static u32 gpsp_gp2x_gpiodev;
36
37static u32 gp2x_audio_volume = 74/2;
2823a4c8 38
4742480d 39static volatile u16 *gpsp_gp2x_memregs;
40static volatile u32 *gpsp_gp2x_memregl;
2823a4c8 41
eb3668fc 42u32 button_plat_mask_to_config[PLAT_BUTTON_COUNT] =
43c24b30 43{
44 GP2X_UP,
45 GP2X_LEFT,
46 GP2X_DOWN,
47 GP2X_RIGHT,
48 GP2X_START,
49 GP2X_SELECT,
50 GP2X_L,
51 GP2X_R,
52 GP2X_A,
53 GP2X_B,
54 GP2X_X,
55 GP2X_Y,
56 GP2X_VOL_DOWN,
57 GP2X_VOL_UP,
58 GP2X_PUSH,
59 GP2X_VOL_MIDDLE
60};
61
eb3668fc 62u32 gamepad_config_map[PLAT_BUTTON_COUNT] =
43c24b30 63{
64 BUTTON_ID_UP, // Up
65 BUTTON_ID_LEFT, // Left
66 BUTTON_ID_DOWN, // Down
67 BUTTON_ID_RIGHT, // Right
68 BUTTON_ID_START, // Start
69 BUTTON_ID_SELECT, // Select
70 BUTTON_ID_L, // Ltrigger
71 BUTTON_ID_R, // Rtrigger
72 BUTTON_ID_FPS, // A
73 BUTTON_ID_A, // B
74 BUTTON_ID_B, // X
75 BUTTON_ID_MENU, // Y
76 BUTTON_ID_VOLDOWN, // Vol down
77 BUTTON_ID_VOLUP, // Vol up
78 BUTTON_ID_FPS, // Push
79 BUTTON_ID_MENU // Vol middle
80};
81
4cdfc0bc 82#ifdef WIZ_BUILD
83#include <linux/fb.h>
84void *gpsp_gp2x_screen;
5c6e71a0 85#define fb_buf_count 4
86static u32 fb_paddr[fb_buf_count];
87static void *fb_vaddr[fb_buf_count];
4cdfc0bc 88static u32 fb_work_buf;
5c6e71a0 89static int fb_buf_use;
42c81190 90static int fbdev;
4cdfc0bc 91
92static void fb_video_init()
93{
94 struct fb_fix_screeninfo fbfix;
95 int i, ret;
4cdfc0bc 96
97 fbdev = open("/dev/fb0", O_RDWR);
98 if (fbdev < 0) {
99 perror("can't open fbdev");
100 exit(1);
101 }
102
103 ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix);
104 if (ret == -1)
105 {
106 perror("ioctl(fbdev) failed");
107 exit(1);
108 }
109
110 printf("framebuffer: \"%s\" @ %08lx\n", fbfix.id, fbfix.smem_start);
111 fb_paddr[0] = fbfix.smem_start;
4cdfc0bc 112
113 fb_vaddr[0] = mmap(0, 320*240*2*fb_buf_count, PROT_READ|PROT_WRITE,
114 MAP_SHARED, gpsp_gp2x_dev, fb_paddr[0]);
115 if (fb_vaddr[0] == MAP_FAILED)
116 {
117 perror("mmap(fb_vaddr) failed");
118 exit(1);
119 }
120 memset(fb_vaddr[0], 0, 320*240*2*fb_buf_count);
121
122 printf(" %p -> %08x\n", fb_vaddr[0], fb_paddr[0]);
123 for (i = 1; i < fb_buf_count; i++)
124 {
125 fb_paddr[i] = fb_paddr[i-1] + 320*240*2;
126 fb_vaddr[i] = (char *)fb_vaddr[i-1] + 320*240*2;
127 printf(" %p -> %08x\n", fb_vaddr[i], fb_paddr[i]);
128 }
129 fb_work_buf = 0;
130 fb_buf_use = fb_buf_count;
131
132 pollux_video_flip();
133 warm_change_cb_upper(WCB_C_BIT|WCB_B_BIT, 1);
134}
135
136void pollux_video_flip()
137{
4cdfc0bc 138 gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[fb_work_buf];
139 gpsp_gp2x_memregl[0x4058>>2] |= 0x10;
140 fb_work_buf++;
141 if (fb_work_buf >= fb_buf_use)
142 fb_work_buf = 0;
143 gpsp_gp2x_screen = fb_vaddr[fb_work_buf];
144}
145
146void fb_use_buffers(int count)
147{
148 if (count < 1)
149 count = 1;
150 else if (count > fb_buf_count)
151 count = fb_buf_count;
152 fb_buf_use = count;
5c6e71a0 153 memset(fb_vaddr[0], 0, 320*240*2*count);
4cdfc0bc 154}
155
42c81190 156void wiz_lcd_set_portrait(int y)
157{
158 static int old_y = -1;
159 int cmd[2] = { 0, 0 };
160
161 if (old_y == y)
162 return;
163 cmd[0] = y ? 6 : 5;
164 ioctl(fbdev, _IOW('D', 90, int[2]), cmd);
165 gpsp_gp2x_memregl[0x4004>>2] = y ? 0x013f00ef : 0x00ef013f;
166 gpsp_gp2x_memregl[0x4000>>2] |= 1 << 3;
167 old_y = y;
e8f5db5d 168
169 /* the above ioctl resets LCD timings, so set them here */
170 pollux_dpc_set(gpsp_gp2x_memregs, getenv("pollux_dpc_set"));
42c81190 171}
172
4cdfc0bc 173static void fb_video_exit()
174{
42c81190 175 /* switch to default fb mem, turn portrait off */
4cdfc0bc 176 gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[0];
177 gpsp_gp2x_memregl[0x4058>>2] |= 0x10;
42c81190 178 wiz_lcd_set_portrait(0);
179 close(fbdev);
180}
181
182static int wiz_gamepak_fd = -1;
183static u32 wiz_gamepak_size;
184
185static void wiz_gamepak_cleanup()
186{
187 if (wiz_gamepak_size)
188 munmap(gamepak_rom, wiz_gamepak_size);
189 if (wiz_gamepak_fd >= 0)
190 close(wiz_gamepak_fd);
191 gamepak_rom = NULL;
192 wiz_gamepak_size = 0;
193 wiz_gamepak_fd = -1;
4cdfc0bc 194}
42c81190 195
196u32 wiz_load_gamepak(char *name)
197{
198 char *dot_position = strrchr(name, '.');
199 u32 ret;
200
201 if (!strcasecmp(dot_position, ".zip"))
202 {
203 if (wiz_gamepak_fd >= 0)
204 {
205 wiz_gamepak_cleanup();
206 printf("switching to ROM malloc\n");
207 init_gamepak_buffer();
208 }
209 return load_file_zip(name);
210 }
211
212 if (wiz_gamepak_fd < 0)
213 {
214 extern void *gamepak_memory_map;
215 free(gamepak_rom);
216 free(gamepak_memory_map);
217 gamepak_memory_map = NULL;
218 printf("switching to ROM mmap\n");
219 }
220 else
221 wiz_gamepak_cleanup();
222
223 wiz_gamepak_fd = open(name, O_RDONLY|O_NOATIME, S_IRUSR);
224 if (wiz_gamepak_fd < 0)
225 {
226 perror("wiz_load_gamepak: open failed");
227 return -1;
228 }
229
230 ret = lseek(wiz_gamepak_fd, 0, SEEK_END);
231 wiz_gamepak_size = gamepak_ram_buffer_size = ret;
232
233 gamepak_rom = mmap(0, ret, PROT_READ, MAP_SHARED, wiz_gamepak_fd, 0);
234 if (gamepak_rom == MAP_FAILED)
235 {
236 perror("wiz_load_gamepak: mmap failed");
237 return -1;
238 }
239
240 return ret;
241}
242
4cdfc0bc 243#endif
244
5a01fba6 245static int get_romdir(char *buff, size_t size)
2823a4c8 246{
638cc626 247 FILE *f;
5a01fba6 248 char *s;
249 int r = -1;
250
251 f = fopen("romdir.txt", "r");
252 if (f == NULL)
253 return -1;
254
255 s = fgets(buff, size, f);
256 if (s)
257 {
258 r = strlen(s);
259 while (r > 0 && isspace(buff[r-1]))
260 buff[--r] = 0;
261 }
262
263 fclose(f);
264 return r;
265}
266
43c24b30 267void gpsp_plat_init(void)
5a01fba6 268{
269 char buff[256];
270
2823a4c8 271 gpsp_gp2x_dev = open("/dev/mem", O_RDWR);
272 gpsp_gp2x_dev_audio = open("/dev/mixer", O_RDWR);
8b6232a6 273 gpsp_gp2x_memregl = (u32 *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED,
2823a4c8 274 gpsp_gp2x_dev, 0xc0000000);
8b6232a6 275 gpsp_gp2x_memregs = (u16 *)gpsp_gp2x_memregl;
638cc626 276 warm_init();
4742480d 277#ifdef WIZ_BUILD
278 gpsp_gp2x_gpiodev = open("/dev/GPIO", O_RDONLY);
4cdfc0bc 279 fb_video_init();
280#endif
2823a4c8 281
5a01fba6 282 if (get_romdir(buff, sizeof(buff)) > 0)
283 chdir(buff);
638cc626 284
2823a4c8 285 gp2x_sound_volume(1);
286}
287
43c24b30 288void gpsp_plat_quit(void)
2823a4c8 289{
5a01fba6 290 char buff1[256], buff2[256];
638cc626 291
5a01fba6 292 getcwd(buff1, sizeof(buff1));
638cc626 293 chdir(main_path);
5a01fba6 294 if (get_romdir(buff2, sizeof(buff2)) >= 0 &&
295 strcmp(buff1, buff2) != 0)
638cc626 296 {
5a01fba6 297 FILE *f = fopen("romdir.txt", "w");
298 if (f != NULL)
299 {
300 printf("writing romdir: %s\n", buff1);
301 fprintf(f, "%s", buff1);
302 fclose(f);
303 }
638cc626 304 }
305
306 warm_finish();
4742480d 307#ifdef WIZ_BUILD
308 close(gpsp_gp2x_gpiodev);
4cdfc0bc 309 fb_video_exit();
42c81190 310 wiz_gamepak_cleanup();
4742480d 311#endif
4cdfc0bc 312 munmap((void *)gpsp_gp2x_memregl, 0x10000);
313 close(gpsp_gp2x_dev_audio);
314 close(gpsp_gp2x_dev);
90206450 315
638cc626 316 fcloseall();
5a01fba6 317 sync();
42c81190 318 exit(0);
2823a4c8 319}
320
321void gp2x_sound_volume(u32 volume_up)
322{
323 u32 volume;
324 if((volume_up == 0) && (gp2x_audio_volume > 0))
325 gp2x_audio_volume--;
326
327 if((volume_up != 0) && (gp2x_audio_volume < 100))
328 gp2x_audio_volume++;
329
330 volume = (gp2x_audio_volume * 0x50) / 100;
331 volume = (gp2x_audio_volume << 8) | gp2x_audio_volume;
332 ioctl(gpsp_gp2x_dev_audio, SOUND_MIXER_WRITE_PCM, &volume);
333}
334
43c24b30 335u32 gpsp_plat_joystick_read(void)
4742480d 336{
337#ifdef WIZ_BUILD
338 u32 value = 0;
339 read(gpsp_gp2x_gpiodev, &value, 4);
340 if(value & 0x02)
341 value |= 0x05;
342 if(value & 0x08)
343 value |= 0x14;
344 if(value & 0x20)
345 value |= 0x50;
346 if(value & 0x80)
347 value |= 0x41;
348 return value;
349#else
350 u32 value = (gpsp_gp2x_memregs[0x1198 >> 1] & 0x00FF);
351
352 if(value == 0xFD)
353 value = 0xFA;
354 if(value == 0xF7)
355 value = 0xEB;
356 if(value == 0xDF)
357 value = 0xAF;
358 if(value == 0x7F)
359 value = 0xBE;
360
361 return ~((gpsp_gp2x_memregs[0x1184 >> 1] & 0xFF00) | value |
362 (gpsp_gp2x_memregs[0x1186 >> 1] << 16));
363#endif
364}
365
43c24b30 366u32 gpsp_plat_buttons_to_cursor(u32 buttons)
367{
368 gui_action_type new_button = CURSOR_NONE;
369
370 if(buttons & GP2X_A)
371 new_button = CURSOR_BACK;
372
373 if(buttons & GP2X_X)
374 new_button = CURSOR_EXIT;
375
376 if(buttons & GP2X_B)
377 new_button = CURSOR_SELECT;
378
379 if(buttons & GP2X_UP)
380 new_button = CURSOR_UP;
381
382 if(buttons & GP2X_DOWN)
383 new_button = CURSOR_DOWN;
384
385 if(buttons & GP2X_LEFT)
386 new_button = CURSOR_LEFT;
387
388 if(buttons & GP2X_RIGHT)
389 new_button = CURSOR_RIGHT;
390
391 if(buttons & GP2X_L)
392 new_button = CURSOR_L;
393
394 if(buttons & GP2X_R)
395 new_button = CURSOR_R;
396
397 return new_button;
398}
399
638cc626 400// Fout = (m * Fin) / (p * 2^s)
4742480d 401void set_FCLK(u32 MHZ)
402{
638cc626 403 u32 v;
404 u32 mdiv, pdiv, sdiv = 0;
405#ifdef WIZ_BUILD
406 #define SYS_CLK_FREQ 27
407 // m = MDIV, p = PDIV, s = SDIV
408 pdiv = 9;
409 mdiv = (MHZ * pdiv) / SYS_CLK_FREQ;
410 mdiv &= 0x3ff;
411 v = (pdiv<<18) | (mdiv<<8) | sdiv;
412
413 gpsp_gp2x_memregl[0xf004>>2] = v;
414 gpsp_gp2x_memregl[0xf07c>>2] |= 0x8000;
5a01fba6 415 while (gpsp_gp2x_memregl[0xf07c>>2] & 0x8000)
416 ;
638cc626 417#else
418 #define SYS_CLK_FREQ 7372800
419 // m = MDIV + 8, p = PDIV + 2, s = SDIV
420 pdiv = 3;
421 mdiv = (MHZ * pdiv * 1000000) / SYS_CLK_FREQ;
422 mdiv &= 0xff;
423 v = ((mdiv-8)<<8) | ((pdiv-2)<<2) | sdiv;
424 gpsp_gp2x_memregs[0x910>>1] = v;
4742480d 425#endif
638cc626 426}
4742480d 427