Merge pull request #65 from georgemoralis/timers_again

Timers and general refactoring
This commit is contained in:
georgemoralis 2023-10-31 14:10:36 +02:00 committed by GitHub
commit aee25dcaf9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
45 changed files with 505 additions and 188 deletions

View File

@ -31,22 +31,22 @@ set(LIBC_SOURCES src/Emulator/HLE/Libraries/LibC/Libc.cpp
src/Emulator/HLE/Libraries/LibC/libc_cxa.cpp
src/Emulator/HLE/Libraries/LibC/libc_cxa.h
)
set(USERSERVICE_SOURCES src/Emulator/HLE/Libraries/LibUserService/user_service.cpp
src/Emulator/HLE/Libraries/LibUserService/user_service.h
set(USERSERVICE_SOURCES src/core/hle/libraries/libuserservice/user_service.cpp
src/core/hle/libraries/libuserservice/user_service.h
)
set(PAD_SOURCES src/Emulator/HLE/Libraries/LibPad/pad.cpp
src/Emulator/HLE/Libraries/LibPad/pad.h
set(PAD_SOURCES src/core/hle/libraries/libpad/pad.cpp
src/core/hle/libraries/libpad/pad.h
)
set(SYSTEMSERVICE_SOURCES src/Emulator/HLE/Libraries/LibSystemService/system_service.cpp
src/Emulator/HLE/Libraries/LibSystemService/system_service.h
set(SYSTEMSERVICE_SOURCES src/core/hle/libraries/libsystemservice/system_service.cpp
src/core/hle/libraries/libsystemservice/system_service.h
)
set(FILESYSTEM_SOURCES src/Emulator/HLE/Libraries/LibKernel/FileSystem/file_system.cpp
src/Emulator/HLE/Libraries/LibKernel/FileSystem/file_system.h
src/Emulator/HLE/Libraries/LibKernel/FileSystem/posix_file_system.cpp
src/Emulator/HLE/Libraries/LibKernel/FileSystem/posix_file_system.h
set(FILESYSTEM_SOURCES src/core/hle/libraries/libkernel/file_system.cpp
src/core/hle/libraries/libkernel/file_system.h
)
set(HOST_SOURCES src/Emulator/Host/controller.cpp
@ -64,39 +64,41 @@ add_executable(shadps4
${FILESYSTEM_SOURCES}
${HOST_SOURCES}
${UTIL_SOURCES}
src/Lib/Timer.cpp
src/Lib/Timer.h
src/main.cpp
src/types.h
src/Core/FsFile.cpp
src/Core/FsFile.h
src/Core/PS4/Loader/Elf.cpp
src/Core/PS4/Loader/Elf.h
src/core/FsFile.cpp
src/core/FsFile.h
src/core/PS4/Loader/Elf.cpp
src/core/PS4/Loader/Elf.h
src/GUI/ElfViewer.cpp
src/GUI/ElfViewer.h
src/Util/log.h
src/Util/log.cpp
src/Util/config.cpp
src/Util/config.h
src/Core/virtual_memory.cpp
src/Core/virtual_memory.h
src/Core/PS4/Linker.cpp
src/Core/PS4/Linker.h
src/Core/PS4/Stubs.cpp
src/Core/PS4/Stubs.h
src/Core/PS4/Util/aerolib.cpp
src/Core/PS4/HLE/Kernel/Objects/physical_memory.h
src/Core/PS4/HLE/Kernel/Objects/physical_memory.cpp
src/core/virtual_memory.cpp
src/core/virtual_memory.h
src/core/PS4/Linker.cpp
src/core/PS4/Linker.h
src/core/PS4/Stubs.cpp
src/core/PS4/Stubs.h
src/core/PS4/Util/aerolib.cpp
src/core/PS4/HLE/Kernel/Objects/physical_memory.h
src/core/PS4/HLE/Kernel/Objects/physical_memory.cpp
src/Util/string_util.cpp
src/Util/string_util.cpp
src/Core/PS4/HLE/Graphics/video_out.cpp
src/Core/PS4/HLE/Graphics/video_out.h
src/core/PS4/HLE/Graphics/video_out.cpp
src/core/PS4/HLE/Graphics/video_out.h
src/discord.h
src/discord.cpp
src/Core/PS4/HLE/Kernel/event_queues.cpp
src/Core/PS4/HLE/Kernel/event_queues.h
src/Core/PS4/HLE/Kernel/cpu_management.cpp
src/Core/PS4/HLE/Kernel/cpu_management.h
src/core/PS4/HLE/Kernel/event_queues.cpp
src/core/PS4/HLE/Kernel/event_queues.h
src/core/PS4/HLE/Kernel/cpu_management.cpp
src/core/PS4/HLE/Kernel/cpu_management.h
"src/Util/Disassembler.cpp" "src/Util/Disassembler.h" "src/Core/PS4/Util/aerolib.h" "src/Core/PS4/Loader/SymbolsResolver.h" "src/Core/PS4/Loader/SymbolsResolver.cpp" "src/Core/PS4/HLE/Libs.cpp" "src/Core/PS4/HLE/Libs.h" "src/Core/PS4/HLE/LibC.cpp" "src/Core/PS4/HLE/LibC.h" "src/Core/PS4/HLE/LibKernel.cpp" "src/Core/PS4/HLE/LibKernel.h" "src/Core/PS4/HLE/LibSceGnmDriver.cpp" "src/Core/PS4/HLE/LibSceGnmDriver.h" "src/Core/PS4/HLE/Kernel/ThreadManagement.cpp" "src/Core/PS4/HLE/Kernel/ThreadManagement.h" "src/Core/PS4/HLE/ErrorCodes.h" "src/debug.h" "src/Core/PS4/HLE/Kernel/memory_management.cpp" "src/Core/PS4/HLE/Kernel/memory_management.h" "src/Core/PS4/GPU/gpu_memory.cpp" "src/Core/PS4/GPU/gpu_memory.h" "src/emulator.cpp" "src/emulator.h" "src/Core/PS4/HLE/Kernel/Objects/event_queue.h" "src/Core/PS4/HLE/Kernel/Objects/event_queue.cpp" "src/Core/PS4/HLE/Graphics/Objects/video_out_ctx.cpp" "src/Core/PS4/HLE/Graphics/Objects/video_out_ctx.h" "src/Core/PS4/HLE/Graphics/graphics_ctx.h" "src/vulkan_util.cpp" "src/vulkan_util.h" "src/Core/PS4/GPU/video_out_buffer.cpp" "src/Core/PS4/GPU/video_out_buffer.h" "src/Core/PS4/HLE/Graphics/graphics_render.cpp" "src/Core/PS4/HLE/Graphics/graphics_render.h" "src/Core/PS4/GPU/tile_manager.cpp" "src/Core/PS4/GPU/tile_manager.h" "src/version.h" "src/Emulator/HLE/Libraries/LibSystemService/system_service.cpp" "src/Emulator/HLE/Libraries/LibSystemService/system_service.h" )
"src/Util/Disassembler.cpp" "src/Util/Disassembler.h" "src/core/PS4/Util/aerolib.h" "src/core/PS4/Loader/SymbolsResolver.h" "src/core/PS4/Loader/SymbolsResolver.cpp" "src/core/PS4/HLE/Libs.cpp" "src/core/PS4/HLE/Libs.h" "src/core/PS4/HLE/LibC.cpp" "src/core/PS4/HLE/LibC.h" "src/core/PS4/HLE/LibKernel.cpp" "src/core/PS4/HLE/LibKernel.h" "src/core/PS4/HLE/LibSceGnmDriver.cpp" "src/core/PS4/HLE/LibSceGnmDriver.h" "src/core/PS4/HLE/Kernel/ThreadManagement.cpp" "src/core/PS4/HLE/Kernel/ThreadManagement.h" "src/core/PS4/HLE/ErrorCodes.h" "src/debug.h" "src/core/PS4/HLE/Kernel/memory_management.cpp" "src/core/PS4/HLE/Kernel/memory_management.h" "src/core/PS4/GPU/gpu_memory.cpp" "src/core/PS4/GPU/gpu_memory.h" "src/emulator.cpp" "src/emulator.h" "src/core/PS4/HLE/Kernel/Objects/event_queue.h" "src/core/PS4/HLE/Kernel/Objects/event_queue.cpp" "src/core/PS4/HLE/Graphics/Objects/video_out_ctx.cpp" "src/core/PS4/HLE/Graphics/Objects/video_out_ctx.h" "src/core/PS4/HLE/Graphics/graphics_ctx.h" "src/vulkan_util.cpp" "src/vulkan_util.h" "src/core/PS4/GPU/video_out_buffer.cpp" "src/core/PS4/GPU/video_out_buffer.h" "src/core/PS4/HLE/Graphics/graphics_render.cpp" "src/core/PS4/HLE/Graphics/graphics_render.h" "src/core/PS4/GPU/tile_manager.cpp" "src/core/PS4/GPU/tile_manager.h" "src/version.h" "src/emuTimer.cpp" "src/emuTimer.h" "src/core/hle/libraries/libkernel/time_management.cpp" "src/core/hle/libraries/libkernel/time_management.h")
find_package(OpenGL REQUIRED)
target_link_libraries(shadps4 PUBLIC fmt mincore spdlog IMGUI SDL3-shared ${OPENGL_LIBRARY} vulkan-1 spirv-tools-opt spirv-tools)

View File

@ -1,6 +1,6 @@
#pragma once
#include <Core/PS4/HLE/Graphics/graphics_ctx.h>
#include <core/PS4/HLE/Graphics/graphics_ctx.h>
#include <types.h>
#include <mutex>
#include <vector>

View File

@ -1,7 +1,8 @@
#include "video_out_ctx.h"
#include <Core/PS4/HLE/LibKernel.h>
#include <core/PS4/HLE/LibKernel.h>
#include <debug.h>
#include <core/hle/libraries/libkernel/time_management.h>
namespace HLE::Graphics::Objects {
@ -76,7 +77,7 @@ bool FlipQueue::submitFlip(VideoConfigInternal* cfg, s32 index, s64 flip_arg) {
r.cfg = cfg;
r.index = index;
r.flip_arg = flip_arg;
r.submit_tsc = HLE::Libs::LibKernel::sceKernelReadTsc();
r.submit_tsc = Core::Libraries::LibKernel::sceKernelReadTsc();
m_requests.push_back(r);
@ -120,8 +121,8 @@ bool FlipQueue::flip(u32 micros) {
m_done_cond.notify_one();
request->cfg->m_flip_status.count++;
// TODO request.cfg->m_flip_status.processTime = LibKernel::KernelGetProcessTime();
request->cfg->m_flip_status.tsc = HLE::Libs::LibKernel::sceKernelReadTsc();
request->cfg->m_flip_status.processTime = Core::Libraries::LibKernel::sceKernelGetProcessTime();
request->cfg->m_flip_status.tsc = Core::Libraries::LibKernel::sceKernelReadTsc();
request->cfg->m_flip_status.submitTsc = request->submit_tsc;
request->cfg->m_flip_status.flipArg = request->flip_arg;
request->cfg->m_flip_status.currentBuffer = request->index;

View File

@ -2,8 +2,8 @@
#include <condition_variable>
#include <mutex>
#include <Core/PS4/HLE/Graphics/video_out.h>
#include <Core/PS4/HLE/Graphics/graphics_ctx.h>
#include <core/PS4/HLE/Graphics/video_out.h>
#include <core/PS4/HLE/Graphics/graphics_ctx.h>
#include <emulator.h>
using namespace HLE::Libs::Graphics::VideoOut;

View File

@ -1,11 +1,11 @@
#include "video_out.h"
#include <Core/PS4/GPU/gpu_memory.h>
#include <Core/PS4/GPU/video_out_buffer.h>
#include <Core/PS4/HLE/ErrorCodes.h>
#include <Core/PS4/HLE/LibSceGnmDriver.h>
#include <Core/PS4/HLE/Libs.h>
#include <Core/PS4/HLE/UserManagement/UsrMngCodes.h>
#include <core/PS4/GPU/gpu_memory.h>
#include <core/PS4/GPU/video_out_buffer.h>
#include <core/PS4/HLE/ErrorCodes.h>
#include <core/PS4/HLE/LibSceGnmDriver.h>
#include <core/PS4/HLE/Libs.h>
#include <core/PS4/HLE/UserManagement/UsrMngCodes.h>
#include <Util/config.h>
#include <Util/log.h>
#include <debug.h>

View File

@ -1,6 +1,6 @@
#pragma once
#include <Core/PS4/HLE/Kernel/event_queues.h>
#include <Core/PS4/Loader/SymbolsResolver.h>
#include <core/PS4/HLE/Kernel/event_queues.h>
#include <core/PS4/Loader/SymbolsResolver.h>
#include <types.h>
#include <string>

View File

@ -1,7 +1,8 @@
#include "event_queue.h"
#include "debug.h"
#include <chrono>
#include <Lib/Timer.h>
#include "debug.h"
namespace HLE::Kernel::Objects {
EqueueInternal::~EqueueInternal() {}
@ -25,8 +26,9 @@ int EqueueInternal::addEvent(const EqueueEvent& event) {
int EqueueInternal::waitForEvents(SceKernelEvent* ev, int num, u32 micros) {
std::unique_lock lock{m_mutex};
u64 timeElapsed = 0;
const auto start = std::chrono::high_resolution_clock::now();
u32 timeElapsed = 0;
Lib::Timer t;
t.Start();
for (;;) {
int ret = getTriggeredEvents(ev, num);
@ -41,8 +43,7 @@ int EqueueInternal::waitForEvents(SceKernelEvent* ev, int num, u32 micros) {
m_cond.wait_for(lock, std::chrono::microseconds(micros - timeElapsed));
}
const auto end = std::chrono::high_resolution_clock::now();
timeElapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
timeElapsed = static_cast<uint32_t>(t.GetTimeSec() * 1000000.0);
}
return 0;

View File

@ -1,7 +1,7 @@
#pragma once
#include <types.h>
#include <Core/virtual_memory.h>
#include <Core/PS4/GPU/gpu_memory.h>
#include <core/virtual_memory.h>
#include <core/PS4/GPU/gpu_memory.h>
#include <mutex>
#include <vector>

View File

@ -1,7 +1,7 @@
#include "cpu_management.h"
#include "Util/config.h"
#include <Util/log.h>
#include <Core/PS4/HLE/Libs.h>
#include <core/PS4/HLE/Libs.h>
namespace HLE::Libs::LibKernel::CPUManagement {
int PS4_SYSV_ABI sceKernelIsNeoMode() {

View File

@ -1,7 +1,7 @@
#include "event_queues.h"
#include <Core/PS4/HLE/ErrorCodes.h>
#include <Core/PS4/HLE/Libs.h>
#include <core/PS4/HLE/ErrorCodes.h>
#include <core/PS4/HLE/Libs.h>
#include <Util/log.h>
#include <debug.h>

View File

@ -1,7 +1,7 @@
#include "memory_management.h"
#include <Core/PS4/GPU/gpu_memory.h>
#include <Core/virtual_memory.h>
#include <core/PS4/GPU/gpu_memory.h>
#include <core/virtual_memory.h>
#include <Util/log.h>
#include <debug.h>

View File

@ -11,8 +11,8 @@
#include "Kernel/event_queues.h"
#include "Kernel/memory_management.h"
#include "Libs.h"
#include "Emulator/HLE/Libraries/LibKernel/FileSystem/file_system.h"
#include "Emulator/HLE/Libraries/LibKernel/FileSystem/posix_file_system.h"
#include "core/hle/libraries/libkernel/file_system.h"
#include "core/hle/libraries/libkernel/time_management.h"
namespace HLE::Libs::LibKernel {
@ -24,11 +24,7 @@ int32_t PS4_SYSV_ABI sceKernelReleaseDirectMemory(off_t start, size_t len) {
}
static PS4_SYSV_ABI void stack_chk_fail() { BREAKPOINT(); }
u64 PS4_SYSV_ABI sceKernelReadTsc() {
LARGE_INTEGER c;
QueryPerformanceCounter(&c);
return c.QuadPart;
}
int PS4_SYSV_ABI sceKernelMunmap(void* addr, size_t len) { BREAKPOINT(); }
void LibKernel_Register(SymbolsResolver* sym) {
// obj
@ -45,11 +41,9 @@ void LibKernel_Register(SymbolsResolver* sym) {
// misc
LIB_FUNCTION("WslcK1FQcGI", "libkernel", 1, "libkernel", 1, 1, CPUManagement::sceKernelIsNeoMode);
LIB_FUNCTION("Ou3iL1abvng", "libkernel", 1, "libkernel", 1, 1, stack_chk_fail);
// time
LIB_FUNCTION("-2IRUCO--PM", "libkernel", 1, "libkernel", 1, 1, sceKernelReadTsc);
// fs
LIB_FUNCTION("1G3lF1Gg1k8", "libkernel", 1, "libkernel", 1, 1, Emulator::HLE::Libraries::LibKernel::FileSystem::sceKernelOpen);
LIB_FUNCTION("wuCroIGjt2g", "libScePosix", 1, "libkernel", 1, 1, Emulator::HLE::Libraries::LibKernel::FileSystem::POSIX::open);
Core::Libraries::LibKernel::fileSystemSymbolsRegister(sym);
Core::Libraries::LibKernel::timeSymbolsRegister(sym);
}
}; // namespace HLE::Libs::LibKernel

View File

@ -6,6 +6,5 @@ void LibKernel_Register(SymbolsResolver* sym);
// functions
u64 PS4_SYSV_ABI sceKernelReadTsc();
int32_t PS4_SYSV_ABI sceKernelReleaseDirectMemory(off_t start, size_t len);
}; // namespace HLE::Libs::LibKernel

View File

@ -3,7 +3,7 @@
#include "../Loader/Elf.h"
#include <Util/log.h>
#include <debug.h>
#include <Core/PS4/GPU/gpu_memory.h>
#include <core/PS4/GPU/gpu_memory.h>
#include <emulator.h>
namespace HLE::Libs::LibSceGnmDriver {

View File

@ -3,10 +3,10 @@
#include "LibC.h"
#include "LibKernel.h"
#include "LibSceGnmDriver.h"
#include <Core/PS4/HLE/Graphics/video_out.h>
#include "Emulator/HLE/Libraries/LibUserService/user_service.h"
#include "Emulator/HLE/Libraries/LibPad/pad.h"
#include <Emulator/HLE/Libraries/LibSystemService/system_service.h>
#include <core/PS4/HLE/Graphics/video_out.h>
#include "core/hle/libraries/libuserservice/user_service.h"
#include "core/hle/libraries/libpad/pad.h"
#include <core/hle/libraries/libsystemservice/system_service.h>
namespace HLE::Libs {
@ -15,8 +15,8 @@ void Init_HLE_Libs(SymbolsResolver *sym) {
LibKernel::LibKernel_Register(sym);
Graphics::VideoOut::videoOutRegisterLib(sym);
LibSceGnmDriver::LibSceGnmDriver_Register(sym);
Emulator::HLE::Libraries::LibUserService::libUserService_Register(sym);
Emulator::HLE::Libraries::LibPad::libPad_Register(sym);
Emulator::HLE::Libraries::LibSystemService::libSystemService_Register(sym);
Core::Libraries::LibUserService::userServiceSymbolsRegister(sym);
Core::Libraries::LibPad::padSymbolsRegister(sym);
Core::Libraries::LibSystemService::systemServiceSymbolsRegister(sym);
}
} // namespace HLE::Libs

View File

@ -1,6 +1,6 @@
#pragma once
#include "../Loader/SymbolsResolver.h"
#include <Core/PS4/Loader/Elf.h>
#include <core/PS4/Loader/Elf.h>
#define LIB_FUNCTION(nid, lib, libversion, mod, moduleVersionMajor, moduleVersionMinor, function) \
{\

View File

@ -0,0 +1,29 @@
#include "file_system.h"
#include <core/PS4/HLE/Libs.h>
#include <Util/log.h>
#include <debug.h>
namespace Core::Libraries::LibKernel {
constexpr bool log_file_fs = true; // disable it to disable logging
int PS4_SYSV_ABI sceKernelOpen(const char* path, int flags, u16 mode) {
LOG_INFO_IF(log_file_fs, "sceKernelOpen path = {} flags = {:#x} mode = {:#x}\n", path, flags, mode);
return 0;
}
int PS4_SYSV_ABI open(const char* path, int flags, /* SceKernelMode*/ u16 mode) {
LOG_INFO_IF(log_file_fs, "posix open redirect to sceKernelOpen\n");
int result = sceKernelOpen(path, flags, mode);
if (result < 0) {
BREAKPOINT(); // posix calls different only for their return values
}
return result;
}
void fileSystemSymbolsRegister(SymbolsResolver* sym) {
LIB_FUNCTION("1G3lF1Gg1k8", "libkernel", 1, "libkernel", 1, 1, sceKernelOpen);
LIB_FUNCTION("wuCroIGjt2g", "libScePosix", 1, "libkernel", 1, 1, open);
}
} // namespace Core::Libraries::LibKernel

View File

@ -0,0 +1,13 @@
#pragma once
#include <types.h>
#include "core/PS4/Loader/SymbolsResolver.h"
namespace Core::Libraries::LibKernel {
int PS4_SYSV_ABI sceKernelOpen(const char *path, int flags, /* SceKernelMode*/ u16 mode);
// posix file system
int PS4_SYSV_ABI open(const char *path, int flags, /* SceKernelMode*/ u16 mode);
void fileSystemSymbolsRegister(SymbolsResolver *sym);
} // namespace Core::Libraries::LibKernel

View File

@ -0,0 +1,24 @@
#include "time_management.h"
#include <core/PS4/HLE/Libs.h>
#include "Lib/Timer.h"
#include "emuTimer.h"
namespace Core::Libraries::LibKernel {
u64 PS4_SYSV_ABI sceKernelGetProcessTime() {
return static_cast<u64>(Emulator::emuTimer::getTimeMsec() * 1000.0); // return time in microseconds
}
u64 PS4_SYSV_ABI sceKernelGetProcessTimeCounter() { return Emulator::emuTimer::getTimeCounter(); }
u64 PS4_SYSV_ABI sceKernelGetProcessTimeCounterFrequency() { return Emulator::emuTimer::getTimeFrequency(); }
u64 PS4_SYSV_ABI sceKernelReadTsc() { return Lib::Timer::getQueryPerformanceCounter(); }
void timeSymbolsRegister(SymbolsResolver* sym) {
LIB_FUNCTION("4J2sUJmuHZQ", "libkernel", 1, "libkernel", 1, 1, sceKernelGetProcessTime);
LIB_FUNCTION("fgxnMeTNUtY", "libkernel", 1, "libkernel", 1, 1, sceKernelGetProcessTimeCounter);
LIB_FUNCTION("BNowx2l588E", "libkernel", 1, "libkernel", 1, 1, sceKernelGetProcessTimeCounterFrequency);
LIB_FUNCTION("-2IRUCO--PM", "libkernel", 1, "libkernel", 1, 1, sceKernelReadTsc);
}
} // namespace Core::Libraries::LibKernel

View File

@ -0,0 +1,13 @@
#pragma once
#include "types.h"
#include "core/PS4/Loader/SymbolsResolver.h"
namespace Core::Libraries::LibKernel {
u64 PS4_SYSV_ABI sceKernelGetProcessTime();
u64 PS4_SYSV_ABI sceKernelGetProcessTimeCounter();
u64 PS4_SYSV_ABI sceKernelGetProcessTimeCounterFrequency();
u64 PS4_SYSV_ABI sceKernelReadTsc();
void timeSymbolsRegister(SymbolsResolver* sym);
}

View File

@ -1,6 +1,6 @@
#include "virtual_memory.h"
#include "Core/PS4/Loader/Elf.h"
#include "core/PS4/Loader/Elf.h"
#ifdef _WIN64
#include <windows.h>

View File

@ -1,13 +0,0 @@
#include "file_system.h"
#include <debug.h>
#include <Util/log.h>
namespace Emulator::HLE::Libraries::LibKernel::FileSystem {
constexpr bool log_file_fs = true; // disable it to disable logging
int PS4_SYSV_ABI sceKernelOpen(const char* path, int flags, u16 mode) {
LOG_INFO_IF(log_file_fs, "sceKernelOpen path = {} flags = {} mode = {}\n", path, log_hex_full(flags), log_hex_full(mode));
return 0;
}
} // namespace Emulator::HLE::Libraries::LibKernel::FileSystem

View File

@ -1,7 +0,0 @@
#pragma once
#include <types.h>
namespace Emulator::HLE::Libraries::LibKernel::FileSystem {
int PS4_SYSV_ABI sceKernelOpen(const char *path, int flags, /* SceKernelMode*/ u16 mode);
}

View File

@ -1,15 +0,0 @@
#include "posix_file_system.h"
#include <debug.h>
#include "file_system.h"
namespace Emulator::HLE::Libraries::LibKernel::FileSystem::POSIX {
int PS4_SYSV_ABI open(const char* path, int flags, /* SceKernelMode*/ u16 mode) {
int result = sceKernelOpen(path, flags, mode);
if (result < 0) {
BREAKPOINT(); // posix calls different only for their return values
}
return result;
}
} // namespace Emulator::HLE::Libraries::LibKernel::FileSystem::POSIX

View File

@ -1,6 +0,0 @@
#pragma once
#include "types.h"
namespace Emulator::HLE::Libraries::LibKernel::FileSystem::POSIX {
int PS4_SYSV_ABI open(const char *path, int flags, /* SceKernelMode*/ u16 mode);
}

View File

@ -1,16 +0,0 @@
#include <Core/PS4/HLE/ErrorCodes.h>
#include <Core/PS4/HLE/Libs.h>
#include "system_service.h"
namespace Emulator::HLE::Libraries::LibSystemService {
s32 PS4_SYSV_ABI sceSystemServiceHideSplashScreen() {
// dummy
return SCE_OK;
}
void libSystemService_Register(SymbolsResolver* sym) {
LIB_FUNCTION("Vo5V8KAwCmk", "libSceSystemService", 1, "libSceSystemService", 1, 1, sceSystemServiceHideSplashScreen);
}
}; // namespace Emulator::HLE::Libraries::LibUserService

View File

@ -1,11 +0,0 @@
#pragma once
#include "Core/PS4/Loader/SymbolsResolver.h"
namespace Emulator::HLE::Libraries::LibSystemService {
//HLE functions
s32 PS4_SYSV_ABI sceSystemServiceHideSplashScreen();
void libSystemService_Register(SymbolsResolver* sym);
}; // namespace Emulator::HLE::Libraries::LibUserService

View File

@ -1,4 +1,5 @@
#include "controller.h"
#include <core/hle/libraries/libkernel/time_management.h>
namespace Emulator::Host::Controller {
GameController::GameController() { m_states_num = 0;
@ -39,6 +40,7 @@ void GameController::addState(const State& state) {
void GameController::checKButton(int id, u32 button, bool isPressed) {
std::scoped_lock lock{m_mutex};
auto state = getLastState();
state.time = Core::Libraries::LibKernel::sceKernelGetProcessTime();
if (isPressed) {
state.buttonsState |= button;
} else {

View File

@ -5,6 +5,7 @@
namespace Emulator::Host::Controller {
struct State {
u32 buttonsState =0;
u64 time = 0;
};
constexpr u32 MAX_STATES = 64;

View File

@ -1,5 +1,5 @@
#pragma once
#include "../Core/PS4/Loader/Elf.h"
#include "../core/PS4/Loader/Elf.h"
class ElfViewer {
private:

109
src/Lib/Timer.cpp Normal file
View File

@ -0,0 +1,109 @@
#include "Timer.h"
#ifdef _WIN64
#include <windows.h>
#endif
Lib::Timer::Timer() {
#ifdef _WIN64
LARGE_INTEGER f;
QueryPerformanceFrequency(&f);
m_Frequency = f.QuadPart;
#else
#error Unimplemented Timer constructor
#endif
}
void Lib::Timer::Start() {
#ifdef _WIN64
LARGE_INTEGER c;
QueryPerformanceCounter(&c);
m_StartTime = c.QuadPart;
#else
#error Unimplemented Timer::Start()
#endif
m_is_timer_paused = false;
}
void Lib::Timer::Pause() {
#ifdef _WIN64
LARGE_INTEGER c;
QueryPerformanceCounter(&c);
m_PauseTime = c.QuadPart;
#else
#error Unimplemented Timer::Pause()
#endif
m_is_timer_paused = true;
}
void Lib::Timer::Resume() {
u64 current_time = 0;
#ifdef _WIN64
LARGE_INTEGER c;
QueryPerformanceCounter(&c);
current_time = c.QuadPart;
#else
#error Unimplemented Timer::Resume()
#endif
m_StartTime += current_time - m_PauseTime;
m_is_timer_paused = false;
}
bool Lib::Timer::IsPaused() const { return m_is_timer_paused; }
double Lib::Timer::GetTimeMsec() const {
if (m_is_timer_paused) {
return 1000.0 * (static_cast<double>(m_PauseTime - m_StartTime)) / static_cast<double>(m_Frequency);
}
u64 current_time = 0;
#ifdef _WIN64
LARGE_INTEGER c;
QueryPerformanceCounter(&c);
current_time = c.QuadPart;
#else
#error Unimplemented Timer::GetTimeMsec()
#endif
return 1000.0 * (static_cast<double>(current_time - m_StartTime)) / static_cast<double>(m_Frequency);
}
double Lib::Timer::GetTimeSec() const {
if (m_is_timer_paused) {
return (static_cast<double>(m_PauseTime - m_StartTime)) / static_cast<double>(m_Frequency);
}
u64 current_time = 0;
#ifdef _WIN64
LARGE_INTEGER c;
QueryPerformanceCounter(&c);
current_time = c.QuadPart;
#else
#error Unimplemented Timer::GetTimeSec()
#endif
return (static_cast<double>(current_time - m_StartTime)) / static_cast<double>(m_Frequency);
}
u64 Lib::Timer::GetTicks() const {
if (m_is_timer_paused) {
return (m_PauseTime - m_StartTime);
}
u64 current_time = 0;
#ifdef _WIN64
LARGE_INTEGER c;
QueryPerformanceCounter(&c);
current_time = c.QuadPart;
#else
#error Unimplemented Timer::GetTicks()
#endif
return (current_time - m_StartTime);
}
u64 Lib::Timer::GetFrequency() const { return m_Frequency; }
u64 Lib::Timer::getQueryPerformanceCounter() {
LARGE_INTEGER c;
QueryPerformanceCounter(&c);
return c.QuadPart;
}

34
src/Lib/Timer.h Normal file
View File

@ -0,0 +1,34 @@
#pragma once
#include "../types.h"
namespace Lib {
class Timer final
{
public:
Timer();
~Timer() = default;
void Start();
void Pause();
void Resume();
bool IsPaused() const;
double GetTimeMsec() const;// return time in milliseconds
double GetTimeSec() const;// return time in seconds
u64 GetTicks() const;// return time in ticks
u64 GetFrequency() const;// return ticks frequency
[[nodiscard]] static u64 getQueryPerformanceCounter();
public:
Timer(const Timer&) = delete;
Timer& operator=(const Timer&) = delete;
Timer(Timer&&) = delete;
Timer& operator=(Timer&&) = delete;
private:
bool m_is_timer_paused = true;
u64 m_Frequency = 0;
u64 m_StartTime = 0;
u64 m_PauseTime = 0;
};
}

View File

@ -1,21 +1,22 @@
#include "pad.h"
#include <Core/PS4/HLE/ErrorCodes.h>
#include <Core/PS4/HLE/Libs.h>
#include <core/PS4/HLE/ErrorCodes.h>
#include <core/PS4/HLE/Libs.h>
#include "Emulator/Util/singleton.h"
#include "Emulator/Host/controller.h"
#include <debug.h>
#include <Util/log.h>
namespace Emulator::HLE::Libraries::LibPad {
namespace Core::Libraries::LibPad {
constexpr bool log_file_pad = true; // disable it to disable logging
int PS4_SYSV_ABI scePadInit() { return SCE_OK; }
int PS4_SYSV_ABI scePadOpen(Emulator::HLE::Libraries::LibUserService::SceUserServiceUserId userId, s32 type, s32 index,
int PS4_SYSV_ABI scePadOpen(Core::Libraries::LibUserService::SceUserServiceUserId userId, s32 type, s32 index,
const ScePadOpenParam* pParam) {
LOG_INFO_IF(log_file_pad, "scePadOpen userid = {} type = {} index = {}\n", userId, type, index);
return 1; // dummy
}
@ -38,7 +39,7 @@ int PS4_SYSV_ABI scePadReadState(int32_t handle, ScePadData* pData) {
pData->orientation.y = 0;
pData->orientation.z = 0;
pData->orientation.w = 0;
pData->timestamp = state.time;
pData->connected = true; // isConnected; //TODO fix me proper
pData->connectedCount = 1;//connectedCount;
pData->deviceUniqueDataLen = 0;
@ -46,7 +47,7 @@ int PS4_SYSV_ABI scePadReadState(int32_t handle, ScePadData* pData) {
return SCE_OK;
}
void libPad_Register(SymbolsResolver* sym) {
void padSymbolsRegister(SymbolsResolver* sym) {
LIB_FUNCTION("hv1luiJrqQM", "libScePad", 1, "libScePad", 1, 1, scePadInit);
LIB_FUNCTION("xk0AcarP3V4", "libScePad", 1, "libScePad", 1, 1, scePadOpen);
LIB_FUNCTION("YndgXqQVV7c", "libScePad", 1, "libScePad", 1, 1, scePadReadState);

View File

@ -1,10 +1,10 @@
#pragma once
#include <Emulator/HLE/Libraries/LibUserService/user_service.h>
#include <types.h>
#include "Core/PS4/Loader/SymbolsResolver.h"
#include "core/PS4/Loader/SymbolsResolver.h"
#include "core/hle/libraries/libuserservice/user_service.h"
namespace Emulator::HLE::Libraries::LibPad {
namespace Core::Libraries::LibPad {
typedef enum : u32 {
SCE_PAD_BUTTON_L3 = 0x00000002,
@ -90,9 +90,9 @@ struct ScePadData {
};
// hle functions
int PS4_SYSV_ABI scePadInit();
int PS4_SYSV_ABI scePadOpen(Emulator::HLE::Libraries::LibUserService::SceUserServiceUserId userId, s32 type, s32 index,
int PS4_SYSV_ABI scePadOpen(Core::Libraries::LibUserService::SceUserServiceUserId userId, s32 type, s32 index,
const ScePadOpenParam* pParam);
int PS4_SYSV_ABI scePadReadState(int32_t handle, ScePadData* pData);
void libPad_Register(SymbolsResolver* sym);
}; // namespace Emulator::HLE::Libraries::LibPad
void padSymbolsRegister(SymbolsResolver* sym);
}; // namespace Core::Libraries::LibPad

View File

@ -0,0 +1,16 @@
#include <core/PS4/HLE/ErrorCodes.h>
#include <core/PS4/HLE/Libs.h>
#include <Util/log.h>
#include "system_service.h"
namespace Core::Libraries::LibSystemService {
s32 PS4_SYSV_ABI sceSystemServiceHideSplashScreen() {
PRINT_DUMMY_FUNCTION_NAME();
return SCE_OK;
}
void systemServiceSymbolsRegister(SymbolsResolver* sym) {
LIB_FUNCTION("Vo5V8KAwCmk", "libSceSystemService", 1, "libSceSystemService", 1, 1, sceSystemServiceHideSplashScreen);
}
}; // namespace Emulator::HLE::Libraries::LibUserService

View File

@ -0,0 +1,11 @@
#pragma once
#include "core/PS4/Loader/SymbolsResolver.h"
namespace Core::Libraries::LibSystemService {
//HLE functions
s32 PS4_SYSV_ABI sceSystemServiceHideSplashScreen();
void systemServiceSymbolsRegister(SymbolsResolver* sym);
}; // namespace Emulator::HLE::Libraries::LibUserService

View File

@ -1,17 +1,19 @@
#include "user_service.h"
#include <Core/PS4/HLE/ErrorCodes.h>
#include <Core/PS4/HLE/Libs.h>
#include <core/PS4/HLE/ErrorCodes.h>
#include <core/PS4/HLE/Libs.h>
namespace Emulator::HLE::Libraries::LibUserService {
#include "Util/log.h"
namespace Core::Libraries::LibUserService {
s32 PS4_SYSV_ABI sceUserServiceInitialize(const SceUserServiceInitializeParams* initParams) {
// dummy
PRINT_DUMMY_FUNCTION_NAME();
return SCE_OK;
}
s32 PS4_SYSV_ABI sceUserServiceGetLoginUserIdList(SceUserServiceLoginUserIdList* userIdList) {
// dummy
PRINT_DUMMY_FUNCTION_NAME();
userIdList->user_id[0] = 1;
userIdList->user_id[1] = -1;
userIdList->user_id[2] = -1;
@ -19,8 +21,9 @@ s32 PS4_SYSV_ABI sceUserServiceGetLoginUserIdList(SceUserServiceLoginUserIdList*
return SCE_OK;
}
void libUserService_Register(SymbolsResolver* sym) {
void userServiceSymbolsRegister(SymbolsResolver* sym) {
LIB_FUNCTION("j3YMu1MVNNo", "libSceUserService", 1, "libSceUserService", 1, 1, sceUserServiceInitialize);
LIB_FUNCTION("fPhymKNvK-A", "libSceUserService", 1, "libSceUserService", 1, 1, sceUserServiceGetLoginUserIdList);
}
}; // namespace Emulator::HLE::Libraries::LibUserService
} // namespace Core::Libraries::LibUserService

View File

@ -1,7 +1,7 @@
#pragma once
#include "Core/PS4/Loader/SymbolsResolver.h"
#include "core/PS4/Loader/SymbolsResolver.h"
namespace Emulator::HLE::Libraries::LibUserService {
namespace Core::Libraries::LibUserService {
using SceUserServiceUserId = s32;
@ -16,5 +16,5 @@ struct SceUserServiceLoginUserIdList {
s32 PS4_SYSV_ABI sceUserServiceInitialize(const SceUserServiceInitializeParams* initParams);
s32 PS4_SYSV_ABI sceUserServiceGetLoginUserIdList(SceUserServiceLoginUserIdList* userIdList);
void libUserService_Register(SymbolsResolver* sym);
void userServiceSymbolsRegister(SymbolsResolver* sym);
}; // namespace Emulator::HLE::Libraries::LibUserService

14
src/emuTimer.cpp Normal file
View File

@ -0,0 +1,14 @@
#include "Lib/Timer.h"
namespace Emulator::emuTimer {
static Lib::Timer timer;
void start() { timer.Start(); }
double getTimeMsec() { return timer.GetTimeMsec(); }
u64 getTimeCounter() { return timer.GetTicks(); }
u64 getTimeFrequency() { return timer.GetFrequency(); }
} // namespace Emulator::emuTimer

9
src/emuTimer.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include "types.h"
namespace Emulator::emuTimer {
void start();
double getTimeMsec();
u64 getTimeCounter();
u64 getTimeFrequency();
} // namespace Emulator::emuTimer

View File

@ -1,17 +1,30 @@
#include "emulator.h"
#include <fmt/core.h>
#include <Core/PS4/HLE/Graphics/graphics_render.h>
#include <core/PS4/HLE/Graphics/graphics_render.h>
#include <Emulator/Host/controller.h>
#include "Emulator/Util/singleton.h"
#include <Lib/Timer.h>
#include <fmt/core.h>
#include <vulkan_util.h>
#include "Core/PS4/HLE/Graphics/video_out.h"
#include "Emulator/HLE/Libraries/LibPad/pad.h"
#include "core/PS4/HLE/Graphics/video_out.h"
#include "core/hle/libraries/libpad/pad.h"
#include "Emulator/Util/singleton.h"
#include "version.h"
namespace Emu {
bool m_emu_needs_exit = false;
bool m_game_is_paused = {false};
double m_current_time_seconds = {0.0};
double m_previous_time_seconds = {0.0};
int m_update_num = {0};
int m_frame_num = {0};
double m_update_time_seconds = {0.0};
double m_current_fps = {0.0};
int m_max_updates_per_frame = {4};
double m_update_fixed_time = 1.0 / 60.0;
int m_fps_frames_num = {0};
double m_fps_start_time = {0};
void emuInit(u32 width, u32 height) {
auto* window_ctx = singleton<Emu::WindowCtx>::instance();
@ -49,7 +62,39 @@ static void CreateSdlWindow(WindowCtx* ctx) {
SDL_SetWindowResizable(ctx->m_window, SDL_FALSE); // we don't support resizable atm
}
static void update() {
static double lag = 0.0;
lag += m_current_time_seconds - m_previous_time_seconds;
int num = 0;
while (lag >= m_update_fixed_time) {
if (num < m_max_updates_per_frame) {
m_update_num++;
num++;
m_update_time_seconds = m_update_num * m_update_fixed_time;
}
lag -= m_update_fixed_time;
}
}
static void calculateFps(double game_time_s) {
m_previous_time_seconds = m_current_time_seconds;
m_current_time_seconds = game_time_s;
m_frame_num++;
m_fps_frames_num++;
if (m_current_time_seconds - m_fps_start_time > 0.25f) {
m_current_fps = static_cast<double>(m_fps_frames_num) / (m_current_time_seconds - m_fps_start_time);
m_fps_frames_num = 0;
m_fps_start_time = m_current_time_seconds;
}
}
void emuRun() {
Lib::Timer timer;
timer.Start();
auto* window_ctx = singleton<Emu::WindowCtx>::instance();
{
// init window and wait until init finishes
@ -58,6 +103,7 @@ void emuRun() {
Graphics::Vulkan::vulkanCreate(window_ctx);
window_ctx->m_is_graphic_initialized = true;
window_ctx->m_graphic_initialized_cond.notify_one();
calculateFps(timer.GetTimeSec());
}
bool exit_loop = false;
@ -83,14 +129,68 @@ void emuRun() {
case SDL_EVENT_DID_ENTER_FOREGROUND: break;
case SDL_EVENT_KEY_DOWN:
case SDL_EVENT_KEY_UP: keyboardEvent(&event); break;
case SDL_EVENT_KEY_UP:
if (event.type == SDL_EVENT_KEY_DOWN){
if (event.key.keysym.sym == SDLK_p) {
m_game_is_paused = !m_game_is_paused;
}
}
keyboardEvent(&event);
break;
}
continue;
}
if (m_game_is_paused) {
if (!timer.IsPaused()) {
timer.Pause();
}
SDL_WaitEvent(&event);
switch (event.type) {
case SDL_EVENT_QUIT: m_emu_needs_exit = true; break;
case SDL_EVENT_TERMINATING: m_emu_needs_exit = true; break;
case SDL_EVENT_WILL_ENTER_BACKGROUND: break;
case SDL_EVENT_DID_ENTER_BACKGROUND: break;
case SDL_EVENT_WILL_ENTER_FOREGROUND: break;
case SDL_EVENT_DID_ENTER_FOREGROUND: break;
case SDL_EVENT_KEY_DOWN:
case SDL_EVENT_KEY_UP:
if (event.type == SDL_EVENT_KEY_DOWN) {
if (event.key.keysym.sym == SDLK_p) {
m_game_is_paused = !m_game_is_paused;
}
}
keyboardEvent(&event);
break;
}
exit_loop = m_emu_needs_exit;
continue;
}
exit_loop = m_emu_needs_exit;
if (m_game_is_paused) {
if (!timer.IsPaused()) {
timer.Pause();
}
} else {
if (timer.IsPaused()) {
timer.Resume();
}
if (!exit_loop) {
HLE::Libs::Graphics::VideoOut::videoOutFlip(100000); // flip every 0.1 sec
update();
}
if (!exit_loop) {
if (HLE::Libs::Graphics::VideoOut::videoOutFlip(100000)) { // flip every 0.1 sec
calculateFps(timer.GetTimeSec());
}
}
}
}
std::exit(0);
@ -102,6 +202,12 @@ HLE::Libs::Graphics::GraphicCtx* getGraphicCtx() {
return &window_ctx->m_graphic_ctx;
}
void updateSDLTitle() {
char title[256];
sprintf(title, "shadps4 v %s FPS: %f", Emulator::VERSION, m_current_fps);
auto* window_ctx = singleton<Emu::WindowCtx>::instance();
SDL_SetWindowTitle(window_ctx->m_window, title);
}
void DrawBuffer(HLE::Libs::Graphics::VideoOutVulkanImage* image) {
auto* window_ctx = singleton<Emu::WindowCtx>::instance();
if (window_ctx->is_window_hidden) {
@ -192,10 +298,11 @@ void DrawBuffer(HLE::Libs::Graphics::VideoOutVulkanImage* image) {
fmt::print("vkQueuePresentKHR failed\n");
std::exit(0);
}
updateSDLTitle();
}
void keyboardEvent(SDL_Event* event) {
using Emulator::HLE::Libraries::LibPad::ScePadButton;
using Core::Libraries::LibPad::ScePadButton;
if (event->type == SDL_EVENT_KEY_DOWN || event->type == SDL_EVENT_KEY_UP) {
u32 button = 0;

View File

@ -1,6 +1,6 @@
#pragma once
#include <Core/PS4/HLE/Graphics/graphics_ctx.h>
#include <core/PS4/HLE/Graphics/graphics_ctx.h>
#include <SDL.h>
#include <mutex>

View File

@ -3,16 +3,17 @@
#include <fmt/core.h>
#include "types.h"
#include "Util/log.h"
#include <Core/PS4/HLE/Graphics/video_out.h>
#include <core/PS4/HLE/Graphics/video_out.h>
#include <Util/config.h>
#include <Zydis/Zydis.h>
#include <emulator.h>
#include <cinttypes>
#include <thread>
#include "Core/PS4/HLE/Libs.h"
#include "Core/PS4/Linker.h"
#include "core/PS4/HLE/Libs.h"
#include "core/PS4/Linker.h"
#include "Emulator/Util\singleton.h"
#include "discord.h"
#include "emuTimer.h"
// Main code
int main(int argc, char* argv[]) {
@ -26,6 +27,7 @@ int main(int argc, char* argv[]) {
auto height = Config::getScreenHeight();
Emu::emuInit(width, height);
HLE::Libs::Graphics::VideoOut::videoOutInit(width, height);
Emulator::emuTimer::start();
const char* const path = argv[1]; // argument 1 is the path of self file to boot

View File

@ -1,6 +1,6 @@
#include "vulkan_util.h"
#include <fmt/core.h>
#include <Core/PS4/GPU/gpu_memory.h>
#include <core/PS4/GPU/gpu_memory.h>
#include <SDL_vulkan.h>
#include <Emulator/Util/singleton.h>
#include <Util/log.h>

View File

@ -1,5 +1,5 @@
#pragma once
#include <Core/PS4/HLE/Graphics/graphics_render.h>
#include <core/PS4/HLE/Graphics/graphics_render.h>
#include <SDL.h>
#include <src/video/khronos/vulkan/vulkan_core.h>