frontend: update libpicofe, fix missed callbacks
[pcsx_rearmed.git] / libpcsxcore / r3000a.c
CommitLineData
ef79bbde
P
1/***************************************************************************
2 * Copyright (C) 2007 Ryan Schultz, PCSX-df Team, PCSX team *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20/*
21* R3000A CPU functions.
22*/
23
24#include "r3000a.h"
25#include "cdrom.h"
26#include "mdec.h"
27#include "gte.h"
61ad2a61 28#include "psxinterpreter.h"
de74f599 29#include "psxbios.h"
9a0a61d2 30#include "psxevents.h"
905b7c25 31#include "../include/compiler_features.h"
92dc6b2f 32#include <assert.h>
33
34#ifndef ARRAY_SIZE
35#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
36#endif
ef79bbde
P
37
38R3000Acpu *psxCpu = NULL;
41e82ad4 39#ifdef DRC_DISABLE
ef79bbde 40psxRegisters psxRegs;
41e82ad4 41#endif
ef79bbde
P
42
43int psxInit() {
92dc6b2f 44 assert(PSXINT_COUNT <= ARRAY_SIZE(psxRegs.intCycle));
45 assert(ARRAY_SIZE(psxRegs.intCycle) == ARRAY_SIZE(psxRegs.event_cycles));
46
41e82ad4 47#ifndef DRC_DISABLE
ef79bbde
P
48 if (Config.Cpu == CPU_INTERPRETER) {
49 psxCpu = &psxInt;
50 } else psxCpu = &psxRec;
51#else
61ad2a61 52 Config.Cpu = CPU_INTERPRETER;
ef79bbde
P
53 psxCpu = &psxInt;
54#endif
55
56 Log = 0;
57
58 if (psxMemInit() == -1) return -1;
59
60 return psxCpu->Init();
61}
62
63void psxReset() {
14b3bd95 64 boolean introBypassed = FALSE;
60afad47 65 boolean oldhle = Config.HLE;
66
ef79bbde
P
67 psxMemReset();
68
69 memset(&psxRegs, 0, sizeof(psxRegs));
70
71 psxRegs.pc = 0xbfc00000; // Start in bootstrap
72
bc7c5acb 73 psxRegs.CP0.n.SR = 0x10600000; // COP0 enabled | BEV = 1 | TS = 1
74 psxRegs.CP0.n.PRid = 0x00000002; // PRevID = Revision ID, same as R3000A
7650b754 75 if (Config.HLE) {
76 psxRegs.CP0.n.SR |= 1u << 30; // COP2 enabled
77 psxRegs.CP0.n.SR &= ~(1u << 22); // RAM exception vector
78 }
ef79bbde 79
60afad47 80 if (Config.HLE != oldhle) {
81 // at least ari64 drc compiles differently so hard reset
82 psxCpu->Shutdown();
83 psxCpu->Init();
84 }
d5aeda23 85 psxCpu->ApplyConfig();
c24732c0 86 psxCpu->Reset();
87
ef79bbde
P
88 psxHwReset();
89 psxBiosInit();
90
7b75929b 91 if (!Config.HLE) {
ef79bbde 92 psxExecuteBios();
14b3bd95 93 if (psxRegs.pc == 0x80030000 && !Config.SlowBoot) {
02b1a085 94 introBypassed = BiosBootBypass();
14b3bd95 95 }
7b75929b 96 }
14b3bd95 97 if (Config.HLE || introBypassed)
98 psxBiosSetupBootState();
ef79bbde
P
99
100#ifdef EMU_LOG
101 EMU_LOG("*BIOS END*\n");
102#endif
103 Log = 0;
104}
105
106void psxShutdown() {
ef79bbde
P
107 psxBiosShutdown();
108
109 psxCpu->Shutdown();
7a8d521f 110
111 psxMemShutdown();
ef79bbde
P
112}
113
6d75addf 114// cp0 is passed separately for lightrec to be less messy
bc7c5acb 115void psxException(u32 cause, enum R3000Abdt bdt, psxCP0Regs *cp0) {
905b7c25 116 u32 opcode = intFakeFetch(psxRegs.pc);
943a507a 117
3d1c03e7 118 if (unlikely(!Config.HLE && (opcode >> 25) == 0x25)) {
665e364a 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)
62656449 122 // so we execute it here
bc7c5acb 123 psxCP2Regs *cp2 = (psxCP2Regs *)(cp0 + 1);
905b7c25 124 psxRegs.code = opcode;
125 psxCP2[opcode & 0x3f](cp2);
665e364a 126 }
127
ef79bbde 128 // Set the Cause
0b1da491 129 cp0->n.Cause = (bdt << 30) | (cp0->n.Cause & 0x700) | cause;
ef79bbde
P
130
131 // Set the EPC & PC
bc7c5acb 132 cp0->n.EPC = bdt ? psxRegs.pc - 4 : psxRegs.pc;
ef79bbde 133
bc7c5acb 134 if (cp0->n.SR & 0x400000)
ef79bbde
P
135 psxRegs.pc = 0xbfc00180;
136 else
137 psxRegs.pc = 0x80000080;
138
bc7c5acb 139 // Set the SR
140 cp0->n.SR = (cp0->n.SR & ~0x3f) | ((cp0->n.SR & 0x0f) << 2);
ef79bbde
P
141}
142
143void psxBranchTest() {
92dc6b2f 144 if ((psxRegs.cycle - psxRegs.psxNextsCounter) >= psxRegs.psxNextCounter)
ef79bbde
P
145 psxRcntUpdate();
146
9a0a61d2 147 irq_test(&psxRegs.CP0);
ef79bbde 148
9a0a61d2 149 if (unlikely(psxRegs.pc == psxRegs.biosBranchCheck))
de74f599 150 psxBiosCheckBranch();
ef79bbde
P
151}
152
153void psxJumpTest() {
154 if (!Config.HLE && Config.PsxOut) {
155 u32 call = psxRegs.GPR.n.t1 & 0xff;
156 switch (psxRegs.pc & 0x1fffff) {
157 case 0xa0:
158#ifdef PSXBIOS_LOG
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); }
161#endif
162 if (biosA0[call])
163 biosA0[call]();
164 break;
165 case 0xb0:
166#ifdef PSXBIOS_LOG
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); }
169#endif
170 if (biosB0[call])
171 biosB0[call]();
172 break;
173 case 0xc0:
174#ifdef PSXBIOS_LOG
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);
176#endif
177 if (biosC0[call])
178 biosC0[call]();
179 break;
180 }
181 }
182}
183
60afad47 184int psxExecuteBiosEnded(void) {
185 return (psxRegs.pc & 0xff800000) == 0x80000000;
186}
187
ef79bbde 188void psxExecuteBios() {
7b75929b 189 int i;
da65071f 190 for (i = 0; i < 5000000; i++) {
c87406ff 191 psxCpu->ExecuteBlock(&psxRegs, EXEC_CALLER_BOOT);
60afad47 192 if (psxExecuteBiosEnded())
da65071f 193 break;
194 }
195 if (psxRegs.pc != 0x80030000)
196 SysPrintf("non-standard BIOS detected (%d, %08x)\n", i, psxRegs.pc);
ef79bbde
P
197}
198
11d23573 199// irq10 stuff, very preliminary
200static int irq10count;
201
202static void psxScheduleIrq10One(u32 cycles_abs) {
203 // schedule relative to frame start
204 u32 c = cycles_abs - rcnts[3].cycleStart;
205 assert((s32)c >= 0);
206 psxRegs.interrupt |= 1 << PSXINT_IRQ10;
207 psxRegs.intCycle[PSXINT_IRQ10].cycle = c;
208 psxRegs.intCycle[PSXINT_IRQ10].sCycle = rcnts[3].cycleStart;
9a0a61d2 209 set_event_raw_abs(PSXINT_IRQ10, cycles_abs);
11d23573 210}
211
212void irq10Interrupt() {
213 u32 prevc = psxRegs.intCycle[PSXINT_IRQ10].sCycle
214 + psxRegs.intCycle[PSXINT_IRQ10].cycle;
215
216 psxHu32ref(0x1070) |= SWAPu32(0x400);
217
218#if 0
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));
224#endif
1351a8fb 225 if (--irq10count > 0) {
226 u32 cycles_per_line = Config.PsxType
227 ? PSXCLK / 50 / 314 : PSXCLK / 60 / 263;
228 psxScheduleIrq10One(prevc + cycles_per_line);
229 }
11d23573 230}
231
232void 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);
237 cycles += x_cycles;
238 psxScheduleIrq10One(cycles);
239 irq10count = irq_count;
240}