forked from kelvinlawson/pykaraoke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_cpuctrl.c
214 lines (170 loc) · 5.36 KB
/
_cpuctrl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
Copyright (C) 2010 Kelvin Lawson ([email protected])
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
This module provides support for controlling the GP2X CPU speed at
runtime, via Python. The core GP2X code in this module is taken
from:
cpuctrl.c for GP2X (CPU/LCD/RAM-Tuner Version 2.0)
Copyright (C) 2006 god_at_hell
original CPU-Overclocker (c) by Hermes/PS2Reality
the gamma-routine was provided by theoddbot
parts (c) Rlyehs Work
gp2xminilib.c
GP2X minimal library v0.5 by rlyeh, 2005.
+ GP2X video library with double buffering.
+ GP2X soundring buffer library with double buffering.
+ GP2X joystick library.
*/
#include <Python.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#define SYS_CLK_FREQ 7372800
static int gp2x_dev_mem_fd = -1;
static volatile unsigned short *gp2x_memregs = 0;
/* If this is not called explicitly, it will be called implicitly. */
static void
init() {
if (gp2x_dev_mem_fd >= 0) {
/* Already initialised. */
return;
}
/* Get a pointer to the memory-mapped address for the GP2X control
registers. */
gp2x_dev_mem_fd = open("/dev/mem", O_RDWR);
gp2x_memregs = (unsigned short *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, gp2x_dev_mem_fd, 0xc0000000);
}
/* Call this to free up resources. */
static void
shutdown() {
if (gp2x_dev_mem_fd < 0) {
/* Already shut down. */
return;
}
munmap((void *)gp2x_memregs, 0x10000);
gp2x_memregs = NULL;
close(gp2x_dev_mem_fd);
gp2x_dev_mem_fd = -1;
}
/* Sets the GP2X CPU speed to the indicated value in MHz. Legal
values are in the range 33 .. 340, though values greater than 266
are overclock values and may crash the machine, depending on the
individual.
Note that there the GP2X also has a separate CPU divider which is
not modified by this function. */
static void
set_FCLK(unsigned int MHZ) {
unsigned int v;
unsigned int mdiv, pdiv = 3, scale = 0;
if (gp2x_dev_mem_fd < 0) {
/* Not initialised. */
printf("Not setting CPU speed to %u: not initialised.\n", MHZ);
return;
}
MHZ *= 1000000;
mdiv = (MHZ * pdiv) / SYS_CLK_FREQ;
mdiv = ((mdiv - 8) << 8) & 0xff00;
pdiv = ((pdiv - 2) << 2) & 0xfc;
scale &= 3;
v = mdiv | pdiv | scale;
gp2x_memregs[0x910 >> 1] = v;
}
/* Returns the current CPU speed in MHz. Note that the GP2X also has
a separate CPU divider which is not consulted by this function. */
static unsigned int
get_FCLK() {
unsigned v;
unsigned mdiv, pdiv, scale;
unsigned MHZ;
/* Implicitly initialise if necessary. */
init();
v = gp2x_memregs[0x910>>1];
mdiv = ((v & 0xff00) >> 8) + 8;
pdiv = ((v & 0xfc) >> 2) + 2;
scale = v & 3;
if (pdiv == 0) {
/* This presumably isn't possible. */
MHZ = 0;
} else {
MHZ = (mdiv * SYS_CLK_FREQ) / pdiv;
MHZ = (MHZ + 500000) / 1000000;
}
return MHZ;
}
static PyObject *
wrapper_init() {
init();
Py_RETURN_NONE;
}
static PyObject *
wrapper_shutdown() {
shutdown();
Py_RETURN_NONE;
}
static PyObject *
wrapper_set_FCLK(PyObject *self, PyObject *args, PyObject *kwds) {
static char *keyword_list[] = { "MHZ", NULL };
unsigned int MHZ;
if (!PyArg_ParseTupleAndKeywords(args, kwds,
"I:set_FCLK",
keyword_list, &MHZ)) {
return NULL;
}
set_FCLK(MHZ);
Py_RETURN_NONE;
}
static PyObject *
wrapper_get_FCLK() {
unsigned int MHZ;
MHZ = get_FCLK();
if ((long)MHZ < 0) {
return PyLong_FromUnsignedLong((unsigned long)MHZ);
} else {
return PyInt_FromLong((long)MHZ);
}
}
/* Returns a 3-tuple (x, y, tvout) representing the current screen
width and height, and true if the screen is in tv-out mode. */
static PyObject *
get_screen_info() {
int x, y, tvout;
/* Implicitly initialise if necessary. */
init();
x = gp2x_memregs[0x2816>>1] + 1;
y = gp2x_memregs[0x2818>>1] + 1;
tvout = (gp2x_memregs[0x2800>>1] & 0x100) != 0;
if (tvout && y < 400) {
/* Not sure why, but this is apparently off by a factor of two in
TV mode. */
y *= 2;
}
return Py_BuildValue("(iii)", x, y, tvout);
}
static PyMethodDef cpuctrl_methods[] = {
{"init", (PyCFunction)wrapper_init, METH_NOARGS },
{"shutdown", (PyCFunction)wrapper_shutdown, METH_NOARGS },
{"set_FCLK", (PyCFunction)wrapper_set_FCLK, METH_VARARGS | METH_KEYWORDS },
{"get_FCLK", (PyCFunction)wrapper_get_FCLK, METH_NOARGS },
{"get_screen_info", (PyCFunction)get_screen_info, METH_NOARGS },
{NULL} /* Sentinel */
};
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif
PyMODINIT_FUNC
init_cpuctrl(void) {
PyObject *m;
m = Py_InitModule3("_cpuctrl", cpuctrl_methods, NULL);
}