-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathfpPS4.lpr
506 lines (447 loc) · 11.5 KB
/
fpPS4.lpr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
uses
{$IFDEF Linux}
cmem,
cthreads,
{$ENDIF}
cpu,
windows,
seh64,
Classes,
sysutils,
stub_manager,
sys_crt,
sys_types,
sys_pthread,
sys_path,
sys_kernel,
ps4libdoc,
ps4_libkernel,
ps4_libSceLibcInternal,
ps4_libSceScreenShot,
ps4_libSceRtc,
ps4_libSceNpSessionSignaling,
ps4_libSceNpSignaling,
ps4_libSceNpMatching2,
ps4_libSceRemoteplay,
ps4_libSceAjm,
ps4_libSceMouse,
ps4_libSceIme,
ps4_libSceLncUtil,
ps4_libSceMove,
ps4_libSceMoveTracker,
ps4_libScePlayGo,
ps4_libSceDiscMap,
ps4_libSceAppContent,
ps4_libSceNet,
ps4_libSceHttp,
ps4_libSceGnmDriver,
ps4_libSceNpScore,
ps4_libSceNpTrophy,
ps4_libSceSystemService,
ps4_libSceSystemGesture,
ps4_libSceNpUtility,
ps4_libSceNpCommon,
ps4_libSceNpManager,
ps4_libSceNpGameIntent,
ps4_libSceNpAuth,
ps4_libSceNpParty,
ps4_libSceSaveData,
ps4_libSceDialogs,
ps4_libSceUserService,
ps4_libSceAudioOut,
ps4_libSceAudio3d,
ps4_libSceVoice,
ps4_libSceVideoOut,
ps4_libSceAvPlayer,
ps4_libSceAudiodec,
ps4_libScePad,
ps4_libSceNpWebApi,
ps4_libSceRudp,
ps4_libSceRandom,
ps4_libSceComposite,
ps4_libSceSysCore,
ps4_libSceSsl,
ps4_libSceFiber,
ps4_libSceUlt,
ps4_libSceGameLiveStreaming,
ps4_libSceSharePlay,
ps4_libSceShareUtility,
ps4_libSceSocialScreen,
ps4_libSceVideoRecording,
ps4_libSceCompanionHttpd,
ps4_libSceCompanionUtil,
ps4_libSceContentExport,
ps4_libSceConvertKeycode,
ps4_libSceUsbd,
ps4_libSceUsbStorage,
ps4_libSceAudiodecCpu,
ps4_libSceDepth,
ps4_libSceNpTus,
ps4_libSceLoginService,
ps4_libSceHmd,
ps4_libSceVrTracker,
ps4_libSceCamera,
ps4_elf,
ps4_pthread,
ps4_program,
ps4_videodrv,
vMemory,
vImageManager,
vImageTiling,
vFlip,
trace_manager;
function ParseCmd:Boolean;
var
i,n:Integer;
label
promo;
begin
if (ParamCount=0) then
begin
promo:
Writeln('fpPS4 (',{$I tag.inc},')');
Writeln('Copyright (c) 2021-2023 by red-prig');
Writeln('PS4 compatibility layer (emulator) written with Free Pascal '+{$I %FPCVERSION%});
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');
Writeln(' SKIP_UNKNOW_TILING //Skip unknown tiling texture types');
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;
else
if (n<>-1) then
begin
Case n of
0:begin
if (ps4_app.app0_file<>'') then Goto promo;
ps4_app.app0_file:=Trim(ParamStr(i));
if (ps4_app.app0_path='') then
begin
ps4_app.app0_path:=ExtractFileDir(ps4_app.app0_file);
if (ExcludeLeadingPathDelimiter(ps4_app.app0_path)='') then ps4_app.app0_path:=GetCurrentDir;
end;
end;
1:begin
ps4_app.app0_path:=Trim(ParamStr(i));
if (ExcludeLeadingPathDelimiter(ps4_app.app0_path)='') then ps4_app.app0_path:=GetCurrentDir;
end;
2:begin
ps4_app.app1_path:=Trim(ParamStr(i));
if (ExcludeLeadingPathDelimiter(ps4_app.app1_path)='') then ps4_app.app1_path:=GetCurrentDir;
end;
3:begin
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;
4:begin
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;
'DISABLE_SRGB_HACK' :vFlip.SRGB_HACK:=False;
'DISABLE_FMV_HACK' :ps4_libsceavplayer.DISABLE_FMV_HACK:=True;
'SKIP_UNKNOW_TILING' :vImageTiling.SKIP_UNKNOW_TILING:=True;
else;
end;
end;
end;
n:=-1;
end;
end;
end;
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;
if not DirectoryExists(ps4_app.app0_path) then
begin
Writeln(StdErr,'Path not found:',ps4_app.app0_path);
Writeln;
Goto promo;
end;
if (ps4_app.app1_path<>'') then
if not DirectoryExists(ps4_app.app1_path) then
begin
Writeln(StdErr,'Path not found:',ps4_app.app1_path);
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;
procedure print_stub(nid:QWORD;lib:PLIBRARY); MS_ABI_Default;
begin
Writeln(StdErr,SysLogPrefix,'nop nid:',lib^.strName,':',HexStr(nid,16),':',ps4libdoc.GetFunctName(nid));
//DebugBreak;
Sleep(INFINITE);
//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;
//Writeln('Resolve:',Info^.lib^.strName,':',ps4libdoc.GetFunctName(Info^.Nid));
{
if (Result<>nil) and ((Info^.sType=STT_FUN) or (Info^.sType=STT_SCE)) then //trace
begin
Case Info^.lib^.strName of
'libSceNpToolkit':;
'libSceSaveDataDialog':;
'libSceVideoOut':;
'libSceSystemService':;
'libSceUserService':;
'libSceNetCtl':;
'libSceNpManager':;
'libSceNpManagerForToolkit':;
'libScePad':;
'libSceFios2':;
'libSceGnmDriver':;
'libSceAjm':;
'libSceAudioOut':;
'libc':;
'libSceLibcInternal':;
'libScePosix':;
'libSceDiscMap':;
else
Case RawByteString(ps4libdoc.GetFunctName(Info^.Nid)) of
'scePadGetControllerInformation':;
'scePadRead':;
'scePadSetVibration':;
'sceUserServiceGetLoginUserIdList':;
'sceSystemServiceGetStatus':;
'sceKernelDebugRaiseException':;
'sceKernelDebugRaiseExceptionOnReleaseMode':;
'_sigprocmask':;
'__error':;
'sceKernelClearEventFlag':;
'sceKernelWaitEventFlag':;
'sceKernelSetEventFlag':;
'sceNetCtlCheckCallbackForNpToolkit':;
'sceKernelReadTsc':;
'scePthreadCondSignal':;
'scePthreadCondTimedwait':;
'scePthreadYield':;
'nanosleep':;
'sceKernelGetProcessTime':;
'sceKernelGetProcessTimeCounter':;
'clock_gettime':;
'pthread_mutex_init':;
'sceKernelLseek':;
'scePthreadMutexDestroy':;
'sceKernelRead':;
'sceKernelSignalSema':;
'sceKernelWaitSema':;
'scePthreadMutexInit':;
'scePthreadMutexattrInit':;
'scePthreadMutexattrDestroy':;
'scePthreadMutexattrSettype':;
'scePthreadMutexTrylock':;
'scePthreadMutexLock':;
'scePthreadMutexUnlock':;
'pthread_self':;
'scePthreadSelf':;
'scePthreadEqual':;
'sceKernelGettimeofday':;
'sceKernelClockGettime':;
'pthread_mutex_lock':;
'pthread_mutex_unlock':;
'sceKernelPread':;
'sceKernelClose':;
'sceDiscMapIsRequestOnHDD':;
//'Unknow':;
'sceFiosIOFilterPsarcDearchiver':;
'sceFiosFHReadSync':;
'sceFiosFHTell':;
'sceNgs2VoiceGetState':;
'sceNgs2SystemRender':;
'sceAudioOutOutputs':;
'__tls_get_addr':;
'scePthreadRwlockRdlock':;
'scePthreadRwlockUnlock':;
'scePthreadCondBroadcast':;
'sceFiosFHCloseSync':;
'sceKernelStat':;
'sceKernelOpen':;
'sceKernelUsleep':;
'_write':;
else
begin
Result:=TStubMemoryTrace(Stub).NewTraceStub(Info^.Nid,Info^.lib,Result,@_trace_enter,@_trace_exit);
end;
end;
end;
end;
}
if (Result=nil) then
begin
if (Info^.sType=STT_FUN) or (Info^.sType=STT_SCE) then
begin
Result:=Stub.NewNopStub(Info^.Nid,Info^.lib,@print_stub);
end else
if (Info^.sBind<>STB_WEAK) then
begin
Writeln(StdWrn,'[Warn]:',Info^.lib^.strName,':',ps4libdoc.GetFunctName(Info^.Nid),':',HexStr(Info^.Nid,16));
end;
end;
if (Result<>nil) then //cache
begin
Info^.lib^.set_proc(Info^.nid,Result);
end;
end;
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);
if (lib=nil) then Exit(nil); //Don't reload!
if (lib^.parent<>node) then Exit(nil); //Don't reload!
Result:=lib^.get_proc(Info^.Nid);
if (Result=nil) then
begin
Writeln(StdWrn,'[Warn]:',Info^.lib^.strName,':',ps4libdoc.GetFunctName(Info^.Nid),':',HexStr(Info^.Nid,16));
Exit(nil); //Don't reload!
end;
if (Result<>nil) then //cache
begin
Info^.lib^.set_proc(Info^.nid,Result);
end;
end;
var
main:pthread;
{$R *.res}
procedure LoadProgram;
var
elf:Telf_file;
f:RawByteString;
begin
elf:=nil;
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;
if (elf=nil) then
begin
//second try app0_file
elf:=Telf_file(LoadPs4ElfFromFile(ps4_app.app0_file));
end;
Assert(elf<>nil,'program not loaded!');
ps4_app.prog:=elf;
end;
begin
DefaultSystemCodePage:=CP_UTF8;
DefaultUnicodeCodePage:=CP_UTF8;
DefaultFileSystemCodePage:=CP_UTF8;
DefaultRTLFileSystemCodePage:=CP_UTF8;
UTF8CompareLocale:=CP_UTF8;
sys_crt_init;
if not cpu.AVX2Support then
begin
Writeln(StdErr,'AVX2 not support!');
Assert(false,'AVX2 not supported!');
Exit;
end;
ps4_app.save_path:=IncludeTrailingPathDelimiter(GetCurrentDir)+'savedata';
if not ParseCmd then Exit;
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);
ps4_app.LoadSymbolImport(nil);
Stub.FinStub;
ps4_app.InitProt;
_pthread_run_entry(@main,GetSceUserMainThreadName,GetSceUserMainThreadStackSize);
ps4_libSceVideoOut.App_Run;
//KillALLThreads TODO
//readln;
end.