Seregon/ShadPKG

A tool for deriving PKG packet encryption keys for ps4 written in c++

C++/47.3 KB/No license
ps4MEL/ps4_runtime_dispatch.cpp
ShadPKG / ps4MEL / ps4_runtime_dispatch.cpp
1#include "ps4_runtime_dispatch.h"
2#include "sdl_backend.h"
3#include <iostream>
4#include <unistd.h>
5 
6namespace PS4Runtime {
7 
8// ═══════════════════════════════════════════════════════════════════════════
9// Backend implementations for known PS4 functions
10// ═══════════════════════════════════════════════════════════════════════════
11 
12// GNM: Submit command buffers (placeholder - logs and returns success)
13static int64_t Backend_GNM_SubmitCommandBuffers(void* obj, uint64_t* args, int argCount) {
14 static uint64_t callCount = 0;
15 callCount++;
16 if (callCount % 60 == 1) {
17 std::cout << "[GNM] SubmitCommandBuffers #" << callCount << std::endl;
18 }
19 return 0; // ORBIS_OK
20}
21 
22// GNM: Submit and flip
23static int64_t Backend_GNM_SubmitAndFlipCommandBuffers(void* obj, uint64_t* args, int argCount) {
24 static uint64_t callCount = 0;
25 callCount++;
26
27 // Trigger SDL present
28 PS4Emu::SDL::PollEvents();
29 PS4Emu::SDL::Present();
30
31 if (callCount % 60 == 1) {
32 std::cout << "[GNM] SubmitAndFlipCommandBuffers #" << callCount << std::endl;
33 }
34 return 0;
35}
36 
37// GNM: Clear render target
38static int64_t Backend_GNM_ClearRenderTarget(void* obj, uint64_t* args, int argCount) {
39 // Use SDL to clear with a color
40 static uint8_t hue = 0;
41 hue += 2;
42
43 // Simple color cycling to show something is happening
44 uint8_t r = (hue < 85) ? (255 - hue * 3) : ((hue < 170) ? 0 : ((hue - 170) * 3));
45 uint8_t g = (hue < 85) ? (hue * 3) : ((hue < 170) ? (255 - (hue - 85) * 3) : 0);
46 uint8_t b = (hue < 85) ? 0 : ((hue < 170) ? ((hue - 85) * 3) : (255 - (hue - 170) * 3));
47
48 PS4Emu::SDL::ClearScreen(r, g, b);
49 return 0;
50}
51 
52// VideoOut: Submit flip
53static int64_t Backend_VideoOut_SubmitFlip(void* obj, uint64_t* args, int argCount) {
54 PS4Emu::SDL::PollEvents();
55 PS4Emu::SDL::Present();
56 return 0;
57}
58 
59// VideoOut: Open
60static int64_t Backend_VideoOut_Open(void* obj, uint64_t* args, int argCount) {
61 // Already initialized via runtime_init
62 return 1; // Return handle
63}
64 
65// VideoOut: Close
66static int64_t Backend_VideoOut_Close(void* obj, uint64_t* args, int argCount) {
67 return 0;
68}
69 
70// VideoOut: Register buffers
71static int64_t Backend_VideoOut_RegisterBuffers(void* obj, uint64_t* args, int argCount) {
72 std::cout << "[VideoOut] RegisterBuffers called" << std::endl;
73 return 0;
74}
75 
76// Pad: Read
77static int64_t Backend_Pad_Read(void* obj, uint64_t* args, int argCount) {
78 // Get current pad state from SDL
79 auto state = PS4Emu::SDL::GetPadState();
80 // The actual data would be written to args[1] (pData)
81 return 1; // Return 1 = success with 1 data packet
82}
83 
84// Kernel: Usleep
85static int64_t Backend_Kernel_Usleep(void* obj, uint64_t* args, int argCount) {
86 uint32_t usec = static_cast<uint32_t>(args[0]);
87 if (usec > 0 && usec < 1000000) {
88 usleep(usec);
89 }
90 return 0;
91}
92 
93// ═══════════════════════════════════════════════════════════════════════════
94// Initialize dispatch system with known functions
95// ═══════════════════════════════════════════════════════════════════════════
96void InitializeDispatch() {
97 auto& registry = RuntimeRegistry::Instance();
98
99 // Register semantic names
100 registry.RegisterBackend(SemanticID::GNM_SubmitCommandBuffers, Backend_GNM_SubmitCommandBuffers);
101 registry.RegisterBackend(SemanticID::GNM_SubmitAndFlipCommandBuffers, Backend_GNM_SubmitAndFlipCommandBuffers);
102 registry.RegisterBackend(SemanticID::GNM_ClearRenderTarget, Backend_GNM_ClearRenderTarget);
103 registry.RegisterBackend(SemanticID::VideoOut_SubmitFlip, Backend_VideoOut_SubmitFlip);
104 registry.RegisterBackend(SemanticID::VideoOut_Open, Backend_VideoOut_Open);
105 registry.RegisterBackend(SemanticID::VideoOut_Close, Backend_VideoOut_Close);
106 registry.RegisterBackend(SemanticID::VideoOut_RegisterBuffers, Backend_VideoOut_RegisterBuffers);
107 registry.RegisterBackend(SemanticID::Pad_Read, Backend_Pad_Read);
108 registry.RegisterBackend(SemanticID::Kernel_Usleep, Backend_Kernel_Usleep);
109
110 std::cout << "[DISPATCH] Runtime dispatch system initialized" << std::endl;
111}
112 
113} // namespace PS4Runtime
114 
115// ═══════════════════════════════════════════════════════════════════════════
116// C-compatible wrappers (used by decompiled code)
117// ═══════════════════════════════════════════════════════════════════════════
118extern "C" {
119 
120int64_t ps4_vtable_dispatch(void* obj, uint64_t offset,
121 uint64_t a1, uint64_t a2, uint64_t a3,
122 uint64_t a4, uint64_t a5, uint64_t a6) {
123 return PS4Runtime::ps4_vtable_dispatch(obj, offset, a1, a2, a3, a4, a5, a6);
124}
125 
126int64_t ps4_indirect_dispatch(void* fnPtr,
127 uint64_t a1, uint64_t a2, uint64_t a3,
128 uint64_t a4, uint64_t a5, uint64_t a6) {
129 return PS4Runtime::ps4_indirect_dispatch(fnPtr, a1, a2, a3, a4, a5, a6);
130}
131 
132}
133