1 |
/* |
2 |
* PearPC |
3 |
* ppc_cpu.cc |
4 |
* |
5 |
* Copyright (C) 2003 Sebastian Biallas (sb@biallas.net) |
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 version 2 as |
9 |
* published by the Free Software Foundation. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
20 |
|
21 |
#include <cerrno> |
22 |
#include <cstring> |
23 |
|
24 |
#include "debug/tracers.h" |
25 |
#include "system/sys.h" |
26 |
#include "system/sysclk.h" |
27 |
#include "system/systhread.h" |
28 |
#include "system/systimer.h" |
29 |
#include "ppc_cpu.h" |
30 |
#include "ppc_dec.h" |
31 |
#include "ppc_mmu.h" |
32 |
#include "jitc.h" |
33 |
#include "jitc_asm.h" |
34 |
#include "jitc_debug.h" |
35 |
|
36 |
PPC_CPU_State gCPU; |
37 |
bool gSinglestep = false; |
38 |
uint32 gBreakpoint; |
39 |
uint32 gBreakpoint2; |
40 |
|
41 |
extern "C" void ppc_display_jitc_stats() |
42 |
{ |
43 |
ht_printf("pg.dest: write: %qd out of pages: %qd out of tc: %qd\r", &gJITC.destroy_write, &gJITC.destroy_oopages, &gJITC.destroy_ootc); |
44 |
} |
45 |
|
46 |
void ppc_fpu_test(); |
47 |
|
48 |
uint64 gJITCCompileTicks; |
49 |
uint64 gJITCRunTicks; |
50 |
uint64 gJITCRunTicksStart; |
51 |
|
52 |
uint64 gClientClockFrequency; |
53 |
uint64 gClientBusFrequency; |
54 |
uint64 gClientTimeBaseFrequency; |
55 |
uint64 gStartHostCLKTicks; |
56 |
uint64 gTBreadITB; |
57 |
int gHostClockScale; |
58 |
|
59 |
uint64 ppc_get_cpu_ideal_timebase() |
60 |
{ |
61 |
uint64 ticks = sys_get_hiresclk_ticks(); |
62 |
if (gHostClockScale < 0) { |
63 |
// negative shift count -> make it positive |
64 |
return (ticks - gStartHostCLKTicks) >> (-gHostClockScale); |
65 |
} else { |
66 |
return (ticks - gStartHostCLKTicks) << gHostClockScale; |
67 |
} |
68 |
} |
69 |
|
70 |
uint64 ppc_get_cpu_timebase() |
71 |
{ |
72 |
uint64 ticks = sys_get_hiresclk_ticks(); |
73 |
if (gHostClockScale < 0) { |
74 |
gCPU.tb += (ticks - gTBreadITB) >> (-gHostClockScale); |
75 |
} else { |
76 |
gCPU.tb += (ticks - gTBreadITB) << gHostClockScale; |
77 |
} |
78 |
|
79 |
gTBreadITB = ticks; |
80 |
return gCPU.tb; |
81 |
} |
82 |
|
83 |
sys_timer gDECtimer; |
84 |
sys_semaphore gCPUDozeSem; |
85 |
|
86 |
extern "C" void cpu_doze() |
87 |
{ |
88 |
sys_lock_semaphore(gCPUDozeSem); |
89 |
if (!gCPU.exception_pending) sys_wait_semaphore_bounded(gCPUDozeSem, 10); |
90 |
sys_unlock_semaphore(gCPUDozeSem); |
91 |
} |
92 |
|
93 |
void ppc_cpu_wakeup() |
94 |
{ |
95 |
sys_signal_semaphore(gCPUDozeSem); |
96 |
} |
97 |
|
98 |
static void decTimerCB(sys_timer t) |
99 |
{ |
100 |
ppc_cpu_atomic_raise_dec_exception(); |
101 |
// cpu_wakeup(); |
102 |
} |
103 |
|
104 |
void ppc_cpu_run() |
105 |
{ |
106 |
// ppc_fpu_test(); |
107 |
// return; |
108 |
gJITCRunTicks = 0; |
109 |
gJITCCompileTicks = 0; |
110 |
gJITCRunTicksStart = jitcDebugGetTicks(); |
111 |
PPC_CPU_TRACE("execution started at %08x\n", gCPU.pc); |
112 |
jitcDebugInit(); |
113 |
/* |
114 |
PPC_CPU_WARN("clock ticks / second = %08qx\n", q); |
115 |
q = sys_get_cpu_ticks(); |
116 |
PPC_CPU_WARN("ticks = %08qx\n", q); |
117 |
q = sys_get_cpu_ticks(); |
118 |
PPC_CPU_WARN("ticks = %08qx\n", q); |
119 |
q = sys_get_cpu_ticks(); |
120 |
PPC_CPU_WARN("ticks = %08qx\n", q);*/ |
121 |
|
122 |
if (!sys_create_timer(&gDECtimer, decTimerCB)) { |
123 |
ht_printf("Unable to create timer\n"); |
124 |
exit(1); |
125 |
} |
126 |
ppc_start_jitc_asm(gCPU.pc); |
127 |
} |
128 |
|
129 |
void ppc_cpu_map_framebuffer(uint32 pa, uint32 ea) |
130 |
{ |
131 |
// use BAT for framebuffer |
132 |
gCPU.dbatu[0] = ea|(7<<2)|0x3; |
133 |
gCPU.dbatl[0] = pa; |
134 |
|
135 |
gCPU.dbat_bl[0] = (~gCPU.dbatu[0] << 15) & 0xfffe0000; |
136 |
gCPU.dbat_nbl[0] = ~gCPU.dbat_bl[0]; |
137 |
|
138 |
gCPU.dbat_bepi[0] = gCPU.dbatu[0] & gCPU.dbat_bl[0]; |
139 |
gCPU.dbat_brpn[0] = gCPU.dbatl[0] & gCPU.dbat_bl[0]; |
140 |
} |
141 |
|
142 |
|
143 |
void ppc_cpu_stop() |
144 |
{ |
145 |
gCPU.exception_pending = true; |
146 |
gCPU.stop_exception = true; |
147 |
} |
148 |
|
149 |
uint64 ppc_get_clock_frequency(int cpu) |
150 |
{ |
151 |
return gClientClockFrequency; |
152 |
} |
153 |
|
154 |
uint64 ppc_get_bus_frequency(int cpu) |
155 |
{ |
156 |
return gClientBusFrequency; |
157 |
} |
158 |
|
159 |
uint64 ppc_get_timebase_frequency(int cpu) |
160 |
{ |
161 |
return gClientTimeBaseFrequency; |
162 |
} |
163 |
|
164 |
|
165 |
void ppc_machine_check_exception() |
166 |
{ |
167 |
PPC_CPU_ERR("machine check exception\n"); |
168 |
} |
169 |
|
170 |
uint32 ppc_cpu_get_gpr(int cpu, int i) |
171 |
{ |
172 |
return gCPU.gpr[i]; |
173 |
} |
174 |
|
175 |
void ppc_cpu_set_gpr(int cpu, int i, uint32 newvalue) |
176 |
{ |
177 |
gCPU.gpr[i] = newvalue; |
178 |
} |
179 |
|
180 |
void ppc_cpu_set_msr(int cpu, uint32 newvalue) |
181 |
{ |
182 |
gCPU.msr = newvalue; |
183 |
} |
184 |
|
185 |
void ppc_cpu_set_pc(int cpu, uint32 newvalue) |
186 |
{ |
187 |
gCPU.pc = newvalue; |
188 |
} |
189 |
|
190 |
uint32 ppc_cpu_get_pc(int cpu) |
191 |
{ |
192 |
return gCPU.pc_ofs + gCPU.current_code_base; |
193 |
} |
194 |
|
195 |
uint32 ppc_cpu_get_pvr(int cpu) |
196 |
{ |
197 |
return gCPU.pvr; |
198 |
} |
199 |
|
200 |
uint32 ppc_cpu_get_lr(int cpu) |
201 |
{ |
202 |
return gCPU.lr; |
203 |
} |
204 |
|
205 |
|
206 |
void ppc_set_singlestep_v(bool v, const char *file, int line, const char *format, ...) |
207 |
{ |
208 |
va_list arg; |
209 |
va_start(arg, format); |
210 |
ht_fprintf(stdout, "singlestep %s from %s:%d, info: ", v ? "set" : "cleared", file, line); |
211 |
ht_vfprintf(stdout, format, arg); |
212 |
ht_fprintf(stdout, "\n"); |
213 |
va_end(arg); |
214 |
gSinglestep = v; |
215 |
} |
216 |
|
217 |
void ppc_set_singlestep_nonverbose(bool v) |
218 |
{ |
219 |
gSinglestep = v; |
220 |
} |
221 |
|
222 |
#define CPU_KEY_PVR "cpu_pvr" |
223 |
|
224 |
#include "configparser.h" |
225 |
|
226 |
bool ppc_cpu_init() |
227 |
{ |
228 |
memset(&gCPU, 0, sizeof gCPU); |
229 |
gCPU.pvr = gConfig->getConfigInt(CPU_KEY_PVR); |
230 |
|
231 |
ppc_dec_init(); |
232 |
// initialize srs (mostly for prom) |
233 |
for (int i=0; i<16; i++) { |
234 |
gCPU.sr[i] = 0x2aa*i; |
235 |
} |
236 |
|
237 |
gCPU.x87cw = 0x37f; |
238 |
|
239 |
sys_create_semaphore(&gCPUDozeSem); |
240 |
|
241 |
gStartHostCLKTicks = sys_get_hiresclk_ticks(); |
242 |
uint64 q = sys_get_hiresclk_ticks_per_second(); |
243 |
gHostClockScale = 0; |
244 |
while (q < PPC_TIMEBASE_FREQUENCY) { |
245 |
gHostClockScale++; |
246 |
q <<= 1; |
247 |
} |
248 |
while (q > (PPC_TIMEBASE_FREQUENCY*2)) { |
249 |
gHostClockScale--; |
250 |
q >>= 1; |
251 |
} |
252 |
gClientTimeBaseFrequency = q; |
253 |
gClientBusFrequency = gClientTimeBaseFrequency * 4; |
254 |
gClientClockFrequency = gClientBusFrequency * 5; |
255 |
|
256 |
return jitc_init(4096, 32*1024*1024); |
257 |
} |
258 |
|
259 |
void ppc_cpu_init_config() |
260 |
{ |
261 |
gConfig->acceptConfigEntryIntDef("cpu_pvr", 0x000c0201); |
262 |
} |