Launcher, based on PickleLauncher
[mupen64plus-pandora.git] / source / mupen64launcher / src / csystem.cpp
1 /**
2  *  @section LICENSE
3  *
4  *  PickleLauncher
5  *  Copyright (C) 2010-2011 Scott Smith
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  *  @section LOCATION
21  */
22
23 #include "csystem.h"
24
25 CSystem::CSystem() : CBase()
26 {
27 #if defined(GP2X) || defined(WIZ) || defined(CAANOO)
28     memdev = open( "/dev/mem", O_RDWR );
29     if (memdev == 0)
30     {
31         Log( "Could not open /dev/mem\n" );
32     }
33     else
34     {
35         memregs = (uint32_t*)mmap( 0, MMAP_ADDRESS, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000 );
36
37         if (memregs == MAP_FAILED)
38         {
39             Log( "Could not mmap hardware registers!\n" );
40             close(memdev);
41         }
42     }
43 #endif
44 }
45
46 CSystem::~CSystem()
47 {
48 #if defined(GP2X) || defined(WIZ) || defined(CAANOO)
49     if (memdev != 0)
50     {
51         memregs = NULL;
52     }
53     close(memdev);
54 #endif
55 }
56
57 void CSystem::SetCPUClock( uint16_t& mhz )
58 {
59     // Range check
60     if (mhz == 0 || mhz > CPU_CLOCK_MAX)
61     {
62         Log( "CPU mhz out of range, resetting to default. Value is now %d and allowed values should be between 0 and %d Mhz.\n", mhz, CPU_CLOCK_MAX );
63         mhz = CPU_CLOCK_DEF;
64     }
65
66 #if defined(PANDORA)
67     string command = "/usr/bin/sudo cpuset " + i_to_a(mhz);
68     execlp( command.c_str(), command.c_str(), NULL, NULL, NULL );
69
70 #elif defined(WIZ) || defined(CAANOO)
71     if (memdev != 0 && memregs != 0)
72     {
73         volatile uint32_t *memregl = static_cast<volatile uint32_t*>((volatile void*)memregs);
74         uint32_t mdiv, pdiv = 9, sdiv = 0;
75         uint32_t v;
76
77         mdiv = (mhz * pdiv) / SYS_CLK_FREQ;
78         if (mdiv & ~0x3ff) return;
79         v = pdiv<<18 | mdiv<<8 | sdiv;
80
81         PLLSETREG0 = v;
82         PWRMODE |= 0x8000;
83         for (int i = 0; (PWRMODE & 0x8000) && i < 0x100000; i++);
84     }
85
86 #elif defined(GP2X)
87     if (memdev != 0 && memregs != 0)
88     {
89         uint32_t v;
90         uint32_t mdiv, pdiv=3, scale=0;
91
92         mhz *= 1000000;
93         mdiv = (mhz * pdiv) / SYS_CLK_FREQ;
94         mdiv = ((mdiv-8)<<8) & 0xff00;
95         pdiv = ((pdiv-2)<<2) & 0xfc;
96         scale &= 3;
97         v = mdiv | pdiv | scale;
98         MEM_REG[0x910>>1] = v;
99     }
100
101 #else
102     Log( "Setting CPU Clock not supported on this machine.\n" );
103 #endif
104 }