From 0890ae159b413fa4a5c84c0db1bf7e2eb05f4849 Mon Sep 17 00:00:00 2001 From: notaz Date: Tue, 19 Sep 2023 01:35:30 +0300 Subject: [PATCH] psxbios: allow custom api overrides BallBlazer Champions hooks custom heap funcs notaz/pcsx_rearmed#307 --- Makefile | 2 +- libpcsxcore/psxbios.c | 159 +++++++++++++++++++++++++++++++++++------- libpcsxcore/psxhle.c | 112 ----------------------------- libpcsxcore/psxhle.h | 10 --- 4 files changed, 136 insertions(+), 147 deletions(-) delete mode 100644 libpcsxcore/psxhle.c diff --git a/Makefile b/Makefile index 86473ab4..63ffcb62 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ endif OBJS += libpcsxcore/cdriso.o libpcsxcore/cdrom.o libpcsxcore/cheat.o libpcsxcore/database.o \ libpcsxcore/decode_xa.o libpcsxcore/mdec.o \ libpcsxcore/misc.o libpcsxcore/plugins.o libpcsxcore/ppf.o libpcsxcore/psxbios.o \ - libpcsxcore/psxcommon.o libpcsxcore/psxcounters.o libpcsxcore/psxdma.o libpcsxcore/psxhle.o \ + libpcsxcore/psxcommon.o libpcsxcore/psxcounters.o libpcsxcore/psxdma.o \ libpcsxcore/psxhw.o libpcsxcore/psxinterpreter.o libpcsxcore/psxmem.o libpcsxcore/r3000a.o \ libpcsxcore/sio.o libpcsxcore/spu.o OBJS += libpcsxcore/gte.o libpcsxcore/gte_nf.o libpcsxcore/gte_divider.o diff --git a/libpcsxcore/psxbios.c b/libpcsxcore/psxbios.c index a41cf54e..83bc684a 100644 --- a/libpcsxcore/psxbios.c +++ b/libpcsxcore/psxbios.c @@ -302,6 +302,11 @@ static u32 floodchk; #define A_CD_EVENTS 0xb9b8 #define A_EXC_GP 0xf450 +#define A_A0_DUMMY 0x1010 +#define A_B0_DUMMY 0x2010 +#define A_C0_DUMMY 0x3010 +#define A_B0_5B_DUMMY 0x43d0 + #define HLEOP(n) SWAPu32((0x3b << 26) | (n)); static u32 loadRam32(u32 addr) @@ -1375,7 +1380,7 @@ void psxBios_getchar() { //0x3b static void psxBios_printf_psxout() { // 0x3f char tmp[1024]; char tmp2[1024]; - u32 save[4]; + u32 save[4] = { 0, }; char *ptmp = tmp; int n=1, i=0, j; void *psp; @@ -3294,6 +3299,11 @@ void psxBiosSetupBootState(void) SPU_writeRegister(0x1f801d80 + i*2, spu_config[i], psxRegs.cycle); } +static void hleExc0_0_1(); +static void hleExc0_0_2(); +static void hleExc0_1_1(); +static void hleExc0_1_2(); + #include "sjisfont.h" void psxBiosInit() { @@ -3705,16 +3715,16 @@ void psxBiosInit() { // (or rather the funcs listed there) ptr = (u32 *)&psxM[A_A0_TABLE]; for (i = 0; i < 256; i++) - ptr[i] = SWAP32(0x1000); + ptr[i] = SWAP32(A_A0_DUMMY); ptr = (u32 *)&psxM[A_B0_TABLE]; for (i = 0; i < 256; i++) - ptr[i] = SWAP32(0x2000); + ptr[i] = SWAP32(A_B0_DUMMY); // B(5b) is special because games patch (sometimes even jump to) // code at fixed offsets from it, nocash lists offsets: // patch: +3d8, +4dc, +594, +62c, +9c8, +1988 // call: +7a0=4b70, +884=4c54, +894=4c64 - ptr[0x5b] = SWAP32(0x43d0); + ptr[0x5b] = SWAP32(A_B0_5B_DUMMY); // 0x43d0 ram32[0x4b70/4] = SWAP32(0x03e00008); // jr $ra // setPadOutputBuf ram32[0x4c54/4] = SWAP32(0x240e0001); // mov $t6, 1 @@ -3726,13 +3736,14 @@ void psxBiosInit() { ptr = (u32 *)&psxM[A_C0_TABLE]; for (i = 0; i < 256/2; i++) - ptr[i] = SWAP32(0x3000); + ptr[i] = SWAP32(A_C0_DUMMY); ptr[6] = SWAP32(A_EXCEPTION); // more HLE traps - ram32[0x1000/4] = HLEOP(hleop_dummy); - ram32[0x2000/4] = HLEOP(hleop_dummy); - ram32[0x3000/4] = HLEOP(hleop_dummy); + ram32[A_A0_DUMMY/4] = HLEOP(hleop_dummy); + ram32[A_B0_DUMMY/4] = HLEOP(hleop_dummy); + ram32[A_C0_DUMMY/4] = HLEOP(hleop_dummy); + ram32[A_B0_5B_DUMMY/4] = HLEOP(hleop_dummy); ram32[0x8000/4] = HLEOP(hleop_execret); ram32[A_EEXIT_PTR/4] = SWAP32(A_EEXIT_DEF); @@ -3782,13 +3793,13 @@ static void handle_chain_x_x_1(u32 enable, u32 irqbit) // hleExc0_{0,1}* are usually removed by A(56)/A(72) on the game's startup, // so this is only partially implemented -void hleExc0_0_1() // A(93h) - CdromDmaIrqFunc2 +static void hleExc0_0_1() // A(93h) - CdromDmaIrqFunc2 { u32 cdrom_dma_ack_enable = 1; // a000b93c handle_chain_x_x_1(cdrom_dma_ack_enable, 3); // IRQ3 DMA } -void hleExc0_0_2() // A(91h) - CdromDmaIrqFunc1 +static void hleExc0_0_2() // A(91h) - CdromDmaIrqFunc1 { u32 ret = 0; //PSXBIOS_LOG("%s\n", __func__); @@ -3803,13 +3814,13 @@ void hleExc0_0_2() // A(91h) - CdromDmaIrqFunc1 mips_return_c(ret, 20); } -void hleExc0_1_1() // A(92h) - CdromIoIrqFunc2 +static void hleExc0_1_1() // A(92h) - CdromIoIrqFunc2 { u32 cdrom_irq_ack_enable = 1; // a000b938 handle_chain_x_x_1(cdrom_irq_ack_enable, 2); // IRQ2 cdrom } -void hleExc0_1_2() // A(90h) - CdromIoIrqFunc1 +static void hleExc0_1_2() // A(90h) - CdromIoIrqFunc1 { u32 ret = 0; if (psxHu32(0x1074) & psxHu32(0x1070) & 4) { // IRQ2 cdrom @@ -3819,7 +3830,7 @@ void hleExc0_1_2() // A(90h) - CdromIoIrqFunc1 mips_return_c(ret, 20); } -void hleExc0_2_2_syscall() // not in any A/B/C table +static void hleExc0_2_2_syscall() // not in any A/B/C table { u32 tcbPtr = loadRam32(A_TT_PCB); TCB *tcb = loadRam32ptr(tcbPtr); @@ -3863,7 +3874,7 @@ void hleExc0_2_2_syscall() // not in any A/B/C table psxBios_ReturnFromException(); } -void hleExc1_0_1(void) +static void hleExc1_0_1(void) { u32 vbl_irq_ack_enable = loadRam32(A_RCNT_VBL_ACK + 0x0c); // 860c handle_chain_x_x_1(vbl_irq_ack_enable, 0); // IRQ0 vblank @@ -3879,45 +3890,45 @@ static void handle_chain_1_x_2(u32 ev_index, u32 irqbit) mips_return_c(ret, 22); } -void hleExc1_0_2(void) +static void hleExc1_0_2(void) { handle_chain_1_x_2(3, 0); // IRQ0 vblank } -void hleExc1_1_1(void) +static void hleExc1_1_1(void) { u32 rcnt_irq_ack_enable = loadRam32(A_RCNT_VBL_ACK + 0x08); // 8608 handle_chain_x_x_1(rcnt_irq_ack_enable, 6); // IRQ6 rcnt2 } -void hleExc1_1_2(void) +static void hleExc1_1_2(void) { handle_chain_1_x_2(2, 6); // IRQ6 rcnt2 } -void hleExc1_2_1(void) +static void hleExc1_2_1(void) { u32 rcnt_irq_ack_enable = loadRam32(A_RCNT_VBL_ACK + 0x04); // 8604 handle_chain_x_x_1(rcnt_irq_ack_enable, 5); // IRQ5 rcnt1 } -void hleExc1_2_2(void) +static void hleExc1_2_2(void) { handle_chain_1_x_2(1, 5); // IRQ5 rcnt1 } -void hleExc1_3_1(void) +static void hleExc1_3_1(void) { u32 rcnt_irq_ack_enable = loadRam32(A_RCNT_VBL_ACK + 0x00); // 8600 handle_chain_x_x_1(rcnt_irq_ack_enable, 4); // IRQ4 rcnt0 } -void hleExc1_3_2(void) +static void hleExc1_3_2(void) { handle_chain_1_x_2(0, 4); // IRQ4 rcnt0 } -void hleExc3_0_2_defint(void) +static void hleExc3_0_2_defint(void) { static const struct { u8 ev, irqbit; @@ -3945,7 +3956,7 @@ void hleExc3_0_2_defint(void) mips_return_c(0, 11 + 7*11 + 7*11 + 12); } -void hleExcPadCard1(void) +static void hleExcPadCard1(void) { if (loadRam32(A_PAD_IRQR_ENA)) { u8 *pad_buf1 = loadRam8ptr(A_PAD_INBUF + 0); @@ -3966,7 +3977,7 @@ void hleExcPadCard1(void) mips_return_c(0, 18); } -void hleExcPadCard2(void) +static void hleExcPadCard2(void) { u32 ret = psxHu32(0x1074) & psxHu32(0x1070) & 1; mips_return_c(ret, 15); @@ -4033,6 +4044,106 @@ void psxBiosException() { psxBios_ReturnFromException(); } +/* HLE */ +static void hleDummy() { + log_unhandled("hleDummy called @%08x ra=%08x\n", psxRegs.pc - 4, ra); + psxRegs.pc = ra; + psxRegs.cycle += 1000; + + psxBranchTest(); +} + +static void hleA0() { + u32 call = t1 & 0xff; + u32 entry = loadRam32(A_A0_TABLE + call * 4); + + if (call < 192 && entry != A_A0_DUMMY) { + PSXBIOS_LOG("custom A%02x %s(0x%x, ) addr=%08x ra=%08x\n", + call, biosA0n[call], a0, entry, ra); + softCall(entry); + pc0 = ra; + PSXBIOS_LOG(" -> %08x\n", v0); + } + else if (biosA0[call]) + biosA0[call](); + + psxBranchTest(); +} + +static void hleB0() { + u32 call = t1 & 0xff; + u32 entry = loadRam32(A_B0_TABLE + call * 4); + int is_custom = 0; + + if (call == 0x5b) + is_custom = entry != A_B0_5B_DUMMY; + else + is_custom = entry != A_B0_DUMMY; + if (is_custom) { + PSXBIOS_LOG("custom B%02x %s(0x%x, ) addr=%08x ra=%08x\n", + call, biosB0n[call], a0, entry, ra); + softCall(entry); + pc0 = ra; + PSXBIOS_LOG(" -> %08x\n", v0); + } + else if (biosB0[call]) + biosB0[call](); + + psxBranchTest(); +} + +static void hleC0() { + u32 call = t1 & 0xff; + u32 entry = loadRam32(A_C0_TABLE + call * 4); + + if (call < 128 && entry != A_C0_DUMMY) { + PSXBIOS_LOG("custom C%02x %s(0x%x, ) addr=%08x ra=%08x\n", + call, biosC0n[call], a0, entry, ra); + softCall(entry); + pc0 = ra; + PSXBIOS_LOG(" -> %08x\n", v0); + } + else if (biosC0[call]) + biosC0[call](); + + psxBranchTest(); +} + +// currently not used +static void hleBootstrap() { + CheckCdrom(); + LoadCdrom(); +} + +static void hleExecRet() { + const EXEC *header = (EXEC *)PSXM(s0); + + PSXBIOS_LOG("ExecRet %x: %x\n", s0, header->ret); + + ra = SWAP32(header->ret); + sp = SWAP32(header->_sp); + fp = SWAP32(header->_fp); + gp = SWAP32(header->_gp); + s0 = SWAP32(header->base); + + v0 = 1; + psxRegs.pc = ra; +} + +void (* const psxHLEt[24])() = { + hleDummy, hleA0, hleB0, hleC0, + hleBootstrap, hleExecRet, psxBiosException, hleDummy, + hleExc0_0_1, hleExc0_0_2, + hleExc0_1_1, hleExc0_1_2, hleExc0_2_2_syscall, + hleExc1_0_1, hleExc1_0_2, + hleExc1_1_1, hleExc1_1_2, + hleExc1_2_1, hleExc1_2_2, + hleExc1_3_1, hleExc1_3_2, + hleExc3_0_2_defint, + hleExcPadCard1, hleExcPadCard2, +}; + + #define bfreeze(ptr, size) { \ if (Mode == 1) memcpy(&psxR[base], ptr, size); \ if (Mode == 0) memcpy(ptr, &psxR[base], size); \ diff --git a/libpcsxcore/psxhle.c b/libpcsxcore/psxhle.c deleted file mode 100644 index 175b86ab..00000000 --- a/libpcsxcore/psxhle.c +++ /dev/null @@ -1,112 +0,0 @@ -/*************************************************************************** - * 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. * - ***************************************************************************/ - -/* -* Internal PSX HLE functions. -*/ - -#include "psxhle.h" - -#if 0 -#define PSXHLE_LOG SysPrintf -#else -#define PSXHLE_LOG(...) -#endif - -static void hleDummy() { - log_unhandled("hleDummy called @%08x ra=%08x\n", - psxRegs.pc - 4, psxRegs.GPR.n.ra); - psxRegs.pc = psxRegs.GPR.n.ra; - psxRegs.cycle += 1000; - - psxBranchTest(); -} - -static void hleA0() { - u32 call = psxRegs.GPR.n.t1 & 0xff; - - if (biosA0[call]) biosA0[call](); - - psxBranchTest(); -} - -static void hleB0() { - u32 call = psxRegs.GPR.n.t1 & 0xff; - - if (biosB0[call]) biosB0[call](); - - psxBranchTest(); -} - -static void hleC0() { - u32 call = psxRegs.GPR.n.t1 & 0xff; - - if (biosC0[call]) biosC0[call](); - - psxBranchTest(); -} - -static void hleBootstrap() { // 0xbfc00000 - PSXHLE_LOG("hleBootstrap\n"); - CheckCdrom(); - LoadCdrom(); - PSXHLE_LOG("CdromLabel: \"%s\": PC = %8.8lx (SP = %8.8lx)\n", CdromLabel, psxRegs.pc, psxRegs.GPR.n.sp); -} - -typedef struct { - u32 _pc0; - u32 gp0; - u32 t_addr; - u32 t_size; - u32 d_addr; - u32 d_size; - u32 b_addr; - u32 b_size; - u32 S_addr; - u32 s_size; - u32 _sp,_fp,_gp,ret,base; -} EXEC; - -static void hleExecRet() { - EXEC *header = (EXEC*)PSXM(psxRegs.GPR.n.s0); - - PSXHLE_LOG("ExecRet %x: %x\n", psxRegs.GPR.n.s0, header->ret); - - psxRegs.GPR.n.ra = SWAP32(header->ret); - psxRegs.GPR.n.sp = SWAP32(header->_sp); - psxRegs.GPR.n.fp = SWAP32(header->_fp); - psxRegs.GPR.n.gp = SWAP32(header->_gp); - psxRegs.GPR.n.s0 = SWAP32(header->base); - - psxRegs.GPR.n.v0 = 1; - psxRegs.pc = psxRegs.GPR.n.ra; -} - -void (* const psxHLEt[24])() = { - hleDummy, hleA0, hleB0, hleC0, - hleBootstrap, hleExecRet, psxBiosException, hleDummy, - hleExc0_0_1, hleExc0_0_2, - hleExc0_1_1, hleExc0_1_2, hleExc0_2_2_syscall, - hleExc1_0_1, hleExc1_0_2, - hleExc1_1_1, hleExc1_1_2, - hleExc1_2_1, hleExc1_2_2, - hleExc1_3_1, hleExc1_3_2, - hleExc3_0_2_defint, - hleExcPadCard1, hleExcPadCard2, -}; diff --git a/libpcsxcore/psxhle.h b/libpcsxcore/psxhle.h index b5508725..e6d2df81 100644 --- a/libpcsxcore/psxhle.h +++ b/libpcsxcore/psxhle.h @@ -28,16 +28,6 @@ extern "C" { #include "r3000a.h" #include "plugins.h" -void hleExc0_0_1(); void hleExc0_0_2(); -void hleExc0_1_1(); void hleExc0_1_2(); -void hleExc0_2_2_syscall(); -void hleExc1_0_1(); void hleExc1_0_2(); -void hleExc1_1_1(); void hleExc1_1_2(); -void hleExc1_2_1(); void hleExc1_2_2(); -void hleExc1_3_1(); void hleExc1_3_2(); -void hleExc3_0_2_defint(); -void hleExcPadCard1(); void hleExcPadCard2(); - enum hle_op { hleop_dummy = 0, hleop_a0, hleop_b0, hleop_c0, hleop_bootstrap, hleop_execret, hleop_exception, hleop_unused, -- 2.39.5