The PCSX2 dynarec (dynamic recompiler) is not currently modular in a way that allows it to be built as a separate .so (shared object library). However, your idea to isolate the x86-specific parts and run them in an x86 emulator like Box64 while building the rest of the project natively for aarch64 is theoretically possible, but would require significant engineering effort. Here's an analysis based on the provided context:
1. Dynarec as a Part of PCSX2
The dynarec is tightly integrated into the PCSX2 core. For instance:
- The
Vif_Dynarec (VIF unit dynamic recompiler) is implemented for both x86 and ARM64 architectures (Vif_Dynarec.cpp for both x86 and arm64), and its behavior depends on the target platform.
- The dynarec is deeply tied to memory management, internal state, and specific emulator subsystems like VU (Vector Units), EE (Emotion Engine), and IOP (Input/Output Processor). For example:
psxRecLUT, recRAM, and recBlocks in iR3000A.cpp are global structures tightly coupled with the recompiler.
- The recompilers (
VifUnpackSSE_Dynarec and VifUnpackNEON_Dynarec) depend on precise memory layouts and CPU features.
2. Challenges in Extracting the Dynarec
The dynarec is not an independent module, and making it a separate .so would require decoupling it from the rest of the PCSX2 codebase. Some challenges include:
-
Memory Layout and State Synchronization:
- The dynarec needs to access emulator memory directly (e.g.,
VURegs, HostMemoryMap, and nVifStruct).
- It also works with internal emulator states, like
psxRegs and psxRecLUT.
- To separate the dynarec, you’d need to create a shared memory model or an API to synchronize state between the main emulator and the dynarec running in a separate process or under Box64.
-
Tight Integration with Emulator Subsystems:
- The dynarec interacts directly with subsystems like the VIF, EE, and IOP. These interactions are not abstracted behind a clean API and are instead implemented at a very low level.
-
Platform-Specific Dependencies:
- The dynarec is platform-specific. For example, the x86 version uses SSE (
Vif_UnpackSSE.h) and x86-specific assembly (x86Emitter in iR3000A.cpp).
- The ARM64 version uses NEON (
Vif_UnpackNEON.h) and ARM-specific assembly (arm64/Vif_UnpackNEON.h).
- To use Box64, you would need to ensure that Box64 can emulate these platform-specific instructions correctly.
-
Compilation and Linkage:
- The current build system (CMake) does not support building the dynarec as a separate library. It would need to be refactored to allow building the dynarec independently.
3. Box64 Integration Feasibility
Running the x86 dynarec via Box64 while building the rest of PCSX2 natively for aarch64 could work in theory:
- You would compile the rest of PCSX2 for native aarch64, excluding the x86-specific dynarec code.
- The dynarec could be replaced with a stub or interface layer that communicates with an x86 binary running in Box64.
However, this requires:
-
Abstracting the Dynarec Interface:
- Define a clean API for the dynarec, so the main emulator can call it as a black-box library.
- Pass emulator state (e.g., memory, registers, VIF/EE/IOP states) to the dynarec via the API.
-
Building the Dynarec Separately:
- Compile the x86 dynarec as a standalone
.so that can be executed in Box64.
- Ensure the dynarec
.so correctly links all necessary x86-specific runtime dependencies.
-
State Synchronization:
- Implement a shared memory or IPC mechanism to synchronize state between the aarch64 emulator and the x86 dynarec.
-
Performance Considerations:
- Using Box64 introduces an extra layer of emulation, which could significantly slow down the dynarec. This might negate the performance benefits of having a dynarec.
4. Alternative Approaches
If your goal is to run PCSX2 efficiently on aarch64:
-
Focus on ARM64 Recompilers:
- PCSX2 already has ARM64-specific code for some subsystems (
Vif_UnpackNEON.h, arm64/Vif_Dynarec.cpp).
- Contributing to the ARM64 recompilers (e.g., EE/VU/IOP recompilers) would provide a native aarch64 solution without relying on Box64.
-
Interpretive Mode:
- PCSX2 can run in interpreter mode, which avoids the need for a dynarec. While slower, this could be used as a starting point for further ARM64 optimizations.
5. Conclusion
The PCSX2 dynarec is not modular and cannot be easily built as a separate .so without major refactoring. Running the x86 dynarec via Box64 is theoretically possible, but it would require:
- Abstracting the dynarec interface.
- Decoupling the dynarec from the rest of the emulator.
- Implementing state synchronization and shared memory mechanisms.
Given the challenges, focusing on enhancing the ARM64 recompilers in PCSX2 may be a more practical and performant long-term solution.