forked from xjsxjs197/WiiSXRX_2022
-
Notifications
You must be signed in to change notification settings - Fork 2
/
r3000a.c
284 lines (249 loc) · 8.79 KB
/
r3000a.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/***************************************************************************
* Copyright (C) 2007 Ryan Schultz, PCSX-df Team, PCSX team *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA. *
***************************************************************************/
/*
* R3000A CPU functions.
*/
#include "r3000a.h"
#include "psxhw.h"
#include "psxdma.h"
#include "cdrom.h"
#include "mdec.h"
#include "psxinterpreter.h"
#include "Gamecube/wiiSXconfig.h"
#include "Gamecube/DEBUG.h"
R3000Acpu *psxCpu;
psxRegisters psxRegs;
extern bool needInitCpu;
int psxInit() {
if (Config.Cpu == DYNACORE_INTERPRETER) {
psxCpu = &psxInt;
}
#if defined(__x86_64__) || defined(__i386__) || defined(__sh__) || defined(__ppc__) || defined(HW_RVL) || defined(HW_DOL)
if (Config.Cpu == DYNACORE_DYNAREC)
{
psxCpu = &psxLightrec;
}
if (Config.Cpu == DYNACORE_DYNAREC_OLD)
{
psxCpu = &psxRec;
}
#endif
Log=0;
int memInitResult = psxMemInit();
if (memInitResult != 0) return memInitResult;
if (needInitCpu)
{
return psxCpu->Init();
}
else
{
return 0;
}
}
void psxReset() {
bool introBypassed = FALSE;
psxMemReset();
memset(&psxRegs, 0, sizeof(psxRegs));
psxRegs.pc = 0xbfc00000; // Start in bootstrap
psxRegs.CP0.n.SR = 0x10600000; // COP0 enabled | BEV = 1 | TS = 1
psxRegs.CP0.n.PRid = 0x00000002; // PRevID = Revision ID, same as R3000A
if (Config.HLE) {
psxRegs.CP0.n.SR |= 1u << 30; // COP2 enabled
psxRegs.CP0.n.SR &= ~(1u << 22); // RAM exception vector
}
psxCpu->ApplyConfig();
psxCpu->Reset();
psxHwReset();
psxBiosInit();
// The automatic correction of pR3000Fix may have an impact on the execution of Bios,
// so we will restore it first
long tmpVal = Config.pR3000Fix;
Config.pR3000Fix = 0;
if (!Config.HLE) {
psxExecuteBios();
if (psxRegs.pc == 0x80030000 && LoadCdBios == BOOTTHRUBIOS_NO) {
introBypassed = BiosBootBypass();
}
}
if (Config.HLE || introBypassed)
psxBiosSetupBootState();
// Set the value of pR3000Fix after the completion of BIOS execution
Config.pR3000Fix = tmpVal;
#ifdef EMU_LOG
EMU_LOG("*BIOS END*\n");
#endif
Log = 0;
}
void psxShutdown() {
psxBiosShutdown();
psxCpu->Shutdown();
psxMemShutdown();
}
// cp0 is passed separately for lightrec to be less messy
void psxException(u32 cause, enum R3000Abdt bdt, psxCP0Regs *cp0) {
u32 opcode = intFakeFetch(psxRegs.pc);
if (unlikely(!Config.HLE && (opcode >> 25) == 0x25)) {
// "hokuto no ken" / "Crash Bandicot 2" ...
// BIOS does not allow to return to GTE instructions
// (just skips it, supposedly because it's scheduled already)
// so we execute it here
psxCP2Regs *cp2 = (psxCP2Regs *)(cp0 + 1);
psxRegs.code = opcode;
extern void (*psxCP2[64])(struct psxCP2Regs *regs);
psxCP2[opcode & 0x3f](cp2);
}
// Set the Cause
cp0->n.Cause = (bdt << 30) | (cp0->n.Cause & 0x700) | cause;
// Set the EPC & PC
cp0->n.EPC = bdt ? psxRegs.pc - 4 : psxRegs.pc;
if (cp0->n.SR & 0x400000)
psxRegs.pc = 0xbfc00180;
else
psxRegs.pc = 0x80000080;
// Set the SR
cp0->n.SR = (cp0->n.SR & ~0x3f) | ((cp0->n.SR & 0x0f) << 2);
}
extern u32 psxNextCounter, psxNextsCounter;
void psxBranchTest() {
if ((psxRegs.cycle - psxNextsCounter) >= psxNextCounter)
psxRcntUpdate();
if (psxRegs.interrupt) {
if ((psxRegs.interrupt & (1 << PSXINT_SIO))) { // sio
if ((psxRegs.cycle - psxRegs.intCycle[PSXINT_SIO].sCycle) >= psxRegs.intCycle[PSXINT_SIO].cycle) {
psxRegs.interrupt &= ~(1 << PSXINT_SIO);
sioInterrupt();
}
}
if (psxRegs.interrupt & (1 << PSXINT_CDR)) { // cdr
if ((psxRegs.cycle - psxRegs.intCycle[PSXINT_CDR].sCycle) >= psxRegs.intCycle[PSXINT_CDR].cycle) {
psxRegs.interrupt &= ~(1 << PSXINT_CDR);
cdrInterrupt();
}
}
if (psxRegs.interrupt & (1 << PSXINT_CDREAD)) { // cdr read
if ((psxRegs.cycle - psxRegs.intCycle[PSXINT_CDREAD].sCycle) >= psxRegs.intCycle[PSXINT_CDREAD].cycle) {
psxRegs.interrupt &= ~(1 << PSXINT_CDREAD);
cdrPlayReadInterrupt();
}
}
if (psxRegs.interrupt & (1 << PSXINT_GPUDMA)) { // gpu dma
if ((psxRegs.cycle - psxRegs.intCycle[PSXINT_GPUDMA].sCycle) >= psxRegs.intCycle[PSXINT_GPUDMA].cycle) {
psxRegs.interrupt &= ~(1 << PSXINT_GPUDMA);
gpuInterrupt();
}
}
if (psxRegs.interrupt & (1 << PSXINT_MDECOUTDMA)) { // mdec out dma
if ((psxRegs.cycle - psxRegs.intCycle[PSXINT_MDECOUTDMA].sCycle) >= psxRegs.intCycle[PSXINT_MDECOUTDMA].cycle) {
psxRegs.interrupt &= ~(1 << PSXINT_MDECOUTDMA);
mdec1Interrupt();
}
}
if (psxRegs.interrupt & (1 << PSXINT_SPUDMA)) { // spu dma
if ((psxRegs.cycle - psxRegs.intCycle[PSXINT_SPUDMA].sCycle) >= psxRegs.intCycle[PSXINT_SPUDMA].cycle) {
psxRegs.interrupt &= ~(1 << PSXINT_SPUDMA);
spuInterrupt();
}
}
if (psxRegs.interrupt & (1 << PSXINT_MDECINDMA)) { // mdec in
if ((psxRegs.cycle - psxRegs.intCycle[PSXINT_MDECINDMA].sCycle) >= psxRegs.intCycle[PSXINT_MDECINDMA].cycle) {
psxRegs.interrupt &= ~(1 << PSXINT_MDECINDMA);
mdec0Interrupt();
}
}
if (psxRegs.interrupt & (1 << PSXINT_GPUOTCDMA)) { // gpu otc
if ((psxRegs.cycle - psxRegs.intCycle[PSXINT_GPUOTCDMA].sCycle) >= psxRegs.intCycle[PSXINT_GPUOTCDMA].cycle) {
psxRegs.interrupt &= ~(1 << PSXINT_GPUOTCDMA);
gpuotcInterrupt();
}
}
if (psxRegs.interrupt & (1 << PSXINT_CDRDMA)) { // cdrom
if ((psxRegs.cycle - psxRegs.intCycle[PSXINT_CDRDMA].sCycle) >= psxRegs.intCycle[PSXINT_CDRDMA].cycle) {
psxRegs.interrupt &= ~(1 << PSXINT_CDRDMA);
cdrDmaInterrupt();
}
}
// if (psxRegs.interrupt & (1 << PSXINT_CDRPLAY)) { // cdr play timing
// if ((psxRegs.cycle - psxRegs.intCycle[PSXINT_CDRPLAY].sCycle) >= psxRegs.intCycle[PSXINT_CDRPLAY].cycle) {
// psxRegs.interrupt &= ~(1 << PSXINT_CDRPLAY);
// cdrPlayInterrupt();
// }
// }
if (psxRegs.interrupt & (1 << PSXINT_CDRLID)) { // cdr lid states
if ((psxRegs.cycle - psxRegs.intCycle[PSXINT_CDRLID].sCycle) >= psxRegs.intCycle[PSXINT_CDRLID].cycle) {
psxRegs.interrupt &= ~(1 << PSXINT_CDRLID);
cdrLidSeekInterrupt();
}
}
if (psxRegs.interrupt & (1 << PSXINT_SPU_UPDATE)) { // scheduled spu update
if ((psxRegs.cycle - psxRegs.intCycle[PSXINT_SPU_UPDATE].sCycle) >= psxRegs.intCycle[PSXINT_SPU_UPDATE].cycle) {
psxRegs.interrupt &= ~(1 << PSXINT_SPU_UPDATE);
spuUpdate();
}
}
// if (psxRegs.interrupt & 0x80000000) {
// psxRegs.interrupt&=~0x80000000;
// psxTestHWInts();
// }
}
psxRegs.CP0.n.Cause &= ~0x400;
if (psxHu32(0x1070) & psxHu32(0x1074))
psxRegs.CP0.n.Cause |= 0x400;
if (((psxRegs.CP0.n.Cause | 1) & psxRegs.CP0.n.SR & 0x401) == 0x401)
psxException(0, 0, &psxRegs.CP0);
}
void psxJumpTest() {
if (!Config.HLE && Config.PsxOut) {
u32 call = psxRegs.GPR.n.t1 & 0xff;
switch (psxRegs.pc & 0x1fffff) {
case 0xa0:
#ifdef PSXBIOS_LOG
if (call != 0x28 && call != 0xe) {
PSXBIOS_LOG("Bios call a0: %s (%x) %x,%x,%x,%x\n", biosA0n[call], call, psxRegs.GPR.n.a0, psxRegs.GPR.n.a1, psxRegs.GPR.n.a2, psxRegs.GPR.n.a3); }
#endif
if (biosA0[call])
biosA0[call]();
break;
case 0xb0:
#ifdef PSXBIOS_LOG
if (call != 0x17 && call != 0xb) {
PSXBIOS_LOG("Bios call b0: %s (%x) %x,%x,%x,%x\n", biosB0n[call], call, psxRegs.GPR.n.a0, psxRegs.GPR.n.a1, psxRegs.GPR.n.a2, psxRegs.GPR.n.a3); }
#endif
if (biosB0[call])
biosB0[call]();
break;
case 0xc0:
#ifdef PSXBIOS_LOG
PSXBIOS_LOG("Bios call c0: %s (%x) %x,%x,%x,%x\n", biosC0n[call], call, psxRegs.GPR.n.a0, psxRegs.GPR.n.a1, psxRegs.GPR.n.a2, psxRegs.GPR.n.a3);
#endif
if (biosC0[call])
biosC0[call]();
break;
}
}
}
void psxExecuteBios() {
int i;
for (i = 0; i < 5000000; i++) {
psxCpu->ExecuteBlock(EXEC_CALLER_BOOT);
if ((psxRegs.pc & 0xff800000) == 0x80000000)
break;
}
if (psxRegs.pc != 0x80030000)
SysPrintf("non-standard BIOS detected (%d, %08x)\n", i, psxRegs.pc);
}