1 /***************************************************************************
2 * Copyright (C) 2007 Ryan Schultz, PCSX-df Team, PCSX team *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA. *
18 ***************************************************************************/
21 * R3000A CPU functions.
28 #include "psxinterpreter.h"
30 #include "psxevents.h"
31 #include "../include/compiler_features.h"
35 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
38 R3000Acpu *psxCpu = NULL;
44 assert(PSXINT_COUNT <= ARRAY_SIZE(psxRegs.intCycle));
45 assert(ARRAY_SIZE(psxRegs.intCycle) == ARRAY_SIZE(psxRegs.event_cycles));
48 if (Config.Cpu == CPU_INTERPRETER) {
50 } else psxCpu = &psxRec;
52 Config.Cpu = CPU_INTERPRETER;
58 if (psxMemInit() == -1) return -1;
60 return psxCpu->Init();
64 boolean introBypassed = FALSE;
65 boolean oldhle = Config.HLE;
69 memset(&psxRegs, 0, sizeof(psxRegs));
71 psxRegs.pc = 0xbfc00000; // Start in bootstrap
73 psxRegs.CP0.n.SR = 0x10600000; // COP0 enabled | BEV = 1 | TS = 1
74 psxRegs.CP0.n.PRid = 0x00000002; // PRevID = Revision ID, same as R3000A
76 psxRegs.CP0.n.SR |= 1u << 30; // COP2 enabled
77 psxRegs.CP0.n.SR &= ~(1u << 22); // RAM exception vector
80 if (Config.HLE != oldhle) {
81 // at least ari64 drc compiles differently so hard reset
85 psxCpu->ApplyConfig();
93 if (psxRegs.pc == 0x80030000 && !Config.SlowBoot) {
94 introBypassed = BiosBootBypass();
97 if (Config.HLE || introBypassed)
98 psxBiosSetupBootState();
101 EMU_LOG("*BIOS END*\n");
114 // cp0 is passed separately for lightrec to be less messy
115 void psxException(u32 cause, enum R3000Abdt bdt, psxCP0Regs *cp0) {
116 u32 opcode = intFakeFetch(psxRegs.pc);
118 if (unlikely(!Config.HLE && (opcode >> 25) == 0x25)) {
119 // "hokuto no ken" / "Crash Bandicot 2" ...
120 // BIOS does not allow to return to GTE instructions
121 // (just skips it, supposedly because it's scheduled already)
122 // so we execute it here
123 psxCP2Regs *cp2 = (psxCP2Regs *)(cp0 + 1);
124 psxRegs.code = opcode;
125 psxCP2[opcode & 0x3f](cp2);
129 cp0->n.Cause = (bdt << 30) | (cp0->n.Cause & 0x700) | cause;
132 cp0->n.EPC = bdt ? psxRegs.pc - 4 : psxRegs.pc;
134 if (cp0->n.SR & 0x400000)
135 psxRegs.pc = 0xbfc00180;
137 psxRegs.pc = 0x80000080;
140 cp0->n.SR = (cp0->n.SR & ~0x3f) | ((cp0->n.SR & 0x0f) << 2);
143 void psxBranchTest() {
144 if ((psxRegs.cycle - psxRegs.psxNextsCounter) >= psxRegs.psxNextCounter)
147 irq_test(&psxRegs.CP0);
149 if (unlikely(psxRegs.pc == psxRegs.biosBranchCheck))
150 psxBiosCheckBranch();
154 if (!Config.HLE && Config.PsxOut) {
155 u32 call = psxRegs.GPR.n.t1 & 0xff;
156 switch (psxRegs.pc & 0x1fffff) {
159 if (call != 0x28 && call != 0xe) {
160 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); }
167 if (call != 0x17 && call != 0xb) {
168 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); }
175 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);
184 int psxExecuteBiosEnded(void) {
185 return (psxRegs.pc & 0xff800000) == 0x80000000;
188 void psxExecuteBios() {
190 for (i = 0; i < 5000000; i++) {
191 psxCpu->ExecuteBlock(&psxRegs, EXEC_CALLER_BOOT);
192 if (psxExecuteBiosEnded())
195 if (psxRegs.pc != 0x80030000)
196 SysPrintf("non-standard BIOS detected (%d, %08x)\n", i, psxRegs.pc);
199 // irq10 stuff, very preliminary
200 static int irq10count;
202 static void psxScheduleIrq10One(u32 cycles_abs) {
203 // schedule relative to frame start
204 u32 c = cycles_abs - rcnts[3].cycleStart;
206 psxRegs.interrupt |= 1 << PSXINT_IRQ10;
207 psxRegs.intCycle[PSXINT_IRQ10].cycle = c;
208 psxRegs.intCycle[PSXINT_IRQ10].sCycle = rcnts[3].cycleStart;
209 set_event_raw_abs(PSXINT_IRQ10, cycles_abs);
212 void irq10Interrupt() {
213 u32 prevc = psxRegs.intCycle[PSXINT_IRQ10].sCycle
214 + psxRegs.intCycle[PSXINT_IRQ10].cycle;
216 psxHu32ref(0x1070) |= SWAPu32(0x400);
219 s32 framec = psxRegs.cycle - rcnts[3].cycleStart;
220 printf("%d:%03d irq10 #%d %3d m=%d,%d\n", frame_counter,
221 (s32)((float)framec / (PSXCLK / 60 / 263.0f)),
222 irq10count, psxRegs.cycle - prevc,
223 (psxRegs.CP0.n.SR & 0x401) != 0x401, !(psxHu32(0x1074) & 0x400));
225 if (--irq10count > 0) {
226 u32 cycles_per_line = Config.PsxType
227 ? PSXCLK / 50 / 314 : PSXCLK / 60 / 263;
228 psxScheduleIrq10One(prevc + cycles_per_line);
232 void psxScheduleIrq10(int irq_count, int x_cycles, int y) {
233 //printf("%s %d, %d, %d\n", __func__, irq_count, x_cycles, y);
234 u32 cycles_per_frame = Config.PsxType ? PSXCLK / 50 : PSXCLK / 60;
235 u32 cycles = rcnts[3].cycleStart + cycles_per_frame;
236 cycles += y * cycles_per_frame / (Config.PsxType ? 314 : 263);
238 psxScheduleIrq10One(cycles);
239 irq10count = irq_count;