libretro / pcsx_rearmed

ARM optimized PCSX fork
GNU General Public License v2.0
168 stars 120 forks source link

lightrec and HLE #755

Closed notaz closed 1 year ago

notaz commented 1 year ago

As I can no longer find games that don't work with HLE (doesn't mean there aren't any, but still), I think it's time to fix lightrec to work with it. So that the emu would just work without any setup.

It should either call back into the emu on the fake instruction, or just return with LIGHTREC_EXIT_RESERVED_INSTRUCTION or something like that. I guess the later makes the most sense in lightrec's case, and I can handle it from there. This would also allow to emulate reserved instruction exception properly. It should do that at least when the op field of the instruction is 0x3b for HLE stuff to work.

@pcercuei

pcercuei commented 1 year ago

Do we need to emulate the "reserved instruction" exception? Are there games that rely on that?

My plan in Lightrec was to consider that unknown instructions are dead code, because if they were to be executed it would crash the PSX. This in turn gives the optimizer insight on the code flow.

I can add support for the HLE opcode yes, and just return with a flag and the current PC set to the address of the opcode.

pcercuei commented 1 year ago

@notaz try the handle-unknown-opcodes branch I just pushed in Lightrec.

notaz commented 1 year ago

@notaz try the handle-unknown-opcodes branch I just pushed in Lightrec.

Thanks. I've tried to do a quick implementation and everything worked on first try - nice. If you're happy with your part I think we can merge.

Do we need to emulate the "reserved instruction" exception? Are there games that rely on that?

None that I'm aware of. However there are some synthetic tests and some people use them to compare emulator "accuracy", like here: https://www.reddit.com/r/emulation/comments/3jwj2b/android_ps1_emulator_accuracy_testing_results/

if they were to be executed it would crash the PSX

That's only true if the game does nothing. The BIOS API offers at least several options to install a handler (an event or an exception chain entry) that could handle reserved instructions and take any action like acting like some syscall, or emulating the FPU if they wanted. The exception vector is also in RAM with some blocks of nops, supposedly left for hooking custom handlers. I've seen games installing such handlers but haven't looked what they're for. Could also just be some leftover debug code or something.