fpPS4/fpPS4.lpr

503 lines
11 KiB
ObjectPascal
Raw Normal View History

2021-12-08 20:04:07 +00:00
uses
{$IFDEF Linux}
cmem,
cthreads,
{$ENDIF}
2023-01-14 14:43:57 +00:00
cpu,
2022-05-31 07:20:10 +00:00
windows,
2021-12-08 20:04:07 +00:00
seh64,
2022-05-31 07:20:10 +00:00
Classes,
sysutils,
2021-12-08 20:04:07 +00:00
stub_manager,
2022-07-13 14:01:22 +00:00
sys_crt,
2022-05-31 07:20:10 +00:00
sys_types,
sys_pthread,
2022-11-09 13:46:13 +00:00
sys_path,
sys_kernel,
2021-12-08 20:04:07 +00:00
ps4libdoc,
2022-11-17 11:42:18 +00:00
ps4_libkernel,
ps4_libSceLibcInternal,
2022-10-12 06:49:52 +00:00
ps4_libSceScreenShot,
2022-09-20 17:57:07 +00:00
ps4_libSceRtc,
2022-07-14 11:59:03 +00:00
ps4_libSceNpSignaling,
ps4_libSceNpMatching2,
2022-07-06 09:41:39 +00:00
ps4_libSceRemoteplay,
2022-06-04 16:00:28 +00:00
ps4_libSceAjm,
2022-07-01 08:11:45 +00:00
ps4_libSceMouse,
2022-06-03 11:15:35 +00:00
ps4_libSceIme,
2022-10-18 13:42:07 +00:00
ps4_libSceMove,
ps4_libSceMoveTracker,
2022-06-03 11:15:35 +00:00
ps4_libScePlayGo,
ps4_libSceDiscMap,
2022-05-31 07:20:10 +00:00
ps4_libSceAppContent,
2021-12-08 20:04:07 +00:00
ps4_libSceNet,
ps4_libSceHttp,
ps4_libSceGnmDriver,
ps4_libSceNpScore,
ps4_libSceNpTrophy,
ps4_libSceSystemService,
ps4_libSceSystemGesture,
ps4_libSceNpUtility,
2023-02-10 07:54:49 +00:00
ps4_libSceNpCommon,
2021-12-08 20:04:07 +00:00
ps4_libSceNpManager,
2022-09-07 10:57:39 +00:00
ps4_libSceNpGameIntent,
ps4_libSceNpAuth,
ps4_libSceNpParty,
2021-12-08 20:04:07 +00:00
ps4_libSceSaveData,
ps4_libSceDialogs,
ps4_libSceUserService,
2022-07-14 11:59:03 +00:00
ps4_libSceAudioOut,
ps4_libSceAudio3d,
2022-11-30 12:05:20 +00:00
ps4_libSceVoice,
2021-12-08 20:04:07 +00:00
ps4_libSceVideoOut,
ps4_libSceAvPlayer,
2023-02-04 18:31:42 +00:00
ps4_libSceAudiodec,
2021-12-08 20:04:07 +00:00
ps4_libScePad,
2022-11-17 11:42:18 +00:00
ps4_libSceNpWebApi,
2022-11-29 08:35:02 +00:00
ps4_libSceRudp,
2022-12-03 16:46:46 +00:00
ps4_libSceRandom,
2023-01-26 18:45:56 +00:00
ps4_libSceComposite,
ps4_libSceSysCore,
ps4_libSceSsl,
ps4_libSceFiber,
2023-01-26 18:45:56 +00:00
ps4_libSceUlt,
ps4_libSceGameLiveStreaming,
ps4_libSceSharePlay,
2024-01-25 17:06:56 +00:00
ps4_libSceShareUtility,
2024-01-10 18:45:31 +00:00
ps4_libSceSocialScreen,
ps4_libSceVideoRecording,
ps4_libSceWebBrowserDialog,
ps4_libSceContentExport,
ps4_libSceConvertKeycode,
ps4_libSceUsbd,
ps4_libSceAudiodecCpu,
ps4_libSceDepth,
ps4_libSceNpTus,
ps4_libSceLoginService,
ps4_libSceHmd,
ps4_libSceVrTracker,
ps4_libSceCamera,
2021-12-08 20:04:07 +00:00
ps4_elf,
ps4_pthread,
ps4_program,
2022-05-31 07:20:10 +00:00
2022-10-14 09:20:02 +00:00
ps4_videodrv,
vMemory,
vImageManager,
2023-01-26 13:54:36 +00:00
vImageTiling,
2022-12-09 18:58:15 +00:00
vFlip,
2022-10-14 09:20:02 +00:00
2022-05-31 07:20:10 +00:00
trace_manager;
2021-12-08 20:04:07 +00:00
function ParseCmd:Boolean;
var
i,n:Integer;
label
promo;
begin
if (ParamCount=0) then
begin
promo:
2022-12-22 08:17:06 +00:00
Writeln('fpPS4 (',{$I tag.inc},')');
Writeln('Copyright (c) 2021-2023 by red-prig');
Writeln('PS4 compatibility layer (emulator) written with Free Pascal '+{$I %FPCVERSION%});
2021-12-08 20:04:07 +00:00
Writeln(' Parameters:');
Writeln(' -e <name> //Decrypted ELF or SELF file name');
Writeln(' -f <name> //Folder of app (/app0)');
Writeln(' -p <name> //Folder of patch (/app1)');
Writeln(' -s <name> //Savedata path');
Writeln(' -w //Fullscreen mode');
Writeln(' -pad <name> //Gamepad interface selection (xinput,sdl2,keyboard) default:xinput');
Writeln(' -led <clr> //Initial LED color of Gamepad ($rrggbb)');
Writeln(' -h <name> //enable hack');
Writeln(' DEPTH_DISABLE_HACK //Disables depth buffer');
Writeln(' COMPUTE_DISABLE_HACK //Disables compute shaders');
Writeln(' MEMORY_BOUND_HACK //Limits the amount of GPU allocated memory (iGPU)');
Writeln(' IMAGE_TEST_HACK //Always marks that the texture has changed');
Writeln(' IMAGE_LOAD_HACK //Never reload textures (improves performance on many games)');
Writeln(' DISABLE_SRGB_HACK //Disables hacked SRGB display');
Writeln(' DISABLE_FMV_HACK //Disables in-game movies');
2023-01-26 13:54:36 +00:00
Writeln(' SKIP_UNKNOW_TILING //Skip unknown tiling texture types');
2022-10-14 09:20:02 +00:00
2021-12-08 20:04:07 +00:00
Exit(False);
end;
n:=-1;
For i:=1 to ParamCount do
begin
case LowerCase(ParamStr(i)) of
'-e':n:=0;
'-f':n:=1;
'-p':n:=2;
'-s':n:=3;
'-h':n:=4;
'-w':ps4_libSceVideoOut.FULLSCREEN_MODE:=True;
'-pad':n:=5;
'-led':n:=6;
2021-12-08 20:04:07 +00:00
else
if (n<>-1) then
begin
Case n of
0:begin
2022-11-09 13:46:13 +00:00
if (ps4_app.app0_file<>'') then Goto promo;
ps4_app.app0_file:=Trim(ParamStr(i));
if (ps4_app.app0_path='') then
2021-12-08 20:04:07 +00:00
begin
2022-11-09 13:46:13 +00:00
ps4_app.app0_path:=ExtractFileDir(ps4_app.app0_file);
if (ExcludeLeadingPathDelimiter(ps4_app.app0_path)='') then ps4_app.app0_path:=GetCurrentDir;
2021-12-08 20:04:07 +00:00
end;
end;
1:begin
2022-11-09 13:46:13 +00:00
ps4_app.app0_path:=Trim(ParamStr(i));
if (ExcludeLeadingPathDelimiter(ps4_app.app0_path)='') then ps4_app.app0_path:=GetCurrentDir;
2021-12-08 20:04:07 +00:00
end;
2:begin
2022-11-09 13:46:13 +00:00
ps4_app.app1_path:=Trim(ParamStr(i));
if (ExcludeLeadingPathDelimiter(ps4_app.app1_path)='') then ps4_app.app1_path:=GetCurrentDir;
2021-12-08 20:04:07 +00:00
end;
2022-10-14 09:20:02 +00:00
3:begin
2022-11-09 13:46:13 +00:00
ps4_app.save_path:=Trim(ParamStr(i));
end;
5:begin
select_pad_interface(Trim(ParamStr(i)));
end;
6:begin
select_led_color(Trim(ParamStr(i)));
end;
2022-11-09 13:46:13 +00:00
4:begin
2022-10-14 09:20:02 +00:00
case UpperCase(ParamStr(i)) of
'DEPTH_DISABLE_HACK' :ps4_videodrv.DEPTH_DISABLE_HACK:=True;
'COMPUTE_DISABLE_HACK':ps4_videodrv.COMPUTE_DISABLE_HACK:=True;
'MEMORY_BOUND_HACK' :vMemory.MEMORY_BOUND_HACK:=True;
'IMAGE_TEST_HACK' :vImageManager.IMAGE_TEST_HACK:=True;
'IMAGE_LOAD_HACK' :vImageManager.IMAGE_LOAD_HACK:=True;
2022-12-09 18:58:15 +00:00
'DISABLE_SRGB_HACK' :vFlip.SRGB_HACK:=False;
'DISABLE_FMV_HACK' :ps4_libsceavplayer.DISABLE_FMV_HACK:=True;
2023-01-26 13:54:36 +00:00
'SKIP_UNKNOW_TILING' :vImageTiling.SKIP_UNKNOW_TILING:=True;
2022-10-14 09:20:02 +00:00
else;
end;
end;
2021-12-08 20:04:07 +00:00
end;
n:=-1;
end;
end;
end;
2022-11-09 13:46:13 +00:00
if (ps4_app.app0_file='') or (ps4_app.app0_path='') or (ps4_app.save_path='') then Goto promo;
if (ps4_app.app1_path=ps4_app.app0_path) then
begin
ps4_app.app1_path:='';
end;
if not FileExists(ps4_app.app0_file) then
begin
Writeln(StdErr,'File not found:',ps4_app.app0_file);
Writeln;
Goto promo;
end;
2021-12-08 20:04:07 +00:00
2022-11-09 13:46:13 +00:00
if not DirectoryExists(ps4_app.app0_path) then
2021-12-08 20:04:07 +00:00
begin
2022-11-09 13:46:13 +00:00
Writeln(StdErr,'Path not found:',ps4_app.app0_path);
2021-12-08 20:04:07 +00:00
Writeln;
Goto promo;
end;
2022-11-09 13:46:13 +00:00
if (ps4_app.app1_path<>'') then
if not DirectoryExists(ps4_app.app1_path) then
2021-12-08 20:04:07 +00:00
begin
2022-11-09 13:46:13 +00:00
Writeln(StdErr,'Path not found:',ps4_app.app1_path);
2021-12-08 20:04:07 +00:00
Writeln;
Goto promo;
end;
Result:=True;
end;
{
type
_TElf_node=class(TElf_node)
end;
procedure Print_libs(node:TElf_node);
var
i,l:SizeInt;
lib:PLIBRARY;
begin
l:=Length(_TElf_node(node).aLibs);
if (l<>0) then
begin
For i:=0 to l-1 do
begin
lib:=_TElf_node(node).aLibs[i];
Writeln(hexStr(lib));
if lib<>nil then
Writeln(lib^.Import,' ',lib^.strName);
end;
end;
end;
}
var
Stub:TStubMemoryProc;
procedure _nop_stub; assembler; nostackframe;
asm
xor %rax,%rax
end;
2022-05-31 07:20:10 +00:00
procedure print_stub(nid:QWORD;lib:PLIBRARY); MS_ABI_Default;
2021-12-08 20:04:07 +00:00
begin
Writeln(StdErr,SysLogPrefix,'nop nid:',lib^.strName,':',HexStr(nid,16),':',ps4libdoc.GetFunctName(nid));
2022-06-03 22:23:50 +00:00
//DebugBreak;
2022-05-31 07:20:10 +00:00
Sleep(INFINITE);
2021-12-08 20:04:07 +00:00
//readln;
//Print_libs(ps4_app.GetFile('libc.prx'));
end;
function ResolveImport(elf:Telf_file;Info:PResolveImportInfo;data:Pointer):Pointer;
var
lib:PLIBRARY;
begin
Result:=nil;
//cache
Result:=Info^.lib^.get_proc(Info^.nid);
if (Result<>nil) then
begin
//Writeln('Cache^:',Info^.lib^.strName,':',ps4libdoc.GetFunctName(Info^.Nid));
Exit;
end;
lib:=ps4_app.GetLib(Info^.lib^.strName);
if (lib<>nil) then
begin
Result:=lib^.get_proc(Info^.Nid);
end;
2022-10-12 06:49:52 +00:00
//Writeln('Resolve:',Info^.lib^.strName,':',ps4libdoc.GetFunctName(Info^.Nid));
2022-05-31 07:20:10 +00:00
2022-09-07 10:57:39 +00:00
{
2022-10-25 19:43:08 +00:00
if (Result<>nil) and ((Info^.sType=STT_FUN) or (Info^.sType=STT_SCE)) then //trace
2022-06-03 22:23:50 +00:00
begin
2022-09-07 10:57:39 +00:00
Case Info^.lib^.strName of
2022-11-01 14:26:18 +00:00
'libSceNpToolkit':;
2022-10-19 13:02:47 +00:00
'libSceSaveDataDialog':;
'libSceVideoOut':;
2022-09-20 17:57:07 +00:00
'libSceSystemService':;
'libSceUserService':;
'libSceNetCtl':;
'libSceNpManager':;
'libSceNpManagerForToolkit':;
'libScePad':;
'libSceFios2':;
2022-09-07 10:57:39 +00:00
'libSceGnmDriver':;
'libSceAjm':;
'libSceAudioOut':;
'libc':;
'libSceLibcInternal':;
2022-10-27 08:25:00 +00:00
'libScePosix':;
'libSceDiscMap':;
2022-09-07 10:57:39 +00:00
else
Case RawByteString(ps4libdoc.GetFunctName(Info^.Nid)) of
2022-10-19 13:02:47 +00:00
'scePadGetControllerInformation':;
'scePadRead':;
'scePadSetVibration':;
'sceUserServiceGetLoginUserIdList':;
'sceSystemServiceGetStatus':;
'sceKernelDebugRaiseException':;
'sceKernelDebugRaiseExceptionOnReleaseMode':;
'_sigprocmask':;
'__error':;
2022-10-12 06:49:52 +00:00
'sceKernelClearEventFlag':;
'sceKernelWaitEventFlag':;
'sceKernelSetEventFlag':;
'sceNetCtlCheckCallbackForNpToolkit':;
'sceKernelReadTsc':;
2022-10-19 13:02:47 +00:00
'scePthreadCondSignal':;
'scePthreadCondTimedwait':;
2022-09-20 17:57:07 +00:00
'scePthreadYield':;
'nanosleep':;
2022-10-27 08:25:00 +00:00
'sceKernelGetProcessTime':;
2022-09-07 10:57:39 +00:00
'sceKernelGetProcessTimeCounter':;
2022-09-20 17:57:07 +00:00
'clock_gettime':;
'pthread_mutex_init':;
'sceKernelLseek':;
'scePthreadMutexDestroy':;
'sceKernelRead':;
2022-09-07 10:57:39 +00:00
'sceKernelSignalSema':;
'sceKernelWaitSema':;
'scePthreadMutexInit':;
'scePthreadMutexattrInit':;
'scePthreadMutexattrDestroy':;
'scePthreadMutexattrSettype':;
'scePthreadMutexTrylock':;
'scePthreadMutexLock':;
'scePthreadMutexUnlock':;
'pthread_self':;
'scePthreadSelf':;
'scePthreadEqual':;
'sceKernelGettimeofday':;
'sceKernelClockGettime':;
'pthread_mutex_lock':;
'pthread_mutex_unlock':;
'sceKernelPread':;
'sceKernelClose':;
'sceDiscMapIsRequestOnHDD':;
2022-09-20 17:57:07 +00:00
//'Unknow':;
2022-09-07 10:57:39 +00:00
'sceFiosIOFilterPsarcDearchiver':;
'sceFiosFHReadSync':;
'sceFiosFHTell':;
2022-11-09 13:46:13 +00:00
'sceNgs2VoiceGetState':;
'sceNgs2SystemRender':;
2022-09-07 10:57:39 +00:00
'sceAudioOutOutputs':;
'__tls_get_addr':;
'scePthreadRwlockRdlock':;
'scePthreadRwlockUnlock':;
2022-10-19 13:02:47 +00:00
'scePthreadCondBroadcast':;
2022-09-07 10:57:39 +00:00
'sceFiosFHCloseSync':;
'sceKernelStat':;
'sceKernelOpen':;
'sceKernelUsleep':;
'_write':;
else
begin
Result:=TStubMemoryTrace(Stub).NewTraceStub(Info^.Nid,Info^.lib,Result,@_trace_enter,@_trace_exit);
end;
end;
end;
2022-06-03 22:23:50 +00:00
end;
2022-09-07 10:57:39 +00:00
}
2022-05-31 07:20:10 +00:00
2021-12-08 20:04:07 +00:00
if (Result=nil) then
begin
2022-10-25 19:43:08 +00:00
if (Info^.sType=STT_FUN) or (Info^.sType=STT_SCE) then
2021-12-08 20:04:07 +00:00
begin
Result:=Stub.NewNopStub(Info^.Nid,Info^.lib,@print_stub);
end else
2022-10-25 19:43:08 +00:00
if (Info^.sBind<>STB_WEAK) then
2021-12-08 20:04:07 +00:00
begin
2023-01-24 17:33:12 +00:00
Writeln(StdWrn,'[Warn]:',Info^.lib^.strName,':',ps4libdoc.GetFunctName(Info^.Nid),':',HexStr(Info^.Nid,16));
2021-12-08 20:04:07 +00:00
end;
2022-10-07 14:21:05 +00:00
2021-12-08 20:04:07 +00:00
end;
if (Result<>nil) then //cache
begin
Info^.lib^.set_proc(Info^.nid,Result);
end;
end;
2022-05-31 07:20:10 +00:00
function ReloadImport(elf:Telf_file;Info:PResolveImportInfo;data:Pointer):Pointer;
var
node:Telf_file;
lib:PLIBRARY;
begin
//prev
Result:=Info^.lib^.get_proc(Info^.nid);
node:=Telf_file(data);
lib:=ps4_app.GetLib(Info^.lib^.strName);
2022-12-15 13:42:38 +00:00
if (lib=nil) then Exit(nil); //Don't reload!
2022-05-31 07:20:10 +00:00
2022-12-15 13:42:38 +00:00
if (lib^.parent<>node) then Exit(nil); //Don't reload!
2022-05-31 07:20:10 +00:00
Result:=lib^.get_proc(Info^.Nid);
2022-12-15 13:42:38 +00:00
if (Result=nil) then
begin
2023-01-24 17:33:12 +00:00
Writeln(StdWrn,'[Warn]:',Info^.lib^.strName,':',ps4libdoc.GetFunctName(Info^.Nid),':',HexStr(Info^.Nid,16));
2022-12-15 13:42:38 +00:00
Exit(nil); //Don't reload!
end;
if (Result<>nil) then //cache
begin
Info^.lib^.set_proc(Info^.nid,Result);
end;
2022-05-31 07:20:10 +00:00
end;
2022-11-09 13:46:13 +00:00
var
main:pthread;
{$R *.res}
procedure LoadProgram;
2021-12-08 20:04:07 +00:00
var
elf:Telf_file;
2022-11-09 13:46:13 +00:00
f:RawByteString;
begin
elf:=nil;
2021-12-08 20:04:07 +00:00
2022-11-09 13:46:13 +00:00
if (ps4_app.app1_path<>'') then
begin
//first try patch
f:='';
if (parse_filename('/app1/eboot.bin',f)=PT_FILE) then
begin
elf:=Telf_file(LoadPs4ElfFromFile(f));
end;
end;
2021-12-08 20:04:07 +00:00
2022-11-09 13:46:13 +00:00
if (elf=nil) then
begin
//second try app0_file
elf:=Telf_file(LoadPs4ElfFromFile(ps4_app.app0_file));
end;
2021-12-08 20:04:07 +00:00
2022-11-09 13:46:13 +00:00
Assert(elf<>nil,'program not loaded!');
ps4_app.prog:=elf;
end;
2022-09-05 13:52:38 +00:00
2021-12-08 20:04:07 +00:00
begin
DefaultSystemCodePage:=CP_UTF8;
DefaultUnicodeCodePage:=CP_UTF8;
DefaultFileSystemCodePage:=CP_UTF8;
DefaultRTLFileSystemCodePage:=CP_UTF8;
UTF8CompareLocale:=CP_UTF8;
2023-01-14 14:43:57 +00:00
2022-07-13 14:01:22 +00:00
sys_crt_init;
2021-12-08 20:04:07 +00:00
2023-01-14 14:43:57 +00:00
if not cpu.AVX2Support then
begin
2023-01-24 17:33:12 +00:00
Writeln(StdErr,'AVX2 not support!');
2023-01-14 15:11:22 +00:00
Assert(false,'AVX2 not supported!');
Exit;
2023-01-14 14:43:57 +00:00
end;
2021-12-08 20:04:07 +00:00
ps4_app.save_path:=IncludeTrailingPathDelimiter(GetCurrentDir)+'savedata';
if not ParseCmd then Exit;
2022-11-09 13:46:13 +00:00
ps4_app.resolve_cb:=@ResolveImport;
ps4_app.reload_cb :=@ReloadImport;
LoadProgram;
ps4_app.prog.Prepare;
ps4_app.RegistredElf (ps4_app.prog);
ps4_app.ResolveDepended (ps4_app.prog);
2022-05-31 07:20:10 +00:00
ps4_app.LoadSymbolImport(nil);
2021-12-08 20:04:07 +00:00
Stub.FinStub;
ps4_app.InitProt;
2022-09-28 20:09:07 +00:00
_pthread_run_entry(@main,GetSceUserMainThreadName,GetSceUserMainThreadStackSize);
2021-12-08 20:04:07 +00:00
ps4_libSceVideoOut.App_Run;
//KillALLThreads TODO
//readln;
end.