From 597d07bf24ff585f847bdb752eff35dbab7f3cb9 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Mon, 15 Sep 2014 02:17:24 +0400 Subject: [PATCH] Small refactoring --- Utilities/BEType.h | 58 +++++++-------- Utilities/SMutex.cpp | 21 ------ Utilities/SMutex.h | 42 ----------- Utilities/Thread.cpp | 32 +++++++-- Utilities/Thread.h | 7 +- Utilities/rFile.cpp | 6 +- rpcs3/Crypto/unedat.cpp | 2 +- rpcs3/Crypto/utils.cpp | 4 +- rpcs3/Emu/ARMv7/ARMv7Decoder.h | 6 +- rpcs3/Emu/ARMv7/ARMv7Interpreter.h | 2 +- rpcs3/Emu/ARMv7/ARMv7Thread.cpp | 11 +-- rpcs3/Emu/ARMv7/ARMv7Thread.h | 3 +- rpcs3/Emu/CPU/CPUDecoder.h | 2 +- rpcs3/Emu/CPU/CPUDisAsm.h | 2 +- rpcs3/Emu/CPU/CPUThread.cpp | 9 ++- rpcs3/Emu/CPU/CPUThread.h | 38 +++++----- rpcs3/Emu/Cell/MFC.h | 2 +- rpcs3/Emu/Cell/PPCDecoder.cpp | 2 +- rpcs3/Emu/Cell/PPCDecoder.h | 2 +- rpcs3/Emu/Cell/PPCThread.cpp | 5 +- rpcs3/Emu/Cell/PPCThread.h | 6 -- rpcs3/Emu/Cell/PPUInterpreter.h | 36 +++++----- rpcs3/Emu/Cell/PPUOpcodes.h | 2 +- rpcs3/Emu/Cell/PPUThread.cpp | 54 +------------- rpcs3/Emu/Cell/PPUThread.h | 7 +- rpcs3/Emu/Cell/RawSPUThread.cpp | 2 +- rpcs3/Emu/Cell/SPUInterpreter.h | 34 ++++----- rpcs3/Emu/Cell/SPUOpcodes.h | 2 +- rpcs3/Emu/Cell/SPURecompiler.h | 60 ++++++++-------- rpcs3/Emu/Cell/SPURecompilerCore.cpp | 4 +- rpcs3/Emu/Cell/SPUThread.cpp | 15 +--- rpcs3/Emu/Cell/SPUThread.h | 1 - rpcs3/Emu/Memory/Memory.cpp | 2 +- rpcs3/Emu/Memory/Memory.h | 10 +-- rpcs3/Emu/Memory/vm.h | 88 ++++++++++++++++++++--- rpcs3/Emu/Memory/vm_var.h | 4 +- rpcs3/Emu/SysCalls/CB_FUNC.h | 11 ++- rpcs3/Emu/SysCalls/Modules/cellGame.cpp | 24 +++---- rpcs3/Emu/SysCalls/SC_FUNC.h | 11 ++- rpcs3/Emu/SysCalls/lv2/sys_event_flag.cpp | 35 ++++++--- rpcs3/Emu/SysCalls/lv2/sys_ppu_thread.cpp | 2 +- rpcs3/Emu/SysCalls/lv2/sys_spu.cpp | 22 +++--- rpcs3/Emu/SysCalls/lv2/sys_tty.cpp | 2 +- rpcs3/Emu/System.cpp | 27 ++++--- rpcs3/Gui/GLGSFrame.cpp | 1 + rpcs3/Loader/ELF.h | 2 +- 46 files changed, 348 insertions(+), 372 deletions(-) diff --git a/Utilities/BEType.h b/Utilities/BEType.h index 50058489d4..64ee1d62bf 100644 --- a/Utilities/BEType.h +++ b/Utilities/BEType.h @@ -4,12 +4,6 @@ union u128 { - struct - { - u64 hi; - u64 lo; - }; - u64 _u64[2]; s64 _s64[2]; u32 _u32[4]; @@ -92,31 +86,27 @@ union u128 //operator bool() const { return _u64[0] != 0 || _u64[1] != 0; } - static u128 from128(u64 hi, u64 lo) - { - u128 ret = { hi, lo }; - return ret; - } - - static u128 from64(u64 src) - { - u128 ret = { 0, src }; - return ret; - } - - static u128 from32(u32 src) + static u128 from64(u64 _0, u64 _1 = 0) { u128 ret; - ret._u32[0] = src; - ret._u32[1] = 0; - ret._u32[2] = 0; - ret._u32[3] = 0; + ret._u64[0] = _0; + ret._u64[1] = _1; + return ret; + } + + static u128 from32(u32 _0, u32 _1 = 0, u32 _2 = 0, u32 _3 = 0) + { + u128 ret; + ret._u32[0] = _0; + ret._u32[1] = _1; + ret._u32[2] = _2; + ret._u32[3] = _3; return ret; } static u128 fromBit(u32 bit) { - u128 ret; + u128 ret = {}; ret._bit[bit] = true; return ret; } @@ -128,42 +118,42 @@ union u128 bool operator == (const u128& right) const { - return (lo == right.lo) && (hi == right.hi); + return (_u64[0] == right._u64[0]) && (_u64[1] == right._u64[1]); } bool operator != (const u128& right) const { - return (lo != right.lo) || (hi != right.hi); + return (_u64[0] != right._u64[0]) || (_u64[1] != right._u64[1]); } u128 operator | (const u128& right) const { - return from128(hi | right.hi, lo | right.lo); + return from64(_u64[0] | right._u64[0], _u64[1] | right._u64[1]); } u128 operator & (const u128& right) const { - return from128(hi & right.hi, lo & right.lo); + return from64(_u64[0] & right._u64[0], _u64[1] & right._u64[1]); } u128 operator ^ (const u128& right) const { - return from128(hi ^ right.hi, lo ^ right.lo); + return from64(_u64[0] ^ right._u64[0], _u64[1] ^ right._u64[1]); } u128 operator ~ () const { - return from128(~hi, ~lo); + return from64(~_u64[0], ~_u64[1]); } void clear() { - hi = lo = 0; + _u64[1] = _u64[0] = 0; } std::string to_hex() const { - return fmt::Format("%08x%08x%08x%08x", _u32[3], _u32[2], _u32[1], _u32[0]); + return fmt::Format("%16llx%16llx", _u64[1], _u64[0]); } std::string to_xyzw() const @@ -174,8 +164,8 @@ union u128 static __forceinline u128 byteswap(const u128 val) { u128 ret; - ret.lo = _byteswap_uint64(val.hi); - ret.hi = _byteswap_uint64(val.lo); + ret._u64[0] = _byteswap_uint64(val._u64[1]); + ret._u64[1] = _byteswap_uint64(val._u64[0]); return ret; } }; diff --git a/Utilities/SMutex.cpp b/Utilities/SMutex.cpp index 80b9fc849f..684429b60e 100644 --- a/Utilities/SMutex.cpp +++ b/Utilities/SMutex.cpp @@ -20,24 +20,3 @@ void SM_Sleep() std::this_thread::sleep_for(std::chrono::milliseconds(1)); } } - -thread_local size_t g_this_thread_id = 0; - -size_t SM_GetCurrentThreadId() -{ - return g_this_thread_id ? g_this_thread_id : g_this_thread_id = std::hash()(std::this_thread::get_id()); -} - -u32 SM_GetCurrentCPUThreadId() -{ - if (CPUThread* t = GetCurrentCPUThread()) - { - return t->GetId(); - } - return 0; -} - -be_t SM_GetCurrentCPUThreadIdBE() -{ - return be_t::MakeFromLE(SM_GetCurrentCPUThreadId()); -} diff --git a/Utilities/SMutex.h b/Utilities/SMutex.h index efd99b840f..8acc971894 100644 --- a/Utilities/SMutex.h +++ b/Utilities/SMutex.h @@ -2,9 +2,6 @@ bool SM_IsAborted(); void SM_Sleep(); -size_t SM_GetCurrentThreadId(); -u32 SM_GetCurrentCPUThreadId(); -be_t SM_GetCurrentCPUThreadIdBE(); enum SMutexResult { @@ -134,44 +131,5 @@ public: } }; -template -class SMutexLockerBase -{ - T& sm; -public: - const Tid tid; - - __forceinline SMutexLockerBase(T& _sm) - : sm(_sm) - , tid(get_tid()) - { - if (!tid) - { - if (!SM_IsAborted()) - { - assert(!"SMutexLockerBase: thread id == 0"); - } - return; - } - sm.lock(tid); - } - - __forceinline ~SMutexLockerBase() - { - if (tid) sm.unlock(tid); - } -}; - -typedef SMutexBase - SMutexGeneral; typedef SMutexBase SMutex; -typedef SMutexBase> - SMutexBE; - -typedef SMutexLockerBase - SMutexGeneralLocker; -typedef SMutexLockerBase - SMutexLocker; -typedef SMutexLockerBase, SM_GetCurrentCPUThreadIdBE> - SMutexBELocker; diff --git a/Utilities/Thread.cpp b/Utilities/Thread.cpp index ab804bd2e4..b24ad1036f 100644 --- a/Utilities/Thread.cpp +++ b/Utilities/Thread.cpp @@ -12,7 +12,27 @@ NamedThreadBase* GetCurrentNamedThread() void SetCurrentNamedThread(NamedThreadBase* value) { - g_tls_this_thread = value; + auto old_value = g_tls_this_thread; + + if (old_value == value) + { + return; + } + + if (value && value->m_tls_assigned.exchange(true)) + { + LOG_ERROR(GENERAL, "Thread '%s' was already assigned to g_tls_this_thread of another thread", value->GetThreadName().c_str()); + g_tls_this_thread = nullptr; + } + else + { + g_tls_this_thread = value; + } + + if (old_value) + { + old_value->m_tls_assigned = false; + } } std::string NamedThreadBase::GetThreadName() const @@ -73,14 +93,15 @@ void ThreadBase::Start() } catch (const char* e) { - LOG_ERROR(HLE, "%s: %s", GetThreadName().c_str(), e); + LOG_ERROR(GENERAL, "%s: %s", GetThreadName().c_str(), e); } catch (const std::string& e) { - LOG_ERROR(HLE, "%s: %s", GetThreadName().c_str(), e.c_str()); + LOG_ERROR(GENERAL, "%s: %s", GetThreadName().c_str(), e.c_str()); } m_alive = false; + SetCurrentNamedThread(nullptr); g_thread_count--; }); } @@ -160,13 +181,14 @@ void thread::start(std::function func) } catch (const char* e) { - LOG_ERROR(HLE, "%s: %s", name.c_str(), e); + LOG_ERROR(GENERAL, "%s: %s", name.c_str(), e); } catch (const std::string& e) { - LOG_ERROR(HLE, "%s: %s", name.c_str(), e.c_str()); + LOG_ERROR(GENERAL, "%s: %s", name.c_str(), e.c_str()); } + SetCurrentNamedThread(nullptr); g_thread_count--; }); } diff --git a/Utilities/Thread.h b/Utilities/Thread.h index 72daae43ab..2566cdf43b 100644 --- a/Utilities/Thread.h +++ b/Utilities/Thread.h @@ -5,16 +5,17 @@ static std::thread::id main_thread; class NamedThreadBase { std::string m_name; - std::condition_variable m_signal_cv; std::mutex m_signal_mtx; public: - NamedThreadBase(const std::string& name) : m_name(name) + std::atomic m_tls_assigned; + + NamedThreadBase(const std::string& name) : m_name(name), m_tls_assigned(false) { } - NamedThreadBase() + NamedThreadBase() : m_tls_assigned(false) { } diff --git a/Utilities/rFile.cpp b/Utilities/rFile.cpp index 302813e50a..554647c40b 100644 --- a/Utilities/rFile.cpp +++ b/Utilities/rFile.cpp @@ -88,9 +88,9 @@ bool rMkpath(const std::string &path) if(dir.size() == 0) continue; #ifdef _WIN32 - if((ret = _mkdir(dir.c_str())) && errno != EEXIST){ + if((ret = _mkdir(dir.c_str()) != 0) && errno != EEXIST){ #else - if((ret = mkdir(dir.c_str(), 0777)) && errno != EEXIST){ + if((ret = mkdir(dir.c_str(), 0777) != 0) && errno != EEXIST){ #endif return !ret; } @@ -269,7 +269,7 @@ size_t rFile::Write(const void *buffer, size_t count) bool rFile::Write(const std::string &text) { - return reinterpret_cast(handle)->Write(reinterpret_cast(text.c_str()),text.size()); + return reinterpret_cast(handle)->Write(reinterpret_cast(text.c_str()),text.size()) != 0; } bool rFile::Close() diff --git a/rpcs3/Crypto/unedat.cpp b/rpcs3/Crypto/unedat.cpp index b484cabf38..7cd9604ac6 100644 --- a/rpcs3/Crypto/unedat.cpp +++ b/rpcs3/Crypto/unedat.cpp @@ -444,7 +444,7 @@ void validate_data(const char* file_name, unsigned char *klicensee, NPD_HEADER * int title_hash_result = 0; int dev_hash_result = 0; - int file_name_length = strlen(file_name); + int file_name_length = (int)strlen(file_name); unsigned char *buf = new unsigned char[0x30 + file_name_length]; unsigned char key[0x10]; diff --git a/rpcs3/Crypto/utils.cpp b/rpcs3/Crypto/utils.cpp index b395643f20..45a71222e3 100644 --- a/rpcs3/Crypto/utils.cpp +++ b/rpcs3/Crypto/utils.cpp @@ -34,7 +34,7 @@ void xor_(unsigned char *dest, unsigned char *src1, unsigned char *src2, int siz // Hex string conversion auxiliary functions. u64 hex_to_u64(const char* hex_str) { - u32 length = strlen(hex_str); + u32 length = (u32)strlen(hex_str); u64 tmp = 0; u64 result = 0; char c; @@ -58,7 +58,7 @@ u64 hex_to_u64(const char* hex_str) void hex_to_bytes(unsigned char *data, const char *hex_str) { - u32 str_length = strlen(hex_str); + u32 str_length = (u32)strlen(hex_str); u32 data_length = str_length / 2; char tmp_buf[3] = {0, 0, 0}; diff --git a/rpcs3/Emu/ARMv7/ARMv7Decoder.h b/rpcs3/Emu/ARMv7/ARMv7Decoder.h index 6603e45e3e..298a23be51 100644 --- a/rpcs3/Emu/ARMv7/ARMv7Decoder.h +++ b/rpcs3/Emu/ARMv7/ARMv7Decoder.h @@ -18,11 +18,11 @@ public: return imm << 1; } - virtual u8 DecodeMemory(const u64 address) + virtual u8 DecodeMemory(const u32 address) { using namespace ARMv7_opcodes; - const u16 code0 = vm::psv::read16((u32)address); - const u16 code1 = vm::psv::read16((u32)address + 2); + const u16 code0 = vm::psv::read16(address); + const u16 code1 = vm::psv::read16(address + 2); switch(code0 >> 12) //15 - 12 { diff --git a/rpcs3/Emu/ARMv7/ARMv7Interpreter.h b/rpcs3/Emu/ARMv7/ARMv7Interpreter.h index bc2ae6f2c7..eaac69aafa 100644 --- a/rpcs3/Emu/ARMv7/ARMv7Interpreter.h +++ b/rpcs3/Emu/ARMv7/ARMv7Interpreter.h @@ -310,7 +310,7 @@ protected: void BL(u32 imm, u8 intstr_size) { - CPU.LR = ((u32)CPU.PC + intstr_size) | 1; + CPU.LR = (CPU.PC + intstr_size) | 1; CPU.SetBranch(CPU.PC + intstr_size + imm); } diff --git a/rpcs3/Emu/ARMv7/ARMv7Thread.cpp b/rpcs3/Emu/ARMv7/ARMv7Thread.cpp index a49c8ed3e9..3fe6595994 100644 --- a/rpcs3/Emu/ARMv7/ARMv7Thread.cpp +++ b/rpcs3/Emu/ARMv7/ARMv7Thread.cpp @@ -18,7 +18,7 @@ void ARMv7Thread::InitRegs() memset(GPR, 0, sizeof(GPR[0]) * 15); APSR.APSR = 0; IPSR.IPSR = 0; - SP = (u32)m_stack_point; + SP = m_stack_addr + m_stack_size; } void ARMv7Thread::InitStack() @@ -26,15 +26,8 @@ void ARMv7Thread::InitStack() if(!m_stack_addr) { m_stack_size = 0x10000; - m_stack_addr = Memory.Alloc(0x10000, 1); + m_stack_addr = (u32)Memory.Alloc(0x10000, 1); } - - m_stack_point = m_stack_addr + m_stack_size; -} - -u64 ARMv7Thread::GetFreeStackSize() const -{ - return SP - GetStackAddr(); } void ARMv7Thread::SetArg(const uint pos, const u64 arg) diff --git a/rpcs3/Emu/ARMv7/ARMv7Thread.h b/rpcs3/Emu/ARMv7/ARMv7Thread.h index 7a4234f00e..e95e9b49ec 100644 --- a/rpcs3/Emu/ARMv7/ARMv7Thread.h +++ b/rpcs3/Emu/ARMv7/ARMv7Thread.h @@ -74,13 +74,12 @@ public: return GPR[n]; } - return (u32)PC; + return PC; } public: virtual void InitRegs(); virtual void InitStack(); - virtual u64 GetFreeStackSize() const; virtual void SetArg(const uint pos, const u64 arg); public: diff --git a/rpcs3/Emu/CPU/CPUDecoder.h b/rpcs3/Emu/CPU/CPUDecoder.h index b9b47dd7f4..df28e54467 100644 --- a/rpcs3/Emu/CPU/CPUDecoder.h +++ b/rpcs3/Emu/CPU/CPUDecoder.h @@ -5,7 +5,7 @@ class CPUDecoder { public: - virtual u8 DecodeMemory(const u64 address)=0; + virtual u8 DecodeMemory(const u32 address)=0; virtual ~CPUDecoder() = default; }; diff --git a/rpcs3/Emu/CPU/CPUDisAsm.h b/rpcs3/Emu/CPU/CPUDisAsm.h index 4a7eca77c0..c9228e01b8 100644 --- a/rpcs3/Emu/CPU/CPUDisAsm.h +++ b/rpcs3/Emu/CPU/CPUDisAsm.h @@ -41,7 +41,7 @@ protected: public: std::string last_opcode; - u64 dump_pc; + u32 dump_pc; u8* offset; protected: diff --git a/rpcs3/Emu/CPU/CPUThread.cpp b/rpcs3/Emu/CPU/CPUThread.cpp index d27a608a49..b0a73b0a30 100644 --- a/rpcs3/Emu/CPU/CPUThread.cpp +++ b/rpcs3/Emu/CPU/CPUThread.cpp @@ -11,7 +11,7 @@ CPUThread* GetCurrentCPUThread() { - return (CPUThread*)GetCurrentNamedThread(); + return dynamic_cast(GetCurrentNamedThread()); } CPUThread::CPUThread(CPUThreadType type) @@ -75,7 +75,6 @@ void CPUThread::CloseStack() } m_stack_size = 0; - m_stack_point = 0; } void CPUThread::SetId(const u32 id) @@ -131,7 +130,7 @@ int CPUThread::ThreadStatus() return CPUThread_Running; } -void CPUThread::SetEntry(const u64 pc) +void CPUThread::SetEntry(const u32 pc) { entry = pc; } @@ -150,7 +149,7 @@ void CPUThread::NextPc(u8 instr_size) } } -void CPUThread::SetBranch(const u64 pc, bool record_branch) +void CPUThread::SetBranch(const u32 pc, bool record_branch) { m_is_branch = true; nPC = pc; @@ -159,7 +158,7 @@ void CPUThread::SetBranch(const u64 pc, bool record_branch) CallStackBranch(pc); } -void CPUThread::SetPc(const u64 pc) +void CPUThread::SetPc(const u32 pc) { PC = pc; } diff --git a/rpcs3/Emu/CPU/CPUThread.h b/rpcs3/Emu/CPU/CPUThread.h index c03bcd934f..1789891f09 100644 --- a/rpcs3/Emu/CPU/CPUThread.h +++ b/rpcs3/Emu/CPU/CPUThread.h @@ -30,15 +30,14 @@ protected: u32 m_error; u32 m_id; u64 m_prio; - u64 m_offset; + u32 m_offset; CPUThreadType m_type; bool m_joinable; bool m_joining; bool m_is_step; - u64 m_stack_addr; + u32 m_stack_addr; u32 m_stack_size; - u64 m_stack_point; u64 m_exit_status; @@ -50,22 +49,19 @@ public: virtual void InitStack()=0; virtual void CloseStack(); - u64 GetStackAddr() const { return m_stack_addr; } + u32 GetStackAddr() const { return m_stack_addr; } u32 GetStackSize() const { return m_stack_size; } - virtual u64 GetFreeStackSize() const=0; - void SetStackAddr(u64 stack_addr) { m_stack_addr = stack_addr; } + void SetStackAddr(u32 stack_addr) { m_stack_addr = stack_addr; } void SetStackSize(u32 stack_size) { m_stack_size = stack_size; } - virtual void SetArg(const uint pos, const u64 arg) = 0; - void SetId(const u32 id); void SetName(const std::string& name); void SetPrio(const u64 prio) { m_prio = prio; } - void SetOffset(const u64 offset) { m_offset = offset; } + void SetOffset(const u32 offset) { m_offset = offset; } void SetExitStatus(const u64 status) { m_exit_status = status; } - u64 GetOffset() const { return m_offset; } + u32 GetOffset() const { return m_offset; } u64 GetExitStatus() const { return m_exit_status; } u64 GetPrio() const { return m_prio; } @@ -118,9 +114,9 @@ public: } public: - u64 entry; - u64 PC; - u64 nPC; + u32 entry; + u32 PC; + u32 nPC; u64 cycle; bool m_is_branch; @@ -155,9 +151,9 @@ public: int ThreadStatus(); void NextPc(u8 instr_size); - void SetBranch(const u64 pc, bool record_branch = false); - void SetPc(const u64 pc); - void SetEntry(const u64 entry); + void SetBranch(const u32 pc, bool record_branch = false); + void SetPc(const u32 pc); + void SetEntry(const u32 entry); void SetError(const u32 error); @@ -185,8 +181,6 @@ public: void Resume(); void Stop(); - virtual void AddArgv(const std::string& arg) {} - virtual std::string RegsToString() = 0; virtual std::string ReadRegString(const std::string& reg) = 0; virtual bool WriteRegString(const std::string& reg, std::string value) = 0; @@ -196,8 +190,8 @@ public: struct CallStackItem { - u64 pc; - u64 branch_pc; + u32 pc; + u32 branch_pc; }; std::vector m_call_stack; @@ -214,7 +208,7 @@ public: return ret; } - void CallStackBranch(u64 pc) + void CallStackBranch(u32 pc) { //look if we're jumping back and if so pop the stack back to that position auto res = std::find_if(m_call_stack.rbegin(), m_call_stack.rend(), @@ -237,7 +231,7 @@ public: m_call_stack.push_back(new_item); } - virtual u64 CallStackGetNextPC(u64 pc) + virtual u32 CallStackGetNextPC(u32 pc) { return pc + 4; } diff --git a/rpcs3/Emu/Cell/MFC.h b/rpcs3/Emu/Cell/MFC.h index 7bd4281f7c..5669daabe9 100644 --- a/rpcs3/Emu/Cell/MFC.h +++ b/rpcs3/Emu/Cell/MFC.h @@ -61,5 +61,5 @@ enum struct DMAC { - u64 ls_offset; + u32 ls_offset; }; diff --git a/rpcs3/Emu/Cell/PPCDecoder.cpp b/rpcs3/Emu/Cell/PPCDecoder.cpp index 4c36affd87..de2b55fdf9 100644 --- a/rpcs3/Emu/Cell/PPCDecoder.cpp +++ b/rpcs3/Emu/Cell/PPCDecoder.cpp @@ -2,7 +2,7 @@ #include "Emu/Memory/Memory.h" #include "PPCDecoder.h" -u8 PPCDecoder::DecodeMemory(const u64 address) +u8 PPCDecoder::DecodeMemory(const u32 address) { u32 instr = vm::read32(address); Decode(instr); diff --git a/rpcs3/Emu/Cell/PPCDecoder.h b/rpcs3/Emu/Cell/PPCDecoder.h index 8c1d55f405..b13ab08efd 100644 --- a/rpcs3/Emu/Cell/PPCDecoder.h +++ b/rpcs3/Emu/Cell/PPCDecoder.h @@ -7,7 +7,7 @@ class PPCDecoder : public CPUDecoder public: virtual void Decode(const u32 code)=0; - virtual u8 DecodeMemory(const u64 address); + virtual u8 DecodeMemory(const u32 address); virtual ~PPCDecoder() = default; }; diff --git a/rpcs3/Emu/Cell/PPCThread.cpp b/rpcs3/Emu/Cell/PPCThread.cpp index 3e75620609..4248fd58a8 100644 --- a/rpcs3/Emu/Cell/PPCThread.cpp +++ b/rpcs3/Emu/Cell/PPCThread.cpp @@ -16,7 +16,6 @@ PPCThread* GetCurrentPPCThread() PPCThread::PPCThread(CPUThreadType type) : CPUThread(type) { - memset(m_args, 0, sizeof(m_args)); } PPCThread::~PPCThread() @@ -31,9 +30,7 @@ void PPCThread::InitStack() { if(m_stack_addr) return; if(m_stack_size == 0) m_stack_size = 0x10000; - m_stack_addr = Memory.StackMem.AllocAlign(m_stack_size, 0x100); - - m_stack_point = m_stack_addr + m_stack_size; + m_stack_addr = (u32)Memory.StackMem.AllocAlign(m_stack_size, 0x100); /* m_stack_point += m_stack_size - 0x10; m_stack_point &= -0x10; diff --git a/rpcs3/Emu/Cell/PPCThread.h b/rpcs3/Emu/Cell/PPCThread.h index e0fbd5888c..f06cf4eaee 100644 --- a/rpcs3/Emu/Cell/PPCThread.h +++ b/rpcs3/Emu/Cell/PPCThread.h @@ -3,16 +3,10 @@ class PPCThread : public CPUThread { -protected: - u64 m_args[4]; - std::vector m_argv_addr; - public: virtual void InitRegs()=0; virtual void InitStack(); - virtual void SetArg(const uint pos, const u64 arg) { assert(pos < 4); m_args[pos] = arg; } - virtual std::string GetThreadName() const { return (GetFName() + fmt::Format("[0x%08llx]", PC)); diff --git a/rpcs3/Emu/Cell/PPUInterpreter.h b/rpcs3/Emu/Cell/PPUInterpreter.h index 267905f4a9..8d79d8f3e9 100644 --- a/rpcs3/Emu/Cell/PPUInterpreter.h +++ b/rpcs3/Emu/Cell/PPUInterpreter.h @@ -72,11 +72,11 @@ private: if(Ini.HLELogging.GetValue()) { - LOG_WARNING(PPU, "SysCall[0x%llx ('%s')] done with code [0x%llx]! #pc: 0x%llx", + LOG_WARNING(PPU, "SysCall[0x%llx ('%s')] done with code [0x%llx]! #pc: 0x%x", sc, SysCalls::GetHLEFuncName((u32)sc).c_str(), CPU.GPR[3], CPU.PC); } #ifdef HLE_CALL_DEBUG - LOG_NOTICE(PPU, "SysCall[%lld] done with code [0x%llx]! #pc: 0x%llx", sc, CPU.GPR[3], CPU.PC); + LOG_NOTICE(PPU, "SysCall[%lld] done with code [0x%llx]! #pc: 0x%x", sc, CPU.GPR[3], CPU.PC); #endif CPU.m_last_syscall = old_sc; @@ -2081,7 +2081,7 @@ private: { if (CheckCondition(bo, bi)) { - const u64 nextLR = CPU.PC + 4; + const u32 nextLR = CPU.PC + 4; CPU.SetBranch(branchTarget((aa ? 0 : CPU.PC), bd), lk); if(lk) CPU.LR = nextLR; } @@ -2096,7 +2096,7 @@ private: Emu.GetSFuncManager().StaticExecute((u32)CPU.GPR[11]); if (Ini.HLELogging.GetValue()) { - LOG_NOTICE(PPU, "'%s' done with code[0x%llx]! #pc: 0x%llx", + LOG_NOTICE(PPU, "'%s' done with code[0x%llx]! #pc: 0x%x", Emu.GetSFuncManager()[CPU.GPR[11]]->name, CPU.GPR[3], CPU.PC); } break; @@ -2107,7 +2107,7 @@ private: } void B(s32 ll, u32 aa, u32 lk) { - const u64 nextLR = CPU.PC + 4; + const u32 nextLR = CPU.PC + 4; CPU.SetBranch(branchTarget(aa ? 0 : CPU.PC, ll), lk); if(lk) CPU.LR = nextLR; } @@ -2119,8 +2119,8 @@ private: { if (CheckCondition(bo, bi)) { - const u64 nextLR = CPU.PC + 4; - CPU.SetBranch(branchTarget(0, CPU.LR), true); + const u32 nextLR = CPU.PC + 4; + CPU.SetBranch(branchTarget(0, (u32)CPU.LR), true); if(lk) CPU.LR = nextLR; } } @@ -2172,8 +2172,8 @@ private: { if(bo & 0x10 || CPU.IsCR(bi) == (bo & 0x8)) { - const u64 nextLR = CPU.PC + 4; - CPU.SetBranch(branchTarget(0, CPU.CTR), true); + const u32 nextLR = CPU.PC + 4; + CPU.SetBranch(branchTarget(0, (u32)CPU.CTR), true); if(lk) CPU.LR = nextLR; } } @@ -2944,10 +2944,10 @@ private: void LVLX(u32 vd, u32 ra, u32 rb) { const u64 addr = ra ? CPU.GPR[ra] + CPU.GPR[rb] : CPU.GPR[rb]; - const u8 eb = addr & 0xf; + const u32 eb = addr & 0xf; CPU.VPR[vd].clear(); - for (u32 i = 0; i < 16 - eb; ++i) CPU.VPR[vd]._u8[15 - i] = vm::read8(addr + i); + for (u32 i = 0; i < 16u - eb; ++i) CPU.VPR[vd]._u8[15 - i] = vm::read8(addr + i); } void LDBRX(u32 rd, u32 ra, u32 rb) { @@ -3043,9 +3043,9 @@ private: void STVLX(u32 vs, u32 ra, u32 rb) { const u64 addr = ra ? CPU.GPR[ra] + CPU.GPR[rb] : CPU.GPR[rb]; - const u8 eb = addr & 0xf; + const u32 eb = addr & 0xf; - for (u32 i = 0; i < 16 - eb; ++i) vm::write8(addr + i, CPU.VPR[vs]._u8[15 - i]); + for (u32 i = 0; i < 16u - eb; ++i) vm::write8(addr + i, CPU.VPR[vs]._u8[15 - i]); } void STSWX(u32 rs, u32 ra, u32 rb) { @@ -3113,10 +3113,10 @@ private: void LVLXL(u32 vd, u32 ra, u32 rb) { const u64 addr = ra ? CPU.GPR[ra] + CPU.GPR[rb] : CPU.GPR[rb]; - const u8 eb = addr & 0xf; + const u32 eb = addr & 0xf; CPU.VPR[vd].clear(); - for (u32 i = 0; i < 16 - eb; ++i) CPU.VPR[vd]._u8[15 - i] = vm::read8(addr + i); + for (u32 i = 0; i < 16u - eb; ++i) CPU.VPR[vd]._u8[15 - i] = vm::read8(addr + i); } void LHBRX(u32 rd, u32 ra, u32 rb) { @@ -3195,9 +3195,9 @@ private: void STVLXL(u32 vs, u32 ra, u32 rb) { const u64 addr = ra ? CPU.GPR[ra] + CPU.GPR[rb] : CPU.GPR[rb]; - const u8 eb = addr & 0xf; + const u32 eb = addr & 0xf; - for (u32 i = 0; i < 16 - eb; ++i) vm::write8(addr + i, CPU.VPR[vs]._u8[15 - i]); + for (u32 i = 0; i < 16u - eb; ++i) vm::write8(addr + i, CPU.VPR[vs]._u8[15 - i]); } void STHBRX(u32 rs, u32 ra, u32 rb) { @@ -3983,7 +3983,7 @@ private: void UNK(const std::string& err, bool pause = true) { - LOG_ERROR(PPU, err + fmt::Format(" #pc: 0x%llx", CPU.PC)); + LOG_ERROR(PPU, err + fmt::Format(" #pc: 0x%x", CPU.PC)); if(!pause) return; diff --git a/rpcs3/Emu/Cell/PPUOpcodes.h b/rpcs3/Emu/Cell/PPUOpcodes.h index ec96bac715..150275ae9c 100644 --- a/rpcs3/Emu/Cell/PPUOpcodes.h +++ b/rpcs3/Emu/Cell/PPUOpcodes.h @@ -451,7 +451,7 @@ namespace PPU_opcodes class PPUOpcodes { public: - static u64 branchTarget(const u64 pc, const u64 imm) + static u32 branchTarget(const u32 pc, const u32 imm) { return pc + (imm & ~0x3ULL); } diff --git a/rpcs3/Emu/Cell/PPUThread.cpp b/rpcs3/Emu/Cell/PPUThread.cpp index b0c69f4daa..03e49b8aa0 100644 --- a/rpcs3/Emu/Cell/PPUThread.cpp +++ b/rpcs3/Emu/Cell/PPUThread.cpp @@ -51,14 +51,6 @@ void PPUThread::DoReset() cycle = 0; } -void PPUThread::AddArgv(const std::string& arg) -{ - m_stack_point -= arg.length() + 1; - m_stack_point = AlignAddr(m_stack_point, 0x10) - 0x10; - m_argv_addr.push_back(m_stack_point); - memcpy(vm::get_ptr(m_stack_point), arg.c_str(), arg.size() + 1); -} - void PPUThread::InitRegs() { const u32 pc = entry ? vm::read32(entry) : 0; @@ -91,46 +83,9 @@ void PPUThread::InitRegs() } */ - m_stack_point = AlignAddr(m_stack_point, 0x200) - 0x200; - - GPR[1] = m_stack_point; + GPR[1] = AlignAddr(m_stack_addr + m_stack_size, 0x200) - 0x200; GPR[2] = rtoc; - /* - for(int i=4; i<32; ++i) - { - if(i != 6) - GPR[i] = (i+1) * 0x10000; - } - */ - if(m_argv_addr.size()) - { - u64 argc = m_argv_addr.size(); - m_stack_point -= 0xc + 4 * argc; - u64 argv = m_stack_point; - - auto argv_list = vm::ptr>::make((u32)argv); - for(int i=0; i(&STOP_wrapper::STOP)), kFuncConvHost, FuncBuilder1()); call->setArg(0, imm_u(code)); c.mov(*pos_var, (CPU.PC >> 2) + 1); @@ -454,7 +454,7 @@ private: } void SYNC(u32 Cbit) { - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); // This instruction must be used following a store instruction that modifies the instruction stream. c.mfence(); c.mov(*pos_var, (CPU.PC >> 2) + 1); @@ -473,7 +473,7 @@ private: } void RDCH(u32 rt, u32 ra) { - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); WRAPPER_BEGIN(rt, ra, yy, zz); CPU.ReadChannel(CPU.GPR[rt], ra); WRAPPER_END(rt, ra, 0, 0); @@ -481,7 +481,7 @@ private: } void RCHCNT(u32 rt, u32 ra) { - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); WRAPPER_BEGIN(rt, ra, yy, zz); CPU.GPR[rt].clear(); CPU.GPR[rt]._u32[3] = CPU.GetChannelCount(ra); @@ -1089,7 +1089,7 @@ private: } void WRCH(u32 ra, u32 rt) { - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); WRAPPER_BEGIN(ra, rt, yy, zz); CPU.WriteChannel(ra, CPU.GPR[rt]); WRAPPER_END(ra, rt, 0, 0); @@ -1137,10 +1137,10 @@ private: return; } - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); do_finalize = true; - c.mov(*addr, (u32)CPU.PC + 4); + c.mov(*addr, CPU.PC + 4); c.mov(*pos_var, cpu_dword(GPR[ra]._u32[3])); c.cmp(cpu_dword(GPR[rt]._u32[3]), 0); c.cmovne(*pos_var, *addr); @@ -1155,10 +1155,10 @@ private: return; } - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); do_finalize = true; - c.mov(*addr, (u32)CPU.PC + 4); + c.mov(*addr, CPU.PC + 4); c.mov(*pos_var, cpu_dword(GPR[ra]._u32[3])); c.cmp(cpu_dword(GPR[rt]._u32[3]), 0); c.cmove(*pos_var, *addr); @@ -1173,10 +1173,10 @@ private: return; } - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); do_finalize = true; - c.mov(*addr, (u32)CPU.PC + 4); + c.mov(*addr, CPU.PC + 4); c.mov(*pos_var, cpu_dword(GPR[ra]._u32[3])); c.cmp(cpu_word(GPR[rt]._u16[6]), 0); c.cmovne(*pos_var, *addr); @@ -1191,10 +1191,10 @@ private: return; } - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); do_finalize = true; - c.mov(*addr, (u32)CPU.PC + 4); + c.mov(*addr, CPU.PC + 4); c.mov(*pos_var, cpu_dword(GPR[ra]._u32[3])); c.cmp(cpu_word(GPR[rt]._u16[6]), 0); c.cmove(*pos_var, *addr); @@ -1240,7 +1240,7 @@ private: return; } - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); do_finalize = true; c.mov(*pos_var, cpu_dword(GPR[ra]._u32[3])); @@ -1257,7 +1257,7 @@ private: XmmInvalidate(rt); - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); do_finalize = true; c.xor_(*pos_var, *pos_var); @@ -1265,7 +1265,7 @@ private: c.mov(cpu_dword(GPR[rt]._u32[1]), *pos_var); c.mov(cpu_dword(GPR[rt]._u32[2]), *pos_var); c.mov(*pos_var, cpu_dword(GPR[ra]._u32[3])); - c.mov(cpu_dword(GPR[rt]._u32[3]), (u32)CPU.PC + 4); + c.mov(cpu_dword(GPR[rt]._u32[3]), CPU.PC + 4); c.shr(*pos_var, 2); LOG_OPCODE(); } @@ -2734,7 +2734,7 @@ private: //0 - 8 void BRZ(u32 rt, s32 i16) { - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); do_finalize = true; c.mov(*addr, (CPU.PC >> 2) + 1); @@ -2763,7 +2763,7 @@ private: } void BRNZ(u32 rt, s32 i16) { - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); do_finalize = true; c.mov(*addr, (CPU.PC >> 2) + 1); @@ -2774,7 +2774,7 @@ private: } void BRHZ(u32 rt, s32 i16) { - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); do_finalize = true; c.mov(*addr, (CPU.PC >> 2) + 1); @@ -2785,7 +2785,7 @@ private: } void BRHNZ(u32 rt, s32 i16) { - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); do_finalize = true; c.mov(*addr, (CPU.PC >> 2) + 1); @@ -2814,7 +2814,7 @@ private: } void BRA(s32 i16) { - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); do_finalize = true; c.mov(*pos_var, branchTarget(0, i16) >> 2); @@ -2844,20 +2844,20 @@ private: { XmmInvalidate(rt); - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); do_finalize = true; c.xor_(*addr, *addr); // zero c.mov(cpu_dword(GPR[rt]._u32[0]), *addr); c.mov(cpu_dword(GPR[rt]._u32[1]), *addr); c.mov(cpu_dword(GPR[rt]._u32[2]), *addr); - c.mov(cpu_dword(GPR[rt]._u32[3]), (u32)CPU.PC + 4); + c.mov(cpu_dword(GPR[rt]._u32[3]), CPU.PC + 4); c.mov(*pos_var, branchTarget(0, i16) >> 2); LOG_OPCODE(); } void BR(s32 i16) { - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); do_finalize = true; c.mov(*pos_var, branchTarget(CPU.PC, i16) >> 2); @@ -2884,14 +2884,14 @@ private: { XmmInvalidate(rt); - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); do_finalize = true; c.xor_(*addr, *addr); // zero c.mov(cpu_dword(GPR[rt]._u32[0]), *addr); c.mov(cpu_dword(GPR[rt]._u32[1]), *addr); c.mov(cpu_dword(GPR[rt]._u32[2]), *addr); - c.mov(cpu_dword(GPR[rt]._u32[3]), (u32)CPU.PC + 4); + c.mov(cpu_dword(GPR[rt]._u32[3]), CPU.PC + 4); c.mov(*pos_var, branchTarget(CPU.PC, i16) >> 2); LOG_OPCODE(); } @@ -3780,7 +3780,7 @@ private: void UNK(const std::string& err) { LOG_ERROR(Log::SPU, err + fmt::Format(" #pc: 0x%x", CPU.PC)); - c.mov(cpu_qword(PC), (u32)CPU.PC); + c.mov(cpu_dword(PC), CPU.PC); do_finalize = true; Emu.Pause(); } diff --git a/rpcs3/Emu/Cell/SPURecompilerCore.cpp b/rpcs3/Emu/Cell/SPURecompilerCore.cpp index ff1d74942b..0427e8effb 100644 --- a/rpcs3/Emu/Cell/SPURecompilerCore.cpp +++ b/rpcs3/Emu/Cell/SPURecompilerCore.cpp @@ -179,10 +179,10 @@ void SPURecompilerCore::Compile(u16 pos) first = false; } -u8 SPURecompilerCore::DecodeMemory(const u64 address) +u8 SPURecompilerCore::DecodeMemory(const u32 address) { assert(CPU.dmac.ls_offset == address - CPU.PC); - const u64 m_offset = CPU.dmac.ls_offset; + const u32 m_offset = CPU.dmac.ls_offset; const u16 pos = (u16)(CPU.PC >> 2); //ConLog.Write("DecodeMemory: pos=%d", pos); diff --git a/rpcs3/Emu/Cell/SPUThread.cpp b/rpcs3/Emu/Cell/SPUThread.cpp index 985f1c5528..75ea92784c 100644 --- a/rpcs3/Emu/Cell/SPUThread.cpp +++ b/rpcs3/Emu/Cell/SPUThread.cpp @@ -69,18 +69,10 @@ void SPUThread::DoReset() void SPUThread::InitRegs() { GPR[1]._u32[3] = 0x40000 - 120; - GPR[3]._u64[1] = m_args[0]; - GPR[4]._u64[1] = m_args[1]; - GPR[5]._u64[1] = m_args[2]; - GPR[6]._u64[1] = m_args[3]; cfg.Reset(); dmac.ls_offset = m_offset; - /*dmac.proxy_pos = 0; - dmac.queue_pos = 0; - dmac.proxy_lock = 0; - dmac.queue_lock = 0;*/ SPU.Status.SetValue(SPU_STATUS_STOPPED); @@ -96,11 +88,6 @@ void SPUThread::InitRegs() m_events = 0; } -u64 SPUThread::GetFreeStackSize() const -{ - return (GetStackAddr() + GetStackSize()) - GPR[1]._u32[3]; -} - void SPUThread::DoRun() { switch(Ini.SPUDecoderMode.GetValue()) @@ -282,7 +269,7 @@ void SPUThread::ListCmd(u32 lsa, u64 ea, u16 tag, u16 size, u32 cmd, MFCReg& MFC for (u32 i = 0; i < list_size; i++) { - auto rec = vm::ptr::make((u32)dmac.ls_offset + list_addr + i * 8); + auto rec = vm::ptr::make(dmac.ls_offset + list_addr + i * 8); u32 size = rec->ts; if (size < 16 && size != 1 && size != 2 && size != 4 && size != 8) diff --git a/rpcs3/Emu/Cell/SPUThread.h b/rpcs3/Emu/Cell/SPUThread.h index 935c169192..0e6070d0bf 100644 --- a/rpcs3/Emu/Cell/SPUThread.h +++ b/rpcs3/Emu/Cell/SPUThread.h @@ -559,7 +559,6 @@ public: public: virtual void InitRegs(); - virtual u64 GetFreeStackSize() const; virtual void Task(); protected: diff --git a/rpcs3/Emu/Memory/Memory.cpp b/rpcs3/Emu/Memory/Memory.cpp index 5c34363535..83a12f41b4 100644 --- a/rpcs3/Emu/Memory/Memory.cpp +++ b/rpcs3/Emu/Memory/Memory.cpp @@ -299,7 +299,7 @@ void MemoryBlock::Init() range_start = 0; range_size = 0; - mem = vm::get_ptr(0); + mem = vm::get_ptr(0u); } void MemoryBlock::InitMemory() diff --git a/rpcs3/Emu/Memory/Memory.h b/rpcs3/Emu/Memory/Memory.h index c043614dab..394c48f399 100644 --- a/rpcs3/Emu/Memory/Memory.h +++ b/rpcs3/Emu/Memory/Memory.h @@ -16,12 +16,12 @@ enum MemoryType Memory_PSP, }; -enum : u64 +enum : u32 { - RAW_SPU_OFFSET = 0x0000000000100000, - RAW_SPU_BASE_ADDR = 0x00000000E0000000, - RAW_SPU_LS_OFFSET = 0x0000000000000000, - RAW_SPU_PROB_OFFSET = 0x0000000000040000, + RAW_SPU_OFFSET = 0x00100000, + RAW_SPU_BASE_ADDR = 0xE0000000, + RAW_SPU_LS_OFFSET = 0x00000000, + RAW_SPU_PROB_OFFSET = 0x00040000, }; class MemoryBase diff --git a/rpcs3/Emu/Memory/vm.h b/rpcs3/Emu/Memory/vm.h index 0d6d77dee9..fd3ab07d38 100644 --- a/rpcs3/Emu/Memory/vm.h +++ b/rpcs3/Emu/Memory/vm.h @@ -12,35 +12,67 @@ namespace vm { return (T*)((u8*)m_base_addr + addr); } + + template + T* const get_ptr(u64 addr) + { + return get_ptr((u32)addr); + } template T& get_ref(u32 addr) { - return *(T*)((u8*)m_base_addr + addr); + return *get_ptr(addr); } - static u8 read8(u32 addr) + template + T& get_ref(u64 addr) { - return *((u8*)m_base_addr + addr); - } - - static void write8(u32 addr, u8 value) - { - *((u8*)m_base_addr + addr) = value; + return get_ref((u32)addr); } namespace ps3 { + static u8 read8(u32 addr) + { + return *((u8*)m_base_addr + addr); + } + + static u8 read8(u64 addr) + { + return read8((u32)addr); + } + + static void write8(u32 addr, u8 value) + { + *((u8*)m_base_addr + addr) = value; + } + + static void write8(u64 addr, u8 value) + { + write8((u32)addr, value); + } + static u16 read16(u32 addr) { return re16(*(u16*)((u8*)m_base_addr + addr)); } + static u16 read16(u64 addr) + { + return read16((u32)addr); + } + static void write16(u32 addr, u16 value) { *(u16*)((u8*)m_base_addr + addr) = re16(value); } + static void write16(u64 addr, u16 value) + { + write16((u32)addr, value); + } + static u32 read32(u32 addr) { if (addr < RAW_SPU_BASE_ADDR || (addr % RAW_SPU_OFFSET) < RAW_SPU_PROB_OFFSET) @@ -53,6 +85,11 @@ namespace vm } } + static u32 read32(u64 addr) + { + return read32((u32)addr); + } + static void write32(u32 addr, u32 value) { if (addr < RAW_SPU_BASE_ADDR || (addr % RAW_SPU_OFFSET) < RAW_SPU_PROB_OFFSET) @@ -65,29 +102,64 @@ namespace vm } } + static void write32(u64 addr, u32 value) + { + write32((u32)addr, value); + } + static u64 read64(u32 addr) { return re64(*(u64*)((u8*)m_base_addr + addr)); } + static u64 read64(u64 addr) + { + return read64((u32)addr); + } + static void write64(u32 addr, u64 value) { *(u64*)((u8*)m_base_addr + addr) = re64(value); } + static void write64(u64 addr, u64 value) + { + write64((u32)addr, value); + } + static u128 read128(u32 addr) { return re128(*(u128*)((u8*)m_base_addr + addr)); } + static u128 read128(u64 addr) + { + return read128((u32)addr); + } + static void write128(u32 addr, u128 value) { *(u128*)((u8*)m_base_addr + addr) = re128(value); } + + static void write128(u64 addr, u128 value) + { + write128((u32)addr, value); + } } namespace psv { + static u8 read8(u32 addr) + { + return *((u8*)m_base_addr + addr); + } + + static void write8(u32 addr, u8 value) + { + *((u8*)m_base_addr + addr) = value; + } + static u16 read16(u32 addr) { return *(u16*)((u8*)m_base_addr + addr); diff --git a/rpcs3/Emu/Memory/vm_var.h b/rpcs3/Emu/Memory/vm_var.h index 4272e6e527..c3d637abda 100644 --- a/rpcs3/Emu/Memory/vm_var.h +++ b/rpcs3/Emu/Memory/vm_var.h @@ -43,7 +43,7 @@ namespace vm { Memory.Free(m_addr); m_addr = 0; - m_ptr = vm::get_ptr(0); + m_ptr = vm::get_ptr(0u); } } @@ -364,7 +364,7 @@ namespace vm { Memory.Free(m_addr); m_addr = 0; - m_ptr = vm::get_ptr(0); + m_ptr = vm::get_ptr(0u); } } diff --git a/rpcs3/Emu/SysCalls/CB_FUNC.h b/rpcs3/Emu/SysCalls/CB_FUNC.h index 7b00d0ac70..b8b612eb94 100644 --- a/rpcs3/Emu/SysCalls/CB_FUNC.h +++ b/rpcs3/Emu/SysCalls/CB_FUNC.h @@ -108,7 +108,16 @@ namespace cb_detail template<> struct _func_res { - __forceinline static u128 get_value(const PPUThread& CPU) + __forceinline static const u128 get_value(const PPUThread& CPU) + { + return CPU.VPR[2]; + } + }; + + template<> + struct _func_res + { + __forceinline static const u128 get_value(const PPUThread& CPU) { return CPU.VPR[2]; } diff --git a/rpcs3/Emu/SysCalls/Modules/cellGame.cpp b/rpcs3/Emu/SysCalls/Modules/cellGame.cpp index b851bba8b8..b137cbe5d2 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellGame.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellGame.cpp @@ -200,12 +200,12 @@ int cellGameContentPermit(vm::ptr contentInfoPath, vm: int cellGameDataCheckCreate2(u32 version, vm::ptr dirName, u32 errDialog, vm::ptr cbResult, vm::ptr get, vm::ptr set)> funcStat, u32 container) { - cellGame->Warning("cellGameDataCheckCreate2(version=0x%x, dirName_addr=0x%x, errDialog=0x%x, funcStat_addr=0x%x, container=%d)", + cellGame->Warning("cellGameDataCheckCreate(2)(version=0x%x, dirName_addr=0x%x, errDialog=0x%x, funcStat_addr=0x%x, container=%d)", version, dirName.addr(), errDialog, funcStat.addr(), container); if (version != CELL_GAMEDATA_VERSION_CURRENT || errDialog > 1) { - cellGame->Error("cellGameDataCheckCreate2(): CELL_GAMEDATA_ERROR_PARAM"); + cellGame->Error("cellGameDataCheckCreate(2)(): CELL_GAMEDATA_ERROR_PARAM"); return CELL_GAMEDATA_ERROR_PARAM; } @@ -215,7 +215,7 @@ int cellGameDataCheckCreate2(u32 version, vm::ptr dirName, u32 errDi if (!Emu.GetVFS().ExistsDir(dir)) { - cellGame->Todo("cellGameDataCheckCreate2(): creating directory '%s'", dir.c_str()); + cellGame->Todo("cellGameDataCheckCreate(2)(): creating directory '%s'", dir.c_str()); // TODO: create data return CELL_GAMEDATA_RET_OK; } @@ -223,14 +223,14 @@ int cellGameDataCheckCreate2(u32 version, vm::ptr dirName, u32 errDi vfsFile f(dir + "/PARAM.SFO"); if (!f.IsOpened()) { - cellGame->Error("cellGameDataCheckCreate2(): CELL_GAMEDATA_ERROR_BROKEN (cannot open PARAM.SFO)"); + cellGame->Error("cellGameDataCheckCreate(2)(): CELL_GAMEDATA_ERROR_BROKEN (cannot open PARAM.SFO)"); return CELL_GAMEDATA_ERROR_BROKEN; } PSFLoader psf(f); if (!psf.Load(false)) { - cellGame->Error("cellGameDataCheckCreate2(): CELL_GAMEDATA_ERROR_BROKEN (cannot read PARAM.SFO)"); + cellGame->Error("cellGameDataCheckCreate(2)(): CELL_GAMEDATA_ERROR_BROKEN (cannot read PARAM.SFO)"); return CELL_GAMEDATA_ERROR_BROKEN; } @@ -269,36 +269,36 @@ int cellGameDataCheckCreate2(u32 version, vm::ptr dirName, u32 errDi if (cbSet->setParam) { // TODO: write PARAM.SFO from cbSet - cellGame->Todo("cellGameDataCheckCreate2(): writing PARAM.SFO parameters (addr=0x%x)", cbSet->setParam.addr()); + cellGame->Todo("cellGameDataCheckCreate(2)(): writing PARAM.SFO parameters (addr=0x%x)", cbSet->setParam.addr()); } switch ((s32)cbResult->result) { case CELL_GAMEDATA_CBRESULT_OK_CANCEL: // TODO: do not process game data - cellGame->Warning("cellGameDataCheckCreate2(): callback returned CELL_GAMEDATA_CBRESULT_OK_CANCEL"); + cellGame->Warning("cellGameDataCheckCreate(2)(): callback returned CELL_GAMEDATA_CBRESULT_OK_CANCEL"); case CELL_GAMEDATA_CBRESULT_OK: return CELL_GAMEDATA_RET_OK; case CELL_GAMEDATA_CBRESULT_ERR_NOSPACE: // TODO: process errors, error message and needSizeKB result - cellGame->Error("cellGameDataCheckCreate2(): callback returned CELL_GAMEDATA_CBRESULT_ERR_NOSPACE"); + cellGame->Error("cellGameDataCheckCreate(2)(): callback returned CELL_GAMEDATA_CBRESULT_ERR_NOSPACE"); return CELL_GAMEDATA_ERROR_CBRESULT; case CELL_GAMEDATA_CBRESULT_ERR_BROKEN: - cellGame->Error("cellGameDataCheckCreate2(): callback returned CELL_GAMEDATA_CBRESULT_ERR_BROKEN"); + cellGame->Error("cellGameDataCheckCreate(2)(): callback returned CELL_GAMEDATA_CBRESULT_ERR_BROKEN"); return CELL_GAMEDATA_ERROR_CBRESULT; case CELL_GAMEDATA_CBRESULT_ERR_NODATA: - cellGame->Error("cellGameDataCheckCreate2(): callback returned CELL_GAMEDATA_CBRESULT_ERR_NODATA"); + cellGame->Error("cellGameDataCheckCreate(2)(): callback returned CELL_GAMEDATA_CBRESULT_ERR_NODATA"); return CELL_GAMEDATA_ERROR_CBRESULT; case CELL_GAMEDATA_CBRESULT_ERR_INVALID: - cellGame->Error("cellGameDataCheckCreate2(): callback returned CELL_GAMEDATA_CBRESULT_ERR_INVALID"); + cellGame->Error("cellGameDataCheckCreate(2)(): callback returned CELL_GAMEDATA_CBRESULT_ERR_INVALID"); return CELL_GAMEDATA_ERROR_CBRESULT; default: - cellGame->Error("cellGameDataCheckCreate2(): callback returned unknown error (code=0x%x)"); + cellGame->Error("cellGameDataCheckCreate(2)(): callback returned unknown error (code=0x%x)"); return CELL_GAMEDATA_ERROR_CBRESULT; } } diff --git a/rpcs3/Emu/SysCalls/SC_FUNC.h b/rpcs3/Emu/SysCalls/SC_FUNC.h index 16e16cd2ab..4a5b1da3e2 100644 --- a/rpcs3/Emu/SysCalls/SC_FUNC.h +++ b/rpcs3/Emu/SysCalls/SC_FUNC.h @@ -92,7 +92,16 @@ namespace detail template<> struct bind_result { - static __forceinline void func(PPUThread& CPU, u128 result) + static __forceinline void func(PPUThread& CPU, const u128 result) + { + CPU.VPR[2] = result; + } + }; + + template<> + struct bind_result + { + static __forceinline void func(PPUThread& CPU, const u128 result) { CPU.VPR[2] = result; } diff --git a/rpcs3/Emu/SysCalls/lv2/sys_event_flag.cpp b/rpcs3/Emu/SysCalls/lv2/sys_event_flag.cpp index 98a29d0643..22cd01956a 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_event_flag.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_event_flag.cpp @@ -113,12 +113,13 @@ s32 sys_event_flag_wait(u32 eflag_id, u64 bitptn, u32 mode, vm::ptr> r EventFlag* ef; if (!sys_event_flag.CheckId(eflag_id, ef)) return CELL_ESRCH; - u32 tid = GetCurrentPPUThread().GetId(); + const u32 tid = GetCurrentPPUThread().GetId(); { - SMutexLocker lock(ef->m_mutex); + ef->m_mutex.lock(tid); if (ef->m_type == SYS_SYNC_WAITER_SINGLE && ef->waiters.size() > 0) { + ef->m_mutex.unlock(tid); return CELL_EPERM; } EventFlagWaiter rec; @@ -144,8 +145,10 @@ s32 sys_event_flag_wait(u32 eflag_id, u64 bitptn, u32 mode, vm::ptr> r if (result) *result = flags; + ef->m_mutex.unlock(tid); return CELL_OK; } + ef->m_mutex.unlock(tid); } u64 counter = 0; @@ -155,8 +158,7 @@ s32 sys_event_flag_wait(u32 eflag_id, u64 bitptn, u32 mode, vm::ptr> r { if (ef->signal.unlock(tid, tid) == SMR_OK) { - SMutexLocker lock(ef->m_mutex); - + ef->m_mutex.lock(tid); u64 flags = ef->flags; for (u32 i = 0; i < ef->waiters.size(); i++) @@ -187,11 +189,13 @@ s32 sys_event_flag_wait(u32 eflag_id, u64 bitptn, u32 mode, vm::ptr> r if (result) *result = flags; + ef->m_mutex.unlock(tid); return CELL_OK; } } ef->signal.unlock(tid); + ef->m_mutex.unlock(tid); return CELL_ECANCELED; } @@ -199,7 +203,7 @@ s32 sys_event_flag_wait(u32 eflag_id, u64 bitptn, u32 mode, vm::ptr> r if (counter++ > max_counter) { - SMutexLocker lock(ef->m_mutex); + ef->m_mutex.lock(tid); for (u32 i = 0; i < ef->waiters.size(); i++) { @@ -210,6 +214,7 @@ s32 sys_event_flag_wait(u32 eflag_id, u64 bitptn, u32 mode, vm::ptr> r } } + ef->m_mutex.unlock(tid); return CELL_ETIMEDOUT; } if (Emu.IsStopped()) @@ -245,7 +250,8 @@ s32 sys_event_flag_trywait(u32 eflag_id, u64 bitptn, u32 mode, vm::ptr EventFlag* ef; if (!sys_event_flag.CheckId(eflag_id, ef)) return CELL_ESRCH; - SMutexLocker lock(ef->m_mutex); + const u32 tid = GetCurrentPPUThread().GetId(); + ef->m_mutex.lock(tid); u64 flags = ef->flags; @@ -263,9 +269,11 @@ s32 sys_event_flag_trywait(u32 eflag_id, u64 bitptn, u32 mode, vm::ptr if (result) *result = flags; + ef->m_mutex.unlock(tid); return CELL_OK; } + ef->m_mutex.unlock(tid); return CELL_EBUSY; } @@ -301,9 +309,10 @@ s32 sys_event_flag_clear(u32 eflag_id, u64 bitptn) EventFlag* ef; if (!sys_event_flag.CheckId(eflag_id, ef)) return CELL_ESRCH; - SMutexLocker lock(ef->m_mutex); + const u32 tid = GetCurrentPPUThread().GetId(); + ef->m_mutex.lock(tid); ef->flags &= bitptn; - + ef->m_mutex.unlock(tid); return CELL_OK; } @@ -316,14 +325,17 @@ s32 sys_event_flag_cancel(u32 eflag_id, vm::ptr> num) std::vector tids; + const u32 tid = GetCurrentPPUThread().GetId(); + { - SMutexLocker lock(ef->m_mutex); + ef->m_mutex.lock(tid); tids.resize(ef->waiters.size()); for (u32 i = 0; i < ef->waiters.size(); i++) { tids[i] = ef->waiters[i].tid; } ef->waiters.clear(); + ef->m_mutex.unlock(tid); } for (u32 i = 0; i < tids.size(); i++) @@ -349,8 +361,9 @@ s32 sys_event_flag_get(u32 eflag_id, vm::ptr> flags) EventFlag* ef; if (!sys_event_flag.CheckId(eflag_id, ef)) return CELL_ESRCH; - SMutexLocker lock(ef->m_mutex); + const u32 tid = GetCurrentPPUThread().GetId(); + ef->m_mutex.lock(tid); *flags = ef->flags; - + ef->m_mutex.unlock(tid); return CELL_OK; } \ No newline at end of file diff --git a/rpcs3/Emu/SysCalls/lv2/sys_ppu_thread.cpp b/rpcs3/Emu/SysCalls/lv2/sys_ppu_thread.cpp index 1114a15d8b..6dca976a72 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_ppu_thread.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_ppu_thread.cpp @@ -178,7 +178,6 @@ s32 sys_ppu_thread_create(vm::ptr> thread_id, u32 entry, u64 arg, s32 *thread_id = new_thread.GetId(); new_thread.SetEntry(entry); - new_thread.SetArg(0, arg); new_thread.SetPrio(prio); new_thread.SetStackSize(stacksize); //new_thread.flags = flags; @@ -191,6 +190,7 @@ s32 sys_ppu_thread_create(vm::ptr> thread_id, u32 entry, u64 arg, s32 if (!is_interrupt) { new_thread.Run(); + new_thread.GPR[3] = arg; new_thread.Exec(); } else diff --git a/rpcs3/Emu/SysCalls/lv2/sys_spu.cpp b/rpcs3/Emu/SysCalls/lv2/sys_spu.cpp index f18236a937..e2f7c19b12 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_spu.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_spu.cpp @@ -82,7 +82,7 @@ s32 sys_spu_thread_initialize(vm::ptr> thread, u32 group, u32 spu_num, // Copy SPU image: // TODO: use correct segment info - auto spu_offset = Memory.Alloc(256 * 1024, 4096); + u32 spu_offset = (u32)Memory.Alloc(256 * 1024, 4096); memcpy(vm::get_ptr(spu_offset), vm::get_ptr(img->segs_addr), 256 * 1024); CPUThread& new_thread = Emu.GetCPU().AddThread(CPU_THREAD_SPU); @@ -90,15 +90,15 @@ s32 sys_spu_thread_initialize(vm::ptr> thread, u32 group, u32 spu_num, new_thread.SetOffset(spu_offset); new_thread.SetEntry(spu_ep); new_thread.SetName(name); - new_thread.SetArg(0, a1); - new_thread.SetArg(1, a2); - new_thread.SetArg(2, a3); - new_thread.SetArg(3, a4); new_thread.Run(); + static_cast(new_thread).GPR[3] = u128::from64(0, a1); + static_cast(new_thread).GPR[4] = u128::from64(0, a2); + static_cast(new_thread).GPR[5] = u128::from64(0, a3); + static_cast(new_thread).GPR[6] = u128::from64(0, a4); u32 id = new_thread.GetId(); *thread = group_info->list[spu_num] = id; - (*(SPUThread*)&new_thread).group = group_info; + static_cast(new_thread).group = group_info; sys_spu.Warning("*** New SPU Thread [%s] (img_offset=0x%x, ls_offset=0x%x, ep=0x%x, a1=0x%llx, a2=0x%llx, a3=0x%llx, a4=0x%llx): id=%d", (attr->name ? attr->name.get_ptr() : ""), (u32)img->segs_addr, ((SPUThread&)new_thread).dmac.ls_offset, spu_ep, a1, a2, a3, a4, id); @@ -117,10 +117,12 @@ s32 sys_spu_thread_set_argument(u32 id, vm::ptr arg) return CELL_ESRCH; } - thr->SetArg(0, arg->arg1); - thr->SetArg(1, arg->arg2); - thr->SetArg(2, arg->arg3); - thr->SetArg(3, arg->arg4); + SPUThread& spu = *(SPUThread*)thr; + + spu.GPR[3] = u128::from64(0, arg->arg1); + spu.GPR[4] = u128::from64(0, arg->arg2); + spu.GPR[5] = u128::from64(0, arg->arg3); + spu.GPR[6] = u128::from64(0, arg->arg4); return CELL_OK; } diff --git a/rpcs3/Emu/SysCalls/lv2/sys_tty.cpp b/rpcs3/Emu/SysCalls/lv2/sys_tty.cpp index 78c77bbee5..9c6125ade1 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_tty.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_tty.cpp @@ -33,6 +33,6 @@ s32 sys_tty_write(s32 ch, vm::ptr buf, u32 len, vm::ptr pwritel LOG_ERROR(TTY, "%s", data.c_str()); } - *pwritelen = data.size(); + *pwritelen = (u32)data.size(); return CELL_OK; } diff --git a/rpcs3/Emu/System.cpp b/rpcs3/Emu/System.cpp index d67f1d3125..80ceb65097 100644 --- a/rpcs3/Emu/System.cpp +++ b/rpcs3/Emu/System.cpp @@ -333,16 +333,11 @@ void Emulator::Load() LOG_NOTICE(LOADER, "max addr = 0x%x", l.GetMaxAddr()); thread.SetOffset(Memory.MainMem.GetStartAddr()); thread.SetEntry(l.GetEntry() - Memory.MainMem.GetStartAddr()); + thread.Run(); break; case MACHINE_PPC64: { - thread.SetEntry(l.GetEntry()); - Memory.StackMem.AllocAlign(0x1000); - thread.InitStack(); - thread.AddArgv(m_elf_path); // it doesn't work - //thread.AddArgv("-emu"); - m_rsx_callback = (u32)Memory.MainMem.AllocAlign(4 * 4) + 4; vm::write32(m_rsx_callback - 4, m_rsx_callback); @@ -366,11 +361,29 @@ void Emulator::Load() ppu_thr_stop_data[1] = BCLR(0x10 | 0x04, 0, 0, 0); vm::write64(Memory.PRXMem.AllocAlign(0x10000), 0xDEADBEEFABADCAFE); + + thread.SetEntry(l.GetEntry()); + thread.SetStackSize(0x10000); + thread.SetPrio(0x50); + thread.Run(); + + u32 arg1 = Memory.MainMem.AllocAlign(m_elf_path.size() + 1 + 0x20, 0x10) + 0x20; + memcpy(vm::get_ptr(arg1), m_elf_path.c_str(), m_elf_path.size() + 1); + u32 argv = arg1 - 0x20; + vm::write64(argv, arg1); + + static_cast(thread).GPR[3] = 1; // arg count + static_cast(thread).GPR[4] = argv; // probably, args** + static_cast(thread).GPR[5] = argv + 0x10; // unknown + static_cast(thread).GPR[6] = 0; // unknown + static_cast(thread).GPR[12] = Emu.GetMallocPageSize(); // ??? + //thread.AddArgv("-emu"); } break; default: thread.SetEntry(l.GetEntry()); + thread.Run(); break; } @@ -381,8 +394,6 @@ void Emulator::Load() GetAudioManager().Init(); GetEventManager().Init(); - thread.Run(); - SendDbgCommand(DID_READY_EMU); } diff --git a/rpcs3/Gui/GLGSFrame.cpp b/rpcs3/Gui/GLGSFrame.cpp index f4f1a5f4b7..8a0fc1aab7 100644 --- a/rpcs3/Gui/GLGSFrame.cpp +++ b/rpcs3/Gui/GLGSFrame.cpp @@ -63,6 +63,7 @@ void GLGSFrame::Flip(void* context) if (fps_t.GetElapsedTimeInSec() >= 0.5) { + // can freeze on exit SetTitle(wxString::Format("FPS: %.2f", (double)m_frames / fps_t.GetElapsedTimeInSec())); m_frames = 0; fps_t.Start(); diff --git a/rpcs3/Loader/ELF.h b/rpcs3/Loader/ELF.h index 3e203e0ba8..44b888b9ff 100644 --- a/rpcs3/Loader/ELF.h +++ b/rpcs3/Loader/ELF.h @@ -1,7 +1,7 @@ #pragma once #include "Loader.h" -class vfsStream; +struct vfsStream; enum ElfClass {