2823a4c8 |
1 | /* Parts used from cpuctrl */ |
2 | /* cpuctrl for GP2X |
3 | Copyright (C) 2005 Hermes/PS2Reality |
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 _BSD_SOURCE |
23 | #define _GNU_SOURCE |
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> |
2823a4c8 |
29 | #include "../common.h" |
30 | #include "gp2x.h" |
4742480d |
31 | #include "warm.h" |
2823a4c8 |
32 | |
90206450 |
33 | u32 gp2x_audio_volume = 74/2; |
2823a4c8 |
34 | u32 gpsp_gp2x_dev_audio = 0; |
35 | u32 gpsp_gp2x_dev = 0; |
4742480d |
36 | u32 gpsp_gp2x_gpiodev = 0; |
2823a4c8 |
37 | |
4742480d |
38 | static volatile u16 *gpsp_gp2x_memregs; |
39 | static volatile u32 *gpsp_gp2x_memregl; |
2823a4c8 |
40 | |
4cdfc0bc |
41 | #ifdef WIZ_BUILD |
42 | #include <linux/fb.h> |
43 | void *gpsp_gp2x_screen; |
5c6e71a0 |
44 | #define fb_buf_count 4 |
45 | static u32 fb_paddr[fb_buf_count]; |
46 | static void *fb_vaddr[fb_buf_count]; |
4cdfc0bc |
47 | static u32 fb_work_buf; |
5c6e71a0 |
48 | static int fb_buf_use; |
42c81190 |
49 | static int fbdev; |
4cdfc0bc |
50 | |
51 | static void fb_video_init() |
52 | { |
53 | struct fb_fix_screeninfo fbfix; |
54 | int i, ret; |
4cdfc0bc |
55 | |
56 | fbdev = open("/dev/fb0", O_RDWR); |
57 | if (fbdev < 0) { |
58 | perror("can't open fbdev"); |
59 | exit(1); |
60 | } |
61 | |
62 | ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix); |
63 | if (ret == -1) |
64 | { |
65 | perror("ioctl(fbdev) failed"); |
66 | exit(1); |
67 | } |
68 | |
69 | printf("framebuffer: \"%s\" @ %08lx\n", fbfix.id, fbfix.smem_start); |
70 | fb_paddr[0] = fbfix.smem_start; |
4cdfc0bc |
71 | |
72 | fb_vaddr[0] = mmap(0, 320*240*2*fb_buf_count, PROT_READ|PROT_WRITE, |
73 | MAP_SHARED, gpsp_gp2x_dev, fb_paddr[0]); |
74 | if (fb_vaddr[0] == MAP_FAILED) |
75 | { |
76 | perror("mmap(fb_vaddr) failed"); |
77 | exit(1); |
78 | } |
79 | memset(fb_vaddr[0], 0, 320*240*2*fb_buf_count); |
80 | |
81 | printf(" %p -> %08x\n", fb_vaddr[0], fb_paddr[0]); |
82 | for (i = 1; i < fb_buf_count; i++) |
83 | { |
84 | fb_paddr[i] = fb_paddr[i-1] + 320*240*2; |
85 | fb_vaddr[i] = (char *)fb_vaddr[i-1] + 320*240*2; |
86 | printf(" %p -> %08x\n", fb_vaddr[i], fb_paddr[i]); |
87 | } |
88 | fb_work_buf = 0; |
89 | fb_buf_use = fb_buf_count; |
90 | |
91 | pollux_video_flip(); |
92 | warm_change_cb_upper(WCB_C_BIT|WCB_B_BIT, 1); |
93 | } |
94 | |
95 | void pollux_video_flip() |
96 | { |
4cdfc0bc |
97 | gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[fb_work_buf]; |
98 | gpsp_gp2x_memregl[0x4058>>2] |= 0x10; |
99 | fb_work_buf++; |
100 | if (fb_work_buf >= fb_buf_use) |
101 | fb_work_buf = 0; |
102 | gpsp_gp2x_screen = fb_vaddr[fb_work_buf]; |
103 | } |
104 | |
105 | void fb_use_buffers(int count) |
106 | { |
107 | if (count < 1) |
108 | count = 1; |
109 | else if (count > fb_buf_count) |
110 | count = fb_buf_count; |
111 | fb_buf_use = count; |
5c6e71a0 |
112 | memset(fb_vaddr[0], 0, 320*240*2*count); |
4cdfc0bc |
113 | } |
114 | |
42c81190 |
115 | void wiz_lcd_set_portrait(int y) |
116 | { |
117 | static int old_y = -1; |
118 | int cmd[2] = { 0, 0 }; |
119 | |
120 | if (old_y == y) |
121 | return; |
122 | cmd[0] = y ? 6 : 5; |
123 | ioctl(fbdev, _IOW('D', 90, int[2]), cmd); |
124 | gpsp_gp2x_memregl[0x4004>>2] = y ? 0x013f00ef : 0x00ef013f; |
125 | gpsp_gp2x_memregl[0x4000>>2] |= 1 << 3; |
126 | old_y = y; |
127 | } |
128 | |
4cdfc0bc |
129 | static void fb_video_exit() |
130 | { |
42c81190 |
131 | /* switch to default fb mem, turn portrait off */ |
4cdfc0bc |
132 | gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[0]; |
133 | gpsp_gp2x_memregl[0x4058>>2] |= 0x10; |
42c81190 |
134 | wiz_lcd_set_portrait(0); |
135 | close(fbdev); |
136 | } |
137 | |
138 | static int wiz_gamepak_fd = -1; |
139 | static u32 wiz_gamepak_size; |
140 | |
141 | static void wiz_gamepak_cleanup() |
142 | { |
143 | if (wiz_gamepak_size) |
144 | munmap(gamepak_rom, wiz_gamepak_size); |
145 | if (wiz_gamepak_fd >= 0) |
146 | close(wiz_gamepak_fd); |
147 | gamepak_rom = NULL; |
148 | wiz_gamepak_size = 0; |
149 | wiz_gamepak_fd = -1; |
4cdfc0bc |
150 | } |
42c81190 |
151 | |
152 | u32 wiz_load_gamepak(char *name) |
153 | { |
154 | char *dot_position = strrchr(name, '.'); |
155 | u32 ret; |
156 | |
157 | if (!strcasecmp(dot_position, ".zip")) |
158 | { |
159 | if (wiz_gamepak_fd >= 0) |
160 | { |
161 | wiz_gamepak_cleanup(); |
162 | printf("switching to ROM malloc\n"); |
163 | init_gamepak_buffer(); |
164 | } |
165 | return load_file_zip(name); |
166 | } |
167 | |
168 | if (wiz_gamepak_fd < 0) |
169 | { |
170 | extern void *gamepak_memory_map; |
171 | free(gamepak_rom); |
172 | free(gamepak_memory_map); |
173 | gamepak_memory_map = NULL; |
174 | printf("switching to ROM mmap\n"); |
175 | } |
176 | else |
177 | wiz_gamepak_cleanup(); |
178 | |
179 | wiz_gamepak_fd = open(name, O_RDONLY|O_NOATIME, S_IRUSR); |
180 | if (wiz_gamepak_fd < 0) |
181 | { |
182 | perror("wiz_load_gamepak: open failed"); |
183 | return -1; |
184 | } |
185 | |
186 | ret = lseek(wiz_gamepak_fd, 0, SEEK_END); |
187 | wiz_gamepak_size = gamepak_ram_buffer_size = ret; |
188 | |
189 | gamepak_rom = mmap(0, ret, PROT_READ, MAP_SHARED, wiz_gamepak_fd, 0); |
190 | if (gamepak_rom == MAP_FAILED) |
191 | { |
192 | perror("wiz_load_gamepak: mmap failed"); |
193 | return -1; |
194 | } |
195 | |
196 | return ret; |
197 | } |
198 | |
4cdfc0bc |
199 | #endif |
200 | |
5a01fba6 |
201 | static int get_romdir(char *buff, size_t size) |
2823a4c8 |
202 | { |
638cc626 |
203 | FILE *f; |
5a01fba6 |
204 | char *s; |
205 | int r = -1; |
206 | |
207 | f = fopen("romdir.txt", "r"); |
208 | if (f == NULL) |
209 | return -1; |
210 | |
211 | s = fgets(buff, size, f); |
212 | if (s) |
213 | { |
214 | r = strlen(s); |
215 | while (r > 0 && isspace(buff[r-1])) |
216 | buff[--r] = 0; |
217 | } |
218 | |
219 | fclose(f); |
220 | return r; |
221 | } |
222 | |
223 | void gp2x_init() |
224 | { |
225 | char buff[256]; |
226 | |
2823a4c8 |
227 | gpsp_gp2x_dev = open("/dev/mem", O_RDWR); |
228 | gpsp_gp2x_dev_audio = open("/dev/mixer", O_RDWR); |
229 | gpsp_gp2x_memregl = |
230 | (unsigned long *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, |
231 | gpsp_gp2x_dev, 0xc0000000); |
232 | gpsp_gp2x_memregs = (unsigned short *)gpsp_gp2x_memregl; |
638cc626 |
233 | warm_init(); |
4742480d |
234 | #ifdef WIZ_BUILD |
235 | gpsp_gp2x_gpiodev = open("/dev/GPIO", O_RDONLY); |
4cdfc0bc |
236 | fb_video_init(); |
237 | #endif |
2823a4c8 |
238 | |
5a01fba6 |
239 | if (get_romdir(buff, sizeof(buff)) > 0) |
240 | chdir(buff); |
638cc626 |
241 | |
2823a4c8 |
242 | gp2x_sound_volume(1); |
243 | } |
244 | |
245 | void gp2x_quit() |
246 | { |
5a01fba6 |
247 | char buff1[256], buff2[256]; |
638cc626 |
248 | |
5a01fba6 |
249 | getcwd(buff1, sizeof(buff1)); |
638cc626 |
250 | chdir(main_path); |
5a01fba6 |
251 | if (get_romdir(buff2, sizeof(buff2)) >= 0 && |
252 | strcmp(buff1, buff2) != 0) |
638cc626 |
253 | { |
5a01fba6 |
254 | FILE *f = fopen("romdir.txt", "w"); |
255 | if (f != NULL) |
256 | { |
257 | printf("writing romdir: %s\n", buff1); |
258 | fprintf(f, "%s", buff1); |
259 | fclose(f); |
260 | } |
638cc626 |
261 | } |
262 | |
263 | warm_finish(); |
4742480d |
264 | #ifdef WIZ_BUILD |
265 | close(gpsp_gp2x_gpiodev); |
4cdfc0bc |
266 | fb_video_exit(); |
42c81190 |
267 | wiz_gamepak_cleanup(); |
4742480d |
268 | #endif |
4cdfc0bc |
269 | munmap((void *)gpsp_gp2x_memregl, 0x10000); |
270 | close(gpsp_gp2x_dev_audio); |
271 | close(gpsp_gp2x_dev); |
90206450 |
272 | |
638cc626 |
273 | fcloseall(); |
5a01fba6 |
274 | sync(); |
42c81190 |
275 | exit(0); |
2823a4c8 |
276 | } |
277 | |
278 | void gp2x_sound_volume(u32 volume_up) |
279 | { |
280 | u32 volume; |
281 | if((volume_up == 0) && (gp2x_audio_volume > 0)) |
282 | gp2x_audio_volume--; |
283 | |
284 | if((volume_up != 0) && (gp2x_audio_volume < 100)) |
285 | gp2x_audio_volume++; |
286 | |
287 | volume = (gp2x_audio_volume * 0x50) / 100; |
288 | volume = (gp2x_audio_volume << 8) | gp2x_audio_volume; |
289 | ioctl(gpsp_gp2x_dev_audio, SOUND_MIXER_WRITE_PCM, &volume); |
290 | } |
291 | |
4742480d |
292 | u32 gpsp_gp2x_joystick_read(void) |
293 | { |
294 | #ifdef WIZ_BUILD |
295 | u32 value = 0; |
296 | read(gpsp_gp2x_gpiodev, &value, 4); |
297 | if(value & 0x02) |
298 | value |= 0x05; |
299 | if(value & 0x08) |
300 | value |= 0x14; |
301 | if(value & 0x20) |
302 | value |= 0x50; |
303 | if(value & 0x80) |
304 | value |= 0x41; |
305 | return value; |
306 | #else |
307 | u32 value = (gpsp_gp2x_memregs[0x1198 >> 1] & 0x00FF); |
308 | |
309 | if(value == 0xFD) |
310 | value = 0xFA; |
311 | if(value == 0xF7) |
312 | value = 0xEB; |
313 | if(value == 0xDF) |
314 | value = 0xAF; |
315 | if(value == 0x7F) |
316 | value = 0xBE; |
317 | |
318 | return ~((gpsp_gp2x_memregs[0x1184 >> 1] & 0xFF00) | value | |
319 | (gpsp_gp2x_memregs[0x1186 >> 1] << 16)); |
320 | #endif |
321 | } |
322 | |
638cc626 |
323 | // Fout = (m * Fin) / (p * 2^s) |
4742480d |
324 | void set_FCLK(u32 MHZ) |
325 | { |
638cc626 |
326 | u32 v; |
327 | u32 mdiv, pdiv, sdiv = 0; |
328 | #ifdef WIZ_BUILD |
329 | #define SYS_CLK_FREQ 27 |
330 | // m = MDIV, p = PDIV, s = SDIV |
331 | pdiv = 9; |
332 | mdiv = (MHZ * pdiv) / SYS_CLK_FREQ; |
333 | mdiv &= 0x3ff; |
334 | v = (pdiv<<18) | (mdiv<<8) | sdiv; |
335 | |
336 | gpsp_gp2x_memregl[0xf004>>2] = v; |
337 | gpsp_gp2x_memregl[0xf07c>>2] |= 0x8000; |
5a01fba6 |
338 | while (gpsp_gp2x_memregl[0xf07c>>2] & 0x8000) |
339 | ; |
638cc626 |
340 | #else |
341 | #define SYS_CLK_FREQ 7372800 |
342 | // m = MDIV + 8, p = PDIV + 2, s = SDIV |
343 | pdiv = 3; |
344 | mdiv = (MHZ * pdiv * 1000000) / SYS_CLK_FREQ; |
345 | mdiv &= 0xff; |
346 | v = ((mdiv-8)<<8) | ((pdiv-2)<<2) | sdiv; |
347 | gpsp_gp2x_memregs[0x910>>1] = v; |
4742480d |
348 | #endif |
638cc626 |
349 | } |
4742480d |
350 | |