1 /***************************************************************************
2 * Copyright (C) 2007 Ryan Schultz, PCSX-df Team, PCSX team *
3 * Copyright (C) 2023 notaz *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA. *
19 ***************************************************************************/
22 * PSX assembly interpreter.
25 #include "psxcommon.h"
29 #include "psxinterpreter.h"
32 #include "../include/compiler_features.h"
34 // these may cause issues: because of poor timing we may step
35 // on instructions that real hardware would never reach
36 #define DO_EXCEPTION_RESERVEDI
37 #define HANDLE_LOAD_DELAY
40 #define INT_ATTR __attribute__((regparm(2)))
45 #define INVALID_PTR NULL
49 static void (INT_ATTR *psxBSC[64])(psxRegisters *regs_, u32 code);
50 static void (INT_ATTR *psxSPC[64])(psxRegisters *regs_, u32 code);
53 static void doLoad(psxRegisters *regs, u32 r, u32 val)
55 #ifdef HANDLE_LOAD_DELAY
56 int sel = regs->dloadSel ^ 1;
57 assert(regs->dloadReg[sel] == 0);
58 regs->dloadReg[sel] = r;
59 regs->dloadVal[sel] = r ? val : 0;
60 if (regs->dloadReg[sel ^ 1] == r)
61 regs->dloadVal[sel ^ 1] = regs->dloadReg[sel ^ 1] = 0;
63 regs->GPR.r[r] = r ? val : 0;
67 static void dloadRt(psxRegisters *regs, u32 r, u32 val)
69 #ifdef HANDLE_LOAD_DELAY
70 int sel = regs->dloadSel;
71 if (unlikely(regs->dloadReg[sel] == r))
72 regs->dloadVal[sel] = regs->dloadReg[sel] = 0;
74 regs->GPR.r[r] = r ? val : 0;
77 static void dloadStep(psxRegisters *regs)
79 #ifdef HANDLE_LOAD_DELAY
80 int sel = regs->dloadSel;
81 regs->GPR.r[regs->dloadReg[sel]] = regs->dloadVal[sel];
82 regs->dloadVal[sel] = regs->dloadReg[sel] = 0;
84 assert(regs->GPR.r[0] == 0);
88 static void dloadFlush(psxRegisters *regs)
90 #ifdef HANDLE_LOAD_DELAY
91 regs->GPR.r[regs->dloadReg[0]] = regs->dloadVal[0];
92 regs->GPR.r[regs->dloadReg[1]] = regs->dloadVal[1];
93 regs->dloadVal[0] = regs->dloadVal[1] = 0;
94 regs->dloadReg[0] = regs->dloadReg[1] = 0;
95 assert(regs->GPR.r[0] == 0);
99 static void dloadClear(psxRegisters *regs)
101 #ifdef HANDLE_LOAD_DELAY
102 regs->dloadVal[0] = regs->dloadVal[1] = 0;
103 regs->dloadReg[0] = regs->dloadReg[1] = 0;
108 static void intException(psxRegisters *regs, u32 pc, u32 cause)
111 //FILE *f = fopen("/tmp/psx_ram.bin", "wb");
112 //fwrite(psxM, 1, 0x200000, f); fclose(f);
113 log_unhandled("exception %08x @%08x ra=%08x\n",
114 cause, pc, regs->GPR.n.ra);
118 psxException(cause, regs->branching, ®s->CP0);
119 regs->branching = R3000A_BRANCH_NONE_OR_EXCEPTION;
122 // exception caused by current instruction (excluding unkasking)
123 static void intExceptionInsn(psxRegisters *regs, u32 cause)
125 cause |= (regs->code & 0x0c000000) << 2;
126 intException(regs, regs->pc - 4, cause);
129 static noinline void intExceptionReservedInsn(psxRegisters *regs)
131 #ifdef DO_EXCEPTION_RESERVEDI
132 static u32 ppc_ = ~0u;
133 if (regs->pc != ppc_) {
134 SysPrintf("reserved instruction %08x @%08x ra=%08x\n",
135 regs->code, regs->pc - 4, regs->GPR.n.ra);
138 intExceptionInsn(regs, R3000E_RI << 2);
142 // 29 Enable for 80000000-ffffffff
143 // 30 Enable for 00000000-7fffffff
144 // 31 Enable exception
145 #define DBR_ABIT(dc, a) ((dc) & (1u << (29+(((a)>>31)^1))))
146 #define DBR_EN_EXEC(dc, a) (((dc) & 0x01800000) == 0x01800000 && DBR_ABIT(dc, a))
147 #define DBR_EN_LD(dc, a) (((dc) & 0x06800000) == 0x06800000 && DBR_ABIT(dc, a))
148 #define DBR_EN_ST(dc, a) (((dc) & 0x0a800000) == 0x0a800000 && DBR_ABIT(dc, a))
149 static void intExceptionDebugBp(psxRegisters *regs, u32 pc)
151 psxCP0Regs *cp0 = ®s->CP0;
153 cp0->n.Cause &= 0x300;
154 cp0->n.Cause |= (regs->branching << 30) | (R3000E_Bp << 2);
155 cp0->n.SR = (cp0->n.SR & ~0x3f) | ((cp0->n.SR & 0x0f) << 2);
156 cp0->n.EPC = regs->branching ? pc - 4 : pc;
157 regs->pc = 0x80000040;
160 static int execBreakCheck(psxRegisters *regs, u32 pc)
162 if (unlikely(DBR_EN_EXEC(regs->CP0.n.DCIC, pc) &&
163 ((pc ^ regs->CP0.n.BPC) & regs->CP0.n.BPCM) == 0))
165 regs->CP0.n.DCIC |= 0x03;
166 if (regs->CP0.n.DCIC & (1u << 31)) {
167 intExceptionDebugBp(regs, pc);
174 // get an opcode without triggering exceptions or affecting cache
175 u32 intFakeFetch(u32 pc)
177 u32 *code = (u32 *)psxm(pc & ~0x3, 0);
178 if (unlikely(code == INVALID_PTR))
180 return SWAP32(*code);
184 static u32 INT_ATTR fetchNoCache(psxRegisters *regs, u8 **memRLUT, u32 pc)
186 u32 *code = (u32 *)psxm_lut(pc & ~0x3, 0, memRLUT);
187 if (unlikely(code == INVALID_PTR)) {
188 SysPrintf("game crash @%08x, ra=%08x\n", pc, regs->GPR.n.ra);
189 intException(regs, pc, R3000E_IBE << 2);
190 return 0; // execute as nop
192 return SWAP32(*code);
197 Use old CPU cache code when the RAM location is updated with new code (affects in-game racing)
199 static struct cache_entry {
204 static u32 INT_ATTR fetchICache(psxRegisters *regs, u8 **memRLUT, u32 pc)
209 // this is not how the hardware works but whatever
210 struct cache_entry *entry = &ICache[(pc & 0xff0) >> 4];
212 if (((entry->tag ^ pc) & 0xfffffff0) != 0 || pc < entry->tag)
214 const u32 *code = (u32 *)psxm_lut(pc & ~0xf, 0, memRLUT);
215 if (unlikely(code == INVALID_PTR)) {
216 SysPrintf("game crash @%08x, ra=%08x\n", pc, regs->GPR.n.ra);
217 intException(regs, pc, R3000E_IBE << 2);
218 return 0; // execute as nop
222 // treat as 4 words, although other configurations are said to be possible
225 case 0x00: entry->data[0] = SWAP32(code[0]);
226 case 0x04: entry->data[1] = SWAP32(code[1]);
227 case 0x08: entry->data[2] = SWAP32(code[2]);
228 case 0x0c: entry->data[3] = SWAP32(code[3]);
231 return entry->data[(pc & 0x0f) >> 2];
234 return fetchNoCache(regs, memRLUT, pc);
237 static u32 (INT_ATTR *fetch)(psxRegisters *regs_, u8 **memRLUT, u32 pc) = fetchNoCache;
239 // Make the timing events trigger faster as we are currently assuming everything
240 // takes one cycle, which is not the case on real hardware.
241 // FIXME: count cache misses, memory latencies, stalls to get rid of this
242 static inline void addCycle(psxRegisters *regs)
244 assert(regs->subCycleStep >= 0x10000);
245 regs->subCycle += regs->subCycleStep;
246 regs->cycle += regs->subCycle >> 16;
247 regs->subCycle &= 0xffff;
250 /**** R3000A Instruction Macros ****/
251 #define _PC_ regs_->pc // The next PC to be executed
253 #define _fOp_(code) ((code >> 26) ) // The opcode part of the instruction register
254 #define _fFunct_(code) ((code ) & 0x3F) // The funct part of the instruction register
255 #define _fRd_(code) ((code >> 11) & 0x1F) // The rd part of the instruction register
256 #define _fRt_(code) ((code >> 16) & 0x1F) // The rt part of the instruction register
257 #define _fRs_(code) ((code >> 21) & 0x1F) // The rs part of the instruction register
258 #define _fSa_(code) ((code >> 6) & 0x1F) // The sa part of the instruction register
259 #define _fIm_(code) ((u16)code) // The immediate part of the instruction register
260 #define _fTarget_(code) (code & 0x03ffffff) // The target part of the instruction register
262 #define _fImm_(code) ((s16)code) // sign-extended immediate
263 #define _fImmU_(code) (code&0xffff) // zero-extended immediate
265 #define _Op_ _fOp_(code)
266 #define _Funct_ _fFunct_(code)
267 #define _Rd_ _fRd_(code)
268 #define _Rt_ _fRt_(code)
269 #define _Rs_ _fRs_(code)
270 #define _Sa_ _fSa_(code)
271 #define _Im_ _fIm_(code)
272 #define _Target_ _fTarget_(code)
274 #define _Imm_ _fImm_(code)
275 #define _ImmU_ _fImmU_(code)
277 #define _rRs_ regs_->GPR.r[_Rs_] // Rs register
278 #define _rRt_ regs_->GPR.r[_Rt_] // Rt register
279 #define _rSa_ regs_->GPR.r[_Sa_] // Sa register
281 #define _rHi_ regs_->GPR.n.hi // The HI register
282 #define _rLo_ regs_->GPR.n.lo // The LO register
284 #define _JumpTarget_ ((_Target_ * 4) + (_PC_ & 0xf0000000)) // Calculates the target during a jump instruction
285 #define _BranchTarget_ ((s16)_Im_ * 4 + _PC_) // Calculates the target during a branch instruction
287 #define _SetLink(x) dloadRt(regs_, x, _PC_ + 4); // Sets the return address in the link register
290 static inline INT_ATTR void name(psxRegisters *regs_, u32 code)
292 // this defines shall be used with the tmp
293 // of the next func (instead of _Funct_...)
294 #define _tFunct_ ((tmp ) & 0x3F) // The funct part of the instruction register
295 #define _tRd_ ((tmp >> 11) & 0x1F) // The rd part of the instruction register
296 #define _tRt_ ((tmp >> 16) & 0x1F) // The rt part of the instruction register
297 #define _tRs_ ((tmp >> 21) & 0x1F) // The rs part of the instruction register
298 #define _tSa_ ((tmp >> 6) & 0x1F) // The sa part of the instruction register
300 #define _i32(x) (s32)(x)
301 #define _u32(x) (u32)(x)
303 #define isBranch(c_) \
304 ((1 <= ((c_) >> 26) && ((c_) >> 26) <= 7) || ((c_) & 0xfc00003e) == 8)
305 #define swap_(a_, b_) { u32 t_ = a_; a_ = b_; b_ = t_; }
307 // tar1 is main branch target, 'code' is opcode in DS
308 static u32 psxBranchNoDelay(psxRegisters *regs_, u32 tar1, u32 code, int *taken) {
311 assert(isBranch(code));
313 switch (code >> 26) {
314 case 0x00: // SPECIAL
321 regs_->GPR.r[_Rd_] = tar1 + 4;
329 regs_->GPR.n.ra = tar1 + 4;
331 return tar1 + (s16)_Im_ * 4;
334 regs_->GPR.n.ra = tar1 + 4;
335 if (_i32(_rRs_) >= 0)
336 return tar1 + (s16)_Im_ * 4;
339 if (rt & 1) { // BGEZ
340 if (_i32(_rRs_) >= 0)
341 return tar1 + (s16)_Im_ * 4;
345 return tar1 + (s16)_Im_ * 4;
351 return (tar1 & 0xf0000000u) + _Target_ * 4;
353 regs_->GPR.n.ra = tar1 + 4;
354 return (tar1 & 0xf0000000u) + _Target_ * 4;
356 if (_i32(_rRs_) == _i32(_rRt_))
357 return tar1 + (s16)_Im_ * 4;
360 if (_i32(_rRs_) != _i32(_rRt_))
361 return tar1 + (s16)_Im_ * 4;
364 if (_i32(_rRs_) <= 0)
365 return tar1 + (s16)_Im_ * 4;
369 return tar1 + (s16)_Im_ * 4;
377 static void psxDoDelayBranch(psxRegisters *regs, u32 tar1, u32 code1) {
381 tar2 = psxBranchNoDelay(regs, tar1, code1, &taken);
387 * taken branch in delay slot:
388 * - execute 1 instruction at tar1
389 * - jump to tar2 (target of branch in delay slot; this branch
390 * has no normal delay slot, instruction at tar1 was fetched instead)
392 for (lim = 0; lim < 8; lim++) {
393 regs->code = code = fetch(regs, psxMemRLUT, tar1);
395 if (likely(!isBranch(code))) {
397 psxBSC[code >> 26](regs, code);
401 tar1 = psxBranchNoDelay(regs, tar2, code, &taken);
407 SysPrintf("Evil chained DS branches @ %08x %08x %08x\n", regs->pc, tar1, tar2);
410 static void doBranch(psxRegisters *regs, u32 tar, enum R3000Abdt taken) {
411 u32 code, pc, pc_final;
413 regs->branchSeen = regs->branching = taken;
414 pc_final = taken == R3000A_BRANCH_TAKEN ? tar : regs->pc + 4;
416 // fetch the delay slot
419 regs->code = code = fetch(regs, psxMemRLUT, pc);
423 // check for branch in delay slot
424 if (unlikely(isBranch(code))) {
426 if (taken == R3000A_BRANCH_TAKEN)
427 psxDoDelayBranch(regs, tar, code);
428 log_unhandled("branch in DS: %08x->%08x\n", pc, regs->pc);
435 psxBSC[code >> 26](regs, code);
437 if (likely(regs->branching != R3000A_BRANCH_NONE_OR_EXCEPTION))
440 regs->CP0.n.Target = pc_final;
446 static void doBranchReg(psxRegisters *regs, u32 tar) {
447 doBranch(regs, tar & ~3, R3000A_BRANCH_TAKEN);
450 static void doBranchRegE(psxRegisters *regs, u32 tar) {
451 if (unlikely(DBR_EN_EXEC(regs->CP0.n.DCIC, tar) &&
452 ((tar ^ regs->CP0.n.BPC) & regs->CP0.n.BPCM) == 0))
453 regs->CP0.n.DCIC |= 0x03;
454 if (unlikely(tar & 3)) {
455 SysPrintf("game crash @%08x, ra=%08x\n", tar, regs->GPR.n.ra);
456 regs->CP0.n.BadVAddr = tar;
457 intException(regs, tar, R3000E_AdEL << 2);
460 doBranch(regs, tar, R3000A_BRANCH_TAKEN);
463 static void addExc(psxRegisters *regs, u32 rt, s32 a1, s32 a2) {
465 if (add_overflow(a1, a2, val)) {
466 //printf("ov %08x + %08x = %08x\n", a1, a2, val);
467 intExceptionInsn(regs, R3000E_Ov << 2);
470 dloadRt(regs, rt, val);
473 static void subExc(psxRegisters *regs, u32 rt, s32 a1, s32 a2) {
475 if (sub_overflow(a1, a2, val)) {
476 intExceptionInsn(regs, R3000E_Ov << 2);
479 dloadRt(regs, rt, val);
482 /*********************************************************
483 * Arithmetic with immediate operand *
484 * Format: OP rt, rs, immediate *
485 *********************************************************/
486 OP(psxADDI) { addExc (regs_, _Rt_, _i32(_rRs_), _Imm_); } // Rt = Rs + Im (Exception on Integer Overflow)
487 OP(psxADDIU) { dloadRt(regs_, _Rt_, _u32(_rRs_) + _Imm_ ); } // Rt = Rs + Im
488 OP(psxANDI) { dloadRt(regs_, _Rt_, _u32(_rRs_) & _ImmU_); } // Rt = Rs And Im
489 OP(psxORI) { dloadRt(regs_, _Rt_, _u32(_rRs_) | _ImmU_); } // Rt = Rs Or Im
490 OP(psxXORI) { dloadRt(regs_, _Rt_, _u32(_rRs_) ^ _ImmU_); } // Rt = Rs Xor Im
491 OP(psxSLTI) { dloadRt(regs_, _Rt_, _i32(_rRs_) < _Imm_ ); } // Rt = Rs < Im (Signed)
492 OP(psxSLTIU) { dloadRt(regs_, _Rt_, _u32(_rRs_) < ((u32)_Imm_)); } // Rt = Rs < Im (Unsigned)
494 /*********************************************************
495 * Register arithmetic *
496 * Format: OP rd, rs, rt *
497 *********************************************************/
498 OP(psxADD) { addExc (regs_, _Rd_, _i32(_rRs_), _i32(_rRt_)); } // Rd = Rs + Rt (Exception on Integer Overflow)
499 OP(psxSUB) { subExc (regs_, _Rd_, _i32(_rRs_), _i32(_rRt_)); } // Rd = Rs - Rt (Exception on Integer Overflow)
500 OP(psxADDU) { dloadRt(regs_, _Rd_, _u32(_rRs_) + _u32(_rRt_)); } // Rd = Rs + Rt
501 OP(psxSUBU) { dloadRt(regs_, _Rd_, _u32(_rRs_) - _u32(_rRt_)); } // Rd = Rs - Rt
502 OP(psxAND) { dloadRt(regs_, _Rd_, _u32(_rRs_) & _u32(_rRt_)); } // Rd = Rs And Rt
503 OP(psxOR) { dloadRt(regs_, _Rd_, _u32(_rRs_) | _u32(_rRt_)); } // Rd = Rs Or Rt
504 OP(psxXOR) { dloadRt(regs_, _Rd_, _u32(_rRs_) ^ _u32(_rRt_)); } // Rd = Rs Xor Rt
505 OP(psxNOR) { dloadRt(regs_, _Rd_, ~_u32(_rRs_ | _u32(_rRt_))); } // Rd = Rs Nor Rt
506 OP(psxSLT) { dloadRt(regs_, _Rd_, _i32(_rRs_) < _i32(_rRt_)); } // Rd = Rs < Rt (Signed)
507 OP(psxSLTU) { dloadRt(regs_, _Rd_, _u32(_rRs_) < _u32(_rRt_)); } // Rd = Rs < Rt (Unsigned)
509 /*********************************************************
510 * Register mult/div & Register trap logic *
511 * Format: OP rs, rt *
512 *********************************************************/
516 if (_rRs_ & 0x80000000) {
522 #if !defined(__arm__) && !defined(__aarch64__)
523 else if (_rRs_ == 0x80000000 && _rRt_ == 0xFFFFFFFF) {
529 _rLo_ = _i32(_rRs_) / _i32(_rRt_);
530 _rHi_ = _i32(_rRs_) % _i32(_rRt_);
535 regs_->muldivBusyCycle = regs_->cycle + 37;
541 _rLo_ = _rRs_ / _rRt_;
542 _rHi_ = _rRs_ % _rRt_;
551 regs_->muldivBusyCycle = regs_->cycle + 37;
552 psxDIVU(regs_, code);
556 u64 res = (s64)_i32(_rRs_) * _i32(_rRt_);
558 regs_->GPR.n.lo = (u32)res;
559 regs_->GPR.n.hi = (u32)(res >> 32);
563 // approximate, but maybe good enough
565 u32 lz = __builtin_clz(((rs ^ ((s32)rs >> 21)) | 1));
566 u32 c = 7 + (2 - (lz / 11)) * 4;
567 regs_->muldivBusyCycle = regs_->cycle + c;
568 psxMULT(regs_, code);
572 u64 res = (u64)_u32(_rRs_) * _u32(_rRt_);
574 regs_->GPR.n.lo = (u32)(res & 0xffffffff);
575 regs_->GPR.n.hi = (u32)((res >> 32) & 0xffffffff);
579 // approximate, but maybe good enough
580 u32 lz = __builtin_clz(_rRs_ | 1);
581 u32 c = 7 + (2 - (lz / 11)) * 4;
582 regs_->muldivBusyCycle = regs_->cycle + c;
583 psxMULTU(regs_, code);
586 /*********************************************************
587 * Register branch logic *
588 * Format: OP rs, offset *
589 *********************************************************/
590 #define BrCond(c) (c) ? R3000A_BRANCH_TAKEN : R3000A_BRANCH_NOT_TAKEN
591 #define RepZBranchi32(op) \
592 doBranch(regs_, _BranchTarget_, BrCond(_i32(_rRs_) op 0));
593 #define RepZBranchLinki32(op) { \
594 s32 temp = _i32(_rRs_); \
597 doBranch(regs_, _BranchTarget_, BrCond(temp op 0)); \
600 OP(psxBGEZ) { RepZBranchi32(>=) } // Branch if Rs >= 0
601 OP(psxBGEZAL) { RepZBranchLinki32(>=) } // Branch if Rs >= 0 and link
602 OP(psxBGTZ) { RepZBranchi32(>) } // Branch if Rs > 0
603 OP(psxBLEZ) { RepZBranchi32(<=) } // Branch if Rs <= 0
604 OP(psxBLTZ) { RepZBranchi32(<) } // Branch if Rs < 0
605 OP(psxBLTZAL) { RepZBranchLinki32(<) } // Branch if Rs < 0 and link
607 /*********************************************************
608 * Shift arithmetic with constant shift *
609 * Format: OP rd, rt, sa *
610 *********************************************************/
611 OP(psxSLL) { dloadRt(regs_, _Rd_, _u32(_rRt_) << _Sa_); } // Rd = Rt << sa
612 OP(psxSRA) { dloadRt(regs_, _Rd_, _i32(_rRt_) >> _Sa_); } // Rd = Rt >> sa (arithmetic)
613 OP(psxSRL) { dloadRt(regs_, _Rd_, _u32(_rRt_) >> _Sa_); } // Rd = Rt >> sa (logical)
615 /*********************************************************
616 * Shift arithmetic with variant register shift *
617 * Format: OP rd, rt, rs *
618 *********************************************************/
619 OP(psxSLLV) { dloadRt(regs_, _Rd_, _u32(_rRt_) << (_u32(_rRs_) & 0x1F)); } // Rd = Rt << rs
620 OP(psxSRAV) { dloadRt(regs_, _Rd_, _i32(_rRt_) >> (_u32(_rRs_) & 0x1F)); } // Rd = Rt >> rs (arithmetic)
621 OP(psxSRLV) { dloadRt(regs_, _Rd_, _u32(_rRt_) >> (_u32(_rRs_) & 0x1F)); } // Rd = Rt >> rs (logical)
623 /*********************************************************
624 * Load higher 16 bits of the first word in GPR with imm *
625 * Format: OP rt, immediate *
626 *********************************************************/
627 OP(psxLUI) { dloadRt(regs_, _Rt_, code << 16); } // Upper halfword of Rt = Im
629 /*********************************************************
630 * Move from HI/LO to GPR *
632 *********************************************************/
633 OP(psxMFHI) { dloadRt(regs_, _Rd_, _rHi_); } // Rd = Hi
634 OP(psxMFLO) { dloadRt(regs_, _Rd_, _rLo_); } // Rd = Lo
636 static void mflohiCheckStall(psxRegisters *regs_)
638 u32 left = regs_->muldivBusyCycle - regs_->cycle;
640 //printf("muldiv stall %u\n", left);
641 regs_->cycle = regs_->muldivBusyCycle;
645 OP(psxMFHI_stall) { mflohiCheckStall(regs_); psxMFHI(regs_, code); }
646 OP(psxMFLO_stall) { mflohiCheckStall(regs_); psxMFLO(regs_, code); }
648 /*********************************************************
649 * Move to GPR to HI/LO & Register jump *
651 *********************************************************/
652 OP(psxMTHI) { _rHi_ = _rRs_; } // Hi = Rs
653 OP(psxMTLO) { _rLo_ = _rRs_; } // Lo = Rs
655 /*********************************************************
656 * Special purpose instructions *
658 *********************************************************/
660 intExceptionInsn(regs_, R3000E_Bp << 2);
664 intExceptionInsn(regs_, R3000E_Syscall << 2);
667 static inline void execI_(u8 **memRLUT, psxRegisters *regs_);
669 static inline void psxTestSWInts(psxRegisters *regs_, int step) {
670 if ((regs_->CP0.n.Cause & regs_->CP0.n.SR & 0x0300) &&
671 (regs_->CP0.n.SR & 0x1)) {
673 execI_(psxMemRLUT, regs_);
674 regs_->CP0.n.Cause &= ~0x7c;
675 intException(regs_, regs_->pc, regs_->CP0.n.Cause);
680 regs_->CP0.n.SR = (regs_->CP0.n.SR & ~0x0f) | ((regs_->CP0.n.SR & 0x3c) >> 2);
681 psxTestSWInts(regs_, 0);
684 /*********************************************************
685 * Register branch logic *
686 * Format: OP rs, rt, offset *
687 *********************************************************/
688 #define RepBranchi32(op) \
689 doBranch(regs_, _BranchTarget_, BrCond(_i32(_rRs_) op _i32(_rRt_)));
691 OP(psxBEQ) { RepBranchi32(==) } // Branch if Rs == Rt
692 OP(psxBNE) { RepBranchi32(!=) } // Branch if Rs != Rt
694 /*********************************************************
696 * Format: OP target *
697 *********************************************************/
698 OP(psxJ) { doBranch(regs_, _JumpTarget_, R3000A_BRANCH_TAKEN); }
702 doBranch(regs_, _JumpTarget_, R3000A_BRANCH_TAKEN);
705 /*********************************************************
707 * Format: OP rs, rd *
708 *********************************************************/
710 doBranchReg(regs_, _rRs_);
715 doBranchRegE(regs_, _rRs_);
720 u32 temp = _u32(_rRs_);
722 if (_Rd_) { _SetLink(_Rd_); }
723 doBranchReg(regs_, temp);
727 u32 temp = _u32(_rRs_);
729 if (_Rd_) { _SetLink(_Rd_); }
730 doBranchRegE(regs_, temp);
733 /*********************************************************
734 *********************************************************/
736 // revisit: incomplete
737 #define BUS_LOCKED_ADDR(a) \
738 ((0x1fc80000u <= (a) && (a) < 0x80000000u) || \
739 (0xc0000000u <= (a) && (a) < 0xfffe0000u))
741 // exception checking order is important
742 static inline int checkLD(psxRegisters *regs, u32 addr, u32 m) {
744 if (unlikely(DBR_EN_LD(regs->CP0.n.DCIC, addr) &&
745 ((addr ^ regs->CP0.n.BDA) & regs->CP0.n.BDAM) == 0)) {
746 regs->CP0.n.DCIC |= 0x0d;
747 bpException = regs->CP0.n.DCIC >> 31;
749 if (unlikely(addr & m)) {
750 regs->CP0.n.BadVAddr = addr;
751 intExceptionInsn(regs, R3000E_AdEL << 2);
754 if (unlikely(bpException)) {
755 intExceptionDebugBp(regs, regs->pc - 4);
758 if (unlikely(BUS_LOCKED_ADDR(addr))) {
759 log_unhandled("bus error read addr=%08x @%08x ra=%08x\n",
760 addr, regs->pc - 4, regs->GPR.n.ra);
761 intException(regs, regs->pc - 4, R3000E_DBE << 2);
767 static inline int checkST(psxRegisters *regs, u32 addr, u32 m) {
769 if (unlikely(DBR_EN_ST(regs->CP0.n.DCIC, addr) &&
770 ((addr ^ regs->CP0.n.BDA) & regs->CP0.n.BDAM) == 0)) {
771 regs->CP0.n.DCIC |= 0x15;
772 bpException = regs->CP0.n.DCIC >> 31;
774 if (unlikely(addr & m)) {
775 regs->CP0.n.BadVAddr = addr;
776 intExceptionInsn(regs, R3000E_AdES << 2);
779 if (unlikely(bpException)) {
780 intExceptionDebugBp(regs, regs->pc - 4);
783 if (unlikely(BUS_LOCKED_ADDR(addr))) {
784 log_unhandled("bus error write addr=%08x @%08x ra=%08x\n",
785 addr, regs->pc - 4, regs->GPR.n.ra);
786 intException(regs, regs->pc - 4, R3000E_DBE << 2);
792 /*********************************************************
793 * Load and store for GPR *
794 * Format: OP rt, offset(base) *
795 *********************************************************/
797 /*********************************************************
798 * Load and store for GPR *
799 * Format: OP rt, offset(base) *
800 *********************************************************/
802 #define _oB_ (regs_->GPR.r[_Rs_] + _Imm_)
804 OP(psxLB) { doLoad(regs_, _Rt_, (s8)psxMemRead8(_oB_)); }
805 OP(psxLBU) { doLoad(regs_, _Rt_, psxMemRead8(_oB_)); }
806 OP(psxLH) { doLoad(regs_, _Rt_, (s16)psxMemRead16(_oB_ & ~1)); }
807 OP(psxLHU) { doLoad(regs_, _Rt_, psxMemRead16(_oB_ & ~1)); }
808 OP(psxLW) { doLoad(regs_, _Rt_, psxMemRead32(_oB_ & ~3)); }
810 OP(psxLBe) { if (checkLD(regs_, _oB_, 0)) doLoad(regs_, _Rt_, (s8)psxMemRead8(_oB_)); }
811 OP(psxLBUe) { if (checkLD(regs_, _oB_, 0)) doLoad(regs_, _Rt_, psxMemRead8(_oB_)); }
812 OP(psxLHe) { if (checkLD(regs_, _oB_, 1)) doLoad(regs_, _Rt_, (s16)psxMemRead16(_oB_)); }
813 OP(psxLHUe) { if (checkLD(regs_, _oB_, 1)) doLoad(regs_, _Rt_, psxMemRead16(_oB_)); }
814 OP(psxLWe) { if (checkLD(regs_, _oB_, 3)) doLoad(regs_, _Rt_, psxMemRead32(_oB_)); }
816 static void doLWL(psxRegisters *regs, u32 rt, u32 addr) {
817 static const u32 LWL_MASK[4] = { 0xffffff, 0xffff, 0xff, 0 };
818 static const u32 LWL_SHIFT[4] = { 24, 16, 8, 0 };
819 u32 shift = addr & 3;
821 u32 oldval = regs->GPR.r[rt];
823 #ifdef HANDLE_LOAD_DELAY
824 int sel = regs->dloadSel;
825 if (regs->dloadReg[sel] == rt)
826 oldval = regs->dloadVal[sel];
828 mem = psxMemRead32(addr & ~3);
829 val = (oldval & LWL_MASK[shift]) | (mem << LWL_SHIFT[shift]);
830 doLoad(regs, rt, val);
833 Mem = 1234. Reg = abcd
835 0 4bcd (mem << 24) | (reg & 0x00ffffff)
836 1 34cd (mem << 16) | (reg & 0x0000ffff)
837 2 234d (mem << 8) | (reg & 0x000000ff)
838 3 1234 (mem ) | (reg & 0x00000000)
842 static void doLWR(psxRegisters *regs, u32 rt, u32 addr) {
843 static const u32 LWR_MASK[4] = { 0, 0xff000000, 0xffff0000, 0xffffff00 };
844 static const u32 LWR_SHIFT[4] = { 0, 8, 16, 24 };
845 u32 shift = addr & 3;
847 u32 oldval = regs->GPR.r[rt];
849 #ifdef HANDLE_LOAD_DELAY
850 int sel = regs->dloadSel;
851 if (regs->dloadReg[sel] == rt)
852 oldval = regs->dloadVal[sel];
854 mem = psxMemRead32(addr & ~3);
855 val = (oldval & LWR_MASK[shift]) | (mem >> LWR_SHIFT[shift]);
856 doLoad(regs, rt, val);
859 Mem = 1234. Reg = abcd
861 0 1234 (mem ) | (reg & 0x00000000)
862 1 a123 (mem >> 8) | (reg & 0xff000000)
863 2 ab12 (mem >> 16) | (reg & 0xffff0000)
864 3 abc1 (mem >> 24) | (reg & 0xffffff00)
868 OP(psxLWL) { doLWL(regs_, _Rt_, _oB_); }
869 OP(psxLWR) { doLWR(regs_, _Rt_, _oB_); }
871 OP(psxLWLe) { if (checkLD(regs_, _oB_ & ~3, 0)) doLWL(regs_, _Rt_, _oB_); }
872 OP(psxLWRe) { if (checkLD(regs_, _oB_ , 0)) doLWR(regs_, _Rt_, _oB_); }
874 OP(psxSB) { psxMemWrite8 (_oB_, _rRt_); }
875 OP(psxSH) { psxMemWrite16(_oB_, _rRt_); }
876 OP(psxSW) { psxMemWrite32(_oB_, _rRt_); }
878 OP(psxSBe) { if (checkST(regs_, _oB_, 0)) psxMemWrite8 (_oB_, _rRt_); }
879 OP(psxSHe) { if (checkST(regs_, _oB_, 1)) psxMemWrite16(_oB_, _rRt_); }
880 OP(psxSWe) { if (checkST(regs_, _oB_, 3)) psxMemWrite32(_oB_, _rRt_); }
882 static void doSWL(psxRegisters *regs, u32 rt, u32 addr) {
883 u32 val = regs->GPR.r[rt];
885 case 0: psxMemWrite8( addr , val >> 24); break;
886 case 1: psxMemWrite16(addr & ~3, val >> 16); break;
887 case 2: // revisit: should be a single 24bit write
888 psxMemWrite16(addr & ~3, (val >> 8) & 0xffff);
889 psxMemWrite8( addr , val >> 24); break;
890 case 3: psxMemWrite32(addr & ~3, val); break;
893 Mem = 1234. Reg = abcd
895 0 123a (reg >> 24) | (mem & 0xffffff00)
896 1 12ab (reg >> 16) | (mem & 0xffff0000)
897 2 1abc (reg >> 8) | (mem & 0xff000000)
898 3 abcd (reg ) | (mem & 0x00000000)
902 static void doSWR(psxRegisters *regs, u32 rt, u32 addr) {
903 u32 val = regs->GPR.r[rt];
905 case 0: psxMemWrite32(addr , val); break;
906 case 1: // revisit: should be a single 24bit write
907 psxMemWrite8 (addr , val & 0xff);
908 psxMemWrite16(addr + 1, (val >> 8) & 0xffff); break;
909 case 2: psxMemWrite16(addr , val & 0xffff); break;
910 case 3: psxMemWrite8 (addr , val & 0xff); break;
914 Mem = 1234. Reg = abcd
916 0 abcd (reg ) | (mem & 0x00000000)
917 1 bcd4 (reg << 8) | (mem & 0x000000ff)
918 2 cd34 (reg << 16) | (mem & 0x0000ffff)
919 3 d234 (reg << 24) | (mem & 0x00ffffff)
923 OP(psxSWL) { doSWL(regs_, _Rt_, _oB_); }
924 OP(psxSWR) { doSWR(regs_, _Rt_, _oB_); }
926 OP(psxSWLe) { if (checkST(regs_, _oB_ & ~3, 0)) doSWL(regs_, _Rt_, _oB_); }
927 OP(psxSWRe) { if (checkST(regs_, _oB_ , 0)) doSWR(regs_, _Rt_, _oB_); }
929 /*********************************************************
930 * Moves between GPR and COPx *
931 * Format: OP rt, fs *
932 *********************************************************/
935 if (unlikely(0x00000417u & (1u << r)))
936 intExceptionReservedInsn(regs_);
937 doLoad(regs_, _Rt_, regs_->CP0.r[r]);
940 static void setupCop(u32 sr);
942 void MTC0(psxRegisters *regs_, int reg, u32 val) {
943 // SysPrintf("MTC0 %d: %x\n", reg, val);
946 if (unlikely((regs_->CP0.n.SR ^ val) & (1u << 16)))
947 psxMemOnIsolate((val >> 16) & 1);
948 if (unlikely((regs_->CP0.n.SR ^ val) & (7u << 29)))
950 regs_->CP0.n.SR = val;
951 psxTestSWInts(regs_, 1);
955 regs_->CP0.n.Cause &= ~0x0300;
956 regs_->CP0.n.Cause |= val & 0x0300;
957 psxTestSWInts(regs_, 0);
961 if ((regs_->CP0.n.DCIC ^ val) & 0xff800000)
962 log_unhandled("DCIC: %08x->%08x\n", regs_->CP0.n.DCIC, val);
965 if (regs_->CP0.n.BPC != val)
966 log_unhandled("BPC: %08x->%08x\n", regs_->CP0.n.BPC, val);
971 regs_->CP0.r[reg] = val;
976 OP(psxMTC0) { MTC0(regs_, _Rd_, _u32(_rRt_)); }
979 static inline void psxNULLne(psxRegisters *regs) {
980 log_unhandled("unhandled op %08x @%08x\n", regs->code, regs->pc - 4);
983 /*********************************************************
984 * Unknown instruction (would generate an exception) *
986 *********************************************************/
990 intExceptionReservedInsn(regs_);
993 void gteNULL(struct psxCP2Regs *regs) {
994 psxRegisters *regs_ = (psxRegisters *)((u8 *)regs - offsetof(psxRegisters, CP2));
999 psxSPC[_Funct_](regs_, code);
1005 u32 op2 = code & 0x1f;
1010 case 0x08: psxNULL(regs_, code); break;
1011 case 0x10: psxRFE(regs_, code); break;
1012 default: psxNULLne(regs_); break;
1017 case 0x00: psxMFC0(regs_, code); break;
1018 case 0x04: psxMTC0(regs_, code); break;
1020 case 0x06: psxNULL(regs_, code); break; // CTC -> exception
1022 case 0x0c: log_unhandled("BC0 %08x @%08x\n", code, regs_->pc - 4);
1023 default: psxNULLne(regs_); break;
1028 // ??? what actually happens here?
1029 log_unhandled("COP1 %08x @%08x\n", code, regs_->pc - 4);
1033 u32 rt = _Rt_, rd = _Rd_, rs = _Rs_;
1035 psxCP2[_Funct_](®s_->CP2);
1039 case 0x00: doLoad(regs_, rt, MFC2(®s_->CP2, rd)); break; // MFC2
1040 case 0x02: doLoad(regs_, rt, regs_->CP2C.r[rd]); break; // CFC2
1041 case 0x04: MTC2(®s_->CP2, regs_->GPR.r[rt], rd); break; // MTC2
1042 case 0x06: CTC2(®s_->CP2, regs_->GPR.r[rt], rd); break; // CTC2
1044 case 0x0c: log_unhandled("BC2 %08x @%08x\n", code, regs_->pc - 4);
1045 default: psxNULLne(regs_); break;
1052 psxCOP2(regs_, code);
1056 MTC2(®s_->CP2, psxMemRead32(_oB_), _Rt_);
1061 gteLWC2(regs_, code);
1064 OP(gteLWC2e_stall) {
1066 if (checkLD(regs_, _oB_, 3))
1067 MTC2(®s_->CP2, psxMemRead32(_oB_), _Rt_);
1071 psxMemWrite32(_oB_, MFC2(®s_->CP2, _Rt_));
1076 gteSWC2(regs_, code);
1079 OP(gteSWC2e_stall) {
1081 if (checkST(regs_, _oB_, 3))
1082 gteSWC2(regs_, code);
1086 // ??? what actually happens here?
1087 log_unhandled("COP3 %08x @%08x\n", code, regs_->pc - 4);
1091 log_unhandled("disabled cop%d @%08x\n", (code >> 26) & 3, regs_->pc - 4);
1092 #ifdef DO_EXCEPTION_RESERVEDI
1093 intExceptionInsn(regs_, R3000E_CpU << 2);
1098 log_unhandled("LWCx %08x @%08x\n", code, regs_->pc - 4);
1099 checkLD(regs_, _oB_, 3);
1103 // does this write something to memory?
1104 log_unhandled("SWCx %08x @%08x\n", code, regs_->pc - 4);
1105 checkST(regs_, _oB_, 3);
1111 case 0x10: psxBLTZAL(regs_, code); break;
1112 case 0x11: psxBGEZAL(regs_, code); break;
1115 psxBGEZ(regs_, code);
1117 psxBLTZ(regs_, code);
1123 if (unlikely(!Config.HLE)) {
1124 psxSWCx(regs_, code);
1127 hleCode = code & 0x03ffffff;
1128 if (hleCode >= (sizeof(psxHLEt) / sizeof(psxHLEt[0]))) {
1129 psxSWCx(regs_, code);
1134 regs_->branchSeen = 1;
1137 static void (INT_ATTR *psxBSC[64])(psxRegisters *regs_, u32 code) = {
1138 psxSPECIAL, psxREGIMM, psxJ , psxJAL , psxBEQ , psxBNE , psxBLEZ, psxBGTZ,
1139 psxADDI , psxADDIU , psxSLTI, psxSLTIU, psxANDI, psxORI , psxXORI, psxLUI ,
1140 psxCOP0 , psxCOPd , psxCOP2, psxCOPd, psxNULL, psxNULL, psxNULL, psxNULL,
1141 psxNULL , psxNULL , psxNULL, psxNULL, psxNULL, psxNULL, psxNULL, psxNULL,
1142 psxLB , psxLH , psxLWL , psxLW , psxLBU , psxLHU , psxLWR , psxNULL,
1143 psxSB , psxSH , psxSWL , psxSW , psxNULL, psxNULL, psxSWR , psxNULL,
1144 psxLWCx , psxLWCx , gteLWC2, psxLWCx , psxNULL, psxNULL, psxNULL, psxNULL,
1145 psxSWCx , psxSWCx , gteSWC2, psxHLE , psxNULL, psxNULL, psxNULL, psxNULL,
1148 static void (INT_ATTR *psxSPC[64])(psxRegisters *regs_, u32 code) = {
1149 psxSLL , psxNULL , psxSRL , psxSRA , psxSLLV , psxNULL , psxSRLV, psxSRAV,
1150 psxJR , psxJALR , psxNULL, psxNULL, psxSYSCALL, psxBREAK, psxNULL, psxNULL,
1151 psxMFHI, psxMTHI , psxMFLO, psxMTLO, psxNULL , psxNULL , psxNULL, psxNULL,
1152 psxMULT, psxMULTU, psxDIV , psxDIVU, psxNULL , psxNULL , psxNULL, psxNULL,
1153 psxADD , psxADDU , psxSUB , psxSUBU, psxAND , psxOR , psxXOR , psxNOR ,
1154 psxNULL, psxNULL , psxSLT , psxSLTU, psxNULL , psxNULL , psxNULL, psxNULL,
1155 psxNULL, psxNULL , psxNULL, psxNULL, psxNULL , psxNULL , psxNULL, psxNULL,
1156 psxNULL, psxNULL , psxNULL, psxNULL, psxNULL , psxNULL , psxNULL, psxNULL
1159 void (*psxCP2[64])(struct psxCP2Regs *regs) = {
1160 gteNULL , gteRTPS , gteNULL , gteNULL, gteNULL, gteNULL , gteNCLIP, gteNULL, // 00
1161 gteNULL , gteNULL , gteNULL , gteNULL, gteOP , gteNULL , gteNULL , gteNULL, // 08
1162 gteDPCS , gteINTPL, gteMVMVA, gteNCDS, gteCDP , gteNULL , gteNCDT , gteNULL, // 10
1163 gteNULL , gteNULL , gteNULL , gteNCCS, gteCC , gteNULL , gteNCS , gteNULL, // 18
1164 gteNCT , gteNULL , gteNULL , gteNULL, gteNULL, gteNULL , gteNULL , gteNULL, // 20
1165 gteSQR , gteDCPL , gteDPCT , gteNULL, gteNULL, gteAVSZ3, gteAVSZ4, gteNULL, // 28
1166 gteRTPT , gteNULL , gteNULL , gteNULL, gteNULL, gteNULL , gteNULL , gteNULL, // 30
1167 gteNULL , gteNULL , gteNULL , gteNULL, gteNULL, gteGPF , gteGPL , gteNCCT // 38
1170 ///////////////////////////////////////////
1172 static int intInit() {
1177 static void intReset() {
1178 dloadClear(&psxRegs);
1179 psxRegs.subCycle = 0;
1182 static inline void execI_(u8 **memRLUT, psxRegisters *regs) {
1189 regs->code = fetch(regs, memRLUT, pc);
1190 psxBSC[regs->code >> 26](regs, regs->code);
1193 static inline void execIbp(u8 **memRLUT, psxRegisters *regs) {
1199 if (execBreakCheck(regs, pc))
1203 regs->code = fetch(regs, memRLUT, pc);
1204 psxBSC[regs->code >> 26](regs, regs->code);
1207 static void intExecute(psxRegisters *regs) {
1208 u8 **memRLUT = psxMemRLUT;
1211 execI_(memRLUT, regs);
1214 static void intExecuteBp(psxRegisters *regs) {
1215 u8 **memRLUT = psxMemRLUT;
1218 execIbp(memRLUT, regs);
1221 static void intExecuteBlock(psxRegisters *regs, enum blockExecCaller caller) {
1222 u8 **memRLUT = psxMemRLUT;
1224 regs->branchSeen = 0;
1225 while (!regs->branchSeen)
1226 execI_(memRLUT, regs);
1229 static void intExecuteBlockBp(psxRegisters *regs, enum blockExecCaller caller) {
1230 u8 **memRLUT = psxMemRLUT;
1232 regs->branchSeen = 0;
1233 while (!regs->branchSeen)
1234 execIbp(memRLUT, regs);
1237 static void intClear(u32 Addr, u32 Size) {
1240 static void intNotify(enum R3000Anote note, void *data) {
1242 case R3000ACPU_NOTIFY_BEFORE_SAVE:
1243 dloadFlush(&psxRegs);
1245 case R3000ACPU_NOTIFY_AFTER_LOAD:
1246 dloadClear(&psxRegs);
1247 psxRegs.subCycle = 0;
1248 setupCop(psxRegs.CP0.n.SR);
1250 case R3000ACPU_NOTIFY_CACHE_ISOLATED: // Armored Core?
1251 if (fetch == fetchICache)
1252 memset(&ICache, 0xff, sizeof(ICache));
1254 case R3000ACPU_NOTIFY_CACHE_UNISOLATED:
1259 static void setupCop(u32 sr)
1261 if (sr & (1u << 29))
1262 psxBSC[17] = psxCOP1;
1264 psxBSC[17] = psxCOPd;
1265 if (sr & (1u << 30))
1266 psxBSC[18] = Config.DisableStalls ? psxCOP2 : psxCOP2_stall;
1268 psxBSC[18] = psxCOPd;
1269 if (sr & (1u << 31))
1270 psxBSC[19] = psxCOP3;
1272 psxBSC[19] = psxCOPd;
1275 void intApplyConfig() {
1278 assert(psxSPC[16] == psxMFHI || psxSPC[16] == psxMFHI_stall);
1279 assert(psxSPC[18] == psxMFLO || psxSPC[18] == psxMFLO_stall);
1280 assert(psxSPC[24] == psxMULT || psxSPC[24] == psxMULT_stall);
1281 assert(psxSPC[25] == psxMULTU || psxSPC[25] == psxMULTU_stall);
1282 assert(psxSPC[26] == psxDIV || psxSPC[26] == psxDIV_stall);
1283 assert(psxSPC[27] == psxDIVU || psxSPC[27] == psxDIVU_stall);
1285 if (Config.DisableStalls) {
1286 psxBSC[18] = psxCOP2;
1287 psxBSC[50] = gteLWC2;
1288 psxBSC[58] = gteSWC2;
1289 psxSPC[16] = psxMFHI;
1290 psxSPC[18] = psxMFLO;
1291 psxSPC[24] = psxMULT;
1292 psxSPC[25] = psxMULTU;
1293 psxSPC[26] = psxDIV;
1294 psxSPC[27] = psxDIVU;
1296 psxBSC[18] = psxCOP2_stall;
1297 psxBSC[50] = gteLWC2_stall;
1298 psxBSC[58] = gteSWC2_stall;
1299 psxSPC[16] = psxMFHI_stall;
1300 psxSPC[18] = psxMFLO_stall;
1301 psxSPC[24] = psxMULT_stall;
1302 psxSPC[25] = psxMULTU_stall;
1303 psxSPC[26] = psxDIV_stall;
1304 psxSPC[27] = psxDIVU_stall;
1306 setupCop(psxRegs.CP0.n.SR);
1308 if (Config.PreciseExceptions) {
1309 psxBSC[0x20] = psxLBe;
1310 psxBSC[0x21] = psxLHe;
1311 psxBSC[0x22] = psxLWLe;
1312 psxBSC[0x23] = psxLWe;
1313 psxBSC[0x24] = psxLBUe;
1314 psxBSC[0x25] = psxLHUe;
1315 psxBSC[0x26] = psxLWRe;
1316 psxBSC[0x28] = psxSBe;
1317 psxBSC[0x29] = psxSHe;
1318 psxBSC[0x2a] = psxSWLe;
1319 psxBSC[0x2b] = psxSWe;
1320 psxBSC[0x2e] = psxSWRe;
1321 psxBSC[0x32] = gteLWC2e_stall;
1322 psxBSC[0x3a] = gteSWC2e_stall;
1323 psxSPC[0x08] = psxJRe;
1324 psxSPC[0x09] = psxJALRe;
1325 psxInt.Execute = intExecuteBp;
1326 psxInt.ExecuteBlock = intExecuteBlockBp;
1328 psxBSC[0x20] = psxLB;
1329 psxBSC[0x21] = psxLH;
1330 psxBSC[0x22] = psxLWL;
1331 psxBSC[0x23] = psxLW;
1332 psxBSC[0x24] = psxLBU;
1333 psxBSC[0x25] = psxLHU;
1334 psxBSC[0x26] = psxLWR;
1335 psxBSC[0x28] = psxSB;
1336 psxBSC[0x29] = psxSH;
1337 psxBSC[0x2a] = psxSWL;
1338 psxBSC[0x2b] = psxSW;
1339 psxBSC[0x2e] = psxSWR;
1340 // LWC2, SWC2 handled by Config.DisableStalls
1341 psxSPC[0x08] = psxJR;
1342 psxSPC[0x09] = psxJALR;
1343 psxInt.Execute = intExecute;
1344 psxInt.ExecuteBlock = intExecuteBlock;
1347 // the dynarec may occasionally call the interpreter, in such a case the
1348 // cache won't work (cache only works right if all fetches go through it)
1349 if (!Config.icache_emulation || psxCpu != &psxInt) {
1350 fetch = fetchNoCache;
1351 memset(&ICache, 0xff, sizeof(ICache));
1354 fetch = fetchICache;
1356 cycle_mult = Config.cycle_multiplier_override && Config.cycle_multiplier == CYCLE_MULT_DEFAULT
1357 ? Config.cycle_multiplier_override : Config.cycle_multiplier;
1358 psxRegs.subCycleStep = 0x10000 * cycle_mult / 100;
1361 static void intShutdown() {
1362 dloadClear(&psxRegs);
1365 // single step (may do several ops in case of a branch or load delay)
1366 // called by asm/dynarec
1367 void execI(psxRegisters *regs) {
1369 execIbp(psxMemRLUT, regs);
1370 } while (regs->dloadReg[0] || regs->dloadReg[1]);
1373 R3000Acpu psxInt = {