move emu loop to common; redo timing; add pollux timer
[libpicofe.git] / gp2x / soc.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <sys/mman.h>
7 #include <unistd.h>
8
9 #include "soc.h"
10
11 void (*gp2x_video_flip)(void);
12 void (*gp2x_video_flip2)(void);
13 void (*gp2x_video_changemode_ll)(int bpp);
14 void (*gp2x_video_setpalette)(int *pal, int len);
15 void (*gp2x_video_RGB_setscaling)(int ln_offs, int W, int H);
16 void (*gp2x_video_wait_vsync)(void);
17
18 void (*gp2x_set_cpuclk)(unsigned int mhz);
19
20 void (*set_lcd_custom_rate)(int is_pal);
21 void (*unset_lcd_custom_rate)(void);
22 void (*set_lcd_gamma)(int g100, int A_SNs_curve);
23
24 void (*set_ram_timings)(void);
25 void (*unset_ram_timings)(void);
26 int  (*gp2x_read_battery)(void);
27
28 unsigned int (*gp2x_get_ticks_ms)(void);
29 unsigned int (*gp2x_get_ticks_us)(void);
30
31
32 gp2x_soc_t soc_detect(void)
33 {
34         volatile unsigned short *memregs;
35         volatile unsigned int *memregl;
36         static gp2x_soc_t ret = -1;
37         int pollux_chipname[0x30/4 + 1];
38         char *pollux_chipname_c = (char *)pollux_chipname;
39         int memdev;
40         int i;
41
42         if (ret != -1)
43                 /* already detected */
44                 return ret;
45
46         memdev = open("/dev/mem", O_RDONLY);
47         if (memdev == -1)
48         {
49                 perror("open(/dev/mem)");
50                 return -1;
51         }
52
53         memregs = mmap(0, 0x20000, PROT_READ, MAP_SHARED, memdev, 0xc0000000);
54         if (memregs == MAP_FAILED)
55         {
56                 perror("mmap(memregs)");
57                 close(memdev);
58                 return -1;
59         }
60         memregl = (volatile void *)memregs;
61
62         if (memregs[0x1836>>1] == 0x2330)
63         {
64                 printf("looks like this is MMSP2\n");
65                 ret = SOCID_MMSP2;
66                 goto out;
67         }
68
69         /* perform word reads. Byte reads might also work,
70          * but we don't want to play with that. */
71         for (i = 0; i < 0x30; i += 4)
72         {
73                 pollux_chipname[i >> 2] = memregl[(0x1f810 + i) >> 2];
74         }
75         pollux_chipname_c[0x30] = 0;
76
77         for (i = 0; i < 0x30; i++)
78         {
79                 unsigned char c = pollux_chipname_c[i];
80                 if (c < 0x20 || c > 0x7f)
81                         goto not_pollux_like;
82         }
83
84         printf("found pollux-like id: \"%s\"\n", pollux_chipname_c);
85
86         if (strncmp(pollux_chipname_c, "MAGICEYES-LEAPFROG-LF1000", 25) ||
87                 strncmp(pollux_chipname_c, "MAGICEYES-POLLUX", 16))
88         {
89                 ret = SOCID_POLLUX;
90                 goto out;
91         }
92
93 not_pollux_like:
94 out:
95         munmap((void *)memregs, 0x20000);
96         close(memdev);
97         return ret;     
98 }
99