moved tsc and asm utilities to rx

This commit is contained in:
DH 2025-10-05 19:28:03 +03:00
parent bd215fab92
commit 640df36c48
121 changed files with 706 additions and 1225 deletions

View file

@ -53,10 +53,10 @@
#include "sys_usbd.h"
#include "sys_vm.h"
#include "rx/tsc.hpp"
#include "util/atomic_bit_set.h"
#include "util/init_mutex.hpp"
#include "util/sysinfo.hpp"
#include "util/tsc.hpp"
#include <algorithm>
#include <deque>
#include <optional>
@ -2138,7 +2138,7 @@ void lv2_obj::schedule_all(u64 current_time) {
}
if (const u64 freq = s_yield_frequency) {
const u64 tsc = utils::get_tsc();
const u64 tsc = rx::get_tsc();
const u64 last_tsc = s_last_yield_tsc;
if (tsc >= last_tsc && tsc <= s_max_allowed_yield_tsc &&
@ -2297,7 +2297,7 @@ mwaitx_func static void __mwaitx(u32 cycles, u32 cstate) {
// First bit indicates cstate, 0x0 for C.02 state (lower power) or 0x1 for C.01
// state (higher power)
waitpkg_func static void __tpause(u32 cycles, u32 cstate) {
const u64 tsc = utils::get_tsc() + cycles;
const u64 tsc = rx::get_tsc() + cycles;
_tpause(cstate, tsc);
}
#endif

View file

@ -9,7 +9,7 @@
#include "sys_cond.h"
#include "util/asm.hpp"
#include "rx/asm.hpp"
LOG_CHANNEL(sys_cond);
@ -454,7 +454,7 @@ error_code sys_cond_wait(ppu_thread &ppu, u32 cond_id, u64 timeout) {
}
for (usz i = 0; cpu_flag::signal - ppu.state && i < 50; i++) {
busy_wait(500);
rx::busy_wait(500);
}
if (ppu.state & cpu_flag::signal) {

View file

@ -8,7 +8,8 @@
#include "Emu/Memory/vm_locking.h"
#include "rpcsx/fw/ps3/sys_lv2dbg.h"
#include "util/asm.hpp"
#include "rx/align.hpp"
#include "rx/asm.hpp"
void ppu_register_function_at(u32 addr, u32 size,
ppu_intrp_func_t ptr = nullptr);
@ -92,7 +93,7 @@ error_code sys_dbg_write_process_memory(s32 pid, u32 address, u32 size,
for (u32 i = address, exec_update_size = 0; i < end;) {
const u32 op_size =
std::min<u32>(utils::align<u32>(i + 1, 0x10000), end) - i;
std::min<u32>(rx::alignUp<u32>(i + 1, 0x10000), end) - i;
const bool is_exec =
vm::check_addr(i, vm::page_executable | vm::page_readable);

View file

@ -11,7 +11,7 @@
#include "Emu/Cell/SPUThread.h"
#include "sys_process.h"
#include "util/asm.hpp"
#include "rx/asm.hpp"
LOG_CHANNEL(sys_event);
@ -468,7 +468,7 @@ error_code sys_event_queue_receive(ppu_thread &ppu, u32 equeue_id,
}
for (usz i = 0; cpu_flag::signal - ppu.state && i < 50; i++) {
busy_wait(500);
rx::busy_wait(500);
}
if (ppu.state & cpu_flag::signal) {

View file

@ -7,7 +7,7 @@
#include "Emu/Cell/ErrorCodes.h"
#include "Emu/Cell/PPUThread.h"
#include "util/asm.hpp"
#include "rx/asm.hpp"
LOG_CHANNEL(sys_event_flag);
@ -195,7 +195,7 @@ error_code sys_event_flag_wait(ppu_thread &ppu, u32 id, u64 bitptn, u32 mode,
}
for (usz i = 0; cpu_flag::signal - ppu.state && i < 50; i++) {
busy_wait(500);
rx::busy_wait(500);
}
if (ppu.state & cpu_flag::signal) {

View file

@ -1,9 +1,9 @@
#include "stdafx.h"
#include "rx/asm.hpp"
#include "sys_fs.h"
#include "sys_memory.h"
#include "sys_sync.h"
#include "util/asm.hpp"
#include "Crypto/unedat.h"
#include "Emu/Cell/PPUThread.h"
@ -618,7 +618,7 @@ struct lv2_file::file_view : fs::file_base {
fs::stat_t stat = m_file->file.get_stat();
// TODO: Check this on realhw
// stat.size = utils::sub_saturate<u64>(stat.size, m_off);
// stat.size = rx::sub_saturate<u64>(stat.size, m_off);
stat.is_writable = false;
return stat;
@ -655,7 +655,7 @@ struct lv2_file::file_view : fs::file_base {
}
u64 size() override {
return utils::sub_saturate<u64>(m_file->file.size(), m_off);
return rx::sub_saturate<u64>(m_file->file.size(), m_off);
}
fs::file_id get_id() override {

View file

@ -8,7 +8,7 @@
#include "Emu/Cell/PPUThread.h"
#include "sys_lwmutex.h"
#include "util/asm.hpp"
#include "rx/asm.hpp"
LOG_CHANNEL(sys_lwcond);
@ -490,7 +490,7 @@ error_code _sys_lwcond_queue_wait(ppu_thread &ppu, u32 lwcond_id,
}
for (usz i = 0; cpu_flag::signal - ppu.state && i < 50; i++) {
busy_wait(500);
rx::busy_wait(500);
}
if (ppu.state & cpu_flag::signal) {

View file

@ -7,7 +7,7 @@
#include "Emu/Cell/ErrorCodes.h"
#include "Emu/Cell/PPUThread.h"
#include "util/asm.hpp"
#include "rx/asm.hpp"
LOG_CHANNEL(sys_lwmutex);
@ -194,7 +194,7 @@ error_code _sys_lwmutex_lock(ppu_thread &ppu, u32 lwmutex_id, u64 timeout) {
}
for (usz i = 0; cpu_flag::signal - ppu.state && i < 50; i++) {
busy_wait(500);
rx::busy_wait(500);
}
if (ppu.state & cpu_flag::signal) {

View file

@ -8,7 +8,8 @@
#include "Emu/IdManager.h"
#include "Emu/Memory/vm_locking.h"
#include "util/asm.hpp"
#include "rx/align.hpp"
#include "rx/asm.hpp"
LOG_CHANNEL(sys_memory);
@ -75,11 +76,11 @@ struct sys_memory_address_table {
};
std::shared_ptr<vm::block_t> reserve_map(u32 alloc_size, u32 align) {
return vm::reserve_map(
align == 0x10000 ? vm::user64k : vm::user1m, 0,
align == 0x10000 ? 0x20000000 : utils::align(alloc_size, 0x10000000),
align == 0x10000 ? (vm::page_size_64k | vm::bf0_0x1)
: (vm::page_size_1m | vm::bf0_0x1));
return vm::reserve_map(align == 0x10000 ? vm::user64k : vm::user1m, 0,
align == 0x10000 ? 0x20000000
: rx::alignUp(alloc_size, 0x10000000),
align == 0x10000 ? (vm::page_size_64k | vm::bf0_0x1)
: (vm::page_size_1m | vm::bf0_0x1));
}
// Todo: fix order of error checks

View file

@ -5,7 +5,7 @@
#include "Emu/Cell/ErrorCodes.h"
#include "Emu/Cell/PPUThread.h"
#include "util/asm.hpp"
#include "rx/asm.hpp"
#include "sys_mutex.h"
@ -147,7 +147,7 @@ error_code sys_mutex_lock(ppu_thread &ppu, u32 mutex_id, u64 timeout) {
// Try busy waiting a bit if advantageous
for (u32 i = 0, end = lv2_obj::has_ppus_in_running_state() ? 3 : 10;
id_manager::g_mutex.is_lockable() && i < end; i++) {
busy_wait(300);
rx::busy_wait(300);
result = mutex.try_lock(ppu);
if (!result ||
@ -212,7 +212,7 @@ error_code sys_mutex_lock(ppu_thread &ppu, u32 mutex_id, u64 timeout) {
}
for (usz i = 0; cpu_flag::signal - ppu.state && i < 40; i++) {
busy_wait(500);
rx::busy_wait(500);
}
if (ppu.state & cpu_flag::signal) {

View file

@ -15,7 +15,8 @@
#include "sys_mmapper.h"
#include "sys_process.h"
#include "util/asm.hpp"
#include "rx/align.hpp"
#include "rx/asm.hpp"
#include <thread>
@ -148,7 +149,7 @@ void _sys_ppu_thread_exit(ppu_thread &ppu, u64 errorcode) {
// Need to wait until the current writers finish
if (ppu.state & cpu_flag::memory) {
for (; writer_mask; writer_mask &= vm::g_range_lock_bits[1]) {
busy_wait(200);
rx::busy_wait(200);
}
}
}
@ -468,7 +469,7 @@ error_code _sys_ppu_thread_create(ppu_thread &ppu, vm::ptr<u64> thread_id,
const u32 tls = param->tls;
// Compute actual stack size and allocate
const u32 stack_size = utils::align<u32>(std::max<u32>(_stacksz, 4096), 4096);
const u32 stack_size = rx::alignUp<u32>(std::max<u32>(_stacksz, 4096), 4096);
auto &dct = g_fxo->get<lv2_memory_container>();

View file

@ -10,9 +10,9 @@
#include "Emu/RSX/Core/RSXReservationLock.hpp"
#include "Emu/RSX/RSXThread.h"
#include "Emu/System.h"
#include "rx/asm.hpp"
#include "sys_event.h"
#include "sys_vm.h"
#include "util/asm.hpp"
LOG_CHANNEL(sys_rsx);
@ -46,7 +46,7 @@ static void set_rsx_dmactl(rsx::thread *render, u64 get_put) {
}
}
utils::pause();
rx::pause();
}
// Schedule FIFO interrupt to deal with this immediately

View file

@ -7,7 +7,7 @@
#include "Emu/Cell/ErrorCodes.h"
#include "Emu/Cell/PPUThread.h"
#include "util/asm.hpp"
#include "rx/asm.hpp"
LOG_CHANNEL(sys_rwlock);
@ -151,7 +151,7 @@ error_code sys_rwlock_rlock(ppu_thread &ppu, u32 rw_lock_id, u64 timeout) {
}
for (usz i = 0; cpu_flag::signal - ppu.state && i < 50; i++) {
busy_wait(500);
rx::busy_wait(500);
}
if (ppu.state & cpu_flag::signal) {
@ -355,7 +355,7 @@ error_code sys_rwlock_wlock(ppu_thread &ppu, u32 rw_lock_id, u64 timeout) {
}
for (usz i = 0; cpu_flag::signal - ppu.state && i < 50; i++) {
busy_wait(500);
rx::busy_wait(500);
}
if (ppu.state & cpu_flag::signal) {

View file

@ -7,7 +7,7 @@
#include "Emu/Cell/ErrorCodes.h"
#include "Emu/Cell/PPUThread.h"
#include "util/asm.hpp"
#include "rx/asm.hpp"
LOG_CHANNEL(sys_semaphore);
@ -167,7 +167,7 @@ error_code sys_semaphore_wait(ppu_thread &ppu, u32 sem_id, u64 timeout) {
}
for (usz i = 0; cpu_flag::signal - ppu.state && i < 50; i++) {
busy_wait(500);
rx::busy_wait(500);
}
if (ppu.state & cpu_flag::signal) {

View file

@ -21,7 +21,8 @@
#include "sys_mmapper.h"
#include "sys_process.h"
#include "util/asm.hpp"
#include "rx/align.hpp"
#include "rx/asm.hpp"
LOG_CHANNEL(sys_spu);
@ -129,7 +130,7 @@ void sys_spu_image::load(const fs::file &stream) {
this->nsegs = 0;
this->segs = vm::null;
vm::page_protect(segs.addr(), utils::align(mem_size, 4096), 0, 0,
vm::page_protect(segs.addr(), rx::alignUp(mem_size, 4096), 0, 0,
vm::page_writable);
}
@ -196,8 +197,8 @@ void sys_spu_image::deploy(u8 *loc, std::span<const sys_spu_segment> segs,
}
auto mem_translate = [loc](u32 addr, u32 size) {
return utils::add_saturate<u32>(addr, size) <= SPU_LS_SIZE ? loc + addr
: nullptr;
return rx::add_saturate<u32>(addr, size) <= SPU_LS_SIZE ? loc + addr
: nullptr;
};
// Apply the patch
@ -1259,7 +1260,7 @@ error_code sys_spu_thread_group_terminate(ppu_thread &ppu, u32 id, s32 value) {
// termination
auto short_sleep = [](ppu_thread &ppu) {
lv2_obj::sleep(ppu);
busy_wait(3000);
rx::busy_wait(3000);
ppu.check_state();
ppu.state += cpu_flag::wait;
};

View file

@ -5,8 +5,8 @@
#include "Emu/Cell/ErrorCodes.h"
#include "Emu/Cell/timers.hpp"
#include "Emu/system_config.h"
#include "rx/tsc.hpp"
#include "sys_process.h"
#include "util/tsc.hpp"
#include "util/sysinfo.hpp"
@ -14,7 +14,7 @@ u64 g_timebase_offs{};
static u64 systemtime_offset;
#ifndef __linux__
#include "util/asm.hpp"
#include "rx/asm.hpp"
#endif
#ifdef _WIN32
@ -151,7 +151,7 @@ u64 convert_to_timebased_time(u64 time) {
u64 get_timebased_time() {
if (u64 freq = utils::get_tsc_freq()) {
const u64 tsc = utils::get_tsc();
const u64 tsc = rx::get_tsc();
#if _MSC_VER
const u64 result =
@ -218,7 +218,7 @@ void initialize_timebased_time(u64 timebased_init, bool reset) {
// Returns some relative time in microseconds, don't change this fact
u64 get_system_time() {
if (u64 freq = utils::get_tsc_freq()) {
const u64 tsc = utils::get_tsc();
const u64 tsc = rx::get_tsc();
#if _MSC_VER
const u64 result = static_cast<u64>(u128_from_mul(tsc, 1000000ull) / freq);
@ -358,7 +358,7 @@ error_code sys_time_get_current_time(vm::ptr<s64> sec, vm::ptr<s64> nsec) {
// Get time difference in nanoseconds (using 128 bit accumulator)
const u64 diff_sl = diff_base * 1000000000ull;
const u64 diff_sh = utils::umulh64(diff_base, 1000000000ull);
const u64 diff_sh = rx::umulh64(diff_base, 1000000000ull);
const u64 diff = utils::udiv128(diff_sh, diff_sl, s_time_aux_info.perf_freq);
// get time since Epoch in nanoseconds

View file

@ -9,9 +9,9 @@
#include "Emu/System.h"
#include "Emu/system_config.h"
#include "rx/asm.hpp"
#include "sys_event.h"
#include "sys_process.h"
#include "util/asm.hpp"
#include <deque>
#include <thread>
@ -77,9 +77,9 @@ u64 lv2_timer::check_unlocked(u64 _now) noexcept {
if (period) {
// Set next expiration time and check again
const u64 expire0 = utils::add_saturate<u64>(next, period);
const u64 expire0 = rx::add_saturate<u64>(next, period);
expire.release(expire0);
return utils::sub_saturate<u64>(expire0, _now);
return rx::sub_saturate<u64>(expire0, _now);
}
// Stop after oneshot
@ -265,11 +265,11 @@ error_code _sys_timer_start(ppu_thread &ppu, u32 timer_id, u64 base_time,
const u64 expire =
period == 0 ? base_time : // oneshot
base_time == 0
? utils::add_saturate(start_time, period)
? rx::add_saturate(start_time, period)
:
// periodic timer with no base (using start time as base)
start_time < utils::add_saturate(base_time, period)
? utils::add_saturate(base_time, period)
start_time < rx::add_saturate(base_time, period)
? rx::add_saturate(base_time, period)
:
// periodic with base time over start time
[&]() -> u64 // periodic timer base before start time (align to
@ -282,10 +282,10 @@ error_code _sys_timer_start(ppu_thread &ppu, u32 timer_id, u64 base_time,
// }
// while (base_time < start_time);
const u64 start_time_with_base_time_reminder = utils::add_saturate(
const u64 start_time_with_base_time_reminder = rx::add_saturate(
start_time - start_time % period, base_time % period);
return utils::add_saturate(
return rx::add_saturate(
start_time_with_base_time_reminder,
start_time_with_base_time_reminder < start_time ? period : 0);
}();
@ -428,10 +428,10 @@ error_code sys_timer_usleep(ppu_thread &ppu, u64 sleep_time) {
// Over/underflow checks
if (add_time >= 0) {
sleep_time = utils::add_saturate<u64>(sleep_time, add_time);
sleep_time = rx::add_saturate<u64>(sleep_time, add_time);
} else {
sleep_time =
std::max<u64>(1, utils::sub_saturate<u64>(sleep_time, -add_time));
std::max<u64>(1, rx::sub_saturate<u64>(sleep_time, -add_time));
}
lv2_obj::sleep(ppu, g_cfg.core.sleep_timers_accuracy <