refactoring for Wiz port; random cleanups
[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 #include "../common/emu.h"
11
12 gp2x_soc_t gp2x_soc = -1;
13
14 gp2x_soc_t soc_detect(void)
15 {
16         volatile unsigned short *memregs;
17         volatile unsigned int *memregl;
18         int pollux_chipname[0x30/4 + 1];
19         char *pollux_chipname_c = (char *)pollux_chipname;
20         gp2x_soc_t ret = -1;
21         int memdev;
22         int i;
23
24         memdev = open("/dev/mem", O_RDONLY);
25         if (memdev == -1)
26         {
27                 perror("open(/dev/mem)");
28                 return -1;
29         }
30
31         memregs = mmap(0, 0x20000, PROT_READ, MAP_SHARED, memdev, 0xc0000000);
32         if (memregs == MAP_FAILED)
33         {
34                 perror("mmap(memregs)");
35                 close(memdev);
36                 return -1;
37         }
38         memregl = (volatile void *)memregs;
39
40         if (memregs[0x1836>>1] == 0x2330)
41         {
42                 printf("looks like this is MMSP2\n");
43                 ret = SOCID_MMSP2;
44                 goto out;
45         }
46
47         /* perform word reads. Byte reads might also work,
48          * but we don't want to play with that. */
49         for (i = 0; i < 0x30; i += 4)
50         {
51                 pollux_chipname[i >> 2] = memregl[(0x1f810 + i) >> 2];
52         }
53         pollux_chipname_c[0x30] = 0;
54
55         for (i = 0; i < 0x30; i++)
56         {
57                 unsigned char c = pollux_chipname_c[i];
58                 if (c < 0x20 || c > 0x7f)
59                         goto not_pollux_like;
60         }
61
62         printf("found pollux-like id: \"%s\"\n", pollux_chipname_c);
63
64         if (strncmp(pollux_chipname_c, "MAGICEYES-LEAPFROG-LF1000", 25) ||
65                 strncmp(pollux_chipname_c, "MAGICEYES-POLLUX", 16))
66         {
67                 ret = SOCID_POLLUX;
68                 goto out;
69         }
70
71 not_pollux_like:
72 out:
73         munmap((void *)memregs, 0x20000);
74         close(memdev);
75         gp2x_soc = ret;
76         return ret;     
77 }
78