Skip to content

Commit

Permalink
CPU: HLE implementation of PCDrv (host file access)
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Apr 29, 2023
1 parent 5439718 commit 84e5fbe
Show file tree
Hide file tree
Showing 13 changed files with 430 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -76,6 +76,8 @@ add_library(core
negcon.h
pad.cpp
pad.h
pcdrv.cpp
pcdrv.h
pgxp.cpp
pgxp.h
playstation_mouse.cpp
Expand Down
21 changes: 21 additions & 0 deletions src/core/bus.cpp
Expand Up @@ -2019,6 +2019,27 @@ bool SafeReadMemoryWord(VirtualMemoryAddress addr, u32* value)
return true;
}

bool SafeReadMemoryCString(VirtualMemoryAddress addr, std::string* value, u32 max_length /*= 1024*/)
{
value->clear();

u8 ch;
while (SafeReadMemoryByte(addr, &ch))
{
if (ch == 0)
return true;

value->push_back(ch);
if (value->size() >= max_length)
return true;

addr++;
}

value->clear();
return false;
}

bool SafeWriteMemoryByte(VirtualMemoryAddress addr, u8 value)
{
u32 temp = ZeroExtend32(value);
Expand Down
2 changes: 2 additions & 0 deletions src/core/core.vcxproj
Expand Up @@ -64,6 +64,7 @@
<ClCompile Include="negcon.cpp" />
<ClCompile Include="pad.cpp" />
<ClCompile Include="controller.cpp" />
<ClCompile Include="pcdrv.cpp" />
<ClCompile Include="pgxp.cpp" />
<ClCompile Include="playstation_mouse.cpp" />
<ClCompile Include="psf_loader.cpp" />
Expand Down Expand Up @@ -139,6 +140,7 @@
<ClInclude Include="negcon.h" />
<ClInclude Include="pad.h" />
<ClInclude Include="controller.h" />
<ClInclude Include="pcdrv.h" />
<ClInclude Include="pgxp.h" />
<ClInclude Include="playstation_mouse.h" />
<ClInclude Include="psf_loader.h" />
Expand Down
2 changes: 2 additions & 0 deletions src/core/core.vcxproj.filters
Expand Up @@ -59,6 +59,7 @@
<ClCompile Include="gpu_hw_d3d12.cpp" />
<ClCompile Include="host.cpp" />
<ClCompile Include="game_database.cpp" />
<ClCompile Include="pcdrv.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="types.h" />
Expand Down Expand Up @@ -125,5 +126,6 @@
<ClInclude Include="achievements.h" />
<ClInclude Include="game_database.h" />
<ClInclude Include="input_types.h" />
<ClInclude Include="pcdrv.h" />
</ItemGroup>
</Project>
20 changes: 19 additions & 1 deletion src/core/cpu_core.cpp
Expand Up @@ -11,6 +11,7 @@
#include "cpu_recompiler_thunks.h"
#include "gte.h"
#include "host.h"
#include "pcdrv.h"
#include "pgxp.h"
#include "settings.h"
#include "system.h"
Expand Down Expand Up @@ -293,6 +294,20 @@ void RaiseException(Exception excode)
g_state.current_instruction_pc, GetExceptionVector());
}

void RaiseBreakException(u32 CAUSE_bits, u32 EPC, u32 instruction_bits)
{
if (PCDrv::HandleSyscall(instruction_bits, g_state.regs))
{
// immediately return
g_state.regs.npc = EPC + 4;
FlushPipeline();
return;
}

// normal exception
RaiseException(CAUSE_bits, EPC, GetExceptionVector());
}

void SetExternalInterrupt(u8 bit)
{
g_state.cop0_regs.cause.Ip |= static_cast<u8>(1u << bit);
Expand Down Expand Up @@ -1109,7 +1124,10 @@ ALWAYS_INLINE_RELEASE static void ExecuteInstruction()

case InstructionFunct::break_:
{
RaiseException(Exception::BP);
RaiseBreakException(Cop0Registers::CAUSE::MakeValueForException(
Exception::BP, g_state.current_instruction_in_branch_delay_slot,
g_state.current_instruction_was_branch_taken, g_state.current_instruction.cop.cop_n),
g_state.current_instruction_pc, g_state.current_instruction.bits);
}
break;

Expand Down
2 changes: 2 additions & 0 deletions src/core/cpu_core.h
Expand Up @@ -8,6 +8,7 @@
#include "types.h"
#include <array>
#include <optional>
#include <string>
#include <vector>

class StateWrapper;
Expand Down Expand Up @@ -147,6 +148,7 @@ ALWAYS_INLINE bool InKernelMode()
bool SafeReadMemoryByte(VirtualMemoryAddress addr, u8* value);
bool SafeReadMemoryHalfWord(VirtualMemoryAddress addr, u16* value);
bool SafeReadMemoryWord(VirtualMemoryAddress addr, u32* value);
bool SafeReadMemoryCString(VirtualMemoryAddress addr, std::string* value, u32 max_length = 1024);
bool SafeWriteMemoryByte(VirtualMemoryAddress addr, u8 value);
bool SafeWriteMemoryHalfWord(VirtualMemoryAddress addr, u16 value);
bool SafeWriteMemoryWord(VirtualMemoryAddress addr, u32 value);
Expand Down
1 change: 1 addition & 0 deletions src/core/cpu_core_private.h
Expand Up @@ -10,6 +10,7 @@ namespace CPU {
// exceptions
void RaiseException(Exception excode);
void RaiseException(u32 CAUSE_bits, u32 EPC);
void RaiseBreakException(u32 CAUSE_bits, u32 EPC, u32 instruction_bits);

ALWAYS_INLINE bool HasPendingInterrupt()
{
Expand Down
13 changes: 11 additions & 2 deletions src/core/cpu_recompiler_code_generator.cpp
Expand Up @@ -931,8 +931,17 @@ void CodeGenerator::GenerateExceptionExit(const CodeBlockInstruction& cbi, Excep
m_register_cache.FlushAllGuestRegisters(true, true);
m_register_cache.FlushLoadDelay(true);

EmitFunctionCall(nullptr, static_cast<void (*)(u32, u32)>(&CPU::RaiseException), CAUSE_bits,
GetCurrentInstructionPC());
if (excode == Exception::BP)
{
EmitFunctionCall(nullptr, static_cast<void (*)(u32, u32, u32)>(&CPU::RaiseBreakException), CAUSE_bits,
GetCurrentInstructionPC(), Value::FromConstantU32(cbi.instruction.bits));
}
else
{
EmitFunctionCall(nullptr, static_cast<void (*)(u32, u32)>(&CPU::RaiseException), CAUSE_bits,
GetCurrentInstructionPC());
}

return;
}

Expand Down

0 comments on commit 84e5fbe

Please sign in to comment.