mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-07-04 22:11:26 +12:00
Was using fileExists/dirExists before when really should have just been exists. File or Dir doesn't matter and would only create false negatives. Current working directory shouldn't really be used at all. This is just the folder the application is run from (not even where the .exe resides). Some of the infos required by vfsLocalDir such as executable may not be portable. Not sure of their intended function as they are never used.
483 lines
9.8 KiB
C++
483 lines
9.8 KiB
C++
#include "stdafx.h"
|
|
#include "Utilities/Log.h"
|
|
#include "Emu/Memory/Memory.h"
|
|
#include "Emu/System.h"
|
|
//#include "Ini.h"
|
|
|
|
#include "Emu/GameInfo.h"
|
|
#include "Emu/SysCalls/Static.h"
|
|
#include "Emu/SysCalls/ModuleManager.h"
|
|
#include "Emu/Cell/PPUThread.h"
|
|
#include "Emu/Cell/SPUThread.h"
|
|
#include "Emu/Cell/PPUInstrTable.h"
|
|
#include "Emu/FS/vfsFile.h"
|
|
#include "Emu/FS/vfsDeviceLocalFile.h"
|
|
#include "Emu/DbgCommand.h"
|
|
|
|
#include "Emu/CPU/CPUThreadManager.h" //gui dependency
|
|
|
|
#include "../Crypto/unself.h"
|
|
#include <cstdlib>
|
|
#include <fstream>
|
|
using namespace PPU_instr;
|
|
|
|
static const std::string& BreakPointsDBName = "BreakPoints.dat";
|
|
static const u16 bpdb_version = 0x1000;
|
|
extern std::atomic<u32> g_thread_count;
|
|
|
|
ModuleInitializer::ModuleInitializer()
|
|
{
|
|
Emu.AddModuleInit(std::move(std::unique_ptr<ModuleInitializer>(this)));
|
|
}
|
|
|
|
Emulator::Emulator()
|
|
: m_status(Stopped)
|
|
, m_mode(DisAsm)
|
|
, m_rsx_callback(0)
|
|
, m_ppu_callback_thr(0)
|
|
, m_event_manager(new EventManager())
|
|
, m_sfunc_manager(new StaticFuncManager())
|
|
, m_module_manager(new ModuleManager())
|
|
, m_thread_manager(new CPUThreadManager())
|
|
{
|
|
}
|
|
|
|
void Emulator::Init()
|
|
{
|
|
while(m_modules_init.size())
|
|
{
|
|
m_modules_init[0]->Init();
|
|
m_modules_init.erase(m_modules_init.begin());
|
|
}
|
|
//if(m_memory_viewer) m_memory_viewer->Close();
|
|
//m_memory_viewer = new MemoryViewerPanel(wxGetApp().m_MainFrame);
|
|
}
|
|
|
|
void Emulator::SetPath(const std::string& path, const std::string& elf_path)
|
|
{
|
|
m_path = path;
|
|
m_elf_path = elf_path;
|
|
}
|
|
|
|
void Emulator::SetTitleID(const std::string& id)
|
|
{
|
|
m_title_id = id;
|
|
}
|
|
|
|
void Emulator::CheckStatus()
|
|
{
|
|
std::vector<CPUThread *>& threads = GetCPU().GetThreads();
|
|
if(!threads.size())
|
|
{
|
|
Stop();
|
|
return;
|
|
}
|
|
|
|
bool IsAllPaused = true;
|
|
for(u32 i=0; i<threads.size(); ++i)
|
|
{
|
|
if(threads[i]->IsPaused()) continue;
|
|
IsAllPaused = false;
|
|
break;
|
|
}
|
|
if(IsAllPaused)
|
|
{
|
|
//ConLog.Warning("all paused!");
|
|
Pause();
|
|
return;
|
|
}
|
|
|
|
bool IsAllStoped = true;
|
|
for(u32 i=0; i<threads.size(); ++i)
|
|
{
|
|
if(threads[i]->IsStopped()) continue;
|
|
IsAllStoped = false;
|
|
break;
|
|
}
|
|
if(IsAllStoped)
|
|
{
|
|
//ConLog.Warning("all stoped!");
|
|
Pause(); //Stop();
|
|
}
|
|
}
|
|
|
|
bool Emulator::BootGame(const std::string& path)
|
|
{
|
|
static const char* elf_path[6] =
|
|
{
|
|
"/PS3_GAME/USRDIR/BOOT.BIN",
|
|
"/USRDIR/BOOT.BIN",
|
|
"/BOOT.BIN",
|
|
"/PS3_GAME/USRDIR/EBOOT.BIN",
|
|
"/USRDIR/EBOOT.BIN",
|
|
"/EBOOT.BIN",
|
|
};
|
|
|
|
for(int i=0; i<sizeof(elf_path) / sizeof(*elf_path);i++)
|
|
{
|
|
const std::string& curpath = path + elf_path[i];
|
|
|
|
if(rFile::Access(curpath, rFile::read))
|
|
{
|
|
SetPath(curpath);
|
|
Load();
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void Emulator::Load()
|
|
{
|
|
GetModuleManager().init();
|
|
|
|
if(!rExists(m_path)) return;
|
|
|
|
if(IsSelf(m_path))
|
|
{
|
|
std::string self_path = m_path;
|
|
std::string elf_path = rFileName(m_path).GetPath();
|
|
|
|
if (fmt::CmpNoCase(rFileName(m_path).GetFullName(),"EBOOT.BIN") == 0)
|
|
{
|
|
elf_path += "/BOOT.BIN";
|
|
}
|
|
else
|
|
{
|
|
elf_path += "/" + rFileName(m_path).GetName() + ".elf";
|
|
}
|
|
|
|
if(!DecryptSelf(elf_path, self_path))
|
|
return;
|
|
|
|
m_path = elf_path;
|
|
}
|
|
|
|
LOG_NOTICE(LOADER, "Loading '%s'...", m_path.c_str());
|
|
GetInfo().Reset();
|
|
m_vfs.Init(m_path);
|
|
|
|
LOG_NOTICE(LOADER, " "); //used to be skip_line
|
|
LOG_NOTICE(LOADER, "Mount info:");
|
|
for(uint i=0; i<m_vfs.m_devices.size(); ++i)
|
|
{
|
|
LOG_NOTICE(LOADER, "%s -> %s", m_vfs.m_devices[i]->GetPs3Path().c_str(), m_vfs.m_devices[i]->GetLocalPath().c_str());
|
|
}
|
|
// bdvd inserting imitation
|
|
vfsFile f1("/app_home/dev_bdvd.path");
|
|
if (f1.IsOpened())
|
|
{
|
|
std::string bdvd;
|
|
bdvd.resize(f1.GetSize());
|
|
f1.Read(&bdvd[0], bdvd.size());
|
|
|
|
// load desired /dev_bdvd/ real directory and remount
|
|
Emu.GetVFS().Mount("/dev_bdvd/", bdvd, new vfsDeviceLocalFile());
|
|
LOG_NOTICE(LOADER, "/dev_bdvd/ remounted into %s", bdvd.c_str());
|
|
}
|
|
LOG_NOTICE(LOADER, " ");//used to be skip_line
|
|
|
|
if(m_elf_path.empty())
|
|
{
|
|
GetVFS().GetDeviceLocal(m_path, m_elf_path);
|
|
}
|
|
|
|
vfsFile f(m_elf_path);
|
|
|
|
if(!f.IsOpened())
|
|
{
|
|
LOG_ERROR(LOADER, "Elf not found! (%s - %s)", m_path.c_str(), m_elf_path.c_str());
|
|
return;
|
|
}
|
|
|
|
bool is_error;
|
|
Loader l(f);
|
|
|
|
try
|
|
{
|
|
if(!(is_error = !l.Analyze()) && l.GetMachine() != MACHINE_Unknown)
|
|
{
|
|
switch(l.GetMachine())
|
|
{
|
|
case MACHINE_SPU:
|
|
Memory.Init(Memory_PS3);
|
|
Memory.MainMem.AllocFixed(Memory.MainMem.GetStartAddr(), 0x40000);
|
|
break;
|
|
|
|
case MACHINE_PPC64:
|
|
Memory.Init(Memory_PS3);
|
|
break;
|
|
|
|
case MACHINE_MIPS:
|
|
Memory.Init(Memory_PSP);
|
|
break;
|
|
|
|
case MACHINE_ARM:
|
|
Memory.Init(Memory_PSV);
|
|
break;
|
|
}
|
|
|
|
is_error = !l.Load();
|
|
}
|
|
|
|
}
|
|
catch(const std::string& e)
|
|
{
|
|
LOG_ERROR(LOADER, e);
|
|
is_error = true;
|
|
}
|
|
catch(...)
|
|
{
|
|
LOG_ERROR(LOADER, "Unhandled loader error.");
|
|
is_error = true;
|
|
}
|
|
|
|
CPUThreadType thread_type;
|
|
|
|
if(!is_error)
|
|
{
|
|
switch(l.GetMachine())
|
|
{
|
|
case MACHINE_PPC64: thread_type = CPU_THREAD_PPU; break;
|
|
case MACHINE_SPU: thread_type = CPU_THREAD_SPU; break;
|
|
case MACHINE_ARM: thread_type = CPU_THREAD_ARMv7; break;
|
|
|
|
default:
|
|
LOG_ERROR(LOADER, "Unimplemented thread type for machine.");
|
|
is_error = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(is_error)
|
|
{
|
|
Memory.Close();
|
|
Stop();
|
|
return;
|
|
}
|
|
|
|
LoadPoints(BreakPointsDBName);
|
|
|
|
CPUThread& thread = GetCPU().AddThread(thread_type);
|
|
|
|
switch(l.GetMachine())
|
|
{
|
|
case MACHINE_SPU:
|
|
LOG_NOTICE(LOADER, "offset = 0x%llx", Memory.MainMem.GetStartAddr());
|
|
LOG_NOTICE(LOADER, "max addr = 0x%x", l.GetMaxAddr());
|
|
thread.SetOffset(Memory.MainMem.GetStartAddr());
|
|
thread.SetEntry(l.GetEntry() - Memory.MainMem.GetStartAddr());
|
|
break;
|
|
|
|
case MACHINE_PPC64:
|
|
{
|
|
m_ppu_callback_thr = &GetCPU().AddThread(CPU_THREAD_PPU);
|
|
|
|
thread.SetEntry(l.GetEntry());
|
|
Memory.StackMem.AllocAlign(0x1000);
|
|
thread.InitStack();
|
|
thread.AddArgv(m_elf_path);
|
|
//thread.AddArgv("-emu");
|
|
|
|
m_rsx_callback = Memory.MainMem.AllocAlign(4 * 4) + 4;
|
|
Memory.Write32(m_rsx_callback - 4, m_rsx_callback);
|
|
|
|
mem32_ptr_t callback_data(m_rsx_callback);
|
|
callback_data += ADDI(11, 0, 0x3ff);
|
|
callback_data += SC(2);
|
|
callback_data += BCLR(0x10 | 0x04, 0, 0, 0);
|
|
|
|
m_ppu_thr_exit = Memory.MainMem.AllocAlign(4 * 4);
|
|
|
|
mem32_ptr_t ppu_thr_exit_data(m_ppu_thr_exit);
|
|
ppu_thr_exit_data += ADDI(3, 0, 0);
|
|
ppu_thr_exit_data += ADDI(11, 0, 41);
|
|
ppu_thr_exit_data += SC(2);
|
|
ppu_thr_exit_data += BCLR(0x10 | 0x04, 0, 0, 0);
|
|
|
|
Memory.Write64(Memory.PRXMem.AllocAlign(0x10000), 0xDEADBEEFABADCAFE);
|
|
}
|
|
break;
|
|
|
|
default:
|
|
thread.SetEntry(l.GetEntry());
|
|
break;
|
|
}
|
|
|
|
GetGSManager().Init();
|
|
GetCallbackManager().Init();
|
|
GetAudioManager().Init();
|
|
GetEventManager().Init();
|
|
|
|
thread.Run();
|
|
|
|
m_status = Ready;
|
|
SendDbgCommand(DID_READY_EMU);
|
|
}
|
|
|
|
void Emulator::Run()
|
|
{
|
|
|
|
if(!IsReady())
|
|
{
|
|
Load();
|
|
if(!IsReady()) return;
|
|
}
|
|
|
|
if(IsRunning()) Stop();
|
|
if(IsPaused())
|
|
{
|
|
Resume();
|
|
return;
|
|
}
|
|
SendDbgCommand(DID_START_EMU);
|
|
|
|
//ConLog.Write("run...");
|
|
m_status = Running;
|
|
|
|
//if(m_memory_viewer && m_memory_viewer->exit) safe_delete(m_memory_viewer);
|
|
|
|
//m_memory_viewer->SetPC(loader.GetEntry());
|
|
//m_memory_viewer->Show();
|
|
//m_memory_viewer->ShowPC();
|
|
|
|
GetCPU().Exec();
|
|
SendDbgCommand(DID_STARTED_EMU);
|
|
}
|
|
|
|
void Emulator::Pause()
|
|
{
|
|
if(!IsRunning()) return;
|
|
//ConLog.Write("pause...");
|
|
SendDbgCommand(DID_PAUSE_EMU);
|
|
|
|
if (InterlockedCompareExchange((volatile unsigned long*)&m_status, Paused, Running) == Running)
|
|
{
|
|
SendDbgCommand(DID_PAUSED_EMU);
|
|
}
|
|
}
|
|
|
|
void Emulator::Resume()
|
|
{
|
|
if(!IsPaused()) return;
|
|
//ConLog.Write("resume...");
|
|
SendDbgCommand(DID_RESUME_EMU);
|
|
|
|
m_status = Running;
|
|
|
|
CheckStatus();
|
|
//if(IsRunning() && Ini.CPUDecoderMode.GetValue() != 1) GetCPU().Exec();
|
|
SendDbgCommand(DID_RESUMED_EMU);
|
|
}
|
|
|
|
void Emulator::Stop()
|
|
{
|
|
if(IsStopped()) return;
|
|
//ConLog.Write("shutdown...");
|
|
|
|
SendDbgCommand(DID_STOP_EMU);
|
|
m_status = Stopped;
|
|
|
|
u32 uncounted = 0;
|
|
u32 counter = 0;
|
|
while (true)
|
|
{
|
|
if (g_thread_count <= uncounted)
|
|
{
|
|
LOG_NOTICE(HLE, "All threads stopped...");
|
|
break;
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
if (counter++ > 3000)
|
|
{
|
|
LOG_ERROR(HLE, "%d threads not stopped (timeout)", (u32)(g_thread_count - uncounted));
|
|
break;
|
|
}
|
|
}
|
|
|
|
m_rsx_callback = 0;
|
|
|
|
// TODO: check finalization order
|
|
|
|
SavePoints(BreakPointsDBName);
|
|
m_break_points.clear();
|
|
m_marked_points.clear();
|
|
|
|
m_vfs.UnMountAll();
|
|
|
|
GetGSManager().Close();
|
|
GetAudioManager().Close();
|
|
GetEventManager().Clear();
|
|
GetCPU().Close();
|
|
GetIdManager().Clear();
|
|
GetPadManager().Close();
|
|
GetKeyboardManager().Close();
|
|
GetMouseManager().Close();
|
|
GetCallbackManager().Clear();
|
|
GetModuleManager().UnloadModules();
|
|
|
|
CurGameInfo.Reset();
|
|
Memory.Close();
|
|
|
|
//if(m_memory_viewer && m_memory_viewer->IsShown()) m_memory_viewer->Hide();
|
|
SendDbgCommand(DID_STOPPED_EMU);
|
|
}
|
|
|
|
void Emulator::SavePoints(const std::string& path)
|
|
{
|
|
std::ofstream f(path, std::ios::binary | std::ios::trunc);
|
|
|
|
u32 break_count = m_break_points.size();
|
|
u32 marked_count = m_marked_points.size();
|
|
|
|
f << bpdb_version << break_count << marked_count;
|
|
|
|
if(break_count)
|
|
{
|
|
f.write(reinterpret_cast<char*>(&m_break_points[0]), sizeof(u64) * break_count);
|
|
}
|
|
|
|
if(marked_count)
|
|
{
|
|
f.write(reinterpret_cast<char*>(&m_marked_points[0]), sizeof(u64) * marked_count);
|
|
}
|
|
}
|
|
|
|
void Emulator::LoadPoints(const std::string& path)
|
|
{
|
|
struct stat buf;
|
|
if (!stat(path.c_str(), &buf))
|
|
return;
|
|
std::ifstream f(path, std::ios::binary);
|
|
if (!f.is_open())
|
|
return;
|
|
f.seekg(0, std::ios::end);
|
|
int length = f.tellg();
|
|
f.seekg(0, std::ios::beg);
|
|
u32 break_count, marked_count;
|
|
u16 version;
|
|
f >> version >> break_count >> marked_count;
|
|
|
|
if(version != bpdb_version ||
|
|
(sizeof(u16) + break_count * sizeof(u64) + sizeof(u32) + marked_count * sizeof(u64) + sizeof(u32)) != length)
|
|
{
|
|
LOG_ERROR(LOADER, "'%s' is broken", path.c_str());
|
|
return;
|
|
}
|
|
|
|
if(break_count > 0)
|
|
{
|
|
m_break_points.resize(break_count);
|
|
f.read(reinterpret_cast<char*>(&m_break_points[0]), sizeof(u64) * break_count);
|
|
}
|
|
|
|
if(marked_count > 0)
|
|
{
|
|
m_marked_points.resize(marked_count);
|
|
f.read(reinterpret_cast<char*>(&m_marked_points[0]), sizeof(u64) * marked_count);
|
|
}
|
|
}
|
|
|
|
Emulator Emu;
|