rpcsx/rpcs3/Emu/Cell/lv2/sys_game.cpp

258 lines
4.8 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "util/sysinfo.hpp"
#include "util/v128.hpp"
#include "Emu/Memory/vm_ptr.h"
#include "Emu/Cell/ErrorCodes.h"
2022-10-16 07:23:48 +02:00
#include "Emu/System.h"
#include "Emu/system_utils.hpp"
2022-10-17 20:40:44 +02:00
#include "Emu/IdManager.h"
#include "Utilities/Thread.h"
2022-10-16 07:23:48 +02:00
#include "sys_game.h"
LOG_CHANNEL(sys_game);
struct board_storage
{
public:
bool read(u8* buffer)
{
if (!buffer)
return false;
const auto data = storage.load();
memcpy(buffer, &data, size);
return true;
}
bool write(u8* buffer)
{
if (!buffer)
return false;
storage.store(read_from_ptr<be_t<v128>>(buffer));
written = true;
return true;
}
board_storage()
{
memset(&storage.raw(), -1, size);
if (fs::file file; file.open(file_path, fs::read))
file.read(&storage.raw(), std::min(file.size(), size));
}
board_storage(const board_storage&) = delete;
board_storage& operator =(const board_storage&) = delete;
~board_storage()
{
if (written)
{
if (fs::file file; file.open(file_path, fs::create + fs::write + fs::lock))
{
file.write(&storage.raw(), size);
file.trunc(size);
}
}
}
private:
atomic_be_t<v128> storage;
bool written = false;
const std::string file_path = rpcs3::utils::get_hdd1_dir() + "/caches/board_storage.bin";
static constexpr u64 size = sizeof(v128);
};
2022-10-17 20:40:44 +02:00
struct watchdog_t
2022-10-16 07:23:48 +02:00
{
2022-10-17 20:40:44 +02:00
struct alignas(8) control_t
{
bool needs_restart = false;
bool active = false;
char pad[sizeof(u32) - sizeof(bool) * 2]{};
u32 timeout = 0;
};
2022-10-16 07:23:48 +02:00
2022-10-17 20:40:44 +02:00
atomic_t<control_t> control;
2022-10-16 07:23:48 +02:00
2022-10-17 20:40:44 +02:00
void operator()()
2022-10-16 07:23:48 +02:00
{
2022-10-17 20:40:44 +02:00
u64 start_time = get_system_time();
u64 old_time = start_time;
u64 current_time = old_time;
2022-10-16 07:23:48 +02:00
2022-10-17 20:40:44 +02:00
constexpr u64 sleep_time = 50'000;
while (thread_ctrl::state() != thread_state::aborting)
2022-10-16 07:23:48 +02:00
{
2022-10-17 20:40:44 +02:00
if (Emu.GetStatus(false) == system_state::paused)
{
start_time += current_time - old_time;
old_time = current_time;
thread_ctrl::wait_for(sleep_time);
current_time = get_system_time();
continue;
}
old_time = std::exchange(current_time, get_system_time());
const auto old = control.fetch_op([&](control_t& data)
2022-10-16 07:23:48 +02:00
{
2022-10-17 20:40:44 +02:00
if (data.needs_restart)
2022-10-16 07:23:48 +02:00
{
2022-10-17 20:40:44 +02:00
data.needs_restart = false;
return true;
2022-10-16 07:23:48 +02:00
}
2022-10-17 20:40:44 +02:00
return false;
}).first;
if (old.active && old.needs_restart)
{
start_time = current_time;
old_time = current_time;
continue;
}
if (old.active && current_time - start_time >= old.timeout)
{
sys_game.success("Watchdog timeout! Restarting the game...");
2022-10-17 20:40:44 +02:00
Emu.CallFromMainThread([]()
{
Emu.Restart(false);
2022-10-17 20:40:44 +02:00
});
return;
2022-10-16 07:23:48 +02:00
}
2022-10-17 20:40:44 +02:00
thread_ctrl::wait_for(sleep_time);
2022-10-16 07:23:48 +02:00
}
2022-10-17 20:40:44 +02:00
}
static constexpr auto thread_name = "LV2 Watchdog Thread"sv;
};
void abort_lv2_watchdog()
{
if (auto thr = g_fxo->try_get<named_thread<watchdog_t>>())
{
sys_game.notice("Aborting %s...", thr->thread_name);
*thr = thread_state::aborting;
}
}
2022-10-17 20:40:44 +02:00
error_code _sys_game_watchdog_start(u32 timeout)
{
sys_game.trace("sys_game_watchdog_start(timeout=%d)", timeout);
// According to disassembly
timeout *= 1'000'000;
timeout &= -64;
2022-10-16 07:23:48 +02:00
2022-10-17 20:40:44 +02:00
if (!g_fxo->get<named_thread<watchdog_t>>().control.fetch_op([&](watchdog_t::control_t& data)
{
if (data.active)
{
return false;
}
data.needs_restart = true;
data.active = true;
data.timeout = timeout;
return true;
}).second)
{
return CELL_EABORT;
}
2022-10-16 07:23:48 +02:00
return CELL_OK;
}
error_code _sys_game_watchdog_stop()
{
sys_game.trace("sys_game_watchdog_stop()");
2022-10-17 20:40:44 +02:00
g_fxo->get<named_thread<watchdog_t>>().control.fetch_op([](watchdog_t::control_t& data)
{
if (!data.active)
{
return false;
}
data.active = false;
return true;
});
2022-10-16 07:23:48 +02:00
return CELL_OK;
}
error_code _sys_game_watchdog_clear()
{
sys_game.trace("sys_game_watchdog_clear()");
2022-10-17 20:40:44 +02:00
g_fxo->get<named_thread<watchdog_t>>().control.fetch_op([](watchdog_t::control_t& data)
{
if (!data.active || data.needs_restart)
{
return false;
}
data.needs_restart = true;
return true;
});
2022-10-16 07:23:48 +02:00
return CELL_OK;
}
u64 _sys_game_get_system_sw_version()
{
return stof(utils::get_firmware_version()) * 10000;
}
error_code _sys_game_board_storage_read(vm::ptr<u8> buffer, vm::ptr<u8> status)
{
sys_game.trace("sys_game_board_storage_read(buffer=*0x%x, status=*0x%x)", buffer, status);
if (!buffer || !status)
{
return CELL_EFAULT;
}
*status = g_fxo->get<board_storage>().read(buffer.get_ptr()) ? 0x00 : 0xFF;
return CELL_OK;
}
error_code _sys_game_board_storage_write(vm::ptr<u8> buffer, vm::ptr<u8> status)
{
sys_game.trace("sys_game_board_storage_write(buffer=*0x%x, status=*0x%x)", buffer, status);
if (!buffer || !status)
{
return CELL_EFAULT;
}
*status = g_fxo->get<board_storage>().write(buffer.get_ptr()) ? 0x00 : 0xFF;
return CELL_OK;
}
error_code _sys_game_get_rtc_status(vm::ptr<s32> status)
{
sys_game.trace("sys_game_get_rtc_status(status=*0x%x)", status);
if (!status)
{
return CELL_EFAULT;
}
*status = 0;
return CELL_OK;
}